@@ -66,14 +66,17 @@ from torch.utils import cpp_extension
66
66
67
67
dir_path = os.path.dirname(os.path.realpath(__file__))
68
68
69
+ # library_dirs should point to the libtrtorch.so, include_dirs should point to the dir that include the headers
70
+ # 1) download the latest package from https://github.com/NVIDIA/TRTorch/releases/
71
+ # 2) Extract the file from downloaded package, we will get the "trtorch" directory
72
+ # 3) Set trtorch_path to that directory
73
+ trtorch_path = os.path.abspath("trtorch")
74
+
69
75
ext_modules = [
70
76
cpp_extension.CUDAExtension('elu_converter', ['elu_converter.cpp'],
71
- library_dirs=[(
72
- dir_path + "/../../bazel-bin/cpp/api/lib/"
73
- )],
77
+ library_dirs=[(trtorch_path + "/lib/")],
74
78
libraries=["trtorch"],
75
- include_dirs=[dir_path + "/../../"]
76
- )
79
+ include_dirs=[trtorch_path + "/include/trtorch/"])
77
80
]
78
81
79
82
setup(
83
86
)
84
87
```
85
88
Make sure to include the path for header files in ` include_dirs ` and the path
86
- for dependent libraries in ` library_dirs ` . You could also add other compilation
89
+ for dependent libraries in ` library_dirs ` . Generally speaking, you should download
90
+ the latest package from [ here] ( https://github.com/NVIDIA/TRTorch/releases ) , extract
91
+ the files, and the set the ` trtorch_path ` to it. You could also add other compilation
87
92
flags in cpp_extension if you need. Then, run above python scripts as:
88
93
``` shell
89
94
python3 setup.py install --user
@@ -140,20 +145,20 @@ if __name__ == "__main__":
140
145
Run this script, we can get the Tensor before and after ELU operator.
141
146
### Example Output
142
147
``` bash
143
- graph(%self : __torch__.Elu,
144
- %x.1 : Tensor):
145
- %2 : __torch__.torch.nn.modules.activation.ELU = prim::GetAttr[name= " elu" ](%self)
146
- %4 : Tensor = prim::CallMethod[name= " forward" ](%2, %x.1) # elu_converter_test.py:13:15
147
- return (%4)
148
-
149
- tensor([[ 1.3482, 1.9848, -1.0818, -1.3252, 0.2470, 0.7011, 0.3174, -1.8349,
150
- 0.3024, -0.0453, -0.0681, -1.7377, 1.5909, 0.2549, -0.3029, 0.2583,
151
- 0.0242, 2.0748, -0.5454, 0.7137, 1.6688, 0.7108, -0.8681, 0.2486,
152
- -1.3981, 1.0241, 1.2413, 0.2725, 1.4265, 0.9329, 0.4020, -2.6813]])
153
- tensor([[ 1.3486, 1.9844, -0.6611, -0.7344, 0.2471, 0.7012, 0.3174, -0.8403,
154
- 0.3025, -0.0443, -0.0659, -0.8242, 1.5908, 0.2549, -0.2615, 0.2583,
155
- 0.0242, 2.0742, -0.4204, 0.7139, 1.6689, 0.7109, -0.5801, 0.2485,
156
- -0.7529, 1.0244, 1.2412, 0.2725, 1.4268, 0.9331, 0.4021, -0.9316]],
148
+ PyTorch output:
149
+ tensor([[ 0.8804, 2.4355, -0.7920, -0.2070, -0.5352, 0.4775, 1.3604, -0.3350,
150
+ -0.1802, -0.7563, -0.1758, 0.4067, 1.2510, -0.7100, -0.6221, -0.7207,
151
+ -0.1118, 0.9966, 1.6396, -0.1367, -0.5742, 0.5859, 0.8511, 0.6572,
152
+ -0.3481, 0.5933, -0.0488, -0.4287, -0.4102, -0.7402, 0.7515, -0.7710]],
153
+ device=' cuda:0' , dtype=torch.float16)
154
+ TRTorch output:
155
+ tensor([[ 0.8804, 2.4355, -0.7920, -0.2070, -0.5356, 0.4775, 1.3604, -0.3347,
156
+ -0.1802, -0.7563, -0.1758, 0.4067, 1.2510, -0.7100, -0.6221, -0.7207,
157
+ -0.1117, 0.9966, 1.6396, -0.1368, -0.5747, 0.5859, 0.8511, 0.6572,
158
+ -0.3484, 0.5933, -0.0486, -0.4285, -0.4102, -0.7402, 0.7515, -0.7710]],
157
159
device=' cuda:0' , dtype=torch.float16)
160
+ Maximum differnce between TRTorch and PyTorch:
161
+ tensor(0.0005, device=' cuda:0' , dtype=torch.float16)
162
+
158
163
159
164
```
0 commit comments