-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(//py): Allowing people using the PyTorch backend to use TRTorch/TRT
INT8 calibrators Signed-off-by: Naren Dasan <naren@narendasan.com> Signed-off-by: Naren Dasan <narens@nvidia.com>
- Loading branch information
1 parent
fe5654f
commit 6c3e0ad
Showing
6 changed files
with
112 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import unittest | ||
import trtorch | ||
from trtorch.logging import * | ||
import torch | ||
import torch.nn as nn | ||
from torch.nn import functional as F | ||
import torchvision | ||
import torchvision.transforms as transforms | ||
from model_test_case import ModelTestCase | ||
|
||
|
||
class TestAccuracy(ModelTestCase): | ||
|
||
def setUp(self): | ||
self.input = torch.randn((1, 3, 32, 32)).to("cuda") | ||
self.testing_dataset = torchvision.datasets.CIFAR10(root='./data', | ||
train=False, | ||
download=True, | ||
transform=transforms.Compose([ | ||
transforms.ToTensor(), | ||
transforms.Normalize((0.4914, 0.4822, 0.4465), | ||
(0.2023, 0.1994, 0.2010)) | ||
])) | ||
|
||
self.testing_dataloader = torch.utils.data.DataLoader(self.testing_dataset, | ||
batch_size=1, | ||
shuffle=False, | ||
num_workers=1) | ||
self.calibrator = trtorch.ptq.DataLoaderCalibrator(self.testing_dataloader, | ||
cache_file='./calibration.cache', | ||
use_cache=False, | ||
algo_type=trtorch.ptq.CalibrationAlgo.ENTROPY_CALIBRATION_2, | ||
device=torch.device('cuda:0')) | ||
|
||
self.spec = { | ||
"forward": | ||
trtorch.TensorRTCompileSpec({ | ||
"input_shapes": [[1, 3, 32, 32]], | ||
"op_precision": torch.int8, | ||
"calibrator": self.calibrator, | ||
"device": { | ||
"device_type": trtorch.DeviceType.GPU, | ||
"gpu_id": 0, | ||
"dla_core": 0, | ||
"allow_gpu_fallback": False, | ||
} | ||
}) | ||
} | ||
|
||
def compute_accuracy(self, testing_dataloader, model): | ||
total = 0 | ||
correct = 0 | ||
loss = 0.0 | ||
class_probs = [] | ||
class_preds = [] | ||
|
||
with torch.no_grad(): | ||
idx = 0 | ||
for data, labels in testing_dataloader: | ||
data, labels = data.cuda(), labels.cuda(non_blocking=True) | ||
out = model(data) | ||
preds = torch.max(out, 1)[1] | ||
class_probs.append([F.softmax(i, dim=0) for i in out]) | ||
class_preds.append(preds) | ||
total += labels.size(0) | ||
correct += (preds == labels).sum().item() | ||
idx += 1 | ||
|
||
test_probs = torch.cat([torch.stack(batch) for batch in class_probs]) | ||
test_preds = torch.cat(class_preds) | ||
return correct / total | ||
|
||
def test_compile_script(self): | ||
|
||
fp32_test_acc = self.compute_accuracy(self.testing_dataloader, self.model) | ||
log(Level.Info, "[Pyt FP32] Test Acc: {:.2f}%".format(100 * fp32_test_acc)) | ||
|
||
trt_mod = torch._C._jit_to_backend("tensorrt", self.model, self.spec) | ||
int8_test_acc = self.compute_accuracy(self.testing_dataloader, trt_mod) | ||
log(Level.Info, "[TRT INT8 Backend] Test Acc: {:.2f}%".format(100 * int8_test_acc)) | ||
acc_diff = fp32_test_acc - int8_test_acc | ||
self.assertTrue(abs(acc_diff) < 3) | ||
|
||
|
||
def test_suite(): | ||
suite = unittest.TestSuite() | ||
suite.addTest(TestAccuracy.parametrize(TestAccuracy, model=torch.jit.load('./trained_vgg16.jit.pt'))) | ||
|
||
return suite | ||
|
||
|
||
suite = test_suite() | ||
|
||
runner = unittest.TextTestRunner() | ||
result = runner.run(suite) | ||
|
||
exit(int(not result.wasSuccessful())) |