-
Notifications
You must be signed in to change notification settings - Fork 944
Support for DECODE operator #3132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ddavis-2015
wants to merge
5
commits into
tensorflow:main
Choose a base branch
from
ddavis-2015:decode-testing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,370
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ac4bec3
Support for DECODE operator
ddavis-2015 15ac156
update copyright
ddavis-2015 6f96b29
Don't use constructors with global objects (bluepill will not call th…
ddavis-2015 d126d50
Merge branch 'main' into decode-testing
ddavis-2015 7ddd84b
return error if DecodeState cannot be created.
ddavis-2015 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* Copyright 2025 The TensorFlow Authors. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#include "tensorflow/lite/c/common.h" | ||
#include "tensorflow/lite/kernels/internal/compatibility.h" | ||
#include "tensorflow/lite/kernels/kernel_util.h" | ||
#include "tensorflow/lite/micro/kernels/decode_state.h" | ||
#include "tensorflow/lite/micro/kernels/kernel_util.h" | ||
#include "tensorflow/lite/micro/micro_context.h" | ||
#include "tensorflow/lite/micro/micro_log.h" | ||
|
||
namespace tflite { | ||
namespace { | ||
|
||
TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { | ||
const size_t num_inputs = NumInputs(node); | ||
const size_t num_outputs = NumOutputs(node); | ||
TF_LITE_ENSURE(context, num_outputs > 0); | ||
TF_LITE_ENSURE_EQ(context, num_inputs, num_outputs * 2); | ||
|
||
MicroContext* const micro_context = GetMicroContext(context); | ||
|
||
node->user_data = micro_context->AllocatePersistentBuffer( | ||
num_outputs * sizeof(DecodeState*)); | ||
TF_LITE_ENSURE(context, node->user_data != nullptr); | ||
DecodeState** const dsp_arr = | ||
reinterpret_cast<DecodeState**>(node->user_data); | ||
|
||
TfLiteTensor* input = nullptr; | ||
TfLiteTensor* ancillary = nullptr; | ||
TfLiteTensor* output = nullptr; | ||
TfLiteStatus status = kTfLiteOk; | ||
|
||
for (size_t i = 0; i < num_inputs; i += 2) { | ||
input = micro_context->AllocateTempInputTensor(node, i); | ||
if (input == nullptr) { | ||
MicroPrintf("failed to allocate input tensor %u", i); | ||
status = kTfLiteError; | ||
break; | ||
} | ||
ancillary = micro_context->AllocateTempInputTensor(node, i + 1); | ||
if (ancillary == nullptr) { | ||
MicroPrintf("failed to allocate ancillary tensor %u", i + 1); | ||
status = kTfLiteError; | ||
break; | ||
} | ||
output = micro_context->AllocateTempOutputTensor(node, i / 2); | ||
if (output == nullptr) { | ||
MicroPrintf("failed to allocate output tensor %u", i / 2); | ||
status = kTfLiteError; | ||
break; | ||
} | ||
|
||
if (DecodeState::Version(*ancillary) != 1) { | ||
MicroPrintf("version %u != 1", DecodeState::Version(*ancillary)); | ||
status = kTfLiteError; | ||
break; | ||
} | ||
|
||
DecodeState* dsp = nullptr; | ||
switch (DecodeState::Type(*ancillary)) { | ||
case DecodeState::kDcmTypeLUT: | ||
dsp = DecodeState::CreateDecodeStateLUT( | ||
context, micro_context->GetAlternateProfiler()); | ||
break; | ||
case DecodeState::kDcmTypeCustom: | ||
MicroPrintf("Custom decode type not yet supported"); | ||
break; | ||
default: | ||
MicroPrintf("unsupported decode type %u", | ||
DecodeState::Type(*ancillary)); | ||
break; | ||
} | ||
|
||
if (dsp != nullptr) { | ||
status = dsp->Setup(*input, *ancillary, *output); | ||
if (status != kTfLiteOk) { | ||
break; | ||
} | ||
dsp_arr[i / 2] = dsp; | ||
} else { | ||
MicroPrintf("failed to allocate DecodeState[%u]", i / 2); | ||
status = kTfLiteError; | ||
break; | ||
} | ||
|
||
micro_context->DeallocateTempTfLiteTensor(input); | ||
micro_context->DeallocateTempTfLiteTensor(ancillary); | ||
micro_context->DeallocateTempTfLiteTensor(output); | ||
input = nullptr; | ||
ancillary = nullptr; | ||
output = nullptr; | ||
} | ||
|
||
if (input != nullptr) { | ||
micro_context->DeallocateTempTfLiteTensor(input); | ||
} | ||
if (ancillary != nullptr) { | ||
micro_context->DeallocateTempTfLiteTensor(ancillary); | ||
} | ||
if (output != nullptr) { | ||
micro_context->DeallocateTempTfLiteTensor(output); | ||
} | ||
|
||
return status; | ||
} | ||
|
||
TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { | ||
const size_t num_inputs = NumInputs(node); | ||
DecodeState** const dsp_arr = | ||
reinterpret_cast<DecodeState**>(node->user_data); | ||
|
||
for (size_t i = 0; i < num_inputs; i += 2) { | ||
const TfLiteEvalTensor* input = | ||
tflite::micro::GetEvalInput(context, node, i); | ||
TF_LITE_ENSURE(context, input != nullptr); | ||
const TfLiteEvalTensor* ancillary = | ||
tflite::micro::GetEvalInput(context, node, i + 1); | ||
TF_LITE_ENSURE(context, ancillary != nullptr); | ||
const TfLiteEvalTensor* output = | ||
tflite::micro::GetEvalOutput(context, node, i / 2); | ||
TF_LITE_ENSURE(context, output != nullptr); | ||
|
||
TfLiteStatus status = dsp_arr[i / 2]->Decode(*input, *ancillary, *output); | ||
TF_LITE_ENSURE(context, status == kTfLiteOk); | ||
} | ||
|
||
return kTfLiteOk; | ||
} | ||
|
||
} // namespace | ||
|
||
TFLMRegistration Register_DECODE() { | ||
return tflite::micro::RegisterOp(nullptr, Prepare, Eval); | ||
} | ||
|
||
} // namespace tflite |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* Copyright 2025 The TensorFlow Authors. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#include "tensorflow/lite/micro/kernels/decode_state.h" | ||
|
||
#include "tensorflow/lite/micro/kernels/decode_state_lut.h" | ||
#include "tensorflow/lite/micro/micro_context.h" | ||
|
||
namespace tflite { | ||
|
||
DecodeState* DecodeState::CreateDecodeStateLUT( | ||
const TfLiteContext* context, MicroProfilerInterface* profiler) { | ||
MicroContext* const micro_context = GetMicroContext(context); | ||
void* buffer = | ||
micro_context->AllocatePersistentBuffer(sizeof(DecodeStateLUT)); | ||
if (buffer == nullptr) { | ||
return nullptr; | ||
} | ||
DecodeState* dsp = new (buffer) DecodeStateLUT(context, profiler); | ||
|
||
return dsp; | ||
} | ||
|
||
} // namespace tflite |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* Copyright 2025 The TensorFlow Authors. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#ifndef TENSORFLOW_LITE_MICRO_MICRO_KERNELS_DECODE_STATE_H_ | ||
#define TENSORFLOW_LITE_MICRO_MICRO_KERNELS_DECODE_STATE_H_ | ||
|
||
#include <cstdint> | ||
|
||
#include "tensorflow/lite/c/common.h" | ||
#include "tensorflow/lite/core/c/c_api_types.h" | ||
#include "tensorflow/lite/kernels/kernel_util.h" | ||
#include "tensorflow/lite/micro/compatibility.h" | ||
#include "tensorflow/lite/micro/kernels/kernel_util.h" | ||
#include "tensorflow/lite/micro/micro_profiler_interface.h" | ||
|
||
namespace tflite { | ||
|
||
struct DecodeState { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change this to class per https://google.github.io/styleguide/cppguide.html#Structs_vs._Classes |
||
DecodeState() = delete; | ||
|
||
DecodeState(const TfLiteContext* context, MicroProfilerInterface* profiler) | ||
: context_(context), micro_profiler_(profiler) {} | ||
|
||
virtual TfLiteStatus Setup(const TfLiteTensor& input, | ||
const TfLiteTensor& ancillary, | ||
const TfLiteTensor& output) = 0; | ||
virtual TfLiteStatus Decode(const TfLiteEvalTensor& input, | ||
const TfLiteEvalTensor& ancillary, | ||
const TfLiteEvalTensor& output) = 0; | ||
|
||
static DecodeState* CreateDecodeStateLUT(const TfLiteContext* context, | ||
MicroProfilerInterface* profiler); | ||
|
||
static uint8_t Type(const TfLiteTensor& ancillary) { | ||
return GetTensorData<uint8_t>(&ancillary)[kDcmDecodeTypeOffset]; | ||
} | ||
|
||
static uint8_t Type(const TfLiteEvalTensor& ancillary) { | ||
return micro::GetTensorData<uint8_t>(&ancillary)[kDcmDecodeTypeOffset]; | ||
} | ||
|
||
static uint8_t Version(const TfLiteTensor& ancillary) { | ||
return GetTensorData<uint8_t>(&ancillary)[kDcmVersionOffset]; | ||
} | ||
|
||
static uint8_t Version(const TfLiteEvalTensor& ancillary) { | ||
return micro::GetTensorData<uint8_t>(&ancillary)[kDcmVersionOffset]; | ||
} | ||
|
||
protected: | ||
virtual ~DecodeState() = default; | ||
|
||
// Decode Common Metadata constants | ||
public: | ||
static constexpr uint8_t kDcmTypeLUT = 0; | ||
static constexpr uint8_t kDcmTypeCustom = 127; | ||
|
||
static constexpr size_t kDcmSizeInBytes = 16; | ||
|
||
private: | ||
static constexpr size_t kDcmDecodeTypeOffset = 0; | ||
static constexpr size_t kDcmVersionOffset = 1; | ||
|
||
// DecodeState vars | ||
protected: | ||
const TfLiteContext* context_; | ||
MicroProfilerInterface* micro_profiler_; | ||
|
||
private: | ||
TF_LITE_REMOVE_VIRTUAL_DELETE | ||
}; | ||
|
||
} // namespace tflite | ||
|
||
#endif // TENSORFLOW_LITE_MICRO_MICRO_KERNELS_DECODE_STATE_H_ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about converting DecodeState into an interface called IDecodeAlgorithm? This would clarify its role, and we could then reorganize the helper functions into a separate container class.