Skip to content

Turn BytesContext into FromTensorContext #721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 11, 2025
Merged

Conversation

scotts
Copy link
Contributor

@scotts scotts commented Jun 11, 2025

Fixes #720.

Reading from a raw void* is problematic because we have no way of claiming ownership of it. This PR makes sure reading from bytes only happens through a tensor. The tensor is effectively a smart pointer, so it ensures that we actually keep the data around. We now have both a tensor-reading context and a tensor-writing context. This PR also refactors both to share code - which we thought we may eventually need to do.

The test added by this PR fails without these changes in the C++ level.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jun 11, 2025
@scotts scotts marked this pull request as ready for review June 11, 2025 14:28
Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!!

Comment on lines 129 to 130
def test_create_bytes_ownership(self):
# Note that the bytes object we use to instantiate the decoder does not
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_create_bytes_ownership(self):
# Note that the bytes object we use to instantiate the decoder does not
def test_create_bytes_ownership(self):
# Non-regression test for https://github.com/pytorch/torchcodec/issues/720
# Note that the bytes object we use to instantiate the decoder does not


SingleStreamDecoder::SeekMode realSeek = SingleStreamDecoder::SeekMode::exact;
if (seek_mode.has_value()) {
realSeek = seekModeFromString(seek_mode.value());
}

auto contextHolder = std::make_unique<AVIOBytesContext>(data, length);
auto contextHolder = std::make_unique<AVIOFromTensorContext>(video_tensor);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you confirm my understanding that the crux of the fix is this line, where we now pass-in the tensor which is ref-counted, instead of passing the raw underlying data which wasn't ref-counted and thus freed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is correct!

Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!!

@scotts scotts merged commit f4a351c into pytorch:main Jun 11, 2025
46 of 51 checks passed
@scotts scotts deleted the own_bytes branch June 11, 2025 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Meta Open Source bot.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Creating a decoder from bytes does not adopt the memory
3 participants