-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[API Compatibility]API supports functionality checks for paddle.dtype #74545
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
Merged
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -1422,51 +1422,64 @@ def convert_np_dtype_to_proto_type( | |
| """ | ||
|
|
||
| # Convert the data type string to numpy data type. | ||
| if isinstance(np_dtype, str) and np_dtype == "bfloat16": | ||
| dtype = np.uint16 | ||
| elif isinstance(np_dtype, str) and np_dtype == "float8_e4m3fn": | ||
| dtype = 'float8_e4m3fn' | ||
| elif isinstance(np_dtype, str) and np_dtype == "float8_e5m2": | ||
| dtype = 'float8_e5m2' | ||
| else: | ||
| dtype = np.dtype(np_dtype) | ||
|
|
||
| if dtype == np.float32: | ||
| return core.VarDesc.VarType.FP32 | ||
| elif dtype == np.float64: | ||
| return core.VarDesc.VarType.FP64 | ||
| elif dtype == 'float8_e4m3fn': | ||
| return core.VarDesc.VarType.FP8_E4M3FN | ||
| elif dtype == 'float8_e5m2': | ||
| return core.VarDesc.VarType.FP8_E5M2 | ||
| elif dtype == np.float16: | ||
| return core.VarDesc.VarType.FP16 | ||
| elif dtype == np.int32: | ||
| return core.VarDesc.VarType.INT32 | ||
| elif dtype == np.int16: | ||
| return core.VarDesc.VarType.INT16 | ||
| elif dtype == np.int64: | ||
| return core.VarDesc.VarType.INT64 | ||
| elif dtype == np.bool_: | ||
| return core.VarDesc.VarType.BOOL | ||
| elif dtype == np.uint16: | ||
| # since there is still no support for bfloat16 in NumPy, | ||
| # uint16 is used for casting bfloat16 | ||
| return core.VarDesc.VarType.BF16 | ||
| elif dtype == np.uint8: | ||
| return core.VarDesc.VarType.UINT8 | ||
| elif dtype == np.int8: | ||
| return core.VarDesc.VarType.INT8 | ||
| elif dtype == np.complex64: | ||
| return core.VarDesc.VarType.COMPLEX64 | ||
| elif dtype == np.complex128: | ||
| return core.VarDesc.VarType.COMPLEX128 | ||
|
|
||
| str_to_var_type = { | ||
| 'float32': core.VarDesc.VarType.FP32, | ||
| 'float64': core.VarDesc.VarType.FP64, | ||
| 'float16': core.VarDesc.VarType.FP16, | ||
| 'int32': core.VarDesc.VarType.INT32, | ||
| 'int16': core.VarDesc.VarType.INT16, | ||
| 'int64': core.VarDesc.VarType.INT64, | ||
| 'bool': core.VarDesc.VarType.BOOL, | ||
| 'uint8': core.VarDesc.VarType.UINT8, | ||
| 'int8': core.VarDesc.VarType.INT8, | ||
| 'complex64': core.VarDesc.VarType.COMPLEX64, | ||
| 'complex128': core.VarDesc.VarType.COMPLEX128, | ||
| 'bfloat16': core.VarDesc.VarType.BF16, | ||
| 'float8_e4m3fn': core.VarDesc.VarType.FP8_E4M3FN, | ||
| 'float8_e5m2': core.VarDesc.VarType.FP8_E5M2, | ||
| } | ||
|
|
||
| np_dtype_to_var_type = { | ||
| np.dtype("float32"): core.VarDesc.VarType.FP32, | ||
| np.dtype("float64"): core.VarDesc.VarType.FP64, | ||
| np.dtype("float16"): core.VarDesc.VarType.FP16, | ||
| np.dtype("int32"): core.VarDesc.VarType.INT32, | ||
| np.dtype("int16"): core.VarDesc.VarType.INT16, | ||
| np.dtype("int64"): core.VarDesc.VarType.INT64, | ||
| np.dtype("bool_"): core.VarDesc.VarType.BOOL, | ||
| np.dtype("uint16"): core.VarDesc.VarType.BF16, | ||
| np.dtype("uint8"): core.VarDesc.VarType.UINT8, | ||
| np.dtype("int8"): core.VarDesc.VarType.INT8, | ||
| np.dtype("complex64"): core.VarDesc.VarType.COMPLEX64, | ||
| np.dtype("complex128"): core.VarDesc.VarType.COMPLEX128, | ||
| np.float32: core.VarDesc.VarType.FP32, | ||
| np.float64: core.VarDesc.VarType.FP64, | ||
| np.float16: core.VarDesc.VarType.FP16, | ||
| np.int32: core.VarDesc.VarType.INT32, | ||
| np.int16: core.VarDesc.VarType.INT16, | ||
| np.int64: core.VarDesc.VarType.INT64, | ||
| np.bool_: core.VarDesc.VarType.BOOL, | ||
| np.uint8: core.VarDesc.VarType.UINT8, | ||
| np.int8: core.VarDesc.VarType.INT8, | ||
| np.uint16: core.VarDesc.VarType.BF16, | ||
| np.complex64: core.VarDesc.VarType.COMPLEX64, | ||
| np.complex128: core.VarDesc.VarType.COMPLEX128, | ||
| } | ||
|
|
||
| if isinstance(np_dtype, str): | ||
| if np_dtype in str_to_var_type: | ||
| return str_to_var_type[np_dtype] | ||
| dtype = np.dtype(np_dtype) | ||
|
|
||
| if dtype in np_dtype_to_var_type: | ||
| return np_dtype_to_var_type[dtype] | ||
| else: | ||
| raise ValueError(f"Not supported numpy dtype {dtype}") | ||
|
|
||
|
|
||
| def convert_np_dtype_to_dtype_( | ||
| np_dtype: np.dtype | str, | ||
| np_dtype: np.dtype | str | core.VarDesc.VarType | core.DataType, | ||
| ) -> core.VarDesc.VarType | core.DataType: | ||
| """ | ||
| Convert the data type in numpy to the data type in Paddle. | ||
|
|
@@ -1480,8 +1493,12 @@ def convert_np_dtype_to_dtype_( | |
|
|
||
| """ | ||
| if use_pir_api(): | ||
| if isinstance(np_dtype, core.DataType): | ||
| return np_dtype | ||
| return pir.core.convert_np_dtype_to_dtype_(np_dtype) | ||
|
|
||
| if isinstance(np_dtype, core.VarDesc.VarType): | ||
| return np_dtype | ||
| return convert_np_dtype_to_proto_type(np_dtype) | ||
|
Contributor
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. |
||
|
|
||
|
|
||
|
|
||
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 |
|---|---|---|
|
|
@@ -1587,12 +1587,11 @@ def log_softmax( | |
| [-12.31326640, -1.31326640 , -0.31326640 , -15.31326640], | ||
| [-3.44018970 , -2.44018970 , -1.44018970 , -0.44018970 ]]]) | ||
| """ | ||
|
|
||
| if (dtype is not None) and (not isinstance(dtype, core.VarDesc.VarType)): | ||
| dtype = convert_np_dtype_to_dtype_(dtype) | ||
|
|
||
| if in_dynamic_or_pir_mode(): | ||
| if dtype is not None: | ||
| if dtype is not None and x.dtype != dtype: | ||
|
Contributor
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. 这里dtype没有None的情况 |
||
| x = _C_ops.cast(x, dtype) | ||
| return _C_ops.log_softmax(x, axis) | ||
| else: | ||
|
|
||
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
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
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.
这里的判断是不是就不需要了,一般会在外面判断了