expr.py 15.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
"""Expression AST Node in TVM.

User do not need to deal with expression AST node directly.
But they can be helpful for developer to do quick proptyping.
While not displayed in the document and python file.
Each expression node have subfields that can be visited from python side.

For example, you can use addexp.a to get the left operand of an Add node.

.. code-block:: python

  x = tvm.var("n")
  y = x + 2
  assert(isinstance(y, tvm.expr.Add))
  assert(y.a == x)
"""
17
# pylint: disable=missing-docstring
tqchen committed
18
from __future__ import absolute_import as _abs
19
from ._ffi.node import NodeBase, NodeGeneric, register_node
tqchen committed
20
from . import make as _make
21
from . import generic as _generic
22
from . import _api_internal
tqchen committed
23

24

25
class ExprOp(object):
tqchen committed
26
    def __add__(self, other):
27
        return _generic.add(self, other)
tqchen committed
28 29 30 31 32

    def __radd__(self, other):
        return self.__add__(other)

    def __sub__(self, other):
33
        return _generic.subtract(self, other)
tqchen committed
34 35

    def __rsub__(self, other):
36
        return _generic.subtract(other, self)
tqchen committed
37 38

    def __mul__(self, other):
39
        return _generic.multiply(self, other)
tqchen committed
40 41

    def __rmul__(self, other):
42
        return _generic.multiply(other, self)
tqchen committed
43 44

    def __div__(self, other):
45
        return _generic.divide(self, other)
tqchen committed
46 47

    def __rdiv__(self, other):
48
        return _generic.divide(other, self)
tqchen committed
49 50 51 52 53 54 55

    def __truediv__(self, other):
        return self.__div__(other)

    def __rtruediv__(self, other):
        return self.__rdiv__(other)

56 57 58 59 60 61
    def __floordiv__(self, other):
        return self.__div__(other)

    def __rfloordiv__(self, other):
        return self.__rdiv__(other)

62
    def __mod__(self, other):
63
        return _make._OpMod(self, other)
64

tqchen committed
65
    def __neg__(self):
66 67
        neg_one = _api_internal._const(-1, self.dtype)
        return self.__mul__(neg_one)
tqchen committed
68

69
    def __lshift__(self, other):
70
        return _make.left_shift(self, other)
71 72

    def __rshift__(self, other):
73
        return _make.right_shift(self, other)
74 75

    def __and__(self, other):
76
        return _make.bitwise_and(self, other)
77 78

    def __or__(self, other):
79
        return _make.bitwise_or(self, other)
80 81

    def __xor__(self, other):
82
        return _make.bitwise_xor(self, other)
83 84 85 86

    def __invert__(self):
        return _make.Call(self.dtype, "bitwise_not", [self], Call.PureIntrinsic, None, 0)

87
    def __lt__(self, other):
88
        return _make._OpLT(self, other)
89 90

    def __le__(self, other):
91
        return _make._OpLE(self, other)
92 93

    def __eq__(self, other):
94
        return EqualOp(self, other)
95 96

    def __ne__(self, other):
97
        return NotEqualOp(self, other)
98 99

    def __gt__(self, other):
100
        return _make._OpGT(self, other)
101 102

    def __ge__(self, other):
103
        return _make._OpGE(self, other)
104

105 106 107 108 109 110 111
    def __nonzero__(self):
        raise ValueError("Cannot use and / or / not operator to Expr, hint: " +
                         "use tvm.all / tvm.any instead")

    def __bool__(self):
        return self.__nonzero__()

112 113 114 115 116 117 118 119 120 121 122 123 124
    def equal(self, other):
        """Build an equal check expression with other expr.

        Parameters
        ----------
        other : Expr
            The other expression

        Returns
        -------
        ret : Expr
            The equality expression.
        """
125
        return _make._OpEQ(self, other)
126

ziheng committed
127
    def astype(self, dtype):
128 129
        """Cast the expression to other type.

ziheng committed
130 131
        Parameters
        ----------
132
        dtype : str
ziheng committed
133 134 135 136 137 138 139
            The type of new expression

        Returns
        -------
        expr : Expr
            Expression with new type
        """
140
        return _generic.cast(self, dtype)
ziheng committed
141

tqchen committed
142

143 144 145 146 147 148 149 150 151 152 153 154 155 156
class EqualOp(NodeGeneric, ExprOp):
    """Deferred equal operator.

    This is used to support sugar that a == b can either
    mean NodeBase.same_as or NodeBase.equal.

    Parameters
    ----------
    a : Expr
        Left operand.

    b : Expr
        Right operand.
    """
157 158 159
    # This class is not manipulated by C++. So use python's identity check function is sufficient
    same_as = object.__eq__

160 161 162 163 164 165 166 167 168 169 170 171
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def __nonzero__(self):
        return self.a.same_as(self.b)

    def __bool__(self):
        return self.__nonzero__()

    def asnode(self):
        """Convert node."""
172
        return _make._OpEQ(self.a, self.b)
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188


class NotEqualOp(NodeGeneric, ExprOp):
    """Deferred NE operator.

    This is used to support sugar that a != b can either
    mean not NodeBase.same_as or make.NE.

    Parameters
    ----------
    a : Expr
        Left operand.

    b : Expr
        Right operand.
    """
189 190 191
    # This class is not manipulated by C++. So use python's identity check function is sufficient
    same_as = object.__eq__

192 193 194 195 196 197 198 199 200 201 202 203
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def __nonzero__(self):
        return not self.a.same_as(self.b)

    def __bool__(self):
        return self.__nonzero__()

    def asnode(self):
        """Convert node."""
204
        return _make._OpNE(self.a, self.b)
205 206


207
class Expr(ExprOp, NodeBase):
208
    """Base class of all tvm Expressions"""
209 210 211
    # In Python3, We have to explicity tell interpreter to retain __hash__ if we overide __eq__
    # https://docs.python.org/3.1/reference/datamodel.html#object.__hash__
    __hash__ = NodeBase.__hash__
tqchen committed
212

213

tqchen committed
214 215 216 217 218 219 220 221 222 223 224 225
class ConstExpr(Expr):
    pass

class BinaryOpExpr(Expr):
    pass

class CmpExpr(Expr):
    pass

class LogicalExpr(Expr):
    pass

tqchen committed
226 227
@register_node("Variable")
class Var(Expr):
228 229 230 231 232 233 234 235 236 237 238 239 240 241
    """Symbolic variable.

    Parameters
    ----------
    name : str
        The name

    dtype : int
        The data type
    """
    def __init__(self, name, dtype):
        self.__init_handle_by_constructor__(
            _api_internal._Var, name, dtype)

tqchen committed
242

tqchen committed
243 244
@register_node
class Reduce(Expr):
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    """Reduce node.

    Parameters
    ----------
    combiner : CommReducer
        The combiner.

    src : list of Expr
        The source expression.

    rdom : list of IterVar
        The iteration domain

    condition : Expr
        The reduce condition.

    value_index : int
        The value index.
    """
    def __init__(self, combiner, src, rdom, condition, value_index):
        self.__init_handle_by_constructor__(
            _make.Reduce, combiner, src, rdom,
            condition, value_index)

tqchen committed
269

tqchen committed
270 271
@register_node
class FloatImm(ConstExpr):
272 273 274 275 276 277 278 279 280 281 282 283 284
    """Float constant.

    Parameters
    ----------
    dtype : str
        The data type

    value : float
        The constant value.
    """
    def __init__(self, dtype, value):
        self.__init_handle_by_constructor__(
            _make.FloatImm, dtype, value)
tqchen committed
285 286 287

@register_node
class IntImm(ConstExpr):
288 289 290 291 292 293 294 295 296 297 298 299 300 301
    """Int constant.

    Parameters
    ----------
    dtype : str
        The data type

    value : int
        The constant value.
    """
    def __init__(self, dtype, value):
        self.__init_handle_by_constructor__(
            _make.IntImm, dtype, value)

302 303 304
    def __int__(self):
        return self.value

tqchen committed
305 306 307

@register_node
class UIntImm(ConstExpr):
308 309 310 311 312 313 314 315 316 317 318 319 320 321
    """UInt constant.

    Parameters
    ----------
    dtype : str
        The data type

    value : int
        The constant value.
    """
    def __init__(self, dtype, value):
        self.__init_handle_by_constructor__(
            _make.UIntImm, dtype, value)

tqchen committed
322 323 324

@register_node
class StringImm(ConstExpr):
325 326 327 328 329 330 331 332 333 334 335
    """String constant.

    Parameters
    ----------
    value : str
        The value of the function.
    """
    def __init__(self, value):
        self.__init_handle_by_constructor__(
            _make.StringImm, value)

tqchen committed
336 337 338

@register_node
class Cast(Expr):
339 340 341 342 343 344 345 346 347 348 349 350 351 352
    """Cast expression.

    Parameters
    ----------
    dtype : str
        The data type

    value : Expr
        The value of the function.
    """
    def __init__(self, dtype, value):
        self.__init_handle_by_constructor__(
            _make.Cast, dtype, value)

tqchen committed
353 354 355

@register_node
class Add(BinaryOpExpr):
356 357 358 359 360 361 362 363 364 365 366 367 368 369
    """Add node.

    Parameters
    ----------
    a : Expr
        The left hand operand.

    b : Expr
        The right hand operand.
    """
    def __init__(self, a, b):
        self.__init_handle_by_constructor__(
            _make.Add, a, b)

tqchen committed
370 371 372

@register_node
class Sub(BinaryOpExpr):
373 374 375 376 377 378 379 380 381 382 383 384 385 386
    """Sub node.

    Parameters
    ----------
    a : Expr
        The left hand operand.

    b : Expr
        The right hand operand.
    """
    def __init__(self, a, b):
        self.__init_handle_by_constructor__(
            _make.Sub, a, b)

tqchen committed
387 388 389

@register_node
class Mul(BinaryOpExpr):
390 391 392 393 394 395 396 397 398 399 400 401 402 403
    """Mul node.

    Parameters
    ----------
    a : Expr
        The left hand operand.

    b : Expr
        The right hand operand.
    """
    def __init__(self, a, b):
        self.__init_handle_by_constructor__(
            _make.Mul, a, b)

tqchen committed
404 405 406

@register_node
class Div(BinaryOpExpr):
407 408 409 410 411 412 413 414 415 416 417 418 419 420
    """Div node.

    Parameters
    ----------
    a : Expr
        The left hand operand.

    b : Expr
        The right hand operand.
    """
    def __init__(self, a, b):
        self.__init_handle_by_constructor__(
            _make.Div, a, b)

tqchen committed
421 422 423

@register_node
class Mod(BinaryOpExpr):
424 425 426 427 428 429 430 431 432 433 434 435 436 437
    """Mod node.

    Parameters
    ----------
    a : Expr
        The left hand operand.

    b : Expr
        The right hand operand.
    """
    def __init__(self, a, b):
        self.__init_handle_by_constructor__(
            _make.Mod, a, b)

tqchen committed
438 439 440

@register_node
class Min(BinaryOpExpr):
441 442 443 444 445 446 447 448 449 450 451 452 453 454
    """Min node.

    Parameters
    ----------
    a : Expr
        The left hand operand.

    b : Expr
        The right hand operand.
    """
    def __init__(self, a, b):
        self.__init_handle_by_constructor__(
            _make.Min, a, b)

tqchen committed
455 456 457

@register_node
class Max(BinaryOpExpr):
458 459 460 461 462 463 464 465 466 467 468 469 470 471
    """Max node.

    Parameters
    ----------
    a : Expr
        The left hand operand.

    b : Expr
        The right hand operand.
    """
    def __init__(self, a, b):
        self.__init_handle_by_constructor__(
            _make.Max, a, b)

tqchen committed
472 473 474

@register_node
class EQ(CmpExpr):
475 476 477 478 479 480 481 482 483 484 485 486 487 488
    """EQ node.

    Parameters
    ----------
    a : Expr
        The left hand operand.

    b : Expr
        The right hand operand.
    """
    def __init__(self, a, b):
        self.__init_handle_by_constructor__(
            _make.EQ, a, b)

tqchen committed
489 490 491

@register_node
class NE(CmpExpr):
492 493 494 495 496 497 498 499 500 501 502 503 504 505
    """NE node.

    Parameters
    ----------
    a : Expr
        The left hand operand.

    b : Expr
        The right hand operand.
    """
    def __init__(self, a, b):
        self.__init_handle_by_constructor__(
            _make.NE, a, b)

tqchen committed
506 507 508

@register_node
class LT(CmpExpr):
509 510 511 512 513 514 515 516 517 518 519 520 521 522
    """LT node.

    Parameters
    ----------
    a : Expr
        The left hand operand.

    b : Expr
        The right hand operand.
    """
    def __init__(self, a, b):
        self.__init_handle_by_constructor__(
            _make.LT, a, b)

tqchen committed
523 524 525

@register_node
class LE(CmpExpr):
526 527 528 529 530 531 532 533 534 535 536 537 538 539
    """LE node.

    Parameters
    ----------
    a : Expr
        The left hand operand.

    b : Expr
        The right hand operand.
    """
    def __init__(self, a, b):
        self.__init_handle_by_constructor__(
            _make.LE, a, b)

tqchen committed
540 541 542

@register_node
class GT(CmpExpr):
543 544 545 546 547 548 549 550 551 552 553 554 555 556
    """GT node.

    Parameters
    ----------
    a : Expr
        The left hand operand.

    b : Expr
        The right hand operand.
    """
    def __init__(self, a, b):
        self.__init_handle_by_constructor__(
            _make.GT, a, b)

tqchen committed
557 558 559

@register_node
class GE(CmpExpr):
560 561 562 563 564 565 566 567 568 569 570 571 572 573
    """GE node.

    Parameters
    ----------
    a : Expr
        The left hand operand.

    b : Expr
        The right hand operand.
    """
    def __init__(self, a, b):
        self.__init_handle_by_constructor__(
            _make.GE, a, b)

tqchen committed
574 575 576

@register_node
class And(LogicalExpr):
577 578 579 580 581 582 583 584 585 586 587 588 589 590
    """And node.

    Parameters
    ----------
    a : Expr
        The left hand operand.

    b : Expr
        The right hand operand.
    """
    def __init__(self, a, b):
        self.__init_handle_by_constructor__(
            _make.And, a, b)

tqchen committed
591 592 593

@register_node
class Or(LogicalExpr):
594 595 596 597 598 599 600 601 602 603 604 605 606 607
    """Or node.

    Parameters
    ----------
    a : Expr
        The left hand operand.

    b : Expr
        The right hand operand.
    """
    def __init__(self, a, b):
        self.__init_handle_by_constructor__(
            _make.Or, a, b)

tqchen committed
608 609 610

@register_node
class Not(LogicalExpr):
611 612 613 614 615 616 617 618 619 620 621
    """Not node.

    Parameters
    ----------
    a : Expr
        The input value
    """
    def __init__(self, a):
        self.__init_handle_by_constructor__(
            _make.Not, a)

tqchen committed
622 623 624

@register_node
class Select(Expr):
625 626
    """Select node.

627 628 629 630 631 632 633
    Note
    ----
    Select may compute both true_value and false_value.
    Use :any:`tvm.if_then_else` instead if you want to
    get a conditional expression that only evaluates
    the correct branch.

634 635 636 637 638 639 640 641 642 643
    Parameters
    ----------
    condition : Expr
        The condition expression.

    true_value : Expr
        The value to take when condition is true.

    false_value : Expr
        The value to take when condition is false.
644

645 646 647 648 649
    """
    def __init__(self, condition, true_value, false_value):
        self.__init_handle_by_constructor__(
            _make.Select, condition, true_value, false_value)

tqchen committed
650 651 652

@register_node
class Load(Expr):
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
    """Load node.

    Parameters
    ----------
    dtype : str
        The data type.

    buffer_var : Var
        The buffer variable in the load expression.

    index : Expr
        The index in the load.

    predicate : Expr
        The load predicate.
    """
    def __init__(self, dtype, buffer_var, index, predicate):
        self.__init_handle_by_constructor__(
            _make.Load, dtype, buffer_var, index, predicate)

tqchen committed
673 674 675

@register_node
class Ramp(Expr):
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
    """Ramp node.

    Parameters
    ----------
    base : Expr
        The base expression.

    stride : ramp stride
        The stride of the ramp.

    lanes : int
        The lanes of the expression.
    """
    def __init__(self, base, stride, lanes):
        self.__init_handle_by_constructor__(
            _make.Ramp, base, stride, lanes)

tqchen committed
693

tqchen committed
694 695
@register_node
class Broadcast(Expr):
696 697 698 699 700 701 702 703 704 705 706 707 708 709
    """Broadcast node.

    Parameters
    ----------
    value : Expr
        The value of the expression.

    lanes : int
        The lanes of the expression.
    """
    def __init__(self, value, lanes):
        self.__init_handle_by_constructor__(
            _make.Broadcast, value, lanes)

710

tqchen committed
711
@register_node
712
class Shuffle(Expr):
713 714 715 716 717 718 719 720 721 722 723 724 725 726
    """Shuffle node.

    Parameters
    ----------
    vectors : Array of Expr
        The vectors

    indices : Array of indices
        The indices
    """
    def __init__(self, vectors, indices):
        self.__init_handle_by_constructor__(
            _make.Shuffle, vectors, indices)

727 728

@register_node
tqchen committed
729
class Call(Expr):
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
    """Call node.

    Parameters
    ----------
    dtype : str
        The return data type

    name : str
        The name of the function

    args : list of Expr
        The input arguments to the call

    call_type : int
        The type of the call

    func : Operation, optional
        Operation if call_type is Halide

    value_index : int
        The output value index
    """
tqchen committed
752 753 754 755 756 757
    Extern = 0
    ExternCPlusPlus = 1
    PureExtern = 2
    Halide = 3
    Intrinsic = 4
    PureIntrinsic = 5
758 759 760
    def __init__(self, dtype, name, args, call_type, func, value_index):
        self.__init_handle_by_constructor__(
            _make.Call, dtype, name, args, call_type, func, value_index)
761

762

tqchen committed
763 764
@register_node
class Let(Expr):
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
    """Let node.

    Parameters
    ----------
    var : Var
        The variable in the binding.

    value : Expr
        The value in to be binded.

    body : Expr
        The body expression.
    """
    def __init__(self, var, value, body):
        self.__init_handle_by_constructor__(
            _make.Let, var, value, body)