summaryrefslogtreecommitdiff
path: root/runtimes/neurun/backend/srcn/kernel/OperationUtils.cc
blob: 0df0f7b33a8d405948fd045032a03591ef447347 (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
/*
 * 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.
 */

#include "OperationUtils.h"

#include <cmath>
#include <algorithm>
#include <cassert>

#include "util/Utils.h"

namespace neurun
{
namespace backend
{
namespace srcn
{
namespace kernel
{

uint32_t MatchingDim(const Shape &shape1, int index1, const Shape &shape2, int index2)
{
  UNUSED_RELEASE(shape2);
  UNUSED_RELEASE(index2);
  assert(shape1.dimensions[index1] == shape2.dimensions[index2]);
  return shape1.dimensions[index1];
}

Coordinates convertCoordinates(const Coordinates &from_coordinates, FilterLayout from_layout,
                               FilterLayout to_layout)
{
  assert(from_coordinates.size() == 4);
  Coordinates to{from_coordinates};
  if (from_layout == FilterLayout::OHWI && to_layout == FilterLayout::HWOI)
  {
    to.set(0, from_coordinates[1]);
    to.set(1, from_coordinates[2]);
    to.set(2, from_coordinates[0]);
    to.set(3, from_coordinates[3]);
  }
  else
  {
    throw std::runtime_error{"NYI"};
  }

  return to;
}

Shape getShape(const ::neurun::model::Operand &o, ::neurun::model::Layout frontend_layout)
{
  Shape shape;

  auto dims = o.shape().dims();
  if (frontend_layout == ::neurun::model::Layout::NCHW && o.shape().rank() == 4)
  {
    // NCHW -> NHWC
    uint32_t permutation[4] = {0, 2, 3, 1};
    for (int i = 0; i < o.shape().rank(); ++i)
    {
      dims.at(i) = o.shape().dim(permutation[i]);
    }
  }
  shape.dimensions = std::vector<uint32_t>(dims.begin(), dims.end());
  shape.type = static_cast<OperandType>(static_cast<int32_t>(o.typeInfo().type()));
  shape.scale = o.typeInfo().scale();
  shape.offset = o.typeInfo().offset();

  // CPU backend assume that neurun internal shape's rank is always same or less than 4
  assert(shape.dimensions.size() <= 4);

  return shape;
}

} // namespace kernel
} // namespace srcn
} // namespace backend
} // namespace neurun