Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[luci/pass] Experiment with decomposed LayerNorm to InstanceNorm #14467

Closed
6 tasks done
seanshpark opened this issue Dec 17, 2024 · 3 comments
Closed
6 tasks done

[luci/pass] Experiment with decomposed LayerNorm to InstanceNorm #14467

seanshpark opened this issue Dec 17, 2024 · 3 comments

Comments

@seanshpark
Copy link
Contributor

seanshpark commented Dec 17, 2024

From #14447 (comment)

Lets' do some performance experiments with

input:

Input [N, L, D] -> (decomposed) LayerNorm [N, L, D]

output

Input [N, L, D] -> Transpose [N, D, L] -> Reshape [N, 1, D, L] ->
InstanceNorm [N, 1, D, L] -> Reshape [N, D, L] -> Transpose [N, L, D]

with our NPU backend

What to do (with draft)

  • choose some models for experiments
  • introduce CircleLayerNorm virtual IR
  • add pass to fuse to CircleLayerNorm
  • add pass to convert to CircleInstanceNorm with Transpose + Reshape
  • add tests for transformation
  • measure in NPU with source model vs converted model
@seanshpark
Copy link
Contributor Author

seanshpark commented Dec 23, 2024

pytorch source

import onnx
import torch
import torch.nn as nn


class LayerNormNet(nn.Module):
    def __init__(self):
        super().__init__()
        self.ln = nn.LayerNorm((4))

    def forward(self, x):
        out = self.ln(x)
        return out


net = LayerNormNet()
inp = torch.randn(1, 16, 4)

torch.onnx.export(net, inp, "ln11.onnx", opset_version=11)
onnx.shape_inference.infer_shapes_path('ln11.onnx', 'ln11-si.onnx')

torch.onnx.export(net, inp, "ln17.onnx", opset_version=17)
onnx.shape_inference.infer_shapes_path('ln17.onnx', 'ln17-si.onnx')

use ln11-si.onnx

@seanshpark
Copy link
Contributor Author

seanshpark commented Dec 23, 2024

circle model

before after
image image

@seanshpark
Copy link
Contributor Author

seanshpark commented Dec 26, 2024

Instance norm with 2 x Transpose + 2 x Reshape uses more cycles than decomposed LayerNorm.
Doesn't look worth adding this transformation.

ref internal ONE/issues/127

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant