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;
46pub mod cpu_accel;
47pub mod cuda_backend;
48pub mod decode_shape;
49pub mod device;
50pub mod dtype;
51pub mod factories;
52pub mod inference_context;
53pub mod judge;
54pub mod kv_block_pool_device;
55pub mod lazy;
56pub mod lazy_latent_cache;
57pub mod metal_backend;
58pub mod nf4;
59pub mod persistent_decode;
60pub mod pipelined_bridge;
61pub mod planner;
62pub mod reproducibility;
63pub mod sampling;
64pub mod scheduling;
65pub mod test_utils;
66#[cfg(feature = "vulkan")]
67pub mod vulkan_backend;
68
69pub use device::{Device, DeviceLocation, NdArray};
70pub use dtype::{DType, DTypeParseError, FloatDType, IntDType, WithDType};
71pub use fuel_ir::error::{Context, Error, Result};
72// `device.rs` uses `crate::Layout`/`crate::Shape` (root-qualified) internally;
73// these are the same fuel_ir re-exports fuel-core's own root carries, needed
74// here because fuel-tensor is now the crate device.rs's `crate::` resolves
75// within.
76pub use fuel_ir::layout::Layout;
77pub use fuel_ir::shape::{D, Shape};
78// device.rs's `use crate::{DType, HostBuffer, Result, Shape, Storage,
79// WithDType};` -- a GROUPED import, which an earlier `crate::X` text census
80// missed (it doesn't contain the substring `crate::HostBuffer`, only
81// `HostBuffer` inside `crate::{...}`'s braces). Caught by the first real
82// `cargo check`, not the census.
83pub use fuel_backend_contract::Storage;
84pub use fuel_ir::HostBuffer;
85
86#[doc(hidden)]
87#[cfg(feature = "cuda")]
88pub use cuda_backend as cuda;
89
90#[cfg(feature = "cuda")]
91pub use cuda_backend::{CudaDevice, CudaStorage};
92
93#[cfg(feature = "cuda")]
94pub use fuel_cuda_backend::builder_arg;
95
96#[cfg(feature = "metal")]
97pub use metal_backend::{MetalDevice, MetalError, MetalStorage};
98
99#[cfg(feature = "mkl")]
100extern crate intel_mkl_src;
101
102#[cfg(feature = "accelerate")]
103extern crate accelerate_src;
104
105// `bail!` consolidation (fuel-core dissolution, GAP-347 PR 4): this used to
106// be a LOCAL duplicate of `fuel_core::bail!`, kept separate only because
107// depending on `fuel_core::bail!` directly would have created a
108// `fuel-tensor` <-> `fuel-core` cargo cycle (`fuel-core` needs
109// `fuel-tensor` for `train.rs`). That reasoning never applied to
110// `fuel_ir`: this crate already depends on it directly (`Error`/`Result`
111// above are a bare re-export of `fuel_ir::error::{Error, Result}`, not a
112// distinct type), and `fuel_ir` is tier 10 — below everything. `$crate`
113// inside a `macro_rules!` body resolves to the crate that WROTE the
114// macro, not the one that re-exports or invokes it, so re-exporting here
115// keeps `fuel_tensor::bail!` producing byte-identical `fuel_ir::Error::Msg`
116// values — proved by the pre-existing `bail_tests::bail_macro_returns_a_typed_err`
117// below, which is unchanged and still exercises this re-export.
118pub use fuel_ir::bail;
119
120#[cfg(test)]
121mod bail_tests {
122 use crate::bail;
123 use fuel_ir::error::Result;
124
125 fn always_bails() -> Result<()> {
126 bail!("deliberate failure for the macro test");
127 #[allow(unreachable_code)]
128 Ok(())
129 }
130
131 #[test]
132 fn bail_macro_returns_a_typed_err() {
133 let err = always_bails().expect_err("bail! must produce an Err, never panic");
134 assert!(err.to_string().contains("deliberate failure"));
135 }
136}