-
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
[Tensor Operants & Prim] Tensor pow API uses elementwise_pow #50886
Merged
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 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
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 |
---|---|---|
|
@@ -29,6 +29,8 @@ | |
|
||
indent = " " | ||
|
||
specific_ops_map = {"elementwise_pow": "pow"} | ||
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. 可以加点注释,说明一下作用 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. Next PR fix this. |
||
|
||
|
||
operants_base_include = """// Generated by paddle/phi/api/yaml/generator/tensor_operants_gen.py | ||
|
||
|
@@ -68,6 +70,10 @@ class TensorOperantsBase { | |
virtual Tensor multiply(const Scalar& x, const Tensor& y) = 0; | ||
|
||
virtual Tensor subtract(const Scalar& x, const Tensor& y) = 0; | ||
|
||
virtual Tensor pow(const Tensor& x, const Tensor& y) = 0; | ||
|
||
virtual Tensor pow(const Tensor& x, const Scalar& y) = 0; | ||
""" | ||
|
||
|
||
|
@@ -143,6 +149,14 @@ class TensorOperantsBase { | |
return scale(-1.0, 0.0, true); | ||
} | ||
|
||
Tensor Tensor::pow(const Tensor& y) const { | ||
return paddle::OperantsManager::Instance().pow(static_cast<const Tensor &>(*this), y); | ||
} | ||
|
||
Tensor Tensor::pow(const Scalar& y) const { | ||
return paddle::OperantsManager::Instance().pow(static_cast<const Tensor &>(*this), y); | ||
} | ||
|
||
PADDLE_API Tensor operator+(const Scalar& x, const Tensor& y) { | ||
return paddle::OperantsManager::Instance().add(x, y); | ||
} | ||
|
@@ -211,6 +225,10 @@ class PhiTensorOperants : public TensorOperantsBase { | |
|
||
Tensor divide(const Scalar& x, const Tensor& y); | ||
|
||
Tensor pow(const Tensor& x, const Tensor& y); | ||
|
||
Tensor pow(const Tensor& x, const Scalar& y); | ||
|
||
""" | ||
|
||
|
||
|
@@ -267,6 +285,14 @@ class PhiTensorOperants : public TensorOperantsBase { | |
Tensor PhiTensorOperants::divide(const Scalar& x, const Tensor& y) { | ||
return paddle::experimental::divide(paddle::experimental::full_like(y, x), y); | ||
} | ||
|
||
Tensor PhiTensorOperants::pow(const Tensor& x, const Tensor& y) { | ||
return paddle::experimental::elementwise_pow(x, y); | ||
} | ||
|
||
Tensor PhiTensorOperants::pow(const Tensor& x, const Scalar& y) { | ||
return paddle::experimental::elementwise_pow(x, paddle::experimental::full_like(x, y)); | ||
} | ||
""" | ||
|
||
|
||
|
@@ -359,6 +385,10 @@ class OperantsManager { | |
|
||
Tensor divide(const Scalar& x, const Tensor& y); | ||
|
||
Tensor pow(const Tensor& x, const Tensor& y); | ||
|
||
Tensor pow(const Tensor& x, const Scalar& y); | ||
|
||
""" | ||
|
||
|
||
|
@@ -512,8 +542,10 @@ def gene_operants_implementation(self): | |
|
||
""" | ||
|
||
def gene_operants_manager_code(self): | ||
def gene_operants_manager_code(self, is_specific_op=False): | ||
func_name = self.get_api_func_name() | ||
if is_specific_op: | ||
func_name = specific_ops_map[func_name] | ||
func_args = self.inputs['names'] + self.attrs['names'] | ||
func_args_code = ", ".join(func_args) | ||
return f""" | ||
|
@@ -552,11 +584,19 @@ def gene_operants_manager_code(self): | |
def gene_operants_manager_implementation(self): | ||
func_name = self.get_api_func_name() | ||
final_code = "" | ||
# Codes for arthemetic operants | ||
if func_name in ["add", "subtract", "multiply", "divide"]: | ||
final_code += f""" | ||
{self.get_return_type()} OperantsManager::{func_name}(const Tensor& x, const Scalar& y) {{{self.gene_operants_manager_code()}}} | ||
|
||
{self.get_return_type()} OperantsManager::{func_name}(const Scalar& x, const Tensor& y) {{{self.gene_operants_manager_code()}}} | ||
""" | ||
# Codes for specific operants | ||
if func_name in specific_ops_map.keys(): | ||
final_code += f""" | ||
{self.get_return_type()} OperantsManager::{specific_ops_map[func_name]}(const Tensor& x, const Tensor& y) {{{self.gene_operants_manager_code(is_specific_op=True)}}} | ||
|
||
{self.get_return_type()} OperantsManager::{specific_ops_map[func_name]}(const Tensor& x, const Scalar& y) {{{self.gene_operants_manager_code(is_specific_op=True)}}} | ||
""" | ||
# func decalaration | ||
if func_name[-1] != '_': | ||
|
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
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.
这个为什么不使用pow的接口?
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.
此处来自组合算子需求,要求尽可能使用基础算子,使用 elementwise + full_like 做组合,可以节省一个 pow 算子(接口)
Prim requires to use basic operators as much as possible. Using
elementwise + full_like
can save the operator ofpow
.