Instruction file imported from DucHuyTa04/Independent-TracIn (
.github/instructions/src-library.instructions.md). Copyright stays with the author.
src/ Library Rules
The 4 Engineering Rules (mandatory)
-
Memory (Rule 1): Wrap extraction in
torch.no_grad(). After each batch:delactivation/error/ghost tensors, calltorch.cuda.empty_cache(). -
Non-destructive hooks (Rule 2): Always use
HookManageras a context manager. Never register raw hooks. Hooks must be removed infinally. -
Dim standardization (Rule 3): All tensors entering
form_ghost_vectorsmust be 2D[Batch, Hidden]. Use_flatten_to_2d()for 3D (Transformer) and 4D (CNN) inputs. -
Types (Rule 4): Every public function has type hints and a Google-style docstring with Args/Returns.
-
Tensor ordering (ghost vs weight): Ghost vectors use
(H, C)row-major fromform_ghost_vectors. PyTorchnn.Linear.weightand Adamexp_avg_squse(C, H). Align second moments to ghost layout beforeapply_adam_correction(transpose then flatten); passweight_shapeintoload_adam_second_momentwhen needed.
API Design
- Functions accept explicit parameters, never config dicts
error_fnis always user-provided:(logits, targets) -> E- No model-specific code in src/ — that belongs in testModels/