Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[PT FE] Enhance aten::layer_norm translation with type alignment for mixed precision support (#36670)

Mateusz Mikolajczyk81c7a428b3c4
diff --git a/src/frontends/pytorch/src/op/layer_norm.cpp b/src/frontends/pytorch/src/op/layer_norm.cpp
index ffe0260fb40..d561d32a136 100644
--- a/src/frontends/pytorch/src/op/layer_norm.cpp
+++ b/src/frontends/pytorch/src/op/layer_norm.cpp
@@ -5,6 +5,7 @@
#include "openvino/frontend/pytorch/node_context.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/constant.hpp"
+#include "openvino/op/convert_like.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/mvn.hpp"
#include "openvino/op/range.hpp"
@@ -34,10 +35,14 @@ OutputVector translate_layer_norm(const NodeContext& context) {
auto out_node =
context.mark_node(std::make_shared<v6::MVN>(context.get_input(0), axes, true, eps, MVNEpsMode::INSIDE_SQRT));
if (!context.input_is_none(2)) {
- out_node = context.mark_node(std::make_shared<v1::Multiply>(out_node, context.get_input(2)));
+ auto weight = context.get_input(2);
+ weight = context.mark_node(std::make_shared<v1::ConvertLike>(weight, out_node));
+ out_node = context.mark_node(std::make_shared<v1::Multiply>(out_node, weight));
}
if (!context.input_is_none(3)) {
- out_node = context.mark_node(std::make_shared<v1::Add>(out_node, context.get_input(3)));
+ auto bias = context.get_input(3);
+ bias = context.mark_node(std::make_shared<v1::ConvertLike>(bias, out_node));
+ out_node = context.mark_node(std::make_shared<v1::Add>(out_node, bias));
}
// Input with index 5 is flag "cudnn_enabled" we can ignore it
return {out_node};
diff --git a/tests/layer_tests/pytorch_tests/test_layer_norm.py b/tests/layer_tests/pytorch_tests/test_layer_norm.py
index 9d8fb6ea826..0d41b544c31 100644
--- a/tests/layer_tests/pytorch_tests/test_layer_norm.py
+++ b/tests/layer_tests/pytorch_tests/test_layer_norm.py
@@ -48,4 +48,50 @@ class TestLayerNorm(PytorchLayerTest):
@pytest.mark.precommit
@pytest.mark.precommit_torch_export
def test_layer_norm(self, normalized_shape, weight, bias, eps, ie_device, precision, ir_version):
- self._test(*self.create_model(normalized_shape, weight, bias, eps), ie_device, precision, ir_version)
\ No newline at end of file
+ self._test(*self.create_model(normalized_shape, weight, bias, eps), ie_device, precision, ir_version)
+
+
+class TestLayerNormMixedPrecision(PytorchLayerTest):
+ def _prepare_input(self):
+ return (self.random.randn(20, 5, 10, 10),)
+
+ def create_model(self, normalized_shape, input_dtype, with_bias):
+ import torch
+ import torch.nn.functional as F
+
+ weight = self.random.torch_randn(*normalized_shape)
+ bias = self.random.torch_randn(*normalized_shape) if with_bias else None
+
+ class aten_layer_norm_mixed(torch.nn.Module):
+ def __init__(self, normalized_shape, weight, bias, input_dtype):
+ super().__init__()
+ self.normalized_shape = normalized_shape
+ self.weight = weight
+ self.bias = bias
+ self.input_dtype = input_dtype
+
+ def forward(self, x):
+ x = x.to(self.input_dtype)
+ out = F.layer_norm(x, self.normalized_shape, self.weight, self.bias, 1e-5)
+ return out.to(torch.float32)
+
+ return aten_layer_norm_mixed(normalized_shape, weight, bias, input_dtype), "aten::layer_norm"
+
+ @pytest.mark.parametrize("normalized_shape", [[10,], [10, 10]])
+ @pytest.mark.parametrize("input_dtype", ["float16", "bfloat16"])
+ @pytest.mark.parametrize("with_bias", [False, True])
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
+ def test_layer_norm_mixed_precision(self, normalized_shape, input_dtype, with_bias, ie_device, precision, ir_version):
+ import torch
+
+ torch_dtype = {"float16": torch.float16, "bfloat16": torch.bfloat16}[input_dtype]
+ custom_eps = 1e-2 if input_dtype == "float16" else 5e-2
+ self._test(
+ *self.create_model(normalized_shape, torch_dtype, with_bias),
+ ie_device,
+ precision,
+ ir_version,
+ custom_eps=custom_eps,
+ )
\ No newline at end of file