Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

[Dispatch] use dispatch replace getitem, setitem, delitem and bool #198

Merged
merged 12 commits into from
Jun 27, 2023

Conversation

gouzil
Copy link
Member

@gouzil gouzil commented Jun 24, 2023

  • operator.getitem -> VariableBase/TensorVariable/ListVariable/TupleVariable/DictVariable.__getitem__
  • operator.setitem -> VariableBase/TensorVariable/ListVariable/TupleVariable/DictVariable.__setitem__
  • operator.delitem -> ListVariable/TupleVariable/DictVariable.__delitem__
  • bool -> ConstantVariable.__bool__
  • 整理单测
  • 补充 use dispatch instead of monkey patch #182 遗漏单测

close #194

@paddle-bot
Copy link

paddle-bot bot commented Jun 24, 2023

Thanks for your contribution!

@paddle-bot paddle-bot bot added contributor External developers status: proposed labels Jun 24, 2023
@@ -78,17 +78,17 @@ def __init__(
self.value = val_list

def get_value(self):
return [self[i].get_value() for i in range(len(self))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为了方便,这里就不要改了

def __getitem__(self, idx): # 看情况是只给容器加还是 VariableBase 加
    return self.getitem(idx)

与 getattr 不同,getattr 只要有 . 就会进入,很容易出问题,但 __getitem__ 不会

return self.graph.call_tensor_method(
'__getitem__',
'getitem',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为什么要改?这样还会对吗?

@@ -291,22 +291,6 @@ def __repr__(self):
def __str__(self):
return self.__repr__()

def __getitem__(self, item):
# TODO: Remove this function after we use builtin dispatcher instead
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个函数不一定能删吧,这里只是我早期认为可以直接替换掉,实际上可能不是这样

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#182 我忘记加 binary op、unary op 相关的了,可以在 test_14_operators 加一下 operator.add(x, y) 这类的

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需不需要把list那些也移动到test_14_operators, 比如test_04_list里的list_getitem

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以的

@SigureMo SigureMo changed the title use dispatch replace getitem、setitem、delitem、bool use dispatch replace getitem, setitem, delitem and bool Jun 24, 2023
@SigureMo SigureMo changed the title use dispatch replace getitem, setitem, delitem and bool [Dispatch] use dispatch replace getitem, setitem, delitem and bool Jun 24, 2023
sot/opcode_translator/executor/variable_dispatch.py Outdated Show resolved Hide resolved
operator.getitem,
(
"VariableBase | TensorVariable | ListVariable | TupleVariable | DictVariable",
"int | str | TensorVariable",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有没有可能是 slice?还有 tuple?

  • slice: a[1:10:3]
  • tuple: a[10, 11]

还有其他类型数据可不可以呢?如果都可以的话这里只加这几种类型是有问题的

gouzil and others added 2 commits June 25, 2023 10:36
@gouzil
Copy link
Member Author

gouzil commented Jun 25, 2023

单测的内容我会最后一起整理

Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

主体逻辑看起来是没啥问题了

operator.getitem,
(
"VariableBase | TensorVariable | ContainerVariable",
"int | str | TensorVariable | slice",
Copy link
Member

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 即可

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -188,17 +188,17 @@ def from_value(value: Any, graph: FunctionGraph | None, tracker: Tracker):
class TupleVariable(ContainerVariable):
def __init__(
self,
val_tuple: list[VariableBase],
val_tuple: list[VariableBase] | tuple[VariableBase],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里就不要允许传入 list[VariableBase] 了,直接强制 tuple[VariableBase] 吧,应该没有什么需求是传入 list?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@gouzil gouzil requested a review from SigureMo June 26, 2023 09:03
Dispatcher.register(
operator.getitem,
(
"VariableBase | TensorVariable | ContainerVariable",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里 VariableBase 已经包含了 TensorVariable | ContainerVariable,所以后面的需要删掉

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

'''
why __setitem__ is ok:
why __setitem__ or setitem is ok:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释里 __setitem__ 就不需要了吧

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -142,18 +142,25 @@ def f(x = [t0, t1])
f"[{self.__class__.__name__}]: received {key} as key."
)

if not isinstance(value, VariableBase):
if isinstance(value, TensorVariable):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

什么情况会传进来 Tensor 呢?对 Tensor get_value 不对吧

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_04list_setitem_tensor

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那也不应该 get_value,这里不应该修改,这是没有支持 SideEffects 导致的,单测先禁掉加 TODO 即可

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -51,6 +51,7 @@
operator.pow: ("__pow__", "__rpow__"),
operator.rshift: ("__rshift__", "__rrshift__"),
operator.sub: ("__sub__", "__rsub__"),
operator.setitem: ("__setitem__", None),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setitem 不是 binary op 吧,这个是在什么 case 使用到的呢?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已删除

@gouzil gouzil requested a review from SigureMo June 26, 2023 12:24
Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM~

Copy link
Collaborator

@2742195759 2742195759 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

利用 Dispatch 机制替换掉现有的魔法函数自动派发机制
3 participants