Elffuss BraidHub

kikocis/ncnn public

master · 29.7 KiBRenderedRawPermalink
1# PNNX
2PyTorch Neural Network eXchange(PNNX) is an open standard for PyTorch model interoperability. PNNX provides an open model format for PyTorch. It defines computation graph as well as high level operators strictly matches PyTorch.
3
4# Rationale
5PyTorch is currently one of the most popular machine learning frameworks. We need to deploy the trained AI model to various hardware and environments more conveniently and easily.
6
7Before PNNX, we had the following methods:
8
91. export to ONNX, and deploy with ONNX-runtime
102. export to ONNX, and convert onnx to inference-framework specific format, and deploy with TensorRT/OpenVINO/ncnn/etc.
113. export to TorchScript, and deploy with libtorch
12
13As far as we know, ONNX has the ability to express the PyTorch model and it is an open standard. People usually use ONNX as an intermediate representation between PyTorch and the inference platform. However, ONNX still has the following fatal problems, which makes the birth of PNNX necessary:
14
151. ONNX does not have a human-readable and editable file representation, making it difficult for users to easily modify the computation graph or add custom operators.
162. The operator definition of ONNX is not completely in accordance with PyTorch. When exporting some PyTorch operators, glue operators are often added passively by ONNX, which makes the computation graph inconsistent with PyTorch and may impact the inference efficiency.
173. There are a large number of additional parameters designed to be compatible with various ML frameworks in the operator definition in ONNX. These parameters increase the burden of inference implementation on hardware and software.
18
19PNNX tries to define a set of operators and a simple and easy-to-use format that are completely contrasted with the python api of PyTorch, so that the conversion and interoperability of PyTorch models are more convenient.
20
21# Features
22
231. [Human readable and editable format](#the-pnnxparam-format)
242. [Plain model binary in storage zip](#the-pnnxbin-format)
253. [One-to-one mapping of PNNX operators and PyTorch python api](#pnnx-operator)
264. [Preserve math expression as one operator](#pnnx-expression-operator)
275. [Preserve torch function as one operator](#pnnx-torch-function-operator)
286. [Preserve miscellaneous module as one operator](#pnnx-module-operator)
297. [Inference via exported PyTorch python code](#pnnx-python-inference)
308. [Tensor shape propagation](#pnnx-shape-propagation)
319. [Model optimization](#pnnx-model-optimization)
3210. [Custom operator support](#pnnx-custom-operator)
33
34# Build TorchScript to PNNX converter
35
361. Install PyTorch and TorchVision c++ library
372. Build PNNX with cmake
38
39# Usage
40
411. Export your model to TorchScript
42
43```python
44import torch
45import torchvision.models as models
46
47net = models.resnet18(pretrained=True)
48net = net.eval()
49
50x = torch.rand(1, 3, 224, 224)
51
52# You could try disabling checking when tracing raises error
53# mod = torch.jit.trace(net, x, check_trace=False)
54mod = torch.jit.trace(net, x)
55
56mod.save("resnet18.pt")
57```
58
592. Convert TorchScript to PNNX
60
61```shell
62pnnx resnet18.pt inputshape=[1,3,224,224]
63```
64
65Normally, you will get seven files
66
67```resnet18.pnnx.param``` PNNX graph definition
68
69```resnet18.pnnx.bin``` PNNX model weight
70
71```resnet18_pnnx.py``` PyTorch script for inference, the python code for model construction and weight initialization
72
73```resnet18.pnnx.onnx``` PNNX model in onnx format
74
75```resnet18.ncnn.param``` ncnn graph definition
76
77```resnet18.ncnn.bin``` ncnn model weight
78
79```resnet18_ncnn.py``` pyncnn script for inference
80
813. Visualize PNNX with Netron
82
83Open https://netron.app/ in browser, and drag resnet18.pnnx.param into it.
84
854. PNNX command line options
86
87```
88Usage: pnnx [model.pt] [(key=value)...]
89 pnnxparam=model.pnnx.param
90 pnnxbin=model.pnnx.bin
91 pnnxpy=model_pnnx.py
92 pnnxonnx=model.pnnx.onnx
93 ncnnparam=model.ncnn.param
94 ncnnbin=model.ncnn.bin
95 ncnnpy=model_ncnn.py
96 fp16=1
97 optlevel=2
98 device=cpu/gpu
99 inputshape=[1,3,224,224],...
100 inputshape2=[1,3,320,320],...
101 customop=/home/nihui/.cache/torch_extensions/fused/fused.so,...
102 moduleop=models.common.Focus,models.yolo.Detect,...
103Sample usage: pnnx mobilenet_v2.pt inputshape=[1,3,224,224]
104 pnnx yolov5s.pt inputshape=[1,3,640,640] inputshape2=[1,3,320,320] device=gpu moduleop=models.common.Focus,models.yolo.Detect
105```
106
107Parameters:
108
109`pnnxparam` (default="*.pnnx.param", * is the model name): PNNX graph definition file
110
111`pnnxbin` (default="*.pnnx.bin"): PNNX model weight
112
113`pnnxpy` (default="*_pnnx.py"): PyTorch script for inference, including model construction and weight initialization code
114
115`pnnxonnx` (default="*.pnnx.onnx"): PNNX model in onnx format
116
117`ncnnparam` (default="*.ncnn.param"): ncnn graph definition
118
119`ncnnbin` (default="*.ncnn.bin"): ncnn model weight
120
121`ncnnpy` (default="*_ncnn.py"): pyncnn script for inference
122
123`fp16` (default=1): save ncnn weight and onnx in fp16 data type
124
125`optlevel` (default=2): graph optimization level
126
127| Option | Optimization level |
128|--------|---------------------------------|
129| 0 | do not apply optimization |
130| 1 | optimization for inference |
131| 2 | optimization more for inference |
132
133`device` (default="cpu"): device type for the input in TorchScript model, cpu or gpu
134
135`inputshape` (Optional): shapes of model inputs. It is used to resolve tensor shapes in model graph. for example, `[1,3,224,224]` for the model with only 1 input, `[1,3,224,224],[1,3,224,224]` for the model that have 2 inputs.
136
137`inputshape2` (Optional): shapes of alternative model inputs, the format is identical to `inputshape`. Usually, it is used with `inputshape` to resolve dynamic shape (-1) in model graph.
138
139`customop` (Optional): list of Torch extensions (dynamic library) for custom operators, separated by ",". For example, `/home/nihui/.cache/torch_extensions/fused/fused.so,...`
140
141`moduleop` (Optional): list of modules to keep as one big operator, separated by ",". for example, `models.common.Focus,models.yolo.Detect`
142
143# The pnnx.param format
144
145### example
146```
1477767517
1484 3
149pnnx.Input input 0 1 0
150nn.Conv2d conv_0 1 1 0 1 bias=1 dilation=(1,1) groups=1 in_channels=12 kernel_size=(3,3) out_channels=16 padding=(0,0) stride=(1,1) @bias=(16)f32 @weight=(16,12,3,3)f32
151nn.Conv2d conv_1 1 1 1 2 bias=1 dilation=(1,1) groups=1 in_channels=16 kernel_size=(2,2) out_channels=20 padding=(2,2) stride=(2,2) @bias=(20)f32 @weight=(20,16,2,2)f32
152pnnx.Output output 1 0 2
153```
154### overview
155```
156[magic]
157```
158* magic number : 7767517
159```
160[operator count] [operand count]
161```
162* operator count : count of the operator line follows
163* operand count : count of all operands
164### operator line
165```
166[type] [name] [input count] [output count] [input operands] [output operands] [operator params]
167```
168* type : type name, such as Conv2d ReLU etc
169* name : name of this operator
170* input count : count of the operands this operator needs as input
171* output count : count of the operands this operator produces as output
172* input operands : name list of all the input blob names, separated by space
173* output operands : name list of all the output blob names, separated by space
174* operator params : key=value pair list, separated by space, operator weights are prefixed by ```@``` symbol, tensor shapes are prefixed by ```#``` symbol, input parameter keys are prefixed by ```$```
175
176# The pnnx.bin format
177
178pnnx.bin file is a zip file with store-only mode(no compression)
179
180weight binary file has its name composed by operator name and weight name
181
182For example, ```nn.Conv2d conv_0 1 1 0 1 bias=1 dilation=(1,1) groups=1 in_channels=12 kernel_size=(3,3) out_channels=16 padding=(0,0) stride=(1,1) @bias=(16) @weight=(16,12,3,3)``` would pull conv_0.weight and conv_0.bias into pnnx.bin zip archive.
183
184weight binaries can be listed or modified with any archive application eg. 7zip
185
186![pnnx.bin](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/pnnx.bin.png)
187
188# PNNX operator
189PNNX always preserve operators from what PyTorch python api provides.
190
191Here is the netron visualization comparison among ONNX, TorchScript and PNNX with the original PyTorch python code shown.
192
193```python
194import torch
195import torch.nn as nn
196
197class Model(nn.Module):
198 def __init__(self):
199 super(Model, self).__init__()
200
201 self.attention = nn.MultiheadAttention(embed_dim=256, num_heads=32)
202
203 def forward(self, x):
204 x, _ = self.attention(x, x, x)
205 return x
206```
207
208|ONNX|TorchScript|PNNX|
209|----|---|---|
210|![MultiheadAttention.onnx](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/MultiheadAttention.onnx.png)|![MultiheadAttention.pt](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/MultiheadAttention.pt.png)|![MultiheadAttention.pnnx](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/MultiheadAttention.pnnx.png)|
211
212# PNNX expression operator
213PNNX trys to preserve expression from what PyTorch python code writes.
214
215Here is the netron visualization comparison among ONNX, TorchScript and PNNX with the original PyTorch python code shown.
216
217```python
218import torch
219
220def foo(x, y):
221 return torch.sqrt((2 * x + y) / 12)
222```
223
224|ONNX|TorchScript|PNNX|
225|---|---|---|
226|![math.onnx](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/math.onnx.png)|![math.pt](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/math.pt.png)|![math.pnnx](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/math.pnnx.png)|
227
228# PNNX torch function operator
229PNNX trys to preserve torch functions and Tensor member functions as one operator from what PyTorch python api provides.
230
231Here is the netron visualization comparison among ONNX, TorchScript and PNNX with the original PyTorch python code shown.
232
233```python
234import torch
235import torch.nn.functional as F
236
237class Model(nn.Module):
238 def __init__(self):
239 super(Model, self).__init__()
240
241 def forward(self, x):
242 x = F.normalize(x, eps=1e-3)
243 return x
244```
245
246|ONNX|TorchScript|PNNX|
247|---|---|---|
248|![function.onnx](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/function.onnx.png)|![function.pt](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/function.pt.png)|![function.pnnx](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/function.pnnx.png)|
249
250
251# PNNX module operator
252Users could ask PNNX to keep module as one big operator when it has complex logic.
253
254The process is optional and could be enabled via moduleop command line option.
255
256After pass_level0, all modules will be presented in terminal output, then you can pick the interesting ones as module operators.
257```
258############# pass_level0
259inline module = models.common.Bottleneck
260inline module = models.common.C3
261inline module = models.common.Concat
262inline module = models.common.Conv
263inline module = models.common.Focus
264inline module = models.common.SPP
265inline module = models.yolo.Detect
266inline module = utils.activations.SiLU
267```
268
269```bash
270pnnx yolov5s.pt inputshape=[1,3,640,640] moduleop=models.common.Focus,models.yolo.Detect
271```
272
273Here is the netron visualization comparison among ONNX, TorchScript and PNNX with the original PyTorch python code shown.
274
275```python
276import torch
277import torch.nn as nn
278
279class Focus(nn.Module):
280 # Focus wh information into c-space
281 def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
282 super().__init__()
283 self.conv = Conv(c1 * 4, c2, k, s, p, g, act)
284
285 def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2)
286 return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))
287```
288
289|ONNX|TorchScript|PNNX|PNNX with module operator|
290|---|---|---|---|
291|![focus.onnx](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/focus.onnx.png)|![focus.pt](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/focus.pt.png)|![focus.pnnx](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/focus.pnnx.png)|![focus.pnnx2](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/focus.pnnx2.png)|
292
293
294# PNNX python inference
295
296A python script will be generated by default when converting TorchScript to pnnx.
297
298This script is the python code representation of PNNX and can be used for model inference.
299
300There are some utility functions for loading weight binary from pnnx.bin.
301
302You can even export the model TorchScript AGAIN from this generated code!
303
304```python
305import torch
306import torch.nn as nn
307import torch.nn.functional as F
308
309class Model(nn.Module):
310 def __init__(self):
311 super(Model, self).__init__()
312
313 self.linear_0 = nn.Linear(in_features=128, out_features=256, bias=True)
314 self.linear_1 = nn.Linear(in_features=256, out_features=4, bias=True)
315
316 def forward(self, x):
317 x = self.linear_0(x)
318 x = F.leaky_relu(x, 0.15)
319 x = self.linear_1(x)
320 return x
321```
322
323```python
324import os
325import numpy as np
326import tempfile, zipfile
327import torch
328import torch.nn as nn
329import torch.nn.functional as F
330
331class Model(nn.Module):
332 def __init__(self):
333 super(Model, self).__init__()
334
335 self.linear_0 = nn.Linear(bias=True, in_features=128, out_features=256)
336 self.linear_1 = nn.Linear(bias=True, in_features=256, out_features=4)
337
338 archive = zipfile.ZipFile('../../function.pnnx.bin', 'r')
339 self.linear_0.bias = self.load_pnnx_bin_as_parameter(archive, 'linear_0.bias', (256), 'float32')
340 self.linear_0.weight = self.load_pnnx_bin_as_parameter(archive, 'linear_0.weight', (256,128), 'float32')
341 self.linear_1.bias = self.load_pnnx_bin_as_parameter(archive, 'linear_1.bias', (4), 'float32')
342 self.linear_1.weight = self.load_pnnx_bin_as_parameter(archive, 'linear_1.weight', (4,256), 'float32')
343 archive.close()
344
345 def load_pnnx_bin_as_parameter(self, archive, key, shape, dtype):
346 return nn.Parameter(self.load_pnnx_bin_as_tensor(archive, key, shape, dtype))
347
348 def load_pnnx_bin_as_tensor(self, archive, key, shape, dtype):
349 fd, tmppath = tempfile.mkstemp()
350 with os.fdopen(fd, 'wb') as tmpf, archive.open(key) as keyfile:
351 tmpf.write(keyfile.read())
352 m = np.memmap(tmppath, dtype=dtype, mode='r', shape=shape).copy()
353 os.remove(tmppath)
354 return torch.from_numpy(m)
355
356 def forward(self, v_x_1):
357 v_7 = self.linear_0(v_x_1)
358 v_input_1 = F.leaky_relu(input=v_7, negative_slope=0.150000)
359 v_12 = self.linear_1(v_input_1)
360 return v_12
361```
362
363# PNNX shape propagation
364Users could ask PNNX to resolve all tensor shapes in model graph and constify some common expressions involved when tensor shapes are known.
365
366The process is optional and could be enabled via inputshape command line option.
367
368```bash
369pnnx shufflenet_v2_x1_0.pt inputshape=[1,3,224,224]
370```
371
372```python
373def channel_shuffle(x: Tensor, groups: int) -> Tensor:
374 batchsize, num_channels, height, width = x.size()
375 channels_per_group = num_channels // groups
376
377 # reshape
378 x = x.view(batchsize, groups, channels_per_group, height, width)
379
380 x = torch.transpose(x, 1, 2).contiguous()
381
382 # flatten
383 x = x.view(batchsize, -1, height, width)
384
385 return x
386```
387
388|without shape propagation|with shape propagation|
389|---|---|
390|![noshapeinfer](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/noshapeinfer.png)|![shapeinfer](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/shapeinfer.pnnx.png)|
391
392
393# PNNX model optimization
394
395|ONNX|TorchScript|PNNX without optimization|PNNX with optimization|
396|---|---|---|---|
397|![optlessonnx](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/optless.onnx.png)|![optlesspt](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/optless.pt.png)|![optless](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/optless.pnnx.png)|![opt](https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/opt.pnnx.png)|
398
399
400# PNNX custom operator
401
402```python
403import os
404
405import torch
406from torch.autograd import Function
407from torch.utils.cpp_extension import load, _import_module_from_library
408
409module_path = os.path.dirname(__file__)
410upfirdn2d_op = load(
411 'upfirdn2d',
412 sources=[
413 os.path.join(module_path, 'upfirdn2d.cpp'),
414 os.path.join(module_path, 'upfirdn2d_kernel.cu'),
415 ],
416 is_python_module=False
417)
418
419def upfirdn2d(input, kernel, up=1, down=1, pad=(0, 0)):
420 pad_x0 = pad[0]
421 pad_x1 = pad[1]
422 pad_y0 = pad[0]
423 pad_y1 = pad[1]
424
425 kernel_h, kernel_w = kernel.shape
426 batch, channel, in_h, in_w = input.shape
427
428 input = input.reshape(-1, in_h, in_w, 1)
429
430 out_h = (in_h * up + pad_y0 + pad_y1 - kernel_h) // down + 1
431 out_w = (in_w * up + pad_x0 + pad_x1 - kernel_w) // down + 1
432
433 out = torch.ops.upfirdn2d_op.upfirdn2d(input, kernel, up, up, down, down, pad_x0, pad_x1, pad_y0, pad_y1)
434
435 out = out.view(-1, channel, out_h, out_w)
436
437 return out
438```
439
440```cpp
441#include <torch/extension.h>
442
443torch::Tensor upfirdn2d(const torch::Tensor& input, const torch::Tensor& kernel,
444 int64_t up_x, int64_t up_y, int64_t down_x, int64_t down_y,
445 int64_t pad_x0, int64_t pad_x1, int64_t pad_y0, int64_t pad_y1) {
446 // operator body
447}
448
449TORCH_LIBRARY(upfirdn2d_op, m) {
450 m.def("upfirdn2d", upfirdn2d);
451}
452```
453
454<img src="https://raw.githubusercontent.com/nihui/ncnn-assets/master/pnnx/customop.pnnx.png" width="400" />
455
456# Supported PyTorch operator status
457
458| torch.nn | Is Supported | Export to ncnn |
459|---------------------------|----|---|
460|nn.AdaptiveAvgPool1d | :heavy_check_mark: | :heavy_check_mark: |
461|nn.AdaptiveAvgPool2d | :heavy_check_mark: | :heavy_check_mark: |
462|nn.AdaptiveAvgPool3d | :heavy_check_mark: | :heavy_check_mark: |
463|nn.AdaptiveMaxPool1d | :heavy_check_mark: | :heavy_check_mark: |
464|nn.AdaptiveMaxPool2d | :heavy_check_mark: | :heavy_check_mark: |
465|nn.AdaptiveMaxPool3d | :heavy_check_mark: | :heavy_check_mark: |
466|nn.AlphaDropout | :heavy_check_mark: | :heavy_check_mark: |
467|nn.AvgPool1d | :heavy_check_mark: | :heavy_check_mark:* |
468|nn.AvgPool2d | :heavy_check_mark: | :heavy_check_mark:* |
469|nn.AvgPool3d | :heavy_check_mark: | :heavy_check_mark:* |
470|nn.BatchNorm1d | :heavy_check_mark: | :heavy_check_mark: |
471|nn.BatchNorm2d | :heavy_check_mark: | :heavy_check_mark: |
472|nn.BatchNorm3d | :heavy_check_mark: | :heavy_check_mark: |
473|nn.Bilinear | |
474|nn.CELU | :heavy_check_mark: | :heavy_check_mark: |
475|nn.ChannelShuffle | :heavy_check_mark: | :heavy_check_mark: |
476|nn.ConstantPad1d | :heavy_check_mark: | :heavy_check_mark: |
477|nn.ConstantPad2d | :heavy_check_mark: | :heavy_check_mark: |
478|nn.ConstantPad3d | :heavy_check_mark: | :heavy_check_mark: |
479|nn.Conv1d | :heavy_check_mark: | :heavy_check_mark: |
480|nn.Conv2d | :heavy_check_mark: | :heavy_check_mark: |
481|nn.Conv3d | :heavy_check_mark: | :heavy_check_mark: |
482|nn.ConvTranspose1d | :heavy_check_mark: | :heavy_check_mark: |
483|nn.ConvTranspose2d | :heavy_check_mark: | :heavy_check_mark: |
484|nn.ConvTranspose3d | :heavy_check_mark: | :heavy_check_mark: |
485|nn.CosineSimilarity | |
486|nn.Dropout | :heavy_check_mark: | :heavy_check_mark: |
487|nn.Dropout2d | :heavy_check_mark: | :heavy_check_mark: |
488|nn.Dropout3d | :heavy_check_mark: | :heavy_check_mark: |
489|nn.ELU | :heavy_check_mark: | :heavy_check_mark: |
490|nn.Embedding | :heavy_check_mark: | :heavy_check_mark: |
491|nn.EmbeddingBag | |
492|nn.Flatten | :heavy_check_mark: |
493|nn.Fold | :heavy_check_mark: | :heavy_check_mark: |
494|nn.FractionalMaxPool2d | |
495|nn.FractionalMaxPool3d | |
496|nn.GELU | :heavy_check_mark: | :heavy_check_mark: |
497|nn.GLU | :heavy_check_mark: | :heavy_check_mark: |
498|nn.GroupNorm | :heavy_check_mark: | :heavy_check_mark: |
499|nn.GRU | :heavy_check_mark: | :heavy_check_mark: |
500|nn.GRUCell | |
501|nn.Hardshrink | :heavy_check_mark: |
502|nn.Hardsigmoid | :heavy_check_mark: | :heavy_check_mark: |
503|nn.Hardswish | :heavy_check_mark: | :heavy_check_mark: |
504|nn.Hardtanh | :heavy_check_mark: | :heavy_check_mark: |
505|nn.Identity | :heavy_check_mark: | :heavy_check_mark: |
506|nn.InstanceNorm1d | :heavy_check_mark: |
507|nn.InstanceNorm2d | :heavy_check_mark: | :heavy_check_mark: |
508|nn.InstanceNorm3d | :heavy_check_mark: |
509|nn.LayerNorm | :heavy_check_mark: | :heavy_check_mark: |
510|nn.LazyBatchNorm1d | |
511|nn.LazyBatchNorm2d | |
512|nn.LazyBatchNorm3d | |
513|nn.LazyConv1d | |
514|nn.LazyConv2d | |
515|nn.LazyConv3d | |
516|nn.LazyConvTranspose1d | |
517|nn.LazyConvTranspose2d | |
518|nn.LazyConvTranspose3d | |
519|nn.LazyLinear | |
520|nn.LeakyReLU | :heavy_check_mark: | :heavy_check_mark: |
521|nn.Linear | :heavy_check_mark: | :heavy_check_mark: |
522|nn.LocalResponseNorm | :heavy_check_mark: | :heavy_check_mark: |
523|nn.LogSigmoid | :heavy_check_mark: | :heavy_check_mark: |
524|nn.LogSoftmax | :heavy_check_mark: | :heavy_check_mark: |
525|nn.LPPool1d | :heavy_check_mark: |
526|nn.LPPool2d | :heavy_check_mark: |
527|nn.LSTM | :heavy_check_mark: | :heavy_check_mark: |
528|nn.LSTMCell | |
529|nn.MaxPool1d | :heavy_check_mark: | :heavy_check_mark: |
530|nn.MaxPool2d | :heavy_check_mark: | :heavy_check_mark: |
531|nn.MaxPool3d | :heavy_check_mark: | :heavy_check_mark: |
532|nn.MaxUnpool1d | |
533|nn.MaxUnpool2d | |
534|nn.MaxUnpool3d | |
535|nn.Mish | :heavy_check_mark: | :heavy_check_mark: |
536|nn.MultiheadAttention | :heavy_check_mark: | :heavy_check_mark:* |
537|nn.PairwiseDistance | |
538|nn.PixelShuffle | :heavy_check_mark: | :heavy_check_mark: |
539|nn.PixelUnshuffle | :heavy_check_mark: | :heavy_check_mark: |
540|nn.PReLU | :heavy_check_mark: | :heavy_check_mark: |
541|nn.ReflectionPad1d | :heavy_check_mark: | :heavy_check_mark: |
542|nn.ReflectionPad2d | :heavy_check_mark: | :heavy_check_mark: |
543|nn.ReLU | :heavy_check_mark: | :heavy_check_mark: |
544|nn.ReLU6 | :heavy_check_mark: | :heavy_check_mark: |
545|nn.ReplicationPad1d | :heavy_check_mark: | :heavy_check_mark: |
546|nn.ReplicationPad2d | :heavy_check_mark: | :heavy_check_mark: |
547|nn.ReplicationPad3d | :heavy_check_mark: |
548|nn.RNN | :heavy_check_mark: | :heavy_check_mark:* |
549|nn.RNNBase | |
550|nn.RNNCell | |
551|nn.RReLU | :heavy_check_mark: |
552|nn.SELU | :heavy_check_mark: | :heavy_check_mark: |
553|nn.Sigmoid | :heavy_check_mark: | :heavy_check_mark: |
554|nn.SiLU | :heavy_check_mark: | :heavy_check_mark: |
555|nn.Softmax | :heavy_check_mark: | :heavy_check_mark: |
556|nn.Softmax2d | :heavy_check_mark: | :heavy_check_mark: |
557|nn.Softmin | :heavy_check_mark: |
558|nn.Softplus | :heavy_check_mark: |
559|nn.Softshrink | :heavy_check_mark: |
560|nn.Softsign | :heavy_check_mark: |
561|nn.SyncBatchNorm | |
562|nn.Tanh | :heavy_check_mark: | :heavy_check_mark: |
563|nn.Tanhshrink | :heavy_check_mark: |
564|nn.Threshold | :heavy_check_mark: |
565|nn.Transformer | |
566|nn.TransformerDecoder | |
567|nn.TransformerDecoderLayer | |
568|nn.TransformerEncoder | |
569|nn.TransformerEncoderLayer | |
570|nn.Unflatten | |
571|nn.Unfold | :heavy_check_mark: | :heavy_check_mark: |
572|nn.Upsample | :heavy_check_mark: | :heavy_check_mark: |
573|nn.UpsamplingBilinear2d | :heavy_check_mark: | :heavy_check_mark: |
574|nn.UpsamplingNearest2d | :heavy_check_mark: | :heavy_check_mark: |
575|nn.ZeroPad2d | :heavy_check_mark: | :heavy_check_mark: |
576
577
578| torch.nn.functional | Is Supported | Export to ncnn |
579|---------------------------|----|----|
580|F.adaptive_avg_pool1d | :heavy_check_mark: | :heavy_check_mark: |
581|F.adaptive_avg_pool2d | :heavy_check_mark: | :heavy_check_mark: |
582|F.adaptive_avg_pool3d | :heavy_check_mark: | :heavy_check_mark: |
583|F.adaptive_max_pool1d | :heavy_check_mark: | :heavy_check_mark: |
584|F.adaptive_max_pool2d | :heavy_check_mark: | :heavy_check_mark: |
585|F.adaptive_max_pool3d | :heavy_check_mark: | :heavy_check_mark: |
586|F.affine_grid | :heavy_check_mark: |
587|F.alpha_dropout | :heavy_check_mark: | :heavy_check_mark: |
588|F.avg_pool1d | :heavy_check_mark: | :heavy_check_mark:* |
589|F.avg_pool2d | :heavy_check_mark: | :heavy_check_mark:* |
590|F.avg_pool3d | :heavy_check_mark: | :heavy_check_mark:* |
591|F.batch_norm | :heavy_check_mark: | :heavy_check_mark: |
592|F.bilinear | |
593|F.celu | :heavy_check_mark: |
594|F.conv1d | :heavy_check_mark: | :heavy_check_mark: |
595|F.conv2d | :heavy_check_mark: | :heavy_check_mark: |
596|F.conv3d | :heavy_check_mark: | :heavy_check_mark: |
597|F.conv_transpose1d | :heavy_check_mark: | :heavy_check_mark: |
598|F.conv_transpose2d | :heavy_check_mark: | :heavy_check_mark: |
599|F.conv_transpose3d | :heavy_check_mark: | :heavy_check_mark: |
600|F.cosine_similarity | |
601|F.dropout | :heavy_check_mark: | :heavy_check_mark: |
602|F.dropout2d | :heavy_check_mark: | :heavy_check_mark: |
603|F.dropout3d | :heavy_check_mark: | :heavy_check_mark: |
604|F.elu | :heavy_check_mark: | :heavy_check_mark: |
605|F.elu_ | :heavy_check_mark: | :heavy_check_mark: |
606|F.embedding | :heavy_check_mark: | :heavy_check_mark: |
607|F.embedding_bag | |
608|F.feature_alpha_dropout | :heavy_check_mark: | :heavy_check_mark: |
609|F.fold | :heavy_check_mark: | :heavy_check_mark: |
610|F.fractional_max_pool2d | |
611|F.fractional_max_pool3d | |
612|F.gelu | :heavy_check_mark: | :heavy_check_mark: |
613|F.glu | :heavy_check_mark: | :heavy_check_mark: |
614|F.grid_sample | :heavy_check_mark: | :heavy_check_mark: |
615|F.group_norm | :heavy_check_mark: | :heavy_check_mark: |
616|F.gumbel_softmax | |
617|F.hardshrink | :heavy_check_mark: |
618|F.hardsigmoid | :heavy_check_mark: | :heavy_check_mark: |
619|F.hardswish | :heavy_check_mark: | :heavy_check_mark: |
620|F.hardtanh | :heavy_check_mark: | :heavy_check_mark: |
621|F.hardtanh_ | :heavy_check_mark: | :heavy_check_mark: |
622|F.instance_norm | :heavy_check_mark: | :heavy_check_mark: |
623|F.interpolate | :heavy_check_mark: | :heavy_check_mark: |
624|F.layer_norm | :heavy_check_mark: | :heavy_check_mark: |
625|F.leaky_relu | :heavy_check_mark: | :heavy_check_mark: |
626|F.leaky_relu_ | :heavy_check_mark: | :heavy_check_mark: |
627|F.linear | :heavy_check_mark: | :heavy_check_mark:* |
628|F.local_response_norm | :heavy_check_mark: | :heavy_check_mark: |
629|F.logsigmoid | :heavy_check_mark: | :heavy_check_mark: |
630|F.log_softmax | :heavy_check_mark: | :heavy_check_mark: |
631|F.lp_pool1d | :heavy_check_mark: |
632|F.lp_pool2d | :heavy_check_mark: |
633|F.max_pool1d | :heavy_check_mark: | :heavy_check_mark: |
634|F.max_pool2d | :heavy_check_mark: | :heavy_check_mark: |
635|F.max_pool3d | :heavy_check_mark: | :heavy_check_mark: |
636|F.max_unpool1d | |
637|F.max_unpool2d | |
638|F.max_unpool3d | |
639|F.mish | :heavy_check_mark: | :heavy_check_mark: |
640|F.normalize | :heavy_check_mark: | :heavy_check_mark: |
641|F.one_hot | |
642|F.pad | :heavy_check_mark: | :heavy_check_mark: |
643|F.pairwise_distance | |
644|F.pdist | |
645|F.pixel_shuffle | :heavy_check_mark: | :heavy_check_mark: |
646|F.pixel_unshuffle | :heavy_check_mark: | :heavy_check_mark: |
647|F.prelu | :heavy_check_mark: | :heavy_check_mark: |
648|F.relu | :heavy_check_mark: | :heavy_check_mark: |
649|F.relu_ | :heavy_check_mark: | :heavy_check_mark: |
650|F.relu6 | :heavy_check_mark: | :heavy_check_mark: |
651|F.rrelu | :heavy_check_mark: |
652|F.rrelu_ | :heavy_check_mark: |
653|F.scaled_dot_product_attention | :heavy_check_mark: | |
654|F.selu | :heavy_check_mark: | :heavy_check_mark: |
655|F.sigmoid | :heavy_check_mark: | :heavy_check_mark: |
656|F.silu | :heavy_check_mark: | :heavy_check_mark: |
657|F.softmax | :heavy_check_mark: | :heavy_check_mark: |
658|F.softmin | :heavy_check_mark: |
659|F.softplus | :heavy_check_mark: |
660|F.softshrink | :heavy_check_mark: |
661|F.softsign | :heavy_check_mark: |
662|F.tanh | :heavy_check_mark: | :heavy_check_mark: |
663|F.tanhshrink | :heavy_check_mark: |
664|F.threshold | :heavy_check_mark: |
665|F.threshold_ | :heavy_check_mark: |
666|F.unfold | :heavy_check_mark: | :heavy_check_mark: |
667|F.upsample | :heavy_check_mark: | :heavy_check_mark: |
668|F.upsample_bilinear | :heavy_check_mark: | :heavy_check_mark: |
669|F.upsample_nearest | :heavy_check_mark: | :heavy_check_mark: |