Cast.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (C) 2018 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define LOG_TAG "Operations"
  17. #include "Cast.h"
  18. #include "Tracing.h"
  19. namespace android {
  20. namespace nn {
  21. namespace cast {
  22. namespace {
  23. template <typename FromT, typename ToT>
  24. void copyCast(const FromT* in, ToT* out, int numElements) {
  25. std::transform(in, in + numElements, out, [](FromT a) -> ToT {
  26. if constexpr (std::is_same_v<ToT, uint8_t>) {
  27. if (a < 0) return 0;
  28. if (a > 255) return 255;
  29. }
  30. return static_cast<ToT>(a);
  31. });
  32. }
  33. template <typename FromT>
  34. bool copyToTensor(const FromT* inputData, int numElements, uint8_t* outputData,
  35. const Shape& outputShape) {
  36. #define ANDROID_NN_COPY_CAST(operandType, dataType) \
  37. case operandType: { \
  38. NNTRACE_COMP("cast::copyCast::" #dataType); \
  39. copyCast(inputData, reinterpret_cast<dataType*>(outputData), numElements); \
  40. return true; \
  41. }
  42. switch (outputShape.type) {
  43. ANDROID_NN_COPY_CAST(OperandType::TENSOR_FLOAT16, _Float16);
  44. ANDROID_NN_COPY_CAST(OperandType::TENSOR_FLOAT32, float);
  45. ANDROID_NN_COPY_CAST(OperandType::TENSOR_INT32, int32_t);
  46. ANDROID_NN_COPY_CAST(OperandType::TENSOR_QUANT8_ASYMM, uint8_t);
  47. default:
  48. LOG(ERROR) << "Unsupported CAST output type";
  49. return false;
  50. }
  51. #undef ANDROID_NN_COPY_CAST
  52. }
  53. } // namespace
  54. bool prepare(const Shape& input, Shape* output) {
  55. if (input.dimensions.size() != output->dimensions.size()) {
  56. return false;
  57. }
  58. output->dimensions = input.dimensions;
  59. return true;
  60. }
  61. bool eval(const uint8_t* inputData, const Shape& inputShape, uint8_t* outputData,
  62. const Shape& outputShape) {
  63. NNTRACE_TRANS("cast::eval");
  64. int numElements = getNumberOfElements(inputShape);
  65. #define ANDROID_NN_COPY_TO_TENSOR(operandType, dataType) \
  66. case operandType: { \
  67. NNTRACE_TRANS("cast::copyToTensor::" #dataType); \
  68. copyToTensor(reinterpret_cast<const dataType*>(inputData), numElements, outputData, \
  69. outputShape); \
  70. return true; \
  71. }
  72. switch (inputShape.type) {
  73. ANDROID_NN_COPY_TO_TENSOR(OperandType::TENSOR_FLOAT16, _Float16);
  74. ANDROID_NN_COPY_TO_TENSOR(OperandType::TENSOR_FLOAT32, float);
  75. ANDROID_NN_COPY_TO_TENSOR(OperandType::TENSOR_INT32, int32_t);
  76. ANDROID_NN_COPY_TO_TENSOR(OperandType::TENSOR_QUANT8_ASYMM, uint8_t);
  77. default:
  78. LOG(ERROR) << "Unsupported CAST input type";
  79. return false;
  80. }
  81. #undef ANDROID_NN_COPY_TO_TENSOR
  82. }
  83. } // namespace cast
  84. } // namespace nn
  85. } // namespace android