-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(//py): New API to embed engine in new module
Also adds tests to confirm TRT Python API intercompatiability Signed-off-by: Naren Dasan <naren@narendasan.com> Signed-off-by: Naren Dasan <narens@nvidia.com>
- Loading branch information
1 parent
3ec836e
commit 88d07a9
Showing
10 changed files
with
129 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import unittest | ||
import trtorch | ||
import torch | ||
import torchvision.models as models | ||
import tensorrt as trt | ||
|
||
from model_test_case import ModelTestCase | ||
|
||
|
||
class TestPyTorchToTRTEngine(ModelTestCase): | ||
def setUp(self): | ||
self.input = torch.randn((1, 3, 224, 224)).to("cuda:0") | ||
self.ts_model = torch.jit.script(self.model) | ||
|
||
def test_pt_to_trt(self): | ||
compile_spec = { | ||
"input_shapes": [self.input.shape], | ||
"device": { | ||
"device_type": trtorch.DeviceType.GPU, | ||
"gpu_id": 0, | ||
"dla_core": 0, | ||
"allow_gpu_fallback": False, | ||
"disable_tf32": False | ||
} | ||
} | ||
|
||
trt_engine = trtorch.convert_method_to_trt_engine(self.ts_model, "forward", compile_spec) | ||
|
||
TRT_LOGGER = trt.Logger(trt.Logger.WARNING) | ||
with trt.Runtime(TRT_LOGGER) as rt: | ||
engine = rt.deserialize_cuda_engine(trt_engine) | ||
with engine.create_execution_context() as ctx: | ||
out = torch.empty(size=tuple(engine.get_binding_shape(1))).to("cuda:0") | ||
bindings = [self.input.contiguous().data_ptr(), out.contiguous().data_ptr()] | ||
ctx.execute_async(batch_size=1, bindings=bindings, stream_handle=torch.cuda.current_stream(device='cuda:0').cuda_stream) | ||
same = (out - self.ts_model(self.input)).abs().max() | ||
self.assertTrue(same < 2e-3) | ||
|
||
def test_suite(): | ||
suite = unittest.TestSuite() | ||
suite.addTest(TestPyTorchToTRTEngine.parametrize(TestPyTorchToTRTEngine, model=models.resnet18(pretrained=True))) | ||
|
||
return suite | ||
|
||
|
||
suite = test_suite() | ||
|
||
runner = unittest.TextTestRunner() | ||
result = runner.run(suite) | ||
|
||
exit(int(not result.wasSuccessful())) |