ONE - On-device Neural Engine
Loading...
Searching...
No Matches
FullyConnected.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 "FullyConnected.h"
19#include "Assert.h"
20
21#if 0
22#include "internal/Matrix.h"
23#include "internal/Fused.h"
24#include "internal/GEMM.h"
26#endif
27
28bool fullyConnectedPrepare(const Shape &input, const Shape &weights, const Shape &bias,
29 Shape *output)
30{
31 // Check all the parameters of tensor match within themselves and match the
32 // input configuration.
33 ASSERT(input.type == weights.type);
34 if (input.type == OperandType::TENSOR_QUANT8_ASYMM)
35 {
36 ASSERT(bias.type == OperandType::TENSOR_INT32);
37 }
38 else
39 {
40 ASSERT(input.type == bias.type);
41 }
42 ASSERT(getNumberOfDimensions(input) >= 2);
43 uint32_t input_size = getNumberOfElements(input);
44 uint32_t num_units = getSizeOfDimension(weights, 0);
45
46 // modified to resolve Coverity 118949 (Apr 25, 2018) by hyunsik.yoon
47 // Original Code:
48 // uint32_t batch_size = input_size / getSizeOfDimension(weights, 1);
49 //
50 // Coverity Detection: Division by zero
51 //
52 // Code below is modified code
53
54 uint32_t shape_size = getSizeOfDimension(weights, 1);
55 if (shape_size == 0)
56 {
57 return false;
58 }
59
60 uint32_t batch_size = input_size / shape_size;
61
62 ASSERT(getSizeOfDimension(bias, 0) == num_units);
63 ASSERT(getSizeOfDimension(weights, 1) * batch_size == input_size);
64 ASSERT(getNumberOfDimensions(weights) == 2);
65
66 output->type = input.type;
67 output->dimensions = {batch_size, num_units};
68
69 return true;
70}
#define ASSERT(v)
Definition Assert.h:24
uint32_t getNumberOfElements(const Shape &shape)
Definition Shape.cpp:48
uint32_t getSizeOfDimension(const Shape &shape, uint32_t dimensionIdx)
Definition Shape.cpp:60
uint32_t getNumberOfDimensions(const Shape &shape)
Definition Shape.cpp:58
bool fullyConnectedPrepare(const Shape &input, const Shape &weights, const Shape &bias, Shape *output)
Definition Shape.h:28
OperandType type
Definition Shape.h:29