-
Notifications
You must be signed in to change notification settings - Fork 5.9k
support tensor index. #34824
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
support tensor index. #34824
Changes from 3 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 |
|---|---|---|
|
|
@@ -22,7 +22,7 @@ | |
| from .. import framework | ||
| from .. import core | ||
| from .. import unique_name | ||
| from ..framework import Variable, Parameter, ParamBase, _getitem_impl_ | ||
| from ..framework import Variable, Parameter, ParamBase, _getitem_impl_, _setitem_impl_ | ||
| from .base import switch_to_static_graph | ||
| from .math_op_patch import monkey_patch_math_varbase | ||
| from .parallel import scale_loss | ||
|
|
@@ -559,7 +559,25 @@ def contain_tensor(item): | |
| return True | ||
| return False | ||
|
|
||
| if contain_tensor(item): | ||
| def is_list_tuple(index, contain_type): | ||
| def _is_list_tuple(item): | ||
| if not (isinstance(item, (list, tuple)) or | ||
| type(item) == contain_type): | ||
| return False | ||
| if isinstance(item, (tuple, list)): | ||
| for s in item: | ||
| if not _is_list_tuple(s): | ||
| return False | ||
| return True | ||
|
|
||
| if not isinstance(index, (tuple, list)): | ||
| return False | ||
| for s in index: | ||
| if not _is_list_tuple(s): | ||
| return False | ||
| return True | ||
|
|
||
| if contain_tensor(item) or is_list_tuple(item, int): | ||
| # 1. Call _getitem_impl_ when item contains tensor. | ||
| # Why not call a c++ function ? Because item can't be parsed when it contains tensor. | ||
| return _getitem_impl_(self, item) | ||
|
|
@@ -568,6 +586,31 @@ def contain_tensor(item): | |
| # 2. Call c++ func getitem_index_not_tensor to speedup. | ||
| return self._getitem_index_not_tensor(item) | ||
|
|
||
| def __setitem__(self, item, value): | ||
| def contain_tensor(item): | ||
| if not isinstance(item, tuple): | ||
| item = [item] | ||
|
|
||
| for slice_item in item: | ||
| if isinstance(slice_item, slice): | ||
| if isinstance(slice_item.start, Variable) \ | ||
| or isinstance(slice_item.stop, Variable) \ | ||
| or isinstance(slice_item.step, Variable): | ||
| return True | ||
| else: | ||
| if isinstance(slice_item, Variable): | ||
| return True | ||
| return False | ||
|
|
||
| if contain_tensor(item): | ||
| # 1. Call _getitem_impl_ when item contains tensor. | ||
|
||
| # Why not call a c++ function ? Because item can't be parsed when it contains tensor. | ||
| return _setitem_impl_(self, item, value) | ||
|
|
||
| else: | ||
| # 2. Call c++ func getitem_index_not_tensor to speedup. | ||
| return self.__setitem_varbase__(item, value) | ||
|
|
||
| for method_name, method in ( | ||
| ("__bool__", __bool__), ("__nonzero__", __nonzero__), | ||
| ("_to_static_var", _to_static_var), ("set_value", set_value), | ||
|
|
@@ -577,7 +620,8 @@ def contain_tensor(item): | |
| ("__str__", __str__), ("__repr__", __str__), | ||
| ("__deepcopy__", __deepcopy__), ("__module__", "paddle"), | ||
| ("__name__", "Tensor"), ("__array__", __array__), | ||
| ("__getitem__", __getitem__), ("item", item)): | ||
| ("__getitem__", __getitem__), ("item", item), | ||
| ("__setitem__", __setitem__)): | ||
| setattr(core.VarBase, method_name, method) | ||
|
|
||
| # NOTE(zhiqiu): pybind11 will set a default __str__ method of enum class. | ||
|
|
||
Oops, something went wrong.
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.
是否可以和
__getitem__共用一份contain_tensor代码 ?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.
Done, thx.