Skip to content

Conversation

@NicolasHug
Copy link
Contributor

@NicolasHug NicolasHug commented Oct 18, 2024

Small refactor discussed in #260

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 18, 2024
@NicolasHug NicolasHug changed the title Get frames at indices cleanup Let getFramesAtIndices rely on getFrameAtIndex Oct 18, 2024
@NicolasHug NicolasHug marked this pull request as ready for review October 18, 2024 15:21
Copy link
Contributor

@ahmadsharif1 ahmadsharif1 left a comment

Choose a reason for hiding this comment

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

Can you run the benchmark just to be paranoid?

@NicolasHug
Copy link
Contributor Author

On main:

swscale
med = 21.88ms +- 2.99
filtergraph
med = 25.42ms +- 5.84

This PR:

swscale
med = 21.92ms +- 1.66
filtergraph
med = 25.83ms +- 4.60
import torch
from time import perf_counter_ns

from torchcodec.decoders._core import (
    _add_video_stream,
    create_from_file,
    get_frames_at_indices,
    scan_all_streams_to_update_metadata,
)

import torch
from time import perf_counter_ns


def bench(f, color_conversion_library, num_exp=100):

    times = []
    for _ in range(num_exp):
        VIDEO_PATH = "./test/resources/nasa_13013.mp4"
        decoder = create_from_file(VIDEO_PATH)
        _add_video_stream(
            decoder,
            color_conversion_library=color_conversion_library,
        )
        scan_all_streams_to_update_metadata(decoder)

        start = perf_counter_ns()
        f(decoder)
        end = perf_counter_ns()
        times.append(end - start)
    return torch.tensor(times).float()

def report_stats(times, unit="ms"):
    mul = {
        "ns": 1,
        "µs": 1e-3,
        "ms": 1e-6,
        "s": 1e-9,
    }[unit]
    times = times * mul
    std = times.std().item()
    med = times.median().item()
    print(f"{med = :.2f}{unit} +- {std:.2f}")
    return med



NUM_EXP = 100
stream_index=3
frame_indices = list(range(100))

def _get_frames_at_indices(decoder):
    get_frames_at_indices(decoder=decoder, stream_index=stream_index, frame_indices=frame_indices)

for color_conversion_library in ("swscale", "filtergraph"):
    print(color_conversion_library)
    times = bench(_get_frames_at_indices, color_conversion_library=color_conversion_library, num_exp=NUM_EXP)
    report_stats(times)

@NicolasHug NicolasHug merged commit c91e33e into meta-pytorch:main Oct 18, 2024
24 checks passed
@NicolasHug NicolasHug deleted the getFramesAtIndices_cleanup branch October 18, 2024 15:50
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.

3 participants