|
6 | 6 | * LICENSE file in the root directory of this source tree. |
7 | 7 | */ |
8 | 8 |
|
9 | | -#include <unordered_map> |
10 | | - |
11 | 9 | #include <executorch/runtime/core/exec_aten/exec_aten.h> |
12 | 10 |
|
13 | 11 | namespace executorch::extension { |
@@ -39,58 +37,109 @@ constexpr static int kTensorDTypeBits16 = 22; |
39 | 37 |
|
40 | 38 | using executorch::aten::ScalarType; |
41 | 39 |
|
42 | | -const std::unordered_map<ScalarType, int> scalar_type_to_java_dtype = { |
43 | | - {ScalarType::Byte, kTensorDTypeUInt8}, |
44 | | - {ScalarType::Char, kTensorDTypeInt8}, |
45 | | - {ScalarType::Short, kTensorDTypeInt16}, |
46 | | - {ScalarType::Int, kTensorDTypeInt32}, |
47 | | - {ScalarType::Long, kTensorDTypeInt64}, |
48 | | - {ScalarType::Half, kTensorDTypeHalf}, |
49 | | - {ScalarType::Float, kTensorDTypeFloat}, |
50 | | - {ScalarType::Double, kTensorDTypeDouble}, |
| 40 | +// Returns the Java dtype code for a ScalarType, or -1 if unsupported. |
| 41 | +constexpr int scalar_type_to_java_dtype(ScalarType scalar_type) { |
| 42 | + switch (scalar_type) { |
| 43 | + case ScalarType::Byte: |
| 44 | + return kTensorDTypeUInt8; |
| 45 | + case ScalarType::Char: |
| 46 | + return kTensorDTypeInt8; |
| 47 | + case ScalarType::Short: |
| 48 | + return kTensorDTypeInt16; |
| 49 | + case ScalarType::Int: |
| 50 | + return kTensorDTypeInt32; |
| 51 | + case ScalarType::Long: |
| 52 | + return kTensorDTypeInt64; |
| 53 | + case ScalarType::Half: |
| 54 | + return kTensorDTypeHalf; |
| 55 | + case ScalarType::Float: |
| 56 | + return kTensorDTypeFloat; |
| 57 | + case ScalarType::Double: |
| 58 | + return kTensorDTypeDouble; |
51 | 59 | // These types are not supported yet |
52 | | - // {ScalarType::ComplexHalf, kTensorDTypeComplexHalf}, |
53 | | - // {ScalarType::ComplexFloat, kTensorDTypeComplexFloat}, |
54 | | - // {ScalarType::ComplexDouble, kTensorDTypeComplexDouble}, |
55 | | - {ScalarType::Bool, kTensorDTypeBool}, |
56 | | - {ScalarType::QInt8, kTensorDTypeQint8}, |
57 | | - {ScalarType::QUInt8, kTensorDTypeQuint8}, |
58 | | - {ScalarType::QInt32, kTensorDTypeQint32}, |
59 | | - {ScalarType::BFloat16, kTensorDTypeBFloat16}, |
60 | | - {ScalarType::QUInt4x2, kTensorDTypeQuint4x2}, |
61 | | - {ScalarType::QUInt2x4, kTensorDTypeQuint2x4}, |
62 | | - {ScalarType::Bits1x8, kTensorDTypeBits1x8}, |
63 | | - {ScalarType::Bits2x4, kTensorDTypeBits2x4}, |
64 | | - {ScalarType::Bits4x2, kTensorDTypeBits4x2}, |
65 | | - {ScalarType::Bits8, kTensorDTypeBits8}, |
66 | | - {ScalarType::Bits16, kTensorDTypeBits16}, |
67 | | -}; |
| 60 | + // case ScalarType::ComplexHalf: |
| 61 | + // case ScalarType::ComplexFloat: |
| 62 | + // case ScalarType::ComplexDouble: |
| 63 | + case ScalarType::Bool: |
| 64 | + return kTensorDTypeBool; |
| 65 | + case ScalarType::QInt8: |
| 66 | + return kTensorDTypeQint8; |
| 67 | + case ScalarType::QUInt8: |
| 68 | + return kTensorDTypeQuint8; |
| 69 | + case ScalarType::QInt32: |
| 70 | + return kTensorDTypeQint32; |
| 71 | + case ScalarType::BFloat16: |
| 72 | + return kTensorDTypeBFloat16; |
| 73 | + case ScalarType::QUInt4x2: |
| 74 | + return kTensorDTypeQuint4x2; |
| 75 | + case ScalarType::QUInt2x4: |
| 76 | + return kTensorDTypeQuint2x4; |
| 77 | + case ScalarType::Bits1x8: |
| 78 | + return kTensorDTypeBits1x8; |
| 79 | + case ScalarType::Bits2x4: |
| 80 | + return kTensorDTypeBits2x4; |
| 81 | + case ScalarType::Bits4x2: |
| 82 | + return kTensorDTypeBits4x2; |
| 83 | + case ScalarType::Bits8: |
| 84 | + return kTensorDTypeBits8; |
| 85 | + case ScalarType::Bits16: |
| 86 | + return kTensorDTypeBits16; |
| 87 | + default: |
| 88 | + return -1; |
| 89 | + } |
| 90 | +} |
68 | 91 |
|
69 | | -const std::unordered_map<int, ScalarType> java_dtype_to_scalar_type = { |
70 | | - {kTensorDTypeUInt8, ScalarType::Byte}, |
71 | | - {kTensorDTypeInt8, ScalarType::Char}, |
72 | | - {kTensorDTypeInt16, ScalarType::Short}, |
73 | | - {kTensorDTypeInt32, ScalarType::Int}, |
74 | | - {kTensorDTypeInt64, ScalarType::Long}, |
75 | | - {kTensorDTypeHalf, ScalarType::Half}, |
76 | | - {kTensorDTypeFloat, ScalarType::Float}, |
77 | | - {kTensorDTypeDouble, ScalarType::Double}, |
| 92 | +// Returns the ScalarType for a Java dtype code, or ScalarType::Undefined if |
| 93 | +// unsupported. |
| 94 | +constexpr ScalarType java_dtype_to_scalar_type(int java_dtype) { |
| 95 | + switch (java_dtype) { |
| 96 | + case kTensorDTypeUInt8: |
| 97 | + return ScalarType::Byte; |
| 98 | + case kTensorDTypeInt8: |
| 99 | + return ScalarType::Char; |
| 100 | + case kTensorDTypeInt16: |
| 101 | + return ScalarType::Short; |
| 102 | + case kTensorDTypeInt32: |
| 103 | + return ScalarType::Int; |
| 104 | + case kTensorDTypeInt64: |
| 105 | + return ScalarType::Long; |
| 106 | + case kTensorDTypeHalf: |
| 107 | + return ScalarType::Half; |
| 108 | + case kTensorDTypeFloat: |
| 109 | + return ScalarType::Float; |
| 110 | + case kTensorDTypeDouble: |
| 111 | + return ScalarType::Double; |
78 | 112 | // These types are not supported yet |
79 | | - // {kTensorDTypeComplexHalf, ScalarType::ComplexHalf}, |
80 | | - // {kTensorDTypeComplexFloat, ScalarType::ComplexFloat}, |
81 | | - // {kTensorDTypeComplexDouble, ScalarType::ComplexDouble}, |
82 | | - {kTensorDTypeBool, ScalarType::Bool}, |
83 | | - {kTensorDTypeQint8, ScalarType::QInt8}, |
84 | | - {kTensorDTypeQuint8, ScalarType::QUInt8}, |
85 | | - {kTensorDTypeQint32, ScalarType::QInt32}, |
86 | | - {kTensorDTypeBFloat16, ScalarType::BFloat16}, |
87 | | - {kTensorDTypeQuint4x2, ScalarType::QUInt4x2}, |
88 | | - {kTensorDTypeQuint2x4, ScalarType::QUInt2x4}, |
89 | | - {kTensorDTypeBits1x8, ScalarType::Bits1x8}, |
90 | | - {kTensorDTypeBits2x4, ScalarType::Bits2x4}, |
91 | | - {kTensorDTypeBits4x2, ScalarType::Bits4x2}, |
92 | | - {kTensorDTypeBits8, ScalarType::Bits8}, |
93 | | - {kTensorDTypeBits16, ScalarType::Bits16}, |
94 | | -}; |
| 113 | + // case kTensorDTypeComplexHalf: |
| 114 | + // case kTensorDTypeComplexFloat: |
| 115 | + // case kTensorDTypeComplexDouble: |
| 116 | + case kTensorDTypeBool: |
| 117 | + return ScalarType::Bool; |
| 118 | + case kTensorDTypeQint8: |
| 119 | + return ScalarType::QInt8; |
| 120 | + case kTensorDTypeQuint8: |
| 121 | + return ScalarType::QUInt8; |
| 122 | + case kTensorDTypeQint32: |
| 123 | + return ScalarType::QInt32; |
| 124 | + case kTensorDTypeBFloat16: |
| 125 | + return ScalarType::BFloat16; |
| 126 | + case kTensorDTypeQuint4x2: |
| 127 | + return ScalarType::QUInt4x2; |
| 128 | + case kTensorDTypeQuint2x4: |
| 129 | + return ScalarType::QUInt2x4; |
| 130 | + case kTensorDTypeBits1x8: |
| 131 | + return ScalarType::Bits1x8; |
| 132 | + case kTensorDTypeBits2x4: |
| 133 | + return ScalarType::Bits2x4; |
| 134 | + case kTensorDTypeBits4x2: |
| 135 | + return ScalarType::Bits4x2; |
| 136 | + case kTensorDTypeBits8: |
| 137 | + return ScalarType::Bits8; |
| 138 | + case kTensorDTypeBits16: |
| 139 | + return ScalarType::Bits16; |
| 140 | + default: |
| 141 | + return ScalarType::Undefined; |
| 142 | + } |
| 143 | +} |
95 | 144 |
|
96 | 145 | } // namespace executorch::extension |
0 commit comments