brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.5 KiB · 06bc0e7 Raw
113 lines · plain
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 %s13 14// Validate that the TOSA lowering for tosa.max_pool2d produces the same results when15// for fully static and fully dynamic inputs.16 17!tensor_type = tensor<1x4x4x1xf32>18!memref_type = memref<1x4x4x1xf32>19 20// Utility functions21func.func private @printMemrefF32(memref<*xf32>) attributes { llvm.emit_c_interface }22 23func.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 32func.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 with42// a fully dynamically shaped max_pool2d on the same inputs.43func.func @main() {44  %A = arith.constant dense<[[45    [[0.0], [0.1], [0.2], [0.3]], // H = 046    [[1.0], [1.1], [1.2], [1.3]], // H = 147    [[2.0], [2.1], [2.2], [2.3]], // H = 248    [[3.0], [3.1], [3.2], [3.3]]  // H = 349  ]]> : 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 variants54  %result_static  = func.call @max_pool_static(%A) : (!tensor_type) -> !tensor_type55  %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_type58  %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.163  // CHECK-NEXT: 1.264  // CHECK-NEXT: 1.365  // CHECK-NEXT: 1.366 67  // CHECK-NEXT: 2.168  // CHECK-NEXT: 2.269  // CHECK-NEXT: 2.370  // CHECK-NEXT: 2.371 72  // CHECK-NEXT: 3.173  // CHECK-NEXT: 3.274  // CHECK-NEXT: 3.375  // CHECK-NEXT: 3.376 77  // CHECK-NEXT: 3.178  // CHECK-NEXT: 3.279  // CHECK-NEXT: 3.380  // CHECK-NEXT: 3.381 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.189  // CHECK-NEXT: 1.290  // CHECK-NEXT: 1.391  // CHECK-NEXT: 1.392 93  // CHECK-NEXT: 2.194  // CHECK-NEXT: 2.295  // CHECK-NEXT: 2.396  // CHECK-NEXT: 2.397 98  // CHECK-NEXT: 3.199  // CHECK-NEXT: 3.2100  // CHECK-NEXT: 3.3101  // CHECK-NEXT: 3.3102 103  // CHECK-NEXT: 3.1104  // CHECK-NEXT: 3.2105  // CHECK-NEXT: 3.3106  // CHECK-NEXT: 3.3107 108  func.call @printMemrefF32(%unranked_dynamic_buffer) : (memref<*xf32>) -> ()109 110  return111}112 113