Skip to content

Commit

Permalink
Add examples of runtime dimensions to examples/02-ops.rs (#698)
Browse files Browse the repository at this point in the history
* Add example for `realize` method

* Add unary and binary egs for dynamic tensors
  • Loading branch information
VasanthakumarV committed Apr 12, 2023
1 parent 7e359e4 commit 6e6f544
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
6 changes: 6 additions & 0 deletions examples/01-tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use dfdx::{
shapes::{Const, Rank1, Rank2, Rank3},
tensor::{AsArray, OnesTensor, SampleTensor, Tensor, TensorFrom, ZerosTensor},
tensor_ops::RealizeTo,
};

#[cfg(not(feature = "cuda"))]
Expand Down Expand Up @@ -35,6 +36,11 @@ fn main() {
let _: Tensor<(usize, usize, usize), f32, _> = dev.ones_like(&(3, 4, 5));
let _: Tensor<(usize, usize, Const<5>), f32, _> = dev.ones_like(&(3, 4, Const));

// `realize` method helps us move between dynamic and known size for the dimensions,
// if the conversion is incompatible, it may result in runtime error
let a: Tensor<(usize, usize), f32, _> = dev.zeros_like(&(2, 3));
let _: Tensor<(usize, Const<3>), f32, _> = a.realize().expect("`a` should have 3 columns");

// each of the creation methods also supports specifying the shape on the function
// note to change the dtype we specify the dtype as the 2nd generic parameter
let _: Tensor<Rank2<2, 3>, f64, _> = dev.zeros();
Expand Down
22 changes: 16 additions & 6 deletions examples/02-ops.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Intro to dfdx::tensor_ops

use dfdx::{
shapes::{Rank0, Rank1, Rank2},
shapes::{Const, Rank0, Rank1, Rank2},
tensor::{AsArray, AutoDevice, SampleTensor, Tensor},
tensor_ops::{MeanTo, TryMatMul},
tensor_ops::{MeanTo, RealizeTo, TryMatMul},
};

fn main() {
Expand Down Expand Up @@ -42,11 +42,21 @@ fn main() {
.powf(0.5)
/ 2.0;

// binary and unary operations can also be performed on dynamically sized tensors
let mut a: Tensor<(Const<3>, usize), f32, _> = dev.sample_uniform_like(&(Const, 5));
a = a + 0.5;
let b: Tensor<(usize, Const<5>), f32, _> = dev.sample_uniform_like(&(3, Const));
// note the use of `realize`
let _: Tensor<(Const<3>, usize), f32, _> = a + b.realize().expect("`b` should have 3 rows");

// then we have things like matrix and vector multiplication:
let a: Tensor<Rank2<3, 5>, f32, _> = dev.sample_normal();
let b: Tensor<Rank2<5, 7>, f32, _> = dev.sample_normal();
let c = a.matmul(b);
dbg!(c.array());
let a: Tensor<(usize, Const<5>), f32, _> = dev.sample_normal_like(&(3, Const));
let b: Tensor<(usize, usize), f32, _> = dev.sample_normal_like(&(5, 7));
// if type inference is not possible, we explicitly provide the shape for `realize`
let _: Tensor<(usize, usize), f32, _> = a.matmul(
b.realize::<(Const<5>, usize)>()
.expect("`b` should have 5 rows"),
);

// which even the outer product between two vectors!
let a: Tensor<Rank1<3>, f32, _> = dev.sample_normal();
Expand Down

0 comments on commit 6e6f544

Please sign in to comment.