1// RUN: mlir-opt %s -split-input-file -quant-convert-const | FileCheck %s 2 3// Magic numbers: 4// 7.8125e-03 = 1/128 = 2/256 : real range = [-1.0, 0.9921875] (for 8bit, zeroPoint=128) 5// 1.250000e-01 = 1/8 = 2/16 : real range = [-1.0, 0.875] (for 4bit, zeroPoint=8) 6 7// ----- 8// Verifies u8 affine quantization on a splat tensor. 9// Note that MLIR prints int attributes as signed, so the constant, when 10// quantized, is the signed printed version of an unsigned quantity 11// (-64 signed == 192 unsigned). 12// CHECK-LABEL: constant_splat_tensor_u8_affine 13func.func @constant_splat_tensor_u8_affine() -> tensor<4xf32> { 14 // CHECK: %cst = arith.constant dense<-64> : tensor<4xi8> 15 // CHECK-NEXT: %0 = "quant.scast"(%cst) : (tensor<4xi8>) -> tensor<4x!quant.uniform<u8:f32, 7.812500e-03:128>> 16 %cst = arith.constant dense<0.5> : tensor<4xf32> 17 %1 = "quant.qcast"(%cst) : (tensor<4xf32>) -> tensor<4x!quant.uniform<u8:f32, 7.812500e-03:128>> 18 %2 = "quant.dcast"(%1) : (tensor<4x!quant.uniform<u8:f32, 7.812500e-03:128>>) -> (tensor<4xf32>) 19 return %2 : tensor<4xf32> 20} 21 22// ----- 23// Verifies i8 affine quantization on a splat tensor. 24// CHECK-LABEL: constant_splat_tensor_i8_affine 25func.func @constant_splat_tensor_i8_affine() -> tensor<4xf32> { 26 // CHECK: %cst = arith.constant dense<63> : tensor<4xi8> 27 // CHECK-NEXT: %0 = "quant.scast"(%cst) : (tensor<4xi8>) -> tensor<4x!quant.uniform<i8:f32, 7.812500e-03:-1>> 28 %cst = arith.constant dense<0.5> : tensor<4xf32> 29 %1 = "quant.qcast"(%cst) : (tensor<4xf32>) -> tensor<4x!quant.uniform<i8:f32, 7.812500e-03:-1>> 30 %2 = "quant.dcast"(%1) : (tensor<4x!quant.uniform<i8:f32, 7.812500e-03:-1>>) -> (tensor<4xf32>) 31 return %2 : tensor<4xf32> 32} 33 34// ----- 35// Verifies i8 fixedpoint quantization on a splat tensor. 36// CHECK-LABEL: const_splat_tensor_i8_fixedpoint 37func.func @const_splat_tensor_i8_fixedpoint() -> tensor<4xf32> { 38 // CHECK: %cst = arith.constant dense<64> : tensor<4xi8> 39 // CHECK-NEXT: %0 = "quant.scast"(%cst) : (tensor<4xi8>) -> tensor<4x!quant.uniform<i8:f32, 7.812500e-03>> 40 %cst = arith.constant dense<0.5> : tensor<4xf32> 41 %1 = "quant.qcast"(%cst) : (tensor<4xf32>) -> tensor<4x!quant.uniform<i8:f32, 7.812500e-03>> 42 %2 = "quant.dcast"(%1) : (tensor<4x!quant.uniform<i8:f32, 7.812500e-03>>) -> (tensor<4xf32>) 43 return %2 : tensor<4xf32> 44} 45 46// ----- 47// Verifies i8 fixedpoint quantization on a splat tensor resulting in a negative storage value. 48// CHECK-LABEL: const_splat_tensor_i8_fixedpoint_neg 49func.func @const_splat_tensor_i8_fixedpoint_neg() -> tensor<4xf32> { 50 // CHECK: %cst = arith.constant dense<-64> : tensor<4xi8> 51 %cst = arith.constant dense<-0.5> : tensor<4xf32> 52 %1 = "quant.qcast"(%cst) : (tensor<4xf32>) -> tensor<4x!quant.uniform<i8:f32, 7.812500e-03>> 53 %2 = "quant.dcast"(%1) : (tensor<4x!quant.uniform<i8:f32, 7.812500e-03>>) -> (tensor<4xf32>) 54 return %2 : tensor<4xf32> 55} 56 57// ----- 58// Verifies i8 fixedpoint quantization on a dense tensor, sweeping values. 59// CHECK-LABEL: const_dense_tensor_i8_fixedpoint 60func.func @const_dense_tensor_i8_fixedpoint() -> tensor<7xf32> { 61 // CHECK: %cst = arith.constant dense<[-128, -128, -64, 0, 64, 127, 127]> : tensor<7xi8> 62 %cst = arith.constant dense<[-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, 2.0]> : tensor<7xf32> 63 %1 = "quant.qcast"(%cst) : (tensor<7xf32>) -> tensor<7x!quant.uniform<i8:f32, 7.812500e-03>> 64 %2 = "quant.dcast"(%1) : (tensor<7x!quant.uniform<i8:f32, 7.812500e-03>>) -> (tensor<7xf32>) 65 return %2 : tensor<7xf32> 66} 67 68// ----- 69// Verifies i8 fixedpoint quantization on a sparse tensor, sweeping values. 70// CHECK-LABEL: const_sparse_tensor_i8_fixedpoint 71func.func @const_sparse_tensor_i8_fixedpoint() -> tensor<2x7xf32> { 72 // NOTE: Ugly regex match pattern for opening "[[" of indices tensor. 73 // CHECK: %cst = arith.constant sparse<{{\[}}[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6]], [-128, -128, -64, 0, 64, 127, 127]> : tensor<2x7xi8> 74 %cst = arith.constant sparse< 75 [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6]], 76 [-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, 2.0]> : tensor<2x7xf32> 77 %1 = "quant.qcast"(%cst) : (tensor<2x7xf32>) -> tensor<2x7x!quant.uniform<i8:f32, 7.812500e-03>> 78 %2 = "quant.dcast"(%1) : (tensor<2x7x!quant.uniform<i8:f32, 7.812500e-03>>) -> (tensor<2x7xf32>) 79 return %2 : tensor<2x7xf32> 80} 81 82// ----- 83// Verifies i8 fixedpoint quantization on a primitive const. 84// CHECK-LABEL: const_primitive_float_i8_fixedpoint 85func.func @const_primitive_float_i8_fixedpoint() -> f32 { 86 // CHECK: %c64_i8 = arith.constant 64 : i8 87 // CHECK-NEXT: %0 = "quant.scast"(%c64_i8) : (i8) -> !quant.uniform<i8:f32, 7.812500e-03> 88 %cst = arith.constant 0.5 : f32 89 %1 = "quant.qcast"(%cst) : (f32) -> !quant.uniform<i8:f32, 7.812500e-03> 90 %2 = "quant.dcast"(%1) : (!quant.uniform<i8:f32, 7.812500e-03>) -> (f32) 91 return %2 : f32 92} 93 94// ----- 95// Verifies u4 affine quantization on a dense tensor, sweeping values. 96// CHECK-LABEL: const_dense_tensor_u4_affine 97func.func @const_dense_tensor_u4_affine() -> tensor<7xf32> { 98 // NOTE: Unsigned quantities printed by MLIR as signed. 99 // CHECK: %cst = arith.constant dense<[0, 0, 4, -8, -4, -1, -1]> : tensor<7xi4> 100 %cst = arith.constant dense<[-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, 2.0]> : tensor<7xf32> 101 %1 = "quant.qcast"(%cst) : (tensor<7xf32>) -> tensor<7x!quant.uniform<u4:f32, 1.250000e-01:8>> 102 %2 = "quant.dcast"(%1) : (tensor<7x!quant.uniform<u4:f32, 1.250000e-01:8>>) -> (tensor<7xf32>) 103 return %2 : tensor<7xf32> 104} 105 106// ----- 107// Verifies i4 affine quantization on a dense tensor, sweeping values. 108// CHECK-LABEL: const_dense_tensor_i4_affine 109func.func @const_dense_tensor_i4_affine() -> tensor<7xf32> { 110 // NOTE: Unsigned quantities printed by MLIR as signed. 111 // CHECK: %cst = arith.constant dense<[-8, -8, -5, -1, 3, 7, 7]> : tensor<7xi4> 112 %cst = arith.constant dense<[-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, 2.0]> : tensor<7xf32> 113 %1 = "quant.qcast"(%cst) : (tensor<7xf32>) -> tensor<7x!quant.uniform<i4:f32, 1.250000e-01:-1>> 114 %2 = "quant.dcast"(%1) : (tensor<7x!quant.uniform<i4:f32, 1.250000e-01:-1>>) -> (tensor<7xf32>) 115 return %2 : tensor<7xf32> 116} 117 118// ----- 119// Verifies i4 fixed point quantization on a dense tensor, sweeping values. 120// CHECK-LABEL: const_dense_tensor_i4_fixedpoint 121func.func @const_dense_tensor_i4_fixedpoint() -> tensor<7xf32> { 122 // CHECK: %cst = arith.constant dense<[-8, -8, -4, 0, 4, 7, 7]> : tensor<7xi4> 123 %cst = arith.constant dense<[-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, 2.0]> : tensor<7xf32> 124 %1 = "quant.qcast"(%cst) : (tensor<7xf32>) -> tensor<7x!quant.uniform<i4:f32, 1.250000e-01>> 125 %2 = "quant.dcast"(%1) : (tensor<7x!quant.uniform<i4:f32, 1.250000e-01>>) -> (tensor<7xf32>) 126 return %2 : tensor<7xf32> 127} 128 129// ----- 130// Verifies i8 fixedpoint quantization on a dense tensor, sweeping values, and 131// custom storage range. (the -128 should be clamped to -100, and the 127 should 132// be clamped to 100). 133// CHECK-LABEL: const_custom_storage_range_i8_fixedpoint 134func.func @const_custom_storage_range_i8_fixedpoint() -> tensor<7xf32> { 135 // CHECK: %cst = arith.constant dense<[-100, -100, -64, 0, 64, 100, 100]> : tensor<7xi8> 136 %cst = arith.constant dense<[-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, 2.0]> : tensor<7xf32> 137 %1 = "quant.qcast"(%cst) : (tensor<7xf32>) -> tensor<7x!quant.uniform<i8<-100:100>:f32, 7.812500e-03>> 138 %2 = "quant.dcast"(%1) : (tensor<7x!quant.uniform<i8<-100:100>:f32, 7.812500e-03>>) -> (tensor<7xf32>) 139 return %2 : tensor<7xf32> 140} 141 142// ----- 143// Verifies quantization results of all-0.0 tensors are quantized to zero points. 144// CHECK-LABEL: zero_tensors_to_zero_points 145func.func @zero_tensors_to_zero_points() -> (tensor<7xf32>, tensor<7xf32>, tensor<7xf32>, tensor<7xf32>) { 146 147// CHECK-DAG: %[[cst1:.*]] = arith.constant dense<1> : tensor<7xi8> 148// CHECK-DAG: %[[cst:.*]] = arith.constant dense<-127> : tensor<7xi8> 149// CHECK-DAG: %[[cst0:.*]] = arith.constant dense<0> : tensor<7xi8> 150// CHECK: "quant.scast"(%[[cst0]]) : (tensor<7xi8>) -> tensor<7x!quant.uniform<i8:f32, 1.000000e+00>> 151// CHECK: "quant.scast"(%[[cst]]) : (tensor<7xi8>) -> tensor<7x!quant.uniform<i8<-127:127>:f32, 1.000000e+00:-127>> 152// CHECK: "quant.scast"(%[[cst0]]) : (tensor<7xi8>) -> tensor<7x!quant.uniform<u8:f32, 1.000000e+00>> 153// CHECK: "quant.scast"(%[[cst1]]) : (tensor<7xi8>) -> tensor<7x!quant.uniform<u8<1:255>:f32, 1.000000e+00:1>> 154 155 %cst = arith.constant dense<0.0> : tensor<7xf32> 156 %1 = "quant.qcast"(%cst) : (tensor<7xf32>) -> tensor<7x!quant.uniform<i8:f32, 1.0>> 157 %2 = "quant.dcast"(%1) : (tensor<7x!quant.uniform<i8:f32, 1.0>>) -> (tensor<7xf32>) 158 159 %cst0 = arith.constant dense<0.0> : tensor<7xf32> 160 %3 = "quant.qcast"(%cst0) : (tensor<7xf32>) -> tensor<7x!quant.uniform<i8<-127:127>:f32, 1.0:-127>> 161 %4 = "quant.dcast"(%3) : (tensor<7x!quant.uniform<i8<-127:127>:f32, 1.0:-127>>) -> (tensor<7xf32>) 162 163 %cst1 = arith.constant dense<0.0> : tensor<7xf32> 164 %5 = "quant.qcast"(%cst1) : (tensor<7xf32>) -> tensor<7x!quant.uniform<u8:f32, 1.0>> 165 %6 = "quant.dcast"(%5) : (tensor<7x!quant.uniform<u8:f32, 1.0>>) -> (tensor<7xf32>) 166 167 %cst2 = arith.constant dense<0.0> : tensor<7xf32> 168 %7 = "quant.qcast"(%cst2) : (tensor<7xf32>) -> tensor<7x!quant.uniform<u8<1:255>:f32, 1.0:1>> 169 %8 = "quant.dcast"(%7) : (tensor<7x!quant.uniform<u8<1:255>:f32, 1.0:1>>) -> (tensor<7xf32>) 170 171 return %2, %4, %6, %8 : tensor<7xf32>, tensor<7xf32>, tensor<7xf32>, tensor<7xf32> 172} 173 174// ----- 175// Verifies per-axis quantization results for dense. 176// CHECK-LABEL: per_axis_dense_quantization 177func.func @per_axis_dense_quantization() -> (tensor<2x3xf32>, tensor<2x3xf32>) { 178 179// CHECK-DAG: %[[cst0:.*]] = arith.constant dense<{{\[}}[-128, -1, 1], [127, 1, 3]]> : tensor<2x3xi8> 180// CHECK-DAG: %[[cst:.*]] = arith.constant dense<{{\[}}[-128, 64, 127], [0, 1, 2]]> : tensor<2x3xi8> 181// CHECK: "quant.scast"(%[[cst]]) : (tensor<2x3xi8>) -> tensor<2x3x!quant.uniform<i8:f32:0, {7.812500e-03:128,1.000000e+00}>> 182// CHECK: "quant.scast"(%[[cst0]]) : (tensor<2x3xi8>) -> tensor<2x3x!quant.uniform<i8:f32:1, {7.812500e-03:128,1.000000e+00,1.000000e+00:1}>> 183 184 %cst = arith.constant dense<[[-2.0, -0.5, 0.0], [0.0, 1.0, 2.0]]> : tensor<2x3xf32> 185 %1 = "quant.qcast"(%cst) : (tensor<2x3xf32>) -> tensor<2x3x!quant.uniform<i8:f32:0, {7.812500e-03:128, 1.0}>> 186 %2 = "quant.dcast"(%1) : (tensor<2x3x!quant.uniform<i8:f32:0, {7.812500e-03:128, 1.0}>>) -> (tensor<2x3xf32>) 187 188 %cst0 = arith.constant dense<[[-2.0, -0.5, 0.0], [0.0, 1.0, 2.0]]> : tensor<2x3xf32> 189 %3 = "quant.qcast"(%cst0) : (tensor<2x3xf32>) -> tensor<2x3x!quant.uniform<i8:f32:1, {7.812500e-03:128, 1.0, 1.0:1}>> 190 %4 = "quant.dcast"(%3) : (tensor<2x3x!quant.uniform<i8:f32:1, {7.812500e-03:128, 1.0, 1.0:1}>>) -> (tensor<2x3xf32>) 191 192 return %2, %4 : tensor<2x3xf32>, tensor<2x3xf32> 193} 194