Update device agnostic encoding to support multiple devices#1500
Conversation
Signed-off-by: Edgar Romo Montiel <edgar.romo.montiel@intel.com> Co-Authored-by: Dmitry Rogozhkin <dmitry.v.rogozhkin@intel.com>
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/meta-pytorch/torchcodec/1500
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 7ab694a with merge base fb8e33d ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
NicolasHug
left a comment
There was a problem hiding this comment.
Thanks @eromomon this mostly LGTM - on top of the minor suggestion below, could you also add the new APIs you're using to the third-party tests.
There was a problem hiding this comment.
We should remove this one now that we have it in the CudaDeviceInterface class.
There was a problem hiding this comment.
Thanks! good catch. Now it is done
I've extended [DummyDeviceInterface] in test/third-party-interface/ThirdPartyInterfaceTest.cpp to override the two encoder-side APIs this PR introduces on [DeviceInterface]:
[get_encoding_pixel_format(...)] the new virtual.
[setup_hardware_frame_context_for_encoding(...)], same signature as before, but its default is now a no-op instead of throwing.
The existing [test_third_party_interface.py] cases (pkgconfig, with_prefix, no_ffmpeg) build and dlopen the updated dummy as-is, so no changes to the Python side or [CMakeLists.txt] were needed.
Let me know if I need to address something else.
Addresses review comment. Signed-off-by: Edgar Romo Montiel <edgar.romo.montiel@intel.com>
NicolasHug
left a comment
There was a problem hiding this comment.
Thanks @eromomon , LGTM
Enable hardware-agnostic encoding in
MultiStreamEncoder(XPU plugin support)Signed-off-by: Edgar Romo Montiel edgar.romo.montiel@intel.com
Co-Authored-by: Dmitry Rogozhkin dmitry.v.rogozhkin@intel.com
Summary
This PR makes
MultiStreamEncoderhardware-agnostic by moving every device-specific step of the video-encoding pipeline behind the existingDeviceInterfaceabstraction. The immediate motivation is enabling Intel XPU hardware encoding through the external torchcodec plugin mechanism, without requiring any further changes to the core encoder.Companion PR (XPU plugin implementation):
intel/torchlib-xpu#58
Motivation
Prior to this change,
Encoder.cpphad CUDA-specific branches for codec discovery, pixel-format selection, hardware-frame context setup, and tensor→AVFrameconversion. That prevented any other backend (XPU, or any future device) from providing hardware encoding without patching the core encoder.By promoting those steps to virtual hooks on
DeviceInterface, each backend (CPU, CUDA, XPU plugin, …) implements only the pieces it needs, andMultiStreamEncoderremains device-neutral.What changed
DeviceInterfacenew / generalized virtual methods used by the encoder:find_codec(codec_id, is_decoder=false)allow a device to substitute the container’s default codec with its hardware equivalent.get_encoding_pixel_format(av_codec, user_pixel_format)pixel-format selection is delegated to the device.register_hardware_device_with_codec(codec_context)no-op on CPU, attaches theAVHWDeviceContexton HW backends.setup_hardware_frame_context_for_encoding(codec_context)no-op on CPU, attaches anAVHWFramesContexton HW backends.convert_tensor_to_av_frame_for_encoding(tensor, frame_index, codec_context)device owns tensor→AVFrameconversion (upload, tiling, colour conversion, etc.).Encoder.cpp(MultiStreamEncoder::initialize_video_streamandadd_frames) CUDA-specific branches removed and replaced with unconditional calls to the hooks above. The CPU path is preserved via default no-op implementations.CpuDeviceInterface/CudaDeviceInterfaceexisting behaviour preserved by implementing the new hooks (CUDA keeps NV12 default, hardware frames context, andnvenccodec substitution).test/plugin/,torchcodec/_core/DeviceInterface.h) unchanged in shape, the XPU plugin implements the same virtual interface out-of-tree.Design notes
register_device_interface).Testing
Risk / compatibility
STD_TORCH_CHECK(false, ...)) so unrelated backends fail loudly rather than silently misbehave.Follow-ups (out of scope)
Thanks in advance for the review, happy to split further or adjust the hook naming/signatures if that helps land this cleanly.
CC: @dvrogozh, @NicolasHug, @Dan-Flores