Skip to content

Commit 4f2cfc8

Browse files
authored
refactor: organize source tree into multi-level folder structure (#3)
* refactor: organize source tree into multi-level folder structure Reorganized the project source files into a multi-level directory structure for better navigability and conceptual grouping. Changes: - Group first-order and second-order AD separately: src/{mono,multi}/first_order.rs and second_order/{rr,fr,rf,common}.rs - Group graph, tape, builder under multi/graph/ - Move compiled IR and execution backends under multi/compiled/ - Split compiled.rs into ir.rs + batches.rs (batch buffer types) - Merge test-only example impls (mf1..mf4, f1..f3) into examples.rs - Rename mono_fn.rs -> func.rs, multi_fn.rs -> func.rs - Replace flat mono.rs/multi.rs with mod.rs files - Update all internal import paths and lib.rs re-exports No public API changes. 865 tests pass, clippy clean. * fix: use checked arithmetic in BatchInputs::try_row to prevent overflow The multiplication index * input_dim and subsequent slice range could overflow when BatchInputs is constructed manually with large values (since fields are public, validation in new() can be bypassed). Replace direct arithmetic with checked_mul / checked_add and use data.get(start..end) instead of direct indexing to prevent panics. Fixes the review comment from gemini-code-assist.
1 parent 7cb69d3 commit 4f2cfc8

48 files changed

Lines changed: 614 additions & 529 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/lib.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,16 @@ pub use mono::MonoAD;
145145
pub use mono::{MonoAD2FR, MonoAD2RF, MonoAD2RR};
146146

147147
pub use multi::{
148-
builder::GraphBuilder, multi_ad_fr::MultiAD2FR, multi_ad_rf::MultiAD2RF,
149-
multi_ad_rr::MultiAD2RR, AcceleratorDeviceContext, AcceleratorDeviceKind, BackendCapabilities,
150-
BackendKind, BackendRejectionReason, BackendSupportReport, BatchGradients,
151-
BatchGradientsBuffer, BatchInputs, BatchLayout, BatchValues, BatchValuesBuffer, CompiledGraph,
152-
CompiledGraphMetadata, CompiledWorkspace, DeviceBackend, DeviceBatchPlan, DeviceBuffer,
153-
DeviceBufferHandle, DeviceBufferKind, DeviceBufferLayout, DeviceBufferSet, DeviceExecutionMode,
148+
AcceleratorDeviceContext, AcceleratorDeviceKind, BackendCapabilities, BackendKind,
149+
BackendRejectionReason, BackendSupportReport, BatchGradients, BatchGradientsBuffer,
150+
BatchInputs, BatchLayout, BatchValues, BatchValuesBuffer, CompiledGraph, CompiledGraphMetadata,
151+
CompiledWorkspace, DeviceBackend, DeviceBatchPlan, DeviceBuffer, DeviceBufferHandle,
152+
DeviceBufferKind, DeviceBufferLayout, DeviceBufferSet, DeviceExecutionMode,
154153
DeviceExecutionTrace, DeviceMemoryLocation, DeviceTransferKind, DeviceTransferPlan,
155154
DeviceTransferPolicy, DomainPolicy, ExecutionBackend, ExprGraph, ExprNode, FlatInstruction,
156-
GpuBackendBoundary, GradientCheckEntry, GradientCheckReport, Graph, GraphNode, GraphStats,
157-
Instruction, MockDeviceBackend, MultiAD, NodeId, OpCode, ScalarBackend, SimdBackend, Tape,
158-
TapeWorkspace, UNUSED_NODE_ID,
155+
GpuBackendBoundary, GradientCheckEntry, GradientCheckReport, Graph, GraphBuilder, GraphNode,
156+
GraphStats, Instruction, MockDeviceBackend, MultiAD, MultiAD2FR, MultiAD2RF, MultiAD2RR,
157+
NodeId, OpCode, ScalarBackend, SimdBackend, Tape, TapeWorkspace, UNUSED_NODE_ID,
159158
};
160159
#[cfg(feature = "backend-wgpu")]
161160
pub use multi::{

src/mono.rs

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/mono/examples.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//! Test-only example implementations of [`MonoFn`] for library tests.
2+
//!
3+
//! These are production-type examples used internally by the test suite and
4+
//! are gated behind `#[cfg(test)]` to keep them out of release builds.
5+
6+
#[cfg(test)]
7+
use super::func::{GraphType, MonoFn};
8+
#[cfg(test)]
9+
use crate::mono_ops;
10+
11+
/// f(x) = exp(sin(sin(x)))
12+
#[cfg(test)]
13+
pub struct MF1(pub f64);
14+
15+
#[cfg(test)]
16+
impl MonoFn for MF1 {
17+
fn input(&self) -> f64 {
18+
self.0
19+
}
20+
21+
fn graph(&self) -> &'static GraphType {
22+
&mono_ops![sin, sin, exp]
23+
}
24+
25+
fn expected_value(&self) -> f64 {
26+
(self.0.sin().sin()).exp()
27+
}
28+
29+
fn expected_gradient(&self) -> f64 {
30+
(self.0.sin().sin()).exp() * self.0.sin().cos() * self.0.cos()
31+
}
32+
}
33+
34+
/// f(x) = -x
35+
#[cfg(test)]
36+
pub struct MF2(pub f64);
37+
38+
#[cfg(test)]
39+
impl MonoFn for MF2 {
40+
fn input(&self) -> f64 {
41+
self.0
42+
}
43+
44+
fn graph(&self) -> &'static GraphType {
45+
&mono_ops![neg]
46+
}
47+
48+
fn expected_value(&self) -> f64 {
49+
-self.0
50+
}
51+
52+
fn expected_gradient(&self) -> f64 {
53+
-1.0
54+
}
55+
}
56+
57+
/// f(x) = sin(-x)
58+
#[cfg(test)]
59+
pub struct MF3(pub f64);
60+
61+
#[cfg(test)]
62+
impl MonoFn for MF3 {
63+
fn input(&self) -> f64 {
64+
self.0
65+
}
66+
67+
fn graph(&self) -> &'static GraphType {
68+
&mono_ops![neg, sin]
69+
}
70+
71+
fn expected_value(&self) -> f64 {
72+
(-self.0).sin()
73+
}
74+
75+
fn expected_gradient(&self) -> f64 {
76+
-((-self.0).cos())
77+
}
78+
}
79+
80+
/// f(x) = -sin(x)
81+
#[cfg(test)]
82+
pub struct MF4(pub f64);
83+
84+
#[cfg(test)]
85+
impl MonoFn for MF4 {
86+
fn input(&self) -> f64 {
87+
self.0
88+
}
89+
90+
fn graph(&self) -> &'static GraphType {
91+
&mono_ops![sin, neg]
92+
}
93+
94+
fn expected_value(&self) -> f64 {
95+
-(self.0.sin())
96+
}
97+
98+
fn expected_gradient(&self) -> f64 {
99+
-(self.0.cos())
100+
}
101+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub use super::mono_ad::MonoAD;
1+
pub use super::first_order::MonoAD;
22
pub use super::types::BackwardResultBox;
33

44
/// Type alias for a graph of mono operations (slice of MonoAD)

src/mono/mf1.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/mono/mf2.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/mono/mf3.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/mono/mf4.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/mono/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//! Single-variable automatic differentiation.
2+
//!
3+
//! This module provides functionality for computing derivatives of
4+
//! single-variable functions using reverse-mode differentiation.
5+
6+
pub mod types;
7+
8+
#[cfg(test)]
9+
mod tests;
10+
11+
#[cfg(test)]
12+
mod tests_ho;
13+
14+
pub mod first_order;
15+
pub use first_order::MonoAD;
16+
17+
pub mod second_order;
18+
pub use second_order::fr::MonoAD2FR;
19+
pub use second_order::rf::MonoAD2RF;
20+
pub use second_order::rr::MonoAD2RR;
21+
22+
pub mod func;
23+
// Re-export trait for library extension - users can implement custom mono functions
24+
#[allow(unused_imports)]
25+
pub use func::MonoFn;
26+
27+
#[cfg(test)]
28+
mod examples;

0 commit comments

Comments
 (0)