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