Skip to content

Commit 5862854

Browse files
authored
[ONNX][TORCH-MLIR] LayerNorm (#2716)
Layer Normalization using the torch.aten.native_layer_norm nod-ai/SHARK-ModelDev#325
1 parent 0860c41 commit 5862854

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

lib/Conversion/TorchOnnxToTorch/DefaultDomainGtoP.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,49 @@ void mlir::torch::onnx_c::populateDefaultDomainGtoP(
410410
}
411411
return failure();
412412
});
413+
patterns.onOp("LayerNormalization", 17,
414+
[](OpBinder binder, ConversionPatternRewriter &rewriter) {
415+
Torch::ValueTensorType Y_type;
416+
Torch::ValueTensorType Mean_type;
417+
Torch::ValueTensorType InvStdDev_type;
418+
Value X;
419+
Value Scale;
420+
Value B;
421+
int64_t axis;
422+
float epsilon;
423+
int64_t stash_type;
424+
if (binder.tensorOperandAtIndex(X, 0) ||
425+
binder.tensorOperandAtIndex(Scale, 1) ||
426+
binder.tensorOperandAtIndex(B, 2) ||
427+
binder.tensorResultTypeAtIndex(Y_type, 0) ||
428+
binder.tensorResultTypeAtIndex(Mean_type, 1) ||
429+
binder.tensorResultTypeAtIndex(InvStdDev_type, 2) ||
430+
binder.s64IntegerAttr(axis, "axis", -1) ||
431+
binder.f32FloatAttr(epsilon, "epsilon", 0.00001) ||
432+
binder.s64IntegerAttr(stash_type, "stash_type", 1))
433+
return failure();
434+
Value constEpsilon = rewriter.create<Torch::ConstantFloatOp>(
435+
binder.getLoc(), rewriter.getType<Torch::FloatType>(),
436+
rewriter.getF64FloatAttr(epsilon));
437+
unsigned rank = 1;
438+
if(std::optional<unsigned> maybeRank = Torch::getTensorRank(X))
439+
rank = *maybeRank;
440+
SmallVector<Value> normalized;
441+
axis = Torch::toPositiveDim(axis, rank);
442+
auto X_type = X.getType().cast<Torch::ValueTensorType>();
443+
ArrayRef<int64_t> X_shape = X_type.getSizes();
444+
for (int64_t n = axis; n < rank ; n++) {
445+
normalized.push_back(rewriter.create<Torch::ConstantIntOp>(
446+
binder.getLoc(), rewriter.getI64IntegerAttr(X_shape[n])));
447+
}
448+
Value normalized_shape = rewriter.create<Torch::PrimListConstructOp>(
449+
binder.getLoc(),
450+
Torch::ListType::get(Torch::IntType::get(binder.op->getContext())),
451+
normalized);
452+
rewriter.replaceOpWithNewOp<Torch::AtenNativeLayerNormOp>(
453+
binder.op, Y_type, Mean_type, InvStdDev_type, X, normalized_shape, Scale, B, constEpsilon);
454+
return success();
455+
});
413456
patterns.onOp("LeakyRelu", 1,
414457
[](OpBinder binder, ConversionPatternRewriter &rewriter) {
415458
Torch::ValueTensorType resultType;

test/Conversion/TorchOnnxToTorch/simple_ops_g_to_p.mlir

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,19 @@ func.func @test_gemm_alpha_beta(%arg0: !torch.vtensor<[3,5],f32>, %arg1: !torch.
116116

117117
// -----
118118

119+
// CHECK-LABEL : func.func @test_layer_norm
120+
func.func @test_layer_norm(%arg0: !torch.vtensor<[3,4],f32>, %arg1: !torch.vtensor<[3,4],f32>, %arg2: !torch.vtensor<[3,4],f32>) -> (!torch.vtensor<[3,4], f32>, !torch.vtensor<[1,1],f32>, !torch.vtensor<[1,1],f32>)
121+
attributes {torch.onnx_meta.ir_version = 6 : si64, torch.onnx_meta.opset_version = 17 : si64, torch.onnx_meta.producer_name = "backend-test", torch.onnx_meta.producer_version = ""} {
122+
// CHECK: %int3 = torch.constant.int 3
123+
// CHECK: %int4 = torch.constant.int 4
124+
// CHECK: %0 = torch.prim.ListConstruct %int3, %int4 : (!torch.int, !torch.int) -> !torch.list<int>
125+
// CHECK: %result0, %result1, %result2 = torch.aten.native_layer_norm %arg0, %0, %arg1, %arg2
126+
%0:3 = torch.operator "onnx.LayerNormalization"(%arg0, %arg1, %arg2) {torch.onnx.axis = 0 : si64} : (!torch.vtensor<[3,4],f32>, !torch.vtensor<[3,4],f32>, !torch.vtensor<[3,4],f32>) -> (!torch.vtensor<[3,4],f32>, !torch.vtensor<[1,1],f32>, !torch.vtensor<[1,1],f32>)
127+
return %0#0, %0#1, %0#2 : !torch.vtensor<[3,4],f32>, !torch.vtensor<[1,1],f32>, !torch.vtensor<[1,1],f32>
128+
}
129+
130+
// -----
131+
119132
// CHECK-LABEL: func.func @test_leaky_relu
120133
func.func @test_leaky_relu(%arg0: !torch.vtensor<[3,4,5],f32>) -> !torch.vtensor<[3,4,5],f32> attributes {torch.onnx_meta.opset_version = 16 : si64} {
121134
// CHECK-DAG: %[[F2:.+]] = torch.constant.float 2

0 commit comments

Comments
 (0)