Skip to main content

coeus_ops/
lib.rs

1//! Tensor operation kernels and backend dispatch for the Coeus stack.
2//!
3//! # Operation families
4//! - **Elementwise** — [`unary`] and [`binary`] kernels dispatched via [`ElementwiseOps::elementwise_unary`] / [`ElementwiseOps::elementwise_binary`].
5//! - **Linear algebra** — [`matmul()`], [`sparse`] SpMM/SpMV.
6//! - **Reductions** — [`reduction`]: `sum`, `mean`, `max/min`, `argmax/argmin`, `topk`, cumulative sum/product scans, plus vector arithmetic `dot` (flat inner product) and `cross` (per-channel 3-vector cross along `dim`), plus matrix norms `frobenius_norm` / `frobenius_norm_batched` (compose on `norm` for `torch.linalg.matrix_norm(A, ord='fro')`).
7//! - **Convolution** — 1-D/2-D/3-D forward+backward routed through `BackendOps::conv1d`/`conv2d`/`conv3d`.
8//! - **Pooling** — max and average pooling (2-D/3-D) with backward gradients.
9//! - **Resize** — nearest/bilinear/bicubic/area spatial resize (1-D/2-D) in [`resize`], with
10//!   the matching reverse-mode scatter.
11//! - **Attention** — [`attention::scaled_dot_product_attention`] with causal/padding mask support.
12//! - **Fused expressions** — [`fuse`] lazy expression DAG evaluated in a single pass on CPU.
13//! - **Optimizer steps** — provider-dispatched `sgd_step`, `adam_step`,
14//!   `adamw_step`, `rmsprop_step`, and `adagrad_step` on [`OptimizerOps`].
15
16// ── Coeus Ops ──
17// Tensor operations: unary, binary, matmul, reductions, FFT.
18#![deny(missing_docs)]
19/// Backend dispatch trait and CPU implementation.
20pub mod backend_ops;
21pub(crate) mod ptr;
22pub use backend_ops::{
23    cross_fold, AttentionOps, AttentionScalar, Axis, BackendOps, BinaryOp, ConvOps,
24    ConvolutionBackward, ConvolutionForward, CpuBackend, CrossEntropyOps, CrossOps, CtcBatch,
25    CtcOps, ElementwiseOps, FiniteDifference3DOps, FiniteDifference3DScheme, MatmulOps,
26    OptimizerOps, OptimizerStateRef, OptimizerStepRule, OptimizerStepValidation, PoolOps,
27    RandomInitOps, ReductionOp, ReductionOps, RotateHalfOps, ScalarPowerOps, StaggeredPairOps,
28    UnaryOp, UnfoldFoldOps,
29};
30/// Element-wise binary operations (add, sub, mul, div).
31pub mod binary;
32/// Embedding lookup and backward pass.
33pub mod embedding;
34/// Coordinate-grid interpolation operations.
35pub mod interpolation;
36/// Matrix multiplication kernels (matmul, bmm, outer).
37pub mod matmul;
38/// Reduction operations (sum, mean, max, min, norms, variance).
39pub mod reduction;
40/// Spatial resize (nearest / bilinear / bicubic / area) forward and reverse mode.
41pub mod resize;
42/// Provider-selected half-vector rotation.
43pub mod rotate_half;
44/// Shape manipulation operations (cat, stack, reshape, flip, sort, etc.).
45pub mod shape;
46/// Sparse tensor operations (SpMM, SpMV, format conversions).
47pub mod sparse;
48/// Element-wise unary operations (activations, math functions).
49pub mod unary;
50
51pub use unary::{
52    abs, abs_assign, acos, acosh, asin, asinh, atan, atanh, causal_softmax, ceil, ceil_assign, cos,
53    cos_assign, cosh, elementwise_unary, elementwise_unary_assign, elementwise_unary_to, elu,
54    elu_assign, erf, erfc, exp, exp2, exp_assign, expm1, floor, floor_assign, gelu, gelu_assign,
55    gelu_tanh, gelu_tanh_assign, glu, leaky_relu, leaky_relu_assign, lgamma, log, log10, log1p,
56    log2, log_assign, log_softmax_axis, masked_softmax, mish, mish_assign, neg, neg_assign,
57    pow_scalar, recip, recip_assign, relu, relu_assign, round, round_assign, sigmoid,
58    sigmoid_assign, sign, sign_assign, silu, silu_assign, sin, sin_assign, sinh, softplus,
59    softplus_assign, sqrt, sqrt_assign, tan, tanh, tanh_assign, trunc, trunc_assign,
60};
61
62pub use binary::{
63    add, add_assign, div, div_assign, elementwise_binary, elementwise_binary_to, eq, ge, gt, le,
64    lt, mul, mul_assign, ne, sub, sub_assign,
65};
66pub use embedding::{embedding, embedding_backward, embedding_backward_with_padding_idx};
67pub use interpolation::{
68    linear_interpolation, linear_interpolation_backward, BoundaryPolicy, Dimension,
69    InterpolationError, InterpolationFailure, InterpolationGradients, Replicate,
70    SupportedDimension,
71};
72pub use matmul::{bmm, matmul, matmul_accumulate, outer};
73pub use reduction::{
74    amax, amin, argmax, argmin, cross, cumprod, cumsum, dot, frobenius_norm,
75    frobenius_norm_batched, max_axis, mean, mean_axis, min_axis, norm, norm_p, norm_p_axis,
76    norm_p_tensor, prod, prod_axis, prod_tensor, std_dev, std_dev_axis, std_mean, std_mean_axis,
77    suffix_prod, suffix_sum, sum, sum_axis, topk, var, var_axis, var_mean, var_mean_axis,
78};
79pub use resize::{
80    resize_1d, resize_1d_backward, resize_1d_backward_with_scale, resize_1d_with_scale, resize_2d,
81    resize_2d_backward, resize_2d_backward_with_scale, resize_2d_with_scale, ResizeMode,
82};
83pub use rotate_half::rotate_half;
84pub use shape::{
85    broadcast_to, cat, chunk, diag, diagonal, einsum, einsum3, flip, gather, index_put,
86    index_select, masked_fill, masked_select, meshgrid, nonzero, one_hot, pad, repeat_interleave,
87    roll, scatter_add, sort, split, stack, tile, tril, triu, where_cond,
88};
89pub use sparse::{
90    coo_to_csr, coo_to_dense, csr_to_dense, dense_to_coo, dense_to_csr, spmm, spmm_backward_dense,
91    spmm_backward_values, spmv,
92};
93
94/// Device-neutral fused expression DAG with CPU-addressable evaluation support.
95pub mod fuse;
96pub use fuse::{
97    evaluate_fused_cpu, evaluate_fused_mean_cpu, evaluate_fused_reduce_cpu, scalar, CpuExprNode,
98    Expr, ExprNode, TensorExprExt,
99};
100
101/// Scaled dot-product attention with causal and padding mask support.
102pub mod attention;
103pub use attention::{scaled_dot_product_attention, scaled_dot_product_attention_backward};
104
105/// Transposed convolution operations (1-D, 2-D, and 3-D).
106pub mod conv_transpose;
107pub use conv_transpose::{conv_transpose1d, conv_transpose2d, conv_transpose3d};
108
109/// Adaptive pooling operations (avg and max in 1D/2D).
110pub mod adaptive_pool;
111pub use adaptive_pool::{
112    adaptive_avg_pool1d, adaptive_avg_pool2d, adaptive_max_pool1d, adaptive_max_pool2d,
113};
114
115/// Tensor constructors (linspace, logspace, geomspace).
116pub mod constructors;
117pub use constructors::{geomspace, linspace, logspace};
118
119/// Sliding-window extraction and adjoint accumulation.
120pub mod unfold_fold;
121pub use unfold_fold::{fold1d, fold2d, unfold1d, unfold2d};