微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Pytorch:CNN 的 NNAPI 转换 - 节点类型不在 ADDER_MAP 中

如何解决Pytorch:CNN 的 NNAPI 转换 - 节点类型不在 ADDER_MAP 中

我正在尝试使用新的 torch.backends._nnapi.prepare.convert_model_to_nnapi() 函数在 Android 设备上为 nnapi 准备 PyTorch CNN 模型。我基本上遵循 this 教程。这是我的代码

#Prepare the trained model for nnapi on Android device
import sys
import os
import torch
import torch.utils.bundled_inputs
import torch.utils.mobile_optimizer
import torch.backends._nnapi.prepare
import torchvision.models.quantization.mobilenet
from pathlib import Path

#import a function from a different module that returns the model in eval mode,loading the state dict from a checkpoint file (pretrained)
from common.model import trace_model


# This script supports 3 modes of quantization:
# - "none": Fully floating-point model.
# - "core": Quantize the core of the model,but wrap it a
#    quantizer/dequantizer pair,so the interface uses floating point.
# - "full": Quantize the model,and use quantized tensors
#   for input and output.
#
# "none" maintains maximum accuracy
# "core" sacrifices some accuracy for performance,# but maintains the same interface.
# "full" maximized performance (with the same accuracy as "core"),# but requires the application to use quantized tensors.
#
# There is a fourth option,not supported by this script,# where we include the quant/dequant steps as nnapi operators.
def make_mymodel_nnapi(output_dir_path,quantize_mode):
    quantize_core,quantize_iface = {
        "none": (False,False),"core": (True,"full": (True,True),}[quantize_mode]

#get the model. Ignore layers
    model,layers = trace_model() 

#Commented this section out because model I'm using doesn't have fuse_model() fxn.
    # Fuse Batchnorm operators in the floating point model.
    # (Quantized models already have this done.)
    # Remove dropout for this inference-only use case.
    #if not quantize_core:
    #    model.fuse_model()

    
    #assert type(model.classifier[0]) == torch.nn.Dropout

    #model.classifier[0] = torch.nn.Identity()

#create some input based on my CNN's shape
    input_float = torch.zeros(2,272,17,2)
    input_tensor = input_float

    
    # If we're doing a quantized model,we need to trace only the quantized core.
    # So capture the quantizer and dequantizer,use them to prepare the input,# and replace them with identity modules so we can trace without them.
    if quantize_core:
        quantizer = model.quant
        dequantizer = model.dequant
        model.quant = torch.nn.Identity()
        model.dequant = torch.nn.Identity()
        input_tensor = quantizer(input_float)


    
    # Many nnapi backends prefer NHWC tensors,so convert our input to channels_last,# and set the "nnapi_nhwc" attribute for the converter.
    input_tensor = input_tensor.contiguous(memory_format=torch.channels_last)
    input_tensor.nnapi_nhwc = True

    # Trace the model.  nnapi conversion only works with TorchScript models,# and traced models are more likely to convert successfully than scripted.
    with torch.no_grad():
        traced = torch.jit.trace(model,input_tensor)
       

#ERROR IN THIS FUNCTION
#Prepare model for nnapi
    nnapi_model = torch.backends._nnapi.prepare.convert_model_to_nnapi(traced,input_tensor)




if __name__ == "__main__":
    for quantize_mode in ["none","core","full"]:
        make_mymodel_nnapi(Path(os.environ["HOME"]) / "mobilenetv2-nnapi",quantize_mode)

我得到的是 convert_model_to_nnapi() 函数中的错误

Traceback (most recent call last):
  File "trace_model_nnapi.py",line 131,in <module>
    make_mymodel_nnapi(Path(os.environ["HOME"]) / "mobilenetv2-nnapi",quantize_mode)
  File "trace_model_nnapi.py",line 89,in make_mymodel_nnapi
    nnapi_model = torch.backends._nnapi.prepare.convert_model_to_nnapi(traced,input_tensor)
  File "/home/nodog/.local/lib/python3.8/site-packages/torch/backends/_nnapi/prepare.py",line 169,in convert_model_to_nnapi
    ser_model,used_weights,inp_mem_fmts,out_mem_fmts = serialize_model(model,inputs)
  File "/home/nodog/.local/lib/python3.8/site-packages/torch/backends/_nnapi/serializer.py",line 1363,in serialize_model
    return _nnapiSerializer(config).serialize_model(module,line 520,in serialize_model
    self.add_node(node)
  File "/home/nodog/.local/lib/python3.8/site-packages/torch/backends/_nnapi/serializer.py",line 641,in add_node
    raise Exception("Unsupported node kind (%r) in node %r" % (node.kind(),node))
Exception: Unsupported node kind ('aten::contiguous') in node %x.2 : Tensor = aten::contiguous(%x.1,%39) # /home/nodog/docs/asp/mymodel/common/model.py:267:0

如果我注释掉代码的“许多 nnapi 后端更喜欢 NHWC 张量”部分:

Traceback (most recent call last):
  File "trace_model_nnapi.py",node))
Exception: Unsupported node kind ('aten::size') in node %40 : int = aten::size(%x.1,%39) # /home/nodog/docs/asp/mymodel/common/model.py:269:0

似乎“aten::size”和“aten::contiguous”没有包含在 serializer.py 的 ADDER_MAP(看起来是一个节点类型列表)中。有谁知道我是否做错了什么,或者某些功能是否还没有添加nnapi 系统中? 顺便说一下,我使用的 CNN 模型有 Conv1d、Batchnorm、Dropout 和 Relu 层。

谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?