-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FX] refactor the fx path in compile function (#1141)
* compile interface * add compile method * update * update * Update lower_setting.py * update fx2trt_example * add docstring * update dynamic_batch default to False * add docstring * add save/load module
- Loading branch information
Wei
authored
Jun 28, 2022
1 parent
5b03083
commit 3c87214
Showing
8 changed files
with
143 additions
and
161 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 was deleted.
Oops, something went wrong.
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,57 @@ | ||
import torch | ||
import copy | ||
import torchvision | ||
import torch_tensorrt | ||
from torch_tensorrt.fx import InputTensorSpec | ||
|
||
|
||
def test_torch_tensorrt(model, inputs): | ||
# torchscript path | ||
model_ts = copy.deepcopy(model) | ||
inputs_ts = copy.deepcopy(inputs) | ||
# fp32 test | ||
with torch.inference_mode(): | ||
ref_fp32 = model_ts(*inputs_ts) | ||
trt_ts_module = torch_tensorrt.compile( | ||
model_ts, inputs=inputs_ts, enabled_precisions={torch.float32} | ||
) | ||
result_fp32 = trt_ts_module(*inputs_ts) | ||
assert(torch.nn.functional.cosine_similarity(ref_fp32.flatten(), result_fp32.flatten(), dim=0)>0.9999) | ||
# fp16 test | ||
model_ts = model_ts.half() | ||
inputs_ts = [i.cuda().half() for i in inputs_ts] | ||
with torch.inference_mode(): | ||
ref_fp16 = model_ts(*inputs_ts) | ||
trt_ts_module = torch_tensorrt.compile( | ||
model_ts, inputs=inputs_ts, enabled_precisions={torch.float16} | ||
) | ||
result_fp16 = trt_ts_module(*inputs_ts) | ||
assert(torch.nn.functional.cosine_similarity(ref_fp16.flatten(), result_fp16.flatten(), dim=0)>0.99) | ||
|
||
# FX path | ||
model_fx = copy.deepcopy(model) | ||
inputs_fx = copy.deepcopy(inputs) | ||
# fp32 test | ||
with torch.inference_mode(): | ||
ref_fp32 = model_fx(*inputs_fx) | ||
trt_fx_module = torch_tensorrt.compile( | ||
model_fx, ir="fx", inputs=inputs_fx, enabled_precisions={torch.float32} | ||
) | ||
result_fp32 = trt_fx_module(*inputs_fx) | ||
assert(torch.nn.functional.cosine_similarity(ref_fp32.flatten(), result_fp32.flatten(), dim=0)>0.9999) | ||
# fp16 test | ||
model_fx = model_fx.cuda().half() | ||
inputs_fx = [i.cuda().half() for i in inputs_fx] | ||
with torch.inference_mode(): | ||
ref_fp16 = model_fx(*inputs_fx) | ||
trt_fx_module = torch_tensorrt.compile( | ||
model_fx, ir="fx", inputs=inputs_fx, enabled_precisions={torch.float16} | ||
) | ||
result_fp16 = trt_fx_module(*inputs_fx) | ||
assert(torch.nn.functional.cosine_similarity(ref_fp16.flatten(), result_fp16.flatten(), dim=0)>0.99 ) | ||
|
||
|
||
if __name__ == "__main__": | ||
model = torchvision.models.resnet18(pretrained=True).cuda().eval() | ||
inputs = [torch.ones((32, 3, 224, 224), device=torch.device('cuda'))] # type: ignore[attr-defined] | ||
test_torch_tensorrt(model, inputs) |
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
Oops, something went wrong.