summaryrefslogtreecommitdiff
path: root/runtimes/neurun/core/src/exec/interp/operations/OperationUtil.h
blob: 4d2b4e1d839e91dfbe11107293c46f1fd06d0a7d (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
/*
 * Copyright (c) 2019 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 __NEURUN_EXEC_INTERP_OPERATIONS_OPERATION_UTILS_H_
#define __NEURUN_EXEC_INTERP_OPERATIONS_OPERATION_UTILS_H_

#include "model/Shape.h"
#include "model/InternalType.h"

#include <cker/Shape.h>

namespace neurun
{
namespace exec
{
namespace interp
{

inline nnfw::cker::Shape convertShape(const model::Shape &shape)
{
  auto dimensions = std::vector<uint32_t>(shape.dims().begin(), shape.dims().end());

  std::vector<int32_t> raw_shape;
  raw_shape.resize(4);

  for (uint32_t i = 0; i < 4; ++i)
  {
    if (i >= dimensions.size())
    {
      raw_shape[i] = 1;
    }
    else
    {
      raw_shape[i] = dimensions[i];
    }
  }

  return nnfw::cker::GetShape(raw_shape);
}

inline nnfw::cker::Shape convertExtendShape(const model::Shape &shape)
{
  auto dimensions = std::vector<uint32_t>(shape.dims().begin(), shape.dims().end());

  std::vector<int32_t> raw_shape;
  raw_shape.resize(4);
  uint32_t start = 4 - dimensions.size();

  for (uint32_t i = 0; i < 4; ++i)
  {
    if (i < start)
    {
      raw_shape[i] = 1;
    }
    else
    {
      raw_shape[i] = dimensions[i - start];
    }
  }

  return nnfw::cker::GetShape(raw_shape);
}

template <typename T>
void calculateActivationRange(model::Activation activation, T *activation_min, T *activation_max)
{
  if (activation == model::Activation::RELU)
  {
    *activation_min = 0;
    *activation_max = std::numeric_limits<T>::max();
  }
  else if (activation == model::Activation::RELU6)
  {
    *activation_min = 0;
    *activation_max = 6;
  }
  else if (activation == model::Activation::RELU1)
  {
    *activation_min = -1;
    *activation_max = 1;
  }
  else if (activation == model::Activation::NONE)
  {
    *activation_min = std::numeric_limits<T>::lowest();
    *activation_max = std::numeric_limits<T>::max();
  }
  else
  {
    throw std::runtime_error{"Unsupported activation type"};
  }
}

} // namespace interp
} // namespace exec
} // namespace neurun

#endif // __NEURUN_EXEC_INTERP_OPERATIONS_OPERATION_UTILS_H_