fix: copy cropped tensor slices so the uncropped storage can be freed

This commit is contained in:
Terry Jia
2026-08-20 20:47:43 -04:00
parent 4a2058c7c7
commit b71ea70f33
2 changed files with 18 additions and 2 deletions

View File

@@ -91,11 +91,11 @@ class VideoInput(ABC):
cx, cy, cw, ch = rect
return VideoFromComponents(
VideoComponents(
images=components.images[:, cy:cy + ch, cx:cx + cw, :],
images=components.images[:, cy:cy + ch, cx:cx + cw, :].clone(),
audio=components.audio,
frame_rate=components.frame_rate,
metadata=components.metadata,
alpha=components.alpha[:, cy:cy + ch, cx:cx + cw]
alpha=components.alpha[:, cy:cy + ch, cx:cx + cw].clone()
if components.alpha is not None
else None,
),

View File

@@ -1348,3 +1348,19 @@ def test_cropped_decode_and_save_paths_select_same_pixels(tmp_path):
assert save_column in (2, 3)
assert decode_column == save_column
def test_as_cropped_components_releases_uncropped_storage():
images = torch.rand(2, 8, 8, 3)
video = VideoFromComponents(
VideoComponents(images=images, frame_rate=Fraction(8))
)
cropped = video.as_cropped(0, 0, 4, 4)
cropped_images = cropped.get_components().images
assert tuple(cropped_images.shape[1:3]) == (4, 4)
assert (
cropped_images.untyped_storage().data_ptr()
!= images.untyped_storage().data_ptr()
)