Skip to main content

burn_tensor/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2#![warn(missing_docs)]
3#![cfg_attr(docsrs, feature(doc_cfg))]
4
5//! Burn's tensor API.
6//!
7//! [`Tensor<D, K>`](Tensor) is a tensor of rank `D` and kind `K` ([`Float`] by default, [`Int`]
8//! or [`Bool`]). Its [`Device`] decides which backend runs it, at runtime: the same `Tensor<2>`
9//! can live on CUDA, wgpu or the CPU, and code that uses tensors has no backend type parameter.
10//! Applications use this crate through `burn::tensor`.
11//!
12//! ```rust,no_run
13//! use burn_tensor::{Device, Tensor, s};
14//!
15//! let device = Device::default();
16//! let x = Tensor::<2>::from_floats([[1.0, 2.0], [3.0, 4.0]], &device);
17//! let y = x.clone().matmul(x.transpose()).exp();
18//! let first_row = y.slice(s![0..1, ..]);
19//! ```
20//!
21//! - [`Device`]: backend selection, configuration and enumeration. A backend's constructor
22//!   (`Device::cuda`, `Device::wgpu`, `Device::flex`, ...) exists when its feature is enabled.
23//! - [`TensorData`], [`DType`] and [`Shape`]: tensor contents and metadata, independent of any
24//!   backend.
25//! - [`activation`], [`loss`] and [`module`]: functional forms of activations, losses and neural
26//!   network operations such as convolution and pooling.
27//! - [`quantization`], [`grid`] and [`distributed`]: quantized tensors, grid sampling and
28//!   collective operations across devices.
29//! - `einsum!`, `assert_shape!` and [`s!`]: macros for Einstein summation, shape checks and
30//!   slicing.
31//!
32//! With the `autodiff` feature, create tensors on a `device.autodiff()` device and mark source
33//! leaves with [`Tensor::require_grad`] to record gradients.
34//!
35//! # Feature flags
36//!
37//! - `std` (default): standard library support. Without it the crate is `no_std` with `alloc`.
38//! - Backends: `cuda`, `rocm`, `wgpu`, `metal`, `vulkan`, `webgpu`, `cpu`, `flex`, and the
39//!   deprecated `ndarray` and `tch`.
40//! - `autodiff`, `fusion`: backend decorators.
41//! - `remote`, `remote-server`, `remote-websocket`: devices hosted by another machine.
42//! - `capture`: record operation graphs instead of executing them.
43//! - `extension`: access to backend primitives for backend extensions.
44//! - `tracing`: instrument operations with the `tracing` crate.
45//!
46//! # Note for contributors: `*_impl` helpers
47//!
48//! Throughout this crate (e.g. in `tensor::api::float`, `tensor::api::int`,
49//! `tensor::api::bool`, `tensor::api::cast`, and `tensor::activation`), public
50//! generic methods on `Tensor<D, K>` that need to call into `burn_dispatch` are
51//! routed through small non-generic helper functions named `*_impl`, grouped
52//! together at the bottom of each file under a banner like:
53//!
54//! ```text
55//! // =====================================================================
56//! // Non-generic implementation helpers (outlined from the generic API).
57//! // =====================================================================
58//! ```
59//!
60//! These helpers take and return only `BridgeTensor` (a type-erased blob — no
61//! `DispatchTensor` or other `burn_dispatch` types appear in their
62//! signatures). Because the helpers are not generic over `D`, they are
63//! compiled once, and the MIR of the public generic methods does not mention
64//! any `burn_dispatch` types. Downstream crates that monomorphize the public
65//! generic API therefore never have to resolve the cubecl-backed type tree,
66//! which drastically cuts compile times for user code.
67//!
68//! When adding a new public method that calls a `Dispatch::*` op, follow the
69//! same pattern: keep the generic method body thin and forward to a
70//! non-generic `*_impl` helper alongside the existing ones.
71
72#[macro_use]
73extern crate derive_new;
74
75extern crate alloc;
76
77mod bridge;
78mod tensor;
79
80pub(crate) use tensor::check::macros::check;
81pub use tensor::*;
82
83mod einsum_macros;
84mod shape_macros;
85#[doc(hidden)]
86pub use burn_derive::{__assert_shape, __debug_assert_shape, __einsum};
87
88// Re-exported types
89#[cfg(feature = "autodiff")]
90pub use burn_dispatch::GradientCheckpointingStrategy;
91pub use burn_std::{
92    AllocationProperty, Bytes, bf16, f16, flex32,
93    reader::{read_sync, try_read_sync},
94    stream::StreamId,
95};
96
97mod device;
98pub use device::*;
99
100/// Configure a wgpu runtime or share an application's existing wgpu setup.
101#[cfg(feature = "wgpu")]
102pub mod wgpu;
103
104#[cfg(feature = "remote")]
105pub mod remote;
106#[cfg(feature = "remote-server")]
107pub mod server;
108
109pub(crate) use burn_backend::TensorPrimitive;