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 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
use super::cat::cat_with_slice_assign;
use super::{BoolTensor, Device, FloatTensor, IntElem, IntTensor};
use crate::Tensor;
use crate::{backend::Backend, tensor::Shape, Data, Distribution, ElementConversion, Int};
use crate::{tensor::api::chunk, tensor::api::narrow};
use alloc::vec::Vec;
use burn_common::reader::Reader;
use core::ops::Range;
use num_traits::ToPrimitive;
#[cfg(any(feature = "wasm-sync", not(target_family = "wasm")))]
use crate::{argsort, sort, sort_with_indices};
/// Int Tensor API for basic and numeric operations, see [tensor](crate::Tensor)
/// for documentation on each function.
pub trait IntTensorOps<B: Backend> {
/// Creates a new int tensor.
///
/// # Arguments
///
/// * `shape` - The shape of the tensor.
/// * `device` - The device to create the tensor on.
///
/// # Returns
///
/// The integer tensor with the given shape.
fn int_empty<const D: usize>(shape: Shape<D>, device: &Device<B>) -> IntTensor<B, D>;
/// Returns the shape of the tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor.
///
/// # Returns
///
/// The shape of the tensor.
fn int_shape<const D: usize>(tensor: &IntTensor<B, D>) -> Shape<D>;
/// Converts the tensor to a data structure.
///
/// # Arguments
///
/// * `tensor` - The tensor.
///
/// # Returns
///
/// The data structure with the tensor's data.
fn int_into_data<const D: usize>(tensor: IntTensor<B, D>) -> Reader<Data<IntElem<B>, D>>;
/// Gets the data from the tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor.
///
/// # Returns
///
/// The data cloned from the data structure.
fn int_to_data<const D: usize>(tensor: &IntTensor<B, D>) -> Reader<Data<IntElem<B>, D>> {
Self::int_into_data(tensor.clone())
}
/// Creates a tensor from the data structure.
///
/// # Arguments
///
/// * `data` - The data structure.
/// * `device` - The device to create the tensor on.
///
/// # Returns
///
/// The tensor with the data.
fn int_from_data<const D: usize>(
data: Data<IntElem<B>, D>,
device: &Device<B>,
) -> IntTensor<B, D>;
/// Gets the device of the tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor.
///
/// # Returns
///
/// The device of the tensor.
fn int_device<const D: usize>(tensor: &IntTensor<B, D>) -> Device<B>;
/// Moves the tensor to the given device.
fn int_to_device<const D: usize>(
tensor: IntTensor<B, D>,
device: &Device<B>,
) -> IntTensor<B, D>;
/// Reshapes the tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor.
/// * `shape` - The new shape.
///
/// # Returns
///
/// The tensor with the new shape.
fn int_reshape<const D1: usize, const D2: usize>(
tensor: IntTensor<B, D1>,
shape: Shape<D2>,
) -> IntTensor<B, D2>;
/// Gets the element at the given indices.
///
/// # Arguments
///
/// * `tensor` - The tensor.
/// * `indices` - The indices.
///
/// # Returns
///
/// The elements at the given indices.
fn int_slice<const D1: usize, const D2: usize>(
tensor: IntTensor<B, D1>,
indices: [Range<usize>; D2],
) -> IntTensor<B, D1>;
/// Sets the element at the given indices.
///
/// # Arguments
///
/// * `tensor` - The tensor.
/// * `indices` - The indices.
///
/// # Returns
///
/// The tensor with the element at the given indices set.
fn int_slice_assign<const D1: usize, const D2: usize>(
tensor: IntTensor<B, D1>,
indices: [Range<usize>; D2],
value: IntTensor<B, D1>,
) -> IntTensor<B, D1>;
/// Converts int tensor to float tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor.
///
/// # Returns
///
/// The int tensor with the same data as the float tensor.
fn int_into_float<const D: usize>(tensor: IntTensor<B, D>) -> FloatTensor<B, D>;
/// Fills the tensor with values from the source tensor if the mask is true at the given
/// indices.
///
/// # Arguments
///
/// * `tensor` - The tensor.
/// * `mask` - The mask.
/// * `source` - The source tensor.
///
/// # Returns
///
/// The tensor with the values filled.
fn int_mask_where<const D: usize>(
tensor: IntTensor<B, D>,
mask: BoolTensor<B, D>,
source: IntTensor<B, D>,
) -> IntTensor<B, D>;
/// Fills the tensor with the given value if the mask is true at the given indices.
///
/// # Arguments
///
/// * `tensor` - The tensor.
/// * `mask` - The mask.
/// * `value` - The value.
///
/// # Returns
///
/// The tensor with the values filled.
fn int_mask_fill<const D: usize>(
tensor: IntTensor<B, D>,
mask: BoolTensor<B, D>,
value: IntElem<B>,
) -> IntTensor<B, D>;
/// Gather elements from the tensor at the given indices.
///
/// # Arguments
///
/// * `dim` - The dimension to gather from.
/// * `tensor` - The tensor.
/// * `indices` - The indices.
fn int_gather<const D: usize>(
dim: usize,
tensor: IntTensor<B, D>,
indices: IntTensor<B, D>,
) -> IntTensor<B, D>;
/// Scatter a given value to the tensor at the given indices.
///
/// # Arguments
///
/// * `dim` - The dimension to scatter to.
/// * `tensor` - The tensor.
/// * `indices` - The indices.
/// * `value` - The value.
///
/// # Returns
///
/// The tensor with the values scattered.
fn int_scatter<const D: usize>(
dim: usize,
tensor: IntTensor<B, D>,
indices: IntTensor<B, D>,
value: IntTensor<B, D>,
) -> IntTensor<B, D>;
/// Select tensor elements along the given dimension corresponding to the given indices.
///
/// # Arguments
///
/// * `tensor` - The tensor.
/// * `dim` - The dimension to select from.
/// * `indices` - The indices.
///
/// # Returns
///
/// The tensor with the selected elements.
fn int_select<const D: usize>(
tensor: IntTensor<B, D>,
dim: usize,
indices: IntTensor<B, 1>,
) -> IntTensor<B, D>;
/// Assign the selected elements along the given dimension corresponding to the given indices
/// to the given value.
///
/// # Arguments
///
/// * `tensor` - The tensor.
/// * `dim` - The dimension to select from.
/// * `indices` - The indices.
/// * `value` - The value.
///
/// # Returns
///
/// The tensor with the selected elements assigned to the given value.
fn int_select_assign<const D: usize>(
tensor: IntTensor<B, D>,
dim: usize,
indices: IntTensor<B, 1>,
value: IntTensor<B, D>,
) -> IntTensor<B, D>;
/// Repeats the tensor along the given dimension the given number of times.
///
/// # Arguments
///
/// * `tensor` - The tensor.
/// * `dim` - The dimension to repeat.
/// * `times` - The number of times to repeat.
///
/// # Returns
///
/// The tensor with the given dimension repeated the given number of times.
fn int_repeat<const D: usize>(
tensor: IntTensor<B, D>,
dim: usize,
times: usize,
) -> IntTensor<B, D> {
let mut shape = Self::int_shape(&tensor);
if shape.dims[dim] != 1 {
panic!("Can only repeat dimension with dim=1");
}
shape.dims[dim] = times;
let mut i = 0;
let indices_select_all = [0; D].map(|_| {
let start = 0;
let end = shape.dims[i];
i += 1;
start..end
});
let mut tensor_output = Self::int_empty(shape, &Self::int_device(&tensor));
for i in 0..times {
let mut indices = indices_select_all.clone();
indices[dim] = i..i + 1;
tensor_output = Self::int_slice_assign(tensor_output, indices, tensor.clone());
}
tensor_output
}
/// Concatenates the given tensors along the given dimension.
///
/// # Arguments
///
/// * `tensors` - The tensors.
/// * `dim` - The dimension to concatenate along.
///
/// # Returns
///
/// The concatenated tensor.
fn int_cat<const D: usize>(tensors: Vec<IntTensor<B, D>>, dim: usize) -> IntTensor<B, D> {
cat_with_slice_assign::<B, D, Int>(
tensors
.into_iter()
.map(Tensor::<B, D, Int>::from_primitive)
.collect(),
dim,
)
.into_primitive()
}
/// Element-wise equality comparison.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side tensor.
///
/// # Returns
///
/// The boolean tensor with the result of the comparison.
fn int_equal<const D: usize>(lhs: IntTensor<B, D>, rhs: IntTensor<B, D>) -> BoolTensor<B, D>;
/// Element-wise non-equality comparison.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side tensor.
///
/// # Returns
///
/// The boolean tensor with the result of the comparison.
fn int_not_equal<const D: usize>(
lhs: IntTensor<B, D>,
rhs: IntTensor<B, D>,
) -> BoolTensor<B, D> {
let equal_tensor = B::int_equal(lhs, rhs);
B::bool_not(equal_tensor)
}
/// Element-wise equality comparison with a scalar.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side scalar.
///
/// # Returns
///
/// The boolean tensor with the result of the comparison.
fn int_equal_elem<const D: usize>(lhs: IntTensor<B, D>, rhs: IntElem<B>) -> BoolTensor<B, D>;
/// Element-wise non-equality comparison with a scalar.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side scalar.
///
/// # Returns
///
/// The boolean tensor with the result of the comparison.
fn int_not_equal_elem<const D: usize>(
lhs: IntTensor<B, D>,
rhs: IntElem<B>,
) -> BoolTensor<B, D> {
let equal_tensor = B::int_equal_elem(lhs, rhs);
B::bool_not(equal_tensor)
}
/// Element-wise greater than comparison.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side tensor.
///
/// # Returns
///
/// The boolean tensor with the result of the comparison.
fn int_greater<const D: usize>(lhs: IntTensor<B, D>, rhs: IntTensor<B, D>) -> BoolTensor<B, D>;
/// Element-wise greater than comparison with a scalar.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side scalar.
///
/// # Returns
///
/// The boolean tensor with the result of the comparison.
fn int_greater_elem<const D: usize>(lhs: IntTensor<B, D>, rhs: IntElem<B>) -> BoolTensor<B, D>;
/// Element-wise greater than or equal comparison.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side tensor.
///
/// # Returns
///
/// The boolean tensor with the result of the comparison.
fn int_greater_equal<const D: usize>(
lhs: IntTensor<B, D>,
rhs: IntTensor<B, D>,
) -> BoolTensor<B, D>;
/// Element-wise greater than or equal comparison with a scalar.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side scalar.
///
/// # Returns
///
/// The boolean tensor with the result of the comparison.
fn int_greater_equal_elem<const D: usize>(
lhs: IntTensor<B, D>,
rhs: IntElem<B>,
) -> BoolTensor<B, D>;
/// Element-wise less than comparison.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side tensor.
///
/// # Returns
///
/// The boolean tensor with the result of the comparison.
fn int_lower<const D: usize>(lhs: IntTensor<B, D>, rhs: IntTensor<B, D>) -> BoolTensor<B, D>;
/// Element-wise less than comparison with a scalar.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side scalar.
///
/// # Returns
///
/// The boolean tensor with the result of the comparison.
fn int_lower_elem<const D: usize>(lhs: IntTensor<B, D>, rhs: IntElem<B>) -> BoolTensor<B, D>;
/// Element-wise less than or equal comparison.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side tensor.
///
/// # Returns
///
/// The boolean tensor with the result of the comparison.
fn int_lower_equal<const D: usize>(
lhs: IntTensor<B, D>,
rhs: IntTensor<B, D>,
) -> BoolTensor<B, D>;
/// Element-wise less than or equal comparison with a scalar.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side scalar.
///
/// # Returns
///
/// The boolean tensor with the result of the comparison.
fn int_lower_equal_elem<const D: usize>(
lhs: IntTensor<B, D>,
rhs: IntElem<B>,
) -> BoolTensor<B, D>;
// ==== NUMERIC ==== //
/// Element-wise addition.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side tensor.
///
/// # Returns
///
/// The result of the addition.
fn int_add<const D: usize>(lhs: IntTensor<B, D>, rhs: IntTensor<B, D>) -> IntTensor<B, D>;
/// Element-wise addition with a scalar.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side scalar.
///
/// # Returns
///
/// The result of the addition.
fn int_add_scalar<const D: usize>(lhs: IntTensor<B, D>, rhs: IntElem<B>) -> IntTensor<B, D>;
/// Element-wise power with a IntTensor.
///
/// # Arguments
///
/// * `lhs` - The left hand side IntTensor.
/// * `rhs` - The right hand side IntTensor.
///
/// # Returns
///
/// The elements of `lhs` raised to the power of the elements of `rhs`.
fn int_powi<const D: usize>(lhs: IntTensor<B, D>, rhs: IntTensor<B, D>) -> IntTensor<B, D> {
B::float_into_int(B::float_powf(
B::int_into_float(lhs),
B::int_into_float(rhs),
))
}
/// Element-wise power with a floatTensor.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side floatTensor.
///
/// # Returns
///
/// The elements of `lhs` raised to the value of `rhs`. Result is an IntTensor.
fn int_powf<const D: usize>(lhs: IntTensor<B, D>, rhs: FloatTensor<B, D>) -> IntTensor<B, D> {
B::float_into_int(B::float_powf(B::int_into_float(lhs), rhs))
}
/// Element-wise power with a scalar.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side scalar.
///
/// # Returns
///
/// The elements of `lhs` raised to the value of `rhs`.
fn int_powi_scalar<const D: usize>(lhs: IntTensor<B, D>, rhs: IntElem<B>) -> IntTensor<B, D> {
B::float_into_int(B::float_powf_scalar(
B::int_into_float(lhs),
rhs.to_f32().unwrap(),
))
}
/// Element-wise power with a floatTensor.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side scalar.
///
/// # Returns
///
/// The elements of `lhs` raised to the value of `rhs`. Result is an IntTensor.
fn int_powf_scalar<const D: usize>(lhs: IntTensor<B, D>, rhs: f32) -> IntTensor<B, D> {
B::float_into_int(B::float_powf_scalar(B::int_into_float(lhs), rhs))
}
/// Clamps a tensor under a minimum value.
///
/// # Arguments
///
/// * `tensor` - The tensor to clamp.
/// * `min` - The minimum value.
///
/// # Returns
///
/// The clamped tensor.
fn int_clamp_min<const D: usize>(tensor: IntTensor<B, D>, min: IntElem<B>) -> IntTensor<B, D> {
let mask = Self::int_lower_elem(tensor.clone(), min);
Self::int_mask_fill(tensor, mask, min)
}
/// Clamps a tensor over a maximum value.
///
/// # Arguments
///
/// * `tensor` - The tensor to clamp.
/// * `max` - The maximum value.
///
/// # Returns
///
/// The clamped tensor.
fn int_clamp_max<const D: usize>(tensor: IntTensor<B, D>, max: IntElem<B>) -> IntTensor<B, D> {
let mask = Self::int_greater_elem(tensor.clone(), max);
Self::int_mask_fill(tensor, mask, max)
}
/// Clamps a tensor between a minimum and maximum value.
///
/// # Arguments
///
/// * `tensor` - The tensor to clamp.
/// * `min` - The minimum value.
/// * `max` - The maximum value.
///
/// # Returns
///
/// The clamped tensor.
fn int_clamp<const D: usize>(
tensor: IntTensor<B, D>,
min: IntElem<B>,
max: IntElem<B>,
) -> IntTensor<B, D> {
Self::int_clamp_min(Self::int_clamp_max(tensor, max), min)
}
/// Element-wise subtraction.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side tensor.
///
/// # Returns
///
/// The result of the subtraction.
fn int_sub<const D: usize>(lhs: IntTensor<B, D>, rhs: IntTensor<B, D>) -> IntTensor<B, D>;
/// Element-wise subtraction with a scalar.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side scalar.
///
/// # Returns
///
/// The result of the subtraction.
fn int_sub_scalar<const D: usize>(lhs: IntTensor<B, D>, rhs: IntElem<B>) -> IntTensor<B, D>;
/// Element-wise multiplication.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side tensor.
///
/// # Returns
///
/// The result of the multiplication.
fn int_mul<const D: usize>(lhs: IntTensor<B, D>, rhs: IntTensor<B, D>) -> IntTensor<B, D>;
/// Element-wise multiplication with a scalar.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side scalar.
///
/// # Returns
///
/// The result of the multiplication.
fn int_mul_scalar<const D: usize>(lhs: IntTensor<B, D>, rhs: IntElem<B>) -> IntTensor<B, D>;
/// Element-wise division.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side tensor.
///
/// # Returns
///
/// The result of the division.
fn int_div<const D: usize>(lhs: IntTensor<B, D>, rhs: IntTensor<B, D>) -> IntTensor<B, D>;
/// Element-wise division with a scalar.
///
/// # Arguments
///
/// * `lhs` - The left hand side tensor.
/// * `rhs` - The right hand side scalar.
///
/// # Returns
///
/// The result of the division.
fn int_div_scalar<const D: usize>(lhs: IntTensor<B, D>, rhs: IntElem<B>) -> IntTensor<B, D>;
/// Element-wise negation.
///
/// # Arguments
///
/// * `tensor` - The tensor to negate.
///
/// # Returns
///
/// The negated tensor.
fn int_neg<const D: usize>(tensor: IntTensor<B, D>) -> IntTensor<B, D> {
Self::int_mul_scalar(tensor, (-1.0).elem::<IntElem<B>>())
}
/// Creates a tensor of zeros.
///
/// # Arguments
///
/// * `shape` - The shape of the tensor.
/// * `device` - The device to create the tensor on.
///
/// # Returns
///
/// The tensor of zeros.
fn int_zeros<const D: usize>(shape: Shape<D>, device: &Device<B>) -> IntTensor<B, D>;
/// Creates a tensor of ones.
///
/// # Arguments
///
/// * `shape` - The shape of the tensor.
/// * `device` - The device to create the tensor on.
///
/// # Returns
///
/// The tensor of ones.
fn int_ones<const D: usize>(shape: Shape<D>, device: &Device<B>) -> IntTensor<B, D>;
/// Creates a tensor filled with given value.
///
/// # Arguments
///
/// * `shape` - The shape of the tensor.
/// * `fill_value` - The value with which to fill the tensor.
/// * `device` - The device to create the tensor on.
///
/// # Returns
///
/// The tensor filled with given value
fn int_full<const D: usize>(
shape: Shape<D>,
fill_value: IntElem<B>,
device: &Device<B>,
) -> IntTensor<B, D> {
Self::int_add_scalar(Self::int_zeros(shape, device), fill_value)
}
/// Sums all elements in the tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor to sum.
///
/// # Returns
///
/// The sum of all elements in the tensor.
fn int_sum<const D: usize>(tensor: IntTensor<B, D>) -> IntTensor<B, 1>;
/// Sums all elements in the tensor along a dimension.
///
/// # Arguments
///
/// * `tensor` - The tensor to sum.
/// * `dim` - The dimension to sum along.
///
/// # Returns
///
/// The sum of all elements in the tensor along the dimension.
fn int_sum_dim<const D: usize>(tensor: IntTensor<B, D>, dim: usize) -> IntTensor<B, D>;
/// Computes the product of all elements in the tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor to compute the product of.
///
/// # Returns
///
/// The product of all elements in the tensor.
fn int_prod<const D: usize>(tensor: IntTensor<B, D>) -> IntTensor<B, 1>;
/// Computes the product of all elements in the tensor along a dimension.
///
/// # Arguments
///
/// * `tensor` - The tensor to compute the product of.
/// * `dim` - The dimension to compute the product along.
///
/// # Returns
///
/// The product of all elements in the tensor along the dimension.
fn int_prod_dim<const D: usize>(tensor: IntTensor<B, D>, dim: usize) -> IntTensor<B, D>;
/// Computes the mean of all elements in the tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor to compute the mean of.
///
/// # Returns
///
/// The mean of all elements in the tensor.
fn int_mean<const D: usize>(tensor: IntTensor<B, D>) -> IntTensor<B, 1> {
let num_elems = B::int_shape(&tensor).num_elements();
B::int_div_scalar(B::int_sum(tensor), (num_elems as i64).elem())
}
/// Computes the mean of all elements in the tensor along a dimension.
///
/// # Arguments
///
/// * `tensor` - The tensor to compute the mean of.
///
/// # Returns
///
/// The mean of all elements in the tensor along the dimension.
fn int_mean_dim<const D: usize>(tensor: IntTensor<B, D>, dim: usize) -> IntTensor<B, D>;
/// Gets the indices of the maximum elements along a dimension.
///
/// # Arguments
///
/// * `tensor` - The tensor to get the maximum indices of.
/// * `dim` - The dimension to get the maximum indices along.
///
/// # Returns
///
/// The indices of the maximum elements along the dimension.
fn int_argmax<const D: usize>(tensor: IntTensor<B, D>, dim: usize) -> IntTensor<B, D>;
/// Gets the indices of the minimum elements along a dimension.
///
/// # Arguments
///
/// * `tensor` - The tensor to get the minimum indices of.
/// * `dim` - The dimension to get the minimum indices along.
///
/// # Returns
///
/// The indices of the minimum elements along the dimension.
fn int_argmin<const D: usize>(tensor: IntTensor<B, D>, dim: usize) -> IntTensor<B, D>;
/// Gets the maximum element in the tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor to get the maximum element of.
///
/// # Returns
///
/// The maximum element in the tensor.
fn int_max<const D: usize>(tensor: IntTensor<B, D>) -> IntTensor<B, 1> {
let shape = B::int_shape(&tensor);
let tensor = B::int_reshape(tensor, Shape::new([shape.num_elements()]));
B::int_max_dim(tensor, 0)
}
/// Gets the maximum element in the tensor along a dimension.
///
/// # Arguments
///
/// * `tensor` - The tensor to get the maximum element of.
/// * `dim` - The dimension to get the maximum element along.
///
/// # Returns
///
/// The maximum element in the tensor along the dimension.
fn int_max_dim<const D: usize>(tensor: IntTensor<B, D>, dim: usize) -> IntTensor<B, D> {
let index = B::int_argmax(tensor.clone(), dim);
B::int_gather(D - 1, tensor, index)
}
/// Gets the maximum elements and corresponding indices along a dimension.
///
/// # Arguments
///
/// * `tensor` - The tensor to get the maximum elements and indices of.
/// * `dim` - The dimension to get the maximum elements and indices along.
///
/// # Returns
///
/// The maximum elements and corresponding indices along the dimension.
fn int_max_dim_with_indices<const D: usize>(
tensor: IntTensor<B, D>,
dim: usize,
) -> (IntTensor<B, D>, IntTensor<B, D>) {
let index = B::int_argmax(tensor.clone(), dim);
let values = B::int_gather(D - 1, tensor, index.clone());
(values, index)
}
/// Gets the minimum element in the tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor to get the minimum element of.
///
/// # Returns
///
/// The minimum element in the tensor.
fn int_min<const D: usize>(tensor: IntTensor<B, D>) -> IntTensor<B, 1> {
let shape = B::int_shape(&tensor);
let tensor = B::int_reshape(tensor, Shape::new([shape.num_elements()]));
B::int_min_dim(tensor, 0)
}
/// Gets the minimum elements in the tensor along a dimension.
///
/// # Arguments
///
/// * `tensor` - The tensor to get the minimum element of.
/// * `dim` - The dimension to get the minimum element along.
///
/// # Returns
///
/// The minimum element in the tensor along the dimension.
fn int_min_dim<const D: usize>(tensor: IntTensor<B, D>, dim: usize) -> IntTensor<B, D> {
let index = B::int_argmin(tensor.clone(), dim);
B::int_gather(D - 1, tensor, index)
}
/// Gets the minimum elements and corresponding indices along a dimension.
///
/// # Arguments
///
/// * `tensor` - The tensor to get the minimum elements and indices of.
/// * `dim` - The dimension to get the minimum elements and indices along.
///
/// # Returns
///
/// The minimum elements and corresponding indices along the dimension.
fn int_min_dim_with_indices<const D: usize>(
tensor: IntTensor<B, D>,
dim: usize,
) -> (IntTensor<B, D>, IntTensor<B, D>) {
let indices = B::int_argmin(tensor.clone(), dim);
let values = B::int_gather(D - 1, tensor, indices.clone());
(values, indices)
}
/// Returns a new tensor with absolute values.
///
/// # Arguments
///
/// * `tensor` - The tensor to take absolute value of.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with absolute values.
fn int_abs<const D: usize>(tensor: IntTensor<B, D>) -> IntTensor<B, D>;
/// Transposes an int tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor to transpose.
///
/// # Returns
///
/// The transposed tensor.
fn int_transpose<const D: usize>(tensor: IntTensor<B, D>) -> IntTensor<B, D> {
Self::int_swap_dims(tensor, D - 2, D - 1)
}
/// Swaps two dimensions of an int tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor to swap the dimensions of.
/// * `dim1` - The first dimension to swap.
/// * `dim2` - The second dimension to swap.
///
/// # Returns
///
/// The tensor with the dimensions swapped.
fn int_swap_dims<const D: usize>(
tensor: IntTensor<B, D>,
dim1: usize,
dim2: usize,
) -> IntTensor<B, D>;
/// Permutes the dimensions of a tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor to permute the dimensions of.
/// * `axes` - The new order of the dimensions.
/// # Returns
///
/// The tensor with the dimensions permuted.
fn int_permute<const D: usize>(tensor: IntTensor<B, D>, axes: [usize; D]) -> IntTensor<B, D>;
/// Reverse the order of elements in a tensor along the given axes.
///
/// # Arguments
///
/// * `tensor` - The tensor to reverse.
/// * `axes` - The axes to reverse.
///
/// The tensor with the elements reversed.
fn int_flip<const D: usize>(tensor: IntTensor<B, D>, axes: &[usize]) -> IntTensor<B, D>;
/// Returns a new tensor with the given dimension narrowed to the given range.
///
/// # Arguments
///
/// * `dim` - The dimension along which the tensor will be narrowed.
/// * `start` - The starting point of the given range.
/// * `length` - The ending point of the given range.
/// # Panics
///
/// - If the dimension is greater than the number of dimensions of the tensor.
/// - If the given range exceeds the number of elements on the given dimension.
///
/// # Returns
///
/// A new tensor with the given dimension narrowed to the given range.
fn int_narrow<const D: usize>(
tensor: IntTensor<B, D>,
dim: usize,
start: usize,
length: usize,
) -> IntTensor<B, D> {
narrow::<B, D, Int>(tensor, dim, start, length)
}
/// Split the tensor along the given dimension into chunks.
///
/// # Arguments
///
/// * `tensor` - The tensor.
/// * `chunks` - The number of chunks to be produced
/// * `times` - The dimension along which the tensor will be split.
///
/// # Returns
///
/// A vector of tensors
fn int_chunk<const D: usize>(
tensor: IntTensor<B, D>,
chunks: usize,
dim: usize,
) -> Vec<IntTensor<B, D>> {
chunk::<B, D, Int>(tensor, chunks, dim)
}
/// Creates a new int tensor with random values.
///
/// # Arguments
/// * `shape` - The shape of the tensor.
/// * `distribution` - The distribution to sample from.
/// * `device` - The device to create the tensor on.
///
/// # Returns
///
/// The tensor with the given shape and random values.
fn int_random<const D: usize>(
shape: Shape<D>,
distribution: Distribution,
device: &Device<B>,
) -> IntTensor<B, D>;
/// Creates a new tensor with values from the given range with the given step size.
///
/// # Arguments
///
/// * `range` - The range of values.
/// * `step` - The step size.
/// * `device` - The device to create the tensor on.
///
/// # Returns
///
/// The tensor with the given values.
fn int_arange_step(range: Range<i64>, step: usize, device: &Device<B>) -> IntTensor<B, 1> {
let value = range
.step_by(step)
.map(|i| i.elem())
.collect::<Vec<IntElem<B>>>();
let shape = Shape::new([value.len()]);
let data = Data::new(value, shape);
B::int_from_data(data, device)
}
/// Creates a new tensor with values from the given range.
///
/// # Arguments
///
/// * `range` - The range of values.
/// * `device` - The device to create the tensor on.
///
/// # Returns
///
/// The tensor with the given values.
///
/// # Remarks
///
/// Uses `arange_step` with a step size of 1 under the hood.
fn int_arange(range: Range<i64>, device: &Device<B>) -> IntTensor<B, 1> {
Self::int_arange_step(range, 1, device)
}
/// Tests if any element in the int `tensor` evaluates to True.
///
/// # Arguments
///
/// * `tensor` - The tensor to test.
///
/// # Returns
///
/// A boolean tensor with a single element, True if any element in the tensor is True, False otherwise.
fn int_any<const D: usize>(tensor: IntTensor<B, D>) -> BoolTensor<B, 1> {
let bool_tensor = B::int_equal_elem(tensor, 0.elem());
let bool_tensor = B::bool_not(bool_tensor);
let sum = B::int_sum(B::bool_into_int(bool_tensor));
B::int_greater_elem(sum, 0.elem())
}
/// Tests if any element in the int `tensor` evaluates to True along a given dimension `dim`.
///
/// # Arguments
///
/// * `tensor` - The tensor to test.
/// * `dim` - The axis along which to test.
///
/// # Returns
///
/// A boolean tensor `Tensor<B, D, Bool>` with the same size as input `tensor`, except in the `dim` axis
/// where the size is 1. The elem in the `dim` axis is True if any element along this dim in the input
/// evaluates to True, False otherwise.
fn int_any_dim<const D: usize>(tensor: IntTensor<B, D>, dim: usize) -> BoolTensor<B, D> {
let bool_tensor = B::int_equal_elem(tensor, 0.elem());
let bool_tensor = B::bool_not(bool_tensor);
let sum = B::int_sum_dim(B::bool_into_int(bool_tensor), dim);
B::int_greater_elem(sum, 0.elem())
}
/// Tests if all elements in the int `tensor` evaluate to True.
///
/// # Arguments
///
/// * `tensor` - The tensor to test.
///
/// # Returns
///
/// A boolean tensor `Tensor<B, 1, Bool>` with a single element, True if all elements in the input tensor
/// evaluate to True, False otherwise.
fn int_all<const D: usize>(tensor: IntTensor<B, D>) -> BoolTensor<B, 1> {
let num_elems = B::int_shape(&tensor).num_elements();
let bool_tensor = B::int_equal_elem(tensor, 0.elem());
let bool_tensor = B::bool_not(bool_tensor);
let sum = B::int_sum(B::bool_into_int(bool_tensor));
B::int_equal_elem(sum, (num_elems as i32).elem())
}
/// Tests if all elements in the int `tensor` evaluate to True along a given dimension `dim`.
///
/// # Arguments
///
/// * `tensor` - The tensor to test.
/// * `dim` - The axis along which to test.
///
/// # Returns
///
/// A boolean tensor `Tensor<B, D, Bool>` with the same size as input `tensor`, except in the `dim` axis
/// where the size is 1. The elem in the `dim` axis is True if all elements along this dim in the input
/// evaluates to True, False otherwise.
fn int_all_dim<const D: usize>(tensor: IntTensor<B, D>, dim: usize) -> BoolTensor<B, D> {
let num_elems = B::int_shape(&tensor).dims[dim];
let bool_tensor = B::int_equal_elem(tensor, 0.elem());
let bool_tensor = B::bool_not(bool_tensor);
let sum = B::int_sum_dim(B::bool_into_int(bool_tensor), dim);
B::int_equal_elem(sum, (num_elems as i32).elem())
}
/// Returns the signs of the int `tensor`.
///
/// # Arguments
///
/// * `tensor` - The tensor to extract the signs from.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` containing the signs of the elements of `tensor`.
fn int_sign<const D: usize>(tensor: IntTensor<B, D>) -> IntTensor<B, D> {
let zeros = B::int_zeros(B::int_shape(&tensor), &B::int_device(&tensor));
let less_than_zero = B::int_lower_elem(tensor.clone(), 0.0f32.elem());
let greater_than_zero = B::int_greater_elem(tensor, 0.0f32.elem());
let mut result = B::int_mask_fill(zeros, less_than_zero, (-1.0f32).elem());
result = B::int_mask_fill(result, greater_than_zero, 1.0f32.elem());
result
}
/// Broadcasts the int `tensor` to the given `shape`.
fn int_expand<const D1: usize, const D2: usize>(
tensor: IntTensor<B, D1>,
shape: Shape<D2>,
) -> IntTensor<B, D2>;
/// Sort the elements of the input `tensor` by value along a given dimension.
///
/// This sort is unstable (i.e., may reorder equal elements).
///
/// # Arguments
///
/// * `tensor` - The input tensor.
/// * `dim` - The axis along which to sort.
/// * `descending` - The sorting order.
///
/// # Returns
///
/// A tensor with the same shape as the input tensor, where the elements are sorted by value.
#[cfg(any(feature = "wasm-sync", not(target_family = "wasm")))]
fn int_sort<const D: usize>(
tensor: IntTensor<B, D>,
dim: usize,
descending: bool,
) -> IntTensor<B, D> {
sort::<B, D, Int>(tensor, dim, descending)
}
/// Sort the elements of the input `tensor` by value along a given dimension.
///
/// This sort is unstable (i.e., may reorder equal elements).
///
/// # Arguments
///
/// * `tensor` - The input tensor.
/// * `dim` - The axis along which to sort.
///
/// # Returns
///
/// A tensor with the same shape as the input tensor and corresponding indices, where
/// the elements are sorted by value and the indices map back to the original input tensor.
#[cfg(any(feature = "wasm-sync", not(target_family = "wasm")))]
fn int_sort_with_indices<const D: usize>(
tensor: IntTensor<B, D>,
dim: usize,
descending: bool,
) -> (IntTensor<B, D>, IntTensor<B, D>) {
sort_with_indices::<B, D, Int>(tensor, dim, descending)
}
/// Returns the indices that sort the elements of the input `tensor` by value
/// along a given dimension.
///
/// This sort is unstable (i.e., may reorder equal elements).
///
/// # Arguments
///
/// * `tensor` - The input tensor.
/// * `dim` - The axis along which to sort.
/// * `descending` - The sorting order.
///
/// # Returns
///
/// A tensor with the same shape as the input tensor the indices map back to the original input tensor.
#[cfg(any(feature = "wasm-sync", not(target_family = "wasm")))]
fn int_argsort<const D: usize>(
tensor: IntTensor<B, D>,
dim: usize,
descending: bool,
) -> IntTensor<B, D> {
argsort::<B, D, Int>(tensor, dim, descending)
}
}