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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
#pragma once
#include <c10/core/ScalarType.h>
#include <c10/util/Optional.h>
#include <torch/csrc/lazy/backend/backend_interface.h>
#include <torch/csrc/lazy/core/config.h>
#include <torch/csrc/lazy/core/ir.h>
#include <torch/csrc/lazy/core/tensor.h>
#include <torch/csrc/lazy/core/trie.h>
#include <vector>
// This file is part of the backend interface. So, ops shouldn't be added or
// removed without due process The exception to this being the view ops which
// will be removed soon pending functionalization
namespace torch {
namespace lazy {
template <typename T, typename... Args>
NodePtr ReuseNode(Args&&... args) {
if (FLAGS_torch_lazy_reuse_ir) {
return LookupNodeFromTrieCache<T>(std::forward<Args>(args)...);
}
return nullptr;
}
// Caching an IR node into TrieCache
static inline void CacheNode(NodePtr node) {
if (FLAGS_torch_lazy_reuse_ir) {
TrieCache::Get()->Insert(std::move(node));
}
}
template <typename T, typename... Args>
NodePtr MakeNode(Args&&... args) {
return std::make_shared<T>(std::forward<Args>(args)...);
}
// op is passed in for a more efficient node casting, see the implementation of
// NodeCast
template <typename T, typename... Args>
NodePtr ReuseOrMakeNode(Args&&... args) {
NodePtr node = ReuseNode<T>(std::forward<Args>(args)...);
if (!node) {
node = MakeNode<T>(std::forward<Args>(args)...);
CacheNode(node);
}
return node;
}
struct IrBuilder {
virtual NodePtr MakeDeviceData(
const std::shared_ptr<BackendData>& data) const = 0;
virtual NodePtr MakeScalar(
const at::Scalar& value,
const at::ScalarType& type) const = 0;
virtual NodePtr MakeExpand(
const Value& input0,
const std::vector<int64_t>& size,
const bool& is_scalar_expand) const = 0;
virtual NodePtr MakeView(
const Value& input0,
const std::vector<int64_t>& output_size) const = 0;
virtual NodePtr MakeCast(
const Value& input0,
const at::ScalarType& dtype,
const c10::optional<at::ScalarType>& stype = c10::nullopt) const = 0;
virtual NodePtr MakeTensorList(const OpList& inputs) const = 0;
virtual NodePtr MakeGeneric(
const OpKind& op,
const OpList& operands,
const Shape& shape,
const size_t& num_outputs = 1,
const hash_t& hash_seed = static_cast<uint32_t>(0x5a2d296e9)) const = 0;
// View op nodes
virtual NodePtr MakeAsStridedViewUpdate(
const Value& input0,
const Value& input1,
const std::vector<int64_t>& size,
const std::vector<int64_t>& stride,
const int64_t& storage_offset) const = 0;
virtual NodePtr MakeAsStrided(
const Value& input0,
const std::vector<int64_t>& size,
const std::vector<int64_t>& stride,
const int64_t& storage_offset) const = 0;
virtual NodePtr MakeDiagonalViewUpdate(
const Value& input0,
const Value& input1,
const int64_t& offset,
const int64_t& dim1,
const int64_t& dim2) const = 0;
virtual NodePtr MakeDiagonal(
const Value& input0,
const int64_t& offset,
const int64_t& dim1,
const int64_t& dim2) const = 0;
virtual NodePtr MakeNarrowViewUpdate(
const Value& input0,
const Value& input1,
const std::vector<int64_t>& base_indices) const = 0;
virtual NodePtr MakeNarrow(
const Value& input0,
const std::vector<int64_t>& base_indices,
const std::vector<int64_t>& sizes) const = 0;
virtual NodePtr MakePermute(
const Value& input0,
const std::vector<int64_t>& dims) const = 0;
virtual NodePtr MakeResize(
const Value& input0,
const std::vector<int64_t>& size) const = 0;
virtual NodePtr MakeSelectViewUpdate(
const Value& input0,
const Value& input1,
const int64_t& dim,
const int64_t& start,
const int64_t& end,
const int64_t& stride) const = 0;
virtual NodePtr MakeSelect(
const Value& input0,
const int64_t& dim,
const int64_t& start,
const int64_t& end,
const int64_t& stride) const = 0;
virtual NodePtr MakeSqueeze(const Value& input0, const int& dim) const = 0;
virtual NodePtr MakeUnsqueeze(const Value& input0, const int& dim) const = 0;
// dynamic ir nodes
virtual NodePtr MakeSizeNode(const Value& input, size_t dim) const = 0;
virtual NodePtr MakeSizeAdd(const Value& a, const Value& b) const = 0;
virtual NodePtr MakeSizeMul(const Value& a, const Value& b) const = 0;
virtual NodePtr MakeSizeDiv(const Value& a, const Value& b) const = 0;
virtual ~IrBuilder() = default;
};
static inline NodePtr MakeDeviceData(const std::shared_ptr<BackendData>& data) {
return getIrBuilder()->MakeDeviceData(data);
}
static inline NodePtr MakeScalar(
const at::Scalar& value,
const at::ScalarType& type) {
return getIrBuilder()->MakeScalar(value, type);
}
static inline NodePtr MakeExpand(
const Value& input0,
const std::vector<int64_t>& size,
const bool& is_scalar_expand) {
return getIrBuilder()->MakeExpand(input0, size, is_scalar_expand);
}
static inline NodePtr MakeView(
const Value& input0,
const std::vector<int64_t>& output_size) {
return getIrBuilder()->MakeView(input0, output_size);
}
static inline NodePtr MakeCast(
const Value& input0,
const at::ScalarType& dtype,
const c10::optional<at::ScalarType>& stype = c10::nullopt) {
return getIrBuilder()->MakeCast(input0, dtype, stype);
}
static inline NodePtr MakeTensorList(const OpList& inputs) {
return getIrBuilder()->MakeTensorList(inputs);
}
static inline NodePtr MakeGeneric(
const OpKind& op,
const OpList& operands,
const Shape& shape,
const size_t& num_outputs = 1,
const hash_t& hash_seed = static_cast<uint32_t>(0x5a2d296e9)) {
return getIrBuilder()->MakeGeneric(
op, operands, shape, num_outputs, hash_seed);
}
// View op nodes
static inline NodePtr MakeAsStridedViewUpdate(
const Value& input0,
const Value& input1,
const std::vector<int64_t>& size,
const std::vector<int64_t>& stride,
const int64_t& storage_offset) {
return getIrBuilder()->MakeAsStridedViewUpdate(
input0, input1, size, stride, storage_offset);
}
static inline NodePtr MakeAsStrided(
const Value& input0,
const std::vector<int64_t>& size,
const std::vector<int64_t>& stride,
const int64_t& storage_offset) {
return getIrBuilder()->MakeAsStrided(input0, size, stride, storage_offset);
}
static inline NodePtr MakeDiagonalViewUpdate(
const Value& input0,
const Value& input1,
const int64_t& offset,
const int64_t& dim1,
const int64_t& dim2) {
return getIrBuilder()->MakeDiagonalViewUpdate(
input0, input1, offset, dim1, dim2);
}
static inline NodePtr MakeDiagonal(
const Value& input0,
const int64_t& offset,
const int64_t& dim1,
const int64_t& dim2) {
return getIrBuilder()->MakeDiagonal(input0, offset, dim1, dim2);
}
static inline NodePtr MakeNarrowViewUpdate(
const Value& input0,
const Value& input1,
const std::vector<int64_t>& base_indices) {
return getIrBuilder()->MakeNarrowViewUpdate(input0, input1, base_indices);
}
static inline NodePtr MakeNarrow(
const Value& input0,
const std::vector<int64_t>& base_indices,
const std::vector<int64_t>& sizes) {
return getIrBuilder()->MakeNarrow(input0, base_indices, sizes);
}
static inline NodePtr MakePermute(
const Value& input0,
const std::vector<int64_t>& dims) {
return getIrBuilder()->MakePermute(input0, dims);
}
static inline NodePtr MakeResize(
const Value& input0,
const std::vector<int64_t>& size) {
return getIrBuilder()->MakeResize(input0, size);
}
static inline NodePtr MakeSelectViewUpdate(
const Value& input0,
const Value& input1,
const int64_t& dim,
const int64_t& start,
const int64_t& end,
const int64_t& stride) {
return getIrBuilder()->MakeSelectViewUpdate(
input0, input1, dim, start, end, stride);
}
static inline NodePtr MakeSelect(
const Value& input0,
const int64_t& dim,
const int64_t& start,
const int64_t& end,
const int64_t& stride) {
return getIrBuilder()->MakeSelect(input0, dim, start, end, stride);
}
static inline NodePtr MakeSqueeze(const Value& input0, const int& dim) {
return getIrBuilder()->MakeSqueeze(input0, dim);
}
static inline NodePtr MakeUnsqueeze(const Value& input0, const int& dim) {
return getIrBuilder()->MakeUnsqueeze(input0, dim);
}
// dynamic ir nodes
static inline NodePtr MakeSizeNode(const Value& input, size_t dim) {
return getIrBuilder()->MakeSizeNode(input, dim);
}
static inline NodePtr MakeSizeAdd(const Value& a, const Value& b) {
return getIrBuilder()->MakeSizeAdd(a, b);
}
static inline NodePtr MakeSizeMul(const Value& a, const Value& b) {
return getIrBuilder()->MakeSizeAdd(a, b);
}
static inline NodePtr MakeSizeDiv(const Value& a, const Value& b) {
return getIrBuilder()->MakeSizeDiv(a, b);
}
inline Value GetSymIntValue(c10::SymInt a) {
return Value(
a.is_symbolic() ? dynamic_cast<torch::lazy::SymIntNodeImpl*>(
a.toSymIntNodeImpl().get())
->node_
: MakeScalar(a.as_int_unchecked(), at::kLong),
0);
}
// TODO: this should return Value
inline std::vector<int64_t> GetSymIntArrayRefValue(c10::SymIntArrayRef arr) {
std::vector<int64_t> r;
for (const auto& a : arr) {
r.emplace_back(a.expect_int());
}
return r;
}
} // namespace lazy
} // namespace torch
|