-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Squat Packing's rotate and sum operation and resolved type mism…
…atch issues. PiperOrigin-RevId: 715833899
- Loading branch information
1 parent
3917e51
commit 48f181e
Showing
11 changed files
with
499 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
425 changes: 311 additions & 114 deletions
425
lib/Dialect/LinAlg/Conversions/LinalgToTensorExt/LinalgToTensorExt.cpp
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
tests/Dialect/LinAlg/Conversions/linalg_to_tensor_ext/float_small_fc_network.mlir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// This test verifies that a small fully connected network lowers without returning | ||
// an error. | ||
// TODO: write a test that verifies the correctness of the lowering. | ||
|
||
// RUN: heir-opt %s --linalg-to-tensor-ext=tiling-size=4 --tosa-to-secret-arith --canonicalize | FileCheck %s | ||
|
||
// CHECK: func.func @test_float_small_fc_network(%[[ARG:.*]]: !secret.secret<tensor<1x4xf32>>) | ||
module { | ||
func.func @test_float_small_fc_network(%input : !secret.secret<tensor<1x1xf32>>) -> !secret.secret<tensor<1x1xf32>> { | ||
%matrix1 = arith.constant dense<[[1.0, 2.0, 3.0, 4.0]]> : tensor<1x4xf32> | ||
%bias1 = arith.constant dense<[[5.0, 6.0, 7.0, 8.0]]> : tensor<1x4xf32> | ||
%layer1 = secret.generic ins (%input : !secret.secret<tensor<1x1xf32>>) { | ||
^bb0(%converted_input1: tensor<1x1xf32>): | ||
%0 = linalg.matmul ins(%converted_input1, %matrix1 : tensor<1x1xf32>, tensor<1x4xf32>) outs(%bias1 : tensor<1x4xf32>) -> tensor<1x4xf32> | ||
secret.yield %0 : tensor<1x4xf32> | ||
} -> !secret.secret<tensor<1x4xf32>> | ||
|
||
%activation_layer1 = secret.generic ins (%layer1 : !secret.secret<tensor<1x4xf32>>) { | ||
^bb0(%converted_activation_layer_vec1: tensor<1x4xf32>): | ||
%0 = tosa.sigmoid %converted_activation_layer_vec1 : (tensor<1x4xf32>) -> tensor<1x4xf32> | ||
secret.yield %0 : tensor<1x4xf32> | ||
} -> !secret.secret<tensor<1x4xf32>> | ||
|
||
%matrix2 = arith.constant dense<[[10.0, 20.0, 30.0, 40.0], [50.0, 60.0, 70.0, 80.0], [90.0, 100.0, 110.0, 120.0], [130.0, 140.0, 150.0, 160.0]]> : tensor<4x4xf32> | ||
%bias2 = arith.constant dense<[[170.0, 180.0, 190.0, 200.0]]> : tensor<1x4xf32> | ||
%layer2 = secret.generic ins (%layer1 : !secret.secret<tensor<1x4xf32>>) { | ||
^bb0(%converted_vec2: tensor<1x4xf32>): | ||
%1 = linalg.matmul ins(%converted_vec2, %matrix2 : tensor<1x4xf32>, tensor<4x4xf32>) outs(%bias2 : tensor<1x4xf32>) -> tensor<1x4xf32> | ||
secret.yield %1 : tensor<1x4xf32> | ||
} -> !secret.secret<tensor<1x4xf32>> | ||
|
||
%activation_layer2 = secret.generic ins (%layer2 : !secret.secret<tensor<1x4xf32>>) { | ||
^bb0(%converted_activation_layer_vec2: tensor<1x4xf32>): | ||
%0 = tosa.sigmoid %converted_activation_layer_vec2 : (tensor<1x4xf32>) -> tensor<1x4xf32> | ||
secret.yield %0 : tensor<1x4xf32> | ||
} -> !secret.secret<tensor<1x4xf32>> | ||
|
||
%matrix3 = arith.constant dense<[[100.0], [200.0], [300.0], [400.0]]> : tensor<4x1xf32> | ||
%bias3 = arith.constant dense<[[500.0]]> : tensor<1x1xf32> | ||
%layer3 = secret.generic ins (%activation_layer2 : !secret.secret<tensor<1x4xf32>>) { | ||
^bb0(%converted_vec3: tensor<1x4xf32>): | ||
%0 = linalg.matmul ins(%converted_vec3, %matrix3 : tensor<1x4xf32>, tensor<4x1xf32>) outs(%bias3 : tensor<1x1xf32>) -> tensor<1x1xf32> | ||
secret.yield %0 : tensor<1x1xf32> | ||
} -> !secret.secret<tensor<1x1xf32>> | ||
return %layer3 : !secret.secret<tensor<1x1xf32>> | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
tests/Dialect/LinAlg/Conversions/linalg_to_tensor_ext/float_vector_small_matrix_matmul.mlir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// RUN: heir-opt %s --linalg-to-tensor-ext=tiling-size=4 --canonicalize | FileCheck %s | ||
|
||
// CHECK: func.func @test_float_vector_small_matrix_matmul(%[[ARG:.*]]: !secret.secret<tensor<1x4xf32>>) | ||
// CHECK-DAG: %[[TWO:.*]] = arith.constant 2 : index | ||
// CHECK-DAG: %[[ONE:.*]] = arith.constant 1 : index | ||
// CHECK-DAG: %[[BIAS:.*]] = arith.constant dense<5.{{0*}}e+00> : tensor<1x4xf32> | ||
// CHECK-DAG: %[[DIAGONALIZED_MATRIX:.*]] = arith.constant dense | ||
// CHECK-SAME{LITERAL}: <[[ | ||
// CHECK-SAME: 1.{{0*}}e+00, 2.{{0*}}e+00, 3.{{0*}}e+00, 4.{{0*}}e+00], [2.{{0*}}e+00, 3.{{0*}}e+00, 4.{{0*}}e+00, 1.{{0*}}e+00], [3.{{0*}}e+00, 4.{{0*}}e+00, 1.{{0*}}e+00, 2.{{0*}}e+00], [4.{{0*}}e+00, 1.{{0*}}e+00, 2.{{0*}}e+00, 3.{{0*}}e+00 | ||
// CHECK-SAME{LITERAL}: ]]> | ||
// CHECK-DAG: %[[SLICE:.*]] = tensor.extract_slice %[[DIAGONALIZED_MATRIX]][3, 0] [1, 4] [1, 1] | ||
// CHECK: %[[OUT:.*]] = secret.generic ins(%[[ARG]] : !secret.secret<tensor<1x4xf32>>) | ||
// CHECK: ^body(%[[ARG_CONVERTED:.*]]: tensor<1x4xf32>): | ||
// CHECK: %[[MUL:.*]] = arith.mulf %[[ARG_CONVERTED]], %[[SLICE]] | ||
// CHECK: %[[SUM:.*]] = arith.addf %[[MUL]], %[[BIAS]] | ||
// CHECK: %[[ROTATE1:.*]] = tensor_ext.rotate %[[SUM]], %[[TWO]] | ||
// CHECK: %[[ROTATE_AND_SUM_1:.*]] = arith.addf %[[SUM]], %[[ROTATE1]] | ||
// CHECK: %[[ROTATE2:.*]] = tensor_ext.rotate %[[ROTATE_AND_SUM_1]], %[[ONE]] | ||
// CHECK: %[[FINAL_SUM:.*]] = arith.addf %[[ROTATE_AND_SUM_1]], %[[ROTATE2]] | ||
// CHECK: secret.yield %[[FINAL_SUM]] | ||
// CHECK: return %[[OUT]] | ||
module { | ||
func.func @test_float_vector_small_matrix_matmul(%vec : !secret.secret<tensor<1x4xf32>>) -> !secret.secret<tensor<1x1xf32>> { | ||
%matrix = arith.constant dense<[[1.0], [2.0], [3.0], [4.0]]> : tensor<4x1xf32> | ||
%bias = arith.constant dense<[[5.0]]> : tensor<1x1xf32> | ||
%out = secret.generic ins (%vec : !secret.secret<tensor<1x4xf32>>) { | ||
^bb0(%converted_vec: tensor<1x4xf32>): | ||
%0 = linalg.matmul ins(%converted_vec, %matrix : tensor<1x4xf32>, tensor<4x1xf32>) outs(%bias : tensor<1x1xf32>) -> tensor<1x1xf32> | ||
secret.yield %0 : tensor<1x1xf32> | ||
} -> !secret.secret<tensor<1x1xf32>> | ||
return %out : !secret.secret<tensor<1x1xf32>> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...Dialect/LinAlg/Conversions/linalg_to_tensor_ext/integer_rect_matrix_vector_matmul_op.mlir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// RUN: heir-opt %s --linalg-to-tensor-ext=tiling-size=4 --canonicalize | FileCheck %s | ||
|
||
// CHECK: func.func @test_integer_rect_matrix_vector_matmul(%[[ARG:.*]]: !secret.secret<tensor<4x1xi16>>) | ||
// CHECK-DAG: %[[ONE:.*]] = arith.constant 1 : index | ||
// CHECK-DAG: %[[TWO:.*]] = arith.constant 2 : index | ||
// CHECK-DAG: %[[BIAS:.*]] = arith.constant dense | ||
// CHECK-SAME{LITERAL}: <[[17], [18], [17], [18]]> : tensor<4x1xi16> | ||
// CHECK-DAG: %[[DIAGONALIZED_MATRIX:.*]] = arith.constant dense | ||
// CHECK-SAME{LITERAL}: <[[1, 2, 3, 4], [6, 7, 8, 5], [3, 4, 1, 2], [8, 5, 6, 7]]> : tensor<4x4xi16> | ||
// CHECK-DAG: %[[LAST_SLICE:.*]] = tensor.extract_slice %[[DIAGONALIZED_MATRIX]][0, 1] [4, 1] [1, 1] | ||
// CHECK: %[[OUT:.*]] = secret.generic ins(%[[ARG]] : !secret.secret<tensor<4x1xi16>>) | ||
// CHECK: ^body(%[[ARG_CONVERTED:.*]]: tensor<4x1xi16>): | ||
// CHECK: %[[FOR_LOOP_OUT:.*]]:2 = affine.for %[[I:.*]] = 0 to 1 iter_args(%[[RUNNING_SUM:.*]] = %[[BIAS]], %[[ROTATED_VEC:.*]] = %[[ARG_CONVERTED]]) | ||
// CHECK: %[[SLICE:.*]] = tensor.extract_slice %[[DIAGONALIZED_MATRIX]][0, %[[I]]] [4, 1] [1, 1] | ||
// CHECK: %[[MUL:.*]] = arith.muli %[[ROTATED_VEC]], %[[SLICE]] | ||
// CHECK: %[[UPDATED_SUM:.*]] = arith.addi %[[RUNNING_SUM]], %[[MUL]] | ||
// CHECK: %[[UPDATED_ROTATED_VEC:.*]] = tensor_ext.rotate %[[ROTATED_VEC]], %[[ONE]] | ||
// CHECK: affine.yield %[[UPDATED_SUM]], %[[UPDATED_ROTATED_VEC]] | ||
// CHECK: %[[LAST_MUL:.*]] = arith.muli %[[FOR_LOOP_OUT]]#1, %[[LAST_SLICE]] | ||
// CHECK: %[[BEFORE_ROTATE_AND_SUM:.*]] = arith.addi %[[FOR_LOOP_OUT]]#0, %[[LAST_MUL]] | ||
// CHECK: %[[ROTATED_SUM:.*]] = tensor_ext.rotate %[[BEFORE_ROTATE_AND_SUM]], %[[TWO]] | ||
// CHECK: %[[FINAL_SUM:.*]] = arith.addi %[[BEFORE_ROTATE_AND_SUM]], %[[ROTATED_SUM]] | ||
// CHECK: secret.yield %[[FINAL_SUM]] | ||
// CHECK: return %[[OUT]] | ||
module { | ||
func.func @test_integer_rect_matrix_vector_matmul(%vec : !secret.secret<tensor<4x1xi16>>) -> !secret.secret<tensor<2x1xi16>> { | ||
%matrix = arith.constant dense<[[1, 2, 3, 4], [5, 6, 7, 8]]> : tensor<2x4xi16> | ||
%bias = arith.constant dense<[[17], [18]]> : tensor<2x1xi16> | ||
%out = secret.generic ins (%vec : !secret.secret<tensor<4x1xi16>>) { | ||
^bb0(%converted_vec: tensor<4x1xi16>): | ||
%0 = linalg.matmul ins(%matrix, %converted_vec : tensor<2x4xi16>, tensor<4x1xi16>) outs(%bias : tensor<2x1xi16>) -> tensor<2x1xi16> | ||
secret.yield %0 : tensor<2x1xi16> | ||
} -> !secret.secret<tensor<2x1xi16>> | ||
return %out : !secret.secret<tensor<2x1xi16>> | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ialect/LinAlg/Conversions/linalg_to_tensor_ext/integer_small_vector_matrix_matmul_op.mlir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// RUN: heir-opt %s --linalg-to-tensor-ext=tiling-size=4 --canonicalize | FileCheck %s | ||
|
||
// CHECK: func.func @test_integer_square_matrix_vector_matmul(%[[ARG:.*]]: !secret.secret<tensor<4x1xi16>>) | ||
// CHECK-DAG: %[[ONE:.*]] = arith.constant 1 : index | ||
// CHECK-DAG: %[[DIAGONALIZED_MATRIX:.*]] = arith.constant dense | ||
// CHECK-SAME{LITERAL}: <[[1, 2, 3, 4], [6, 7, 8, 5], [11, 12, 9, 10], [16, 13, 14, 15]]> : tensor<4x4xi16> | ||
// CHECK-DAG: %[[BIAS:.*]] = arith.constant dense | ||
// CHECK-SAME{LITERAL}: <[[17], [18], [19], [20]]> : tensor<4x1xi16> | ||
// CHECK-DAG: %[[LAST_SLICE:.*]] = tensor.extract_slice %[[DIAGONALIZED_MATRIX]][0, 3] [4, 1] [1, 1] | ||
// CHECK: %[[OUT:.*]] = secret.generic ins(%[[ARG]] : !secret.secret<tensor<4x1xi16>>) | ||
// CHECK: ^body(%[[ARG_CONVERTED:.*]]: tensor<4x1xi16>): | ||
// CHECK: %[[FOR_LOOP_OUT:.*]]:2 = affine.for %[[I:.*]] = 0 to 3 iter_args(%[[RUNNING_SUM:.*]] = %[[BIAS]], %[[ROTATED_VEC:.*]] = %[[ARG_CONVERTED]]) | ||
// CHECK: %[[SLICE:.*]] = tensor.extract_slice %[[DIAGONALIZED_MATRIX]][0, %[[I]]] [4, 1] [1, 1] | ||
// CHECK: %[[MUL:.*]] = arith.muli %[[ROTATED_VEC]], %[[SLICE]] | ||
// CHECK: %[[UPDATED_SUM:.*]] = arith.addi %[[RUNNING_SUM]], %[[MUL]] | ||
// CHECK: %[[UPDATED_ROTATED_VEC:.*]] = tensor_ext.rotate %[[ROTATED_VEC]], %[[ONE]] | ||
// CHECK: affine.yield %[[UPDATED_SUM]], %[[UPDATED_ROTATED_VEC]] | ||
// CHECK: %[[LAST_MUL:.*]] = arith.muli %[[FOR_LOOP_OUT]]#1, %[[LAST_SLICE]] | ||
// CHECK: %[[FINAL_SUM:.*]] = arith.addi %[[FOR_LOOP_OUT]]#0, %[[LAST_MUL]] | ||
// CHECK: secret.yield %[[FINAL_SUM]] | ||
// CHECK: return %[[OUT]] | ||
module { | ||
func.func @test_integer_square_matrix_vector_matmul(%vec : !secret.secret<tensor<4x1xi16>>) -> !secret.secret<tensor<4x1xi16>> { | ||
%matrix = arith.constant dense<[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]> : tensor<4x4xi16> | ||
%bias = arith.constant dense<[[17], [18], [19], [20]]> : tensor<4x1xi16> | ||
%out = secret.generic ins (%vec : !secret.secret<tensor<4x1xi16>>) { | ||
^bb0(%converted_vec: tensor<4x1xi16>): | ||
%0 = linalg.matmul ins(%matrix, %converted_vec : tensor<4x4xi16>, tensor<4x1xi16>) outs(%bias : tensor<4x1xi16>) -> tensor<4x1xi16> | ||
secret.yield %0 : tensor<4x1xi16> | ||
} -> !secret.secret<tensor<4x1xi16>> | ||
return %out : !secret.secret<tensor<4x1xi16>> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters