summaryrefslogtreecommitdiff
path: root/compiler/luci/import/src/Nodes/CircleConst.cpp
blob: fad7a0757c7da1ed307d377f6fb89ea1aecd71df (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
150
/*
 * Copyright (c) 2020 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 "luci/Import/Nodes/CircleConst.h"

#include <luci/IR/Nodes/CircleConst.h>
#include <luci/Log.h>

#include <loco.h>
#include <oops/UserExn.h>

#include <cassert>

namespace
{

std::ostream &operator<<(std::ostream &os, const std::vector<int32_t> &vect)
{
  uint32_t seq = 0;
  for (auto &v : vect)
  {
    if (seq)
      os << ", ";
    os << v;
    seq++;
  }
  return os;
}

} // namespace

namespace luci
{

template <loco::DataType DT>
static void copy_data(const std::vector<uint8_t> &raw_data, uint32_t num_elements,
                      CircleConst *const_node)
{
  using T = typename loco::DataTypeImpl<DT>::Type;

  assert(raw_data.size() == num_elements * sizeof(T));
  const auto *data = reinterpret_cast<const T *>(raw_data.data());

  const_node->size<DT>(num_elements);
  for (uint32_t i = 0; i < num_elements; ++i)
  {
    const_node->at<DT>(i) = data[i];
  }
}

//
// circleconst_from_tensor() ?
//
CircleConst *create_circleconst(GraphBuilderContext *context, int32_t tensor_index)
{
  LOGGER(l);

  auto graph = context->graph();
  auto reader = context->reader();
  const auto &tensors = reader->tensors();
  const circle::TensorT &const_tensor = *tensors[tensor_index];

  const std::vector<uint8_t> &buffer = reader->buffers()[const_tensor.buffer]->data;
  std::vector<int32_t> const_dims = const_tensor.shape; // in NHWC
  if (const_dims.size() == 0 && buffer.empty())
  {
    // unknown shape tensor
    return nullptr;
  }

  // if tensor_index is used as output to some other operator, this is not a constant
  auto tensoroutputs = context->tensoroutputs();
  if (tensoroutputs->find(tensor_index))
  {
    // other operator output tensor
    return nullptr;
  }

  uint32_t num_elements = 1;
  for (uint32_t r = 0; r < const_dims.size(); ++r)
  {
    num_elements = num_elements * const_dims[r];
  }

  if (buffer.empty() && num_elements > 0)
  {
    // normal empty tensor
    return nullptr;
  }

  auto const_node = graph->nodes()->create<CircleConst>();
  copy_tensor_attributes(const_tensor, const_node);
  const_node->shape_status(luci::ShapeStatus::VALID);
  INFO(l) << "[luci] NodeFinder const_node(" << tensor_index << ") -> " << const_node << " "
          << const_dims << std::endl;
  if (num_elements > 0)
  {
    switch (luci_datatype(const_tensor.type))
    {
      case loco::DataType::FLOAT32:
        copy_data<loco::DataType::FLOAT32>(buffer, num_elements, const_node);
        break;

      case loco::DataType::U8:
        copy_data<loco::DataType::U8>(buffer, num_elements, const_node);
        break;

      case loco::DataType::S8:
        copy_data<loco::DataType::S8>(buffer, num_elements, const_node);
        break;

      case loco::DataType::S16:
        copy_data<loco::DataType::S16>(buffer, num_elements, const_node);
        break;

      case loco::DataType::S32:
        copy_data<loco::DataType::S32>(buffer, num_elements, const_node);
        break;

      case loco::DataType::S64:
        copy_data<loco::DataType::S64>(buffer, num_elements, const_node);
        break;

      case loco::DataType::BOOL:
        copy_data<loco::DataType::BOOL>(buffer, num_elements, const_node);
        break;

      default:
        throw oops::UserExn("Unsupported tensor type",
                            circle::EnumNameTensorType(const_tensor.type));
    }
  }

  return const_node;
}

} // namespace luci