Skip to content

Conversation

@fdrocha
Copy link
Collaborator

@fdrocha fdrocha commented Nov 2, 2022

Stack from ghstack (oldest at bottom):

This fixes pytorch/torchdynamo#1515

To fix it, we need to keep track of whether a Triton variable is a scalar (so we can not use a mask when doing indirect loads through them). This requires a way of annotating variable names generated by CSE with properties.

So now CSE will use CSEVariable class to keep track of variables and let backends subclass it so they can annotate them with whatever information they want. TritonCSEVariable is such a subclass that track the is_scalar property.

cc @mlazos @soumith @voznesenskym @yanboliang @penguinwu @anijain2305 @EikanWang @jgong5 @Guobing-Chen @chunyuan-w @XiaobingSuper @zhuhaozhe @blzheng @Xia-Weiwen @wenzhe-nrv @jiayisunx @peterbell10 @desertfire @lezcano

@pytorch-bot
Copy link

pytorch-bot bot commented Nov 2, 2022

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/88347

Note: Links to docs will display an error until the docs builds have been completed.

⏳ No Failures, 37 Pending

As of commit dab7127:
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@fdrocha
Copy link
Collaborator Author

fdrocha commented Nov 2, 2022

@ngimel sorry, not sure what happened, had to close previous PR and do a new one. I copied your comment below:

Something went wrong with ghstack, this pr is only formatting changes, and the real meat is in #88336 which is closed, can you reopen in a more convenient shape? The general approach is fine, however I'd like the fix to also cover cases like #1654 where (it looks) the problematic tensors are 1-element but not scalars. Let me know if you have trouble reproing #1654
IIRC, the #1654 problem is that triton allows mask can be broadcasted to tensor, but not the other way around (which is reasonable), but we generate 1-element tensor and 128 element mask.

I will look to address the above.

@fdrocha fdrocha requested a review from ngimel November 2, 2022 18:58
@fdrocha fdrocha added the topic: not user facing topic category label Nov 2, 2022
def masked(mask, body, other):
code = BracesBuffer()
var = V.kernel.cse.newvar()
var = V.kernel.cse.newvar(is_scalar=False)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The arg name is_scalar is kind of confusing for cpp codegen. Variables are by default scalars in cpp and we are adding vectorization support but we manage the vector variable and scalar locally. Is it better named as is_triton_scalar and make it default to False to avoid changing cpp codegen? cc @EikanWang who is working on cpp vectorization support.

line = f"static_cast<float>({line})"
return self.cse.generate(self.loads, line)
# Not tracking whether results of loads are scalar
return self.cse.generate(self.loads, line, is_scalar_expr=False)
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto.

self.loads, f"reduction {name} {cexpr(index)}", write=False
self.loads,
f"reduction {name} {cexpr(index)}",
is_scalar_expr=False,
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto

@ngimel
Copy link
Collaborator

ngimel commented Nov 10, 2022

@fdrocha is this ready to review? Does it handle #1654 or do you want to give up on handling #1654 for now? Also please address @jgong5's comments

@fdrocha
Copy link
Collaborator Author

fdrocha commented Nov 11, 2022

@fdrocha is this ready to review? Does it handle #1654 or do you want to give up on handling #1654 for now? Also please address @jgong5's comments

No, please hold off.
I've been thinking about how to best handle #1654, and it will probably require changing the approach here a bit.
When I do that, I will do it in a way that addresses @jgong5's points, don't think it's worth doing in the present form.

This fixes pytorch/torchdynamo#1515

It keeps track of which variables in generated code are scalars, and does not include a mask when doing loads generated by these.

It uses variable names (e.g., tmp_scalar4) to keep track of which ones are scalars, which makes generated code a little more readable, but is a little hacky. Can try to make it better, but wanted to get some feedback on overall approach first.

This only really matters for triton backend, it would be nice to contain it to triton.py by didn't find a nice way of doing that.

cc mlazos soumith voznesenskym yanboliang penguinwu anijain2305 EikanWang jgong5 Guobing-Chen chunyuan-w XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx lezcano

[ghstack-poisoned]
@fdrocha
Copy link
Collaborator Author

fdrocha commented Nov 12, 2022

I have rewritten this to better separate the Triton specific aspects and make it a little more generic. This required introducing a new type CSEVariable to keep track of the is_scalar property, that could be extended for other properties if it is necessary. With the new approach is_scalar only appears on the triton backend. @jgong5 I think this addresses your concerns, please let me know.

@ngimel after thinking about it for a bit, it seems to me pytorch/torchdynamo#1654 is related but different enough that it is better to handle it separately. I will try to put up a PR for it as well.
Otherwise, this is ready to review, so please have a look and let me know what you think when you have a chance.

@fdrocha fdrocha changed the title [inductor] Track if variables are scalars [inductor] Introduce CSEVariable type and use it to track if Triton variables are scalar Nov 12, 2022
Copy link
Collaborator

@ngimel ngimel left a comment

Choose a reason for hiding this comment

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

Please add a testcase

def generate(
self, buffer: IndentedBuffer, expr: typing.Union[str, CSEVariable], write=True
) -> CSEVariable:
assert isinstance(expr, str) or isinstance(expr, CSEVariable), type(expr)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit:

assert isinstance(expr, (str, CSEVariable))

@lezcano lezcano requested a review from jgong5 November 14, 2022 09:51
…if Triton variables are scalar"


This fixes pytorch/torchdynamo#1515

To fix it, we need to keep track of whether a Triton variable is a scalar (so we can not use a mask when doing indirect loads through them). This requires a way of annotating variable names generated by CSE with properties.

So now CSE will use CSEVariable class to keep track of variables and let backends subclass it so they can annotate them with whatever information they want. TritonCSEVariable is such a subclass that track the `is_scalar` property.

cc mlazos soumith voznesenskym yanboliang penguinwu anijain2305 EikanWang jgong5 Guobing-Chen chunyuan-w XiaobingSuper zhuhaozhe blzheng Xia-Weiwen wenzhe-nrv jiayisunx lezcano

[ghstack-poisoned]
@fdrocha
Copy link
Collaborator Author

fdrocha commented Nov 14, 2022

Please add a testcase

The next PR in this stack is a more comprehensive test for this. I added a simple more explicit test to this one just now.

Copy link
Collaborator

@jgong5 jgong5 left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the abstraction!

@fdrocha fdrocha requested a review from ngimel November 15, 2022 09:09
@ngimel
Copy link
Collaborator

ngimel commented Nov 15, 2022

Simpler test is always beneficial

@fdrocha
Copy link
Collaborator Author

fdrocha commented Nov 15, 2022

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Nov 15, 2022
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

kulinseth pushed a commit to kulinseth/pytorch that referenced this pull request Dec 10, 2022
…ariables are scalar (pytorch#88347)

This fixes pytorch/torchdynamo#1515

To fix it, we need to keep track of whether a Triton variable is a scalar (so we can not use a mask when doing indirect loads through them). This requires a way of annotating variable names generated by CSE with properties.

So now CSE will use CSEVariable class to keep track of variables and let backends subclass it so they can annotate them with whatever information they want. TritonCSEVariable is such a subclass that track the `is_scalar` property.

Pull Request resolved: pytorch#88347
Approved by: https://github.com/jgong5, https://github.com/ngimel
@facebook-github-bot facebook-github-bot deleted the gh/fdrocha/23/head branch June 8, 2023 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants