Skip to content

[mlir][nfc] Update tests for Contract -> Op transforms #76054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 21, 2023
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
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
// RUN: mlir-opt %s --transform-interpreter --split-input-file | FileCheck %s

// NOTE - tests in this file are duplicated so that there's a version for
// * _fixed width_ and for _scalable_ vectors.
// In order for the "vector.contract -> vector.outerproduct" patterns to work,
// only the non-reduction dimension can be scalable (*). For Matmul operations
// that is set to be the N dimension (i.e. rows of the output matrix), which
// matches how matrix multiplication are normally implemented for e.g.
// Arm SVE. However, making the M dimension scalable (i.e. columns of the
// output matrix) should work as well.
//
// (*) The conversion tested in this file unrolls along the reduction
// dimension, which is not supported for scalable vectors.
/// Tests for `vector.contract` -> `vector.outerproduct` transformations for
/// matmul operations:
/// C += A * B.
/// (A, B and C are 2-d matrices). ATM three different variants / are tested:
/// * plain (no mask, fixed-wdith vectors),
/// * masked (fixed-width vectors,
/// * scalable (mask + scalable vectors).
/// In order for the "vector.contract -> vector.outerproduct" patterns to work,
/// only the non-reduction dimension can be scalable (*). For matmul operations
/// that is set to be the N dimension (i.e. rows of the output matrix), which
/// matches how matrix multiplication are normally implemented for e.g.
/// Arm SVE. However, making the M dimension scalable (i.e. columns of the
/// output matrix) should work as well.
///
/// (*) The conversion tested in this file unrolls along the reduction
/// dimension, which is not supported for scalable vectors.

// ============================================================================
// Matmul 0 (plain + masked + mixed types)
// ============================================================================
#matmat_accesses_0 = [
affine_map<(m, n, k) -> (m, k)>,
affine_map<(m, n, k) -> (k, n)>,
Expand All @@ -25,6 +27,49 @@
iterator_types = ["parallel", "parallel", "reduction"]
}

#matmat_accesses_1 = [
affine_map<(m, n, k) -> (m, k)>,
affine_map<(m, n, k) -> (n, k)>,
affine_map<(m, n, k) -> (m, n)>
]
#matmat_trait_1 = {
indexing_maps = #matmat_accesses_1,
iterator_types = ["parallel", "parallel", "reduction"]
}

#matmat_accesses_2 = [
affine_map<(m, n, k) -> (k, m)>,
affine_map<(m, n, k) -> (k, n)>,
affine_map<(m, n, k) -> (m, n)>
]
#matmat_trait_2 = {
indexing_maps = #matmat_accesses_2,
iterator_types = ["parallel", "parallel", "reduction"]
}

#matmat_accesses_3 = [
affine_map<(m, n, k) -> (k, m)>,
affine_map<(m, n, k) -> (n, k)>,
affine_map<(m, n, k) -> (m, n)>
]
#matmat_trait_3 = {
indexing_maps = #matmat_accesses_3,
iterator_types = ["parallel", "parallel", "reduction"]
}

#matmat_accesses_4 = [
affine_map<(m, n, k) -> (m, k)>,
affine_map<(m, n, k) -> (k, n)>,
affine_map<(m, n, k) -> (n, m)>
]
#matmat_trait_4 = {
indexing_maps = #matmat_accesses_4,
iterator_types = ["parallel", "parallel", "reduction"]
}

// ============================================================================
// Matmul 0 (plain + masked + mixed types)
// ============================================================================
// CHECK-LABEL: func @matmul
// CHECK-SAME: %[[A:[a-zA-Z0-9]*]]: vector<2x4xf32>,
// CHECK-SAME: %[[B:[a-zA-Z0-9]*]]: vector<4x3xf32>,
Expand Down Expand Up @@ -53,10 +98,10 @@
// CHECK-SAME: : vector<2xf32>, vector<3xf32>
//
// CHECK: return %[[c3]] : vector<2x3xf32>
func.func @matmul(%arg0: vector<2x4xf32>,
%arg1: vector<4x3xf32>,
%arg2: vector<2x3xf32>) -> vector<2x3xf32> {
%0 = vector.contract #matmat_trait_0 %arg0, %arg1, %arg2
func.func @matmul(%A: vector<2x4xf32>,
%B: vector<4x3xf32>,
%C: vector<2x3xf32>) -> vector<2x3xf32> {
%0 = vector.contract #matmat_trait_0 %A, %B, %C
: vector<2x4xf32>, vector<4x3xf32> into vector<2x3xf32>
return %0 : vector<2x3xf32>
}
Expand Down Expand Up @@ -89,10 +134,10 @@ func.func @matmul(%arg0: vector<2x4xf32>,
// CHECK-SAME: : vector<2xf32>, vector<[3]xf32>
//
// CHECK: return %[[c3]] : vector<2x[3]xf32>
func.func @matmul_scalable(%arg0: vector<2x4xf32>,
%arg1: vector<4x[3]xf32>,
%arg2: vector<2x[3]xf32>) -> vector<2x[3]xf32> {
%0 = vector.contract #matmat_trait_0 %arg0, %arg1, %arg2
func.func @matmul_scalable(%A: vector<2x4xf32>,
%B: vector<4x[3]xf32>,
%C: vector<2x[3]xf32>) -> vector<2x[3]xf32> {
%0 = vector.contract #matmat_trait_0 %A, %B, %C
: vector<2x4xf32>, vector<4x[3]xf32> into vector<2x[3]xf32>
return %0 : vector<2x[3]xf32>
}
Expand All @@ -114,11 +159,11 @@ func.func @matmul_scalable(%arg0: vector<2x4xf32>,
// CHECK: %[[T_MASK_R4:.*]] = vector.extract %[[T_MASK]][4] : vector<3x7xi1> from vector<5x3x7xi1>
// CHECK: %{{.*}} = vector.mask %[[T_MASK_R4]] { vector.outerproduct %{{.*}} {kind = #vector.kind<add>} : vector<3xf32>, vector<7xf32> } : vector<3x7xi1> -> vector<3x7xf32>

func.func @masked_matmul(%arg0: vector<3x5xf32>,
%arg1: vector<5x7xf32>,
%arg2: vector<3x7xf32>,
func.func @masked_matmul(%A: vector<3x5xf32>,
%B: vector<5x7xf32>,
%C: vector<3x7xf32>,
%m : vector<3x7x5xi1>) -> vector<3x7xf32> {
%0 = vector.mask %m { vector.contract #matmat_trait_0 %arg0, %arg1, %arg2
%0 = vector.mask %m { vector.contract #matmat_trait_0 %A, %B, %C
: vector<3x5xf32>, vector<5x7xf32> into vector<3x7xf32> } : vector<3x7x5xi1> -> vector<3x7xf32>
return %0 : vector<3x7xf32>
}
Expand All @@ -140,11 +185,11 @@ func.func @masked_matmul(%arg0: vector<3x5xf32>,
// CHECK: %[[T_MASK_R4:.*]] = vector.extract %[[T_MASK]][4] : vector<3x[7]xi1> from vector<5x3x[7]xi1>
// CHECK: %{{.*}} = vector.mask %[[T_MASK_R4]] { vector.outerproduct %{{.*}} {kind = #vector.kind<add>} : vector<3xf32>, vector<[7]xf32> } : vector<3x[7]xi1> -> vector<3x[7]xf32>

func.func @masked_matmul_scalable(%arg0: vector<3x5xf32>,
%arg1: vector<5x[7]xf32>,
%arg2: vector<3x[7]xf32>,
func.func @masked_matmul_scalable(%A: vector<3x5xf32>,
%B: vector<5x[7]xf32>,
%C: vector<3x[7]xf32>,
%m : vector<3x[7]x5xi1>) -> vector<3x[7]xf32> {
%0 = vector.mask %m { vector.contract #matmat_trait_0 %arg0, %arg1, %arg2
%0 = vector.mask %m { vector.contract #matmat_trait_0 %A, %B, %C
: vector<3x5xf32>, vector<5x[7]xf32> into vector<3x[7]xf32> } : vector<3x[7]x5xi1> -> vector<3x[7]xf32>
return %0 : vector<3x[7]xf32>
}
Expand All @@ -160,11 +205,11 @@ func.func @masked_matmul_scalable(%arg0: vector<3x5xf32>,
// CHECK: %[[b1:.*]] = arith.extf %[[b0]] : vector<3xf16> to vector<3xf32>
// CHECK: %[[c0:.*]] = vector.outerproduct %[[a1]], %[[b1]], %[[C]]
// CHECK: return %[[c0]] : vector<2x3xf32>
func.func @matmul_mixed(%arg0: vector<2x1xf16>,
%arg1: vector<1x3xf16>,
%arg2: vector<2x3xf32>) -> vector<2x3xf32>
func.func @matmul_mixed(%A: vector<2x1xf16>,
%B: vector<1x3xf16>,
%C: vector<2x3xf32>) -> vector<2x3xf32>
{
%0 = vector.contract #matmat_trait_0 %arg0, %arg1, %arg2
%0 = vector.contract #matmat_trait_0 %A, %B, %C
: vector<2x1xf16>, vector<1x3xf16> into vector<2x3xf32>
return %0 : vector<2x3xf32>
}
Expand All @@ -180,28 +225,18 @@ func.func @matmul_mixed(%arg0: vector<2x1xf16>,
// CHECK: %[[b1:.*]] = arith.extf %[[b0]] : vector<[3]xf16> to vector<[3]xf32>
// CHECK: %[[c0:.*]] = vector.outerproduct %[[a1]], %[[b1]], %[[C]]
// CHECK: return %[[c0]] : vector<2x[3]xf32>
func.func @matmul_mixed_scalable(%arg0: vector<2x1xf16>,
%arg1: vector<1x[3]xf16>,
%arg2: vector<2x[3]xf32>) -> vector<2x[3]xf32>
func.func @matmul_mixed_scalable(%A: vector<2x1xf16>,
%B: vector<1x[3]xf16>,
%C: vector<2x[3]xf32>) -> vector<2x[3]xf32>
{
%0 = vector.contract #matmat_trait_0 %arg0, %arg1, %arg2
%0 = vector.contract #matmat_trait_0 %A, %B, %C
: vector<2x1xf16>, vector<1x[3]xf16> into vector<2x[3]xf32>
return %0 : vector<2x[3]xf32>
}

// ============================================================================
// Matmul 1 (plain)
// Matmul 1 (plain + scalable)
// ============================================================================
#matmat_accesses_1 = [
affine_map<(m, n, k) -> (m, k)>,
affine_map<(m, n, k) -> (n, k)>,
affine_map<(m, n, k) -> (m, n)>
]
#matmat_trait_1 = {
indexing_maps = #matmat_accesses_1,
iterator_types = ["parallel", "parallel", "reduction"]
}

// CHECK-LABEL: func @matmul_1
// CHECK-SAME: %[[A:[a-zA-Z0-9]*]]: vector<2x1xf32>,
// CHECK-SAME: %[[B:[a-zA-Z0-9]*]]: vector<3x1xf32>,
Expand All @@ -212,11 +247,11 @@ func.func @matmul_mixed_scalable(%arg0: vector<2x1xf16>,
// CHECK: %[[b0:.*]] = vector.extract %[[Bt]][0] : vector<3xf32> from vector<1x3xf32>
// CHECK: %[[c0:.*]] = vector.outerproduct %[[a0]], %[[b0]], %[[C]]
// CHECK: return %[[c0]] : vector<2x3xf32>
func.func @matmul_1(%arg0: vector<2x1xf32>,
%arg1: vector<3x1xf32>,
%arg2: vector<2x3xf32>) -> vector<2x3xf32>
func.func @matmul_1(%A: vector<2x1xf32>,
%B: vector<3x1xf32>,
%C: vector<2x3xf32>) -> vector<2x3xf32>
{
%0 = vector.contract #matmat_trait_1 %arg0, %arg1, %arg2
%0 = vector.contract #matmat_trait_1 %A, %B, %C
: vector<2x1xf32>, vector<3x1xf32> into vector<2x3xf32>
return %0 : vector<2x3xf32>
}
Expand All @@ -231,28 +266,18 @@ func.func @matmul_1(%arg0: vector<2x1xf32>,
// CHECK: %[[b0:.*]] = vector.extract %[[Bt]][0] : vector<[3]xf32> from vector<1x[3]xf32>
// CHECK: %[[c0:.*]] = vector.outerproduct %[[a0]], %[[b0]], %[[C]]
// CHECK: return %[[c0]] : vector<2x[3]xf32>
func.func @matmul_1_scalable(%arg0: vector<2x1xf32>,
%arg1: vector<[3]x1xf32>,
%arg2: vector<2x[3]xf32>) -> vector<2x[3]xf32>
func.func @matmul_1_scalable(%A: vector<2x1xf32>,
%B: vector<[3]x1xf32>,
%C: vector<2x[3]xf32>) -> vector<2x[3]xf32>
{
%0 = vector.contract #matmat_trait_1 %arg0, %arg1, %arg2
%0 = vector.contract #matmat_trait_1 %A, %B, %C
: vector<2x1xf32>, vector<[3]x1xf32> into vector<2x[3]xf32>
return %0 : vector<2x[3]xf32>
}

// ============================================================================
// Matmul 2 (plain)
// Matmul 2 (plain + scalable)
// ============================================================================
#matmat_accesses_2 = [
affine_map<(m, n, k) -> (k, m)>,
affine_map<(m, n, k) -> (k, n)>,
affine_map<(m, n, k) -> (m, n)>
]
#matmat_trait_2 = {
indexing_maps = #matmat_accesses_2,
iterator_types = ["parallel", "parallel", "reduction"]
}

// CHECK-LABEL: func @matmul_2
// CHECK-SAME: %[[A:[a-zA-Z0-9]*]]: vector<1x2xf32>,
// CHECK-SAME: %[[B:[a-zA-Z0-9]*]]: vector<1x3xf32>,
Expand All @@ -261,11 +286,11 @@ func.func @matmul_1_scalable(%arg0: vector<2x1xf32>,
// CHECK: %[[b0:.*]] = vector.extract %[[B]][0] : vector<3xf32> from vector<1x3xf32>
// CHECK: %[[c0:.*]] = vector.outerproduct %[[a0]], %[[b0]], %[[C]]
// CHECK: return %[[c0]] : vector<2x3xf32>
func.func @matmul_2(%arg0: vector<1x2xf32>,
%arg1: vector<1x3xf32>,
%arg2: vector<2x3xf32>) -> vector<2x3xf32>
func.func @matmul_2(%A: vector<1x2xf32>,
%B: vector<1x3xf32>,
%C: vector<2x3xf32>) -> vector<2x3xf32>
{
%0 = vector.contract #matmat_trait_2 %arg0, %arg1, %arg2
%0 = vector.contract #matmat_trait_2 %A, %B, %C
: vector<1x2xf32>, vector<1x3xf32> into vector<2x3xf32>
return %0 : vector<2x3xf32>
}
Expand All @@ -278,28 +303,18 @@ func.func @matmul_2(%arg0: vector<1x2xf32>,
// CHECK: %[[b0:.*]] = vector.extract %[[B]][0] : vector<[3]xf32> from vector<1x[3]xf32>
// CHECK: %[[c0:.*]] = vector.outerproduct %[[a0]], %[[b0]], %[[C]]
// CHECK: return %[[c0]] : vector<2x[3]xf32>
func.func @matmul_2_scalable(%arg0: vector<1x2xf32>,
%arg1: vector<1x[3]xf32>,
%arg2: vector<2x[3]xf32>) -> vector<2x[3]xf32>
func.func @matmul_2_scalable(%A: vector<1x2xf32>,
%B: vector<1x[3]xf32>,
%C: vector<2x[3]xf32>) -> vector<2x[3]xf32>
{
%0 = vector.contract #matmat_trait_2 %arg0, %arg1, %arg2
%0 = vector.contract #matmat_trait_2 %A, %B, %C
: vector<1x2xf32>, vector<1x[3]xf32> into vector<2x[3]xf32>
return %0 : vector<2x[3]xf32>
}

// ============================================================================
// Matmul 3 (plain)
// Matmul 3 (plain + scalable)
// ============================================================================
#matmat_accesses_3 = [
affine_map<(m, n, k) -> (k, m)>,
affine_map<(m, n, k) -> (n, k)>,
affine_map<(m, n, k) -> (m, n)>
]
#matmat_trait_3 = {
indexing_maps = #matmat_accesses_3,
iterator_types = ["parallel", "parallel", "reduction"]
}

// CHECK-LABEL: func @matmul_3
// CHECK-SAME: %[[A:[a-zA-Z0-9]*]]: vector<1x2xf32>,
// CHECK-SAME: %[[B:[a-zA-Z0-9]*]]: vector<3x1xf32>,
Expand All @@ -309,11 +324,11 @@ func.func @matmul_2_scalable(%arg0: vector<1x2xf32>,
// CHECK: %[[b0:.*]] = vector.extract %[[Bt]][0] : vector<3xf32> from vector<1x3xf32>
// CHECK: %[[c0:.*]] = vector.outerproduct %[[a0]], %[[b0]], %[[C]]
// CHECK: return %[[c0]] : vector<2x3xf32>
func.func @matmul_3(%arg0: vector<1x2xf32>,
%arg1: vector<3x1xf32>,
%arg2: vector<2x3xf32>) -> vector<2x3xf32>
func.func @matmul_3(%A: vector<1x2xf32>,
%B: vector<3x1xf32>,
%C: vector<2x3xf32>) -> vector<2x3xf32>
{
%0 = vector.contract #matmat_trait_3 %arg0, %arg1, %arg2
%0 = vector.contract #matmat_trait_3 %A, %B, %C
: vector<1x2xf32>, vector<3x1xf32> into vector<2x3xf32>
return %0 : vector<2x3xf32>
}
Expand All @@ -327,28 +342,18 @@ func.func @matmul_3(%arg0: vector<1x2xf32>,
// CHECK: %[[b0:.*]] = vector.extract %[[Bt]][0] : vector<[3]xf32> from vector<1x[3]xf32>
// CHECK: %[[c0:.*]] = vector.outerproduct %[[a0]], %[[b0]], %[[C]]
// CHECK: return %[[c0]] : vector<2x[3]xf32>
func.func @matmul_3_scalable(%arg0: vector<1x2xf32>,
%arg1: vector<[3]x1xf32>,
%arg2: vector<2x[3]xf32>) -> vector<2x[3]xf32>
func.func @matmul_3_scalable(%A: vector<1x2xf32>,
%B: vector<[3]x1xf32>,
%C: vector<2x[3]xf32>) -> vector<2x[3]xf32>
{
%0 = vector.contract #matmat_trait_3 %arg0, %arg1, %arg2
%0 = vector.contract #matmat_trait_3 %A, %B, %C
: vector<1x2xf32>, vector<[3]x1xf32> into vector<2x[3]xf32>
return %0 : vector<2x[3]xf32>
}

// ============================================================================
// Matmul 4 (plain)
// Matmul 4 (plain + scalable)
// ============================================================================
#matmat_accesses_4 = [
affine_map<(m, n, k) -> (m, k)>,
affine_map<(m, n, k) -> (k, n)>,
affine_map<(m, n, k) -> (n, m)>
]
#matmat_trait_4 = {
indexing_maps = #matmat_accesses_4,
iterator_types = ["parallel", "parallel", "reduction"]
}

// CHECK-LABEL: func @matmul_4
// CHECK-SAME: %[[A:[a-zA-Z0-9]*]]: vector<2x1xf32>,
// CHECK-SAME: %[[B:[a-zA-Z0-9]*]]: vector<1x3xf32>,
Expand All @@ -358,11 +363,11 @@ func.func @matmul_3_scalable(%arg0: vector<1x2xf32>,
// CHECK: %[[a0:.*]] = vector.extract %[[At]][0] : vector<2xf32> from vector<1x2xf32>
// CHECK: %[[c0:.*]] = vector.outerproduct %[[b0]], %[[a0]], %[[C]]
// CHECK: return %[[c0]] : vector<3x2xf32>
func.func @matmul_4(%arg0: vector<2x1xf32>,
%arg1: vector<1x3xf32>,
%arg2: vector<3x2xf32>) -> vector<3x2xf32>
func.func @matmul_4(%A: vector<2x1xf32>,
%B: vector<1x3xf32>,
%C: vector<3x2xf32>) -> vector<3x2xf32>
{
%0 = vector.contract #matmat_trait_4 %arg0, %arg1, %arg2
%0 = vector.contract #matmat_trait_4 %A, %B, %C
: vector<2x1xf32>, vector<1x3xf32> into vector<3x2xf32>
return %0 : vector<3x2xf32>
}
Expand All @@ -376,11 +381,11 @@ func.func @matmul_4(%arg0: vector<2x1xf32>,
// CHECK: %[[a0:.*]] = vector.extract %[[At]][0] : vector<[2]xf32> from vector<1x[2]xf32>
// CHECK: %[[c0:.*]] = vector.outerproduct %[[b0]], %[[a0]], %[[C]]
// CHECK: return %[[c0]] : vector<3x[2]xf32>
func.func @matmul_4_scalable(%arg0: vector<[2]x1xf32>,
%arg1: vector<1x3xf32>,
%arg2: vector<3x[2]xf32>) -> vector<3x[2]xf32>
func.func @matmul_4_scalable(%A: vector<[2]x1xf32>,
%B: vector<1x3xf32>,
%C: vector<3x[2]xf32>) -> vector<3x[2]xf32>
{
%0 = vector.contract #matmat_trait_4 %arg0, %arg1, %arg2
%0 = vector.contract #matmat_trait_4 %A, %B, %C
: vector<[2]x1xf32>, vector<1x3xf32> into vector<3x[2]xf32>
return %0 : vector<3x[2]xf32>
}
Expand Down
Loading