-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[PIR] Adapt paddle.assign to pir #57780
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2146,15 +2146,24 @@ def assign(x, output=None): | |
[2.5 2.5]] | ||
""" | ||
# speed up | ||
if x is output and isinstance(x, Variable): | ||
if x is output and isinstance(x, (Variable, paddle.pir.OpResult)): | ||
return x | ||
|
||
input = x | ||
helper = LayerHelper('assign', **locals()) | ||
check_type( | ||
input, | ||
'input', | ||
(Variable, np.ndarray, list, tuple, float, int, bool), | ||
( | ||
Variable, | ||
paddle.pir.OpResult, | ||
np.ndarray, | ||
list, | ||
tuple, | ||
float, | ||
int, | ||
bool, | ||
), | ||
'assign', | ||
) | ||
|
||
|
@@ -2167,12 +2176,17 @@ def assign(x, output=None): | |
# but in_dynamic_mode()==False under @to_static, which means | ||
# isinstance(Tensor, Variable) == False. It will cause return None | ||
# after this api. | ||
if isinstance(input, (Variable, core.eager.Tensor)): | ||
if isinstance(input, (Variable, core.eager.Tensor, paddle.pir.OpResult)): | ||
if in_dynamic_mode(): | ||
if output is None: | ||
output = _C_ops.assign(input) | ||
else: | ||
_C_ops.assign_out_(input, output) | ||
elif in_pir_mode(): | ||
if output is None: | ||
output = _C_ops.assign(input) | ||
else: | ||
output = _C_ops.assign_out_(input, output) | ||
else: | ||
check_dtype( | ||
input.dtype, | ||
|
@@ -2200,19 +2214,25 @@ def assign(x, output=None): | |
) | ||
elif isinstance(input, np.ndarray): | ||
# We now support the form of [var, VAR...] if the Var.shape=[1,] | ||
if len(input.shape) > 0 and any(isinstance(x, Variable) for x in input): | ||
if len(input.shape) > 0 and any( | ||
isinstance(x, (Variable, paddle.pir.OpResult)) for x in input | ||
): | ||
# We only deal with the case where the list is nested one level, convert all scalars into variables, and then use stack to process. It is necessary to ensure the consistency of types. | ||
if not all( | ||
x.shape == (1,) | ||
for x in input | ||
if isinstance(x, (Variable, core.eager.Tensor)) | ||
if isinstance( | ||
x, (Variable, core.eager.Tensor, paddle.pir.OpResult) | ||
) | ||
): | ||
raise TypeError( | ||
"Unsupport paddle.assign([Variable, Variable...]) with non-scalar variable." | ||
) | ||
|
||
def convert_scalar(x): | ||
if not isinstance(x, (Variable, core.eager.Tensor)): | ||
if not isinstance( | ||
x, (Variable, core.eager.Tensor, paddle.pir.OpResult) | ||
): | ||
return assign(x) | ||
return x | ||
|
||
|
@@ -2237,16 +2257,33 @@ def convert_scalar(x): | |
"it to float32" | ||
) | ||
dtype = core.VarDesc.VarType.FP32 | ||
if dtype == core.VarDesc.VarType.BOOL: | ||
|
||
if dtype == core.DataType.FLOAT64: | ||
# Setting FP64 numpy data is not supported in Paddle, so we | ||
# use FP32 here | ||
warnings.warn( | ||
"paddle.assign doesn't support float64 input now due " | ||
"to current platform protobuf data limitation, we convert " | ||
"it to float32" | ||
) | ||
dtype = core.DataType.FLOAT32 | ||
|
||
if dtype == core.VarDesc.VarType.BOOL or dtype == core.DataType.BOOL: | ||
value_name = "bool_values" | ||
values = [int(v) for v in input.flat] | ||
elif dtype == core.VarDesc.VarType.FP32: | ||
elif ( | ||
dtype == core.VarDesc.VarType.FP32 or dtype == core.DataType.FLOAT32 | ||
): | ||
value_name = "fp32_values" | ||
values = [float(v) for v in input.flat] | ||
elif dtype == core.VarDesc.VarType.INT32: | ||
elif ( | ||
dtype == core.VarDesc.VarType.INT32 or dtype == core.DataType.INT32 | ||
): | ||
value_name = "int32_values" | ||
values = [int(v) for v in input.flat] | ||
elif dtype == core.VarDesc.VarType.INT64: | ||
elif ( | ||
dtype == core.VarDesc.VarType.INT64 or dtype == core.DataType.INT64 | ||
): | ||
value_name = "int64_values" | ||
values = [int(v) for v in input.flat] | ||
else: | ||
|
@@ -2260,16 +2297,25 @@ def convert_scalar(x): | |
"The size of input is too big. Please consider " | ||
"saving it to file and 'load_op' to load it" | ||
) | ||
if in_dynamic_mode(): | ||
if in_dynamic_or_pir_mode(): | ||
if output is None: | ||
output = zeros(list(input.shape), dtype) | ||
_C_ops.assign_value_( | ||
output, | ||
list(input.shape), | ||
dtype, | ||
values, | ||
_current_expected_place(), | ||
) | ||
if in_dynamic_mode(): | ||
_C_ops.assign_value_( | ||
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. 这里动态图下没有返回么?还是说其实返回了output本身。如果是后者的话,其实这里可以统一成一个分支? 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. 动态图没返回,因为是inplace的,而新IR下inplace是需要返回一个新value来表示的,故拆成俩个分支 |
||
output, | ||
list(input.shape), | ||
dtype, | ||
values, | ||
_current_expected_place(), | ||
) | ||
else: | ||
output = _C_ops.assign_value_( | ||
output, | ||
list(input.shape), | ||
dtype, | ||
values, | ||
_current_expected_place(), | ||
) | ||
else: | ||
if output is None: | ||
output = helper.create_variable_for_type_inference( | ||
|
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.
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.
感谢,下个pr修改