Skip to main content

fuel_tensor/
lib.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! `fuel-tensor` — Fuel's user-facing `Tensor` handle and `Device`
3//! abstraction, moved here from `fuel-core` (board #109; CireSnave:
4//! "Move Tensor into fuel-tensor. We can worry about a semantic split
5//! later."). This is a MECHANICAL move -- every item below kept its own
6//! content; only the crate boundary changed. `fuel-core`'s own
7//! `lib.rs` re-exports these same paths so existing
8//! `fuel_tensor::lazy::Tensor` / `fuel::Tensor` call sites keep compiling.
9//!
10//! The moved cluster (`lazy`, `device`, the 3 GPU backend bridges,
11//! `dtype`'s `WithDType`/`IntDType`/`FloatDType` trio, `pipelined_bridge`,
12//! `judge`, `factories`, `planner`, `decode_shape`, `lazy_latent_cache`,
13//! `test_utils`, `scheduling`, `inference_context`,
14//! `kv_block_pool_device`, `persistent_decode`, `nf4`) moved together in
15//! one PR because it is one mutually-referencing graph, not several
16//! independent files that happen to touch `Tensor` -- measured via a
17//! full crate::-reference census before any file moved, not assumed
18//! from file names. See `docs/release-0.13.0-wave.md`.
19//!
20//! ```rust
21//! use fuel_tensor::lazy::{Tensor, realize_many_f32};
22//! use fuel_tensor::Device;
23//! # use fuel_tensor::Error;
24//! # fn main() -> Result<(), Error> {
25//! let dev = Device::cpu();
26//! let a = Tensor::from_f32((0..6).map(|x| x as f32).collect::<Vec<_>>(), (2, 3), &dev)?;
27//! let b = Tensor::from_f32_on(a.graph(), (0..12).map(|x| x as f32).collect::<Vec<_>>(), (3, 4), &dev)?;
28//! let c = a.matmul(&b)?;
29//! assert_eq!(c.shape().dims(), &[2, 4]);
30//! let out = realize_many_f32(&[&c]);
31//! assert_eq!(out[0].len(), 8);
32//! # Ok(())}
33//! ```
34
35// GAP-229: `clippy::identity_op` fires across fuel-core+fuel-dispatch (and now
36// fuel-tensor, since the flagged sites in judge/mod.rs and lazy.rs moved here)
37// and is a defect in 0 of them — it measures a house idiom, not debt, so it is
38// allowed at the crate root. Carried forward verbatim from fuel-core/src/lib.rs
39// (board #109 move) rather than re-derived, since the measurement and the two
40// intentional classes it documents (DOC-INDEX, DOC-SHAPE) are about the CODE
41// that moved, not about which crate currently compiles it. See fuel-core's own
42// copy of this comment (unaffected by this move) for the full rationale.
43#![allow(clippy::identity_op)]
44
45pub mod awq;
46mod const_residency;
47pub use const_residency::WeightResidencyStats;
48pub mod cpu_accel;
49pub mod cuda_backend;
50pub mod decode_shape;
51pub mod device;
52pub mod dtype;
53pub mod factories;
54pub mod inference_context;
55pub mod judge;
56pub mod kv_block_pool_device;
57pub mod lazy;
58pub mod lazy_latent_cache;
59pub mod metal_backend;
60pub mod nf4;
61pub mod persistent_decode;
62pub mod pipelined_bridge;
63pub mod planner;
64pub mod reproducibility;
65pub mod sampling;
66pub mod scheduling;
67pub mod test_utils;
68#[cfg(feature = "vulkan")]
69pub mod vulkan_backend;
70
71pub use device::{Device, DeviceLocation, NdArray};
72pub use dtype::{DType, DTypeParseError, FloatDType, IntDType, WithDType};
73pub use fuel_ir::error::{Context, Error, Result};
74// `device.rs` uses `crate::Layout`/`crate::Shape` (root-qualified) internally;
75// these are the same fuel_ir re-exports fuel-core's own root carries, needed
76// here because fuel-tensor is now the crate device.rs's `crate::` resolves
77// within.
78pub use fuel_ir::layout::Layout;
79pub use fuel_ir::shape::{D, Shape};
80// device.rs's `use crate::{DType, HostBuffer, Result, Shape, Storage,
81// WithDType};` -- a GROUPED import, which an earlier `crate::X` text census
82// missed (it doesn't contain the substring `crate::HostBuffer`, only
83// `HostBuffer` inside `crate::{...}`'s braces). Caught by the first real
84// `cargo check`, not the census.
85pub use fuel_backend_contract::Storage;
86pub use fuel_ir::HostBuffer;
87
88#[doc(hidden)]
89#[cfg(feature = "cuda")]
90pub use cuda_backend as cuda;
91
92#[cfg(feature = "cuda")]
93pub use cuda_backend::{CudaDevice, CudaStorage};
94
95#[cfg(feature = "cuda")]
96pub use fuel_cuda_backend::builder_arg;
97
98#[cfg(feature = "metal")]
99pub use metal_backend::{MetalDevice, MetalError, MetalStorage};
100
101#[cfg(feature = "accelerate")]
102extern crate accelerate_src;
103
104// `bail!` consolidation (fuel-core dissolution, GAP-347 PR 4): this used to
105// be a LOCAL duplicate of `fuel_core::bail!`, kept separate only because
106// depending on `fuel_core::bail!` directly would have created a
107// `fuel-tensor` <-> `fuel-core` cargo cycle (`fuel-core` needs
108// `fuel-tensor` for `train.rs`). That reasoning never applied to
109// `fuel_ir`: this crate already depends on it directly (`Error`/`Result`
110// above are a bare re-export of `fuel_ir::error::{Error, Result}`, not a
111// distinct type), and `fuel_ir` is tier 10 — below everything. `$crate`
112// inside a `macro_rules!` body resolves to the crate that WROTE the
113// macro, not the one that re-exports or invokes it, so re-exporting here
114// keeps `fuel_tensor::bail!` producing byte-identical `fuel_ir::Error::Msg`
115// values — proved by the pre-existing `bail_tests::bail_macro_returns_a_typed_err`
116// below, which is unchanged and still exercises this re-export.
117pub use fuel_ir::bail;
118
119#[cfg(test)]
120mod bail_tests {
121    use crate::bail;
122    use fuel_ir::error::Result;
123
124    fn always_bails() -> Result<()> {
125        bail!("deliberate failure for the macro test");
126        #[allow(unreachable_code)]
127        Ok(())
128    }
129
130    #[test]
131    fn bail_macro_returns_a_typed_err() {
132        let err = always_bails().expect_err("bail! must produce an Err, never panic");
133        assert!(err.to_string().contains("deliberate failure"));
134    }
135}