Skip to content

[SOT][CINN] Only create SymbolicVariable when value >= 2 & add corresponding guards and C++ constraints #71748

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

Merged
merged 31 commits into from
Apr 1, 2025

Conversation

GoldenStain
Copy link
Contributor

@GoldenStain GoldenStain commented Mar 18, 2025

PR Category

Execute Infrastructure

PR Types

Performance

Description

1. Conditional symbolic variable creation in SOT

Modification: Symbolic variables are now only instantiated when tensor dimensions ≥ 2 in the SOT compiler phase.

Impact: Reduce the number of kernels generated by CINN in later pass.

2. Add corresponding Guards

Current approach: Implemented temporary validation using StringifiedGuard
Future plan: Replace it with FasterGuard.

3. Cross-language constraint (Python → C++)

Implementation:

  1. Introduced ConstrainedInputSpec subclass extending InputSpec
  2. Designed constraint storage format:
    # ConstrainedInputSpec.ranges: [list[tuple[int, int | None, int | None]]]
    [
      (index of dim, min, max)
    ]
  3. Established serialization pipeline: Python list → PyBind11 type conversion → C++ InputDynamicDimSpec
    Extract the constraint information from ConstrainedInputSpec, then pass it to ConcreteProgram, where it is finally passed on to PartialProgramLayer, where we bind the constraint information using interface exposed by Pybind.

4. Add new "Append" mode for SpecifyInputDynamicDim as AppendInputDynamicDim

This new interface can be useful in future expansion.

5. Add new __init__() arguments for PartialProgramLayer and ConcreteProgram

Now both the 2 classes have a new key-word argument named constraints, whose default value is None.

6. Add new __init__() argument for MetaInfo

Add a new positional argument spec_name for MetaInfo in __init__(), remember to pass it when creating new MetaInfo.

PCard-66972

Copy link

paddle-bot bot commented Mar 18, 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.

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.

其他的看起来没啥问题

@SigureMo
Copy link
Member

突然想起来这个 PR 是不是没把 >=2 加到 guard 里?从正确性上来讲这是不够的,需要测一下:

shape = [2, 2, 3] # cache miss,静态 shape
shape = [3, 2, 3] # cache miss,动态 shape
shape = [1, 2, 3] # cache miss,静态 shape,按我理解现在的实现这里是会 hit 到上面的动态 shape 的,这是不对的

@GoldenStain
Copy link
Contributor Author

GoldenStain commented Mar 21, 2025

突然想起来这个 PR 是不是没把 >=2 加到 guard 里?从正确性上来讲这是不够的,需要测一下:

shape = [2, 2, 3] # cache miss,静态 shape
shape = [3, 2, 3] # cache miss,动态 shape
shape = [1, 2, 3] # cache miss,静态 shape,按我理解现在的实现这里是会 hit 到上面的动态 shape 的,这是不对的

guard的修改在TODO的第三项,在完成了第三个改动之后再加入相应的单测。

@SigureMo SigureMo self-requested a review March 25, 2025 03:25
@GoldenStain GoldenStain changed the title [SOT] Only create symbolic_variable when value is greater than or equal to 2 [SOT] Only create symbolic_variable when value is greater than or equal to 2 & add corresponding guards and C++ constraints Mar 27, 2025
@GoldenStain
Copy link
Contributor Author

PartialProgramLayer添加参数时:

def __init__(
        self,
        main_program,
        inputs,
        outputs,
        constrained_inputs=None,
        parameters=None,
        **kwargs,
    ):

把constrained_inputs放在了parameters的前面,导致按位置传递参数时,拿不到正确的parameters,导致scope拿不到正确的信息,在save后load时挂掉。

@GoldenStain GoldenStain changed the title [SOT] Only create symbolic_variable when value is greater than or equal to 2 & add corresponding guards and C++ constraints [Dy2St][SOT] Only create symbolic_variable when value is greater than or equal to 2 & add corresponding guards and C++ constraints Mar 28, 2025
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.

其余目测没啥大问题

@SigureMo
Copy link
Member

另外 approval 也需要看一下,看起来可能是信息太短之类的问题

zyfncg
zyfncg previously approved these changes Mar 31, 2025
Comment on lines 133 to 137
if (!to_append) {
shape_analysis.SetInputDynamicDimSpec(input_dynamic_dim_spec);
} else {
shape_analysis.AppendInputDynamicDimSpec(input_dynamic_dim_spec);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

可以写成两个函数接口,语义上更清晰

Comment on lines +112 to +117
if (std::get<1>(range_info).has_value()) {
range.min = std::get<1>(range_info).value();
}
if (std::get<2>(range_info).has_value()) {
range.max = std::get<2>(range_info).value();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

可以显式设置个min max的默认值

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Range结构体内部有min max的默认值

SigureMo
SigureMo previously approved these changes Mar 31, 2025
@SigureMo SigureMo changed the title [Dy2St][SOT] Only create symbolic_variable when value is greater than or equal to 2 & add corresponding guards and C++ constraints [SOT][CINN] Only create symbolicVariable when value is greater than or equal to 2 & add corresponding guards and C++ constraints Mar 31, 2025
@SigureMo SigureMo changed the title [SOT][CINN] Only create symbolicVariable when value is greater than or equal to 2 & add corresponding guards and C++ constraints [SOT][CINN] Only create SymbolicVariable when value is greater than or equal to 2 & add corresponding guards and C++ constraints Mar 31, 2025
@GoldenStain GoldenStain dismissed stale reviews from SigureMo and zyfncg via d74b530 March 31, 2025 14:05
@SigureMo SigureMo changed the title [SOT][CINN] Only create SymbolicVariable when value is greater than or equal to 2 & add corresponding guards and C++ constraints [SOT][CINN] Only create SymbolicVariable when value >= 2 & add corresponding guards and C++ constraints Apr 1, 2025
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.

Great work!

LGTMeow 🐾

@SigureMo SigureMo merged commit 0da6563 into PaddlePaddle:develop Apr 1, 2025
34 of 35 checks passed
YqGe585 pushed a commit to YqGe585/Paddle that referenced this pull request May 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants