-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[CINN]Apply input dynamic dim specification #67628
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
Hongqing-work
merged 2 commits into
PaddlePaddle:develop
from
Hongqing-work:add-shape-config
Aug 31, 2024
Merged
Changes from all commits
Commits
Show all changes
2 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
110 changes: 110 additions & 0 deletions
110
paddle/cinn/hlir/dialect/operator/transforms/specify_input_dynamic_dim_util.cc
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,110 @@ | ||
| // Copyright (c) 2024 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/cinn/hlir/dialect/operator/transforms/specify_input_dynamic_dim_util.h" | ||
|
|
||
| #include <sys/stat.h> | ||
| #include <fstream> | ||
| #include "nlohmann/json.hpp" | ||
|
|
||
| using Json = nlohmann::json; | ||
|
|
||
| namespace cinn { | ||
| namespace dialect { | ||
| namespace ir { | ||
|
|
||
| namespace { | ||
|
|
||
| std::vector<pir::InputDynamicDimSpec> DeserializeInputDynamicDimSpecFromJson( | ||
| const Json& json) { | ||
| std::vector<pir::InputDynamicDimSpec> res; | ||
| for (const auto& element : json.items()) { | ||
| pir::InputDynamicDimSpec dim_spec; | ||
| dim_spec.dim_name = [&]() -> std::string { return element.key(); }(); | ||
| dim_spec.input_bind = [&]() { | ||
| const auto& value = element.value(); | ||
| std::vector<std::pair<std::string, int>> res; | ||
| PADDLE_ENFORCE_EQ(value.contains("input_bind"), | ||
| true, | ||
| ::common::errors::InvalidArgument( | ||
| "input dynamic dim spec must contain input_bind")); | ||
| for (const auto& bind_item : value["input_bind"]) { | ||
| const auto& input_name = bind_item[0].get<std::string>(); | ||
| const auto& dim_index = bind_item[1].get<int>(); | ||
| res.emplace_back(std::make_pair(input_name, dim_index)); | ||
| } | ||
| return res; | ||
| }(); | ||
| dim_spec.range = [&]() { | ||
| const auto& value = element.value(); | ||
| symbol::ConstraintsManager::Range range; | ||
| if (value.contains("min")) { | ||
| range.min = value["min"].get<int>(); | ||
| } | ||
| if (value.contains("max")) { | ||
| range.max = value["max"].get<int>(); | ||
| } | ||
| return range; | ||
| }(); | ||
| res.emplace_back(std::move(dim_spec)); | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| bool PathExists(const std::string& path) { | ||
| struct stat statbuf; | ||
| if (stat(path.c_str(), &statbuf) != -1) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| std::vector<pir::InputDynamicDimSpec> | ||
| DeserializeInputDynamicDimSpecFromJsonFile(std::string file_path) { | ||
| PADDLE_ENFORCE_EQ( | ||
| PathExists(file_path), | ||
| true, | ||
| ::common::errors::InvalidArgument( | ||
| "File path for input dynamic dim spec not exists: %s.", file_path)); | ||
| std::ifstream ifs(file_path); | ||
| PADDLE_ENFORCE_EQ( | ||
| !ifs, | ||
| false, | ||
| ::common::errors::InvalidArgument( | ||
| "File path for input dynamic dim spec fail to open for reading: %s.", | ||
| file_path)); | ||
| Json json; | ||
| ifs >> json; | ||
| return DeserializeInputDynamicDimSpecFromJson(json); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| void SpecifyInputDynamicDim( | ||
| pir::Program* program, | ||
| const std::vector<pir::InputDynamicDimSpec>& input_dynamic_dim_spec) { | ||
| pir::ShapeConstraintIRAnalysis& shape_analysis = | ||
| pir::ShapeAnalysisManager::Instance().Get(program); | ||
| shape_analysis.SetInputDynamicDimSpec(input_dynamic_dim_spec); | ||
| } | ||
|
|
||
| void SpecifyInputDynamicDimFromFile(pir::Program* program, | ||
| std::string filepath) { | ||
| SpecifyInputDynamicDim(program, | ||
| DeserializeInputDynamicDimSpecFromJsonFile(filepath)); | ||
| } | ||
|
|
||
| } // namespace ir | ||
| } // namespace dialect | ||
| } // namespace cinn |
30 changes: 30 additions & 0 deletions
30
paddle/cinn/hlir/dialect/operator/transforms/specify_input_dynamic_dim_util.h
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,30 @@ | ||
| // Copyright (c) 2024 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 "paddle/pir/include/core/program.h" | ||
| #include "paddle/pir/include/dialect/shape/utils/shape_analysis.h" | ||
|
|
||
| namespace cinn { | ||
| namespace dialect { | ||
| namespace ir { | ||
| void SpecifyInputDynamicDim( | ||
| pir::Program* program, | ||
| const std::vector<pir::InputDynamicDimSpec>& input_dynamic_dim_spec); | ||
| void SpecifyInputDynamicDimFromFile(pir::Program* program, | ||
| std::string filepath); | ||
| } // namespace ir | ||
| } // namespace dialect | ||
| } // namespace cinn |
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1104,6 +1104,32 @@ PHI_DEFINE_EXPORTED_string(cinn_subgraph_graphviz_dir, | |||||
| "Specify the directory path of dot file of " | ||||||
| "graph, which is used for debug."); | ||||||
|
|
||||||
| /* | ||||||
| * CINN related FLAG | ||||||
| * Name: FLAGS_cinn_specify_input_dynamic_dim | ||||||
| * Since Version: develop | ||||||
| * Value Range: bool, default=false | ||||||
| * Example: FLAGS_cinn_specify_input_dynamic_dim=true will use file set by | ||||||
| * FLAGS_cinn_input_dynamic_dim_spec_file to specify input dynamic dimention. | ||||||
| */ | ||||||
| PHI_DEFINE_EXPORTED_bool(cinn_specify_input_dynamic_dim, | ||||||
| false, | ||||||
| "Whether to specify input dynamic dimention."); | ||||||
|
|
||||||
| /* | ||||||
| * CINN related FLAG | ||||||
| * Name: FLAGS_cinn_input_dynamic_dim_spec_file | ||||||
| * Since Version: develop | ||||||
|
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.
Suggested change
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. 收到,下一个PR顺带修改 |
||||||
| * Value Range: string, default="" | ||||||
| * Example: FLAGS_cinn_input_dynamic_dim_spec_file="./config.json", | ||||||
| * FLAGS_cinn_specify_input_dynamic_dim=true would use input dynamic dimention | ||||||
| * predefined in ./config.json to specify input dynamic dimention. | ||||||
| */ | ||||||
| PHI_DEFINE_EXPORTED_string( | ||||||
| cinn_input_dynamic_dim_spec_file, | ||||||
| "", | ||||||
| "File path of predefined input dynamic dimention specification."); | ||||||
|
|
||||||
| #endif | ||||||
|
|
||||||
| /* | ||||||
|
|
||||||
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
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.
这个flag是不可以不用添加?
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.
之后考虑给path指定默认值,所以这里需要一个直接表明是否开启的开关