Skip to content

Add torchcodec in docstrings/tests for datasets 4.0#39156

Merged
lhoestq merged 22 commits into
mainfrom
fix-dataset-run_object_detection-and-add-torchcodec
Jul 8, 2025
Merged

Add torchcodec in docstrings/tests for datasets 4.0#39156
lhoestq merged 22 commits into
mainfrom
fix-dataset-run_object_detection-and-add-torchcodec

Conversation

@lhoestq

@lhoestq lhoestq commented Jul 1, 2025

Copy link
Copy Markdown
Member

continuation of #39060 (so this PR also contains the object detection fix so we can run the CI with datasets on main)

it also adds support for torchcodec.decoders.AudioDecoder as input to audio classification and asr pipelines

@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.

@ydshieh

ydshieh commented Jul 1, 2025

Copy link
Copy Markdown
Collaborator

@ydshieh

ydshieh commented Jul 2, 2025

Copy link
Copy Markdown
Collaborator

Hi @lhoestq There is slack reporting issues, but I saw many failed tests

FAILED tests/models/clvp/test_modeling_clvp.py::ClvpModelForConditionalGenerationTest::test_gradient_checkpointing_backward_compatibility - ImportError: To support decoding audio data, please install 'torchcodec'.

I only add

      - name: Install datasets main
        working-directory: /transformers
        run: python3 -m pip install --no-cache-dir git+https://github.com/huggingface/datasets.git@main

maybe I should also install something else?

Could you provide a full installation steps 🙏 ? I will rerun the CI again today.

It's best to verify it before merge, as we have several events like switching all jobs to A10, run with torch 2.8 etc. and they already cause quite some chaos.

@lhoestq

lhoestq commented Jul 2, 2025

Copy link
Copy Markdown
Member Author

added the missing torchcodec installations, it should be fine now !

@ydshieh

ydshieh commented Jul 3, 2025

Copy link
Copy Markdown
Collaborator

the examples_torch job has

        if isinstance(data_args.train_val_split, float) and data_args.train_val_split > 0.0:
            split = dataset["train"].train_test_split(data_args.train_val_split, seed=training_args.seed)
            dataset["train"] = split["train"]
            dataset["validation"] = split["test"]
    
        # Get dataset categories and prepare mappings for label_name <-> label_id
>       categories = dataset["train"].features["objects"]["category"].feature.names
E       TypeError: 'Sequence' object is not subscriptable

so maybe from datasets ??

Also a tiny change to fix

https://app.circleci.com/pipelines/github/huggingface/transformers/136595/workflows/5580c370-0b69-49f5-a3e9-7c96485eab15/jobs/1810677

is also necessary. (update the 1 src/transformers/models/hubert/modeling_hubert.py or src/transformers/models/hubert/modular_hubert.py)

@lhoestq

lhoestq commented Jul 3, 2025

Copy link
Copy Markdown
Member Author

I fixed the TypeError and the modeling_hubert :)

Comment on lines +74 to +75
... speech, _ = sf.read(batch["file"])
... batch["speech"] = speech
... decoder = AudioDecoder(batch["file"])
... batch["speech"] = torch.mean(decoder.get_all_samples().data, axis=0)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

cc @eustlb since datasets is now using torchcodec I updated this part of the docs (no need to have both soundfile and torchcodec), but since torchcodec isn't available for windows users I might revert, wdyt ?

(shouldn't necessarily block this PR since the goal is to make the CI happy for datasets release, happy to revert later)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I agree that it's better not updating the default docs until it is supported also on windows. I just realised those map_to_array everywhere are broken anyway...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I reverted them (no torchcodec) and then I fixed them since the code docs example on main was actually broken

@lhoestq lhoestq marked this pull request as ready for review July 3, 2025 12:27

@qubvel qubvel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, LGTM for object detection changes

ENV UV_PYTHON=/usr/local/bin/python
RUN pip --no-cache-dir install uv && uv venv && uv pip install --no-cache-dir -U pip setuptools
RUN uv pip install --no-cache-dir 'torch' 'torchaudio' 'torchvision' --index-url https://download.pytorch.org/whl/cpu
RUN uv pip install --no-cache-dir 'torch' 'torchaudio' 'torchvision' 'torchcodec' --index-url https://download.pytorch.org/whl/cpu

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should we add it to any requirements instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's also in the requirements, at examples/pytorch/_tests_requirements.txt

I assume having it in the dockerfile with the other torch-related libs is about making sure it's the CPU version that is installed

@eustlb eustlb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @lhoestq! I love torchcodec! (it would be amazing in the near future to move from numpy arrays by default → torch to everything torch native?). Comments:

  1. can we revert doc changes? snippets for the few I tried are broken anyway, let's change to torchcodec when it is not blocking for windows users
  2. why the stereo → mono conversion in the pipeline? AFAIK it is directly handled by the models no?

Comment on lines +74 to +75
... speech, _ = sf.read(batch["file"])
... batch["speech"] = speech
... decoder = AudioDecoder(batch["file"])
... batch["speech"] = torch.mean(decoder.get_all_samples().data, axis=0)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I agree that it's better not updating the default docs until it is supported also on windows. I just realised those map_to_array everywhere are broken anyway...

Comment on lines +376 to +377
_data = torch.mean(_data, 0) if _data.ndim > 1 else _data
inputs = {"array": _data, "sampling_rate": _audio_samples.sample_rate}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

AFAIK we do not do mono → stereo conversion here by default has it's directly handled by the model's feature extractor (eg here). Have you added it here for any reason?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done ! ok to merge ?

@lhoestq

lhoestq commented Jul 4, 2025

Copy link
Copy Markdown
Member Author

can we revert doc changes? snippets for the few I tried are broken anyway, let's change to torchcodec when it is not blocking for windows users

sure !

why the stereo → mono conversion in the pipeline? AFAIK it is directly handled by the models no?

oh I didn't know that, let me change it :)

I was converting to mono because the {"array": ..., "sampling_rate": ...} input format was already mono so I assumed the models were also mono.

@eustlb eustlb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Tiny nit, otherwise LGTM! 🤗

@@ -436,9 +436,10 @@ def forward(
input_values (`torch.FloatTensor` of shape `(batch_size, max_length, num_mel_bins)`):
Float values mel features extracted from the raw audio waveform. Raw audio waveform can be obtained by
loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we propagate this change?

Suggested change
loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via
loading a `.flac` or `.wav` audio file into an array of type `list[float]`, a `numpy.ndarray` or a `torch.Tensor`, *e.g.* via

@github-actions

github-actions Bot commented Jul 8, 2025

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: audio_spectrogram_transformer, clvp, data2vec, hubert, moonshine, qwen2_5_omni, qwen2_audio, sew, sew_d, speech_encoder_decoder, speech_to_text

@lhoestq lhoestq merged commit 1ecd52e into main Jul 8, 2025
26 checks passed
@lhoestq lhoestq deleted the fix-dataset-run_object_detection-and-add-torchcodec branch July 8, 2025 15:06
rjgleaton pushed a commit to rjgleaton/transformers that referenced this pull request Jul 17, 2025
)

* fix dataset run_object_detection

* bump version

* keep same dataset actually

* torchcodec in docstrings and testing utils

* torchcodec in dockerfiles and requirements

* remove duplicate

* add torchocodec to all the remaining docker files

* fix tests

* support torchcodec in audio classification and ASR

* [commit to revert] build ci-dev images

* [commit to revert] trigger circleci

* [commit to revert] build ci-dev images

* fix

* fix modeling_hubert

* backward compatible run_object_detection

* revert ci trigger commits

* fix mono conversion and support torch tensor as input

* revert map_to_array docs + fix it

* revert mono

* nit in docstring

* style

* fix modular

---------

Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
)

* fix dataset run_object_detection

* bump version

* keep same dataset actually

* torchcodec in docstrings and testing utils

* torchcodec in dockerfiles and requirements

* remove duplicate

* add torchocodec to all the remaining docker files

* fix tests

* support torchcodec in audio classification and ASR

* [commit to revert] build ci-dev images

* [commit to revert] trigger circleci

* [commit to revert] build ci-dev images

* fix

* fix modeling_hubert

* backward compatible run_object_detection

* revert ci trigger commits

* fix mono conversion and support torch tensor as input

* revert map_to_array docs + fix it

* revert mono

* nit in docstring

* style

* fix modular

---------

Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
)

* fix dataset run_object_detection

* bump version

* keep same dataset actually

* torchcodec in docstrings and testing utils

* torchcodec in dockerfiles and requirements

* remove duplicate

* add torchocodec to all the remaining docker files

* fix tests

* support torchcodec in audio classification and ASR

* [commit to revert] build ci-dev images

* [commit to revert] trigger circleci

* [commit to revert] build ci-dev images

* fix

* fix modeling_hubert

* backward compatible run_object_detection

* revert ci trigger commits

* fix mono conversion and support torch tensor as input

* revert map_to_array docs + fix it

* revert mono

* nit in docstring

* style

* fix modular

---------

Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
)

* fix dataset run_object_detection

* bump version

* keep same dataset actually

* torchcodec in docstrings and testing utils

* torchcodec in dockerfiles and requirements

* remove duplicate

* add torchocodec to all the remaining docker files

* fix tests

* support torchcodec in audio classification and ASR

* [commit to revert] build ci-dev images

* [commit to revert] trigger circleci

* [commit to revert] build ci-dev images

* fix

* fix modeling_hubert

* backward compatible run_object_detection

* revert ci trigger commits

* fix mono conversion and support torch tensor as input

* revert map_to_array docs + fix it

* revert mono

* nit in docstring

* style

* fix modular

---------

Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
)

* fix dataset run_object_detection

* bump version

* keep same dataset actually

* torchcodec in docstrings and testing utils

* torchcodec in dockerfiles and requirements

* remove duplicate

* add torchocodec to all the remaining docker files

* fix tests

* support torchcodec in audio classification and ASR

* [commit to revert] build ci-dev images

* [commit to revert] trigger circleci

* [commit to revert] build ci-dev images

* fix

* fix modeling_hubert

* backward compatible run_object_detection

* revert ci trigger commits

* fix mono conversion and support torch tensor as input

* revert map_to_array docs + fix it

* revert mono

* nit in docstring

* style

* fix modular

---------

Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
)

* fix dataset run_object_detection

* bump version

* keep same dataset actually

* torchcodec in docstrings and testing utils

* torchcodec in dockerfiles and requirements

* remove duplicate

* add torchocodec to all the remaining docker files

* fix tests

* support torchcodec in audio classification and ASR

* [commit to revert] build ci-dev images

* [commit to revert] trigger circleci

* [commit to revert] build ci-dev images

* fix

* fix modeling_hubert

* backward compatible run_object_detection

* revert ci trigger commits

* fix mono conversion and support torch tensor as input

* revert map_to_array docs + fix it

* revert mono

* nit in docstring

* style

* fix modular

---------

Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
)

* fix dataset run_object_detection

* bump version

* keep same dataset actually

* torchcodec in docstrings and testing utils

* torchcodec in dockerfiles and requirements

* remove duplicate

* add torchocodec to all the remaining docker files

* fix tests

* support torchcodec in audio classification and ASR

* [commit to revert] build ci-dev images

* [commit to revert] trigger circleci

* [commit to revert] build ci-dev images

* fix

* fix modeling_hubert

* backward compatible run_object_detection

* revert ci trigger commits

* fix mono conversion and support torch tensor as input

* revert map_to_array docs + fix it

* revert mono

* nit in docstring

* style

* fix modular

---------

Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
)

* fix dataset run_object_detection

* bump version

* keep same dataset actually

* torchcodec in docstrings and testing utils

* torchcodec in dockerfiles and requirements

* remove duplicate

* add torchocodec to all the remaining docker files

* fix tests

* support torchcodec in audio classification and ASR

* [commit to revert] build ci-dev images

* [commit to revert] trigger circleci

* [commit to revert] build ci-dev images

* fix

* fix modeling_hubert

* backward compatible run_object_detection

* revert ci trigger commits

* fix mono conversion and support torch tensor as input

* revert map_to_array docs + fix it

* revert mono

* nit in docstring

* style

* fix modular

---------

Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
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.

5 participants