[preconditioner] thread-safe TF32 context manager, generalize rank_deficient_stability_config to MatrixFunctionConfig, refactor _precondition_grad - #273
Open
Vishal-sys-code wants to merge 2 commits into
Conversation
…_config, and optimize _precondition_grad
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.
Resolves three open TODOs in
distributed_shampoo/preconditioner.Thread-safe TF32 context manager:
_matrix_inverse_root_coupled_higher_order_newtonwas togglingtorch.backends.cuda.matmul.allow_tf32directly with try/finally. Since this flag is process-global, concurrent calls from multi-threaded optimizer setups could race on it. Added_TF32_LOCK(RLock) and_scoped_tf32_settingcontext manager so the read/modify/restore is atomic. The fast path (whendisable_tf32is False) skips lock acquisition entirely. Added a multithreaded regression test inmatrix_functions_test.py.Generalize
rank_deficient_stability_configtoMatrixFunctionConfig: The field was onEigendecompositionConfigwith a TODO to move it up to the base class. Moved it toMatrixFunctionConfigalong with the default factory. Cleaned up the conditional TODOs inPerturbationConfigandPseudoInverseConfigdocstrings that were contingent on this generalization. Removed theassert isinstance(..., EigendecompositionConfig)guards in_compute_preconditioned_gradientand_compute_outer_product_listthat only existed because the field was not on the base class yet.Refactor
_precondition_grad: The implementation usedfunctools.reducewith a lambda that calledgrad.to(dtype=target_dtype)on every iteration and reconstructed(*range(1, grad.ndim), 0)on every non-preconditioned step. Replaced with a direct loop, a single upfront dtype cast, and a precomputed permutation tuple. Removed the now-unusedfunctools.reduceimport.