-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
non_zero_plugin.py
352 lines (286 loc) · 12.2 KB
/
non_zero_plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import onnx_graphsurgeon as gs
import numpy as np
import onnx
import os
import sys
import tensorrt as trt
from polygraphy.backend.trt import (
CreateConfig,
EngineFromNetwork,
NetworkFromOnnxPath,
TrtRunner,
create_network,
engine_from_network,
)
import argparse
from polygraphy import mod
sys.path.insert(1, os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir))
from plugin_utils import checkCudaErrors, KernelHelper, UnownedMemory, volume
cuda = mod.lazy_import("cuda.cuda")
cudart = mod.lazy_import("cuda.cudart")
nvrtc = mod.lazy_import("cuda.nvrtc")
torch = mod.lazy_import("torch")
cp = mod.lazy_import("cupy")
non_zero_half_kernel = r'''
#include <cuda_fp16.h>
extern "C" __global__
void find_non_zero_indices_half(
half const* X, int* indices, int* count, int R, int C)
{
int row = blockIdx.x * blockDim.x + threadIdx.x;
// Check if the row index is within bounds
if (row < R)
{
for (int col = 0; col < C; ++col)
{
half const z = static_cast<half>(0.F);
if (X[col + C * row] != z)
{
int index = atomicAdd(count, 1); // Increment count atomically and get the previous value
indices[2 * index] = row;
indices[2 * index + 1] = col;
}
}
}
}
'''
non_zero_float_kernel = r'''
extern "C" __global__
void find_non_zero_indices_float(
float const* X, int* indices, int* count, int R, int C)
{
int row = blockIdx.x * blockDim.x + threadIdx.x;
// Check if the row index is within bounds
if (row < R)
{
for (int col = 0; col < C; ++col)
{
if (X[col + C * row] != 0.F)
{
int index = atomicAdd(count, 1); // Increment count atomically and get the previous value
indices[2 * index] = row;
indices[2 * index + 1] = col;
}
}
}
}
'''
class NonZeroPlugin(trt.IPluginV3, trt.IPluginV3OneCore, trt.IPluginV3OneBuild, trt.IPluginV3OneRuntime):
def __init__(self, backend = None):
trt.IPluginV3.__init__(self)
trt.IPluginV3OneCore.__init__(self)
trt.IPluginV3OneBuild.__init__(self)
trt.IPluginV3OneRuntime.__init__(self)
self.num_outputs = 2
self.plugin_namespace = ""
self.plugin_name = "NonZeroPlugin"
self.plugin_version = "1"
if backend is not None:
self.backend = backend.tobytes().decode("utf-8")
else:
self.backend = "cuda_python"
self.cuDevice = None
def get_capability_interface(self, type):
return self
def get_output_data_types(self, input_types):
return [trt.DataType.INT32, trt.DataType.INT32]
def get_output_shapes(self, inputs, shape_inputs, exprBuilder):
# First output is 2-D
# Second output is a size tensor, which must be declared a scalar (0-D)
output_dims = [trt.DimsExprs(2), trt.DimsExprs(0)]
upper_bound = exprBuilder.operation(trt.DimensionOperation.PROD, inputs[0][0], inputs[0][1])
opt_value = exprBuilder.operation(trt.DimensionOperation.FLOOR_DIV, upper_bound, exprBuilder.constant(2))
num_non_zero_size_tensor = exprBuilder.declare_size_tensor(1, opt_value, upper_bound)
output_dims[0][0] = num_non_zero_size_tensor
output_dims[0][1] = exprBuilder.constant(2)
return output_dims
def get_fields_to_serialize(self):
return trt.PluginFieldCollection(
[
trt.PluginField(
"backend", self.backend.encode(), trt.PluginFieldType.CHAR
)
]
)
def configure_plugin(self, inp, out):
if self.backend == "cuda_python":
err, self.cuDevice = cuda.cuDeviceGet(0)
def on_shape_change(self, inp, out):
if self.backend == "cuda_python":
err, self.cuDevice = cuda.cuDeviceGet(0)
def supports_format_combination(self, pos, in_out, num_inputs):
assert num_inputs == 1
assert pos < len(in_out)
type_ok = False
# first input should be float16 or float32
if pos == 0:
type_ok = in_out[0].desc.type == trt.DataType.FLOAT or in_out[0].desc.type == trt.DataType.HALF
elif pos == 1:
type_ok = in_out[1].desc.type == trt.DataType.INT32
else: # pos == 2
# size tensor outputs must be NCHW INT32
type_ok = in_out[2].desc.type == trt.DataType.INT32
return in_out[pos].desc.format == trt.TensorFormat.LINEAR and type_ok
def enqueue(self, input_desc, output_desc, inputs, outputs, workspace, stream):
inp_dtype = trt.nptype(input_desc[0].type)
if self.backend == "cuda_python":
R = input_desc[0].dims[0]
C = input_desc[0].dims[1]
blockSize = 256
numBlocks = int((C + blockSize - 1) // blockSize)
d_in = np.array([inputs[0]], dtype=np.uint64)
d_out_0 = np.array([outputs[0]], dtype=np.uint64)
d_out_1 = np.array([outputs[1]], dtype=np.uint64)
args = [d_in, d_out_0, d_out_1, np.array(R, dtype=np.uint32), np.array(C, dtype=np.uint32)]
kernelArgs = np.array([arg.ctypes.data for arg in args], dtype=np.uint64)
stream_ptr = np.array([stream], dtype=np.uint64)
if inp_dtype == np.float32:
kernelHelper = KernelHelper(non_zero_float_kernel, int(self.cuDevice))
_non_zero_float_kernel = kernelHelper.getFunction(b'find_non_zero_indices_float')
checkCudaErrors(cuda.cuLaunchKernel(_non_zero_float_kernel,
numBlocks, 1, 1,
blockSize, 1, 1,
0,
stream_ptr,
kernelArgs, 0))
elif inp_dtype == np.float16:
kernelHelper = KernelHelper(non_zero_half_kernel, int(self.cuDevice))
_non_zero_half_kernel = kernelHelper.getFunction(b'find_non_zero_indices_half')
checkCudaErrors(cuda.cuLaunchKernel(_non_zero_half_kernel,
numBlocks, 1, 1,
blockSize, 1, 1,
0,
stream_ptr,
kernelArgs, 0))
else:
raise ValueError("inp_dtype not valid")
elif self.backend == "torch":
inp_mem = UnownedMemory(inputs[0], input_desc[0].dims, inp_dtype)
out_mem = UnownedMemory(
outputs[0], 2 * volume(input_desc[0].dims), np.int32
)
out_1_mem = UnownedMemory(outputs[1], 1, np.int32)
a_t = torch.as_tensor(inp_mem.d, device="cuda")
out = torch.nonzero(a_t)
out_mem.d[: volume(out.shape)] = cp.reshape(cp.asarray(out), (-1,))
cp.copyto(out_1_mem.d, cp.reshape(cp.asarray([out.shape[0]]), (-1,)))
else:
raise ValueError(f"backend not valid: {self.backend}")
def attach_to_context(self, context):
return self.clone()
def set_tactic(self, tactic):
pass
def clone(self):
cloned_plugin = NonZeroPlugin()
cloned_plugin.__dict__.update(self.__dict__)
return cloned_plugin
#
# The following defaults take effect since the respective methods are not overriden
#
# def get_valid_tactics(self):
# return []
# def get_workspace_size(self, input_desc, output_desc):
# return 0
# def destroy(self):
# pass
class NonZeroPluginCreator(trt.IPluginCreatorV3One):
def __init__(self):
trt.IPluginCreatorV3One.__init__(self)
self.name = "NonZeroPlugin"
self.plugin_namespace = ""
self.plugin_version = "1"
self.field_names = trt.PluginFieldCollection(
[trt.PluginField("backend", np.array([]), trt.PluginFieldType.CHAR)]
)
def create_plugin(self, name, fc, phase):
backend = None
for f in fc:
if f.name == "backend":
backend = f.data[:-1] if f.data[-1] == 0 else f.data
return NonZeroPlugin(backend)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--precision', type=str, default="fp32", choices=["fp32", "fp16"])
parser.add_argument("--backend", type=str, default="torch", choices=["cuda_python", "torch"])
parser.add_argument('--net_type', type=str, default="onnx", choices=["onnx", "inetdef"])
args = parser.parse_args()
if args.backend == "cuda_python":
# Initialize CUDA Driver API
err, = cuda.cuInit(0)
# Retrieve handle for device 0
err, cuDevice = cuda.cuDeviceGet(0)
# Create context
_, cudaCtx = cuda.cuCtxCreate(0, cuDevice)
precision = np.float32 if args.precision == "fp32" else np.float16
inp_shape = (128, 128)
X = np.random.normal(size=inp_shape).astype(precision)
# Zero out a random set of indices
indices = np.random.choice(np.prod(inp_shape), replace=False, size=np.random.randint(0, np.prod(inp_shape) + 1))
X[np.unravel_index(indices, inp_shape)] = 0
# Register plugin creator
plg_registry = trt.get_plugin_registry()
my_plugin_creator = NonZeroPluginCreator()
plg_registry.register_creator(my_plugin_creator, "")
if args.net_type == "onnx":
# create ONNX model
onnx_path = "test_NonZeroPlugin.onnx"
inputX = gs.Variable(name="X", shape=inp_shape, dtype=precision)
Y = gs.Variable(name="Y", dtype=np.int32)
Y_num = gs.Variable(name="Y_num", dtype=np.int32)
nonZeroPluginNode = gs.Node(
name="NonZeroPlugin",
op="NonZeroPlugin",
inputs=[inputX],
outputs=[Y, Y_num],
attrs={"backend": args.backend.encode()},
)
graph = gs.Graph(nodes=[nonZeroPluginNode], inputs=[inputX], outputs=[Y], opset=16)
onnx.save(gs.export_onnx(graph), onnx_path)
# build engine
build_engine = EngineFromNetwork(
NetworkFromOnnxPath(onnx_path), CreateConfig(fp16=precision==np.float16)
)
else:
# Create plugin object
builder, network = create_network()
plg_creator = plg_registry.get_creator("NonZeroPlugin", "1", "")
plugin_fields_list = [
trt.PluginField("backend", args.backend.encode(), trt.PluginFieldType.CHAR)
]
pfc = trt.PluginFieldCollection(plugin_fields_list)
plugin = plg_creator.create_plugin("NonZeroPlugin", pfc, trt.TensorRTPhase.BUILD)
# Populate network
inputX = network.add_input(name="X", dtype=trt.float32 if precision==np.float32 else trt.float16, shape=inp_shape)
out = network.add_plugin_v3([inputX], [], plugin)
out.get_output(0).name = "Y"
network.mark_output(tensor=out.get_output(0))
build_engine = engine_from_network((builder, network), CreateConfig(fp16=precision==trt.float16))
# Compare against Numpy's nonzero
Y_ref = np.transpose(np.nonzero(X))
# Run
with TrtRunner(build_engine, "trt_runner")as runner:
outputs = runner.infer({"X": X})
Y = outputs["Y"]
Y = Y[np.lexsort(np.fliplr(Y).T)]
if np.allclose(Y, Y_ref):
print("Inference result correct!")
else:
print("Inference result incorrect!")
if args.backend == "cuda_python":
checkCudaErrors(cuda.cuCtxDestroy(cudaCtx))