Coder Social home page Coder Social logo

Comments (5)

peterjc123 avatar peterjc123 commented on May 20, 2024

emm,这是因为你的模型没法过jit.trace,底层原因是因为你给定的路径下的LayerNorm2d用了torch.autograd.Function来实现,这个需要自己来实现模型的转换映射逻辑。当然还有个简单的方案就是把他给换回普通的torch.nn.LayerNorm或者torch.nn.functional.layer_norm.

import torch
import torch.nn as nn
import torch.nn.functional as F

def new_layer_norm(self, x):
    normalized_shape = x.shape[1:]
    return F.layer_norm(x, normalized_shape, self.weight.view(-1,1,1).expand(normalized_shape), self.bias.view(-1,1,1).expand(normalized_shape), self.eps)

def patch_layer_norm(model):
    for name, module in model.named_modules():
        if type(module).__name__ == 'LayerNorm2d':
            module.forward = new_layer_norm.get(module, type(module))

patch_layer_norm(model)

from tinyneuralnetwork.

YilanWang avatar YilanWang commented on May 20, 2024

确实我发现是trace的问题, 我也打算用pytorch.ln试试, 但是还是不死心想问问能不能支持下这个 :)

from tinyneuralnetwork.

peterjc123 avatar peterjc123 commented on May 20, 2024

@YilanWang 如果你要走前面那条路也行的,需要实现LayerNormFunction的symbolic方法,参见https://pytorch.org/docs/master/onnx.html#static-symbolic-method 以及 https://pytorch.org/docs/master/onnx.html#c-operators ,就是需要把g.Op里面的方法名换成aten::layer_norm,然后参数前面按照这个函数的方式来传。

from tinyneuralnetwork.

peterjc123 avatar peterjc123 commented on May 20, 2024

@YilanWang 深入研究了一下,发现这个确实是可以支持的,当然还有个问题就是这个graph是不能save的,所以需要先trace再convert,这样可以跳过save这一步。

from tinyneuralnetwork.

peterjc123 avatar peterjc123 commented on May 20, 2024

@YilanWang 结合 #293 应该已经可以了,试下下面的代码

model.eval()
graph = torch.jit.trace(model, dummy_input)

converter = TFLiteConverter(graph, dummy_input, ...)
converter.convert()

from tinyneuralnetwork.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.