-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tree-based-model #31696
Merged
Merged
tree-based-model #31696
Changes from 16 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
a431b31
first_commit for index_dataset
123malin 4849f6f
add basic index_sampler
123malin 16fc441
tmp
123malin 87837ac
test=develop, fix index_sampler
123malin 56c5c15
tmp, add tree_learning & tdm model(testing)
123malin d05f645
tmp, fix shared_ptr
123malin bca106f
test=develop, update
123malin f2af4d0
test=develop, update
123malin d9fd612
test=develop, update
123malin 99f48c8
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
123malin b7358d7
test=develop, add unittest
123malin 1c0a781
test=develop, fix
123malin c4b83e0
test=develop, add unittest
123malin 1904595
test=develop, syntax
123malin 51d138c
test=develop, fix py3 bug
123malin 480134b
test=develop, paddle_enforce error message optimize
123malin 23aa883
test=develop, mv index_dataset to distributed
123malin 99aeb61
test=develop, fix
123malin e425fe2
test=develop, fix
123malin b044e55
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
123malin 26c0b23
test=develop, fix
123malin b74c633
test=develop, fix
123malin cabb3d6
test=develop, rename test_tree_index
123malin 32f704c
test=develop, format
123malin 943c846
test=develop, resolve conflict
123malin 5205faa
test=develop, fix
123malin 85ee165
test=develop, update format
123malin 20c0de6
test=develop, fix
123malin f758c1f
test=develop, add python_proto
123malin 63a32ce
test=develop, fix cmake
123malin bc719d2
test=develop, fix
123malin 952ac68
test=develop, fix
123malin 91f91ba
test=develop, fix
123malin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,86 @@ | ||
// 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/framework/fleet/index_sampler.h" | ||
#include "paddle/fluid/operators/math/sampler.h" | ||
|
||
namespace paddle { | ||
namespace framework { | ||
|
||
using Sampler = paddle::operators::math::Sampler; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it necessary? |
||
|
||
std::vector<std::vector<uint64_t>> LayerWiseSampler::sample( | ||
const std::vector<std::vector<uint64_t>>& user_inputs, | ||
const std::vector<uint64_t>& target_ids, bool with_hierarchy) { | ||
auto input_num = target_ids.size(); | ||
auto user_feature_num = user_inputs[0].size(); | ||
std::vector<std::vector<uint64_t>> outputs( | ||
input_num * layer_counts_sum_, | ||
std::vector<uint64_t>(user_feature_num + 2)); | ||
|
||
auto max_layer = tree_->height(); | ||
std::vector<Sampler*> sampler_vec(max_layer - start_sample_layer_); | ||
std::vector<std::vector<uint64_t>> layer_ids(max_layer - start_sample_layer_); | ||
|
||
auto layer_index = max_layer - 1; | ||
size_t idx = 0; | ||
while (layer_index >= start_sample_layer_) { | ||
layer_ids[idx] = tree_->get_nodes_given_level(layer_index); | ||
sampler_vec[idx] = new paddle::operators::math::UniformSampler( | ||
layer_ids[idx].size() - 1, seed_); | ||
layer_index--; | ||
idx++; | ||
} | ||
|
||
auto ancestors = tree_->get_parent_path(target_ids, start_sample_layer_); | ||
idx = 0; | ||
for (size_t i = 0; i < input_num; i++) { | ||
for (size_t j = 0; j < ancestors[i].size(); j++) { | ||
// user | ||
if (j > 0 && with_hierarchy) { | ||
auto hierarchical_user = | ||
tree_->get_ancestor_given_level(user_inputs[i], max_layer - j - 1); | ||
for (int idx_offset = 0; idx_offset <= layer_counts_[j]; idx_offset++) { | ||
for (size_t k = 0; k < user_feature_num; k++) { | ||
outputs[idx + idx_offset][k] = hierarchical_user[k]; | ||
} | ||
} | ||
} else { | ||
for (int idx_offset = 0; idx_offset <= layer_counts_[j]; idx_offset++) { | ||
for (size_t k = 0; k < user_feature_num; k++) { | ||
outputs[idx + idx_offset][k] = user_inputs[i][k]; | ||
} | ||
} | ||
} | ||
|
||
// sampler ++ | ||
outputs[idx][user_feature_num] = ancestors[i][j]; | ||
outputs[idx][user_feature_num + 1] = 1.0; | ||
idx += 1; | ||
for (int idx_offset = 0; idx_offset < layer_counts_[j]; idx_offset++) { | ||
int sample_res = 0; | ||
do { | ||
sample_res = sampler_vec[j]->Sample(); | ||
} while (layer_ids[j][sample_res] == ancestors[i][j]); | ||
outputs[idx + idx_offset][user_feature_num] = layer_ids[j][sample_res]; | ||
outputs[idx + idx_offset][user_feature_num + 1] = 0; | ||
} | ||
idx += layer_counts_[j]; | ||
} | ||
} | ||
return outputs; | ||
} | ||
|
||
} // end namespace framework | ||
} // end namespace paddle |
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,100 @@ | ||
// 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 <vector> | ||
#include "paddle/fluid/framework/fleet/index_wrapper.h" | ||
#include "paddle/fluid/framework/program_desc.h" | ||
#include "paddle/fluid/platform/enforce.h" | ||
|
||
namespace paddle { | ||
namespace framework { | ||
|
||
class IndexSampler { | ||
public: | ||
virtual ~IndexSampler() {} | ||
IndexSampler() {} | ||
|
||
template <typename T> | ||
static std::shared_ptr<IndexSampler> Init(const std::string& name) { | ||
std::shared_ptr<IndexSampler> instance = nullptr; | ||
instance.reset(new T(name)); | ||
return instance; | ||
} | ||
|
||
virtual void init_layerwise_conf(const std::vector<int>& layer_sample_counts, | ||
int start_sample_layer = 1, int seed = 0) {} | ||
virtual void init_beamsearch_conf(const int64_t k) {} | ||
virtual std::vector<std::vector<uint64_t>> sample( | ||
const std::vector<std::vector<uint64_t>>& user_inputs, | ||
const std::vector<uint64_t>& input_targets, | ||
bool with_hierarchy = false) = 0; | ||
}; | ||
|
||
class LayerWiseSampler : public IndexSampler { | ||
public: | ||
virtual ~LayerWiseSampler() {} | ||
explicit LayerWiseSampler(const std::string& name) { | ||
tree_ = IndexWrapper::GetInstance()->GetTreeIndex(name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. singleton is necessary? |
||
} | ||
|
||
void init_layerwise_conf(const std::vector<int>& layer_sample_counts, | ||
int start_sample_layer, int seed) override { | ||
seed_ = seed; | ||
start_sample_layer_ = start_sample_layer; | ||
|
||
PADDLE_ENFORCE_GT( | ||
start_sample_layer_, 0, | ||
paddle::platform::errors::InvalidArgument( | ||
"start sampler layer = [%d], it should greater than 0.", | ||
start_sample_layer_)); | ||
PADDLE_ENFORCE_LT(start_sample_layer_, tree_->height(), | ||
paddle::platform::errors::InvalidArgument( | ||
"start sampler layer = [%d], it should less than " | ||
"max_layer, which is [%d].", | ||
start_sample_layer_, tree_->height())); | ||
|
||
size_t i = 0; | ||
layer_counts_sum_ = 0; | ||
layer_counts_.clear(); | ||
int cur_layer = start_sample_layer_; | ||
while (cur_layer < tree_->height()) { | ||
int layer_sample_num = 1; | ||
if (i < layer_sample_counts.size()) { | ||
layer_sample_num = layer_sample_counts[i]; | ||
} | ||
layer_counts_sum_ += layer_sample_num + 1; | ||
layer_counts_.push_back(layer_sample_num); | ||
VLOG(1) << "[INFO] level " << cur_layer | ||
<< " sample_layer_counts.push_back: " << layer_sample_num; | ||
cur_layer += 1; | ||
i += 1; | ||
} | ||
reverse(layer_counts_.begin(), layer_counts_.end()); | ||
VLOG(1) << "sample counts sum: " << layer_counts_sum_; | ||
} | ||
std::vector<std::vector<uint64_t>> sample( | ||
const std::vector<std::vector<uint64_t>>& user_inputs, | ||
const std::vector<uint64_t>& target_ids, bool with_hierarchy) override; | ||
|
||
private: | ||
std::vector<int> layer_counts_; | ||
int64_t layer_counts_sum_{0}; | ||
std::shared_ptr<TreeIndex> tree_{nullptr}; | ||
int seed_{0}; | ||
int start_sample_layer_{1}; | ||
}; | ||
|
||
} // end namespace framework | ||
} // end namespace paddle |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
namespace upgrade.