Debian Codegraph @

mlir/test/Integration/Dialect/Tosa/CPU/test-maxpool-dynamic.mlir

raw
1 // DEFINE: %{tosa-to-linalg-pipeline} = -pass-pipeline="builtin.module(func.func(tosa-infer-shapes,tosa-to-linalg-named,tosa-to-linalg,tosa-to-arith))"
2
3 // RUN: mlir-opt %s \
4 // RUN: %{tosa-to-linalg-pipeline} \
5 // RUN: | mlir-opt \
6 // RUN: -one-shot-bufferize="bufferize-function-boundaries" \
7 // RUN: -buffer-deallocation-pipeline \
8 // RUN: -test-lower-to-llvm \
9 // RUN: | mlir-runner \
10 // RUN: -entry-point-result=void \
11 // RUN: -shared-libs=%mlir_runner_utils,%mlir_c_runner_utils \
12 // RUN: | FileCheck %s
13
14 // Validate that the TOSA lowering for tosa.max_pool2d produces the same results when
15 // for fully static and fully dynamic inputs.
16
17 !tensor_type = tensor<1x4x4x1xf32>
18 !memref_type = memref<1x4x4x1xf32>
19
20 // Utility functions
21 func.func private @printMemrefF32(memref<*xf32>) attributes { llvm.emit_c_interface }
22
23 func.func @max_pool_static(%arg0: !tensor_type) -> (!tensor_type) {
24 %0 = tosa.max_pool2d %arg0 {
25 pad = array<i64: 1, 1, 1, 1>,
26 kernel = array<i64: 3, 3>,
27 stride = array<i64: 1, 1>
28 } : (tensor<1x4x4x1xf32>) -> tensor<1x4x4x1xf32>
29 return %0 : tensor<1x4x4x1xf32>
30 }
31
32 func.func @max_pool_dynamic(%arg0: tensor<?x?x?x?xf32>) -> (tensor<?x?x?x?xf32>) {
33 %0 = tosa.max_pool2d %arg0 {
34 pad = array<i64: 1, 1, 1, 1>,
35 kernel = array<i64: 3, 3>,
36 stride = array<i64: 1, 1>
37 } : (tensor<?x?x?x?xf32>) -> tensor<?x?x?x?xf32>
38 return %0 : tensor<?x?x?x?xf32>
39 }
40
41 // Test harness to compare the results of a fully statically shaped max_pool2d with
42 // a fully dynamically shaped max_pool2d on the same inputs.
43 func.func @main() {
44 %A = arith.constant dense<[[
45 [[0.0], [0.1], [0.2], [0.3]], // H = 0
46 [[1.0], [1.1], [1.2], [1.3]], // H = 1
47 [[2.0], [2.1], [2.2], [2.3]], // H = 2
48 [[3.0], [3.1], [3.2], [3.3]] // H = 3
49 ]]> : tensor<1x4x4x1xf32>
50
51 %A_dynamic = tensor.cast %A : !tensor_type to tensor<?x?x?x?xf32>
52
53 // Call both static and dynamically sized variants
54 %result_static = func.call @max_pool_static(%A) : (!tensor_type) -> !tensor_type
55 %result_dynamic = func.call @max_pool_dynamic(%A_dynamic) : (tensor<?x?x?x?xf32>) -> tensor<?x?x?x?xf32>
56
57 %static_buffer = bufferization.to_buffer %result_static : !tensor_type to !memref_type
58 %unranked_static_buffer = memref.cast %static_buffer : !memref_type to memref<*xf32>
59
60 // CHECK: Unranked Memref base@ = {{.*}} rank = 4 offset = 0 sizes = [1, 4, 4, 1] strides = [16, 4, 1, 1] data =
61
62 // CHECK-NEXT: 1.1
63 // CHECK-NEXT: 1.2
64 // CHECK-NEXT: 1.3
65 // CHECK-NEXT: 1.3
66
67 // CHECK-NEXT: 2.1
68 // CHECK-NEXT: 2.2
69 // CHECK-NEXT: 2.3
70 // CHECK-NEXT: 2.3
71
72 // CHECK-NEXT: 3.1
73 // CHECK-NEXT: 3.2
74 // CHECK-NEXT: 3.3
75 // CHECK-NEXT: 3.3
76
77 // CHECK-NEXT: 3.1
78 // CHECK-NEXT: 3.2
79 // CHECK-NEXT: 3.3
80 // CHECK-NEXT: 3.3
81
82 func.call @printMemrefF32(%unranked_static_buffer) : (memref<*xf32>) -> ()
83
84 %dynamic_buffer = bufferization.to_buffer %result_dynamic : tensor<?x?x?x?xf32> to memref<?x?x?x?xf32>
85 %unranked_dynamic_buffer = memref.cast %dynamic_buffer : memref<?x?x?x?xf32> to memref<*xf32>
86
87 // CHECK: Unranked Memref base@ = {{.*}} rank = 4 offset = 0 sizes = [1, 4, 4, 1] strides = [16, 4, 1, 1] data =
88 // CHECK-NEXT: 1.1
89 // CHECK-NEXT: 1.2
90 // CHECK-NEXT: 1.3
91 // CHECK-NEXT: 1.3
92
93 // CHECK-NEXT: 2.1
94 // CHECK-NEXT: 2.2
95 // CHECK-NEXT: 2.3
96 // CHECK-NEXT: 2.3
97
98 // CHECK-NEXT: 3.1
99 // CHECK-NEXT: 3.2
100 // CHECK-NEXT: 3.3
101 // CHECK-NEXT: 3.3
102
103 // CHECK-NEXT: 3.1
104 // CHECK-NEXT: 3.2
105 // CHECK-NEXT: 3.3
106 // CHECK-NEXT: 3.3
107
108 func.call @printMemrefF32(%unranked_dynamic_buffer) : (memref<*xf32>) -> ()
109
110 return
111 }
112