summaryrefslogtreecommitdiff
path: root/libs/kernel/acl/src/FullyConnected.h
blob: 5030a85481fa025c3a516278f8697fd28ccbd813 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __NNFW_KERNEL_ACL_FULLYCONNECTED_COMMON_H__
#define __NNFW_KERNEL_ACL_FULLYCONNECTED_COMMON_H__

#include <OperationsUtils.h>
#include <arm_compute/core/TensorShape.h>
#include <arm_compute/core/TensorInfo.h>
#include <arm_compute/runtime/IFunction.h>

#include "shape.h"
#include "IO_accessor.h"

namespace nnfw {
namespace kernel {
namespace acl {

namespace common {

typedef std::function<void (void)> sync_scheduler_f;

template<class TensorT, class LayerT, class ActT>
bool fullyConnectedFloat32(const float* inputData, const nnfw::rt::Shape& inputShape,
                           const float* weightsData, const nnfw::rt::Shape& weightsShape,
                           const float* biasData, const nnfw::rt::Shape& biasShape,
                           int32_t activation,
                           float* outputData, const nnfw::rt::Shape& outputShape,
                           sync_scheduler_f sync_scheduler) {

  // NNAPI specification: https://developer.android.com/ndk/reference/group___neural_networks.html#ggaabbe492c60331b13038e39d4207940e0aaada7a3dbaf4676aba560c933ff610c5

  // According to the NNAPI Specification,
  // INPUT
  // 1. input rank is up to 4.
  // 2. if input rank > 2, it is flattened to rank 2 [batch_size, input_size]
  nnfw::rt::Shape flattenedInputShape = inputShape;
  switch(inputShape.dimensions.size()) {
  case 1:
    {
      assert("Need to be implemented." && 0);
      break;
    }
  case 2:
    {
      // DO NOTHING.
      break;
    }
  case 3:
    {
      assert("Need to be implemented." && 0);
      break;
    }
  case 4:
    {
      auto N = inputShape.dimensions[0];
      auto H = inputShape.dimensions[1];
      auto W = inputShape.dimensions[2];
      auto C = inputShape.dimensions[3];
      flattenedInputShape.dimensions = {N, H*W*C};
      break;
    }
  default:
    assert(inputShape.dimensions.size() <= 4);
  }
  // Finally, flattenedInputShape is a 2D tensor.

  // WEIGHTS is a 2D tensor
  assert(weightsShape.dimensions.size() == 2);

  // BIAS is a 1D tensor
  assert(biasShape.dimensions.size() == 1);

  // OUTPUT is a 2D tensor.
  assert(outputShape.dimensions.size() == 2);

  auto input_shape = util::fromNNShape(flattenedInputShape);
  auto weights_shape = util::fromNNShape(weightsShape);
  auto bias_shape = util::fromNNShape(biasShape);
  auto output_shape = util::fromNNShape(outputShape);

  assert(activation == ANEURALNETWORKS_FUSED_NONE || activation == ANEURALNETWORKS_FUSED_RELU);

  std::vector<std::shared_ptr<arm_compute::IFunction>> fns;

  TensorT input(arm_compute::TensorInfo(input_shape, arm_compute::Format::F32));
  TensorT output(arm_compute::TensorInfo(output_shape, arm_compute::Format::F32));
  TensorT bias(arm_compute::TensorInfo(bias_shape, arm_compute::Format::F32));
  TensorT weights(arm_compute::TensorInfo(weights_shape, arm_compute::Format::F32));

  auto fc = std::make_shared<LayerT>();
  fc->configure(input.ptr(), weights.ptr(), bias.ptr(), output.ptr());

  fns.emplace_back(fc);

  if (ANEURALNETWORKS_FUSED_RELU == activation)
  {
    auto relu_f = std::make_shared<ActT>();

    const arm_compute::ActivationLayerInfo relu_info{arm_compute::ActivationLayerInfo::ActivationFunction::RELU};

    // Do in-place update
    relu_f->configure(output.ptr(), nullptr, relu_info);

    fns.emplace_back(relu_f);
  }

  input.allocate();
  output.allocate();
  bias.allocate();
  weights.allocate();

  // TODO: Do we need 2D tensor accessor for the input feature?
  TensorAccess<MatrixWeightAccessor>(input.ref(), inputData, inputShape);
  TensorAccess<BiasAccessor>(bias.ref(), biasData, biasShape);
  TensorAccess<MatrixWeightAccessor>(weights.ref(), weightsData, weightsShape);

  for (const auto &fn : fns)
  {
    fn->run();
  }

  sync_scheduler();

  TensorAccess<MatrixOutputAccessor>(output.ref(), outputData, outputShape);

  return true;
}

} // namespace common

} // namespace acl
} // namespace kernel
} // namespace nnfw

#endif // __NNFW_KERNEL_ACL_FULLYCONNECTED_COMMON_H__