fix: correct NaN preprocessing and torch.compile dead assignment#401
Open
Angelopgit wants to merge 1 commit intogoogle-research:masterfrom
Open
fix: correct NaN preprocessing and torch.compile dead assignment#401Angelopgit wants to merge 1 commit intogoogle-research:masterfrom
Angelopgit wants to merge 1 commit intogoogle-research:masterfrom
Conversation
Three bugs in the inference path, all silently producing wrong results: 1. strip_leading_nans (src/ and v1/): np.argmax(~isnan) returns 0 when the input is all-NaN, so arr[0:] returns the full NaN array instead of the empty array promised by the docstring. Add an early-exit guard. 2. linear_interpolation (src/): the except-ValueError fallback uses `if non_nans_values:` where non_nans_values is a NumPy array. NumPy already emits DeprecationWarning for truth-value testing of arrays; a future release will raise ValueError. Use .size > 0. 3. load_checkpoint / _from_pretrained (torch): `self = torch.compile(self)` inside a method only rebinds the local variable — the caller's reference to instance.model is never replaced. torch_compile=True is the default, so every PyTorch user was getting uncompiled inference while believing the model was compiled. Move the torch.compile call to _from_pretrained where the assignment can actually take effect.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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.
Summary
Three silent bugs in the inference preprocessing path, all present in the current
main:1.
strip_leading_nansreturns the wrong value for all-NaN input (src/andv1/)np.argmax(~isnan)returns0when the input is entirely NaN (all-Falsemask — NumPy does not raise). Soarr[0:]silently returns the full NaN array instead of the empty array the docstring promises. Downstream,linear_interpolationconverts the NaN array to all-zeros vianp.where(np.isfinite(...), ..., 0.0), and the model receives a plausible-looking but meaningless zero-padded context with no error.2.
linear_interpolationuses ambiguous bool coercion on a NumPy array (src/)The
except ValueErrorfallback doesif non_nans_values:wherenon_nans_valuesis a NumPy array. NumPy already emitsDeprecationWarningfor truth-value testing of arrays with more than one element; a future release will raiseValueError. The v1 version of this function already useslen(...) > 0— the 2.5 version regressed. Changed to.size > 0to match.3.
torch.compiledead assignment inload_checkpoint(src/, PyTorch backend)Rebinding
selfinside a method never affects the caller's reference.torch.compilereturns a newOptimizedModulewrapper; assigning it to the localselfdiscards it immediately. Sincetorch_compile=Trueis the default, every PyTorch user was getting unoptimized inference while believing the model was compiled.Moved the
torch.compilecall to_from_pretrainedwhereinstance.model = torch.compile(instance.model)actually takes effect.Changes
src/timesfm/timesfm_2p5/timesfm_2p5_base.pystrip_leading_nans; fix bool coercion inlinear_interpolationv1/src/timesfm/timesfm_base.pystrip_leading_nanssrc/timesfm/timesfm_2p5/timesfm_2p5_torch.pytorch.compileto_from_pretrained; simplifyload_checkpoint