Skip to content

Conversation

@0x3878f
Copy link
Contributor

@0x3878f 0x3878f commented Jun 11, 2025

PR Category

Execute Infrastructure

PR Types

New features

Description

Support for simulation of enum types:

  • Add variable EnumVariable
  • operator.eq and operator.ne for EnumVariables

pcard-67164

@paddle-bot
Copy link

paddle-bot bot commented Jun 11, 2025

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.


@Dispatcher.register_decorator(operator.ne)
def dispatch_enum_ne(lhs: EnumVariable, rhs: EnumVariable):
return Dispatcher.call(operator.eq, lhs, rhs).bool_not()
Copy link
Member

Choose a reason for hiding this comment

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

TODO:之后有时间将 (VariableBase, VariableBase) ne 转发到 eq 取反,这样就不必每个都单独写一遍了

这里可以把 TODO 记到代码里

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的

union_free_vars(
frame_value_tracer.free_vars,
{
f"{self.get_py_value().__class__.__name__}": self.get_py_type()
Copy link
Member

Choose a reason for hiding this comment

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

这里的 self.get_py_value().__class__.__name__ 会在哪里用到呢?f"{self.value!s}" 会用到是么? 看来是的:

>>> from enum import Enum >>> >>> class Foo(Enum): ... A = 1 ... B = 2 ... >>> str(Foo.A) 'Foo.A' >>> repr(Foo.A) '<Foo.A: 1>' >>> 

这里其实是有一点风险的,毕竟依赖于 str 的实现,而且还有多个 Foo 冲突的风险,比如

# a.py class Foo(Enum): A: 1 def fn(foo: Foo): ... # b.py class Foo(Enum): B: 2 def fn(foo: Foo): ...

这里 a.fn.__globals__['Foo']b.fn.__globals__['Foo'] 完全不同,但是最终混合到生成的 guard 里会统一变成 Foo,后者会把前者覆盖,那在访问前者 Foo.A 时就会挂掉了

Copy link
Member

Choose a reason for hiding this comment

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

这里建议维护一个 name -> enum class 的映射

known_enum_classes = {} def get_enum_class_id(enum_class: type[Enum]): class_name = enum_class.__name__ known_enum_classes.setdefault(class_name, []) same_name_enums = known_enum_classes[class_name] id = 0 for i, cls in enumerate(same_name_enums): if enum_class == cls: id = i break else: id = len(same_name_enums) same_name_enums.append(enum_class) return id enum_class = instance.__class__ class_name = enum_class.__name__ enum_class_id = get_enum_class_id(enum_class) extern_var_name = f"__{class_name}_{enum_class_id}" # tracker 用 f"{extern_var_name}.{value.name}"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

没有考虑到类名重复的问题,已修改

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 🐾

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 changed the title [SOT] Support for simulation of enum types [SOT][3.13] Support for simulation of enum types Jun 14, 2025
@SigureMo SigureMo merged commit e46d86f into PaddlePaddle:develop Jun 15, 2025
69 of 73 checks passed
@SigureMo SigureMo changed the title [SOT][3.13] Support for simulation of enum types [SOT] Support for simulation of enum types Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants