#contract #burn #tensor

burn-contracts

Fluent Contracts for the Burn library

4 releases (2 breaking)

new 0.3.0 Apr 23, 2025
0.2.1 Mar 25, 2025
0.2.0 Mar 24, 2025
0.1.0 Mar 5, 2025

#554 in Testing

Download history 115/week @ 2025-02-28 34/week @ 2025-03-07 201/week @ 2025-03-21 37/week @ 2025-03-28 1/week @ 2025-04-11

239 downloads per month

MIT license

45KB
824 lines

Fluent Api for Burn Contract and Test Assertions

This crate provides a fluent api for contract and test assertions for the Burn framework.

use burn_contracts::assert_tensor;

let tensor: Tensor<B, 4 > = Tensor::new(& [10, 3, 32, 17]);
assert_tensor( & tensor)
.has_named_dims([('N', 10), ('C', 3), ('H', 32), ('W', 32)]);
// Panics:
// "Expected tensor to have dimensions [('N', 10), ('C', 3), ('H', 32), ('W', 32)] but got [(10, 3, 32, 17)]"

Assertions

The following assertions are available:

'.has_dims()'

fn example<B: Backend>() {
    let tensor = Tensor::<B, 4>::zeros([10, 3, 32, 17], &device);

    assert_tensor(&tensor).has_dims([10, 3, 32, 17]).unwrap();
}

'.has_named_dims()'

fn example<B: Backend>() {
    let tensor = Tensor::<B, 4>::zeros([10, 3, 32, 17], &device);

    assert_tensor(&tensor).has_named_dims(
        [("N", 10), ("C", 3), ("H", 32), ("W", 17)],
    ).unwrap();
}

'.unpacks_shape()'

fn example<B: Backend>() {
    let tensor = Tensor::<B, 6>::zeros([2, 2, 2, 5 * 4, 4 * 4, 3], &device);

    let [b, h, w] = assert_tensor(&tensor).unpacks_shape(
        ["b", "h", "w"],
        "b ... (h p) (w p) c",
        &[("p", 4), ("c", 3)],
    ).unwrap();

    assert_eq!(b, 2);
    assert_eq!(h, 5);
    assert_eq!(w, 4);
}

Testing API

The "testing" feature enables the testing api; which provides expensive methods for testing tensor contents.

To ensure that the testing api is not used in production, the testing api is only available when the "testing" feature is enabled.

To enable the "testing" feature only for testing, add the following to your Cargo.toml:

[dependencies]
burn-contracts = $VERSION

[dev-dependencies]
burn-contracts = { version = $VERSION, features = ["testing"] }

Change Log

0.3.0

  • Update to burn=0.17.0
  • Remove bincode hack for burn=0.16.0

0.2.1

  • Reduce over-broad burn feature deps.
  • Clean up pattern formatting.

0.2.0

Added assert_tensor(&tensor).unpacks_shape(...) assertion.

0.1.0

Initial release.

Dependencies

~57–89MB
~1.5M SLoC