Skip to content

[Tests] Fix slow video tensor creation from list of numpy arrays in SmolVLM#44731

Merged
zucchini-nlp merged 2 commits into
huggingface:mainfrom
Defalt-Meh:fix/slow-tensor-creation-video-tests
Jul 16, 2026
Merged

[Tests] Fix slow video tensor creation from list of numpy arrays in SmolVLM#44731
zucchini-nlp merged 2 commits into
huggingface:mainfrom
Defalt-Meh:fix/slow-tensor-creation-video-tests

Conversation

@Defalt-Meh

@Defalt-Meh Defalt-Meh commented Mar 15, 2026

Copy link
Copy Markdown
Contributor

CI

What does this PR do?

While running SmolVLM tests I noticed this warning in the output:

tests/test_video_processing_common.py:57: UserWarning: Creating a tensor from a list of
numpy.ndarrays is extremely slow. Please consider converting the list to a single
numpy.ndarray with numpy.array() before converting to a tensor.

Tracked it down to prepare_video() in tests/test_video_processing_common.py — it builds a list of numpy arrays (one per frame), then passes that list straight to torch.tensor(). PyTorch really doesn't like this; it ends up iterating over each array one by one instead of doing a bulk conversion.

The fix is simple — consolidate into a single numpy array first, then hand it to torch:

# before
video = torch.tensor(video).permute(0, 3, 1, 2)
 
# after
video = torch.from_numpy(np.array(video)).permute(0, 3, 1, 2)

np.array() was already imported. Output is bit-identical (verified with torch.equal()), warning is gone.

Quick benchmark on Apple M-series, 500 iterations:

Scenario Before After Speedup
What the tests actually use (8 frames, 10x10) 0.37 ms 0.002 ms ~190x
More realistic frames (32 frames, 224x224) 711 ms 0.06 ms ~11,000x

All SmolVLM tests pass (302 passed, 186 skipped, 0 failures). No other instances of this pattern in tests/*.py.

Fixes # (issue)

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@yonigozlan @zucchini-nlp

@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 29311436569:2
Result: success | Jobs: 15 | Tests: 170,983 | Failures: 0 | Duration: 15h 25m

@zucchini-nlp zucchini-nlp left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the test setting and 8 frames, not much win at the end. But yeah, why not, let's merge it an make our tests slightly faster :)

@zucchini-nlp
zucchini-nlp enabled auto-merge July 16, 2026 12:24
@zucchini-nlp
zucchini-nlp added this pull request to the merge queue Jul 16, 2026
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Merged via the queue into huggingface:main with commit fe6ff6c Jul 16, 2026
106 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants