|
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 |
@@ -289,9 +291,19 @@ def dequantize(*values): |
289 | 291 | dequantize(-8, 7) |
290 | 292 |
|
291 | 293 | def _int4_matmul( |
292 | | - self, qdata, scale, x, dtype, runtime_scale=False, refittable=False |
| 294 | + self, |
| 295 | + qdata, |
| 296 | + scale, |
| 297 | + x, |
| 298 | + dtype, |
| 299 | + runtime_scale=False, |
| 300 | + refittable=False, |
| 301 | + refit_qdata=None, |
293 | 302 | ): |
294 | | - """Build and run x @ dequantize_affine(qdata, scale).T, the scale optionally an input.""" |
| 303 | + """Build and run x @ dequantize_affine(qdata, scale).T, the scale optionally an input. |
| 304 | +
|
| 305 | + With refit_qdata, the engine is refit with those weights before it runs. |
| 306 | + """ |
295 | 307 | skip_if_trt_rtx_turing(self, "INT4 dequantize into a matmul") |
296 | 308 | builder = trt.Builder(trt.Logger(trt.Logger.ERROR)) |
297 | 309 | network = builder.create_network( |
@@ -328,6 +340,30 @@ def _int4_matmul( |
328 | 340 | plan = builder.build_serialized_network(network, config) |
329 | 341 | self.assertIsNotNone(plan) |
330 | 342 | engine = trt.Runtime(trt.Logger(trt.Logger.ERROR)).deserialize_cuda_engine(plan) |
| 343 | + if refit_qdata is not None: |
| 344 | + new_ctx = ConversionContext( |
| 345 | + builder.create_network( |
| 346 | + 1 << int(trt.NetworkDefinitionCreationFlag.STRONGLY_TYPED) |
| 347 | + ), |
| 348 | + ctx.compilation_settings, |
| 349 | + ) |
| 350 | + impl.quantize.dequantize_affine( |
| 351 | + new_ctx, |
| 352 | + torch.ops.aten.mul.Tensor, |
| 353 | + None, |
| 354 | + "dq", |
| 355 | + refit_qdata, |
| 356 | + [1, 128], |
| 357 | + scale, |
| 358 | + dtype, |
| 359 | + ) |
| 360 | + with mock.patch.object( |
| 361 | + _refit, |
| 362 | + "construct_refit_mapping", |
| 363 | + return_value=new_ctx.weight_refit_map, |
| 364 | + ): |
| 365 | + _refit._refit_single_trt_engine_with_gm(None, engine, []) |
| 366 | + qdata = refit_qdata |
331 | 367 | context = engine.create_execution_context() |
332 | 368 | buffers = {"x": x.to("cuda", dtype), "scale": scale.cuda()} |
333 | 369 | out = torch.empty(x.shape[0], qdata.shape[0], device="cuda", dtype=dtype) |
@@ -366,6 +402,18 @@ def test_int4_dequantize_in_a_refittable_engine(self): |
366 | 402 | ) |
367 | 403 | torch.testing.assert_close(out, expected, rtol=2e-2, atol=1.0) |
368 | 404 |
|
| 405 | + @unittest.skipIf(not ENABLED_FEATURES.refit, "Refit is not available") |
| 406 | + def test_int4_dequantize_refits_new_weights(self): |
| 407 | + # Refit, which a cached engine also goes through, loads the INT4 weights. |
| 408 | + qdata = torch.randint(-8, 8, (512, 1024), dtype=torch.int8) |
| 409 | + new_qdata = torch.randint(-8, 8, (512, 1024), dtype=torch.int8) |
| 410 | + scale = torch.rand(512, 8, dtype=torch.bfloat16) + 0.5 |
| 411 | + x = torch.randn(4, 1024) |
| 412 | + out, expected = self._int4_matmul( |
| 413 | + qdata, scale, x, torch.bfloat16, refittable=True, refit_qdata=new_qdata |
| 414 | + ) |
| 415 | + torch.testing.assert_close(out, expected, rtol=2e-2, atol=1.0) |
| 416 | + |
369 | 417 | def test_int4_dequantize_in_the_scale_dtype_adds_no_cast(self): |
370 | 418 | # A cast between the DQ and its matmul stops TensorRT from fusing them. |
371 | 419 | skip_if_trt_rtx_turing(self, "INT4 dequantize into a matmul") |
|
0 commit comments