This repository was archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
[Dispatch] use dispatch replace getitem, setitem, delitem and bool
#198
Merged
2742195759
merged 12 commits into
PaddlePaddle:develop
from
gouzil:replace_magic_method
Jun 27, 2023
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
82d41ad
[executor] replace getitem and ConstantVariable.__bool__
gouzil 239bcec
[variables] RollBACK basic and container; [variables] add __getitem__
gouzil 7a1bdd9
[executor] Dispatcher clean and add ContainerVariable
gouzil 0b27dcd
[variables] support slice
gouzil 8855b0e
[test] add tuple slice test
gouzil f539545
[variables] fix TupleVariable and support setitem;[test] add setitem …
gouzil 5129c96
Merge branch 'develop' of github.com:gouzil/paddle-symbolic-trace int…
gouzil c595f30
[executor] suport delitem;[tests] add delitem test
gouzil 3c73305
[executor] add todo
gouzil d828024
[variables] fix list getitem;[tests] move to operators
gouzil 32dc2dc
[tests] supplementary testing
gouzil 61bb321
[variable] fix list setitem; [magic_methods] rm __setitem__; [tests] …
gouzil 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
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 |
|---|---|---|
|
|
@@ -281,6 +281,9 @@ def getattr(self, name: str): | |
| ) | ||
|
|
||
| def __setitem__(self, key, value): | ||
| return self.setitem(key, value) | ||
|
|
||
| def setitem(self, key, value): | ||
| raise NotImplementException(f"{self} is not support setitem.") | ||
|
|
||
| def __repr__(self): | ||
|
|
@@ -291,9 +294,10 @@ def __repr__(self): | |
| def __str__(self): | ||
| return self.__repr__() | ||
|
|
||
| def __getitem__(self, item): | ||
| # TODO: Remove this function after we use builtin dispatcher instead | ||
|
Member
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. 这个函数不一定能删吧,这里只是我早期认为可以直接替换掉,实际上可能不是这样 |
||
| def __getitem__(self, idx): | ||
| return self.getitem(idx) | ||
|
|
||
| def getitem(self, item): | ||
| class_var = VariableFactory.from_value( | ||
| self.get_value().__class__, | ||
| self.graph, | ||
|
|
@@ -331,9 +335,6 @@ def __call__(self, *args, **kwargs): | |
| output = fn_var(*args, **kwargs) | ||
| return output | ||
|
|
||
| def getitem(self, *args, **kwargs): | ||
| pass | ||
|
|
||
| @VariableFactory.register_from_value() | ||
| def from_value( | ||
| value: Any, | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -34,7 +34,7 @@ def len(self): | |
| len(self), self.graph, DummyTracker([self]) | ||
| ) | ||
|
|
||
| def __bool__(self): | ||
| def __bool__(self) -> bool: | ||
| return len(self) > 0 | ||
|
|
||
| def bool(self): | ||
|
|
@@ -78,7 +78,7 @@ def __init__( | |
| self.value = val_list | ||
|
|
||
| def get_value(self): | ||
| return [self[i].get_value() for i in range(len(self))] | ||
|
Member
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. 为了方便,这里就不要改了 def __getitem__(self, idx): # 看情况是只给容器加还是 VariableBase 加
return self.getitem(idx)与 getattr 不同,getattr 只要有 |
||
| return [self[idx].get_value() for idx in range(len(self))] | ||
|
|
||
| def _reconstruct(self, codegen: PyCodeGen): | ||
| size = len(self) | ||
|
|
@@ -102,7 +102,7 @@ def main_info(self) -> dict[str, Any]: | |
| def __len__(self): | ||
| return len(self.value) | ||
|
|
||
| def __getitem__(self, key): | ||
| def getitem(self, key): | ||
| ''' | ||
| we need to make sure that: | ||
| before an inplace change happens to ListVariable, | ||
|
|
@@ -124,9 +124,9 @@ def __getitem__(self, key): | |
|
|
||
| return retval | ||
|
|
||
| def __setitem__(self, key, value): | ||
| def setitem(self, key, value): | ||
| ''' | ||
| why __setitem__ is ok: | ||
| why setitem is ok: | ||
|
|
||
| case: | ||
| def f(x = [t0, t1]) | ||
|
|
@@ -147,13 +147,18 @@ def f(x = [t0, t1]) | |
| f"[{self.__class__.__name__}]: received {value} to set value." | ||
| ) | ||
| self.value[key] = value | ||
| return ConstantVariable.wrap_literal(None) | ||
|
|
||
| def __delitem__(self, key): | ||
| return self.delitem(key) | ||
|
|
||
| def delitem(self, key): | ||
| if isinstance(key, VariableBase): | ||
| raise InnerError( | ||
| f"[{self.__class__.__name__}]: received {key} as key to delete." | ||
| ) | ||
| del self.value[key] | ||
| return ConstantVariable.wrap_literal(None) | ||
|
|
||
| def extend(self, data): | ||
| self.value.extend(data.get_wrapped_items()) | ||
|
|
@@ -188,17 +193,16 @@ def from_value(value: Any, graph: FunctionGraph | None, tracker: Tracker): | |
| class TupleVariable(ContainerVariable): | ||
| def __init__( | ||
| self, | ||
| val_tuple: list[VariableBase], | ||
| val_tuple: tuple[VariableBase], | ||
| graph: FunctionGraph, | ||
| tracker: Tracker, | ||
| ): | ||
| super().__init__(tracker) | ||
| self.graph = graph | ||
| # exactly it is a list (need replace item with VariableBase) | ||
| self.value = list(val_tuple) | ||
| self.value = val_tuple | ||
|
|
||
| def get_value(self): | ||
| return tuple(self[i].get_value() for i in range(len(self))) | ||
| return tuple(self[idx].get_value() for idx in range(len(self))) | ||
|
|
||
| def _reconstruct(self, codegen: PyCodeGen): | ||
| size = len(self) | ||
|
|
@@ -222,7 +226,7 @@ def main_info(self) -> dict[str, Any]: | |
| def __len__(self): | ||
| return len(self.value) | ||
|
|
||
| def __getitem__(self, key): | ||
| def getitem(self, key): | ||
| if isinstance(key, VariableBase): | ||
| raise InnerError( | ||
| f"[{self.__class__.__name__}]: recieved {key} as key." | ||
|
|
@@ -233,12 +237,15 @@ def __getitem__(self, key): | |
| retval, graph=self.graph, tracker=GetItemTracker(self, key) | ||
| ) | ||
|
|
||
| def __setitem__(self, key, value): | ||
| def setitem(self, key, value): | ||
| raise InnerError( | ||
| f"[{self.__class__.__name__}]: setitem is not allowed." | ||
| ) | ||
|
|
||
| def __delitem__(self, key): | ||
| return self.delitem(key) | ||
|
|
||
| def delitem(self, key): | ||
| raise InnerError( | ||
| f"[{self.__class__.__name__}]: delitem is not allowed." | ||
| ) | ||
|
|
@@ -312,7 +319,7 @@ def main_info(self) -> dict[str, Any]: | |
| def __len__(self): | ||
| return len(self.value) | ||
|
|
||
| def __getitem__(self, key): | ||
| def getitem(self, key): | ||
| if isinstance(key, VariableBase): | ||
| raise InnerError( | ||
| f"[{self.__class__.__name__}]: recieved {key} as key." | ||
|
|
@@ -324,7 +331,7 @@ def __getitem__(self, key): | |
| retval, self.graph, tracker=GetItemTracker(self, key) | ||
| ) | ||
|
|
||
| def __setitem__(self, key, value): | ||
| def setitem(self, key, value): | ||
| if isinstance(key, VariableBase): | ||
| raise InnerError( | ||
| f"[{self.__class__.__name__}]: recieved {key} as key." | ||
|
|
@@ -337,12 +344,18 @@ def __setitem__(self, key, value): | |
|
|
||
| self.value[key] = value | ||
|
|
||
| return ConstantVariable.wrap_literal(None) | ||
|
|
||
| def __delitem__(self, key): | ||
| return self.delitem(key) | ||
|
|
||
| def delitem(self, key): | ||
| if isinstance(key, VariableBase): | ||
| raise InnerError( | ||
| f"[{self.__class__.__name__}]: recieved {key} as key to delete." | ||
| ) | ||
| del self.value[key] | ||
| return ConstantVariable.wrap_literal(None) | ||
|
|
||
| def keys(self): | ||
| from .iter import SequenceIterVariable | ||
|
|
||
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
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.
这里 TensorVariable 是不是会出问题呢?现在是不是没有相关的 case,这个问题算是一个遗留问题,和 66 行的 getattr 应该算同一个问题,在 BINARY_SUBSCR 里我们直接将 value 取出来传进 getitem 了,所以
tensor1[tensor2]假如 tensor2 是一个中间变量的话(value 为 None),是会有问题的,这里应该将其 Variable 传入 getitem,并在 getitem 里做取 value 等操作这个 PR 先不改这个问题吧,可以下个 PR 统一修改下 getitem、getattr 的问题,这个 PR 记录一下 TODO 即可
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