Skip to content

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

Open
Defalt-Meh wants to merge 1 commit intohuggingface:mainfrom
Defalt-Meh:fix/slow-tensor-creation-video-tests
Open

[Tests] Fix slow video tensor creation from list of numpy arrays in SmolVLM#44731
Defalt-Meh wants to merge 1 commit intohuggingface:mainfrom
Defalt-Meh:fix/slow-tensor-creation-video-tests

Conversation

@Defalt-Meh
Copy link
Copy Markdown

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

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.

1 participant