burn-contracts

Crates.ioburn-contracts
lib.rsburn-contracts
version0.3.2
created_at2025-03-05 12:32:06.919281+00
updated_at2025-08-13 22:41:13.111715+00
descriptionFluent Contracts for the Burn library
homepage
repositoryhttps://github.com/crutcher/burn-contracts
max_upload_size
id1578953
size179,850
Crutcher Dunnavant (crutcher)

documentation

README

Deprecated: use bimm-contracts instead.

See: bimm-contracts

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

  • Bump burn dep to 0.17.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.

Commit count: 10

cargo fmt