feat: make timesfm2_5 onnx export compatible#45233
feat: make timesfm2_5 onnx export compatible#45233pdufour wants to merge 25 commits intohuggingface:mainfrom
Conversation
4785c38 to
b8c0e9d
Compare
|
Thanks for the PR! cc @IlyasMoutawwakil maybe to include in #41992? |
| @@ -565,16 +565,16 @@ def _preprocess( | |||
| input_ts, input_padding = [], [] | |||
|
|
|||
| for ts in inputs: | |||
There was a problem hiding this comment.
does your fix also work for multiple batches? 👀 this line/for-loop suggests it will be fixed at the batch_size you exported with.
There was a problem hiding this comment.
yeah this will still hardcode the batch size through the loop
There was a problem hiding this comment.
hmm yeah, i think I need to change the input to torch.Tensor, do you know another option?
There was a problem hiding this comment.
…k to be onnx compatible
|
@xenova @IlyasMoutawwakil updated the PR now so onnx export does not fix batch size, also fixed a couple other spots with simliar issues. Put it all in the PR description. Model looks like this now: Was previously showing static value for batch but now it is dynamic:
|
|
@xenova @IlyasMoutawwakil any chance you have time this week to re-review? Thanks! |
| if isinstance(inputs, torch.Tensor) and inputs.ndim == 2: | ||
| x = inputs[:, -context_len:] | ||
| num_front_pad = context_len - x.shape[1] | ||
| x = F.pad(x, (num_front_pad, 0)) | ||
| padding = torch.cat( | ||
| [ | ||
| torch.ones(x.shape[0], num_front_pad, dtype=x.dtype, device=x.device), | ||
| torch.zeros( | ||
| x.shape[0], context_len + self.horizon_len - num_front_pad, dtype=x.dtype, device=x.device | ||
| ), | ||
| ], | ||
| dim=1, | ||
| ) | ||
| result = (x, padding) | ||
| else: | ||
| input_ts, input_padding = [], [] | ||
| for ts in inputs: |
There was a problem hiding this comment.
unfortunately we can't have different paths for tensors and lists in the modeling code, what i suggest is to do something like:
if isisntance(obj, list):
# turn into tensors
# do tensor processing
There was a problem hiding this comment.
@IlyasMoutawwakil updated now! Let me know if that is what you were thinking, I also added an optional past_observed_mask arg since IIRC you can't have variable-length sequences in a single batched tensor.
Let me know if I am wrong about that and I can change it!
| @require_torch | ||
| class TimesFm2_5ForwardInputVariantsTest(unittest.TestCase): |
There was a problem hiding this comment.
i think this test suite is unnecessary if we just switch to tensors in the above testing and explicitly asked users to use tensors if they ever pass lists. my reasoning is that tensor inputs are simply better in this cases.
|
[For maintainers] Suggested jobs to run (before merge) run-slow: timesfm, timesfm2_5 |

What does this PR do?
This fixes an issues with the model that made it incompatible with exporting to onnx.
Specifically the following has been changed:
Will give this error when you try to export the timesfm module through
torch.onnx.exportSolution: use a branch-free code block which onnx can export
for ts in inputs:causes the batch size to be static when you export it via onnxSolution: Allow a tensor input to
_preprocessmethod / forward method_timesfm_moving_averagedoesn't support the above changes:Solution: Allow a tensor input there as well
Test Plan
On
main, create a test file in the repo roottext_onnx_export.py contents:
https://gist.github.com/pdufour/52c7fd722dbe8d60030469d4298779be
Run the export which will export the timesfm2_5 model to onnx
This gives you the following errors:
This is the line:
Now checkout this branch
Run the export again:
See that it passes!
Run a test:
Create a file
test_onnx_infer.pyhttps://gist.github.com/pdufour/6071e44296c236b27250ef308f8e5273
Run it:
Code Agent Policy
The Transformers repo is currently being overwhelmed by a large number of PRs and issue comments written by
code agents. We are currently bottlenecked by our ability to review and respond to them. As a result,
we ask that new users do not submit pure code agent PRs at this time.
You may use code agents in drafting or to help you diagnose issues. We'd also ask autonomous "OpenClaw"-like agents
not to open any PRs or issues for the moment.
PRs that appear to be fully agent-written will probably be closed without review, and we may block users who do this
repeatedly or maliciously.
This is a rapidly-evolving situation that's causing significant shockwaves in the open-source community. As a result,
this policy is likely to be updated regularly in the near future. For more information, please read
CONTRIBUTING.md.Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@xenova @zucchini-nlp @Cyrilvallez @ArthurZucker @vasqu @NielsRogge