[Perf] Bound memory during video frame conversion (#5732)
Signed-off-by: Xunzhuo Liu <xunzhuo.liu@amd.com>
This commit is contained in:
1 parent
93457f29ff
commit
a9ba47d8ce
2 files changed
+70
-10
No files matched your search
@@ -48,6 +48,52 @@ def test_encode_video_bytes_exports_frames_without_interpolation(monkeypatch):
|
||||
assert mux_calls[0]["audio"] is None
|
||||
|
||||
|
||||
def test_float_frames_are_converted_without_stacking_full_video(monkeypatch):
|
||||
frame = np.array(
|
||||
[
|
||||
[[0.0, 0.5, 1.0, 0.2], [1.5, -0.5, 0.5, 0.8]],
|
||||
],
|
||||
dtype=np.float32,
|
||||
)
|
||||
original = frame.copy()
|
||||
|
||||
def fail_stack(*args, **kwargs):
|
||||
raise AssertionError("float video conversion must not stack all frames")
|
||||
|
||||
monkeypatch.setattr(video_api_utils.np, "stack", fail_stack)
|
||||
|
||||
frames = video_api_utils._coerce_video_to_uint8_frames([frame, frame])
|
||||
|
||||
expected = np.array([[[128, 191, 255], [255, 64, 191]]], dtype=np.uint8)
|
||||
assert frames.flags.c_contiguous
|
||||
np.testing.assert_array_equal(frames, np.array([expected, expected]))
|
||||
np.testing.assert_array_equal(frame, original)
|
||||
|
||||
|
||||
def test_two_dimensional_width_four_frames_are_not_treated_as_rgba():
|
||||
frame = np.arange(8, dtype=np.float32).reshape(2, 4) / 8
|
||||
|
||||
frames = video_api_utils._coerce_video_to_uint8_frames([frame, frame])
|
||||
|
||||
expected = np.rint(frame * 255).astype(np.uint8)
|
||||
assert frames.shape == (2, 2, 4)
|
||||
np.testing.assert_array_equal(frames, np.stack([expected, expected]))
|
||||
|
||||
|
||||
def test_mixed_float_dtypes_preserve_stacked_rounding_semantics():
|
||||
frames = [
|
||||
np.full((1, 1, 3), 0.1, dtype=np.float16),
|
||||
np.full((1, 1, 3), 0.2, dtype=np.float32),
|
||||
]
|
||||
stacked = np.stack(frames)
|
||||
expected = np.rint(np.clip(stacked, 0.0, 1.0) * 255.0).astype(np.uint8)
|
||||
|
||||
actual = video_api_utils._coerce_video_to_uint8_frames(frames)
|
||||
|
||||
assert expected[0, 0, 0, 0] == 25
|
||||
np.testing.assert_array_equal(actual, expected)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("frame_count", [3, 4])
|
||||
def test_channel_last_video_tensor_preserves_channel_sized_frame_count(frame_count):
|
||||
video = torch.arange(frame_count * 2 * 5 * 3, dtype=torch.uint8).reshape(frame_count, 2, 5, 3)
|
||||
|
||||
Reference in new issue
Block a user