|
| 1 | +/* Copyright 2025 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +#include "tensorflow/lite/micro/kernels/xtensa/xtensa_decode_state_huffman.h" |
| 17 | + |
| 18 | +#include <cstddef> |
| 19 | + |
| 20 | +#include "tensorflow/lite/kernels/internal/compatibility.h" |
| 21 | +#include "tensorflow/lite/micro/kernels/kernel_util.h" |
| 22 | +#include "tensorflow/lite/micro/kernels/xtensa/xtensa.h" |
| 23 | +#include "tensorflow/lite/micro/micro_log.h" |
| 24 | +#include "tensorflow/lite/micro/micro_profiler.h" |
| 25 | + |
| 26 | +namespace tflite { |
| 27 | + |
| 28 | +TfLiteStatus XtensaDecodeStateHuffman::Decode(const TfLiteEvalTensor& input, |
| 29 | + const TfLiteEvalTensor& ancillary, |
| 30 | + const TfLiteEvalTensor& output) { |
| 31 | + void* const buffer = const_cast<void*>(micro::GetTensorData<void>(&output)); |
| 32 | + TFLITE_DCHECK(buffer != nullptr); |
| 33 | + |
| 34 | + switch (output.type) { |
| 35 | + case kTfLiteInt8: |
| 36 | + if (use_32bit_table_) { |
| 37 | + Decompress32BitTable_Xtensa(static_cast<int8_t*>(buffer)); |
| 38 | + } else { |
| 39 | + Decompress16BitTable_Xtensa(static_cast<int8_t*>(buffer)); |
| 40 | + } |
| 41 | + break; |
| 42 | + case kTfLiteInt16: |
| 43 | + Decompress32BitTable_Xtensa(static_cast<int16_t*>(buffer)); |
| 44 | + break; |
| 45 | + default: |
| 46 | + MicroPrintf("unsupported tensor type %s", TfLiteTypeGetName(output.type)); |
| 47 | + return kTfLiteError; |
| 48 | + } |
| 49 | + |
| 50 | + return kTfLiteOk; |
| 51 | +} |
| 52 | + |
| 53 | +void XtensaDecodeStateHuffman::Decompress16BitTable_Xtensa(int8_t* buffer) { |
| 54 | + ScopedMicroProfiler scoped_profiler(__func__, micro_profiler_); |
| 55 | + |
| 56 | + size_t remaining = count_codewords_; |
| 57 | + const uint16_t* huffman_tables = |
| 58 | + static_cast<const uint16_t*>(huffman_tables_); |
| 59 | + const uint16_t* __restrict p_stream = |
| 60 | + reinterpret_cast<const uint16_t*>(compressed_codewords_); |
| 61 | + |
| 62 | + WAE_BITPTR(15); |
| 63 | + WAE_BITSUSED(1); |
| 64 | + // byte swap the preload half-word |
| 65 | + WAE_BITHEAD(p_stream[0] << 8 | p_stream[0] >> 8); |
| 66 | + WAE_SEARCHDONE(1); |
| 67 | + WAE_FIRST_TS(initial_table_size_); |
| 68 | + AE_VLDL16C(p_stream); |
| 69 | + |
| 70 | + while (remaining--) { |
| 71 | + xtbool complete = 0; |
| 72 | + unsigned long int symbol; |
| 73 | + |
| 74 | + while (!complete) { |
| 75 | + AE_VLDL16T(complete, symbol, huffman_tables); |
| 76 | + AE_VLDL16C(p_stream); |
| 77 | + } |
| 78 | + |
| 79 | + *buffer++ = symbol; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +template <typename T> |
| 84 | +void XtensaDecodeStateHuffman::Decompress32BitTable_Xtensa(T* buffer) { |
| 85 | + ScopedMicroProfiler scoped_profiler(__func__, micro_profiler_); |
| 86 | + |
| 87 | + size_t remaining = count_codewords_; |
| 88 | + const uint32_t* huffman_tables = |
| 89 | + static_cast<const uint32_t*>(huffman_tables_); |
| 90 | + const uint16_t* __restrict p_stream = |
| 91 | + reinterpret_cast<const uint16_t*>(compressed_codewords_); |
| 92 | + |
| 93 | + WAE_BITPTR(15); |
| 94 | + WAE_BITSUSED(1); |
| 95 | + // byte swap the preload half-word |
| 96 | + WAE_BITHEAD(p_stream[0] << 8 | p_stream[0] >> 8); |
| 97 | + WAE_SEARCHDONE(1); |
| 98 | + WAE_FIRST_TS(initial_table_size_); |
| 99 | + AE_VLDL16C(p_stream); |
| 100 | + |
| 101 | + while (remaining--) { |
| 102 | + xtbool complete = 0; |
| 103 | + unsigned long int symbol; |
| 104 | + |
| 105 | + while (!complete) { |
| 106 | + AE_VLDL32T(complete, symbol, huffman_tables); |
| 107 | + AE_VLDL16C(p_stream); |
| 108 | + } |
| 109 | + |
| 110 | + *buffer++ = symbol; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +template void XtensaDecodeStateHuffman::Decompress32BitTable_Xtensa<int8_t>( |
| 115 | + int8_t*); |
| 116 | +template void XtensaDecodeStateHuffman::Decompress32BitTable_Xtensa<int16_t>( |
| 117 | + int16_t*); |
| 118 | + |
| 119 | +} // namespace tflite |
0 commit comments