Refactor: Extract common injection logic into strategy pattern#59
Draft
Refactor: Extract common injection logic into strategy pattern#59
Conversation
… in BaseInjector Co-authored-by: Will-D-AER <197624355+Will-D-AER@users.noreply.github.com>
Co-authored-by: Will-D-AER <197624355+Will-D-AER@users.noreply.github.com>
Co-authored-by: Will-D-AER <197624355+Will-D-AER@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Refactor run_injection function into smaller functions
Refactor: Extract common injection logic into strategy pattern
Dec 10, 2025
Owner
|
/gemini review |
Contributor
There was a problem hiding this comment.
Code Review
This pull request is an excellent refactoring that extracts common injection logic from ExhaustiveSEUInjector and StochasticSEUInjector into a BaseInjector class, effectively applying the Strategy design pattern. This change significantly reduces code duplication, improves maintainability, and makes the system more extensible for future injector types. The simplification of the _run_injector_impl methods is clean, and the addition of comprehensive unit tests for the new helper methods ensures the correctness of this refactoring. I have one minor suggestion to further improve performance.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The
_run_injector_implmethods inExhaustiveSEUInjectorandStochasticSEUInjectorwere 85+ lines each with substantial duplication. Only the index selection strategy differed between implementations.Changes
Extracted 6 helper methods to
BaseInjector:_iterate_layers()- Layer filtering and iteration_prepare_tensor_for_injection()- Tensor cloning and CPU conversion_inject_and_evaluate()- Bitflip, evaluation, restoration cycle_record_injection_result()- Result dictionary population_initialize_results()- Result structure creation_get_injection_indices()- Abstract strategy methodSimplified implementations:
ExhaustiveSEUInjector._run_injector_impl: 85 → 35 linesStochasticSEUInjector._run_injector_impl: 85 → 32 lines_get_injection_indices()for strategyAdded test coverage:
Impact
New injector types require implementing only the selection strategy:
All existing tests pass. No API changes.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.