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

Infinite Recursion in triton.compile() due to flag_gems.use_gems() #111

Open
DuanYaQi opened this issue Jul 10, 2024 · 3 comments
Open

Infinite Recursion in triton.compile() due to flag_gems.use_gems() #111

DuanYaQi opened this issue Jul 10, 2024 · 3 comments

Comments

@DuanYaQi
Copy link
Collaborator

DuanYaQi commented Jul 10, 2024

Issue

There is an identified issue in the triton.compile() pipeline where the flag_gems.use_gems() is being activated all the time. This leads to an infinite recursion problem when certain functions are called to be compiled.

Specifically, if torch.ne.Scalar function is invoked during the triton.compile() pipeline, it will trigger another call to triton.compile() by lib.impl("ne.Scalar", ne_scalar, "CUDA") in FlagGems/src/flag_gems/__init__.py::enable(), causing an infinite loop and eventually a stack overflow.

@StrongSpoon
Copy link
Collaborator

could you please provide a demo code to recurrence this problem?

@DuanYaQi
Copy link
Collaborator Author

DuanYaQi commented Jul 11, 2024

could you please provide a demo code to recurrence this problem?

You can add the following code at the beginning of triton.compile() function to reproduce the issue.

tmp = torch.rand(256).cuda()
tmp.ne(0)

it will recursively compile ne.Scalar,

or we can confirm that the triton.compile() pipeline does not depend on torch. But I haven’t seen any descriptions related to this in the community anywhere.

FlagGems Commit: 3c62c9c
Python 3.10.12
Torch: 2.3.1
Triton: 2.3.1
Pytest: 8.2.2

@iclementine
Copy link
Collaborator

iclementine commented Jul 17, 2024

In triton of commit id fc7a8e35819bda632bdcf1cf75fd9abe4d4e077a, the JITFunction treats all arguments with type annotation as constants, which is not expected behavior. Only those arguments annotated with tl.constexpr should be included.

https://github.com/triton-lang/triton/blob/fc7a8e35819bda632bdcf1cf75fd9abe4d4e077a/python/triton/runtime/jit.py#L316C1-L319C93

    def __init__(self, fn, version=None, do_not_specialize=None):
        ...
        # annotations
        self.annotations = {self.arg_names.index(name): ty for name, ty in fn.__annotations__.items()}
        self.__annotations__ = fn.__annotations__
        # index of constexprs
        self.constexprs = [self.arg_names.index(ann) for ann in self.__annotations__.keys()]

So as a workaround, you can remove type annotation for parameters other than tl.constexprs. To change the generated code by pointwise_dynamic, you need to modify generate_pointwise_kernel in src/flag_gems/utils/pointwise_dynamic.py.

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

No branches or pull requests

3 participants