Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous updates #76

Merged
merged 3 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .github/workflows/cargo-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ jobs:
profile: minimal
toolchain: stable
override: true
- uses: actions/setup-python@v4
with:
python-version: '3.9'
- run: pip install numpy
- uses: actions-rs/cargo@v1
with:
command: test
19 changes: 19 additions & 0 deletions examples/npy_serialize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use dfdx::numpy as np;

fn main() {
np::save("0d-rs.npy", &1.234).expect("Saving failed");
np::save("1d-rs.npy", &[1.0, 2.0, 3.0]).expect("Saving failed");
np::save("2d-rs.npy", &[[1.0, 2.0, 3.0], [-1.0, -2.0, -3.0]]).expect("Saving failed");

let mut expected_0d = 0.0;
np::load("0d-rs.npy", &mut expected_0d).expect("Loading failed");
assert_eq!(expected_0d, 1.234);

let mut expected_1d = [0.0; 3];
np::load("1d-rs.npy", &mut expected_1d).expect("Loading failed");
assert_eq!(expected_1d, [1.0, 2.0, 3.0]);

let mut expected_2d = [[0.0; 3]; 2];
np::load("2d-rs.npy", &mut expected_2d).expect("Loading failed");
assert_eq!(expected_2d, [[1.0, 2.0, 3.0], [-1.0, -2.0, -3.0]]);
}
22 changes: 22 additions & 0 deletions examples/tensors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use dfdx::prelude::*;

fn main() {
let a: Tensor2D<2, 3> = TensorCreator::zeros();

// since add() expects tensors with the same size, we dont need a type for this
let b = TensorCreator::ones();
let c = add(a, &b);

// tensors just store raw rust arrays, use `.data()` to access this.
assert_eq!(c.data(), &[[1.0; 3]; 2]);

// since we pass in an array, rust will figure out that we mean Tensor1D<5> since its an [f32; 5]
let mut d = Tensor1D::new([1.0, 2.0, 3.0, 4.0, 5.0]);

// use `.mut_data()` to access underlying mutable array. type is provided for readability
let raw_data: &mut [f32; 5] = d.mut_data();
for i in 0..5 {
raw_data[i] *= 2.0;
}
assert_eq!(d.data(), &[2.0, 4.0, 6.0, 8.0, 10.0]);
}
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
//! ```rust
//! # use dfdx::prelude::*;
//! let mut mlp: Linear<5, 2> = Default::default();
//! let x: Tensor1D<5> = Tensor1D::zeros();
//! let x = Tensor1D::zeros(); // rust figures out that x must be a `Tensor1D<5>` bc its given to mlp.forward()!
//! let y = mlp.forward(x); // rust will auto figure out that `y` is `Tensor1D<2>`!
//! ```
//!
Expand All @@ -53,8 +53,9 @@
//! // `.trace()` clones `x` and inserts a gradient tape.
//! let x_t: Tensor1D<10, OwnedTape> = x.trace();
//!
//! // The tape is moved through the model during `.forward()`, and ends up in `y`.
//! let y: Tensor1D<5, OwnedTape> = model.forward(x_t);
//! // The tape from the input is moved through the network during .forward().
//! let y: Tensor1D<5, NoneTape> = model.forward(x);
//! let y_t: Tensor1D<5, OwnedTape> = model.forward(x_t);
//! ```
//!
//! 6. Compute gradients with [crate::tensor_ops::backward()]
Expand Down
66 changes: 44 additions & 22 deletions src/numpy/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,9 @@ impl<T: WriteNumbers, const M: usize> WriteNumbers for [T; M] {

#[cfg(test)]
mod tests {
use tempfile::NamedTempFile;

use super::*;
use std::process::Command;

fn describe(p: &Path) -> String {
let output = Command::new("python")
.arg("-c")
.arg(format!(
"import numpy;a=numpy.load({:?});print(a.dtype, a.shape);print(a)",
p.as_os_str(),
))
.output()
.expect("Creating sub process failed");
assert!(output.stderr.is_empty(), "{:?}", output.stderr);
String::from_utf8(output.stdout).expect("")
}
use std::io::Read;
use tempfile::NamedTempFile;

#[test]
fn test_0d_f32_save() {
Expand All @@ -148,9 +134,21 @@ mod tests {
let file = NamedTempFile::new().expect("failed to create tempfile");

save(file.path(), &data).expect("Saving failed");

let mut f = File::open(file.path()).expect("No file found");

let mut found = Vec::new();
f.read_to_end(&mut found).expect("Reading failed");

assert_eq!(
describe(file.path()).replace("\r\n", "\n"),
"float32 ()\n0.0\n"
&found,
&[
147, 78, 85, 77, 80, 89, 1, 0, 64, 0, 123, 39, 100, 101, 115, 99, 114, 39, 58, 32,
39, 60, 102, 52, 39, 44, 32, 39, 102, 111, 114, 116, 114, 97, 110, 95, 111, 114,
100, 101, 114, 39, 58, 32, 70, 97, 108, 115, 101, 44, 32, 39, 115, 104, 97, 112,
101, 39, 58, 32, 40, 41, 44, 32, 125, 32, 32, 32, 32, 32, 32, 32, 32, 10, 0, 0, 0,
0,
]
);
}

Expand All @@ -161,9 +159,21 @@ mod tests {
let file = NamedTempFile::new().expect("failed to create tempfile");

save(file.path(), &data).expect("Saving failed");

let mut f = File::open(file.path()).expect("No file found");

let mut found = Vec::new();
f.read_to_end(&mut found).expect("Reading failed");

assert_eq!(
describe(file.path()).replace("\r\n", "\n"),
"float32 (5,)\n[ 0. 1. 2. 3. -4.]\n"
&found,
&[
147, 78, 85, 77, 80, 89, 1, 0, 64, 0, 123, 39, 100, 101, 115, 99, 114, 39, 58, 32,
39, 60, 102, 52, 39, 44, 32, 39, 102, 111, 114, 116, 114, 97, 110, 95, 111, 114,
100, 101, 114, 39, 58, 32, 70, 97, 108, 115, 101, 44, 32, 39, 115, 104, 97, 112,
101, 39, 58, 32, 40, 53, 44, 41, 44, 32, 125, 32, 32, 32, 32, 32, 32, 10, 0, 0, 0,
0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 128, 192
]
);
}

Expand All @@ -174,9 +184,21 @@ mod tests {
let file = NamedTempFile::new().expect("failed to create tempfile");

save(file.path(), &data).expect("Saving failed");

let mut f = File::open(file.path()).expect("No file found");

let mut found = Vec::new();
f.read_to_end(&mut found).expect("Reading failed");

assert_eq!(
describe(file.path()).replace("\r\n", "\n"),
"float32 (2, 3)\n[[0. 1. 2.]\n [3. 4. 5.]]\n"
&found,
&[
147, 78, 85, 77, 80, 89, 1, 0, 64, 0, 123, 39, 100, 101, 115, 99, 114, 39, 58, 32,
39, 60, 102, 52, 39, 44, 32, 39, 102, 111, 114, 116, 114, 97, 110, 95, 111, 114,
100, 101, 114, 39, 58, 32, 70, 97, 108, 115, 101, 44, 32, 39, 115, 104, 97, 112,
101, 39, 58, 32, 40, 50, 44, 32, 51, 41, 44, 32, 125, 32, 32, 32, 32, 10, 0, 0, 0,
0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 128, 64, 0, 0, 160, 64
]
);
}
}
29 changes: 29 additions & 0 deletions src/tensor/impl_tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,32 @@ tensor_impl!(
[M, N, O],
[[[usize; O]; N]; M]
);

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_ids_with_duplicate() {
let t1: Tensor1D<32> = TensorCreator::zeros();
let t2: Tensor1D<32, NoneTape> = t1.duplicate();
assert_eq!(t1.id, t2.id);
}

#[test]
fn test_ids_with_clone() {
let t1: Tensor1D<32> = TensorCreator::zeros();
let t2: Tensor1D<32, NoneTape> = t1.clone();
assert_ne!(t1.id, t2.id);
}

#[test]
fn test_ids_with_split_and_put() {
let t1: Tensor1D<32> = TensorCreator::zeros();
let t1_id = t1.id;
let (t2, tape) = t1.split_tape();
assert_eq!(t2.id, t1_id);
let t3 = t2.put_tape(tape);
assert_eq!(t3.id, t1_id);
}
}
28 changes: 28 additions & 0 deletions src/tensor/impl_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,31 @@ tensor_impl!(Tensor1D, [M]);
tensor_impl!(Tensor2D, [M, N]);
tensor_impl!(Tensor3D, [M, N, O]);
tensor_impl!(Tensor4D, [M, N, O, P]);

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_trace() {
let t1: Tensor1D<32> = TensorCreator::zeros();
let t2: Tensor1D<32, OwnedTape> = trace(&t1);
assert_eq!(t1.id, t2.id);
}

#[test]
fn test_traced() {
let t1: Tensor1D<32> = TensorCreator::zeros();
let t1_id = t1.id;
let t2: Tensor1D<32, OwnedTape> = traced(t1);
assert_eq!(t1_id, t2.id);
}

#[test]
fn test_trace_split() {
let t1: Tensor1D<32> = TensorCreator::zeros();
let t2: Tensor1D<32, OwnedTape> = t1.trace();
let (t3, tape): (Tensor1D<32, NoneTape>, OwnedTape) = t2.split_tape();
let _: Tensor1D<32, OwnedTape> = t3.put_tape(tape);
}
}