refactor: organize source tree into multi-level folder structure#3
Conversation
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.
There was a problem hiding this comment.
Code Review
This pull request performs a major structural refactoring of the mono and multi modules, organizing functionality into submodules for first-order and second-order automatic differentiation, graph management, and compiled execution. It consolidates test examples and moves batch processing types into a dedicated batches.rs module. Feedback suggests improving the robustness of the BatchInputs::try_row method by using checked arithmetic to prevent potential overflows when calculating slice indices.
There was a problem hiding this comment.
Pull request overview
This PR restructures the mono and multi source trees into a deeper module hierarchy to improve navigability and conceptual grouping, while keeping the public API stable via lib.rs re-exports.
Changes:
- Reorganized first-/second-order AD implementations into
first_order+second_order::{fr,rf,rr,common}modules for bothmonoandmulti. - Grouped the multi-variable graph/tape/builder under
multi::graph::*, and split compiled execution intomulti::compiled::{ir,batches,backend::*}. - Consolidated test-only example functions into
mono/examples.rsandmulti/examples.rs, updating test imports accordingly.
Reviewed changes
Copilot reviewed 44 out of 48 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/multi/tests.rs | Updates test imports to use consolidated examples module. |
| src/multi/second_order/mod.rs | Introduces second_order module boundary for multi Hessian implementations. |
| src/multi/second_order/common.rs | Updates imports to new first-order/op_rules locations. |
| src/multi/second_order/fr.rs | Points FR Hessian implementation at new common module. |
| src/multi/second_order/rf.rs | Points RF Hessian implementation at new common module. |
| src/multi/second_order/rr.rs | Updates RR Hessian implementation imports to new module layout. |
| src/multi/op_rules.rs | Updates docs/imports to reflect first_order::MultiAD location. |
| src/multi/mod.rs | Replaces flat module list with grouped compiled/graph/first_order/second_order structure and re-exports. |
| src/multi/graph/mod.rs | Adds multi::graph module with submodules (builder/core/expr/parser/tape). |
| src/multi/graph/core.rs | Rehomes core graph API under multi::graph::core and updates internal imports. |
| src/multi/graph/expr.rs | Updates expression graph to import core::{Graph, NodeId} and first_order::MultiAD. |
| src/multi/graph/tape.rs | Updates tape implementation imports to new module layout. |
| src/multi/graph/parser.rs | Adds expression parser implementation + tests for graph construction from strings. |
| src/multi/graph/builder.rs | Updates builder imports to new core + first_order locations. |
| src/multi/func.rs | Updates MultiFn support module to re-export first_order::MultiAD. |
| src/multi/first_order.rs | New home for multi-variable first-order tuple-based AD implementation and tests. |
| src/multi/examples.rs | Consolidates former f1/f2/f3 test-only example functions into one module. |
| src/multi/f1.rs | Removed (functionality moved into src/multi/examples.rs). |
| src/multi/f2.rs | Removed (functionality moved into src/multi/examples.rs). |
| src/multi/f3.rs | Removed (functionality moved into src/multi/examples.rs). |
| src/multi/compiled/mod.rs | Adds compiled module boundary and re-exports for IR/batch types/backends. |
| src/multi/compiled/ir.rs | Splits compiled IR into dedicated module and updates imports after refactor. |
| src/multi/compiled/batches.rs | Extracts batch input/output/gradient buffer types into a separate module (+ tests). |
| src/multi/compiled/backend/mod.rs | Introduces nested backend module and re-export surface for compiled execution. |
| src/multi/compiled/backend/device.rs | Updates device/backend wiring to new compiled::backend::* paths. |
| src/multi/compiled/backend/dispatch.rs | Updates backend dispatch to new compiled::backend::* paths. |
| src/multi/compiled/backend/mock.rs | Updates mock backend imports to new module layout. |
| src/multi/compiled/backend/scalar.rs | Updates scalar backend imports to new module layout. |
| src/multi/compiled/backend/simd.rs | Updates SIMD backend imports to new module layout. |
| src/multi/compiled/backend/types.rs | Updates backend core types to use first_order::MultiAD. |
| src/multi/compiled/backend/wgpu.rs | Updates WGPU backend imports to new module layout. |
| src/mono/mod.rs | Replaces flat mono.rs with directory module + explicit re-exports. |
| src/mono/tests.rs | Updates tests to use consolidated examples module and new module paths. |
| src/mono/tests_ho.rs | Updates HO tests to reference second_order::{fr,rf,rr} modules. |
| src/mono/second_order/mod.rs | Introduces second_order module boundary for mono Hessian implementations. |
| src/mono/second_order/common.rs | Updates common second-order utilities to use crate::mono::types::*. |
| src/mono/second_order/fr.rs | Updates FR implementation imports/links to new common module. |
| src/mono/second_order/rf.rs | Updates RF implementation imports/links to new common module. |
| src/mono/second_order/rr.rs | Updates RR implementation imports to new common module and crate::mono::types::*. |
| src/mono/func.rs | Updates MonoFn support module to re-export first_order::MonoAD. |
| src/mono/first_order.rs | New home for mono first-order AD implementation (moved/refactored) + docs. |
| src/mono/examples.rs | Consolidates former mf1..mf4 test-only example functions into one module. |
| src/mono/mf1.rs | Removed (functionality moved into src/mono/examples.rs). |
| src/mono/mf2.rs | Removed (functionality moved into src/mono/examples.rs). |
| src/mono/mf3.rs | Removed (functionality moved into src/mono/examples.rs). |
| src/mono/mf4.rs | Removed (functionality moved into src/mono/examples.rs). |
| src/mono.rs | Removed in favor of src/mono/mod.rs module directory structure. |
| src/lib.rs | Updates multi re-exports to point at new module organization (e.g., GraphBuilder, MultiAD2*). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Summary
Reorganized the project source files into a multi-level directory structure for better navigability and conceptual grouping.
Changes
src/{mono,multi}/first_order.rsandsecond_order/{rr,fr,rf,common.rs}multi/graph/multi/compiled/ir.rs(CompiledGraph) +batches.rs(BatchInputs, buffer types)mf1..mf4,f1..f3merged intoexamples.rsmono.rs/multi.rsreplaced bymod.rsfilesmono_fn.rs→func.rs,multi_fn.rs→func.rs,mono_ad.rs→first_order.rs, etc.Verification
cargo clippy --all-targets --all-features -- -D warningscleancargo fmtcleangit mvoperations preserve file historylib.rs)