1// RUN: flatbuffer_translate -mlir-to-tflite-flatbuffer %s -o - | flatbuffer_translate --tflite-flatbuffer-to-mlir - -o - | FileCheck %s 2// Ensure constants roundtrip exactly 3 4func @bool() -> tensor<4xi1> { 5 // CHECK-LABEL: @bool 6 // CHECK: value = dense<[false, true, true, false]> : tensor<4xi1> 7 %0 = "tfl.pseudo_const"() { value = dense<[false, true, true, false]> : tensor<4xi1> } : () -> tensor<4xi1> 8 return %0 : tensor<4xi1> 9} 10 11func @complex64() -> tensor<4xcomplex<f32>> { 12 // CHECK-LABEL: @complex64 13 // CHECK: value = opaque<"tf", "0x746674656E736F722464747970653A2044545F434F4D504C455836342074656E736F725F7368617065207B2064696D207B2073697A653A2034207D207D2074656E736F725F636F6E74656E743A20225C3030305C3030305C3230303F5C3030305C3030305C3230303F5C3030305C3030305C303030405C3030305C3030305C303030405C3030305C30303040405C3030305C30303040405C3030305C3030305C323030405C3030305C3030305C3230304022"> : tensor<4xcomplex<f32>> 14 %0 = "tfl.pseudo_const"() { value = opaque<"tf", "0x746674656E736F722464747970653A2044545F434F4D504C455836342074656E736F725F7368617065207B2064696D207B2073697A653A2034207D207D2074656E736F725F636F6E74656E743A20225C3030305C3030305C3230303F5C3030305C3030305C3230303F5C3030305C3030305C303030405C3030305C3030305C303030405C3030305C30303040405C3030305C30303040405C3030305C3030305C323030405C3030305C3030305C3230304022"> : tensor<4xcomplex<f32>> } : () -> tensor<4xcomplex<f32>> 15 return %0 : tensor<4xcomplex<f32>> 16} 17 18func @complex128() -> tensor<4xcomplex<f64>> { 19 // CHECK-LABEL: @complex128 20 // CHECK: value = opaque<"tf", "0x746674656E736F722464747970653A2044545F434F4D504C45583132382074656E736F725F7368617065207B2064696D207B2073697A653A2034207D207D2074656E736F725F636F6E74656E743A20225C3030305C3030305C3030305C3030305C3030305C3030305C3336303F5C3030305C3030305C3030305C3030305C3030305C3030305C303030405C3030305C3030305C3030305C3030305C3030305C3030305C303030405C3030305C3030305C3030305C3030305C3030305C3030305C303030405C3030305C3030305C3030305C3030305C3030305C3030305C303130405C3030305C3030305C3030305C3030305C3030305C3030305C303030405C3030305C3030305C3030305C3030305C3030305C3030305C303230405C3030305C3030305C3030305C3030305C3030305C3030305C3030304022"> : tensor<4xcomplex<f64>> 21 %0 = "tfl.pseudo_const"() { value = opaque<"tf", "0x746674656E736F722464747970653A2044545F434F4D504C45583132382074656E736F725F7368617065207B2064696D207B2073697A653A2034207D207D2074656E736F725F636F6E74656E743A20225C3030305C3030305C3030305C3030305C3030305C3030305C3336303F5C3030305C3030305C3030305C3030305C3030305C3030305C303030405C3030305C3030305C3030305C3030305C3030305C3030305C303030405C3030305C3030305C3030305C3030305C3030305C3030305C303030405C3030305C3030305C3030305C3030305C3030305C3030305C303130405C3030305C3030305C3030305C3030305C3030305C3030305C303030405C3030305C3030305C3030305C3030305C3030305C3030305C303230405C3030305C3030305C3030305C3030305C3030305C3030305C3030304022"> : tensor<4xcomplex<f64>> } : () -> tensor<4xcomplex<f64>> 22 return %0 : tensor<4xcomplex<f64>> 23} 24 25func @f16() -> tensor<4xf16> { 26 // CHECK-LABEL: @f16 27 // CHECK: value = dense<[1.000000e+00, 2.000000e+00, 3.000000e+00, 4.000000e+00]> : tensor<4xf16> 28 %0 = "tfl.pseudo_const"() { value = dense<[1.0, 2.0, 3.0, 4.0]> : tensor<4xf16> } : () -> tensor<4xf16> 29 return %0 : tensor<4xf16> 30} 31 32func @f32() -> tensor<4xf32> { 33 // CHECK-LABEL: @f32 34 // CHECK: value = dense<[1.000000e+00, 2.000000e+00, 3.000000e+00, 4.000000e+00]> : tensor<4xf32> 35 %0 = "tfl.pseudo_const"() { value = dense<[1.0, 2.0, 3.0, 4.0]> : tensor<4xf32> } : () -> tensor<4xf32> 36 return %0 : tensor<4xf32> 37} 38 39func @f64() -> tensor<4xf64> { 40 // CHECK-LABEL: @f64 41 // CHECK: value = dense<[1.000000e+00, 2.000000e+00, 3.000000e+00, 4.000000e+00]> : tensor<4xf64> 42 %0 = "tfl.pseudo_const"() { value = dense<[1.0, 2.0, 3.0, 4.0]> : tensor<4xf64> } : () -> tensor<4xf64> 43 return %0 : tensor<4xf64> 44} 45 46func @i8() -> tensor<4xi8> { 47 // CHECK-LABEL: @i8 48 // CHECK: value = dense<[1, 2, 3, 4]> : tensor<4xi8> 49 %0 = "tfl.pseudo_const" () { value = dense<[1, 2, 3, 4]> : tensor<4xi8> } : () -> tensor<4xi8> 50 return %0 : tensor<4xi8> 51} 52 53func @i16() -> tensor<4xi16> { 54 // CHECK-LABEL: @i16 55 // CHECK: value = dense<[1, 2, 3, 258]> : tensor<4xi16> 56 %0 = "tfl.pseudo_const" () { value = dense<[1, 2, 3, 258]> : tensor<4xi16> } : () -> tensor<4xi16> 57 return %0 : tensor<4xi16> 58} 59 60func @i32() -> tensor<4xi32> { 61 // CHECK-LABEL: @i32 62 // CHECK: value = dense<[1, 2, 3, 16909060]> : tensor<4xi32> 63 // Check bytes come back in the right order 64 %0 = "tfl.pseudo_const" () { value = dense<[1, 2, 3, 16909060]> : tensor<4xi32> } : () -> tensor<4xi32> 65 return %0 : tensor<4xi32> 66} 67 68func @i64() -> tensor<4xi64> { 69 // CHECK-LABEL: @i64 70 // CHECK: value = dense<[1, 2, 3, 72623859790382856]> : tensor<4xi64> 71 %0 = "tfl.pseudo_const" () { value = dense<[1, 2, 3, 72623859790382856]> : tensor<4xi64> } : () -> tensor<4xi64> 72 return %0 : tensor<4xi64> 73} 74 75func @string() -> tensor<2x2x!tf_type.string> { 76 // CHECK-LABEL: @string 77 // CHECK: value = dense<{{\[\["1", "12"\], \["123", "1234"\]\]}}> : tensor<2x2x!tf_type.string> 78 %0 = "tfl.pseudo_const"() { value = dense<[["1", "12"], ["123", "1234"]]> : tensor<2x2x!tf_type.string> } : () -> tensor<2x2x!tf_type.string> 79 return %0 : tensor<2x2x!tf_type.string> 80} 81 82func @string_norank() -> tensor<!tf_type.string> { 83 // CHECK-LABEL: @string_norank 84 // CHECK: value = dense<"test"> : tensor<!tf_type.string> 85 %0 = "tfl.pseudo_const"() { value = dense<"test"> : tensor<!tf_type.string> } : () -> tensor<!tf_type.string> 86 return %0 : tensor<!tf_type.string> 87} 88 89func @uint8() -> tensor<4xui8> { 90 // CHECK-LABEL: @uint8 91 // CHECK: value = dense<[222, 173, 190, 239]> : tensor<4xui8> 92 %0 = "tfl.pseudo_const"() {value = dense<[222, 173, 190, 239]> : tensor<4xui8>} : () -> tensor<4xui8> 93 return %0 : tensor<4xui8> 94} 95 96func @qi32_per_axis() -> tensor<3x3x!quant.uniform<i32:f32:1, {1.0, 0.5:1, 0.25:1}>> { 97 // CHECK-LABEL: @qi32_per_axis 98 // CHECK: {qtype = tensor<3x3x!quant.uniform<i32:f32:1, {1.000000e+00,5.000000e-01:1,2.500000e-01:1}>>, value = dense<1> : tensor<3x3xi32>} : () -> tensor<3x3x!quant.uniform<i32:f32:1, {1.000000e+00,5.000000e-01:1,2.500000e-01:1}>> 99 %0 = "tfl.pseudo_qconst"() { qtype = tensor<3x3x!quant.uniform<i32:f32:1, {1.0, 0.5:1, 0.25:1}>>, value = dense<1> : tensor<3x3xi32>} : () -> tensor<3x3x!quant.uniform<i32:f32:1, {1.0, 0.5:1, 0.25:1}>> 100 return %0 : tensor<3x3x!quant.uniform<i32:f32:1, {1.0, 0.5:1, 0.25:1}>> 101} 102 103func @qi32_per_axis_zero() -> tensor<3x3x!quant.uniform<i32:f32:0, {1.0, 0.5:1, 0.25:1}>> { 104 // CHECK-LABEL: @qi32_per_axis_zero 105 // CHECK: {qtype = tensor<3x3x!quant.uniform<i32:f32:0, {1.000000e+00,5.000000e-01:1,2.500000e-01:1}>>, value = dense<1> : tensor<3x3xi32>} : () -> tensor<3x3x!quant.uniform<i32:f32:0, {1.000000e+00,5.000000e-01:1,2.500000e-01:1}>> 106 %0 = "tfl.pseudo_qconst"() { qtype = tensor<3x3x!quant.uniform<i32:f32:0, {1.0, 0.5:1, 0.25:1}>>, value = dense<1> : tensor<3x3xi32>} : () -> tensor<3x3x!quant.uniform<i32:f32:0, {1.0, 0.5:1, 0.25:1}>> 107 return %0 : tensor<3x3x!quant.uniform<i32:f32:0, {1.0, 0.5:1, 0.25:1}>> 108} 109 110func @qu8() -> tensor<3x!quant.uniform<u8<1:255>:f32, 1.0>> { 111 // CHECK-LABEL: @qu8 112 // CHECK: {qtype = tensor<3x!quant.uniform<u8<1:255>:f32, 1.000000e+00>>, value = dense<1> : tensor<3xi8>} : () -> tensor<3x!quant.uniform<u8<1:255>:f32, 1.000000e+00>> 113 %0 = "tfl.pseudo_qconst"() { qtype = tensor<3x!quant.uniform<u8<1:255>:f32, 1.0>>, value = dense<1> : tensor<3xi8>} : () -> tensor<3x!quant.uniform<u8<1:255>:f32, 1.0>> 114 return %0 : tensor<3x!quant.uniform<u8<1:255>:f32, 1.0>> 115} 116 117func @sparse_f32() -> tensor<3x2xf32> { 118 // CHECK-LABEL: @sparse_f32 119 // CHECK: {compressed_data = dense<[1.000000e+00, 2.000000e+00, 5.000000e-01, 2.500000e-01, -1.000000e+00, -2.000000e+00, -5.000000e-01, -2.500000e-01]> : tensor<8xf32>, s_param = {block_map = [3 : i32, 1 : i32], dim_metadata = [{dense_size = 16 : i32, format = "DENSE", indices = [], segments = []}, {dense_size = 1 : i32, format = "DENSE", indices = [], segments = []}, {dense_size = 1 : i32, format = "DENSE", indices = [], segments = []}, {dense_size = 0 : i32, format = "SPARSE_CSR", indices = [1 : i32, 4 : i32, 9 : i32], segments = [0 : i32, 5 : i32, 11 : i32]}], traversal_order = [0 : i32, 1 : i32, 2 : i32, 3 : i32]}, value = dense<0.000000e+00> : tensor<3x2xf32>} 120 %0 = "tfl.pseudo_sparse_const"() {compressed_data = dense<[1.0, 2.0, 0.5, 0.25, -1.0, -2.0, -0.5, -0.25]> : tensor<8xf32>, s_param = {block_map = [3 : i32, 1 : i32], dim_metadata = [{dense_size = 16 : i32, format = "DENSE", indices = [], segments = []}, {dense_size = 1 : i32, format = "DENSE", indices = [], segments = []}, {dense_size = 1 : i32, format = "DENSE", indices = [], segments = []}, {dense_size = 0 : i32, format = "SPARSE_CSR", indices = [1 : i32, 4 : i32, 9 : i32], segments = [0 : i32, 5 : i32, 11 : i32]}], traversal_order = [0 : i32, 1 : i32, 2 : i32, 3 : i32]}, value = dense<0.000000e+00> : tensor<3x2xf32>} : () -> tensor<3x2xf32> 121 return %0: tensor<3x2xf32> 122} 123 124func @sparse_f16() -> tensor<3x2xf16> { 125 // CHECK-LABEL: @sparse_f16 126 // CHECK: {compressed_data = dense<[1.000000e+00, 2.000000e+00, 5.000000e-01, 2.500000e-01, -1.000000e+00, -2.000000e+00, -5.000000e-01, -2.500000e-01]> : tensor<8xf16>, s_param = {block_map = [3 : i32, 1 : i32], dim_metadata = [{dense_size = 16 : i32, format = "DENSE", indices = [], segments = []}, {dense_size = 1 : i32, format = "DENSE", indices = [], segments = []}, {dense_size = 1 : i32, format = "DENSE", indices = [], segments = []}, {dense_size = 0 : i32, format = "SPARSE_CSR", indices = [1 : i32, 4 : i32, 9 : i32], segments = [0 : i32, 5 : i32, 11 : i32]}], traversal_order = [0 : i32, 1 : i32, 2 : i32, 3 : i32]}, value = dense<0.000000e+00> : tensor<3x2xf16>} 127 %0 = "tfl.pseudo_sparse_const"() {compressed_data = dense<[1.0, 2.0, 0.5, 0.25, -1.0, -2.0, -0.5, -0.25]> : tensor<8xf16>, s_param = {block_map = [3 : i32, 1 : i32], dim_metadata = [{dense_size = 16 : i32, format = "DENSE", indices = [], segments = []}, {dense_size = 1 : i32, format = "DENSE", indices = [], segments = []}, {dense_size = 1 : i32, format = "DENSE", indices = [], segments = []}, {dense_size = 0 : i32, format = "SPARSE_CSR", indices = [1 : i32, 4 : i32, 9 : i32], segments = [0 : i32, 5 : i32, 11 : i32]}], traversal_order = [0 : i32, 1 : i32, 2 : i32, 3 : i32]}, value = dense<0.000000e+00> : tensor<3x2xf16>} : () -> tensor<3x2xf16> 128 return %0: tensor<3x2xf16> 129} 130 131func @sparse_qu8() -> tensor<3x2x!quant.uniform<u8<1:255>:f32, 1.0>> { 132 // CHECK-LABEL: @sparse_qu8 133 // CHECK: {compressed_data = dense<[1, 2, 3, 4, -1, -2, -3, -4]> : tensor<8xi8>, qtype = tensor<3x2x!quant.uniform<u8<1:255>:f32, 1.000000e+00>>, s_param = {block_map = [3 : i32, 1 : i32], dim_metadata = [{dense_size = 16 : i32, format = "DENSE", indices = [], segments = []}, {dense_size = 1 : i32, format = "DENSE", indices = [], segments = []}, {dense_size = 1 : i32, format = "DENSE", indices = [], segments = []}, {dense_size = 0 : i32, format = "SPARSE_CSR", indices = [1 : i32, 4 : i32, 9 : i32], segments = [0 : i32, 5 : i32, 11 : i32]}], traversal_order = [0 : i32, 1 : i32, 2 : i32, 3 : i32]}, value = dense<0> : tensor<3x2xi8>} 134 %0 = "tfl.pseudo_sparse_qconst"() {compressed_data = dense<[1, 2, 3, 4, -1, -2, -3, -4]> : tensor<8xi8>, qtype = tensor<3x2x!quant.uniform<u8<1:255>:f32, 1.0>>, s_param = {block_map = [3 : i32, 1 : i32], dim_metadata = [{dense_size = 16 : i32, format = "DENSE", indices = [], segments = []}, {dense_size = 1 : i32, format = "DENSE", indices = [], segments = []}, {dense_size = 1 : i32, format = "DENSE", indices = [], segments = []}, {dense_size = 0 : i32, format = "SPARSE_CSR", indices = [1 : i32, 4 : i32, 9 : i32], segments = [0 : i32, 5 : i32, 11 : i32]}], traversal_order = [0 : i32, 1 : i32, 2 : i32, 3 : i32]}, value = dense<42> : tensor<3x2xi8>} : () -> tensor<3x2x!quant.uniform<u8<1:255>:f32, 1.0>> 135 return %0: tensor<3x2x!quant.uniform<u8<1:255>:f32, 1.0>> 136} 137 138// Identity function to make the exporter happy 139func @main(%arg0: tensor<4xi8>) -> tensor<4xi8> { 140 return %arg0 : tensor<4xi8> 141} 142