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
//! # Tensor
//!
//! See [Tensor].
// TODO maybe replace panics with results
extern crate alloc;
use alloc::{collections::BTreeSet, format, string::String, vec::Vec};
use crate::{
node_id::NodeId,
axes::IntoAxes,
dtype::DType,
graph::{Graph, Node},
libm::fabsf,
parameters::IntoParameters,
shape::Shape,
OutOfMemoryError,
};
use alloc::rc::Rc;
use core::{cell::RefCell, cmp::Ordering};
// Precision when comparing f32 tensors.
const EPSILON: f32 = 0.00001;
/// # `IntoTensor`
/// Trait for converting values into tensor.
#[allow(clippy::module_name_repetitions)]
pub trait IntoTensor {
/// Create tensor from self on given context.
fn into_tensor(self, ctx: &crate::context::Context) -> Tensor;
}
/// # Tensor
///
/// Multidimensional data structure.
/// It can be single value,
/// ```rust
/// # use zyx::context::Context;
/// let mut ctx = Context::new();
/// let mut x = ctx.tensor(42);
/// x.realize();
/// assert_eq!(x, 42);
/// ```
/// vector,
/// ```rust
/// # use zyx::context::Context;
/// # let mut ctx = Context::new();
/// let mut x = ctx.tensor([42, 69, 10]);
/// x.realize();
/// assert_eq!(x, [42, 69, 10]);
/// ```
/// matrix,
/// ```rust
/// # use zyx::context::Context;
/// # let mut ctx = Context::new();
/// let mut x = ctx.tensor([[42, 69, 10], [24, 96, 1]]);
/// x.realize();
/// assert_eq!(x, [[42, 69, 10], [24, 96, 1]]);
/// ```
/// or it can have arbitrary number of dimensions.
/// ```rust
/// # #[cfg(feature = "rand")] {
/// # use zyx::context::Context;
/// # let mut ctx = Context::new();
/// let mut x = ctx.randn((4, 2, 3, 1, 5));
/// assert_eq!(x.shape(), (4, 2, 3, 1, 5));
/// # }
/// ```
/// You can call operations on tensors
/// ```rust
/// # #[cfg(feature = "rand")] {
/// # use zyx::context::Context;
/// # let mut ctx = Context::new();
/// # let x = ctx.randn((4, 2, 3, 1, 5));
/// let y = x.exp();
/// # }
/// ```
/// and calculate their gradients.
/// ```rust
/// # #[cfg(feature = "rand")] {
/// # use zyx::context::Context;
/// # let mut ctx = Context::new();
/// # let mut x = ctx.randn((4, 2, 3, 1, 5));
/// # let y = x.exp();
/// y.backward(&mut x); // calculates gradient for x
/// # }
/// ```
/// Tensors need to be realized before accessing.
/// ```rust
/// # #[cfg(feature = "rand")] {
/// # use zyx::context::Context;
/// # use zyx::parameters::IntoParameters;
/// # let mut ctx = Context::new();
/// # let mut x = ctx.randn((4, 2, 3, 1, 5));
/// # let y = x.exp();
/// # y.backward(&mut x); // calculates gradient for x
/// x.realize_grad().unwrap();
/// println!("{}", x.grad().unwrap());
/// # }
/// ```
#[derive(Debug)]
pub struct Tensor {
pub(crate) data: NodeId,
pub(crate) grad: Option<NodeId>,
pub(crate) graph: Rc<RefCell<Graph>>,
}
impl Clone for Tensor {
fn clone(&self) -> Self {
self.graph.borrow_mut().retain(self.data);
if let Some(grad) = self.grad {
self.graph.borrow_mut().retain(grad);
}
Self {
data: self.data,
grad: self.grad,
graph: self.graph.clone(),
}
}
}
impl Drop for Tensor {
fn drop(&mut self) {
let mut graph = self.graph.borrow_mut();
graph.release(self.data);
if let Some(grad) = self.grad {
graph.release(grad);
}
}
}
impl Tensor {
/// # Backward
///
/// Calculates gradients for all sources.
///```
/// # use zyx::context::Context;
/// # use zyx::parameters::IntoParameters;
/// # let mut ctx = Context::new();
/// let mut x = ctx.tensor([2, 3, 4]);
/// let y = -&x;
/// y.backward(&mut x);
/// x.realize_grad().unwrap();
/// assert_eq!(x.grad().unwrap(), [-1, -1, -1]);
///```
#[allow(single_use_lifetimes)]
pub fn backward<'p>(&self, sources: impl IntoParameters<'p>) {
let shape = self.shape();
let dtype = self.dtype();
let mut sources = sources.into_vec();
let n = shape.numel();
let mut graph = self.graph.borrow_mut();
let grad_nodes = graph.grad_nodes(&sources);
if grad_nodes.contains(&self.data) {
let grad = match dtype {
DType::F32 => graph.push(Node::StoreF32(
core::iter::repeat(1.).take(n).collect(),
shape,
)),
DType::I32 => graph.push(Node::StoreI32(
core::iter::repeat(1).take(n).collect(),
shape,
)),
};
graph.backward(
self.data,
grad,
&mut sources,
&grad_nodes,
&mut BTreeSet::new(),
);
graph.release(grad);
}
// TODO Maybe label gradients automatically if sources have labels
}
/// Cast tensor into tensor with different [`DType`].
/// ```
/// # use zyx::context::Context;
/// # use zyx::dtype::DType;
/// # let mut ctx = Context::new();
/// let x = ctx.tensor([2, 3, 4]);
/// assert_eq!(x.dtype(), DType::I32);
/// let y = x.cast(DType::F32);
/// assert_eq!(y.dtype(), DType::F32);
/// ```
#[must_use]
pub fn cast(&self, dtype: DType) -> Tensor {
self.new_op(Node::Cast(self.data, dtype))
}
/// Get tensor's context
#[must_use]
pub fn context(&self) -> crate::context::Context {
crate::context::Context::from_graph(self.graph.clone())
}
/// Access tensor's data. This is equivalent to cloning the tensor and zeroing it's gradient.
/// ```
/// # use zyx::context::Context;
/// # let mut ctx = Context::new();
/// let mut x = ctx.tensor([2., 3., 4.]);
/// x.exp().backward(&mut x); // creates gradient for x
/// let y = x.data();
/// assert!(y.grad().is_none());
/// ```
#[must_use]
pub fn data(&self) -> Tensor {
self.graph.borrow_mut().retain(self.data);
Tensor {
data: self.data,
grad: None,
graph: self.graph.clone(),
}
}
/// Matmul operation
/// # Panics
/// Panics if x and y tensors have incompatible shapes.
#[must_use]
pub fn dot(&self, rhs: impl IntoTensor) -> Tensor {
// always returns 2d or higher tensor, even if both inputs are 1d
let rhs = rhs.into_tensor(&self.graph.clone().into());
let shape = self.shape().dot(&rhs.shape());
let x = self.transpose();
self.new_op(Node::TDot(x.data, rhs.data, shape))
}
/// Matmul operation with xhs transposed
/// # Panics
/// Panics if x and y tensors have incompatible shapes.
#[must_use]
pub fn t_dot(&self, rhs: impl IntoTensor) -> Tensor {
let rhs = rhs.into_tensor(&self.graph.clone().into());
let shape = self.shape().transpose().dot(&rhs.shape());
self.new_op(Node::TDot(self.data, rhs.data, shape))
}
/// Get tensor's dtype
/// ```
/// # use zyx::context::Context;
/// # use zyx::dtype::DType;
/// # let mut ctx = Context::new();
/// let x = ctx.tensor([2, 3, 4]);
/// assert_eq!(x.dtype(), DType::I32);
/// ```
#[must_use]
pub fn dtype(&self) -> DType {
self.graph.borrow().dtype(self.data)
}
/// Exp operation
#[must_use]
pub fn exp(&self) -> Tensor {
self.new_op(Node::Exp(self.data))
}
/// Expand tensor into larger shape
#[must_use]
pub fn expand(&self, shape: impl Into<Shape>) -> Tensor {
// TODO checks
self.new_op(Node::Expand(self.data, shape.into()))
}
/// Access tensor's gradient.
/// This function returns None when gradient is None,
/// i. e. if gradient wasn't set or if it was zeroed using [`zero_grad`](Tensor::zero_grad).
/// ```
/// # use zyx::context::Context;
/// # let mut ctx = Context::new();
/// let mut x = ctx.tensor([2, 3, 4]);
/// assert!(x.grad().is_none());
/// ```
#[must_use]
pub fn grad(&self) -> Option<Tensor> {
if let Some(grad) = self.grad {
self.graph.borrow_mut().retain(grad);
return Some(Tensor {
data: grad,
grad: None,
graph: self.graph.clone(),
});
}
None
}
/// Get tensor's id.
/// ```
/// # use zyx::context::Context;
/// # let mut ctx = Context::new();
/// let mut x = ctx.tensor([2, 3, 4]);
/// assert_eq!(x.id(), 0);
/// ```
#[must_use]
pub fn id(&self) -> usize {
self.data.i()
}
/// Get tensor's label
/// ```
/// # use zyx::context::Context;
/// # let mut ctx = Context::new();
/// let mut x = ctx.tensor([2, 3, 4]);
/// assert_eq!(x.label(), None);
/// ```
#[must_use]
pub fn label(&self) -> Option<String> {
self.graph.borrow().label(self.data).cloned()
}
/// Ln operation
#[must_use]
pub fn ln(&self) -> Tensor {
self.new_op(Node::Ln(self.data))
}
/// Reduce tensor across axes, returning max in each axes.
/// All axes in result are kept and set to one.
/// # Panics
/// Panics if axes contain axis larger than tensor's rank,
/// or if axes contain duplicates.
#[must_use]
pub fn max(&self, axes: impl IntoAxes) -> Tensor {
let rank = self.rank();
let axes = axes.into_axes(rank);
let mut uniq = BTreeSet::new();
assert!(
axes.into_iter().all(move |x| uniq.insert(x)),
"Cannot max tensor with shape {:?} by axes {:?}, because axes contain duplicates.",
self.shape(),
axes
);
for a in &axes {
assert!(
*a < rank,
"Cannot max tensor with shape {:?} by axes {:?}, because some axes are greater than rank.",
self.shape(),
axes
);
}
let shape = self.shape().reduce(&axes);
self.new_op(Node::Max(self.data, axes, shape))
}
/// Mean square error between tensor and target.
#[must_use]
pub fn mse(&self, target: impl IntoTensor) -> Tensor {
match self.dtype() {
DType::F32 => (self - target).pow(2f32),
DType::I32 => (self - target).pow(2i32),
}
}
/// Permute tensor's dimensions using axes.
#[must_use]
pub fn permute(&self, axes: impl IntoAxes) -> Tensor {
// TODO checks
let axes = axes.into_axes(self.rank());
let shape = self.shape().permute(&axes);
self.new_op(Node::Permute(self.data, axes, shape))
}
/// Pow operation
#[must_use]
pub fn pow(&self, rhs: impl IntoTensor) -> Tensor {
self.new_binary_op(
self.data,
rhs.into_tensor(&self.graph.clone().into()).data,
"pow",
)
}
/// Get tensor's rank
/// ```
/// # use zyx::context::Context;
/// # let mut ctx = Context::new();
/// let mut x = ctx.tensor([[2, 3, 4], [5, 1, 2]]);
/// assert_eq!(x.rank(), 2);
/// ```
#[must_use]
pub fn rank(&self) -> usize {
self.graph.borrow().shape(self.data).rank()
}
/// Realize tensor
/// # Errors
/// Returns [`OutOfMemoryError`] if backend failed to allocate
/// necessary memory for result and intermediate tensors.
pub fn realize(&mut self) -> Result<(), OutOfMemoryError> {
self.graph.borrow_mut().realize(&[self.data])
}
/// Realize tensor's gradient
/// # Errors
/// Returns [`OutOfMemoryError`] if backend failed to allocate
/// necessary memory for result and intermediate tensors.
pub fn realize_grad(&mut self) -> Result<(), OutOfMemoryError> {
if let Some(grad) = self.grad {
self.graph.borrow_mut().realize(&[grad])
} else {
Ok(())
}
}
/// Reshape tensor
/// # Panics
/// Panics if number of elements in shape is
/// different from tensor's number of elements.
#[must_use]
pub fn reshape(&self, shape: impl Into<Shape>) -> Tensor {
let shape = shape.into();
assert_eq!(
self.shape().numel(),
shape.numel(),
"Cannot reshape {:?} into {:?}",
self.shape(),
shape
);
self.new_op(Node::Reshape(self.data, shape))
}
/// Set label
/// ```
/// # use zyx::context::Context;
/// # let mut ctx = Context::new();
/// let mut x = ctx.tensor([[2, 3, 4], [5, 1, 2]]).set_label("tensor x");
/// assert_eq!(x.label().unwrap(), "tensor x");
/// ```
#[allow(clippy::return_self_not_must_use)]
pub fn set_label(&mut self, label: &str) -> Self {
self.graph.borrow_mut().set_label(self.data, label);
self.clone()
}
/// Get tensor's shape
/// ```
/// # use zyx::context::Context;
/// # let mut ctx = Context::new();
/// let x = ctx.tensor([[2, 3, 4], [5, 1, 2]]);
/// assert_eq!(x.shape(), (2, 3));
/// ```
#[must_use]
pub fn shape(&self) -> Shape {
self.graph.borrow().shape(self.data).clone()
}
/// Reduce tensor across axes, returning sum of each axes.
/// All axes in result are kept and set to one.
/// ```
/// # use zyx::context::Context;
/// # let mut ctx = Context::new();
/// let x = ctx.tensor([[2, 3, 4], [5, 1, 2]]);
/// let mut y = x.sum(1);
/// assert_eq!(y.shape(), (2, 1));
/// y.realize().unwrap();
/// assert_eq!(y, [[9], [8]]);
/// ```
/// # Panics
/// Panics if axes contain axis larger than tensor's rank,
/// or if axes contain duplicates.
#[must_use]
pub fn sum(&self, axes: impl IntoAxes) -> Tensor {
let rank = self.rank();
let axes = axes.into_axes(rank);
let mut uniq = BTreeSet::new();
assert!(
axes.into_iter().all(move |x| uniq.insert(x)),
"Cannot sum tensor with shape {:?} by axes {:?}, because axes contain duplicates.",
self.shape(),
axes
);
for a in &axes {
assert!(
*a < rank,
"Cannot sum tensor with shape {:?} by axes {:?}, because some axes are greater than rank.",
self.shape(),
axes
);
}
let shape = self.shape().reduce(&axes);
self.new_op(Node::Sum(self.data, axes, shape))
}
/// Hyperbolic tangent operation
#[must_use]
pub fn tanh(&self) -> Tensor {
let dtype = self.dtype();
assert_eq!(dtype, DType::F32, "Unable to execute tanh on {dtype} input.");
self.new_op(Node::Tanh(self.data))
}
/// Convert tensor to `Vec<f32>`. Returns None if this tensor isn't realized.
/// ```
/// # use zyx::context::Context;
/// # let mut ctx = Context::new();
/// let x = ctx.tensor([[2, 3, 4], [5, 1, 2]]);
/// let mut y = x.sum(1);
/// assert_eq!(y.shape(), (2, 1));
/// y.realize().unwrap();
/// assert_eq!(y, [[9], [8]]);
/// ```
#[must_use]
pub fn to_vec(&self) -> Option<Vec<f32>> {
self.graph
.borrow_mut()
.load_f32(self.data)
.map(|data| data.to_vec())
}
/// Convert tensor to `Vec<i32>`. Returns None if this tensor isn't realized.
#[must_use]
pub fn to_vec_i32(&self) -> Option<Vec<i32>> {
self.graph
.borrow_mut()
.load_i32(self.data)
.map(|data| data.to_vec())
}
/// Transpose op
#[must_use]
pub fn transpose(&self) -> Tensor {
let shape = self.shape();
let axes = shape.transpose_axes();
let res_shape = shape.permute(&axes);
self.new_op(Node::Permute(self.data, axes, res_shape))
}
/// Set tensor's gradient to None
/// ```
/// # use zyx::context::Context;
/// let mut ctx = Context::new();
/// let mut x = ctx.tensor([2., 3., 4.]);
/// x.zero_grad();
/// assert!(x.grad().is_none());
/// ```
pub fn zero_grad(&mut self) {
if let Some(grad) = self.grad {
self.graph.borrow_mut().release(grad);
}
self.grad = None;
}
// This function is the only way to mutate Tensor.
// It is used by optimizers and for loading parameters.
#[allow(clippy::needless_pass_by_value)]
pub(crate) fn set_data(&mut self, data: Tensor) {
debug_assert_eq!(
self.dtype(),
data.dtype(),
"Internal bug, set_data data input dtype != current tensor dtype"
);
debug_assert_eq!(
self.shape(),
data.shape(),
"Internal bug, set_data data input shape != current tensor shape"
);
let data = NodeId::new(data.id());
if let Some(label) = self.label() {
self.graph.borrow_mut().set_label(data, &label);
}
self.graph.borrow_mut().retain(data);
self.graph.borrow_mut().release(self.data);
self.data = data;
// So that we can drop all parameters of this node after it is realized
self.graph.borrow_mut().set_leaf(self.data);
}
fn new_op(&self, node: Node) -> Tensor {
let data = self.graph.borrow_mut().push(node);
Tensor {
data,
grad: None,
graph: self.graph.clone(),
}
}
fn new_binary_op(&self, mut xid: NodeId, mut yid: NodeId, op: &str) -> Tensor {
let mut expandedx = false;
let mut expandedy = false;
{
let mut graph = self.graph.borrow_mut();
assert_eq!(
graph.dtype(xid),
graph.dtype(yid),
"{} parameters {} and {} have different dtypes: {} and {}",
op,
xid,
yid,
graph.dtype(xid),
graph.dtype(yid)
);
let shapex = graph.shape(xid).clone();
let nx = shapex.numel();
let shapey = graph.shape(yid).clone();
let ny = shapey.numel();
match nx.cmp(&ny) {
Ordering::Greater => {
yid = graph.push(Node::Expand(yid, shapex));
expandedy = true;
}
Ordering::Less => {
xid = graph.push(Node::Expand(xid, shapey));
expandedx = true;
}
Ordering::Equal => {}
}
}
let res = match op {
"add" => self.new_op(Node::Add(xid, yid)),
"sub" => self.new_op(Node::Sub(xid, yid)),
"mul" => self.new_op(Node::Mul(xid, yid)),
"div" => self.new_op(Node::Div(xid, yid)),
"pow" => self.new_op(Node::Pow(xid, yid)),
_ => panic!(),
};
if expandedx {
self.graph.borrow_mut().release(xid);
}
if expandedy {
self.graph.borrow_mut().release(yid);
}
res
}
}
impl core::fmt::Display for Tensor {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
// TODO don't print the whole tensor if it is too big
let precision = if let Some(precision) = f.precision() {
precision
} else {
3
};
let res = match self.dtype() {
DType::F32 => {
if let Some(data) = &self.to_vec() {
tensor_to_string(data, &self.shape(), precision)
} else {
"Unrealized f32 tensor".into()
}
}
DType::I32 => {
if let Some(data) = &self.to_vec_i32() {
tensor_to_string(data, &self.shape(), precision)
} else {
"Unrealized i32 tensor".into()
}
}
};
f.write_str(&res)
}
}
fn tensor_to_string<T: core::fmt::Display>(data: &[T], shape: &Shape, precision: usize) -> String {
use core::fmt::Write;
// TODO don't print whole tensor if it is big
let n = shape.numel();
let ndim = shape.rank();
let mut res = String::new();
if data.is_empty() {
return "[]".into();
}
// get maximal width of single value
let mut w = 0;
for x in data {
let l = format!("{x:>w$.precision$}").len();
if l > w {
w = l;
}
}
let d0 = shape[-1];
for (i, x) in data.iter().enumerate() {
{
let mut var = 1;
let mut r = ndim;
while r > 0 {
if i % (n / var) == 0 {
res += &(" ".repeat(ndim - r) + ("[".repeat(r - 1)).as_str());
break;
}
var *= shape[ndim - r];
r -= 1;
}
}
let _ = write!(res, "{x:>w$.precision$}");
if (i + 1) % d0 != 0usize {
res += " ";
}
{
let mut var = 1;
let mut r = ndim;
while r > 0 {
if (i + 1) % (n / var) == 0 {
res += &"]".repeat(r - 1);
break;
}
var *= shape[ndim - r];
r -= 1;
}
}
if (i + 1) % d0 == 0usize && i != n - 1 {
res += "\n";
}
}
res
}
impl core::ops::Neg for Tensor {
type Output = Tensor;
fn neg(self) -> Self::Output {
self.new_op(Node::Neg(self.data))
}
}
impl core::ops::Neg for &Tensor {
type Output = Tensor;
fn neg(self) -> Self::Output {
self.new_op(Node::Neg(self.data))
}
}
impl<IT: IntoTensor> core::ops::Add<IT> for Tensor {
type Output = Tensor;
fn add(self, rhs: IT) -> Self::Output {
self.new_binary_op(
self.data,
rhs.into_tensor(&self.graph.clone().into()).data,
"add",
)
}
}
impl<IT: IntoTensor> core::ops::Add<IT> for &Tensor {
type Output = Tensor;
fn add(self, rhs: IT) -> Self::Output {
self.new_binary_op(
self.data,
rhs.into_tensor(&self.graph.clone().into()).data,
"add",
)
}
}
impl<IT: IntoTensor> core::ops::Sub<IT> for Tensor {
type Output = Tensor;
fn sub(self, rhs: IT) -> Self::Output {
self.new_binary_op(
self.data,
rhs.into_tensor(&self.graph.clone().into()).data,
"sub",
)
}
}
impl<IT: IntoTensor> core::ops::Sub<IT> for &Tensor {
type Output = Tensor;
fn sub(self, rhs: IT) -> Self::Output {
self.new_binary_op(
self.data,
rhs.into_tensor(&self.graph.clone().into()).data,
"sub",
)
}
}
impl<IT: IntoTensor> core::ops::Mul<IT> for Tensor {
type Output = Tensor;
fn mul(self, rhs: IT) -> Self::Output {
self.new_binary_op(
self.data,
rhs.into_tensor(&self.graph.clone().into()).data,
"mul",
)
}
}
impl<IT: IntoTensor> core::ops::Mul<IT> for &Tensor {
type Output = Tensor;
fn mul(self, rhs: IT) -> Self::Output {
self.new_binary_op(
self.data,
rhs.into_tensor(&self.graph.clone().into()).data,
"mul",
)
}
}
impl<IT: IntoTensor> core::ops::Div<IT> for Tensor {
type Output = Tensor;
fn div(self, rhs: IT) -> Self::Output {
self.new_binary_op(
self.data,
rhs.into_tensor(&self.graph.clone().into()).data,
"div",
)
}
}
impl<IT: IntoTensor> core::ops::Div<IT> for &Tensor {
type Output = Tensor;
fn div(self, rhs: IT) -> Self::Output {
self.new_binary_op(
self.data,
rhs.into_tensor(&self.graph.clone().into()).data,
"div",
)
}
}
impl PartialEq<f32> for Tensor {
fn eq(&self, other: &f32) -> bool {
if self.shape() != 1 {
return false;
}
match self.dtype() {
DType::F32 => {
if let Some(data) = self.to_vec() {
if fabsf(data[0] - other) > EPSILON {
return false;
}
} else {
return false;
}
}
DType::I32 => {
return false;
}
}
true
}
}
impl<const L: usize> PartialEq<[f32; L]> for Tensor {
fn eq(&self, other: &[f32; L]) -> bool {
if self.shape() != L {
return false;
}
match self.dtype() {
DType::F32 => {
if let Some(data) = self.to_vec() {
for (x, y) in data.iter().zip(other) {
if fabsf(x - y) > EPSILON {
return false;
}
}
} else {
return false;
}
}
DType::I32 => {
return false;
}
}
true
}
}
impl<const L: usize, const M: usize> PartialEq<[[f32; L]; M]> for Tensor {
fn eq(&self, other: &[[f32; L]; M]) -> bool {
if self.shape() != (M, L) {
return false;
}
match self.dtype() {
DType::F32 => {
if let Some(data) = self.to_vec() {
for (x, y) in data.iter().zip(other.iter().flatten()) {
if fabsf(x - y) > EPSILON {
return false;
}
}
} else {
return false;
}
}
DType::I32 => {
return false;
}
}
true
}
}
impl<const L: usize, const M: usize, const N: usize> PartialEq<[[[f32; L]; M]; N]> for Tensor {
fn eq(&self, other: &[[[f32; L]; M]; N]) -> bool {
if self.shape() != (N, M, L) {
return false;
}
match self.dtype() {
DType::F32 => {
if let Some(data) = self.to_vec() {
for (x, y) in data.iter().zip(other.iter().flatten().flatten()) {
if fabsf(x - y) > EPSILON {
return false;
}
}
} else {
return false;
}
}
DType::I32 => {
return false;
}
}
true
}
}
impl PartialEq<i32> for Tensor {
fn eq(&self, other: &i32) -> bool {
if self.shape() != 1 {
return false;
}
match self.dtype() {
DType::F32 => {
return false;
}
DType::I32 => {
if let Some(data) = self.to_vec_i32() {
if data[0] != *other {
return false;
}
} else {
return false;
}
}
}
true
}
}
impl<const L: usize> PartialEq<[i32; L]> for Tensor {
fn eq(&self, other: &[i32; L]) -> bool {
if self.shape() != L {
return false;
}
match self.dtype() {
DType::F32 => {
return false;
}
DType::I32 => {
if let Some(data) = self.to_vec_i32() {
for (x, y) in data.iter().zip(other) {
if x != y {
return false;
}
}
} else {
return false;
}
}
}
true
}
}
impl<const L: usize, const M: usize> PartialEq<[[i32; L]; M]> for Tensor {
fn eq(&self, other: &[[i32; L]; M]) -> bool {
if self.shape() != (M, L) {
return false;
}
match self.dtype() {
DType::F32 => {
return false;
}
DType::I32 => {
if let Some(data) = self.to_vec_i32() {
for (x, y) in data.iter().zip(other.iter().flatten()) {
if x != y {
return false;
}
}
} else {
return false;
}
}
}
true
}
}
impl<const L: usize, const M: usize, const N: usize> PartialEq<[[[i32; L]; M]; N]> for Tensor {
fn eq(&self, other: &[[[i32; L]; M]; N]) -> bool {
if self.shape() != (N, M, L) {
return false;
}
match self.dtype() {
DType::F32 => {
return false;
}
DType::I32 => {
if let Some(data) = self.to_vec_i32() {
for (x, y) in data.iter().zip(other.iter().flatten().flatten()) {
if x != y {
return false;
}
}
} else {
return false;
}
}
}
true
}
}