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

Make addmm meta kernel consistent with mm #84960

Conversation

antoniojkim
Copy link
Collaborator

Change the names of the parameters in the addmm meta kernel to be more consistent with mm. Functionally, the only difference in behaviour should be that addmm meta kernel gets its options from the input tensor instead of from the bias parameter.

Fixes #84930

CC: @ezyang @ngimel @wconstab @ke1337 @glebk-cerebras

@pytorch-bot
Copy link

pytorch-bot bot commented Sep 13, 2022

🔗 Helpful Links

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

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

✅ No Failures

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

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

@ezyang
Copy link
Contributor

ezyang commented Sep 13, 2022

No, I don't understand this change. Here's the schema for addmm

- func: addmm(Tensor self, Tensor mat1, Tensor mat2, *, Scalar beta=1, Scalar alpha=1) -> Tensor
  structured_delegate: addmm.out
  variants: function, method
  dispatch:
    SparseCPU: addmm_sparse_dense_cpu
    SparseCUDA: addmm_sparse_dense_cuda
    SparseCsrCPU, SparseCsrCUDA: addmm_sparse_compressed_dense

self is the first arg. Reordering it in the macro doesn't make sense?

@antoniojkim
Copy link
Collaborator Author

self is the first arg. Reordering it in the macro doesn't make sense?

oh, I only reordered it to match the mm meta kernel. I didn't need to do this. I can change it back and only make the minimum changes required to resolve the issue

Copy link
Contributor

@ezyang ezyang left a comment

Choose a reason for hiding this comment

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

OK, better. Can we get a test?

@@ -55,7 +55,7 @@ namespace meta {
mat1.sizes()[0], "x", mat1.sizes()[1], " and ", mat2.sizes()[0], "x", mat2.sizes()[1], ")"); \
\
auto names = at::namedinference::propagate_names_for_addmm(mat1, mat2, self); \
set_output_raw_strided(0, {mat1.sizes()[0], mat2.sizes()[1]}, {}, self.options(), names);
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we also check that mat1, mat2 and self dtypes are the same? The actual implementation will error out on the different types.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Seeing as this is an implementation specific detail, and not related to shape inference, is the meta kernel the best place to put this check? I mean, it already errors out in practice which means that the check already exists in the correct places.

Copy link
Contributor

Choose a reason for hiding this comment

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

the point of the meta kernel is not just for shape inference, it is also used in other contexts such as to run through a sequence of operations and produce the same error messages you'd see in eager without actually running the ops in eager. so the practice is to handle all the shape/dtype inference as well as all the error checking in the meta kernel in a way consistent to eager.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah, I see. Okay, I can add that check then

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oh wait, I can add a check to see if the dtypes of self and mat2 match as they are the bias and weight respectively. However, checking if the input dtype matches the parameters will fail in the case that I originally made the issue for

Copy link
Collaborator

Choose a reason for hiding this comment

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

Cuda addmm behavior is the same, all the dtypes have to match.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The case I'm referring to isn't in any execution mode. Its for custom mixed precision on custom vendor hardware that is only traced via lazy tensors. Hence, the case

torch.float32 torch.float16 torch.float32

should be allowed here and should output type torch.float16 to match the input. The actual casting is done further down our stack, not at the top level.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, then I think I would probably claim your custom AMP is wrong. The two ways I can think of solving this: (1) explicitly generate the casts and fuse then away in your backend compiler, (2) produce an alternative matmul that is type promoting and have the backend target that

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

okay, we've decided to do with solution (1). In which case, the changes in this PR aren't quite necessary anymore. Do we still want to merge this in (including the added dtype checks)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Dtype checks would be appreciated!

@IvanYashchuk IvanYashchuk removed their request for review September 14, 2022 07:58
@lezcano lezcano removed their request for review September 14, 2022 08:35
@antoniojkim antoniojkim force-pushed the antoniojkim/fix_addmm_meta_kernel branch from c0d85b2 to 4104516 Compare September 16, 2022 14:53
@@ -48,14 +48,16 @@ namespace detail {
namespace meta {

#define ADDMM_META() \
TORCH_CHECK(self.scalar_type() == mat2.scalar_type(), "self and mat2 must have the same dtype"); \
TORCH_CHECK(mat1.scalar_type() == mat2.scalar_type(), "mat1 and mat2 must have the same dtype"); \
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to add an ErrorInput to the addmm OpInfo test for this case

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure what you mean by ErrorInput and I can't find any examples in the test/ directory. Can you please provide an example of what you mean?


try:
out_nobias = fc_nobias(input)
self.assertTrue(False) # Should never reach here as the above line should throw exception
Copy link
Contributor

Choose a reason for hiding this comment

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

More idiomatic is assertRaisesRegex/assertRaises

def test_addmm_invalid_dtype(self):
"""Tests that the addmm meta kernel returns the correct output type"""
input = torch.ones(2, 2, dtype=torch.float16).to("lazy")
self.assertTrue(input.dtype == torch.float16)
Copy link
Contributor

Choose a reason for hiding this comment

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

more idiomatic: assertEqual

@ezyang
Copy link
Contributor

ezyang commented Sep 19, 2022

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

@pytorchbot successfully started a merge job. Check the current status here.
The merge job was triggered without a flag. This means that your change will be merged once all checks on your PR have passed (ETA: 0-4 Hours). If this is not the intended behavior, feel free to use some of the other merge options in the wiki.
Please reach out to the PyTorch DevX Team with feedback or questions!

@github-actions
Copy link
Contributor

Hey @antoniojkim.
You've committed this PR, but it does not have both a 'release notes: ...' and 'topics: ...' label. Please add one of each to the PR. The 'release notes: ...' label should represent the part of PyTorch that this PR changes (fx, autograd, distributed, etc) and the 'topics: ...' label should represent the kind of PR it is (not user facing, new feature, bug fix, perf improvement, etc). The list of valid labels can be found here for the 'release notes: ...' and here for the 'topics: ...'.
For changes that are 'topic: not user facing' there is no need for a release notes label.


fc_nobias = torch.nn.Linear(2, 2, bias=False, dtype=float32).to("lazy")

with self.assertRaises(Exception):
Copy link
Collaborator

Choose a reason for hiding this comment

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

should this be RuntimeError?

mehtanirav pushed a commit that referenced this pull request Oct 4, 2022
Change the names of the parameters in the `addmm` meta kernel to be more consistent with `mm`. Functionally, the only difference in behaviour should be that `addmm` meta kernel gets its options from the `input` tensor instead of from the `bias` parameter.

Fixes #84930

CC: @ezyang @ngimel @wconstab @ke1337 @glebk-cerebras

Pull Request resolved: #84960
Approved by: https://github.com/ezyang
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.

addmm meta kernel type definition incorrect in edge case
7 participants