LogSoftmax.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "HalInterfaces.h"
  18. #include "OperationResolver.h"
  19. #include "OperationsUtils.h"
  20. #include "Tracing.h"
  21. #include <cmath>
  22. namespace android {
  23. namespace nn {
  24. namespace log_softmax {
  25. constexpr char kOperationName[] = "LOG_SOFTMAX";
  26. constexpr uint32_t kNumInputs = 3;
  27. constexpr uint32_t kInputTensor = 0;
  28. constexpr uint32_t kInputBeta = 1;
  29. constexpr uint32_t kInputAxis = 2;
  30. constexpr uint32_t kNumOutputs = 1;
  31. constexpr uint32_t kOutputTensor = 0;
  32. template <typename T>
  33. inline bool compute(const T* input, const Shape& shape, T beta, uint32_t axis, T* output) {
  34. const uint32_t outerSize = getNumberOfElements(shape, 0, axis);
  35. const uint32_t axisSize = getSizeOfDimension(shape, axis);
  36. const uint32_t innerSize = getNumberOfElements(shape, axis + 1, getNumberOfDimensions(shape));
  37. for (uint32_t outer = 0; outer < outerSize; ++outer) {
  38. for (uint32_t inner = 0; inner < innerSize; ++inner) {
  39. // We subtract the maximum value from each element to ensure
  40. // numerical stability, taking advantage of the following equality:
  41. // exp(x[i])/sum(exp(x[i])) == exp(x[i]+C)/sum(exp(x[i]+C))
  42. T maxValue = input[outer * axisSize * innerSize + inner];
  43. for (uint32_t i = 1; i < axisSize; ++i) {
  44. maxValue = std::max(maxValue, input[(outer * axisSize + i) * innerSize + inner]);
  45. }
  46. T sum = 0;
  47. for (uint32_t i = 0; i < axisSize; ++i) {
  48. sum += std::exp(static_cast<double>(
  49. (input[(outer * axisSize + i) * innerSize + inner] - maxValue) * beta));
  50. }
  51. const T logSum = std::log(static_cast<double>(sum));
  52. for (uint32_t i = 0; i < axisSize; ++i) {
  53. output[(outer * axisSize + i) * innerSize + inner] =
  54. (input[(outer * axisSize + i) * innerSize + inner] - maxValue) * beta -
  55. logSum;
  56. }
  57. }
  58. }
  59. return true;
  60. }
  61. bool validate(const IOperationValidationContext* context) {
  62. NN_RET_CHECK_EQ(context->getNumInputs(), kNumInputs);
  63. NN_RET_CHECK_EQ(context->getNumOutputs(), kNumOutputs);
  64. OperandType inputType = context->getInputType(kInputTensor);
  65. std::vector<OperandType> inExpectedTypes;
  66. std::vector<OperandType> outExpectedTypes;
  67. if (inputType == OperandType::TENSOR_FLOAT32) {
  68. inExpectedTypes = {OperandType::TENSOR_FLOAT32, OperandType::FLOAT32, OperandType::INT32};
  69. outExpectedTypes = {OperandType::TENSOR_FLOAT32};
  70. } else if (inputType == OperandType::TENSOR_FLOAT16) {
  71. inExpectedTypes = {OperandType::TENSOR_FLOAT16, OperandType::FLOAT16, OperandType::INT32};
  72. outExpectedTypes = {OperandType::TENSOR_FLOAT16};
  73. } else {
  74. LOG(ERROR) << "Unsupported input tensor type for operation " << kOperationName;
  75. return false;
  76. }
  77. NN_RET_CHECK(validateInputTypes(context, inExpectedTypes));
  78. NN_RET_CHECK(validateOutputTypes(context, outExpectedTypes));
  79. return validateHalVersion(context, HalVersion::V1_2);
  80. }
  81. bool prepare(IOperationExecutionContext* context) {
  82. return context->setOutputShape(kOutputTensor, context->getInputShape(kInputTensor));
  83. }
  84. bool execute(IOperationExecutionContext* context) {
  85. int32_t axis = context->getInputValue<int32_t>(kInputAxis);
  86. NN_RET_CHECK(handleNegativeAxis(context->getInputShape(kInputTensor), &axis));
  87. switch (context->getInputType(kInputTensor)) {
  88. case OperandType::TENSOR_FLOAT16:
  89. return compute(context->getInputBuffer<_Float16>(kInputTensor),
  90. context->getInputShape(kInputTensor),
  91. context->getInputValue<_Float16>(kInputBeta), axis,
  92. context->getOutputBuffer<_Float16>(kOutputTensor));
  93. case OperandType::TENSOR_FLOAT32:
  94. return compute(context->getInputBuffer<float>(kInputTensor),
  95. context->getInputShape(kInputTensor),
  96. context->getInputValue<float>(kInputBeta), axis,
  97. context->getOutputBuffer<float>(kOutputTensor));
  98. default:
  99. NN_RET_CHECK_FAIL() << "Unsupported tensor type for operation " << kOperationName;
  100. }
  101. }
  102. } // namespace log_softmax
  103. NN_REGISTER_OPERATION(LOG_SOFTMAX, log_softmax::kOperationName, log_softmax::validate,
  104. log_softmax::prepare, log_softmax::execute);
  105. } // namespace nn
  106. } // namespace android