|
| 1 | +import importlib |
| 2 | +import unittest |
| 3 | + |
| 4 | +import torch |
| 5 | +import torch_tensorrt as torch_trt |
| 6 | +from torch.testing._internal.common_utils import TestCase |
| 7 | +from torch_tensorrt.dynamo.utils import COSINE_THRESHOLD, cosine_similarity |
| 8 | + |
| 9 | +assertions = unittest.TestCase() |
| 10 | + |
| 11 | +if importlib.util.find_spec("torchvision"): |
| 12 | + import torchvision.models as models |
| 13 | + |
| 14 | + |
| 15 | +class TestDynamicAllocation(TestCase): |
| 16 | + def test_dynamic_allocation(self): |
| 17 | + inputs = [torch.rand((100, 3, 224, 224)).to("cuda")] |
| 18 | + |
| 19 | + settings = { |
| 20 | + "ir": "dynamo", |
| 21 | + "use_python_runtime": False, |
| 22 | + "enabled_precisions": {torch.float32}, |
| 23 | + "immutable_weights": False, |
| 24 | + "lazy_engine_init": True, |
| 25 | + "dynamically_allocate_resources": True, |
| 26 | + } |
| 27 | + |
| 28 | + model = models.resnet152(pretrained=True).eval().to("cuda") |
| 29 | + compiled_module = torch_trt.compile(model, inputs=inputs, **settings) |
| 30 | + compiled_module(*inputs) |
| 31 | + |
| 32 | + # Inference on PyTorch model |
| 33 | + model_output = model(*inputs) |
| 34 | + cos_sim = cosine_similarity(model_output, compiled_module(*inputs)) |
| 35 | + assertions.assertTrue( |
| 36 | + cos_sim > COSINE_THRESHOLD, |
| 37 | + msg=f"EfficientNet-B0 TRT outputs don't match with the original model. Cosine sim score: {cos_sim} Threshold: {COSINE_THRESHOLD}", |
| 38 | + ) |
| 39 | + |
| 40 | + # Clean up model env |
| 41 | + torch._dynamo.reset() |
0 commit comments