-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
95 changed files
with
4,005 additions
and
847 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// 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. | ||
|
||
#include "paddle/fluid/distributed/table/graph_edge.h" | ||
#include <cstring> | ||
namespace paddle { | ||
namespace distributed { | ||
|
||
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) { | ||
id_arr.push_back(id); | ||
weight_arr.push_back(weight); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// 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. | ||
|
||
#pragma once | ||
#include <cstddef> | ||
#include <cstdint> | ||
#include <vector> | ||
namespace paddle { | ||
namespace distributed { | ||
|
||
class GraphEdgeBlob { | ||
public: | ||
GraphEdgeBlob() {} | ||
virtual ~GraphEdgeBlob() {} | ||
size_t size() { return id_arr.size(); } | ||
virtual void add_edge(uint64_t id, float weight); | ||
uint64_t get_id(int idx) { return id_arr[idx]; } | ||
virtual float get_weight(int idx) { return 1; } | ||
|
||
protected: | ||
std::vector<uint64_t> id_arr; | ||
}; | ||
|
||
class WeightedGraphEdgeBlob : public GraphEdgeBlob { | ||
public: | ||
WeightedGraphEdgeBlob() {} | ||
virtual ~WeightedGraphEdgeBlob() {} | ||
virtual void add_edge(uint64_t id, float weight); | ||
virtual float get_weight(int idx) { return weight_arr[idx]; } | ||
|
||
protected: | ||
std::vector<float> weight_arr; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// 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. | ||
|
||
#include "paddle/fluid/distributed/table/graph_node.h" | ||
#include <cstring> | ||
namespace paddle { | ||
namespace distributed { | ||
|
||
GraphNode::~GraphNode() { | ||
if (sampler != nullptr) { | ||
delete sampler; | ||
sampler = nullptr; | ||
} | ||
if (edges != nullptr) { | ||
delete edges; | ||
edges = nullptr; | ||
} | ||
} | ||
|
||
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; } | ||
|
||
void Node::to_buffer(char* buffer, bool need_feature) { | ||
memcpy(buffer, &id, id_size); | ||
buffer += id_size; | ||
|
||
int feat_num = 0; | ||
memcpy(buffer, &feat_num, sizeof(int)); | ||
} | ||
|
||
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) { | ||
size += feature.size() * int_size; | ||
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) { | ||
edges = new WeightedGraphEdgeBlob(); | ||
} else { | ||
edges = new GraphEdgeBlob(); | ||
} | ||
} | ||
} | ||
void GraphNode::build_sampler(std::string sample_type) { | ||
if (sample_type == "random") { | ||
sampler = new RandomSampler(); | ||
} else if (sample_type == "weighted") { | ||
sampler = new WeightedSampler(); | ||
} | ||
sampler->build(edges); | ||
} | ||
void FeatureNode::to_buffer(char* buffer, bool need_feature) { | ||
memcpy(buffer, &id, id_size); | ||
buffer += id_size; | ||
|
||
int feat_num = 0; | ||
int feat_len; | ||
if (need_feature) { | ||
feat_num += feature.size(); | ||
memcpy(buffer, &feat_num, sizeof(int)); | ||
buffer += sizeof(int); | ||
for (int i = 0; i < feat_num; ++i) { | ||
feat_len = feature[i].size(); | ||
memcpy(buffer, &feat_len, sizeof(int)); | ||
buffer += sizeof(int); | ||
memcpy(buffer, feature[i].c_str(), feature[i].size()); | ||
buffer += feature[i].size(); | ||
} | ||
} else { | ||
memcpy(buffer, &feat_num, sizeof(int)); | ||
} | ||
} | ||
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)); | ||
buffer += sizeof(int); | ||
|
||
char str[feat_len + 1]; | ||
memcpy(str, buffer, feat_len); | ||
buffer += feat_len; | ||
str[feat_len] = '\0'; | ||
feature.push_back(std::string(str)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// 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. | ||
|
||
#pragma once | ||
#include <cstring> | ||
#include <iostream> | ||
#include <sstream> | ||
#include <vector> | ||
#include "paddle/fluid/distributed/table/graph_weighted_sampler.h" | ||
namespace paddle { | ||
namespace distributed { | ||
|
||
class Node { | ||
public: | ||
Node() {} | ||
Node(uint64_t id) : id(id) {} | ||
virtual ~Node() {} | ||
static int id_size, int_size, weight_size; | ||
uint64_t get_id() { return id; } | ||
void set_id(uint64_t id) { this->id = id; } | ||
|
||
virtual void build_edges(bool is_weighted) {} | ||
virtual void build_sampler(std::string sample_type) {} | ||
virtual void add_edge(uint64_t id, float weight) {} | ||
virtual std::vector<int> sample_k(int k) { return std::vector<int>(); } | ||
virtual uint64_t get_neighbor_id(int idx) { return 0; } | ||
virtual float get_neighbor_weight(int idx) { return 1.; } | ||
|
||
virtual int get_size(bool need_feature); | ||
virtual void to_buffer(char *buffer, bool need_feature); | ||
virtual void recover_from_buffer(char *buffer); | ||
virtual std::string get_feature(int idx) { return std::string(""); } | ||
virtual void set_feature(int idx, std::string str) {} | ||
virtual void set_feature_size(int size) {} | ||
virtual int get_feature_size() { return 0; } | ||
|
||
protected: | ||
uint64_t id; | ||
}; | ||
|
||
class GraphNode : public Node { | ||
public: | ||
GraphNode() : Node(), sampler(nullptr), edges(nullptr) {} | ||
GraphNode(uint64_t id) : Node(id), sampler(nullptr), edges(nullptr) {} | ||
virtual ~GraphNode(); | ||
virtual void build_edges(bool is_weighted); | ||
virtual void build_sampler(std::string sample_type); | ||
virtual void add_edge(uint64_t id, float weight) { | ||
edges->add_edge(id, weight); | ||
} | ||
virtual std::vector<int> sample_k(int k) { return sampler->sample_k(k); } | ||
virtual uint64_t get_neighbor_id(int idx) { return edges->get_id(idx); } | ||
virtual float get_neighbor_weight(int idx) { return edges->get_weight(idx); } | ||
|
||
protected: | ||
Sampler *sampler; | ||
GraphEdgeBlob *edges; | ||
}; | ||
|
||
class FeatureNode : public Node { | ||
public: | ||
FeatureNode() : Node() {} | ||
FeatureNode(uint64_t id) : Node(id) {} | ||
virtual ~FeatureNode() {} | ||
virtual int get_size(bool need_feature); | ||
virtual void to_buffer(char *buffer, bool need_feature); | ||
virtual void recover_from_buffer(char *buffer); | ||
virtual std::string get_feature(int idx) { | ||
if (idx < (int)this->feature.size()) { | ||
return this->feature[idx]; | ||
} else { | ||
return std::string(""); | ||
} | ||
} | ||
|
||
virtual void set_feature(int idx, std::string str) { | ||
if (idx >= (int)this->feature.size()) { | ||
this->feature.resize(idx + 1); | ||
} | ||
this->feature[idx] = str; | ||
} | ||
virtual void set_feature_size(int size) { this->feature.resize(size); } | ||
virtual int get_feature_size() { return this->feature.size(); } | ||
|
||
template <typename T> | ||
static std::string parse_value_to_bytes(std::vector<std::string> feat_str) { | ||
T v; | ||
size_t Tsize = sizeof(T) * feat_str.size(); | ||
char buffer[Tsize]; | ||
for (size_t i = 0; i < feat_str.size(); i++) { | ||
std::stringstream ss(feat_str[i]); | ||
ss >> v; | ||
std::memcpy(buffer + sizeof(T) * i, (char *)&v, sizeof(T)); | ||
} | ||
return std::string(buffer, Tsize); | ||
} | ||
|
||
template <typename T> | ||
static std::vector<T> parse_bytes_to_array(std::string feat_str) { | ||
T v; | ||
std::vector<T> out; | ||
size_t start = 0; | ||
const char *buffer = feat_str.data(); | ||
while (start < feat_str.size()) { | ||
std::memcpy((char *)&v, buffer + start, sizeof(T)); | ||
start += sizeof(T); | ||
out.push_back(v); | ||
} | ||
return out; | ||
} | ||
|
||
protected: | ||
std::vector<std::string> feature; | ||
}; | ||
} | ||
} |
Oops, something went wrong.
ceebbf9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅Congratulation! Your pull request passed all required CI. You could ask reviewer(s) to approve and merge. 🎉