summaryrefslogtreecommitdiff
path: root/runtime/onert/backend/gpu_cl/ClConstantInitializer.h
blob: 95e228acd181dbb2ce44458c2d612b7234afff9f (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
/*
 * Copyright (c) 2021 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 __ONERT_COMPILER_GPU_CL_CLCONSTANT_INITIALIZER_H__
#define __ONERT_COMPILER_GPU_CL_CLCONSTANT_INITIALIZER_H__

#include <unordered_map>
#include <functional>

#include <ir/Coordinates.h>
#include <ir/Layout.h>
#include <ir/Operand.h>
#include <ir/Operands.h>
#include <ir/OperationVisitor.h>
#include <backend/ITensorRegistry.h>
#include <util/logging.h>

namespace onert
{
namespace backend
{
namespace gpu_cl
{

template <typename T>
static void Init(const onert::ir::Operand &model_obj, onert::backend::ITensor &obj, const bool copy,
                 const onert::ir::Layout frontend_layout = onert::ir::Layout::UNKNOWN)
{
  const auto shape = model_obj.shape();
  assert(model_obj.data());
  obj.access([&](::onert::backend::ITensor &tensor) {
    switch (shape.rank())
    {
      case 0:
      case 1:
      case 2:
      case 3:
      case 4:
        if (copy)
        {
          tensor.enqueueWriteBuffer(model_obj.data()->base(), true);
        }
        else
        {
          // NYI
          (void)frontend_layout;
          throw std::runtime_error{"Not yet supported"};
        }
        break;
      default:
        throw std::runtime_error{"Not yet supported"};
    }
  });
}

template <typename T>
void copyInit(const onert::ir::Operand &model_obj, onert::backend::ITensor &obj)
{
  Init<T>(model_obj, obj, true);
}

template <typename T>
void permuteInit(const onert::ir::Operand &model_obj, onert::backend::ITensor &obj,
                 const onert::ir::Layout frontend_layout)
{
  const bool copy = frontend_layout == obj.layout();
  Init<T>(model_obj, obj, copy, frontend_layout);
}

class ClConstantInitializer : public ir::OperationVisitor
{
public:
  void run()
  {
    assert(_tensor_reg);
    for (const auto &it : _init_map)
    {
      const auto &ind = it.first;
      const auto &fn = it.second;

      const auto &model_obj = _operands.at(ind);
      auto tensor_obj = _tensor_reg->getNativeITensor(ind);
      assert(tensor_obj != nullptr);
      fn(model_obj, *tensor_obj);
      VERBOSE(FillOperandData) << "Fill data for operand " << ind << std::endl;
    }
    _init_map.clear();
  }

public:
  ClConstantInitializer(const ir::Operands &operands,
                        const std::shared_ptr<ITensorRegistry> &tensor_reg);

public:
  using Initializer = std::function<void(const ir::Operand &, backend::ITensor &)>;

public:
  void registerDefaultInitializer(const ir::OperandIndex &index, const ir::Operand &obj)
  {
    registerPermuteInitializer(index, obj);
  }
  void registerCopyInitializer(const ir::OperandIndex &index, const ir::Operand &obj);
  void registerPermuteInitializer(const ir::OperandIndex &index, const ir::Operand &obj);

public:
  void setLayout(ir::Layout layout) { _current_layout = layout; }
  bool exist(const ir::OperandIndex &ind) { return _init_map.find(ind) != _init_map.end(); }

public:
protected:
  void copyInputInitialize(const ir::Operation &node, uint32_t index);
  void permuteInputInitialize(const ir::Operation &node, uint32_t index);

protected:
  const ir::Operands &_operands;
  std::shared_ptr<ITensorRegistry> _tensor_reg;
  std::unordered_map<ir::OperandIndex, Initializer> _init_map;
  ir::Layout _current_layout;
};

} // namespace gpu_cl
} // namespace backend
} // namespace onert

#endif // __ONERT_COMPILER_GPU_CL_CLCONSTANT_INITIALIZER_H__