Skip to content

prepare_pt2e observes a tensor before an in-place write through a view, so the quantized model ignores its input #5040

Open

Description

@msluszniak

prepare_pt2e observes a tensor before it is written in place through a view, so after convert_pt2e the model reads the unfilled tensor and ignores its input. torchvision's GeneralizedRCNNTransform.batch_images does this (new_full + copy_), so quantized torchvision detectors output a constant.

import torch
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
from torchao.testing.pt2e._xnnpack_quantizer import (
    XNNPACKQuantizer,
    get_symmetric_quantization_config,
)


class M(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.conv = torch.nn.Conv2d(3, 3, 3)

    def forward(self, x):
        buf = x.new_zeros(1, 3, 8, 8)
        buf.select(0, 0).copy_(x)
        return self.conv(buf)


x1, x2 = torch.randn(3, 8, 8), torch.randn(3, 8, 8)
m = torch.export.export(M().eval(), (x1,)).module()
m = prepare_pt2e(m, XNNPACKQuantizer().set_global(get_symmetric_quantization_config()))
m(x1)
m = convert_pt2e(m)
print(torch.equal(m(x1), m(x2)))  # True: the output ignores the input

The conv input observer is inserted right after new_zeros, before select/copy_. If the quantizer also annotates select (as ExecuTorch's QNN quantizer does), its observers break the view chain, so the write lands on a dequantized copy.

torch 2.13, torchao main (1884ad0).

Activity

  1. msluszniak commented on Nov 3, 2026

    @msluszniak
    Author

    This one will be resolved from the level of ExecuTorch exports aka pytorch/executorch#24499. Since I don't have write access I cannot transfer this issue to the correct repo. @jerryzh168, could you transfer it to the ExecuTorch repository?

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