-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[XPU] add dropout bridge and unit test #2650
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
Merged
zhupengyang
merged 3 commits into
PaddlePaddle:develop
from
zhupengyang:dropout-xpu-1223
Dec 24, 2019
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,62 @@ | ||
| // Copyright (c) 2019 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 "lite/kernels/npu/bridges/registry.h" | ||
| #include "lite/kernels/xpu/bridges/graph.h" | ||
| #include "lite/kernels/xpu/bridges/utility.h" | ||
|
|
||
| namespace paddle { | ||
| namespace lite { | ||
| namespace subgraph { | ||
| namespace xpu { | ||
|
|
||
| int DropoutConverter(void* ctx, OpLite* op) { | ||
| CHECK(ctx != nullptr); | ||
| CHECK(op != nullptr); | ||
| auto graph = static_cast<Graph*>(ctx); | ||
| auto op_info = op->op_info(); | ||
| auto op_type = op_info->Type(); | ||
| VLOG(3) << "[XPU] Converting " + op_type + "..."; | ||
|
|
||
| // Create node and set params from op | ||
| auto x_var_name = op_info->Input("X").front(); | ||
| auto out_var_name = op_info->Output("Out").front(); | ||
| auto dropout_prob = op_info->GetAttr<float>("dropout_prob"); | ||
| auto dropout_implementation = | ||
| op_info->GetAttr<std::string>("dropout_implementation"); | ||
| double rate; | ||
| if (dropout_implementation == "downgrade_in_infer") { | ||
| rate = 1. - dropout_prob; | ||
| } else if (dropout_implementation == "upscale_in_train") { | ||
| rate = 1.; | ||
| } else { | ||
| LOG(FATAL) << "unsupported dropout_implementation == " | ||
| << dropout_implementation << " for dropout"; | ||
| } | ||
| CHECK(graph->HasNode(x_var_name)); | ||
| graph->AddNode( | ||
| out_var_name, | ||
| graph->builder_.CreateDropout(*graph->GetNode(x_var_name), rate)); | ||
|
|
||
| return SUCCESS; | ||
| } | ||
|
|
||
| } // namespace xpu | ||
| } // namespace subgraph | ||
| } // namespace lite | ||
| } // namespace paddle | ||
|
|
||
| REGISTER_SUBGRAPH_BRIDGE(XPU, | ||
| dropout, | ||
| paddle::lite::subgraph::xpu::DropoutConverter); |
This file contains hidden or 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 hidden or 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 hidden or 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,132 @@ | ||
| // Copyright (c) 2019 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 <gtest/gtest.h> | ||
| #include <cmath> | ||
| #include <string> | ||
| #include "lite/api/paddle_use_kernels.h" | ||
| #include "lite/api/paddle_use_ops.h" | ||
| #include "lite/core/arena/framework.h" | ||
|
|
||
| namespace paddle { | ||
| namespace lite { | ||
|
|
||
| class DropoutComputeTester : public arena::TestCase { | ||
| protected: | ||
| // common attributes for this op. | ||
| std::string type_ = "dropout"; | ||
| std::string input_ = "x"; | ||
| std::string output_ = "out"; | ||
| std::string mask_ = "mask"; | ||
| DDim dims_{{1}}; | ||
| float dropout_prob_ = 0.5; | ||
| bool fix_seed_ = true; | ||
| int seed_ = 1; | ||
| std::string dropout_implementation_ = "downgrade_in_infer"; | ||
|
|
||
| public: | ||
| DropoutComputeTester(const Place& place, | ||
| const std::string& alias, | ||
| DDim dims, | ||
| float dropout_prob, | ||
| bool fix_seed, | ||
| int seed, | ||
| std::string dropout_implementation) | ||
| : TestCase(place, alias), | ||
| dims_(dims), | ||
| dropout_prob_(dropout_prob), | ||
| fix_seed_(fix_seed), | ||
| seed_(seed), | ||
| dropout_implementation_(dropout_implementation) {} | ||
|
|
||
| void RunBaseline(Scope* scope) override { | ||
| auto* out = scope->NewTensor(output_); | ||
| CHECK(out); | ||
| out->Resize(dims_); | ||
| auto* output_data = out->mutable_data<float>(); | ||
|
|
||
| auto* x = scope->FindTensor(input_); | ||
| const auto* x_data = x->data<float>(); | ||
|
|
||
| if (dropout_implementation_ == "downgrade_in_infer") { | ||
| float rate = 1 - dropout_prob_; | ||
| for (int64_t i = 0; i < dims_.production(); i++) { | ||
| output_data[i] = x_data[i] * rate; | ||
| } | ||
| } else if (dropout_implementation_ == "upscale_in_train") { | ||
| memcpy(output_data, x_data, sizeof(float) * dims_.production()); | ||
| } else { | ||
| LOG(FATAL) << "unsupported dropout_implementation: " | ||
| << dropout_implementation_; | ||
| } | ||
| } | ||
|
|
||
| void PrepareOpDesc(cpp::OpDesc* op_desc) { | ||
| op_desc->SetType(type_); | ||
| op_desc->SetInput("X", {input_}); | ||
| op_desc->SetOutput("Out", {output_}); | ||
| op_desc->SetOutput("Mask", {mask_}); | ||
| op_desc->SetAttr("dropout_prob", dropout_prob_); | ||
| op_desc->SetAttr("fix_seed", fix_seed_); | ||
| op_desc->SetAttr("seed", seed_); | ||
| op_desc->SetAttr("dropout_implementation", dropout_implementation_); | ||
| } | ||
|
|
||
| void PrepareData() override { | ||
| std::vector<float> input_data(dims_.production()); | ||
| for (int i = 0; i < dims_.production(); i++) { | ||
| #if 0 | ||
| float sign = i % 3 == 0 ? -1.0f : 1.0f; | ||
| input_data[i] = sign * static_cast<float>(i % 128) * 0.013f + 0.001; | ||
| #else | ||
| input_data[i] = 1; | ||
| #endif | ||
| } | ||
| SetCommonTensor(input_, dims_, input_data.data()); | ||
| } | ||
| }; | ||
|
|
||
| TEST(Dropout, precision) { | ||
| LOG(INFO) << "test dropout op"; | ||
| float abs_error = 2e-5; | ||
| Place place; | ||
| #if defined(LITE_WITH_XPU) | ||
| place = TARGET(kXPU); | ||
| #else | ||
| return; | ||
| #endif | ||
|
|
||
| std::vector<std::vector<int64_t>> dims{ | ||
| /*{3} ,*/ {3, 4} /*, {3, 4, 5}, {1, 2, 3, 4}, {2, 3, 4, 5}*/}; | ||
|
hong19860320 marked this conversation as resolved.
|
||
| for (auto dim : dims) { | ||
| for (auto dropout_prob : {/*0.,*/ 0.5 /*, 1.*/}) { | ||
| for (auto dropout_implementation : | ||
| {"downgrade_in_infer", "upscale_in_train"}) { | ||
| std::unique_ptr<arena::TestCase> tester( | ||
| new DropoutComputeTester(place, | ||
| "def", | ||
| DDim(dim), | ||
| dropout_prob, | ||
| true, | ||
| 1, | ||
| dropout_implementation)); | ||
| arena::Arena arena(std::move(tester), place, abs_error); | ||
| arena.TestPrecision({"mask"}); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } // namespace lite | ||
| } // namespace paddle | ||
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.
去掉无用的代码