summaryrefslogtreecommitdiff
path: root/runtime/onert/core/include/backend/ITensorRegistry.h
blob: f5a95f49cf6dcd2e9449c768a4b1f60527dc3ad3 (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
/*
 * 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.
 */

#ifndef __ONERT_BACKEND_ITENSOR_REGISTRY__
#define __ONERT_BACKEND_ITENSOR_REGISTRY__

#include <memory>

#include "ir/Index.h"
#include "backend/ITensor.h"

namespace onert
{
namespace backend
{

struct ITensorRegistry
{
  /**
   * @brief Deconstruct itself
   */
  virtual ~ITensorRegistry() = default;

  /**
   * @brief Returns pointer of ITensor among managed and external tensors
   * @note  Return tensor cannot be used longer than dynamic tensor manager
   */
  virtual std::shared_ptr<ITensor> getITensor(const ir::OperandIndex &) = 0;
  /**
   * @brief Returns pointer of ITensor among managed tensors
   *
   * Unlike @c getITensor , this function only searches from managed tensors
   * @note  Return tensor cannot be used longer than dynamic tensor manager
   */
  virtual std::shared_ptr<ITensor> getManagedITensor(const ir::OperandIndex &) = 0;
};

} // namespace backend
} // namespace onert

#include "ir/OperandIndexMap.h"
#include "backend/IPortableTensor.h"

namespace onert
{
namespace backend
{

/**
 * @brief  TensorRegistry template class for the convenience of backend implementations
 *
 * If a backend uses @c IPortableTensor , and there is no special reason to implement @c
 * ITensorRegistry on your own, you may just use this default implementation.
 *
 * @tparam T_Tensor Tensor type. Must be a subclass of @c onert::backend::IPortableTensor .
 */
template <typename T_Tensor> class PortableTensorRegistryTemplate : public ITensorRegistry
{
public:
  std::shared_ptr<ITensor> getITensor(const ir::OperandIndex &ind) override
  {
    static_assert(std::is_base_of<ITensor, T_Tensor>::value, "T_Tensor must derive from ITensor.");
    auto external_tensor = _external.find(ind);
    if (external_tensor != _external.end())
      return external_tensor->second;
    return getManagedTensor(ind);
  }

  std::shared_ptr<ITensor> getManagedITensor(const ir::OperandIndex &ind) override
  {
    return getManagedTensor(ind);
  }

  std::shared_ptr<IPortableTensor> getPortableTensor(const ir::OperandIndex &ind)
  {
    auto external_tensor = _external.find(ind);
    if (external_tensor != _external.end())
    {
      if (external_tensor->second)
        return external_tensor->second;
    }
    return getManagedTensor(ind);
  }

  std::shared_ptr<T_Tensor> getManagedTensor(const ir::OperandIndex &ind)
  {
    auto tensor = _managed.find(ind);
    if (tensor != _managed.end())
      return tensor->second;
    return nullptr;
  }

  bool setExternalTensor(const ir::OperandIndex &ind,
                         const std::shared_ptr<IPortableTensor> &tensor)
  {
    // TODO Uncomment this as two tensors for an index is not allowed.
    //      But now it is temporarily allowed as a workaround. External one hides Managed one.
    // auto itr = _managed.find(ind);
    // if (itr != _managed.end() && itr->second != nullptr && tensor != nullptr)
    //  throw std::runtime_error{
    //      "Tried to set an external tensor but an managed tensor already exists."};
    _external[ind] = tensor;
    return true;
  }

  void setManagedTensor(const ir::OperandIndex &ind, const std::shared_ptr<T_Tensor> &tensor)
  {
    auto itr = _external.find(ind);
    if (itr != _external.end() && itr->second != nullptr && tensor != nullptr)
      throw std::runtime_error{
          "Tried to set a managed tensor but an external tensor already exists."};
    _managed[ind] = tensor;
  }

  const ir::OperandIndexMap<std::shared_ptr<T_Tensor>> &managed_tensors() { return _managed; }

  const ir::OperandIndexMap<std::shared_ptr<IPortableTensor>> &external_tensors()
  {
    return _external;
  }

private:
  ir::OperandIndexMap<std::shared_ptr<IPortableTensor>> _external;
  ir::OperandIndexMap<std::shared_ptr<T_Tensor>> _managed;
};

} // namespace backend
} // namespace onert

#endif // __ONERT_BACKEND_ITENSOR_REGISTRY__