Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions python/tvm/relax/frontend/onnx/onnx_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from tvm.ir import IRModule
from tvm.ir.supply import NameSupply
from tvm.tir.generic import cast
from tvm.topi.utils import get_const_tuple

from ..common import autopad

Expand Down Expand Up @@ -2488,9 +2489,19 @@ def _impl_v17(cls, bb, inputs, attr, params):
axis = attr.get("axis", -1)
epsilon = attr.get("epsilon", 1e-05)

gamma_shape = get_const_tuple(scale.struct_info.shape)

if bias is None:
seq_len = data.struct_info.shape[1].value
bias = relax.const([0.0] * seq_len, dtype="float32")
else:
beta_shape = get_const_tuple(bias.struct_info.shape)
if gamma_shape != beta_shape:
raise ValueError("gamma and beta shapes do not match")

axis = list(axis) if isinstance(axis, (list, tuple)) else [axis]
if len(axis) < len(gamma_shape):
axis.extend(range(axis[-1] + 1, axis[-1] + 1 + len(gamma_shape) - len(axis)))

output = relax.op.nn.layer_norm(data, scale, bias, axis, epsilon)
# Onnx layernorm has 3 outputs but only the first is used.
Expand Down
64 changes: 55 additions & 9 deletions tests/python/relax/test_frontend_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1282,43 +1282,89 @@ def test_mean_variance_norm():


def test_layer_norm():
layer_norm_node = helper.make_node("LayerNormalization", ["a", "b", "c"], ["d"], epsilon=1e-12)
layer_norm_node = helper.make_node(
"LayerNormalization", ["input", "scale", "bias"], ["Y"], epsilon=1e-12
)

graph = helper.make_graph(
[layer_norm_node],
"layer_norm_test",
inputs=[
helper.make_tensor_value_info("a", TensorProto.FLOAT, [32, 32]),
helper.make_tensor_value_info("b", TensorProto.FLOAT, [32]),
helper.make_tensor_value_info("c", TensorProto.FLOAT, [32]),
helper.make_tensor_value_info("input", TensorProto.FLOAT, [32, 32]),
helper.make_tensor_value_info("scale", TensorProto.FLOAT, [32]),
helper.make_tensor_value_info("bias", TensorProto.FLOAT, [32]),
],
outputs=[
helper.make_tensor_value_info("d", TensorProto.FLOAT, [32, 32]),
helper.make_tensor_value_info("Y", TensorProto.FLOAT, [32, 32]),
],
)

model = helper.make_model(graph, producer_name="layer_norm_test")
check_correctness(model)

# Test case with no bias that is an optional input
layer_norm_node = helper.make_node("LayerNormalization", ["a", "b"], ["d"], epsilon=1e-12)
layer_norm_node = helper.make_node(
"LayerNormalization", ["input", "scale"], ["Y"], epsilon=1e-12
)

graph = helper.make_graph(
[layer_norm_node],
"layer_norm_test",
inputs=[
helper.make_tensor_value_info("a", TensorProto.FLOAT, [32, 32]),
helper.make_tensor_value_info("b", TensorProto.FLOAT, [32]),
helper.make_tensor_value_info("input", TensorProto.FLOAT, [32, 32]),
helper.make_tensor_value_info("scale", TensorProto.FLOAT, [32]),
],
outputs=[
helper.make_tensor_value_info("d", TensorProto.FLOAT, [32, 32]),
helper.make_tensor_value_info("Y", TensorProto.FLOAT, [32, 32]),
],
)

model = helper.make_model(graph, producer_name="layer_norm_test")
check_correctness(model)


def test_layer_norm_with_nd_gamma_beta():
layer_norm_node = helper.make_node(
"LayerNormalization", ["input", "scale", "bias"], ["Y"], axis=1, epsilon=1e-12
)

graph = helper.make_graph(
[layer_norm_node],
"layer_norm_with_nd_gamma_beta_test",
inputs=[
helper.make_tensor_value_info("input", TensorProto.FLOAT, [1, 3, 4, 4]),
helper.make_tensor_value_info("scale", TensorProto.FLOAT, [3, 4, 4]),
helper.make_tensor_value_info("bias", TensorProto.FLOAT, [3, 4, 4]),
],
outputs=[
helper.make_tensor_value_info("Y", TensorProto.FLOAT, [1, 3, 4, 4]),
],
)

model = helper.make_model(graph, producer_name="layer_norm_with_nd_gamma_beta_test")
check_correctness(model)

# Test case with no bias that is an optional input
layer_norm_node = helper.make_node(
"LayerNormalization", ["input", "scale"], ["Y"], axis=1, epsilon=1e-12
)

graph = helper.make_graph(
[layer_norm_node],
"layer_norm_with_nd_gamma_beta_test",
inputs=[
helper.make_tensor_value_info("input", TensorProto.FLOAT, [32, 32]),
helper.make_tensor_value_info("scale", TensorProto.FLOAT, [32]),
],
outputs=[
helper.make_tensor_value_info("Y", TensorProto.FLOAT, [32, 32]),
],
)

model = helper.make_model(graph, producer_name="layer_norm_with_nd_gamma_beta_test")
check_correctness(model)


# TODO Enable dynamism
@pytest.mark.parametrize("dynamic", [False])
def test_skiplayernormalization(dynamic):
Expand Down