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 "Cast.h"
20 
21 #include <algorithm>
22 
23 #include "Operations.h"
24 #include "Tracing.h"
25 
26 namespace android {
27 namespace nn {
28 namespace cast {
29 
30 namespace {
31 
32 template <typename FromT, typename ToT>
copyCast(const FromT * in,ToT * out,int numElements)33 void copyCast(const FromT* in, ToT* out, int numElements) {
34     std::transform(in, in + numElements, out, [](FromT a) -> ToT {
35         if constexpr (std::is_same_v<ToT, uint8_t>) {
36             if (a < 0) return 0;
37             if (a > 255) return 255;
38         }
39         return static_cast<ToT>(a);
40     });
41 }
42 
43 template <typename FromT>
copyToTensor(const FromT * inputData,int numElements,uint8_t * outputData,const Shape & outputShape)44 bool copyToTensor(const FromT* inputData, int numElements, uint8_t* outputData,
45                   const Shape& outputShape) {
46 #define ANDROID_NN_COPY_CAST(operandType, dataType)                                \
47     case operandType: {                                                            \
48         NNTRACE_COMP("cast::copyCast::" #dataType);                                \
49         copyCast(inputData, reinterpret_cast<dataType*>(outputData), numElements); \
50         return true;                                                               \
51     }
52 
53     switch (outputShape.type) {
54         ANDROID_NN_COPY_CAST(OperandType::TENSOR_FLOAT16, _Float16);
55         ANDROID_NN_COPY_CAST(OperandType::TENSOR_FLOAT32, float);
56         ANDROID_NN_COPY_CAST(OperandType::TENSOR_INT32, int32_t);
57         ANDROID_NN_COPY_CAST(OperandType::TENSOR_QUANT8_ASYMM, uint8_t);
58         default:
59             LOG(ERROR) << "Unsupported CAST output type";
60             return false;
61     }
62 #undef ANDROID_NN_COPY_CAST
63 }
64 
65 }  // namespace
66 
prepare(const Shape & input,Shape * output)67 bool prepare(const Shape& input, Shape* output) {
68     output->dimensions = input.dimensions;
69     return true;
70 }
71 
eval(const uint8_t * inputData,const Shape & inputShape,uint8_t * outputData,const Shape & outputShape)72 bool eval(const uint8_t* inputData, const Shape& inputShape, uint8_t* outputData,
73           const Shape& outputShape) {
74     NNTRACE_TRANS("cast::eval");
75     int numElements = getNumberOfElements(inputShape);
76 
77 #define ANDROID_NN_COPY_TO_TENSOR(operandType, dataType)                                    \
78     case operandType: {                                                                     \
79         NNTRACE_TRANS("cast::copyToTensor::" #dataType);                                    \
80         copyToTensor(reinterpret_cast<const dataType*>(inputData), numElements, outputData, \
81                      outputShape);                                                          \
82         return true;                                                                        \
83     }
84 
85     switch (inputShape.type) {
86         ANDROID_NN_COPY_TO_TENSOR(OperandType::TENSOR_FLOAT16, _Float16);
87         ANDROID_NN_COPY_TO_TENSOR(OperandType::TENSOR_FLOAT32, float);
88         ANDROID_NN_COPY_TO_TENSOR(OperandType::TENSOR_INT32, int32_t);
89         ANDROID_NN_COPY_TO_TENSOR(OperandType::TENSOR_QUANT8_ASYMM, uint8_t);
90         default:
91             if (inputShape.type == outputShape.type) {
92                 return copyData(inputData, inputShape, outputData, outputShape);
93             } else {
94                 LOG(ERROR) << "Unsupported CAST input type";
95                 return false;
96             }
97     }
98 #undef ANDROID_NN_COPY_TO_TENSOR
99 }
100 
101 }  // namespace cast
102 }  // namespace nn
103 }  // namespace android
104