Skip to content

Quantized ops fail if Tensor.__getitem__ has previously been patched  #50710

@jansel

Description

@jansel

🐛 Bug

I found this one puzzling. I was working on unrelated code and all of a sudden I saw failures coming from test_quantization.py. I narrowed it down to whenever I modified Tensor.__getitem__ quantization would break, even if I undid my change.

This causes the issue:

with unittest.mock.patch.object(torch.Tensor, "__getitem__"):
    pass  # do unrelated stuff with a patched __getitem__
# back to normal (except quantization is broken)
assert torch.Tensor.__getitem__ is torch._C._TensorBase.__getitem__  # passes

The following also causes the same issue (__getitem__ is defined on _TensorBase, so del should restore original state):

torch.Tensor.__getitem__ = None
del torch.Tensor.__getitem__
assert torch.Tensor.__getitem__ is torch._C._TensorBase.__getitem__  # passes

To Reproduce

Apply the following patch

diff --git a/test/test_quantization.py b/test/test_quantization.py
index 1c370913c6..b2f011a746 100644
--- a/test/test_quantization.py
+++ b/test/test_quantization.py
@@ -88,5 +88,13 @@ from quantization.test_equalize import TestEqualizeEager  # noqa: F401
 # Bias Correction
 from quantization.test_bias_correction import TestBiasCorrection  # noqa: F401
 
+import torch, unittest.mock
+
+assert torch.Tensor.__getitem__ is torch._C._TensorBase.__getitem__
+with unittest.mock.patch.object(torch.Tensor, "__getitem__"):
+    pass  # do unrelated stuff with a patched __getitem__
+# back to normal (except quantization is broken)
+assert torch.Tensor.__getitem__ is torch._C._TensorBase.__getitem__
+
 if __name__ == '__main__':
     run_tests()

Run

pytest test/test_quantization.py

Get many errors:

======================================================= short test summary info ========================================================
FAILED test/test_quantization.py::TestQuantizedOps::test_add_scalar_relu - IndexError: The shape of the mask [2] at index 0 does not ...
FAILED test/test_quantization.py::TestQuantizedOps::test_cat_nhwc - IndexError: The shape of the mask [140, 1, 1] at index 0 does not...
FAILED test/test_quantization.py::TestQuantizedOps::test_mul_scalar_relu - IndexError: The shape of the mask [2] at index 0 does not ...
FAILED test/test_quantization.py::TestDynamicQuantizedLinear::test_qlinear - IndexError: The shape of the mask [4] at index 0 does no...
FAILED test/test_quantization.py::TestQuantizedLinear::test_qlinear - IndexError: The shape of the mask [4] at index 0 does not match...
FAILED test/test_quantization.py::TestQNNPackOps::test_hardtanh - AssertionError: False is not true : Quantized representations faile...
FAILED test/test_quantization.py::TestQNNPackOps::test_qnnpack_relu - IndexError: The shape of the mask [2] at index 0 does not match...
FAILED test/test_quantization.py::TestFakeQuantize::test_backward_per_channel - hypothesis.errors.MultipleFailures: Hypothesis found ...
FAILED test/test_quantization.py::TestFakeQuantize::test_fq_module_per_channel - hypothesis.errors.MultipleFailures: Hypothesis found...
FAILED test/test_quantization.py::TestFakeQuantize::test_fq_module_per_tensor - hypothesis.errors.MultipleFailures: Hypothesis found ...
FAILED test/test_quantization.py::TestFakeQuantize::test_learnable_backward_per_channel_cpu - IndexError: The shape of the mask [2] a...
FAILED test/test_quantization.py::TestFakeQuantize::test_learnable_backward_per_tensor_cpu - hypothesis.errors.MultipleFailures: Hypo...
FAILED test/test_quantization.py::TestFakeQuantize::test_learnable_backward_per_tensor_cuda - hypothesis.errors.MultipleFailures: Hyp...
FAILED test/test_quantization.py::TestFakeQuantize::test_learnable_py_module_backward_per_channel - IndexError: The shape of the mask...
FAILED test/test_quantization.py::TestFakeQuantize::test_learnable_py_module_backward_per_tensor - IndexError: The shape of the mask ...
================================= 15 failed, 417 passed, 11 skipped, 298 warnings in 217.05s (0:03:37) =================================

Expected behavior

No errors

Environment

PyTorch version: master
Is debug build: False
CUDA used to build PyTorch: 11.0
ROCM used to build PyTorch: N/A
OS: Ubuntu 20.10 (x86_64)
GCC version: (Ubuntu 10.2.0-13ubuntu1) 9.3.0
CMake version: version 3.16.3
Python version: 3.7 (64-bit runtime)

cc @jerryzh168 @jianyuh @dzhulgakov @raghuramank100 @jamesr66a @vkuzo

Metadata

Metadata

Assignees

Labels

low priorityWe're unlikely to get around to doing this in the near futureoncall: quantizationQuantization support in PyTorch

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions