Skip to content

refactor: organize source tree into multi-level folder structure#3

Merged
leafyoung merged 2 commits into
mainfrom
refactor/source-tree-organization
May 11, 2026
Merged

refactor: organize source tree into multi-level folder structure#3
leafyoung merged 2 commits into
mainfrom
refactor/source-tree-organization

Conversation

@leafyoung

Copy link
Copy Markdown
Owner

Summary

Reorganized the project source files into a multi-level directory structure for better navigability and conceptual grouping.

Changes

  • First-order and second-order AD separated: src/{mono,multi}/first_order.rs and second_order/{rr,fr,rf,common.rs}
  • Graph, tape, builder grouped: under multi/graph/
  • Compiled IR + backends grouped: under multi/compiled/
  • compiled.rs split: into ir.rs (CompiledGraph) + batches.rs (BatchInputs, buffer types)
  • Test-only examples consolidated: mf1..mf4, f1..f3 merged into examples.rs
  • Module declarations: flat mono.rs/multi.rs replaced by mod.rs files
  • Renamed files: mono_fn.rsfunc.rs, multi_fn.rsfunc.rs, mono_ad.rsfirst_order.rs, etc.

Verification

  • ✅ 865 tests pass (0 failures)
  • cargo clippy --all-targets --all-features -- -D warnings clean
  • cargo fmt clean
  • ✅ All git mv operations preserve file history
  • ✅ No public API changes (same re-exports from lib.rs)
  • ✅ Doc-tests pass (29 passed, 2 ignored)

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.
Copilot AI review requested due to automatic review settings May 11, 2026 16:13

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/multi/compiled/batches.rs Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 both mono and multi.
  • Grouped the multi-variable graph/tape/builder under multi::graph::*, and split compiled execution into multi::compiled::{ir,batches,backend::*}.
  • Consolidated test-only example functions into mono/examples.rs and multi/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.
@leafyoung
leafyoung merged commit 4f2cfc8 into main May 11, 2026
4 checks passed
@leafyoung
leafyoung deleted the refactor/source-tree-organization branch May 11, 2026 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants