Port C++ code to rely on torch stable ABI API.#1188
Port C++ code to rely on torch stable ABI API.#1188NicolasHug wants to merge 26 commits intometa-pytorch:mainfrom
Conversation
| class DummyDeviceInterface : public DeviceInterface { | ||
| public: | ||
| DummyDeviceInterface(const torch::Device& device) : DeviceInterface(device) {} | ||
| DummyDeviceInterface(const StableDevice& device) : DeviceInterface(device) {} |
There was a problem hiding this comment.
@dvrogozh , heads up that this is something we might want to do in the near future.
The general context is that we want to rely on torch stable ABI so that we (or you) don't have to synchronize torchcodec releases with torch releases 6X a year.
I think it means that the third-party extensions will have to pass-in "Stable" types - you might still be able to use the non-stable ones internally as long as you convert them.
Let me know if you have any concern about that.
Note that the PR is in a very, very WIP state at the moment. Plenty of things are likely to change. But this is the direction we're heading to.
There was a problem hiding this comment.
@NicolasHug , thank your for heads up. I will start looking into the story, but "stable" sounds very appealing. Once PR will be in a closer to merge stage I would love to give it a try on my side (create respective PR for xpu and see how it goes).
| # Set PyTorch stable ABI target version to 2.11 for consumers. | ||
| # This ensures third-party code uses stable ABI types compatible with | ||
| # the library. Format: 0x MAJOR(2) MINOR(2) PATCH(2) TWEAK(2) zeros(8) | ||
| INTERFACE_COMPILE_DEFINITIONS "TORCH_TARGET_VERSION=0x020b000000000000" |
There was a problem hiding this comment.
TODO: I'm not sure we'll want to enforce that on third-parties?
CC @dvrogozh
| // For Decoding: enables users to pass in the entire video or audio as bytes. | ||
| // Our read and seek functions then traverse the bytes in memory. | ||
| class AVIOFromTensorContext : public AVIOContextHolder { | ||
| class TORCHCODEC_API AVIOFromTensorContext : public AVIOContextHolder { |
There was a problem hiding this comment.
TODO: understand why that's needed. Ideally it shouldn't.
| if (!device_.has_index()) { | ||
| int resolvedIndex = getDeviceIndex(device_); | ||
| device_.set_index(static_cast<StableDeviceIndex>(resolvedIndex)); | ||
| } |
There was a problem hiding this comment.
TODO above: this was causing a hang - investigate.
src/torchcodec/_core/CMakeLists.txt
Outdated
| target_include_directories(${library_name} | ||
| BEFORE PRIVATE | ||
| ${pybind11_INCLUDE_DIRS} | ||
| ) |
There was a problem hiding this comment.
TODO investigate above.
| "-Wno-attributes" | ||
| ) | ||
| endif() | ||
|
|
There was a problem hiding this comment.
TODO on all these
| // For a tensor of shape [numChannels, numSamples], we need to calculate | ||
| // offsets | ||
| outputBuffers[i] = static_cast<uint8_t*>(lastSamples.mutable_data_ptr()) + | ||
| i * numRemainingSamples * sizeof(float); |
| // Note: impl_abstract_pystub is not available in stable ABI. | ||
| // The Python stubs are handled via Python-side registration instead. |
| "_add_video_stream(Tensor(a!) decoder, *, int? num_threads=None, str? dimension_order=None, int? stream_index=None, str device=\"cpu\", str device_variant=\"ffmpeg\", str transform_specs=\"\", Tensor? custom_frame_mappings_all_frames=None, Tensor? custom_frame_mappings_is_key_frame=None, Tensor? custom_frame_mappings_duration=None, str? color_conversion_library=None) -> ()"); | ||
| m.def( | ||
| "add_video_stream(Tensor(a!) decoder, *, int? num_threads=None, str? dimension_order=None, int? stream_index=None, str device=\"cpu\", str device_variant=\"ffmpeg\", str transform_specs=\"\", (Tensor, Tensor, Tensor)? custom_frame_mappings=None) -> ()"); | ||
| "add_video_stream(Tensor(a!) decoder, *, int? num_threads=None, str? dimension_order=None, int? stream_index=None, str device=\"cpu\", str device_variant=\"ffmpeg\", str transform_specs=\"\", Tensor? custom_frame_mappings_all_frames=None, Tensor? custom_frame_mappings_is_key_frame=None, Tensor? custom_frame_mappings_duration=None) -> ()"); |
There was a problem hiding this comment.
TODO: can't pass tuples anymore?
| StableTensor& decoder, | ||
| std::optional<int64_t> num_threads = std::nullopt, | ||
| std::optional<std::string_view> dimension_order = std::nullopt, | ||
| std::optional<std::string> dimension_order = std::nullopt, |
There was a problem hiding this comment.
TODO we went from string_view to string everywhere, is that OK? Is that needed?
This PR adds `deleter` support to `torch::stable::from_blob` by adding a new `aoti_torch_create_tensor_from_blob_v3`. We need it to cleanly port TorchCodec to the stable ABI in meta-pytorch/torchcodec#1188 There's a bit of scaffolding, especially for the tests where I had to create a new `test/cpp_extensions/libtorch_agn_2_11_extension` folder. Most of it is just copy/paste from `test/cpp_extensions/libtorch_agn_2_11_extension/libtorch_agn_2_10/__init__.py` Pull Request resolved: #173371 Approved by: https://github.com/janeyx99
| StableIntArrayRef(strides.data(), strides.size()), | ||
| StableDevice(kStableCPU), | ||
| kStableUInt8, | ||
| avFrameDeleter); |
No description provided.