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 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
|
#pragma once
#include <string>
#include <vector>
#include <c10/util/string_utils.h>
#include <torch/csrc/jit/tensorexpr/exceptions.h>
#include <torch/csrc/jit/tensorexpr/expr.h>
#include <torch/csrc/jit/tensorexpr/fwd_decls.h>
#include <torch/csrc/jit/tensorexpr/stmt.h>
#include <ATen/core/ivalue.h>
namespace torch {
namespace jit {
namespace tensorexpr {
enum CompareSelectOperation {
kEQ = 0,
kGT,
kGE,
kLT,
kLE,
kNE,
};
enum CompareSelectBias {
kUnbiased,
kLikely,
kUnlikely,
};
inline int getPrecedence(IRNodeType ty) {
// Match C++ operator precedence rules, since some pretty-print expressions to
// C++. SEE: https://en.cppreference.com/w/cpp/language/operator_precedence
switch (ty) {
case kPrimitive:
return 0;
case kCast:
case kBitCast:
return 2;
case kAdd:
case kSub:
return 6;
case kMul:
case kDiv:
case kMod:
return 5;
case kMax:
case kMin:
return 99;
case kAnd:
return 11;
case kOr:
return 13;
case kLshift:
case kRshift:
return 7;
case kXor:
return 12;
case kCompareSelect:
return 16;
default:
return 99;
}
}
class TORCH_API Cast : public ExprNode<Cast> {
public:
ExprPtr src_value() const {
return src_value_;
}
void set_src_value(ExprPtr src_value) {
src_value_ = src_value;
}
static ExprHandle make(Dtype dtype, const ExprHandle& src_value) {
return ExprHandle(alloc<Cast>(dtype, src_value.node()));
}
Cast(Dtype dtype, ExprPtr src_value)
: ExprNodeBase(dtype, kCast), src_value_(src_value) {}
bool isConstant() const override {
return src_value_->isConstant();
}
private:
ExprPtr src_value_;
};
template <typename T>
ExprHandle cast(const ExprHandle& src_value) {
return Cast::make(Dtype(ToDtype<T>(), src_value.dtype().lanes()), src_value);
}
// This is a bitwise cast, akin to bitcast in LLVM
class TORCH_API BitCast : public ExprNode<BitCast> {
public:
ExprPtr src_value() const {
return src_value_;
}
void set_src_value(ExprPtr src_value) {
src_value_ = src_value;
}
static ExprHandle make(Dtype dtype, const ExprHandle& src_value) {
return ExprHandle(alloc<BitCast>(dtype, src_value.node()));
}
BitCast(Dtype dtype, ExprPtr src_value)
: ExprNodeBase(dtype, kBitCast), src_value_(src_value) {
TORCH_CHECK(src_value_->dtype().byte_size() == dtype.byte_size());
}
bool isConstant() const override {
return src_value_->isConstant();
}
private:
ExprPtr src_value_;
};
template <typename T>
ExprHandle bitcast(const ExprHandle& src_value) {
return BitCast::make(
Dtype(ToDtype<T>(), src_value.dtype().lanes()), src_value);
}
// Represent the expression node for binary operators.
// A CRTP pattern to share common code among the operators.
template <typename Op>
class BinaryOpNode : public ExprNode<Op> {
public:
ExprPtr lhs() const {
return this->lhs_;
}
ExprPtr rhs() const {
return this->rhs_;
}
void set_lhs(ExprPtr lhs) {
lhs_ = lhs;
}
void set_rhs(ExprPtr rhs) {
rhs_ = rhs;
}
static ExprHandle make(const ExprHandle& lhs, const ExprHandle& rhs) {
return ExprHandle(alloc<Op>(lhs.node(), rhs.node()));
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
BinaryOpNode(
ExprPtr lhs_v,
ExprPtr rhs_v,
IRNodeType expr_type,
ScalarType ret_type = ScalarType::Undefined)
: ExprNode<Op>(
// NOLINTNEXTLINE(clang-analyzer-core.CallAndMessage)
BinaryOpDtype(lhs_v->dtype(), rhs_v->dtype(), ret_type),
expr_type),
lhs_(CastIfNeeded(lhs_v, ExprNode<Op>::dtype())),
rhs_(CastIfNeeded(rhs_v, ExprNode<Op>::dtype())) {}
private:
static ExprPtr CastIfNeeded(ExprPtr expr, Dtype dst_dtype) {
if (expr->dtype() == dst_dtype) {
return expr;
}
return Cast::make(dst_dtype, ExprHandle(expr)).node();
}
ExprPtr lhs_;
ExprPtr rhs_;
};
namespace detail {
template <typename T>
void bin_op_deducer(BinaryOpNode<T>);
bool bin_op_deducer(...);
} // namespace detail
class TORCH_API Add : public BinaryOpNode<Add> {
public:
Add(ExprPtr lhs, ExprPtr rhs) : BinaryOpNode(lhs, rhs, IRNodeType::kAdd) {}
};
class TORCH_API Sub : public BinaryOpNode<Sub> {
public:
Sub(ExprPtr lhs, ExprPtr rhs) : BinaryOpNode(lhs, rhs, IRNodeType::kSub) {}
};
class TORCH_API Mul : public BinaryOpNode<Mul> {
public:
Mul(ExprPtr lhs, ExprPtr rhs) : BinaryOpNode(lhs, rhs, IRNodeType::kMul) {}
};
class TORCH_API Div : public BinaryOpNode<Div> {
public:
Div(ExprPtr lhs, ExprPtr rhs) : BinaryOpNode(lhs, rhs, IRNodeType::kDiv) {}
};
class TORCH_API Mod : public BinaryOpNode<Mod> {
public:
Mod(ExprPtr lhs, ExprPtr rhs) : BinaryOpNode(lhs, rhs, IRNodeType::kMod) {}
};
template <typename Op>
class BitwiseOpNode : public BinaryOpNode<Op> {
public:
BitwiseOpNode(ExprPtr lhs, ExprPtr rhs, IRNodeType type)
: BinaryOpNode<Op>(lhs, rhs, type) {}
static ExprHandle make(const ExprHandle& lhs, const ExprHandle& rhs) {
if (!lhs.dtype().is_integral()) {
throw unsupported_dtype();
}
if (lhs.dtype() != rhs.dtype()) {
throw malformed_input("lhs/rhs dtype mismatch");
}
return BinaryOpNode<Op>::make(lhs, rhs);
}
};
class TORCH_API And : public BitwiseOpNode<And> {
public:
And(ExprPtr lhs, ExprPtr rhs) : BitwiseOpNode(lhs, rhs, IRNodeType::kAnd) {}
};
class TORCH_API Or : public BitwiseOpNode<Or> {
public:
Or(ExprPtr lhs, ExprPtr rhs) : BitwiseOpNode(lhs, rhs, IRNodeType::kOr) {}
};
class TORCH_API Xor : public BitwiseOpNode<Xor> {
public:
Xor(ExprPtr lhs, ExprPtr rhs) : BitwiseOpNode(lhs, rhs, IRNodeType::kXor) {}
};
class TORCH_API Lshift : public BitwiseOpNode<Lshift> {
public:
Lshift(ExprPtr lhs, ExprPtr rhs)
: BitwiseOpNode(lhs, rhs, IRNodeType::kLshift) {}
};
class TORCH_API Rshift : public BitwiseOpNode<Rshift> {
public:
Rshift(ExprPtr lhs, ExprPtr rhs)
: BitwiseOpNode(lhs, rhs, IRNodeType::kRshift) {}
};
// TODO: add TORCH_API
// Currently adding it results in a compilation error on Windows
class Max : public BinaryOpNode<Max> {
private:
bool propagate_nans_;
public:
Max(ExprPtr lhs, ExprPtr rhs, bool propagate_nans)
: BinaryOpNode(lhs, rhs, IRNodeType::kMax),
propagate_nans_(propagate_nans) {}
bool propagate_nans() const {
return propagate_nans_;
}
static ExprHandle make(const ExprHandle& lhs, const ExprHandle& rhs) = delete;
static ExprHandle make(
const ExprHandle& lhs,
const ExprHandle& rhs,
bool propagate_nans) {
return ExprHandle(alloc<Max>(lhs.node(), rhs.node(), propagate_nans));
}
};
// TODO: add TORCH_API
// Currently adding it results in a compilation error on Windows
class Min : public BinaryOpNode<Min> {
private:
bool propagate_nans_;
public:
Min(ExprPtr lhs, ExprPtr rhs, bool propagate_nans)
: BinaryOpNode(lhs, rhs, IRNodeType::kMin),
propagate_nans_(propagate_nans) {}
bool propagate_nans() const {
return propagate_nans_;
}
static ExprHandle make(const ExprHandle& lhs, const ExprHandle& rhs) = delete;
static ExprHandle make(
const ExprHandle& lhs,
const ExprHandle& rhs,
bool propagate_nans) {
return ExprHandle(alloc<Min>(lhs.node(), rhs.node(), propagate_nans));
}
};
// Encode typed immediate values e.g. IntImm, FloatImm.
#define IMM_DECLARE(Type, Name) \
class TORCH_API Name##Imm : public ExprNode<Name##Imm> { \
public: \
Name##Imm(Type value) \
: ExprNodeBase(k##Name, kPrimitive), value_(value) {} \
bool isConstant() const override { \
return true; \
} \
Type value() const { \
return value_; \
} \
static ExprHandle make(Type value) { \
return ExprHandle(alloc<Name##Imm>(value)); \
} \
\
private: \
Type value_; \
};
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, IMM_DECLARE);
#undef IMM_DECLARE
// Get immediate by ScalarType.
template <typename T>
ExprPtr getImmediateByType(ScalarType immType, T initialVal) {
switch (immType) {
#define TYPE_CASE(Type, Name) \
case ScalarType::Name: \
return alloc<Name##Imm>(Type(initialVal));
// NOLINTNEXTLINE(bugprone-branch-clone)
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, TYPE_CASE);
#undef TYPE_CASE
default:
throw unsupported_dtype();
}
return nullptr;
}
template <typename T>
ExprPtr getImmediateByType(Dtype dtype, T initialVal) {
return getImmediateByType<T>(dtype.scalar_type(), initialVal);
}
template <typename T>
ExprPtr immLike(ExprPtr e, T v) {
return getImmediateByType<T>(e->dtype(), v);
}
template <typename T>
ExprPtr immLike(ExprHandle e, T v) {
return immLike(e.node(), v);
}
inline c10::optional<int64_t> intValue(ExprPtr e) {
#define TYPE_CASE(Type, Name) \
if (auto v = to<Name##Imm>(e)) { \
return v->value(); \
}
AT_FORALL_INT_TYPES(TYPE_CASE);
#undef TYPE_CASE
return c10::nullopt;
}
inline c10::optional<int64_t> intValue(ExprHandle e) {
return intValue(e.node());
}
template <typename T>
T immediateAs(ExprPtr e) {
#define TYPE_CASE(Type, Name) \
if (Name##ImmPtr imm = to<Name##Imm>(e)) { \
return imm->value(); \
}
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, TYPE_CASE);
#undef TYPE_CASE
throw unsupported_dtype();
return 0;
}
template <typename T>
T immediateAs(ExprHandle e) {
return immediateAs<T>(e.node());
}
template <typename T>
bool immediateEquals(ExprPtr e, T val) {
#define TYPE_CASE(Type, Name) \
if (Name##ImmPtr imm = to<Name##Imm>(e)) { \
return imm->value() == val; \
}
AT_FORALL_SCALAR_TYPES_AND3(Bool, Half, BFloat16, TYPE_CASE);
#undef TYPE_CASE
throw unsupported_dtype();
return false;
}
TORCH_API bool immediateIsNegative(ExprPtr e);
TORCH_API bool immediateIsPositive(ExprPtr e);
TORCH_API bool immediateIsZero(ExprPtr e);
// Represents a ramp vector node:
// [base, base + 1 * stride, ... , base + (lanes - 1) * stride]
class TORCH_API Ramp : public ExprNode<Ramp> {
public:
ExprPtr base() const {
return base_;
}
ExprPtr stride() const {
return stride_;
}
void set_base(ExprPtr base) {
base_ = base;
}
void set_stride(ExprPtr stride) {
stride_ = stride;
}
static ExprHandle make(
const ExprHandle& base,
const ExprHandle& stride,
int lanes) {
if (stride.dtype() != base.dtype()) {
throw malformed_input("Bad stride in Ramp");
}
return ExprHandle(alloc<Ramp>(base.node(), stride.node(), lanes));
}
int lanes() const {
return lanes_;
}
Ramp(ExprPtr base, ExprPtr stride, int lanes)
: ExprNodeBase(Dtype(base->dtype(), lanes)),
base_(base),
stride_(stride),
lanes_(lanes) {}
private:
ExprPtr base_;
ExprPtr stride_;
int lanes_;
};
class TORCH_API Load : public ExprNode<Load> {
public:
VarPtr base_handle() const {
return buf_->base_handle();
}
std::vector<ExprPtr> indices() const {
return indices_;
}
ExprPtr flat_index() const {
TORCH_CHECK(indices_.size() == 1, "Indices haven't been flattened.");
return indices_[0];
}
BufPtr buf() const {
return buf_;
}
void set_buf(BufPtr buf) {
buf_ = buf;
}
void set_indices(std::vector<ExprPtr> indices) {
indices_ = indices;
}
static ExprHandle make(
Dtype dtype,
const BufHandle& buf,
const std::vector<ExprHandle>& indices);
static ExprHandle make(
const BufHandle& buf,
const std::vector<ExprHandle>& indices);
Load(Dtype dtype, BufPtr base_handle, std::vector<ExprPtr> indices);
Load(BufPtr base_handle, const std::vector<ExprPtr>& indices);
private:
BufPtr buf_;
std::vector<ExprPtr> indices_;
};
class TORCH_API Broadcast : public ExprNode<Broadcast> {
public:
ExprPtr value() const {
return value_;
}
void set_value(ExprPtr value) {
value_ = value;
}
int lanes() const {
return lanes_;
}
static ExprHandle make(const ExprHandle& value, int lanes) {
return ExprHandle(alloc<Broadcast>(value.node(), lanes));
}
Broadcast(ExprPtr value, int lanes)
: ExprNodeBase(Dtype(value->dtype(), lanes)),
value_(value),
lanes_(lanes) {}
private:
ExprPtr value_;
int lanes_;
};
class TORCH_API IfThenElse : public ExprNode<IfThenElse> {
public:
ExprPtr condition() const {
return condition_;
}
// Lazily evaluated only if condition is true
ExprPtr true_value() const {
return true_;
}
// Lazily evaluated only if condition is false
ExprPtr false_value() const {
return false_;
}
void set_condition(ExprPtr condition) {
condition_ = condition;
}
void set_true_value(ExprPtr true_value) {
true_ = true_value;
}
void set_false_value(ExprPtr false_value) {
false_ = false_value;
}
static ExprHandle make(
const ExprHandle& c,
const ExprHandle& t,
const ExprHandle& f) {
if (!c.dtype().is_integral()) {
throw unsupported_dtype();
}
if (c.dtype().lanes() != 1) {
throw unsupported_dtype();
}
if (t.dtype() != f.dtype()) {
throw malformed_input("Bad dtype in IfThenElse");
}
return ExprHandle(alloc<IfThenElse>(c.node(), t.node(), f.node()));
}
IfThenElse(ExprPtr c, ExprPtr t, ExprPtr f)
: ExprNodeBase(t->dtype()), condition_(c), true_(t), false_(f) {}
private:
ExprPtr condition_;
ExprPtr true_;
ExprPtr false_;
};
class TORCH_API CompareSelect : public ExprNode<CompareSelect> {
public:
CompareSelectOperation compare_select_op() const {
return compare_op_;
}
ExprPtr lhs() const {
return this->lhs_;
}
ExprPtr rhs() const {
return this->rhs_;
}
ExprPtr ret_val1() const {
return this->ret_val1_;
}
ExprPtr ret_val2() const {
return this->ret_val2_;
}
void set_lhs(ExprPtr lhs) {
lhs_ = lhs;
}
void set_rhs(ExprPtr rhs) {
rhs_ = rhs;
}
void set_ret_val1(ExprPtr ret_val1) {
ret_val1_ = ret_val1;
}
void set_ret_val2(ExprPtr ret_val2) {
ret_val2_ = ret_val2;
}
CompareSelectBias bias() const {
return bias_;
}
static ExprHandle make(
const ExprHandle& lhs,
const ExprHandle& rhs,
CompareSelectOperation cmp_op,
CompareSelectBias bias = kUnbiased) {
if (lhs.dtype() != rhs.dtype()) {
throw malformed_input("bad dtype in CompareSelect");
}
return ExprHandle(alloc<CompareSelect>(
lhs.node(),
rhs.node(),
IntImm::make(1).node(),
IntImm::make(0).node(),
cmp_op,
bias));
}
static ExprHandle make(
const ExprHandle& lhs,
const ExprHandle& rhs,
const ExprHandle& ret_val1,
const ExprHandle& ret_val2,
CompareSelectOperation cmp_op,
CompareSelectBias bias = kUnbiased) {
if (lhs.dtype() != rhs.dtype() || ret_val1.dtype() != ret_val2.dtype()) {
throw malformed_input("bad dtype in CompareSelect");
}
return ExprHandle(alloc<CompareSelect>(
lhs.node(),
rhs.node(),
ret_val1.node(),
ret_val2.node(),
cmp_op,
bias));
}
CompareSelect(
ExprPtr lhs,
ExprPtr rhs,
ExprPtr ret_val1,
ExprPtr ret_val2,
CompareSelectOperation cmp_op,
CompareSelectBias bias = kUnbiased)
: ExprNodeBase(ret_val1->dtype()),
lhs_(lhs),
rhs_(rhs),
ret_val1_(ret_val1),
ret_val2_(ret_val2),
compare_op_(cmp_op),
bias_(bias) {}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
CompareSelect(
ExprPtr lhs,
ExprPtr rhs,
CompareSelectOperation cmp_op,
CompareSelectBias bias = kUnbiased)
: ExprNodeBase(kInt),
lhs_(lhs),
rhs_(rhs),
ret_val1_(alloc<IntImm>(1)),
ret_val2_(alloc<IntImm>(0)),
compare_op_(cmp_op),
bias_(bias) {}
private:
ExprPtr lhs_;
ExprPtr rhs_;
ExprPtr ret_val1_;
ExprPtr ret_val2_;
CompareSelectOperation compare_op_;
CompareSelectBias bias_;
};
enum IntrinsicsOp {
kSin,
kCos,
kTan,
kAsin,
kAcos,
kAtan,
kAtan2,
kSinh,
kCosh,
kTanh,
kSigmoid,
kExp,
kExpm1,
kAbs,
kLog,
kLog2,
kLog10,
kLog1p,
kErf,
kErfc,
kSqrt,
kRsqrt,
kPow,
kCeil,
kFloor,
kRound,
kTrunc,
kFmod,
kRemainder,
kLgamma,
kFrac,
kIsNan,
kRand, // We need more discussions on this. Should we consider stateful?
kMaxIntrinsicsOp,
};
class TORCH_API Intrinsics : public ExprNode<Intrinsics> {
public:
static ExprHandle make(IntrinsicsOp op_type, const ExprHandle& v1) {
return ExprHandle(alloc<Intrinsics>(op_type, v1.node()));
}
static ExprHandle make(
IntrinsicsOp op_type,
const ExprHandle& v1,
const ExprHandle& v2) {
return ExprHandle(alloc<Intrinsics>(op_type, v1.node(), v2.node()));
}
static ExprHandle make(
IntrinsicsOp op_type,
const std::vector<ExprHandle>& params) {
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
std::vector<ExprPtr> params_nodes(params.size());
for (size_t i = 0; i < params.size(); i++) {
params_nodes[i] = params[i].node();
}
return ExprHandle(alloc<Intrinsics>(op_type, params_nodes));
}
static ExprHandle make(IntrinsicsOp op_type, Dtype dtype) {
return ExprHandle(alloc<Intrinsics>(op_type, dtype));
}
IntrinsicsOp op_type() const {
return op_type_;
}
std::string func_name() const {
switch (op_type()) {
case kSin:
return "sin";
case kCos:
return "cos";
case kTan:
return "tan";
case kAsin:
return "asin";
case kAcos:
return "acos";
case kAtan:
return "atan";
case kAtan2:
return "atan2";
case kSinh:
return "sinh";
case kCosh:
return "cosh";
case kTanh:
return "tanh";
case kSigmoid:
return "sigmoid";
case kExp:
return "exp";
case kAbs:
return "abs";
case kLog:
return "log";
case kLog2:
return "log2";
case kLog10:
return "log10";
case kLog1p:
return "log1p";
case kErf:
return "erf";
case kSqrt:
return "sqrt";
case kRsqrt:
return "rsqrt";
case kPow:
return "pow";
case kCeil:
return "ceil";
case kFloor:
return "floor";
case kRound:
return "round";
case kTrunc:
return "trunc";
case kRand:
return "rand";
case kFmod:
return "fmod";
case kRemainder:
return "remainder";
case kLgamma:
return "lgamma";
case kExpm1:
return "expm1";
case kErfc:
return "erfc";
case kFrac:
return "frac";
case kIsNan:
return "isnan";
default:
throw std::runtime_error(
"invalid op_type: " + c10::to_string(op_type()));
}
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
Intrinsics(IntrinsicsOp op_type, Dtype dtype)
: ExprNodeBase(IntrinsicsDtype(op_type, dtype)),
params_({}),
op_type_(op_type) {
if (OpArgCount(op_type) != 0) {
throw malformed_input("bad arg count in Intrinsics");
}
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
Intrinsics(IntrinsicsOp op_type, ExprPtr v1)
: ExprNodeBase(IntrinsicsDtype(op_type, v1->dtype())),
params_({v1}),
op_type_(op_type) {
if (OpArgCount(op_type) != 1) {
throw malformed_input("bad arg count in Intrinsics");
}
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
Intrinsics(IntrinsicsOp op_type, ExprPtr v1, ExprPtr v2)
: ExprNodeBase(IntrinsicsDtype(op_type, v1->dtype(), v2->dtype())),
params_({v1, v2}),
op_type_(op_type) {
if (OpArgCount(op_type) != 2) {
throw malformed_input("bad arg count in Intrinsics");
}
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
Intrinsics(IntrinsicsOp op_type, const std::vector<ExprPtr>& params)
: ExprNodeBase(IntrinsicsDtype(op_type, params)),
params_(params),
op_type_(op_type) {
if (OpArgCount(op_type) != nparams()) {
throw malformed_input("bad arg count in Intrinsics");
}
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
Intrinsics(
IntrinsicsOp op_type,
Dtype dtype,
const std::vector<ExprPtr>& params)
: ExprNodeBase(IntrinsicsDtype(op_type, dtype)),
params_(params),
op_type_(op_type) {
if (OpArgCount(op_type) != nparams()) {
throw malformed_input("bad arg count in Intrinsics");
}
}
bool isPure() const {
return op_type_ != kRand;
}
int nparams() const {
return params_.size();
}
ExprPtr param(int index) const {
return params_[index];
}
const std::vector<ExprPtr>& params() const {
return params_;
}
void set_params(std::vector<ExprPtr> params) {
params_ = std::move(params);
}
static int OpArgCount(IntrinsicsOp op_type);
private:
static Dtype IntrinsicsDtype(IntrinsicsOp op_type, Dtype dt1);
static Dtype IntrinsicsDtype(IntrinsicsOp op_type, Dtype dt1, Dtype dt2);
static Dtype IntrinsicsDtype(
IntrinsicsOp op_type,
const std::vector<ExprPtr>& params);
std::vector<ExprPtr> params_;
IntrinsicsOp op_type_;
};
TORCH_API std::vector<ExprPtr> ExprHandleVectorToExprVector(
const std::vector<ExprHandle>&);
TORCH_API std::vector<ExprHandle> ExprVectorToExprHandleVector(
const std::vector<ExprPtr>&);
TORCH_API std::vector<VarPtr> VarHandleVectorToVarVector(
const std::vector<VarHandle>&);
TORCH_API std::vector<VarHandle> VarVectorToVarHandleVector(
const std::vector<VarPtr>&);
TORCH_API ExprPtr flatten_index(
const std::vector<ExprPtr>& dims,
const std::vector<ExprPtr>& indices,
const std::vector<ExprPtr>& strides);
} // namespace tensorexpr
} // namespace jit
} // namespace torch
|