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

[SideEffect] add side effects support for ObjectVariable #393

Merged
merged 20 commits into from
Sep 26, 2023

Conversation

gouzil
Copy link
Member

@gouzil gouzil commented Sep 17, 2023

为什么需要支持ObjectVariable的 side effects

class CustomObject:
    def __init__(self, a):
        self.a = a

def object_delattr(cus_obj):
    del cus_obj.a

x = CustomObject(1)
symbolic_translate(object_delattr)(x)
print(dir(x))
# ['__class__', ... , 'a']

x2 = CustomObject(1)
object_delattr(x2)
print(dir(x2))
# ['__class__', ... , '__weakref__']

目前还存在的问题:

  • sot/opcode_translator/executor/side_effects.py中有关Objpost_gen方法
  • 补充单测

close: #307

@paddle-bot paddle-bot bot added the contributor External developers label Sep 17, 2023
raise BreakGraphError(
f"STORE_ATTR don't support {type(obj)}.{key}={val}"
)
obj.setattr(key, val)
Copy link
Member

Choose a reason for hiding this comment

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

直接用 BuiltinVariable dispatch 一下就行了,就没有 if-else 了

Comment on lines 187 to 195
class CustomObject:
def __init__(self):
self.x = 0

def attr_set(cus_obj):
cus_obj.x = 2

cus_obj = CustomObject()
attr_set(cus_obj)
Copy link
Member

Choose a reason for hiding this comment

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

和其他示例一样,只写生成代码

obj.attr = new_value

Comment on lines 214 to 223
"""
class CustomObject:
def __init__(self):
self.x = 0

def attr_set(cus_obj):
del cus_obj.x

cus_obj = CustomObject()
attr_set(cus_obj)
Copy link
Member

Choose a reason for hiding this comment

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

同上

@@ -414,6 +414,7 @@ def __init__(
self, layer: paddle.nn.Layer, graph: FunctionGraph, tracker: Tracker
):
super().__init__(layer, graph, tracker)
self.proxy = self.attr_proxy
Copy link
Member

Choose a reason for hiding this comment

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

这是为什么?proxyattr_proxy 不是一种东西

"""object side effect."""
t = t + 1
cus_obj.x = t
return t, cus_obj
return t, cus_obj.x
Copy link
Member

Choose a reason for hiding this comment

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

单测太少了,多加一些吧

getattr(cus_obj1, key, "Key does not exist"),
getattr(cus_obj2, key, "Key does not exist"),
)
self.assert_nest_match(sym_output, paddle_output)
Copy link
Member

Choose a reason for hiding this comment

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

同样最好加一个机制,在跑动态图和 SOT 前都先 store 一个 attr,然后跑完就恢复

Copy link
Member Author

Choose a reason for hiding this comment

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

有 demo 嘛,目前是只验证传入的那个修改的 key 的值

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.

LGTMeow 🐾

@SigureMo SigureMo merged commit 4fdd284 into PaddlePaddle:develop Sep 26, 2023
10 checks passed
@SigureMo SigureMo deleted the objvariable_side_effects branch May 18, 2024 17:15
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ObjectVariable setattr side effect 支持
3 participants