|
2 | 2 | # SPDX-License-Identifier: BSD-3-Clause |
3 | 3 |
|
4 | 4 | import unittest |
| 5 | +from unittest import mock |
5 | 6 |
|
6 | 7 | import numpy as np |
7 | 8 | import tensorrt as trt |
8 | 9 | import torch |
9 | 10 | from torch.testing._internal.common_utils import TestCase, run_tests |
10 | | -from torch_tensorrt import _enums |
| 11 | +from torch_tensorrt import ENABLED_FEATURES, _enums |
| 12 | +from torch_tensorrt.dynamo import _refit |
11 | 13 | from torch_tensorrt.dynamo._settings import CompilationSettings |
12 | 14 | from torch_tensorrt.dynamo.conversion import aten_ops_converters, impl |
13 | 15 | from torch_tensorrt.dynamo.conversion._ConversionContext import ConversionContext |
@@ -283,9 +285,19 @@ def supported(*values, block_size=(1, 16)): |
283 | 285 | self.assertTrue(supported(-128, 127, block_size=(1, 256))) |
284 | 286 |
|
285 | 287 | def _int4_matmul( |
286 | | - self, qdata, scale, x, dtype, runtime_scale=False, refittable=False |
| 288 | + self, |
| 289 | + qdata, |
| 290 | + scale, |
| 291 | + x, |
| 292 | + dtype, |
| 293 | + runtime_scale=False, |
| 294 | + refittable=False, |
| 295 | + refit_qdata=None, |
287 | 296 | ): |
288 | | - """Build and run x @ dequantize_affine(qdata, scale).T, the scale optionally an input.""" |
| 297 | + """Build and run x @ dequantize_affine(qdata, scale).T, the scale optionally an input. |
| 298 | +
|
| 299 | + With refit_qdata, the engine is refit with those weights before it runs. |
| 300 | + """ |
289 | 301 | skip_if_trt_rtx_turing(self, "INT4 dequantize into a matmul") |
290 | 302 | builder = trt.Builder(trt.Logger(trt.Logger.ERROR)) |
291 | 303 | network = builder.create_network( |
@@ -322,6 +334,30 @@ def _int4_matmul( |
322 | 334 | plan = builder.build_serialized_network(network, config) |
323 | 335 | self.assertIsNotNone(plan) |
324 | 336 | engine = trt.Runtime(trt.Logger(trt.Logger.ERROR)).deserialize_cuda_engine(plan) |
| 337 | + if refit_qdata is not None: |
| 338 | + new_ctx = ConversionContext( |
| 339 | + builder.create_network( |
| 340 | + 1 << int(trt.NetworkDefinitionCreationFlag.STRONGLY_TYPED) |
| 341 | + ), |
| 342 | + ctx.compilation_settings, |
| 343 | + ) |
| 344 | + impl.quantize.dequantize_affine( |
| 345 | + new_ctx, |
| 346 | + torch.ops.aten.mul.Tensor, |
| 347 | + None, |
| 348 | + "dq", |
| 349 | + refit_qdata, |
| 350 | + [1, 128], |
| 351 | + scale, |
| 352 | + dtype, |
| 353 | + ) |
| 354 | + with mock.patch.object( |
| 355 | + _refit, |
| 356 | + "construct_refit_mapping", |
| 357 | + return_value=new_ctx.weight_refit_map, |
| 358 | + ): |
| 359 | + _refit._refit_single_trt_engine_with_gm(None, engine, []) |
| 360 | + qdata = refit_qdata |
325 | 361 | context = engine.create_execution_context() |
326 | 362 | buffers = {"x": x.to("cuda", dtype), "scale": scale.cuda()} |
327 | 363 | out = torch.empty(x.shape[0], qdata.shape[0], device="cuda", dtype=dtype) |
@@ -360,6 +396,18 @@ def test_int4_dequantize_in_a_refittable_engine(self): |
360 | 396 | ) |
361 | 397 | torch.testing.assert_close(out, expected, rtol=2e-2, atol=1.0) |
362 | 398 |
|
| 399 | + @unittest.skipIf(not ENABLED_FEATURES.refit, "Refit is not available") |
| 400 | + def test_int4_dequantize_refits_new_weights(self): |
| 401 | + # Refit, which a cached engine also goes through, loads the INT4 weights. |
| 402 | + qdata = torch.randint(-8, 8, (512, 1024), dtype=torch.int8) |
| 403 | + new_qdata = torch.randint(-8, 8, (512, 1024), dtype=torch.int8) |
| 404 | + scale = torch.rand(512, 8, dtype=torch.bfloat16) + 0.5 |
| 405 | + x = torch.randn(4, 1024) |
| 406 | + out, expected = self._int4_matmul( |
| 407 | + qdata, scale, x, torch.bfloat16, refittable=True, refit_qdata=new_qdata |
| 408 | + ) |
| 409 | + torch.testing.assert_close(out, expected, rtol=2e-2, atol=1.0) |
| 410 | + |
363 | 411 | def test_int4_dequantize_in_the_scale_dtype_adds_no_cast(self): |
364 | 412 | # A cast between the DQ and its matmul stops TensorRT from fusing them. |
365 | 413 | skip_if_trt_rtx_turing(self, "INT4 dequantize into a matmul") |
|
0 commit comments