Skip to content

Audio array remains NumPy after Torch formatting #8755

Open

Description

@abellion

Describe the bug

Since the migration to TorchCodec in Datasets 4.0.0, calling set_format(type="torch") or with_format("torch") on a dataset no longer makes the "array" field of an audio column a PyTorch tensor. It is a NumPy array instead.

However, the current Use with PyTorch documentation explicitly shows the "audio" field being a proper PyTorch tensor after with_format("torch").

Steps to reproduce the bug

from datasets import load_dataset

ds = load_dataset(
    "hf-internal-testing/librispeech_asr_demo",
    "clean",
    split="validation",
)
ds.set_format(type="torch")

audio = ds[0]["audio"]["array"]

print(type(audio)) # numpy.ndarray

Expected behavior

Following the documentation and the behavior before 4.0.0, the "array" field of an audio column should return a PyTorch Tensor when Torch formatting is enabled.

Environment info

  • datasets version: 5.0.1
  • Platform: Linux-7.0.14-linuxkit-aarch64-with-glibc2.41
  • Python version: 3.13.9
  • huggingface_hub version: 1.33.0
  • PyArrow version: 25.0.1
  • Pandas version: 3.0.6
  • fsspec version: 2026.6.0

Activity

  1. ssivitskii commented on Oct 8, 2026

    @ssivitskii

    Hi, I'd like to help with this issue. I see #8757 is already updating the audio examples in the formatting docs. I'd be interested in taking on any remaining investigation or test coverage around set_format(type="torch") / with_format("torch") and the audio decoding behavior.

    Could you confirm the intended behavior after the TorchCodec migration, and whether there is a useful follow-up I could work on alongside the docs PR?

  2. krao19 commented on Oct 26, 2026

    @krao19

    Hi, I looked into this and can reproduce it on current main with a local wav file (no Hub access needed):

    with_format(None)    -> numpy.ndarray
    with_format("numpy") -> numpy.ndarray
    with_format("torch") -> numpy.ndarray
    

    It comes from two places. TorchFormatter passes the torchcodec AudioDecoder through untouched, and the AudioDecoder wrapper in features/_torchcodec.py always does .cpu().numpy() when you ask for ["array"], even though torchcodec already returns a tensor. So you get numpy whatever the format is.

    #8757 updates the docs to match this behaviour. If you'd rather have the code match the old docs instead, I have a small change ready: the torch formatter flags the decoder so ["array"] stays a tensor (channel averaging done in torch), and the numpy path is unchanged. It comes with a regression test that fails on main and passes with the change, and stereo files give the same values as the numpy path.

    Which way would you prefer? Happy to open a PR if the code fix is wanted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions