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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
#pragma once
#include <torch/csrc/WindowsTorchApiMacro.h>
#include <functional>
#include <vector>
#include <torch/csrc/jit/tensorexpr/dim_arg.h>
#include <torch/csrc/jit/tensorexpr/expr.h>
#include <torch/csrc/jit/tensorexpr/reduction.h>
namespace torch {
namespace jit {
namespace tensorexpr {
class Function : public KernelScopedObject {
public:
Function(
const std::string& func_name,
const std::vector<const Expr*>& dims,
const std::vector<const Var*>& args,
const Expr* body)
// TODO: Function should not create buffers, they should be created
// manually before constructing a function.
: func_vars_({new Buf(func_name, dims, body->dtype())}),
dims_(dims),
args_(args),
bodies_({body}) {}
Function(
const std::vector<std::string>& func_names,
const std::vector<const Expr*>& dims,
const std::vector<const Var*>& args,
const std::vector<const Expr*>& bodies)
: func_vars_(func_names.size()),
dims_(dims),
args_(args),
bodies_(bodies) {
for (size_t i = 0; i < func_names.size(); i++) {
func_vars_[i] = new Buf(func_names[i], dims, bodies[i]->dtype());
}
}
Function(
const std::string& func_name,
Buf* func_var,
const std::vector<const Expr*>& dims,
const std::vector<const Var*>& args,
const Expr* body)
: func_vars_({func_var}), dims_(dims), args_(args), bodies_({body}) {}
size_t ndim() const {
return dims_.size();
}
const Expr* dim(size_t index) const {
if (index < 0 || index >= dims_.size()) {
throw out_of_range_index();
}
return dims_[index];
}
const std::vector<const Expr*>& dims() const {
return dims_;
}
const Var* arg(size_t index) const {
if (index < 0 || index >= args_.size()) {
throw out_of_range_index();
}
return args_[index];
}
const std::vector<const Var*>& args() const {
return args_;
}
std::vector<const Expr*> bodies() const {
return bodies_;
}
const Expr* body(size_t index) const {
if (index >= bodies_.size()) {
throw out_of_range_index();
}
return bodies_[index];
}
std::vector<const Buf*> func_vars() const {
return func_vars_;
}
const Buf* func_var(size_t index) const {
if (index >= func_vars_.size()) {
throw out_of_range_index();
}
return func_vars_[index];
}
Stmt* ElementStmt(size_t index);
private:
std::vector<const Buf*> func_vars_;
std::vector<const Expr*> dims_;
std::vector<const Var*> args_;
std::vector<const Expr*> bodies_;
};
class Tensor : KernelScopedObject {
public:
Tensor(Function* function, int output_index)
: function_(function), output_index_(output_index) {}
Function* function() const {
return function_;
}
int output_index() const {
return output_index_;
}
// Wrappers over accessors to fields of the underlying function
const Expr* body() const {
return function()->body(output_index());
}
const Buf* buf() const {
return function()->func_var(output_index());
}
int ndim() const {
return buf()->dims().size();
}
const Expr* dim(int index) const {
return buf()->dim(index);
}
std::vector<const Expr*> dims() const {
return buf()->dims();
}
const Var* arg(int index) const {
return function()->arg(index);
}
const std::vector<const Var*>& args() const {
return function()->args();
}
void initializeTo(const Expr* initializer) {
initializer_ = initializer;
}
const Expr* initializer() const {
return initializer_;
}
template <typename... Ts>
inline ExprHandle operator()(const Ts&... ts);
template <typename T>
inline ExprHandle call(const std::vector<T>& args);
template <typename... Ts>
inline ExprHandle call(const Ts&... ts);
private:
Function* function_;
int output_index_;
const Expr* initializer_{nullptr};
};
class Placeholder {
public:
Placeholder(const BufHandle& data) : data_(data.node()) {
if (data_->base_handle()->dtype() != kHandle) {
throw malformed_input("Placeholder dtype must be Handle");
}
std::vector<ExprHandle> stride_handles(ndim());
for (int i = (int)ndim() - 1; i >= 0; i--) {
if (i == ndim() - 1) {
stride_handles[i] = 1;
} else {
stride_handles[i] = stride_handles[i + 1] * ExprHandle(dim(i + 1));
}
}
strides_ = ExprHandleVectorToExprVector(stride_handles);
}
Placeholder(
const std::string& name,
const Dtype& dtype,
const std::vector<ExprHandle>& dims)
: Placeholder(BufHandle(name, dims, dtype)) {}
const Buf* data() const {
return data_;
}
Dtype dtype() const {
return data_->dtype();
}
int ndim() const {
return data_->ndim();
}
const Expr* dim(int index) const {
return data_->dim(index);
}
std::vector<const Expr*> dims() const {
return data_->dims();
}
template <typename... Ts>
inline ExprHandle load(const Ts&... ts) const;
template <typename T>
inline ExprHandle load(const std::vector<T>& args) const;
inline ExprHandle loadWithMask(
const std::vector<ExprHandle>& args,
const ExprHandle& mask) const {
return ExprHandle(
new Load(data(), ExprHandleVectorToExprVector(args), mask.node()));
}
inline Store* store(
const std::vector<ExprHandle>& args,
const ExprHandle& val) const {
return new Store(
data(), ExprHandleVectorToExprVector(args), val.node(), new IntImm(1));
}
inline Store* storeWithMask(
const std::vector<ExprHandle>& args,
const ExprHandle& val,
const ExprHandle& mask) const {
return new Store(
data(), ExprHandleVectorToExprVector(args), val.node(), mask.node());
}
private:
const Buf* data_;
std::vector<const Expr*> strides_;
};
TORCH_API Tensor* Compute(
const std::string& func_name,
const std::vector<DimArg>& dim_args,
const std::function<ExprHandle(const VarHandle&)>& body_func);
TORCH_API Tensor* Compute(
const std::string& func_name,
const std::vector<DimArg>& dim_args,
const std::function<ExprHandle(const VarHandle&, const VarHandle&)>&
body_func);
TORCH_API Tensor* Compute(
const std::string& func_name,
const std::vector<DimArg>& dim_args,
const std::function<
ExprHandle(const VarHandle&, const VarHandle&, const VarHandle&)>&
body_func);
TORCH_API Tensor* Compute(
const std::string& func_name,
const std::vector<DimArg>& dim_args,
const std::function<ExprHandle(
const VarHandle&,
const VarHandle&,
const VarHandle&,
const VarHandle&)>& body_func);
TORCH_API Tensor* Compute(
const std::string& func_name,
const std::vector<DimArg>& dim_args,
const std::function<ExprHandle(const std::vector<VarHandle>&)>& body_func);
inline void unpack_dim_args(
const std::vector<DimArg>& dim_args,
std::vector<const Expr*>* dims,
std::vector<const Var*>* vars) {
dims->clear();
vars->clear();
for (const DimArg& dim_arg : dim_args) {
dims->push_back(dim_arg.dim().node());
vars->push_back(new Var(dim_arg.name_hint(), kInt));
}
}
// Handle reductions over a Reducer and a body_func which produces values.
template <typename BodyFunc>
Tensor* Reduce(
const std::string& func_name,
const std::vector<DimArg>& dim_args,
const Reducer& reducer,
const BodyFunc& body_func,
const std::vector<DimArg>& reduce_args) {
std::vector<const Expr*> dims;
std::vector<const Var*> vars;
unpack_dim_args(dim_args, &dims, &vars);
std::vector<const Expr*> reduce_dims;
std::vector<const Var*> reduce_vars;
unpack_dim_args(reduce_args, &reduce_dims, &reduce_vars);
std::vector<const Var*> all_vars;
all_vars.insert(all_vars.end(), vars.begin(), vars.end());
all_vars.insert(all_vars.end(), reduce_vars.begin(), reduce_vars.end());
ExprHandle body =
Reducer::getReduceBody(body_func, VarVectorToVarHandleVector(all_vars));
std::vector<const Expr*> output_args(vars.begin(), vars.end());
Buf* func_result = new Buf(func_name, dims, body.dtype());
const ReduceOp* reduce_op =
reducer(func_result, body, output_args, reduce_vars);
dims.insert(dims.end(), reduce_dims.begin(), reduce_dims.end());
Function* func =
new Function(func_name, func_result, dims, all_vars, reduce_op);
Tensor* t = new Tensor(func, 0);
t->initializeTo(new Cast(body.dtype(), reducer.initializer()));
return t;
}
// Overload which allows inline lambda functions for the body_func.
template <typename BodyFunc>
Tensor* Reduce(
const std::string& func_name,
const std::vector<DimArg>& dim_args,
const Reducer& reducer,
const BodyFunc&& body_func,
const std::vector<DimArg>& reduce_args) {
return Reduce(func_name, dim_args, reducer, body_func, reduce_args);
}
// Overload for the common case of all dimensions of a Placeholder.
TORCH_API Tensor* Reduce(
const std::string& func_name,
const std::vector<DimArg>& dim_args,
const Reducer& reducer,
const Placeholder& buffer,
const std::vector<DimArg>& reduce_args);
// Overload for the common case of all dimensions of a prevously Computed
// Tensor.
TORCH_API Tensor* Reduce(
const std::string& func_name,
const std::vector<DimArg>& dim_args,
const Reducer& reducer,
Tensor* tensor,
const std::vector<DimArg>& reduce_args);
class FunctionCall : public CallNode<FunctionCall> {
public:
using BaseClass = CallNode<FunctionCall>;
static ExprHandle make(
Tensor* tensor,
const std::vector<ExprHandle>& params) {
std::vector<const Expr*> params_nodes(params.size());
for (size_t i = 0; i < params.size(); i++) {
params_nodes[i] = params[i].node();
}
return ExprHandle(new FunctionCall(tensor, params_nodes));
}
const Tensor* tensor() const {
return tensor_;
}
Tensor* tensor() {
return tensor_;
}
FunctionCall(Tensor* tensor, const std::vector<const Expr*>& params)
: BaseClass(
tensor->function()->body(tensor->output_index())->dtype(),
kFunctionCall,
params),
tensor_(tensor) {}
private:
const Expr* DefaultMutator(
const std::vector<const Expr*>& new_params) const override {
return new FunctionCall(tensor_, new_params);
}
std::string func_name() const override {
return tensor_->buf()->name_hint();
}
Tensor* tensor_;
};
template <typename... Ts>
inline ExprHandle Tensor::operator()(const Ts&... ts) {
std::vector<ExprHandle> params({ExprHandle(ts)...});
return FunctionCall::make(this, std::move(params));
}
template <typename... Ts>
inline ExprHandle Tensor::call(const Ts&... ts) {
std::vector<ExprHandle> params({ExprHandle(ts)...});
return FunctionCall::make(this, std::move(params));
}
template <typename T>
inline ExprHandle Tensor::call(const std::vector<T>& args) {
std::vector<ExprHandle> params(args.begin(), args.end());
return FunctionCall::make(this, params);
}
template <typename... Ts>
inline ExprHandle Placeholder::load(const Ts&... ts) const {
std::vector<ExprHandle> params({ExprHandle(ts)...});
return ExprHandle(
new Load(data(), ExprHandleVectorToExprVector(params), new IntImm(1)));
}
template <typename T>
inline ExprHandle Placeholder::load(const std::vector<T>& args) const {
std::vector<ExprHandle> params(args.begin(), args.end());
return ExprHandle(
new Load(data(), ExprHandleVectorToExprVector(params), new IntImm(1)));
}
} // namespace tensorexpr
} // namespace jit
} // namespace torch
|