Elementwise
Elementwise ops operate on a per element basis. They don't change the shape of the tensor.
Unary Ops (math)¤
logical_not
¤
logical_not() -> Self
Computes the logical NOT of the tensor element-wise.
print(Tensor([False, True]).logical_not().numpy())
[ True False]
Source code in tinygrad/mixin/elementwise.py
32 33 34 35 36 37 38 39 40 | |
neg
¤
neg() -> Self
Negates the tensor element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).neg().numpy())
[ 3. 2. 1. -0. -1. -2. -3.]
Source code in tinygrad/mixin/elementwise.py
50 51 52 53 54 55 56 57 58 | |
log
¤
log() -> Self
Computes the natural logarithm element-wise.
See: https://en.wikipedia.org/wiki/Logarithm
print(Tensor([1., 2., 4., 8.]).log().numpy())
[0. 0.6931 1.3863 2.0794]
Source code in tinygrad/mixin/elementwise.py
747 748 749 750 751 752 753 754 755 756 757 | |
log2
¤
log2() -> Self
Computes the base-2 logarithm element-wise.
See: https://en.wikipedia.org/wiki/Logarithm
print(Tensor([1., 2., 4., 8.]).log2().numpy())
[0. 1. 2. 3.]
Source code in tinygrad/mixin/elementwise.py
445 446 447 448 449 450 451 452 453 454 455 | |
log10
¤
log10() -> Self
Computes the base-10 logarithm element-wise.
See: https://en.wikipedia.org/wiki/Logarithm
print(Tensor([1., 2., 4., 8.]).log10().numpy())
[0. 0.301 0.6021 0.9031]
Source code in tinygrad/mixin/elementwise.py
759 760 761 762 763 764 765 766 767 768 769 | |
exp
¤
exp() -> Self
Computes the exponential function element-wise.
See: https://en.wikipedia.org/wiki/Exponential_function
print(Tensor([0., 1., 2., 3.]).exp().numpy())
[ 1. 2.7183 7.3891 20.0855]
Source code in tinygrad/mixin/elementwise.py
431 432 433 434 435 436 437 438 439 440 441 442 443 | |
exp2
¤
exp2() -> Self
Computes the base-2 exponential function element-wise.
See: https://en.wikipedia.org/wiki/Exponential_function
print(Tensor([0., 1., 2., 3.]).exp2().numpy())
[1. 2. 4. 8.]
Source code in tinygrad/mixin/elementwise.py
457 458 459 460 461 462 463 464 465 466 467 | |
sqrt
¤
sqrt() -> Self
Computes the square root of the tensor element-wise.
print(Tensor([1., 2., 3., 4.]).sqrt().numpy())
[1. 1.4142 1.7321 2. ]
Source code in tinygrad/mixin/elementwise.py
400 401 402 403 404 405 406 407 408 | |
rsqrt
¤
rsqrt() -> Self
Computes the reciprocal of the square root of the tensor element-wise.
print(Tensor([1., 2., 3., 4.]).rsqrt().numpy())
[1. 0.7071 0.5774 0.5 ]
Source code in tinygrad/mixin/elementwise.py
737 738 739 740 741 742 743 744 745 | |
sin
¤
sin() -> Self
Computes the sine of the tensor element-wise.
print(Tensor([0., math.pi/2, math.pi, 3*math.pi/2, 2*math.pi]).sin().numpy())
[ 0. 1. -0. -1. 0.]
Source code in tinygrad/mixin/elementwise.py
410 411 412 413 414 415 416 417 418 | |
cos
¤
cos() -> Self
Computes the cosine of the tensor element-wise.
print(Tensor([0., math.pi/2, math.pi, 3*math.pi/2, 2*math.pi]).cos().numpy())
[ 1.0000e+00 0.0000e+00 -1.0000e+00 -2.3842e-07 1.0000e+00]
Source code in tinygrad/mixin/elementwise.py
420 421 422 423 424 425 426 427 428 429 | |
tan
¤
tan() -> Self
Computes the tangent of the tensor element-wise.
print(Tensor([0., math.pi/4, math.pi/2, 3*math.pi/4, math.pi]).tan().numpy())
[ 0. 1. inf -1. 0.]
Source code in tinygrad/mixin/elementwise.py
837 838 839 840 841 842 843 844 845 | |
asin
¤
asin() -> Self
Computes the inverse sine (arcsine) of the tensor element-wise.
print(Tensor([-0.9, -0.6, -0.3, 0., 0.3, 0.6, 0.9]).asin().numpy())
[-1.1198 -0.6435 -0.3047 0. 0.3047 0.6435 1.1198]
Source code in tinygrad/mixin/elementwise.py
847 848 849 850 851 852 853 854 855 856 857 858 | |
acos
¤
acos() -> Self
Computes the inverse cosine (arccosine) of the tensor element-wise.
print(Tensor([-0.9, -0.6, -0.3, 0., 0.3, 0.6, 0.9]).acos().numpy())
[2.6906 2.2143 1.8755 1.5708 1.2661 0.9273 0.451 ]
Source code in tinygrad/mixin/elementwise.py
860 861 862 863 864 865 866 867 868 | |
atan
¤
atan() -> Self
Computes the inverse tangent (arctan) of the tensor element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).atan().numpy())
[-1.249 -1.1071 -0.7854 0. 0.7854 1.1071 1.249 ]
Source code in tinygrad/mixin/elementwise.py
870 871 872 873 874 875 876 877 878 | |
trunc
¤
trunc() -> Self
Truncates the tensor element-wise.
print(Tensor([-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5]).trunc().numpy())
[-3. -2. -1. -0. 0. 1. 2. 3.]
Source code in tinygrad/mixin/elementwise.py
390 391 392 393 394 395 396 397 398 | |
ceil
¤
ceil() -> Self
Rounds the tensor element-wise towards positive infinity.
print(Tensor([-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5]).ceil().numpy())
[-3. -2. -1. -0. 1. 2. 3. 4.]
Source code in tinygrad/mixin/elementwise.py
576 577 578 579 580 581 582 583 584 | |
floor
¤
floor() -> Self
Rounds the tensor element-wise towards negative infinity.
print(Tensor([-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5]).floor().numpy())
[-4. -3. -2. -1. 0. 1. 2. 3.]
Source code in tinygrad/mixin/elementwise.py
586 587 588 589 590 591 592 593 594 | |
round
¤
round() -> Self
Rounds the tensor element-wise with rounding half to even.
print(Tensor([-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5]).round().numpy())
[-4. -2. -2. 0. 0. 2. 2. 4.]
Source code in tinygrad/mixin/elementwise.py
807 808 809 810 811 812 813 814 815 | |
isinf
¤
Checks the tensor element-wise to return True where the element is infinity, otherwise returns False
print(Tensor([1, float('inf'), 2, float('-inf'), float('nan')]).isinf().numpy())
[False True False True False]
Source code in tinygrad/mixin/elementwise.py
536 537 538 539 540 541 542 543 544 | |
isnan
¤
isnan() -> Self
Checks the tensor element-wise to return True where the element is NaN, otherwise returns False
print(Tensor([1, float('inf'), 2, float('-inf'), float('nan')]).isnan().numpy())
[False False False False True]
Source code in tinygrad/mixin/elementwise.py
526 527 528 529 530 531 532 533 534 | |
isfinite
¤
isfinite() -> Self
Checks the tensor element-wise to return True where the element is finite, otherwise returns False
print(Tensor([1, float('inf'), 2, float('-inf'), float('nan')]).isfinite().numpy())
[ True False True False False]
Source code in tinygrad/mixin/elementwise.py
546 547 548 549 550 551 552 553 554 | |
lerp
¤
Linearly interpolates between self and end by weight.
print(Tensor([1., 2., 3.]).lerp(Tensor([4., 5., 6.]), 0.5).numpy())
[2.5 3.5 4.5]
Source code in tinygrad/mixin/elementwise.py
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 | |
square
¤
square() -> Self
Squares the tensor element-wise.
Equivalent to self*self.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).square().numpy())
[9. 4. 1. 0. 1. 4. 9.]
Source code in tinygrad/mixin/elementwise.py
498 499 500 501 502 503 504 505 506 507 | |
clamp
¤
clamp(min_=None, max_=None) -> Self
Clips (clamps) the values in the tensor between min_ and max_ element-wise.
If min_ is None, there is no lower bound. If max_ is None, there is no upper bound.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).clip(-1, 1).numpy())
[-1. -1. -1. 0. 1. 1. 1.]
Source code in tinygrad/mixin/elementwise.py
509 510 511 512 513 514 515 516 517 518 519 520 | |
clip
¤
clip(min_=None, max_=None) -> Self
Alias for Tensor.clamp.
Source code in tinygrad/mixin/elementwise.py
522 523 524 | |
sign
¤
sign() -> Self
Returns the sign of the tensor element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).sign().numpy())
[-1. -1. -1. 0. 1. 1. 1.]
Source code in tinygrad/mixin/elementwise.py
817 818 819 820 821 822 823 824 825 | |
abs
¤
abs() -> Self
Computes the absolute value of the tensor element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).abs().numpy())
[3. 2. 1. 0. 1. 2. 3.]
Source code in tinygrad/mixin/elementwise.py
827 828 829 830 831 832 833 834 835 | |
reciprocal
¤
reciprocal() -> Self
Computes 1/x element-wise.
print(Tensor([1., 2., 3., 4.]).reciprocal().numpy())
[1. 0.5 0.3333 0.25 ]
Source code in tinygrad/mixin/elementwise.py
380 381 382 383 384 385 386 387 388 | |
Unary Ops (activation)¤
relu
¤
relu() -> Self
Applies the Rectified Linear Unit (ReLU) function element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).relu().numpy())
[0. 0. 0. 0. 1. 2. 3.]
Source code in tinygrad/mixin/elementwise.py
596 597 598 599 600 601 602 603 604 605 | |
sigmoid
¤
sigmoid() -> Self
Applies the Sigmoid function element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).sigmoid().numpy())
[0.0474 0.1192 0.2689 0.5 0.7311 0.8808 0.9526]
Source code in tinygrad/mixin/elementwise.py
607 608 609 610 611 612 613 614 615 616 617 | |
logsigmoid
¤
logsigmoid() -> Self
Applies the LogSigmoid function element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).logsigmoid().numpy())
[-3.0486 -2.1269 -1.3133 -0.6931 -0.3133 -0.1269 -0.0486]
Source code in tinygrad/mixin/elementwise.py
938 939 940 941 942 943 944 945 946 947 948 | |
hardsigmoid
¤
Applies the Hardsigmoid function element-wise.
NOTE: default alpha and beta values are taken from torch
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).hardsigmoid().numpy())
[0. 0.1667 0.3333 0.5 0.6667 0.8333 1. ]
Source code in tinygrad/mixin/elementwise.py
643 644 645 646 647 648 649 650 651 652 653 654 | |
elu
¤
elu(alpha=1.0) -> Self
Applies the Exponential Linear Unit (ELU) function element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).elu().numpy())
[-0.9502 -0.8647 -0.6321 0. 1. 2. 3. ]
Source code in tinygrad/mixin/elementwise.py
880 881 882 883 884 885 886 887 888 889 890 | |
celu
¤
celu(alpha=1.0) -> Self
Applies the Continuously differentiable Exponential Linear Unit (CELU) function element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).celu().numpy())
[-0.9502 -0.8647 -0.6321 0. 1. 2. 3. ]
Source code in tinygrad/mixin/elementwise.py
892 893 894 895 896 897 898 899 900 901 902 | |
selu
¤
selu(alpha=1.67326, gamma=1.0507) -> Self
Applies the Scaled Exponential Linear Unit (SELU) function element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).selu().numpy())
[-1.6706 -1.5202 -1.1113 0. 1.0507 2.1014 3.1521]
Source code in tinygrad/mixin/elementwise.py
904 905 906 907 908 909 910 911 912 913 914 | |
swish
¤
swish() -> Self
See .silu()
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).swish().numpy())
[-0.1423 -0.2384 -0.2689 0. 0.7311 1.7616 2.8577]
Source code in tinygrad/mixin/elementwise.py
713 714 715 716 717 718 719 720 721 722 723 | |
silu
¤
silu() -> Self
Applies the Sigmoid Linear Unit (SiLU) function element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).silu().numpy())
[-0.1423 -0.2384 -0.2689 0. 0.7311 1.7616 2.8577]
Source code in tinygrad/mixin/elementwise.py
725 726 727 728 729 730 731 732 733 734 735 | |
relu6
¤
relu6() -> Self
Applies the ReLU6 function element-wise.
print(Tensor([-9., -6., -3., 0., 3., 6., 9.]).relu6().numpy())
[0. 0. 0. 0. 3. 6. 6.]
Source code in tinygrad/mixin/elementwise.py
619 620 621 622 623 624 625 626 627 628 629 | |
hardswish
¤
hardswish() -> Self
Applies the Hardswish function element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).hardswish().numpy())
[-0. -0.3333 -0.3333 0. 0.6667 1.6667 3. ]
Source code in tinygrad/mixin/elementwise.py
631 632 633 634 635 636 637 638 639 640 641 | |
tanh
¤
tanh() -> Self
Applies the Hyperbolic Tangent (tanh) function element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).tanh().numpy())
[-0.9951 -0.964 -0.7616 0. 0.7616 0.964 0.9951]
Source code in tinygrad/mixin/elementwise.py
679 680 681 682 683 684 685 686 687 688 689 | |
sinh
¤
sinh() -> Self
Applies the Hyperbolic Sine (sinh) function element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).sinh().numpy())
[-10.0179 -3.6269 -1.1752 0. 1.1752 3.6269 10.0179]
Source code in tinygrad/mixin/elementwise.py
950 951 952 953 954 955 956 957 958 959 960 | |
cosh
¤
cosh() -> Self
Applies the Hyperbolic Cosine (cosh) function element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).cosh().numpy())
[10.0677 3.7622 1.5431 1. 1.5431 3.7622 10.0677]
Source code in tinygrad/mixin/elementwise.py
962 963 964 965 966 967 968 969 970 971 972 | |
atanh
¤
atanh() -> Self
Applies the Inverse Hyperbolic Tangent (atanh) function element-wise.
print(Tensor([-0.9, -0.6, -0.3, 0., 0.3, 0.6, 0.9]).atanh().numpy())
[-1.4722 -0.6931 -0.3095 0. 0.3095 0.6931 1.4722]
Source code in tinygrad/mixin/elementwise.py
771 772 773 774 775 776 777 778 779 780 781 | |
asinh
¤
asinh() -> Self
Applies the Inverse Hyperbolic Sine (asinh) function element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).asinh().numpy())
[-1.8184 -1.4436 -0.8814 0. 0.8814 1.4436 1.8184]
Source code in tinygrad/mixin/elementwise.py
783 784 785 786 787 788 789 790 791 792 793 | |
acosh
¤
acosh() -> Self
Applies the Inverse Hyperbolic Cosine (acosh) function element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).acosh().numpy())
[ nan nan nan nan 0. 1.317 1.7627]
Source code in tinygrad/mixin/elementwise.py
795 796 797 798 799 800 801 802 803 804 805 | |
hardtanh
¤
hardtanh(min_val=-1, max_val=1) -> Self
Applies the Hardtanh function element-wise.
print(Tensor([-1.5, -1.0, -0.5, 0., 0.5, 1.0, 1.5]).hardtanh().numpy())
[-1. -1. -0.5 0. 0.5 1. 1. ]
Source code in tinygrad/mixin/elementwise.py
656 657 658 659 660 661 662 663 664 | |
erf
¤
erf() -> Self
Applies error function element-wise.
- Described: https://en.wikipedia.org/wiki/Error_function
print(Tensor([-1.5, -1.0, -0.5, 0., 0.5, 1.0, 1.5]).erf().numpy())
[-0.9661 -0.8427 -0.5205 0. 0.5205 0.8427 0.9661]
Source code in tinygrad/mixin/elementwise.py
974 975 976 977 978 979 980 981 982 983 984 985 986 | |
gelu
¤
gelu() -> Self
Applies the Gaussian Error Linear Unit (GELU) function element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).gelu().numpy())
[-0.0036 -0.0454 -0.1588 0. 0.8412 1.9546 2.9964]
Source code in tinygrad/mixin/elementwise.py
701 702 703 704 705 706 707 708 709 710 711 | |
quick_gelu
¤
quick_gelu() -> Self
Applies the Sigmoid GELU approximation element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).quick_gelu().numpy())
[-0.0181 -0.0643 -0.1542 0. 0.8458 1.9357 2.9819]
Source code in tinygrad/mixin/elementwise.py
691 692 693 694 695 696 697 698 699 | |
leaky_relu
¤
leaky_relu(neg_slope=0.01) -> Self
Applies the Leaky ReLU function element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).leaky_relu().numpy())
[-0.03 -0.02 -0.01 0. 1. 2. 3. ]
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).leaky_relu(neg_slope=0.42).numpy())
[-1.26 -0.84 -0.42 0. 1. 2. 3. ]
Source code in tinygrad/mixin/elementwise.py
666 667 668 669 670 671 672 673 674 675 676 677 | |
mish
¤
mish() -> Self
Applies the Mish function element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).mish().numpy())
[-0.1456 -0.2525 -0.3034 0. 0.8651 1.944 2.9865]
Source code in tinygrad/mixin/elementwise.py
926 927 928 929 930 931 932 933 934 935 936 | |
softplus
¤
softplus(beta=1.0) -> Self
Applies the Softplus function element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).softplus().numpy())
[0.0486 0.1269 0.3133 0.6931 1.3133 2.1269 3.0486]
Source code in tinygrad/mixin/elementwise.py
916 917 918 919 920 921 922 923 924 | |
softsign
¤
softsign() -> Self
Applies the Softsign function element-wise.
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).softsign().numpy())
[-0.75 -0.6667 -0.5 0. 0.5 0.6667 0.75 ]
Source code in tinygrad/mixin/elementwise.py
988 989 990 991 992 993 994 995 996 | |
Elementwise Ops (broadcasted)¤
add
¤
Adds self and x.
Equivalent to self + x.
Supports broadcasting to a common shape, type promotion, and integer, float, boolean inputs.
Tensor.manual_seed(42)
t = Tensor.randn(4)
print(t.numpy())
[0.6226 0.1706 0.8297 0.3067]
print(t.add(20).numpy())
[20.6226 20.1706 20.8297 20.3067]
print(t.add(Tensor([[2.0], [3.5]])).numpy())
[[2.6226 2.1706 2.8297 2.3067]
[4.1226 3.6706 4.3297 3.8067]]
Source code in tinygrad/mixin/elementwise.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
sub
¤
Subtracts x from self.
Equivalent to self - x.
Supports broadcasting to a common shape, type promotion, and integer, float, boolean inputs.
Tensor.manual_seed(42)
t = Tensor.randn(4)
print(t.numpy())
[0.6226 0.1706 0.8297 0.3067]
print(t.sub(20).numpy())
[-19.3774 -19.8294 -19.1703 -19.6933]
print(t.sub(Tensor([[2.0], [3.5]])).numpy())
[[-1.3774 -1.8294 -1.1703 -1.6933]
[-2.8774 -3.3294 -2.6703 -3.1933]]
Source code in tinygrad/mixin/elementwise.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
mul
¤
Multiplies self and x.
Equivalent to self * x.
Supports broadcasting to a common shape, type promotion, and integer, float, boolean inputs.
Tensor.manual_seed(42)
t = Tensor.randn(4)
print(t.numpy())
[0.6226 0.1706 0.8297 0.3067]
print(t.mul(3).numpy())
[1.8678 0.5117 2.4891 0.9202]
print(t.mul(Tensor([[-1.0], [2.0]])).numpy())
[[-0.6226 -0.1706 -0.8297 -0.3067]
[ 1.2452 0.3412 1.6594 0.6135]]
Source code in tinygrad/mixin/elementwise.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
div
¤
div(
x: Tensor | ConstType | UOp,
reverse=False,
rounding_mode: Literal["trunc", "floor"] | None = None,
) -> Tensor
Divides self by x.
Equivalent to self / x.
Supports broadcasting to a common shape, type promotion, and integer, float, boolean inputs.
div performs true division.
Tensor.manual_seed(42)
t = Tensor.randn(4)
print(t.numpy())
[0.6226 0.1706 0.8297 0.3067]
print(t.div(3).numpy())
[0.2075 0.0569 0.2766 0.1022]
print(Tensor([1, 4, 10]).div(Tensor([2, 3, 4])).numpy())
[0.5 1.3333 2.5 ]
Source code in tinygrad/tensor.py
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 | |
idiv
¤
Divides self by x.
Equivalent to self // x.
Supports broadcasting to a common shape, type promotion, and integer inputs.
idiv performs integer division (truncate towards zero).
print(Tensor([-4, 7, 5, 4, -7, 8]).idiv(Tensor([2, -3, 8, -2, 3, 5])).numpy())
[-2 -2 0 -2 -2 1]
Source code in tinygrad/mixin/elementwise.py
170 171 172 173 174 175 176 177 178 179 180 181 | |
mod
¤
Mod self by x.
Equivalent to self % x.
Supports broadcasting to a common shape, type promotion, and integer inputs.
print(Tensor([-4, 7, 5, 4, -7, 8]).mod(Tensor([2, -3, 8, -2, 3, 5])).numpy())
[ 0 -2 5 0 2 3]
Source code in tinygrad/tensor.py
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 | |
bitwise_xor
¤
Computes bitwise xor of self and x.
Equivalent to self ^ x.
Supports broadcasting to a common shape, type promotion, and integer, boolean inputs.
print(Tensor([-1, -2, 3]).bitwise_xor(Tensor([1, 0, 3])).numpy())
[-2 -2 0]
print(Tensor([True, True, False, False]).bitwise_xor(Tensor([True, False, True, False])).numpy())
[False True True False]
Source code in tinygrad/mixin/elementwise.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
bitwise_and
¤
Computes the bitwise AND of self and x.
Equivalent to self & x.
Supports broadcasting to a common shape, type promotion, and integer, boolean inputs.
print(Tensor([2, 5, 255]).bitwise_and(Tensor([3, 14, 16])).numpy())
[ 2 4 16]
print(Tensor([True, True, False, False]).bitwise_and(Tensor([True, False, True, False])).numpy())
[ True False False False]
Source code in tinygrad/mixin/elementwise.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
bitwise_or
¤
Computes the bitwise OR of self and x.
Equivalent to self | x.
Supports broadcasting to a common shape, type promotion, and integer, boolean inputs.
print(Tensor([2, 5, 255]).bitwise_or(Tensor([4, 4, 4])).numpy())
[ 6 5 255]
print(Tensor([True, True, False, False]).bitwise_or(Tensor([True, False, True, False])).numpy())
[ True True True False]
Source code in tinygrad/mixin/elementwise.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
bitwise_not
¤
bitwise_not() -> Self
Computes the bitwise NOT of self.
Equivalent to ~self.
print(Tensor([0, 2, 5, 255], dtype="int8").bitwise_not().numpy())
[-1 -3 -6 0]
print(Tensor([True, False]).bitwise_not().numpy())
[False True]
Source code in tinygrad/mixin/elementwise.py
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 | |
lshift
¤
Computes left arithmetic shift of self by x bits. self must have integer dtype.
Equivalent to self << x.
print(Tensor([1, 3, 31], dtype=dtypes.uint8).lshift(2).numpy())
[ 4 12 124]
Source code in tinygrad/mixin/elementwise.py
273 274 275 276 277 278 279 280 281 282 | |
rshift
¤
Computes right arithmetic shift of self by x bits. self must have integer dtype.
Equivalent to self >> x.
print(Tensor([4, 13, 125], dtype=dtypes.uint8).rshift(2).numpy())
[ 1 3 31]
Source code in tinygrad/mixin/elementwise.py
284 285 286 287 288 289 290 291 292 293 | |
pow
¤
Computes power of self with x.
Equivalent to self ** x.
print(Tensor([-1, 2, 3]).pow(2.0).numpy())
[1 4 9]
print(Tensor([-1, 2, 3]).pow(Tensor([-1.5, 0.5, 1.5])).numpy())
[-2147483648 1 5]
print((2.0 ** Tensor([-1, 2, 3])).numpy())
[0.5 4. 8. ]
Source code in tinygrad/mixin/elementwise.py
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | |
maximum
¤
Computes element-wise maximum of self and x.
print(Tensor([-1, 2, 3]).maximum(1).numpy())
[1 2 3]
print(Tensor([-1, 2, 3]).maximum(Tensor([-4, -2, 9])).numpy())
[-1 2 9]
Source code in tinygrad/mixin/elementwise.py
307 308 309 310 311 312 313 314 315 316 317 318 | |
minimum
¤
Computes element-wise minimum of self and x.
print(Tensor([-1, 2, 3]).minimum(1).numpy())
[-1 1 1]
print(Tensor([-1, 2, 3]).minimum(Tensor([-4, -2, 9])).numpy())
[-4 -2 3]
Source code in tinygrad/mixin/elementwise.py
322 323 324 325 326 327 328 329 330 331 332 333 334 | |
where
¤
Returns a tensor of elements selected from either x or y, depending on self.
output_i = x_i if self_i else y_i.
cond = Tensor([[True, True, False], [True, False, False]])
print(cond.where(1, 3).numpy())
[[1 1 3]
[1 3 3]]
Tensor.manual_seed(42)
cond = Tensor.randn(2, 3)
print(cond.numpy())
[[ 1.9576 -0.1859 1.6404]
[-0.7647 -0.8695 -0.4379]]
print((cond > 0).where(cond, -float("inf")).numpy())
[[1.9576 -inf 1.6404]
[ -inf -inf -inf]]
Source code in tinygrad/tensor.py
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 | |
copysign
¤
Returns a tensor of with the magnitude of self and the sign of other, elementwise.
Source code in tinygrad/mixin/elementwise.py
336 337 338 339 340 341 342 | |
logaddexp
¤
Calculates (self.exp()+other.exp()).log(), elementwise.
Source code in tinygrad/mixin/elementwise.py
344 345 346 347 348 349 | |
Casting Ops¤
cast
¤
cast(dtype: DTypeLike) -> Tensor
Casts self to the given dtype.
t = Tensor([-1, 2.5, 3], dtype=dtypes.float)
print(t.dtype, t.numpy())
dtypes.float [-1. 2.5 3. ]
t = t.cast(dtypes.int32)
print(t.dtype, t.numpy())
dtypes.int [-1 2 3]
t = t.cast(dtypes.uint8)
print(t.dtype, t.numpy())
dtypes.uchar [255 2 3]
Source code in tinygrad/tensor.py
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 | |
bitcast
¤
bitcast(dtype: DTypeLike) -> Tensor
Bitcasts self to the given dtype of the same itemsize.
self must not require a gradient.
t = Tensor([-1, 2, 3], dtype=dtypes.int32)
print(t.dtype, t.numpy())
dtypes.int [-1 2 3]
t = t.bitcast(dtypes.uint32)
print(t.dtype, t.numpy())
dtypes.uint [4294967295 2 3]
Source code in tinygrad/tensor.py
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 | |
float
¤
float() -> Self
Convenience method to cast self to a float32 Tensor.
t = Tensor([-1, 2, 3], dtype=dtypes.int32)
print(t.dtype, t.numpy())
dtypes.int [-1 2 3]
t = t.float()
print(t.dtype, t.numpy())
dtypes.float [-1. 2. 3.]
Source code in tinygrad/mixin/dtype.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
half
¤
half() -> Self
Convenience method to cast self to a float16 Tensor.
t = Tensor([-1, 2, 3], dtype=dtypes.int32)
print(t.dtype, t.numpy())
dtypes.int [-1 2 3]
t = t.half()
print(t.dtype, t.numpy())
dtypes.half [-1. 2. 3.]
Source code in tinygrad/mixin/dtype.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
int
¤
int() -> Self
Convenience method to cast self to a int32 Tensor.
t = Tensor([-1.5, -0.5, 0.0, 0.5, 1.5])
print(t.dtype, t.numpy())
dtypes.float [-1.5 -0.5 0. 0.5 1.5]
t = t.int()
print(t.dtype, t.numpy())
dtypes.int [-1 0 0 0 1]
Source code in tinygrad/mixin/dtype.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
bool
¤
bool() -> Self
Convenience method to cast self to a bool Tensor.
t = Tensor([-1, 0, 1])
print(t.dtype, t.numpy())
dtypes.int [-1 0 1]
t = t.bool()
print(t.dtype, t.numpy())
dtypes.bool [ True False True]
Source code in tinygrad/mixin/dtype.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
bfloat16
¤
bfloat16() -> Self
Source code in tinygrad/mixin/dtype.py
93 | |
double
¤
double() -> Self
Source code in tinygrad/mixin/dtype.py
94 | |