Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional trt import #215

Merged
merged 2 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 48 additions & 38 deletions alonet/torch2trt/calibrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
import os
import torch
import numpy as np
import tensorrt as trt
import pycuda.driver as cuda

try:
import tensorrt as trt
import pycuda.driver as cuda
prod_package_error = None
except Exception as e:
trt = None
cuda = None
prod_package_error = e
pass


class DataBatchStreamer:
Expand Down Expand Up @@ -74,6 +82,7 @@ def __init__(
limit_batches=None,
**kwargs,
):
if prod_package_error is not None: raise prod_package_error
for sample in dataset[0]:
if not isinstance(sample, (torch.Tensor, np.ndarray, Frame)):
raise TypeError(f"unknown sample type, expected samples to be instance of {' or '.join(self.FTYPES)} got {sample.__class__.__name__} instead")
Expand Down Expand Up @@ -195,45 +204,46 @@ def free(self):
d.free()


class MinMaxCalibrator(BaseCalibrator, trt.IInt8MinMaxCalibrator):
def __init__(
self,
data_streamer,
cache_file,
**kwargs,
):
trt.IInt8MinMaxCalibrator.__init__(self)
super(MinMaxCalibrator, self).__init__(data_streamer=data_streamer, cache_file=cache_file, **kwargs)
if trt is not None:
class MinMaxCalibrator(BaseCalibrator, trt.IInt8MinMaxCalibrator):
def __init__(
self,
data_streamer,
cache_file,
**kwargs,
):
trt.IInt8MinMaxCalibrator.__init__(self)
super(MinMaxCalibrator, self).__init__(data_streamer=data_streamer, cache_file=cache_file, **kwargs)


class LegacyCalibrator(BaseCalibrator, trt.IInt8LegacyCalibrator):
def __init__(
self,
data_streamer,
cache_file,
**kwargs,
):
trt.IInt8LegacyCalibrator.__init__(self)
super(LegacyCalibrator, self).__init__(data_streamer=data_streamer, cache_file=cache_file, **kwargs)
class LegacyCalibrator(BaseCalibrator, trt.IInt8LegacyCalibrator):
def __init__(
self,
data_streamer,
cache_file,
**kwargs,
):
trt.IInt8LegacyCalibrator.__init__(self)
super(LegacyCalibrator, self).__init__(data_streamer=data_streamer, cache_file=cache_file, **kwargs)


class EntropyCalibrator(BaseCalibrator, trt.IInt8EntropyCalibrator):
def __init__(
self,
data_streamer,
cache_file,
**kwargs,
):
trt.IInt8EntropyCalibrator.__init__(self)
super(EntropyCalibrator, self).__init__(data_streamer=data_streamer, cache_file=cache_file, **kwargs)
class EntropyCalibrator(BaseCalibrator, trt.IInt8EntropyCalibrator):
def __init__(
self,
data_streamer,
cache_file,
**kwargs,
):
trt.IInt8EntropyCalibrator.__init__(self)
super(EntropyCalibrator, self).__init__(data_streamer=data_streamer, cache_file=cache_file, **kwargs)


class EntropyCalibrator2(BaseCalibrator, trt.IInt8EntropyCalibrator2):
def __init__(
self,
data_streamer,
cache_file,
**kwargs,
):
trt.IInt8EntropyCalibrator2.__init__(self)
super(EntropyCalibrator2, self).__init__(data_streamer=data_streamer, cache_file=cache_file, **kwargs)
class EntropyCalibrator2(BaseCalibrator, trt.IInt8EntropyCalibrator2):
def __init__(
self,
data_streamer,
cache_file,
**kwargs,
):
trt.IInt8EntropyCalibrator2.__init__(self)
super(EntropyCalibrator2, self).__init__(data_streamer=data_streamer, cache_file=cache_file, **kwargs)
24 changes: 16 additions & 8 deletions alonet/torch2trt/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from alonet import ALONET_ROOT
from alonet.torch2trt.calibrator import (
LegacyCalibrator,
MinMaxCalibrator,
EntropyCalibrator,
EntropyCalibrator2,
)

try:
from alonet.torch2trt.calibrator import (
LegacyCalibrator,
MinMaxCalibrator,
EntropyCalibrator,
EntropyCalibrator2,
)
calibrator_import_error = None
except Exception as e:
calibrator_import_error = e

import os
import ctypes
Expand All @@ -13,9 +18,9 @@
import pycuda.driver as cuda
import onnx_graphsurgeon as gs
import tensorrt as trt

prod_package_error = None
except Exception as prod_package_error:
except Exception as e:
prod_package_error = e
pass


Expand All @@ -27,6 +32,9 @@ def create_calibrator(name: str, *args, **kwargs):
name : str
Calibrator name
"""
if calibrator_import_error is not None:
raise calibrator_import_error

CALIBS = ["minmax", "entropy", "entropy2", "legacy"]
if name == "entropy2":
return EntropyCalibrator2(*args, **kwargs)
Expand Down