-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[CMSIS-NN] Support for Softmax Int16 operator #15407
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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. | ||
| */ | ||
| /*! | ||
| * \file src/relay/backend/contrib/cmsisnn/compute_luts.cc | ||
| * \brief Creates LUTs for operators in different bit formats for accelerating computations. | ||
| */ | ||
|
|
||
| #include "compute_luts.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cmath> | ||
| #include <limits> | ||
|
|
||
| namespace tvm { | ||
| namespace relay { | ||
| namespace contrib { | ||
| namespace cmsisnn { | ||
|
|
||
| void CalculateLUTInt16(int key_zero_point, float key_scale, int value_zero_point, float value_scale, | ||
| float (*func)(float), const int steps, int16_t* lut) { | ||
| const float value_min = static_cast<float>(std::numeric_limits<int16_t>::min()); | ||
| const float value_max = static_cast<float>(std::numeric_limits<int16_t>::max()); | ||
| const float key_min_deq = key_scale * (std::numeric_limits<int16_t>::min() - key_zero_point); | ||
| const float key_max_deq = key_scale * (std::numeric_limits<int16_t>::max() - key_zero_point); | ||
| const float value_min_deq = | ||
| value_scale * (std::numeric_limits<int16_t>::min() - value_zero_point); | ||
| const float value_max_deq = | ||
| value_scale * (std::numeric_limits<int16_t>::max() - value_zero_point); | ||
|
|
||
| const float step_size_deq = (key_max_deq - key_min_deq) / (steps - 1); | ||
| const float half_step_size_deq = step_size_deq / 2; | ||
|
|
||
| const float value_inv_quantizing = | ||
| (std::numeric_limits<int16_t>::max() - std::numeric_limits<int16_t>::min() + 1) / | ||
| (value_max_deq - value_min_deq); | ||
|
|
||
| for (int i = 0; i < steps - 1; i++) { | ||
| float value_deq = func(key_min_deq + i * step_size_deq); | ||
| float mid_value_deq = func(key_min_deq + i * step_size_deq + half_step_size_deq); | ||
| float next_value_deq = func(key_min_deq + (i + 1) * step_size_deq); | ||
|
|
||
| float value = std::round(value_deq * value_inv_quantizing); | ||
| float mid_value = std::round(mid_value_deq * value_inv_quantizing); | ||
| float next_value = std::round(next_value_deq * value_inv_quantizing); | ||
| float mid_iterp_value = std::round((value + next_value) / 2); | ||
|
|
||
| float mid_err = mid_iterp_value - mid_value; | ||
| float bias = std::round(mid_err / 2); | ||
|
|
||
| lut[i] = static_cast<int16_t>(std::max(std::min(value - bias, value_max), value_min)); | ||
| } | ||
|
|
||
| lut[steps - 1] = static_cast<int16_t>( | ||
| std::max(std::min(func(value_max_deq) * value_inv_quantizing, value_max), value_min)); | ||
| } | ||
|
|
||
| } // namespace cmsisnn | ||
| } // namespace contrib | ||
| } // namespace relay | ||
| } // namespace tvm | ||
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,55 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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. | ||
| */ | ||
|
|
||
| /*! | ||
| * \file src/relay/backend/contrib/cmsisnn/compute_luts.h | ||
| * \brief CMSIS-NN LUTs calculation functions | ||
| */ | ||
|
|
||
| #ifndef TVM_RELAY_BACKEND_CONTRIB_CMSISNN_COMPUTE_LUTS_H_ | ||
| #define TVM_RELAY_BACKEND_CONTRIB_CMSISNN_COMPUTE_LUTS_H_ | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| namespace tvm { | ||
| namespace relay { | ||
| namespace contrib { | ||
| namespace cmsisnn { | ||
|
|
||
| /*! | ||
| * \brief Populates an int16 LUT based on the quantization parameters of its keys, values and | ||
| * respective transformation function | ||
| * | ||
| * \param key_zero_point - zero point of table's keys | ||
| * \param key_scale - scale of the table's keys | ||
| * \param value_zero_point - zero point of table's values | ||
| * \param value_scale - scale of the table's values | ||
| * \param func - function pointer of the transformation performed by the LUT | ||
| * \param steps - number of total values inside the table | ||
| * \param lut - int16_t array storing the values of the LUT | ||
| */ | ||
| void CalculateLUTInt16(int key_zero_point, float key_scale, int value_zero_point, float value_scale, | ||
| float (*func)(float), const int steps, int16_t* lut); | ||
|
|
||
| } // namespace cmsisnn | ||
| } // namespace contrib | ||
| } // namespace relay | ||
| } // namespace tvm | ||
|
|
||
| #endif // TVM_RELAY_BACKEND_CONTRIB_CMSISNN_COMPUTE_LUTS_H_ |
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
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.
Uh oh!
There was an error while loading. Please reload this page.