Repository navigation
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/129688
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Merge Blocking SEVsThere is 1 active merge blocking SEVs. Please view them below:
If you must merge, use ✅ You can merge normally! (3 Unrelated Failures)As of commit 5b8219d with merge base ec284d3 ( FLAKY - The following jobs failed but were likely due to flakiness present on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
desertfire
left a comment
There was a problem hiding this comment.
Looks reasonable to me. Is it possible to add a unit test?
|
yep I can probably make a simple test that just passes a view of a param into a matmul (I think that will error today). Where do most AOTI tests live? I'll put it in |
|
fixed lint and added a test |
More context [here](#129682 (comment)), but this change was enough to get this AOTI + float8 repro running for me (below). Previously, it would fail an assertion [here](https://github.com/pytorch/pytorch/blob/main/torch/_meta_registrations.py#L5387) at inductor lowering time. It looks like during lowering, we were supposed to pass `param.transpose(1, 0)` as the second argument to the scaled_mm kernel. But in the inductor IR, this object is a `ReinterpretView` with `get_name()` equal to one of the param constants, so we would end up passing the constant directly into the kernel, instead of performing the view first. I'm not totally sure if this is the right place to make the change, so interested in any thoughts from inductor folks (cc desertfire eellison ) ``` import torch from torch.export import export from torch.export._trace import _export # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the BSD 3-Clause license found in the # LICENSE file in the root directory of this source tree. import copy import io import random import unittest import pytest import torch import torch.nn as nn import torch.nn.functional as F from float8_experimental.float8_dynamic_linear import Float8DynamicLinear from float8_experimental.float8_linear_utils import swap_linear_with_float8_linear from float8_experimental.float8_tensor import Float8Tensor from float8_experimental.float8_utils import compute_error random.seed(0) torch.manual_seed(0) is_H100 = torch.cuda.is_available() and torch.cuda.get_device_capability() >= (9, 0) import torch.nn.utils.parametrize as parametrize # NOTE: we should upstream this directly into export and make it more automatic! class UnwrapTensorSubclass(torch.nn.Module): def forward(self, *tensors): todo = list(tensors) for tp, meta, inner_tensors in reversed(self.rebuild_stack): nb_tensor = len(inner_tensors) inner_tensors = {a: b for a, b in zip(inner_tensors, todo[-nb_tensor:])} todo = todo[nb_tensor:] rebuilt = tp.__tensor_unflatten__(inner_tensors, meta, None, None) todo.append(rebuilt) assert len(todo) == 1 return todo[0] def right_inverse(self, tensor): assert type(tensor) is not torch.Tensor rebuild_stack = [] plain_tensors = [] todo = [tensor] while todo: obj = todo.pop() inner_tensors, metadata = obj.__tensor_flatten__() rebuild_stack.append((type(obj), metadata, inner_tensors)) for attr_name in inner_tensors: val = getattr(obj, attr_name) if type(val) is torch.Tensor: plain_tensors.append(val) else: assert isinstance(val, torch.Tensor) todo.append(val) self.rebuild_stack = rebuild_stack return plain_tensors def unwrap_tensor_subclass(model, filter_fn=None): for name, child in model.named_children(): if ( isinstance(child, Float8DynamicLinear) and hasattr(child, "weight") and type(child.weight) is not torch.Tensor and isinstance(child.weight, torch.Tensor) ): parametrize.register_parametrization(child, "weight", UnwrapTensorSubclass()) unwrap_tensor_subclass(child) return model class FeedForward(nn.Module): def __init__(self) -> None: super().__init__() self.w1 = nn.Linear(4096, 14336, bias=False) self.w3 = nn.Linear(4096, 14336, bias=False) self.w2 = nn.Linear(14336, 4096, bias=False) def forward(self, x: torch.Tensor) -> torch.Tensor: return self.w2(F.silu(self.w1(x)) * self.w3(x)) def reset_parameters(self): for m in self.modules(): if isinstance(m, nn.Linear): m.reset_parameters() export_model = FeedForward().to("cuda") swap_linear_with_float8_linear( export_model, Float8DynamicLinear, from_float_kwargs={"pre_quantize_weight": True}, ) export_model = unwrap_tensor_subclass(export_model) batch_size = 4 num_tokens = 1024 embedding_dim = 4096 input_tensor = torch.randn( batch_size, num_tokens, embedding_dim, device="cuda", dtype=torch.float32 ) example_args = (input_tensor,) # NOTE: this breaks unless we use strict=False, pre_dispatch=False! exported_program: torch.export.ExportedProgram = _export( export_model, example_args, strict=False, pre_dispatch=False, ) with torch.no_grad(): so_path = torch._inductor.aot_compile(exported_program.module(), example_args) print(so_path) ``` cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy chenyang78 kadeng muchulee8 ColinPeppler amjames desertfire chauhang [ghstack-poisoned]
| elif ( | ||
| not isinstance(x, BaseView) | ||
| and x.get_name() in V.graph.torchbind_constants | ||
| ): |
There was a problem hiding this comment.
I don't think it's possible to have a view of torchbind_constants. torchbind_constants are non tensors.
| if x.get_name() in V.graph.constants: | ||
| # if x is a view of a constant, we need to realize the view | ||
| # (we can't pass the constant into the kernel directly) | ||
| if not isinstance(x, BaseView) and x.get_name() in V.graph.constants: |
There was a problem hiding this comment.
Might be better to reconstruct view from the constant, but dont know if it really matters.
That was added because in some cases FakeTensor will have constant which was needed to not-graph break.
e.g.
tensor([0:torch.tensor(8)])
But yea, dont think it matters here, unlikely to have view of 1-dim torch.tensor which is needing for indexing
There was a problem hiding this comment.
oh interesting...
lmk if you want me to try to get that supported in this PR, otherwise I'll probably just leave as-is (alternatively, hopefully const-prop on views just works)
There was a problem hiding this comment.
i think it's probably fine
|
@pytorchbot merge |
Merge failedReason: This PR needs a If not, please add the To add a label, you can comment to pytorchbot, for example For more information, see Details for Dev Infra teamRaised by workflow job |
|
@pytorchbot merge |
Merge startedYour 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 |
More context here, but this change was enough to get this AOTI + float8 repro running for me (below).
Previously, it would fail an assertion here at inductor lowering time. It looks like during lowering, we were supposed to pass
param.transpose(1, 0)as the second argument to the scaled_mm kernel. But in the inductor IR, this object is aReinterpretViewwithget_name()equal to one of the param constants, so we would end up passing the constant directly into the kernel, instead of performing the view first.I'm not totally sure if this is the right place to make the change, so interested in any thoughts from inductor folks (cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @peterbell10 @ipiszy @yf225 @chenyang78 @kadeng @muchulee8 @ColinPeppler @amjames @desertfire @chauhang @eellison )
Stack from ghstack (oldest at bottom):
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @peterbell10 @ipiszy @chenyang78 @kadeng @muchulee8 @ColinPeppler @amjames @desertfire @chauhang