Skip to content

Commit

Permalink
code style
Browse files Browse the repository at this point in the history
  • Loading branch information
seemingwang committed Mar 29, 2021
1 parent 0280122 commit fe5eb72
Show file tree
Hide file tree
Showing 41 changed files with 520 additions and 173 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/---document-issue-.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ For example: no sample code; The sample code is not helpful; The sample code not
For example:Chinese API in this doc is inconsistent with English API, including params, description, sample code, formula, etc.

#### Other
For example: The doc link is broken; The doc page is missing; Dead link in docs.
For example: The doc link is broken; The doc page is missing; Dead link in docs.
2 changes: 1 addition & 1 deletion paddle/fluid/distributed/service/graph_brpc_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

#pragma once

#include <ThreadPool.h>
#include <memory>
#include <string>
#include <vector>
#include <ThreadPool.h>

#include <utility>
#include "ThreadPool.h"
Expand Down
13 changes: 7 additions & 6 deletions paddle/fluid/distributed/table/common_graph_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ int32_t GraphTable::load_nodes(const std::string &path, std::string node_type) {
std::ifstream file(path);
std::string line;
while (std::getline(file, line)) {
count ++;
count++;
auto values = paddle::string::split_string<std::string>(line, "\t");
if (values.size() < 2) continue;
auto id = std::stoull(values[1]);
Expand Down Expand Up @@ -160,12 +160,12 @@ int32_t GraphTable::load_nodes(const std::string &path, std::string node_type) {
<< " not in feature_map.";
}
}
valid_count ++;
valid_count++;
}
}

VLOG(0) << valid_count << "/" << count << " nodes in type " <<
node_type << " are loaded successfully in " << path;
VLOG(0) << valid_count << "/" << count << " nodes in type " << node_type
<< " are loaded successfully in " << path;
return 0;
}

Expand Down Expand Up @@ -209,10 +209,11 @@ int32_t GraphTable::load_edges(const std::string &path, bool reverse_edge) {
size_t index = src_shard_id - shard_start;
shards[index].add_graph_node(src_id)->build_edges(is_weighted);
shards[index].add_neighboor(src_id, dst_id, weight);
valid_count ++;
valid_count++;
}
}
VLOG(0) << valid_count << "/" << count << " edges are loaded successfully in " << path;
VLOG(0) << valid_count << "/" << count << " edges are loaded successfully in "
<< path;

// Build Sampler j

Expand Down
5 changes: 2 additions & 3 deletions paddle/fluid/distributed/table/graph_edge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
namespace paddle {
namespace distributed {

void GraphEdgeBlob::add_edge(uint64_t id, float weight=1){
void GraphEdgeBlob::add_edge(uint64_t id, float weight = 1) {
id_arr.push_back(id);
}

void WeightedGraphEdgeBlob::add_edge(uint64_t id, float weight=1){
void WeightedGraphEdgeBlob::add_edge(uint64_t id, float weight = 1) {
id_arr.push_back(id);
weight_arr.push_back(weight);
}

}
}
37 changes: 15 additions & 22 deletions paddle/fluid/distributed/table/graph_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
namespace paddle {
namespace distributed {


GraphNode::~GraphNode() {
if (sampler != nullptr){
if (sampler != nullptr) {
delete sampler;
sampler = nullptr;
}
if (edges != nullptr){
if (edges != nullptr) {
delete edges;
edges = nullptr;
}
Expand All @@ -33,9 +32,7 @@ int Node::weight_size = sizeof(float);
int Node::id_size = sizeof(uint64_t);
int Node::int_size = sizeof(int);

int Node::get_size(bool need_feature) {
return id_size + int_size;
}
int Node::get_size(bool need_feature) { return id_size + int_size; }

void Node::to_buffer(char* buffer, bool need_feature) {
memcpy(buffer, &id, id_size);
Expand All @@ -45,36 +42,34 @@ void Node::to_buffer(char* buffer, bool need_feature) {
memcpy(buffer, &feat_num, sizeof(int));
}

void Node::recover_from_buffer(char* buffer) {
memcpy(&id, buffer, id_size);
}
void Node::recover_from_buffer(char* buffer) { memcpy(&id, buffer, id_size); }

int FeatureNode::get_size(bool need_feature) {
int size = id_size + int_size; // id, feat_num
if (need_feature){
int size = id_size + int_size; // id, feat_num
if (need_feature) {
size += feature.size() * int_size;
for (const std::string& fea: feature){
for (const std::string& fea : feature) {
size += fea.size();
}
}
return size;
}

void GraphNode::build_edges(bool is_weighted) {
if (edges == nullptr){
if (is_weighted == true){
if (edges == nullptr) {
if (is_weighted == true) {
edges = new WeightedGraphEdgeBlob();
} else {
edges = new GraphEdgeBlob();
}
}
}
void GraphNode::build_sampler(std::string sample_type) {
if (sample_type == "random"){
if (sample_type == "random") {
sampler = new RandomSampler();
} else if (sample_type == "weighted"){
} else if (sample_type == "weighted") {
sampler = new WeightedSampler();
}
}
sampler->build(edges);
}
void FeatureNode::to_buffer(char* buffer, bool need_feature) {
Expand All @@ -87,7 +82,7 @@ void FeatureNode::to_buffer(char* buffer, bool need_feature) {
feat_num += feature.size();
memcpy(buffer, &feat_num, sizeof(int));
buffer += sizeof(int);
for (int i = 0; i < feat_num; ++i){
for (int i = 0; i < feat_num; ++i) {
feat_len = feature[i].size();
memcpy(buffer, &feat_len, sizeof(int));
buffer += sizeof(int);
Expand All @@ -99,14 +94,13 @@ void FeatureNode::to_buffer(char* buffer, bool need_feature) {
}
}
void FeatureNode::recover_from_buffer(char* buffer) {

int feat_num, feat_len;
memcpy(&id, buffer, id_size);
buffer += id_size;

memcpy(&feat_num, buffer, sizeof(int));
buffer += sizeof(int);

feature.clear();
for (int i = 0; i < feat_num; ++i) {
memcpy(&feat_len, buffer, sizeof(int));
Expand All @@ -118,7 +112,6 @@ void FeatureNode::recover_from_buffer(char* buffer) {
str[feat_len] = '\0';
feature.push_back(std::string(str));
}

}
}
}
37 changes: 18 additions & 19 deletions paddle/fluid/distributed/table/weighted_sampler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,74 +14,73 @@

#include "paddle/fluid/distributed/table/weighted_sampler.h"
#include <iostream>
#include<unordered_map>
#include <unordered_map>
namespace paddle {
namespace distributed {

void RandomSampler::build(GraphEdgeBlob* edges) {
this->edges = edges;
}
void RandomSampler::build(GraphEdgeBlob *edges) { this->edges = edges; }

std::vector<int> RandomSampler::sample_k(int k) {
int n = edges->size();
if (k > n){
if (k > n) {
k = n;
}
struct timespec tn;
clock_gettime(CLOCK_REALTIME, &tn);
srand(tn.tv_nsec);
std::vector<int> sample_result;
std::unordered_map<int, int> replace_map;
while(k--){
while (k--) {
int rand_int = rand() % n;
auto iter = replace_map.find(rand_int);
if(iter == replace_map.end()){
if (iter == replace_map.end()) {
sample_result.push_back(rand_int);
}else{
} else {
sample_result.push_back(iter->second);
}

iter = replace_map.find(n - 1);
if(iter == replace_map.end()){
if (iter == replace_map.end()) {
replace_map[rand_int] = n - 1;
}else{
} else {
replace_map[rand_int] = iter->second;
}
--n;
}
return sample_result;
}

WeightedSampler::WeightedSampler(){
WeightedSampler::WeightedSampler() {
left = nullptr;
right = nullptr;
edges = nullptr;
}

WeightedSampler::~WeightedSampler() {
if(left != nullptr){
if (left != nullptr) {
delete left;
left = nullptr;
}
if(right != nullptr){
if (right != nullptr) {
delete right;
right = nullptr;
}
}

void WeightedSampler::build(GraphEdgeBlob* edges) {
if(left != nullptr){
void WeightedSampler::build(GraphEdgeBlob *edges) {
if (left != nullptr) {
delete left;
left = nullptr;
}
if(right != nullptr){
if (right != nullptr) {
delete right;
right = nullptr;
}
return build_one((WeightedGraphEdgeBlob*)edges, 0, edges->size());
return build_one((WeightedGraphEdgeBlob *)edges, 0, edges->size());
}

void WeightedSampler::build_one(WeightedGraphEdgeBlob *edges, int start, int end) {
void WeightedSampler::build_one(WeightedGraphEdgeBlob *edges, int start,
int end) {
count = 0;
this->edges = edges;
if (start + 1 == end) {
Expand Down Expand Up @@ -137,7 +136,7 @@ int WeightedSampler::sample(
if (right_count == 0 ||
left_count > 0 && left->weight - left_subtract >= query_weight) {
return_idx = left->sample(query_weight, subtract_weight_map,
subtract_count_map, subtract);
subtract_count_map, subtract);
} else {
return_idx =
right->sample(query_weight - (left->weight - left_subtract),
Expand Down
27 changes: 13 additions & 14 deletions paddle/fluid/distributed/table/weighted_sampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,38 @@ namespace paddle {
namespace distributed {

class Sampler {
public:
public:
virtual ~Sampler() {}
virtual void build(GraphEdgeBlob* edges) = 0;
virtual void build(GraphEdgeBlob *edges) = 0;
virtual std::vector<int> sample_k(int k) = 0;
};

class RandomSampler: public Sampler {
public:
class RandomSampler : public Sampler {
public:
virtual ~RandomSampler() {}
virtual void build(GraphEdgeBlob* edges);
virtual void build(GraphEdgeBlob *edges);
virtual std::vector<int> sample_k(int k);
GraphEdgeBlob* edges;
GraphEdgeBlob *edges;
};

class WeightedSampler: public Sampler {
class WeightedSampler : public Sampler {
public:
WeightedSampler();
virtual ~WeightedSampler();
WeightedSampler *left, *right;
float weight;
int count;
int idx;
GraphEdgeBlob * edges;
virtual void build(GraphEdgeBlob* edges);
GraphEdgeBlob *edges;
virtual void build(GraphEdgeBlob *edges);
virtual void build_one(WeightedGraphEdgeBlob *edges, int start, int end);
virtual std::vector<int> sample_k(int k);

private:
int sample(
float query_weight,
std::unordered_map<WeightedSampler *, float> &subtract_weight_map,
std::unordered_map<WeightedSampler *, int> &subtract_count_map,
float &subtract);
int sample(float query_weight,
std::unordered_map<WeightedSampler *, float> &subtract_weight_map,
std::unordered_map<WeightedSampler *, int> &subtract_count_map,
float &subtract);
};
}
}
14 changes: 14 additions & 0 deletions paddle/fluid/inference/api/demo_ci/clean.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -x
cd `dirname $0`
rm -rf build/ data/
Expand Down
14 changes: 14 additions & 0 deletions paddle/fluid/train/demo/run.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
#!/bin/bash

# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -x

PADDLE_ROOT=$1
Expand Down
13 changes: 13 additions & 0 deletions paddle/fluid/train/imdb_demo/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -exu
build/demo_trainer --flagfile="train.cfg"
Loading

0 comments on commit fe5eb72

Please sign in to comment.