refactor: LLM AI Coding Agent audit — split monoliths, clean annotations#2
Conversation
- Replace per-item #[allow(unused_imports)] with module-level in multi.rs
- Remove #[allow(dead_code)] from public API items where safe
- Add module-level docs to macros.rs
- Fix trailing comma in mf4.rs macro invocation
- Consolidate lib.rs re-exports into single pub use multi::{...} block
- Remove architecture comments from tests_comprehensive.rs
Move 3200+ lines from the monolithic src/multi/compiled.rs into focused modules under src/multi/backend/: types.rs — OpCode, FlatInstruction, BackendCapabilities, Instruction scalar.rs — ScalarBackend simd.rs — SimdBackend (f64x2, f64x4) with all SIMD helpers wgpu.rs — WgpuBackend, WgpuBuffer, WgpuBufferSet (feature-gated) device.rs — DeviceBatchPlan, DeviceBuffer*, transfer types, traits dispatch.rs — BackendKind, BackendSupportReport, auto-dispatch mock.rs — MockDeviceBackend compiled.rs retains only CompiledGraph, batch types, and workspace. No logic changed — pure code movement with updated imports. 863 tests pass, clippy clean.
Move ExprGraph/ExprNode + operator overloads to src/multi/expr.rs (207 lines). Move Tape/TapeWorkspace + compiled tape logic to src/multi/tape.rs (565 lines). graph.rs goes from 6300 to 5562 lines. 863 tests pass, clippy clean.
There was a problem hiding this comment.
Pull request overview
Refactors the multi-variable autodiff implementation to improve structure and maintainability by splitting large modules into focused submodules (notably backend execution, expression graph construction, and tape evaluation), along with annotation and re-export cleanup.
Changes:
- Split compiled execution/backends into
src/multi/backend/*(scalar/SIMD/WGPU + planning/dispatch/types) and updated dispatch/re-exports accordingly. - Extracted expression-graph builder (
ExprGraph/ExprNode) intosrc/multi/expr.rsand tape evaluation (Tape/TapeWorkspace) intosrc/multi/tape.rs. - Cleaned up annotations/re-exports and small hygiene fixes (macro docs added, trailing comma fix, comment cleanup in comprehensive tests).
Reviewed changes
Copilot reviewed 20 out of 21 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/tests_comprehensive.rs | Removes architecture/testing-location comments from comprehensive tests. |
| src/multi/tape.rs | New tape evaluation module extracted from graph.rs. |
| src/multi/multi_fn.rs | Removes #[allow(dead_code)] annotations from public API items. |
| src/multi/graph.rs | Removes inlined Tape/ExprGraph code and wires in extracted modules + backend imports. |
| src/multi/expr.rs | New expression-graph module extracted from graph.rs. |
| src/multi/builder.rs | Removes #[allow(dead_code)] on GraphBuilder::num_inputs. |
| src/multi/backend/wgpu.rs | New WGPU backend module (feature-gated). |
| src/multi/backend/types.rs | New backend core types/capabilities/opcode definitions. |
| src/multi/backend/simd.rs | New SIMD backend module (x86_64 lanes) for batch compute/gradients. |
| src/multi/backend/scalar.rs | New scalar backend module implementing the execution trait. |
| src/multi/backend/mod.rs | New backend module root with submodule declarations and re-exports. |
| src/multi/backend/mock.rs | New mock “device-style” backend executing on CPU with device plans. |
| src/multi/backend/dispatch.rs | New backend kind + support reporting + dispatch helpers. |
| src/multi/backend/device.rs | New device-oriented planning/buffer/transfer types and trait. |
| src/multi.rs | Adds backend/expr/tape modules and restructures re-exports (plus module-level lint allow). |
| src/mono/mono_fn.rs | Removes #[allow(dead_code)] annotations from public API items. |
| src/mono/mf4.rs | Removes trailing comma in mono_ops! invocation. |
| src/macros.rs | Adds module-level docs for macro APIs. |
| src/lib.rs | Consolidates multi re-exports into a single pub use multi::{...} block. |
| llm-refactor-plan.md | Adds the refactor plan document describing the audit and split strategy. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Addressed the unresolved Copilot review threads in
Validation:
|
There was a problem hiding this comment.
Code Review
This pull request performs a significant refactor of the petite-ad codebase, primarily focusing on splitting monolithic files like compiled.rs and graph.rs into modular backend components (scalar, SIMD, WGPU). It also introduces a new DeviceBackend trait and backend dispatch logic to improve architecture predictability. My review identified several performance concerns: the WGPU backend currently re-initializes the device context on every batch computation, which is highly inefficient. Additionally, the SIMD backend performs unnecessary heap allocations for derivative calculations and uses inefficient vector resizing inside performance-critical loops. I have suggested using fixed-size arrays and fill() for better performance.
…sfers GPU storage buffers use little-endian layout (WGSL spec). Replace to_ne_bytes/from_ne_bytes with to_le_bytes/from_le_bytes in encode_f64_slice, decode_f64_bytes, encode_f32_slice, decode_f32_bytes, and encode_u32_slice to ensure correct bit-pattern interpretation on big-endian hosts.
|
Fixed in |
…expr panic doc simd.rs: - simd_scalar_first_derivatives returns [f64; 2] instead of Vec<f64>, eliminating one heap allocation per derivative lane call - pre-allocate cotangents with correct size before the batch loop (f64x2 and f64x4), replace clear()+resize_with() inside the loop with fill() to zero without reallocation dispatch.rs: - document that BackendKind::Wgpu creates a new device per call; direct callers to hold a WgpuBackend instance for repeated use expr.rs: - add # Panics doc to ExprNode::same_graph explaining that mixing nodes from different ExprGraph instances panics at runtime, which is inherited by all arithmetic operator overloads
|
Fixed all 7 threads in
|
Summary
Implements the refactor plan from
llm-refactor-plan.md— an LLM AI Coding Agent audit of the codebase evaluating against predictable structure, flat architecture, and regenerability principles.Changes
High Priority
compiled.rs(4,615 → 1,346 lines) intosrc/multi/backend/module directory:types.rs,scalar.rs,simd.rs,wgpu.rs,device.rs,dispatch.rs,mock.rsgradient_batch_auto_into— resolved automatically by moving per-backend logic into separateExecutionBackendimplsExprGraph/ExprNodeintosrc/multi/expr.rs(207 lines)Tape/TapeWorkspaceintosrc/multi/tape.rs(565 lines)graph.rsreduced from 6,315 → 5,562 linesCleanup
#[allow(unused_imports)]with module-level annotation inmulti.rs#[allow(dead_code)]from public API items where clippy allowslib.rsre-exports into singlepub use multi::{…}blockmacros.rsmf4.rsmacro invocationtests_comprehensive.rsVerification
cargo test --all-features)cargo clippy --all-targets --all-features -- -D warnings)cargo fmt --checkcleanRemaining (not in scope)
impl Graphacross files