|
| 1 | +#include "larq_compute_engine/mlir/transforms/bitpack.h" |
| 2 | + |
| 3 | +#include <cmath> |
| 4 | +#include <vector> |
| 5 | + |
| 6 | +#include "larq_compute_engine/core/bitpacking/bitpack.h" |
| 7 | +#include "larq_compute_engine/core/types.h" |
| 8 | +#include "mlir/Dialect/Quant/QuantTypes.h" |
| 9 | + |
| 10 | +namespace mlir { |
| 11 | +namespace TFL { |
| 12 | + |
| 13 | +using compute_engine::core::bitpacking_bitwidth; |
| 14 | +using compute_engine::core::round; |
| 15 | +using compute_engine::core::saturate; |
| 16 | +using compute_engine::core::TBitpacked; |
| 17 | +using namespace compute_engine::core::bitpacking; |
| 18 | + |
| 19 | +DenseElementsAttr Bitpack(mlir::Builder* builder, Attribute x) { |
| 20 | + if (!x) return nullptr; |
| 21 | + |
| 22 | + // ShapedType is something like tensor<1x2x3xf32> and element_type is f32 |
| 23 | + auto shaped_type = x.getType().cast<ShapedType>(); |
| 24 | + auto shape = shaped_type.getShape(); |
| 25 | + auto element_type = shaped_type.getElementType(); |
| 26 | + |
| 27 | + int num_rows = shape[0] * shape[1] * shape[2]; |
| 28 | + int unpacked_channels = shape[3]; |
| 29 | + int packed_channels = GetBitpackedSize(unpacked_channels); |
| 30 | + |
| 31 | + std::vector<TBitpacked> new_values(num_rows * packed_channels); |
| 32 | + |
| 33 | + if (element_type.isF32()) { |
| 34 | + const auto& dense_elements_iter = |
| 35 | + x.cast<DenseElementsAttr>().getValues<float>(); |
| 36 | + |
| 37 | + std::vector<float> old_values(num_rows * unpacked_channels); |
| 38 | + |
| 39 | + int i = 0; |
| 40 | + for (float x : dense_elements_iter) { |
| 41 | + old_values[i++] = x; |
| 42 | + } |
| 43 | + assert(i == num_rows * unpacked_channels); |
| 44 | + |
| 45 | + bitpack_matrix(old_values.data(), num_rows, unpacked_channels, |
| 46 | + new_values.data()); |
| 47 | + } else { |
| 48 | + // constant-fold bitpacking int8 tensors is currently not supported |
| 49 | + return nullptr; |
| 50 | + } |
| 51 | + |
| 52 | + RankedTensorType out_tensor_type = |
| 53 | + RankedTensorType::get({shape[0], shape[1], shape[2], packed_channels}, |
| 54 | + builder->getIntegerType(bitpacking_bitwidth)); |
| 55 | + |
| 56 | + return DenseElementsAttr::get<TBitpacked>(out_tensor_type, new_values); |
| 57 | +} |
| 58 | + |
| 59 | +DenseElementsAttr Unpack(Attribute x, ShapedType result_type) { |
| 60 | + if (!x) return nullptr; |
| 61 | + if (!result_type.hasStaticShape()) return nullptr; |
| 62 | + |
| 63 | + auto input_shape = x.getType().cast<ShapedType>().getShape(); |
| 64 | + auto output_shape = result_type.getShape(); |
| 65 | + auto output_type = result_type.getElementType(); |
| 66 | + |
| 67 | + int num_rows = output_shape[0] * output_shape[1] * output_shape[2]; |
| 68 | + int unpacked_channels = output_shape[3]; |
| 69 | + int packed_channels = GetBitpackedSize(unpacked_channels); |
| 70 | + if (input_shape[0] != output_shape[0] || input_shape[1] != output_shape[1] || |
| 71 | + input_shape[2] != output_shape[2] || input_shape[3] != packed_channels) { |
| 72 | + return nullptr; |
| 73 | + } |
| 74 | + |
| 75 | + std::vector<TBitpacked> old_values(num_rows * packed_channels); |
| 76 | + |
| 77 | + const auto& dense_elements_iter = |
| 78 | + x.cast<DenseElementsAttr>().getValues<TBitpacked>(); |
| 79 | + |
| 80 | + int i = 0; |
| 81 | + for (TBitpacked x : dense_elements_iter) { |
| 82 | + old_values[i++] = x; |
| 83 | + } |
| 84 | + assert(i == num_rows * packed_channels); |
| 85 | + |
| 86 | + if (output_type.isF32()) { |
| 87 | + std::vector<float> new_values(num_rows * unpacked_channels); |
| 88 | + |
| 89 | + unpack_matrix(old_values.data(), num_rows, unpacked_channels, |
| 90 | + new_values.data()); |
| 91 | + |
| 92 | + return DenseElementsAttr::get<float>(result_type, new_values); |
| 93 | + } else { |
| 94 | + auto quant_type = output_type.cast<mlir::quant::UniformQuantizedType>(); |
| 95 | + const double scale = quant_type.getScale(); |
| 96 | + const int zero_point = quant_type.getZeroPoint(); |
| 97 | + |
| 98 | + std::int8_t zero_bit_result = saturate(zero_point + round(+1.0 / scale)); |
| 99 | + std::int8_t one_bit_result = saturate(zero_point + round(-1.0 / scale)); |
| 100 | + |
| 101 | + std::vector<std::int8_t> new_values(num_rows * unpacked_channels); |
| 102 | + |
| 103 | + unpack_matrix(old_values.data(), num_rows, unpacked_channels, |
| 104 | + new_values.data(), zero_bit_result, one_bit_result); |
| 105 | + |
| 106 | + return DenseElementsAttr::get<std::int8_t>(result_type, new_values); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +} // namespace TFL |
| 111 | +} // namespace mlir |
0 commit comments