Fix dtype casting inside tree_add_scale. - #1376
Merged
copybara-service[bot] merged 1 commit intoSep 8, 2025
Merged
Conversation
Collaborator
|
@mtthss you introduced this dtype conversion in #926. Do you remember why? @carlosgmartin add a test. It's clearly a bug so a test is welcome. Thanks again! |
Collaborator
|
Also @carlosgmartin, can you explain
There may be other functions that work well thanks to this undocumented dtype conversion. So maybe we'll have to make a pass on references to tree_add_scale |
carlosgmartin
force-pushed
the
tree_add_scale_dtype
branch
from
July 15, 2025 16:53
9c4d1cc to
029f100
Compare
carlosgmartin
force-pushed
the
tree_add_scale_dtype
branch
from
July 15, 2025 16:55
029f100 to
7cf6896
Compare
Contributor
Author
|
@vroulet The induced LBFGS bug was caught by a test. Without the two vec_new = optax.tree.cast_like(vec_new, vec)lines, $ pytest optax/_src/alias_test.py -k test_state_shape_dtype_shard_stabilityoutputs |
rdyro
reviewed
Jul 15, 2025
| chex.assert_trees_all_close(expected, got) | ||
|
|
||
| def test_tree_add_scale_dtype(self): | ||
| got = tu.tree_add_scale(3, 0.1, 2) |
Contributor
Author
Collaborator
|
Removing the dtype cast in |
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 current behavior of
tree_add_scaleis confusing:This is because its current implementation casts
scalarto the dtype ofx.This PR modifies
tree_add_scaleto remove this casting and thus get the expected behavior:Subsequently, it fixes the scan loop bodies inside
_precondition_by_lbfgsto usetree_cast_liketo ensure the carry output matches the carry input.