Reduce the diff with the pytrch:main branch#3
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to reduce divergence from the upstream PyTorch torchcodec main branch by aligning several C++ core behaviors and operator registrations.
Changes:
- Implemented previously stubbed/unimplemented paths for audio resampler flush (
maybeFlushSwrBuffers) and tensor-backed AVIO output (AVIOToTensorContext::getOutputTensor). - Adjusted custom op registration blocks (including adding a
BackendSelectregistration block) and minor include/formatting alignment.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/torchcodec/_core/SingleStreamDecoder.cpp | Implements returning flushed resampled audio samples instead of throwing. |
| src/torchcodec/_core/custom_ops.cpp | Reorders includes and changes TORCH_LIBRARY_IMPL registrations (adds BackendSelect). |
| src/torchcodec/_core/AVIOTensorContext.cpp | Implements returning the encoded output tensor slice instead of throwing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -1419,11 +1419,8 @@ std::optional<torch::Tensor> SingleStreamDecoder::maybeFlushSwrBuffers() { | |||
| auto actualNumRemainingSamples = swr_convert( | |||
| swrContext_.get(), outputBuffers.data(), numRemainingSamples, nullptr, 0); | |||
There was a problem hiding this comment.
swr_convert() can return a negative value on error. Passing that directly as the length to lastSamples.narrow(...) will throw an unrelated/cryptic error (or potentially create an invalid slice). Add an explicit check for actualNumRemainingSamples < 0 and surface a clear failure (e.g., TORCH_CHECK(actualNumRemainingSamples >= 0, ...)) before narrowing; optionally also handle the == 0 case explicitly.
| swrContext_.get(), outputBuffers.data(), numRemainingSamples, nullptr, 0); | |
| swrContext_.get(), outputBuffers.data(), numRemainingSamples, nullptr, 0); | |
| TORCH_CHECK( | |
| actualNumRemainingSamples >= 0, | |
| "swr_convert() failed while flushing buffered audio samples: ", | |
| actualNumRemainingSamples); | |
| if (actualNumRemainingSamples == 0) { | |
| return std::nullopt; | |
| } |
| TORCH_LIBRARY_IMPL(torchcodec_ns, BackendSelect, m) { | ||
| m.impl("create_from_file", &create_from_file); | ||
| m.impl("create_from_tensor", &create_from_tensor); | ||
| m.impl("_create_from_file_like", &_create_from_file_like); |
There was a problem hiding this comment.
Registering create_from_tensor under BackendSelect is risky because it has a Tensor argument and the implementation assumes host-accessible memory (no video_tensor.is_cpu() check; it is passed into AVIOFromTensorContext which reads via data_ptr()/memcpy). With this dispatch key change, CUDA tensors may no longer fail dispatch and could reach this CPU-only code path. Consider registering create_from_tensor under CPU (and only keeping no-Tensor factory ops like create_from_file / _get_json_ffmpeg_library_versions under BackendSelect), or add a strict CPU device check for video_tensor if BackendSelect is intentional.
减少与 Pytorch mian 分支的 diff
相关链接
c10::DispatchKeyand replace dispatch_key_to_string withc10::toStringPaddlePaddle/Paddle#78525std::optionalin error messages PaddlePaddle/Paddle#78521BackendSelectdispatch lookup in torch compat PaddlePaddle/Paddle#78591