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