|
| 1 | +__all__ = [ |
| 2 | + 'OnnxLayerNorm', |
| 3 | +] |
| 4 | + |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +import torch |
| 8 | +import torch.nn.functional as F |
| 9 | +from torch import nn |
| 10 | + |
| 11 | +from onnx2torch.node_converters.registry import add_converter |
| 12 | +from onnx2torch.onnx_graph import OnnxGraph |
| 13 | +from onnx2torch.onnx_node import OnnxNode |
| 14 | +from onnx2torch.utils.common import OnnxMapping |
| 15 | +from onnx2torch.utils.common import OnnxToTorchModule |
| 16 | +from onnx2torch.utils.common import OperationConverterResult |
| 17 | +from onnx2torch.utils.common import get_shape_from_value_info |
| 18 | +from onnx2torch.utils.common import onnx_mapping_from_node |
| 19 | + |
| 20 | +AXIS_DEFAULT_VALUE = -1 |
| 21 | +EPSILON_DEFAULT_VALUE = 1e-5 |
| 22 | + |
| 23 | + |
| 24 | +class OnnxLayerNorm(nn.Module, OnnxToTorchModule): # pylint: disable=missing-docstring |
| 25 | + def __init__(self, axis: int, epsilon: float): |
| 26 | + super().__init__() |
| 27 | + self.axis = axis |
| 28 | + self.epsilon = epsilon |
| 29 | + |
| 30 | + def forward( # pylint: disable=missing-function-docstring |
| 31 | + self, |
| 32 | + inputs: torch.Tensor, |
| 33 | + scale: torch.Tensor, |
| 34 | + bias: Optional[torch.Tensor] = None, |
| 35 | + ) -> torch.Tensor: |
| 36 | + normalized_shape = inputs.shape[self.axis :] |
| 37 | + return F.layer_norm( |
| 38 | + input=inputs, |
| 39 | + normalized_shape=normalized_shape, |
| 40 | + weight=scale, |
| 41 | + bias=bias, |
| 42 | + eps=self.epsilon, |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +@add_converter(operation_type='LayerNormalization', version=17) |
| 47 | +def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult: |
| 48 | + node_attributes = node.attributes |
| 49 | + |
| 50 | + axis = node_attributes.get('axis', AXIS_DEFAULT_VALUE) |
| 51 | + epsilon = node_attributes.get('epsilon', EPSILON_DEFAULT_VALUE) |
| 52 | + |
| 53 | + if all(value_name in graph.initializers for value_name in node.input_values[1:]): |
| 54 | + input_value_info = graph.value_info[node.input_values[0]] |
| 55 | + input_shape = get_shape_from_value_info(input_value_info) |
| 56 | + |
| 57 | + torch_module = nn.LayerNorm( |
| 58 | + normalized_shape=input_shape[axis:], |
| 59 | + eps=epsilon, |
| 60 | + elementwise_affine=True, |
| 61 | + ) |
| 62 | + |
| 63 | + scale_value_name = node.input_values[1] |
| 64 | + bias_value_name = node.input_values[2] if len(node.input_values) > 2 else None |
| 65 | + |
| 66 | + with torch.no_grad(): |
| 67 | + torch_module.weight.data = graph.initializers[scale_value_name].to_torch() |
| 68 | + if bias_value_name is not None: |
| 69 | + torch_module.bias.data = graph.initializers[bias_value_name].to_torch() |
| 70 | + |
| 71 | + onnx_mapping = OnnxMapping(inputs=(node.input_values[0],), outputs=node.output_values) |
| 72 | + else: |
| 73 | + input_value_info = graph.value_info[node.input_values[0]] |
| 74 | + input_shape = get_shape_from_value_info(input_value_info) |
| 75 | + torch_module = OnnxLayerNorm(axis=axis, epsilon=epsilon) |
| 76 | + onnx_mapping = onnx_mapping_from_node(node) |
| 77 | + |
| 78 | + return OperationConverterResult(torch_module=torch_module, onnx_mapping=onnx_mapping) |
0 commit comments