ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Add.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright (C) 2017 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include "Add.h"
19#include "Assert.h"
20
21bool addPrepare(const Shape &in1, const Shape &in2, Shape *out)
22{
24 ASSERT(in1.type == in2.type);
25 if (SameShape(in1, in2))
26 {
27 return SetShape(in1, out);
28 }
29 else
30 {
31 // BroadcastAdd needed
32 uint32_t numberOfDims1 = getNumberOfDimensions(in1);
33 uint32_t numberOfDims2 = getNumberOfDimensions(in2);
34 uint32_t maxDims = std::max(numberOfDims1, numberOfDims2);
35 out->dimensions = std::vector<uint32_t>(maxDims);
36 for (uint32_t i = 1; i <= maxDims; i++)
37 {
38 uint32_t dim1 = 1;
39 if (i <= numberOfDims1)
40 {
41 dim1 = getSizeOfDimension(in1, numberOfDims1 - i);
42 }
43 uint32_t dim2 = 1;
44 if (i <= numberOfDims2)
45 {
46 dim2 = getSizeOfDimension(in2, numberOfDims2 - i);
47 }
48 if (dim1 != dim2 && dim1 != 1 && dim2 != 1)
49 {
50 LOG(ERROR) << "Dimensions mismatch for BroadcastAdd";
51 return false;
52 }
53 out->dimensions[maxDims - i] = std::max(dim1, dim2);
54 }
55 }
56 return true;
57}
#define ASSERT(v)
Definition Assert.h:24
#define LOG(...)
Definition Logging.h:36
bool SetShape(const Shape &in, Shape *out)
Definition Shape.cpp:38
uint32_t getSizeOfDimension(const Shape &shape, uint32_t dimensionIdx)
Definition Shape.cpp:60
bool SameShape(const Shape &in1, const Shape &in2)
Definition Shape.cpp:22
uint32_t getNumberOfDimensions(const Shape &shape)
Definition Shape.cpp:58
bool addPrepare(const Shape &in1, const Shape &in2, Shape *out)
Definition Add.cpp:21
Definition Shape.h:28
OperandType type
Definition Shape.h:29
std::vector< uint32_t > dimensions
Definition Shape.h:30