Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,16 @@ pub use mono::MonoAD;
pub use mono::{MonoAD2FR, MonoAD2RF, MonoAD2RR};

pub use multi::{
builder::GraphBuilder, multi_ad_fr::MultiAD2FR, multi_ad_rf::MultiAD2RF,
multi_ad_rr::MultiAD2RR, AcceleratorDeviceContext, AcceleratorDeviceKind, BackendCapabilities,
BackendKind, BackendRejectionReason, BackendSupportReport, BatchGradients,
BatchGradientsBuffer, BatchInputs, BatchLayout, BatchValues, BatchValuesBuffer, CompiledGraph,
CompiledGraphMetadata, CompiledWorkspace, DeviceBackend, DeviceBatchPlan, DeviceBuffer,
DeviceBufferHandle, DeviceBufferKind, DeviceBufferLayout, DeviceBufferSet, DeviceExecutionMode,
AcceleratorDeviceContext, AcceleratorDeviceKind, BackendCapabilities, BackendKind,
BackendRejectionReason, BackendSupportReport, BatchGradients, BatchGradientsBuffer,
BatchInputs, BatchLayout, BatchValues, BatchValuesBuffer, CompiledGraph, CompiledGraphMetadata,
CompiledWorkspace, DeviceBackend, DeviceBatchPlan, DeviceBuffer, DeviceBufferHandle,
DeviceBufferKind, DeviceBufferLayout, DeviceBufferSet, DeviceExecutionMode,
DeviceExecutionTrace, DeviceMemoryLocation, DeviceTransferKind, DeviceTransferPlan,
DeviceTransferPolicy, DomainPolicy, ExecutionBackend, ExprGraph, ExprNode, FlatInstruction,
GpuBackendBoundary, GradientCheckEntry, GradientCheckReport, Graph, GraphNode, GraphStats,
Instruction, MockDeviceBackend, MultiAD, NodeId, OpCode, ScalarBackend, SimdBackend, Tape,
TapeWorkspace, UNUSED_NODE_ID,
GpuBackendBoundary, GradientCheckEntry, GradientCheckReport, Graph, GraphBuilder, GraphNode,
GraphStats, Instruction, MockDeviceBackend, MultiAD, MultiAD2FR, MultiAD2RF, MultiAD2RR,
NodeId, OpCode, ScalarBackend, SimdBackend, Tape, TapeWorkspace, UNUSED_NODE_ID,
};
#[cfg(feature = "backend-wgpu")]
pub use multi::{
Expand Down
31 changes: 0 additions & 31 deletions src/mono.rs

This file was deleted.

101 changes: 101 additions & 0 deletions src/mono/examples.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//! Test-only example implementations of [`MonoFn`] for library tests.
//!
//! These are production-type examples used internally by the test suite and
//! are gated behind `#[cfg(test)]` to keep them out of release builds.

#[cfg(test)]
use super::func::{GraphType, MonoFn};
#[cfg(test)]
use crate::mono_ops;

/// f(x) = exp(sin(sin(x)))
#[cfg(test)]
pub struct MF1(pub f64);

#[cfg(test)]
impl MonoFn for MF1 {
fn input(&self) -> f64 {
self.0
}

fn graph(&self) -> &'static GraphType {
&mono_ops![sin, sin, exp]
}

fn expected_value(&self) -> f64 {
(self.0.sin().sin()).exp()
}

fn expected_gradient(&self) -> f64 {
(self.0.sin().sin()).exp() * self.0.sin().cos() * self.0.cos()
}
}

/// f(x) = -x
#[cfg(test)]
pub struct MF2(pub f64);

#[cfg(test)]
impl MonoFn for MF2 {
fn input(&self) -> f64 {
self.0
}

fn graph(&self) -> &'static GraphType {
&mono_ops![neg]
}

fn expected_value(&self) -> f64 {
-self.0
}

fn expected_gradient(&self) -> f64 {
-1.0
}
}

/// f(x) = sin(-x)
#[cfg(test)]
pub struct MF3(pub f64);

#[cfg(test)]
impl MonoFn for MF3 {
fn input(&self) -> f64 {
self.0
}

fn graph(&self) -> &'static GraphType {
&mono_ops![neg, sin]
}

fn expected_value(&self) -> f64 {
(-self.0).sin()
}

fn expected_gradient(&self) -> f64 {
-((-self.0).cos())
}
}

/// f(x) = -sin(x)
#[cfg(test)]
pub struct MF4(pub f64);

#[cfg(test)]
impl MonoFn for MF4 {
fn input(&self) -> f64 {
self.0
}

fn graph(&self) -> &'static GraphType {
&mono_ops![sin, neg]
}

fn expected_value(&self) -> f64 {
-(self.0.sin())
}

fn expected_gradient(&self) -> f64 {
-(self.0.cos())
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion src/mono/mono_fn.rs → src/mono/func.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use super::mono_ad::MonoAD;
pub use super::first_order::MonoAD;
pub use super::types::BackwardResultBox;

/// Type alias for a graph of mono operations (slice of MonoAD)
Expand Down
26 changes: 0 additions & 26 deletions src/mono/mf1.rs

This file was deleted.

26 changes: 0 additions & 26 deletions src/mono/mf2.rs

This file was deleted.

26 changes: 0 additions & 26 deletions src/mono/mf3.rs

This file was deleted.

26 changes: 0 additions & 26 deletions src/mono/mf4.rs

This file was deleted.

28 changes: 28 additions & 0 deletions src/mono/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! Single-variable automatic differentiation.
//!
//! This module provides functionality for computing derivatives of
//! single-variable functions using reverse-mode differentiation.

pub mod types;

#[cfg(test)]
mod tests;

#[cfg(test)]
mod tests_ho;

pub mod first_order;
pub use first_order::MonoAD;

pub mod second_order;
pub use second_order::fr::MonoAD2FR;
pub use second_order::rf::MonoAD2RF;
pub use second_order::rr::MonoAD2RR;

pub mod func;
// Re-export trait for library extension - users can implement custom mono functions
#[allow(unused_imports)]
pub use func::MonoFn;

#[cfg(test)]
mod examples;
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

use crate::{AutodiffError, Result};

use super::types::*;
use crate::mono::types::*;

/// Operation kind for mono-variable second-order AD (shared by FR and RF).
///
Expand Down
18 changes: 9 additions & 9 deletions src/mono/mono_ad_fr.rs → src/mono/second_order/fr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! # Supported Operations
//!
//! This type supports a subset of operations compared to [`crate::MonoAD`]:
//! `Sin`, `Cos`, `Tan`, `Exp`, `Neg`, `Ln`, `Sqrt`, `Abs`. See [`super::mono_hessian_common`] for details.
//! `Sin`, `Cos`, `Tan`, `Exp`, `Neg`, `Ln`, `Sqrt`, `Abs`. See [`super::common`] for details.
//!
//! # Mathematical Foundation
//!
Expand Down Expand Up @@ -65,9 +65,9 @@

use crate::Result;

use super::mono_ad_rr::MonoAD2RR;
use super::mono_hessian_common::{self, MonoHessianOpKind};
use super::types::*;
use super::common::{self, MonoHessianOpKind};
use super::rr::MonoAD2RR;
use crate::mono::types::*;

/// Single-variable automatic differentiation operations for Forward-over-Reverse Hessian computation.
///
Expand Down Expand Up @@ -103,13 +103,13 @@ impl MonoAD2FR {
/// Compute forward pass only.
pub fn compute(exprs: &[MonoAD2FR], x: f64) -> f64 {
let ops: Vec<MonoHessianOpKind> = exprs.iter().map(|&op| op.into()).collect();
mono_hessian_common::compute_forward(&ops, x)
common::compute_forward(&ops, x)
}

/// Compute forward pass with opt-in checked-domain validation.
pub fn compute_checked(exprs: &[MonoAD2FR], x: f64) -> Result<f64> {
let ops: Vec<MonoHessianOpKind> = exprs.iter().map(|&op| op.into()).collect();
mono_hessian_common::compute_forward_checked(&ops, x)
common::compute_forward_checked(&ops, x)
}

/// Compute forward pass and return gradient function using reverse-mode.
Expand All @@ -120,15 +120,15 @@ impl MonoAD2FR {
/// Compute forward pass and gradient function with checked-domain validation.
pub fn compute_grad_checked(exprs: &[MonoAD2FR], x: f64) -> Result<BackwardResultBox> {
let ops: Vec<MonoHessianOpKind> = exprs.iter().map(|&op| op.into()).collect();
mono_hessian_common::compute_grad_generic_checked::<Box<DynMathFn>>(&ops, x)
common::compute_grad_generic_checked::<Box<DynMathFn>>(&ops, x)
}

fn compute_grad_generic<W>(exprs: &[MonoAD2FR], x: f64) -> (f64, W)
where
W: From<Box<DynMathFn>> + std::ops::Deref<Target = DynMathFn> + 'static,
{
let ops: Vec<MonoHessianOpKind> = exprs.iter().map(|&op| op.into()).collect();
mono_hessian_common::compute_grad_generic::<W>(&ops, x)
common::compute_grad_generic::<W>(&ops, x)
}

/// Compute exact Hessian using Forward-over-Reverse mode.
Expand Down Expand Up @@ -171,7 +171,7 @@ impl MonoAD2FR {

// Single operation: direct dual-number evaluation
if let [op] = exprs {
return mono_hessian_common::compute_single_op_hessian((*op).into(), x)
return common::compute_single_op_hessian((*op).into(), x)
.expect("all single ops supported");
}

Expand Down
6 changes: 6 additions & 0 deletions src/mono/second_order/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! Exact second-order (Hessian) computation for single-variable functions.

pub(crate) mod common;
pub mod fr;
pub mod rf;
pub mod rr;
Loading
Loading