-
Notifications
You must be signed in to change notification settings - Fork 74k
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
Missing TRANSPOSE Op Kernel #43472
Comments
@victorromeo |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you. |
Bump, as I'm still authoring a TRANSPOSE operation for the micro interpreter. |
As informed above we do not face any errors in the code shared, please confirm and if issue exist share a colab gist with the error or move the issue to closed status if resolved.Thanks! |
This is an issue of a missing C++ micro kernel operation, not a core kernel operation and as such is not appropriate for a python colab gist. As mentioned earlier, I’m working on this as a custom c++ operation. Does Colab support C++11 with Bezal compilation? A Keras TF2 model, when converted to TFLite micro, includes an operation which is not yet supported called TRANSPOSE. This already exists as a TFLite operation (But when used on an ARM microcontroller, this is simply not available). |
@jdduke May I please get some pointers if possible to confirm the approach for an appropriate Transpose operation? There are guides for TFLite custom operations, however not for missing micro operations or custom micro operations. I'm planning on reusing the TransposeContext, from Transpose.cc but dropping the KernelType struct TransposeContext {
TransposeContext(TfLiteContext* context, TfLiteNode* node) {
input = GetInput(context, node, 0);
perm = GetInput(context, node, 1);
output = GetOutput(context, node, 0);
}
const TfLiteTensor* input;
const TfLiteTensor* perm;
TfLiteTensor* output;
}; I'm thinking of basing the micro implementation off of ComputePermutation in tfl_ops.cc // Computes the permutation of a constant `input_tensor` according to `perm`.
// The function recursively traverses the dimensions of the output tensor in
// a row-major order and writes the value in the output tensor into
// `new_values`.
void ComputePermutation(ElementsAttr input_tensor, ArrayRef<int32_t> perm,
ArrayRef<int64_t> output_shape, int num_dimensions,
int output_axis, std::vector<uint64_t> *input_indices,
std::vector<Attribute> *new_values) {
// Refer to the implementation of `Transpose` function in
// tensorflow/lite/kernels/internal/reference/reference_ops.h
assert(output_axis < num_dimensions);
const int input_axis = perm[output_axis];
for (int i = 0; i < output_shape[output_axis]; ++i) {
// Update the input indices on `input_axis`.
input_indices->at(input_axis) = i;
// Write the value from `input_tensor` if it is the last axis or
// recurse into the next axis.
const bool is_last_axis = output_axis == num_dimensions - 1;
if (is_last_axis) {
new_values->push_back(input_tensor.getValue(*input_indices));
} else {
ComputePermutation(input_tensor, perm, output_shape, num_dimensions,
output_axis + 1, input_indices, new_values);
}
}
} Finally, I'm hoping that the current /lite/transpose_test.cc are appropriate for reuse, with generic implementations but specific tests for int8 quantization. Here's my work in progress. |
@advaitjain or @petewarden can you advise on the recommended process for porting a Lite kernel to Micro? |
@victorromeo, as you have found out, TFLM supports a subset of the TfLite ops. We will be adding a guide for porting from Lite to as there are subtleties around not having dynamic memory allocation as well as other differences between Lite and Micro. I would recommend to start by sharing more details about the actual model that you are trying to use, based on our contribution guidelines to help motivate the need for this additional Op in Micro. We can then give you some specific pointers on how to proceed. One example of an Op (along with motivation for why it is worth adding to Micro at this time) that is currently being ported via community contributions is PR #43384 and #43381 |
Thanks @advaitjain The model is currently being used to prepare an audio classifier, which fits onto a micro-controller. The Transpose operation has been shown to be a valuable kernel layer toward a greater goal of effectively switching from time domain to frequency domain and back. As the model is currently being developed for research papers, I have been advised I am unable to share the entire architecture. The architecture doesn't use FFT preprocessing. The origin of the issue is due to the Keras.Permute operation being converted into TFLite.Transpose. I appreciate it is hard to fully appreciate the benefit of adding the operation, without evidence, but this will be in the paper. The model is planned to run on Cortex M4 devices such as the moderately sized Arduino Nano 33 BLE Sense. Side note, it would be great to see a flag to check compatibility of builtin micro operations for use during TFLite conversion, as I had to circle a while to deduce that Transpose was the issue. |
Thanks for the additional context, I understand that you're not able to share model architecture pre-publication. Let's wait for around a week for me to put together a guide for porting ops from lite to micro and I'll link to it from this issue as well. In the meantime, if you want to send a PR with flatbuffer changes for transpose (similar to #43384), that would be great. Since the transpose op does not have any BuilinOptions, the parsing function will be something along the lines of tensorflow/tensorflow/lite/core/api/flatbuffer_conversions.cc Lines 1124 to 1130 in e0ed4b4
and tensorflow/tensorflow/lite/core/api/flatbuffer_conversions.cc Lines 212 to 214 in e0ed4b4
|
Thanks for your additional feedback. I've further implemented all compatible unit tests for Transpose in my fork here and these are now compiling. Its worth noting my implemetnation is not optimal, but does achieve 1D-4D transpose support. Acheived using a port of existing TFLite core Transpose, using micro frameworks and static arrays. Onto final stages of testing now. Cheers. |
@victorromeo |
The most stable implementation of Transpose is https://github.com/victorromeo/tensorflow/tree/v2.3.1_transpose I'm going to rebase this off master, then create a pull request for consideration from the team. |
I created a PR 48192 that should solve this issue. Thank you @victorromeo for your contribution. I refactor some of your code cherry-picking it, in a way your name appears. Having your name associated with the PR, Google now asks for both of our signature in their Contributor License Agreement. It would be really nice if you could quickly sign it so the PR will be pushed forward :) Thank you! |
We can see that the PR that you submitted has been merged. Can you kindly confirm if we can close this issue? Thanks! |
This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you. |
Closing as stale. Please reopen if you'd like to work on this further. |
System information
I'm attempting to use a TFLite converted model, which was created and trained using TF2 + Keras. The converter successfully created the TFLite file, and I've loaded it into a micro-controller app, as a flatbuffer cpp + h file.
I'm unable to share the model at this time due to confidentiality, however the model contains Conv2D, BatchNormalization, ReLu, MaxPooling2D, Permute, Dropout, Flatten, Dense and Softmax.
After conversion, the model is loaded into an Arduino sketch, but upon loading the model, an error is reported.
Given that this operation was chosen from the Builtin operation set, I believe this is a fault/bug. Can you please advise?
type registration
AllOps missing TRANSPOSE
MicroMutableOpResolver missing TRANSPOSE registration
Standalone code to reproduce the issue
Any other info / logs
The text was updated successfully, but these errors were encountered: