• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #define LOG_TAG "Operations"
18 
19 #include "Pow.h"
20 #include "HalInterfaces.h"
21 #include "IndexedShapeWrapper.h"
22 #include "OperationsUtils.h"
23 
24 #include <cmath>
25 
26 namespace android {
27 namespace nn {
28 namespace pow {
29 
30 namespace {
31 
32 using namespace hal;
33 
34 template <typename T>
evalGeneric(const T * baseData,const Shape & baseShape,const T * exponentData,const Shape & exponentShape,T * outputData,const Shape & outputShape)35 bool evalGeneric(const T* baseData, const Shape& baseShape, const T* exponentData,
36                  const Shape& exponentShape, T* outputData, const Shape& outputShape) {
37     IndexedShapeWrapper baseShapeIndexed(baseShape);
38     IndexedShapeWrapper exponentShapeIndexed(exponentShape);
39     IndexedShapeWrapper outputShapeIndexed(outputShape);
40 
41     std::vector<uint32_t> curIndex(outputShape.dimensions.size(), 0);
42     bool lastIndex = false;
43     do {
44         uint32_t outputFlatIndex;
45         NN_CHECK(outputShapeIndexed.indexToFlatIndex(curIndex, &outputFlatIndex));
46         uint32_t baseFlatIndex;
47         NN_CHECK(baseShapeIndexed.broadcastedIndexToFlatIndex(curIndex, &baseFlatIndex));
48         uint32_t exponentFlatIndex;
49         NN_CHECK(exponentShapeIndexed.broadcastedIndexToFlatIndex(curIndex, &exponentFlatIndex));
50 
51         outputData[outputFlatIndex] = std::pow(static_cast<float>(baseData[baseFlatIndex]),
52                                                static_cast<float>(exponentData[exponentFlatIndex]));
53 
54         NN_CHECK(outputShapeIndexed.nextIndexInplace(&curIndex, &lastIndex));
55     } while (!lastIndex);
56 
57     return true;
58 }
59 
60 }  // namespace
61 
prepare(const Shape & baseShape,const Shape & exponentShape,Shape * output)62 bool prepare(const Shape& baseShape, const Shape& exponentShape, Shape* output) {
63     NN_OPS_CHECK(baseShape.type == exponentShape.type);
64     if (SameShape(baseShape, exponentShape)) {
65         return SetShape(baseShape, output);
66     }
67     return calculateBroadcastedShape(baseShape, exponentShape, output);
68 }
69 
eval(const void * baseData,const Shape & baseShape,const void * exponentData,const Shape & exponentShape,void * outputData,const Shape & outputShape)70 bool eval(const void* baseData, const Shape& baseShape, const void* exponentData,
71           const Shape& exponentShape, void* outputData, const Shape& outputShape) {
72     switch (baseShape.type) {
73         case OperandType::TENSOR_FLOAT16: {
74             return evalGeneric(reinterpret_cast<const _Float16*>(baseData), baseShape,
75                                reinterpret_cast<const _Float16*>(exponentData), exponentShape,
76                                reinterpret_cast<_Float16*>(outputData), outputShape);
77         } break;
78         case OperandType::TENSOR_FLOAT32: {
79             return evalGeneric(reinterpret_cast<const float*>(baseData), baseShape,
80                                reinterpret_cast<const float*>(exponentData), exponentShape,
81                                reinterpret_cast<float*>(outputData), outputShape);
82         } break;
83         default: {
84             LOG(ERROR) << "Unsupported data type: " << toString(baseShape.type);
85             return false;
86         }
87     }
88 }
89 
90 }  // namespace pow
91 }  // namespace nn
92 }  // namespace android
93