Skip to content
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

[SOT][dynamic shape] Support dynamic int input #64323

Merged
merged 25 commits into from
May 19, 2024

Conversation

zrr1999
Copy link
Member

@zrr1999 zrr1999 commented May 15, 2024

PR Category

Others

PR Types

Others

Description

Support dynamic int input

TODO:

  • -n 这种 unary 操作会被转为Tensor
  • SymbolicVariable

Copy link

paddle-bot bot commented May 15, 2024

你的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.

@paddle-bot paddle-bot bot added the contributor External developers label May 15, 2024
@@ -566,6 +669,9 @@ def symbolic_call(self, infer_meta_fn, compute_fn, func, *args, **kwargs):
"""
self.collect_input_variables(list(args))
self.collect_input_variables(list(kwargs.values()))
if ENV_SOT_ALLOW_DYNAMIC_SHAPE.get():
self.analyze_dynamic_inputs()
Copy link
Member

Choose a reason for hiding this comment

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

symbolic_call 每次组网都会调用一次吧,这是不是分析次数太多了?按我理解在 input_variables 稳定后调用一次就可以了?

str, DynamicShape | SymbolicInt | int
] = self.dynamic_inputs
for variable in find_traceable_vars(self.input_variables):
if isinstance(variable.tracker, LocalTracker):
Copy link
Member

Choose a reason for hiding this comment

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

从代码风格上来看,推荐使用类似 early return 的形式来减少单分支 if 的缩进

 def fn():
-    if x:
-        foo()
+    if not x:
+        return
+    foo()

 for i in range(n):
-    if x:
-        foo()
+    if not x:
+        continue
+    foo()

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

return f"{self.graph.OUT_VAR_PREFIX}{self.var_name}"

def _reconstruct(self, codegen: PyCodeGen):
codegen.gen_load_const(paddle.to_tensor(self.value))
Copy link
Member

Choose a reason for hiding this comment

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

这里好像不太行?这样的话这个 value 是固定的吧,应该生成代码将其 load 到栈上,然后生成代码将其转化为 tensor,这些都是通过字节码实现的~

Comment on lines 1662 to 1666
if ENV_SOT_ALLOW_DYNAMIC_SHAPE.get() and isinstance(value, int):
self._locals[name] = SymbolicIntVariable(
value, self._graph, tracker
)
else:
Copy link
Member

Choose a reason for hiding this comment

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

诶?所有 locals 里的 int 都是 SymbolicIntVariable?之后通过 var.symbolic 来确定是否真的 symbolic?感觉有点奇怪

另外,是否 symbolic 是在什么时候变化的呢?按我理解,在同一次模拟过程中不应该有非 symbolic 向 symbolic 的转化,一个 var 是否是 symbolic 应该是模拟最开始就决定了的

@zrr1999 zrr1999 requested a review from SigureMo May 18, 2024 15:21
Comment on lines 193 to 196
if self.value in symbolic_input:
symbolic_input[self.value] += 1
else:
symbolic_input[self.value] = 1
Copy link
Member

Choose a reason for hiding this comment

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

这里也可以 setdefault(0) 一下

Comment on lines 687 to 690
if value in symbolic_input:
symbolic_input[value] += 1
else:
symbolic_input[value] = 1
Copy link
Member

Choose a reason for hiding this comment

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

同上,可以利用 setdefault

Copy link
Member

Choose a reason for hiding this comment

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

可以再测下 symbolic int 在传入 paddle API 时候能否正常触发组网(如 paddle.reshape(x, [n, 1])paddle.add(x, n)),感觉应该也是可以的~(但如果有比较麻烦的问题建议下一个 PR)

@zrr1999 zrr1999 changed the title [SOT] Support dynamic int input [SOT][dynamic shape] Support dynamic int input May 19, 2024
@zrr1999 zrr1999 requested a review from SigureMo May 19, 2024 06:15
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 🐾

@zrr1999 zrr1999 merged commit 147d758 into PaddlePaddle:develop May 19, 2024
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants