chore: remove duplicate autograd modules #2870
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Greptile Overview
Updated On: 2025-10-07 10:24:42 UTC
Summary
This PR removes duplicate code in the autograd module by consolidating redundant files and functions. The changes eliminate three duplicate files (`ops_forward.py`, `ops_backward.py`, and `constants_local.py`) and remove deprecated function shims from `autograd.py`.The consolidation moves functionality from:
ops_forward.py→forward.py(functions likesetup_fwd()andpostprocess_fwd())ops_backward.py→backward.py(functions likesetup_adj()andpostprocess_adj())constants_local.py→constants.py(autograd-related constants)The main
autograd.pyfile is updated to import from the consolidated modules (.forward,.backward,.constants) instead of the removed duplicates. Additionally, deprecated backward-compatibility shims (_compute_eps_arrayand_slice_field_data) are removed fromautograd.pysince the actual implementations exist in thebackward.pymodule.This refactoring follows the DRY (Don't Repeat Yourself) principle by maintaining a single source of truth for each function and constant. The autograd functionality for automatic differentiation in electromagnetic simulations remains fully intact, but the module structure is now cleaner and more maintainable. All critical adjoint simulation processing capabilities, forward pass setup, and constant definitions are preserved in their consolidated locations.
Important Files Changed
Changed Files
Confidence score: 5/5
Sequence Diagram
sequenceDiagram participant User participant "web.api.autograd.run" as Run participant "is_valid_for_autograd" as Validator participant "_run" as RunImpl participant "setup_run" as Setup participant "_run_primitive" as Primitive participant "setup_fwd" as FwdSetup participant "_run_tidy3d" as Engine participant "postprocess_fwd" as FwdPost participant "postprocess_run" as PostProcess User->>Run: "run(simulation, task_name, ...)" Run->>Validator: "is_valid_for_autograd(simulation)" Validator-->>Run: "validation result" alt is autograd valid Run->>RunImpl: "_run(simulation, task_name, ...)" RunImpl->>Setup: "setup_run(simulation)" Setup-->>RunImpl: "traced_fields_sim" alt has traced fields RunImpl->>Primitive: "_run_primitive(traced_fields_sim, ...)" Primitive->>FwdSetup: "setup_fwd(sim_fields, sim_original, ...)" FwdSetup-->>Primitive: "sim_combined" alt local_gradient Primitive->>Engine: "_run_tidy3d(sim_combined, ...)" Engine-->>Primitive: "sim_data_combined, task_id" Primitive->>FwdPost: "postprocess_fwd(sim_data_combined, ...)" FwdPost-->>Primitive: "field_map" else server gradient Primitive->>Engine: "_run_tidy3d(sim_original, ...)" Engine-->>Primitive: "sim_data_orig, task_id_fwd" Note over Primitive: "Store task_id_fwd and sim_data_orig in aux_data" Primitive-->>Primitive: "extract field_map from sim_data_orig" end Primitive-->>RunImpl: "field_map" RunImpl->>PostProcess: "postprocess_run(traced_fields_data, aux_data)" PostProcess-->>RunImpl: "sim_data with tracers" else no traced fields Note over RunImpl: "Log warning about no tracers" RunImpl->>Engine: "_run_tidy3d(simulation, ...)" Engine-->>RunImpl: "sim_data, task_id" end RunImpl-->>Run: "simulation_data" else not autograd valid Run->>Engine: "run_webapi(simulation, ...)" Engine-->>Run: "simulation_data" end Run-->>User: "simulation_data"