AI NewsDev toolsAnnouncement

PyTorch's TorchCodec reaches 1.0, adds low-level APIs an agent can auto-tune

TorchCodec 1.0 is the PyTorch library for turning video, audio and images into tensors, and tensors back into files.

AI News

Editorial3 min read

LinkedInX
Diagram of TorchCodec three-stage decoding pipeline

Why it mattersMultimodal teams can now run demux, decode and colour conversion on their own threads. A coding agent tuned the pipeline to 1.4 to 1.9 times the baseline.

A multimodal team that reads video or audio in a PyTorch pipeline has one less thing to build. On 2 December PyTorch released TorchCodec 1.0. It is PyTorch's own media library. It turns encoded video, audio and images into tensors, and tensors back into files. The APIs have been stable for a while, PyTorch says. The 1.0 release is the point at which they promise not to change them.

TorchCodec is already in production. PyTorch names Hugging Face's datasets, transformers and LeRobot as users. These feed training and inference pipelines. vLLM uses it to decode video, audio and images sent to multimodal models, on CPU or GPU. SGLang uses it as its preferred video and audio decoder, including on the GPU.

The library handles video, audio and images. VideoDecoder reads from a file, from bytes, or from a file-like object. A worker can stream only the parts of a video it needs from the cloud. Frames can be indexed like a Python list or looked up by timestamp. Decoding runs on the CPU or on NVIDIA GPUs through NVDEC. NVDEC is a dedicated hardware decoder, separate from the CUDA cores. AudioDecoder handles every audio format FFmpeg supports. decode_image handles JPEG, PNG, WebP, GIF, AVIF and HEIC. On the A100 and H100 the JPEG decoder uses nvJPEG and the hardware JPEG block.

The low-level APIs, and what they open up

The 1.0 release adds a set of low-level APIs that split video and audio decoding into three stages. A Demuxer reads compressed packets out of the container. A VideoPacketDecoder turns packets into raw frames in YUV space. A ColorConverter turns the raw frames into RGB tensors. For audio there is an AudioPacketDecoder and an AudioConverter.

Splitting the stages lets each one run on its own thread, because every stage releases the GIL. PyTorch says that on CPU, overlapping decoding with colour conversion is often faster than running them one after the other. On CUDA, demuxing is the stage worth separating. Colour conversion there is cheap by comparison.

The stages also let a team keep the raw YUV frames. A model that trains in YUV space can skip the conversion step and the copy that goes with it. A ten-bit HDR frame comes out as uint16 with every bit intact. A packet decoder only needs codec parameters and packets. So a team can demux once into a database and decode many times. Packets can also be fed from a live stream, or read by the team's own container reader.

The agent, and the measured gain

PyTorch gave a coding agent the low-level APIs. It asked the agent to write a video decoding pipeline from scratch and climb for throughput. The agent ended up 1.4 to 1.9 times faster than a simple baseline. It was up to 1.36 times faster than an equally tuned high-level VideoDecoder. PyTorch publishes the knobs the agent found in a hill-climbing tutorial. The best knobs depend on the setup, PyTorch says. Each team should run its own search.

pip install torchcodec works on Linux, macOS and Windows. The Linux wheels carry CUDA already. TorchCodec uses the FFmpeg that is already on the system, from version 4 to 9. It is ABI stable, so one version keeps working across future PyTorch releases.

For a team doing agent work with video or audio, the shape of the 1.0 release is the useful part. Decoding is a knob, not a black box, and the agent can turn the knobs itself.

Source

SourcePyTorch

This item was written by an AI system from the linked source. Reveneau is responsible for what it publishes.

Share
LinkedInX
Start a project