Skip to content

🐛 [Bug] With enable_autocast, an in-place update is lost through a view taken before it #4990

Open

Description

@shoumikhin

Bug Description

With enable_autocast=True, an in-place update can be lost when a view of the same tensor was taken before it. In the example below, the view keeps the value from before add_. The default path (no autocast) gives the right answer, and a plain in-place op with no view, such as relu_, also works.

This comes from #4978 (not merged yet). Without it, the autocast output is correct.

To Reproduce

import torch
import torch_tensorrt


class M(torch.nn.Module):
    def forward(self, x):
        y = x.float()
        z = y.view(-1)
        y.add_(1)
        return z * 2


m = M().eval()
x = torch.tensor([[1.0, -2.0]], device="cuda", dtype=torch.float16)
ep = torch.export.export(m, (x,))
for kw in ({}, dict(enable_autocast=True, autocast_low_precision_type=torch.float16)):
    trt = torch_tensorrt.dynamo.compile(ep, arg_inputs=[x], min_block_size=1, **kw)
    print("autocast" if kw else "default ", "eager", m(x).tolist(), "tensorrt", trt(x).tolist())

With #4978:

default  eager [4.0, -2.0] tensorrt [4.0, -2.0]
autocast eager [4.0, -2.0] tensorrt [2.0, -4.0]

z should see the add_, so the answer is [4.0, -2.0]. The autocast engine returns [2.0, -4.0], which is z before the add.

Expected behavior

The autocast engine matches PyTorch. When autocast changes the type of a tensor, every view of that tensor and every in-place update to it should stay together. Simply skipping views and in-place ops in the autocast pass is not enough, because that breaks existing autocast tests.

Environment

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions