1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! Tensors, automatic differentiation, device selection, and errors for RustTorch.
//!
//! Use this crate when you need tensor calculations without the higher-level
//! model and data-loading APIs. Applications that train neural networks can
//! instead use [`rusttorch`](https://docs.rs/rusttorch), which re-exports these
//! types alongside its layers and optimizers.
//!
//! [`Tensor`] is backed by LibTorch and supports numerical operations, views,
//! reductions, and gradient recording. Fallible methods use the `f_` prefix;
//! their errors convert into the crate's [`Result`] type.
//!
//! # Calculate a gradient
//!
//! Mark leaf tensors as requiring gradients before constructing a calculation.
//! Backpropagating a scalar result fills each leaf tensor's gradient.
//!
//! ```
//! use rusttorch_core::{Kind, Result, Tensor};
//!
//! # fn main() -> Result<()> {
//! let values = Tensor::f_from_slice(&[2_f32, 3.])?.set_requires_grad(true);
//! let squared_sum = values.f_mul(&values)?.f_sum(Kind::Float)?;
//! squared_sum.f_backward()?;
//!
//! let gradient = values.grad();
//! assert_eq!(gradient.double_value(&[0]), 4.0);
//! assert_eq!(gradient.double_value(&[1]), 6.0);
//! # Ok(())
//! # }
//! ```
//!
//! Use [`no_grad`] or [`no_grad_guard`] for calculations that do not need a
//! gradient graph, such as prediction and manual parameter updates.
//!
//! # Select a compute device
//!
//! Keep tensors participating in an operation on the same device.
//! [`DeviceSpec::Auto`] selects CUDA, then MPS, then CPU according to runtime
//! availability. An explicit request returns an error if the device is not
//! available; [`available_devices`] reports the usable backends.
//!
//! ```
//! use rusttorch_core::{DeviceSpec, Kind, Result, Tensor, resolve_device};
//!
//! # fn main() -> Result<()> {
//! let device = resolve_device(DeviceSpec::Cpu)?;
//! let features = Tensor::f_zeros([8, 4], (Kind::Float, device))?;
//! assert_eq!(features.device(), device);
//! # Ok(())
//! # }
//! ```
pub use ;
pub use ;
pub use ;
/// Seeds LibTorch's shared random generator before drawing tensors.
///
/// Use this to reproduce the order of draws on one runtime/device. Other threads
/// share the generator, and matching seeds do not guarantee identical streams on
/// different backends. Dataset samplers have their own explicit seed controls.
///
/// ```
/// use rusttorch_core::{Device, Kind, Tensor, manual_seed};
/// manual_seed(42);
/// let first = Tensor::randn([3], (Kind::Float, Device::Cpu));
/// manual_seed(42);
/// let repeated = Tensor::randn([3], (Kind::Float, Device::Cpu));
/// assert!(first.equal(&repeated));
/// ```