spdl.io.fuse

fuse(fn: Callable[[P], T], /, *, cache: bool = False) → Callable[[P], T][source]
fuse(*, cache: bool = False) → Callable[[Callable[[P], T]], Callable[[P], T]]

[Experimental] Execute SPDL IO functions in a function with fewer native calls.

Each SPDL IO function call crosses the boundary between Python and C++, and releases and re-acquires the GIL. When a sample is loaded with multiple calls (demuxing, decoding and conversion, possibly repeated in a loop), re-acquiring the GIL can wait for other Python threads, which dominates the loading time in a busy process. fuse records the SPDL IO function calls in the decorated function, and executes them in C++ with one boundary crossing when the function returns.

The function runs as usual, except that SPDL IO functions return placeholders of their results (packets, frames, buffers and their metadata). When Python code needs an actual value (e.g. if frames.num_frames == 0, a filter computed from packets.width, or tensor operations on the result of to_torch()), the operations recorded so far are executed in one native call. Operations that cannot be recorded (e.g. streaming demuxing and decoding) run as usual with the actual values. The placeholders in the returned value (in lists, tuples, namedtuples, dicts and fields of dataclass instances) are replaced with the actual values.

The recorded functions are Demuxer, demux_audio(), demux_video(), demux_image(), apply_bsf(), decode_packets(), sample_decode_video(), convert_frames(), load_audio(), load_video(), load_image(), load_image_batch(), get_filter_desc(), transfer_buffer(), decode_packets_nvdec(), nv12_to_rgb() and nv12_to_bgr(). CUDA operations are recorded when their input is recorded and the CUDA extension supports fusion. CUDA buffer attributes materialize the buffer. Python allocator callbacks still acquire the GIL when allocating or freeing. Metadata of demuxers, codecs, packets and frames, clone() and indexing of video frames are recorded as well.

When the GIL is disabled (free-threaded Python), fuse does nothing, and the function runs eagerly. Without the GIL, a native call does not wait for other threads, so recording does not save time.

If executing the recorded operations fails (e.g. a source is corrupted), the function is executed again without recording, so that the error is raised where the SPDL IO function is called, and the function handles it (or not) as it does without fuse.

If recording fails instead (fusion fails, e.g. the function uses SPDL IO in a way that fuse does not support), the function is executed again without recording, and fusion is disabled for the function for the rest of the process. A warning with the cause is logged once.

Note

In both cases, Python code in the function runs twice. Avoid side effects that must not be repeated, such as consuming an iterator or modifying an argument.

The following figure and table show the time per call of decoding 32 PNG images and batching them, demuxing 4 windows of a video and decoding them, load_audio(), and a function that computes the filter from packets.width, which cannot be cached, as GIL contention grows from 0% to 100% (Python 3.14 and its free-threaded build 3.14t, mean and 95% confidence interval of 10 trials, in milliseconds). GIL contention is the share of time another Python thread runs Python code. Without fusion, every native call has to re-acquire the GIL, so the time grows quickly with the contention. With fusion, a call re-acquires the GIL about once, so its time grows much more slowly. Compared to the gain of fusing itself, the difference that cache=True makes is marginal. Without the GIL, the time does not change. The dotted line in the figure is the time without contention, and its y-axis is in log scale.

Time per call of the functions without fusion, with fuse, with fuse(cache=True), and without fusion on the free-threaded build, as GIL contention grows from 0% to 100%.

Function

Contention

Eager

fuse

cache=True

3.14t eager

32 PNG images

0%

5.05 ± 0.03

4.59 ± 0.02

3.92 ± 0.02

5.20 ± 0.06

32 PNG images

10%

5.59 ± 0.03

4.79 ± 0.12

4.03 ± 0.02

5.14 ± 0.02

32 PNG images

25%

6.95 ± 0.06

5.00 ± 0.04

5.00 ± 0.03

5.17 ± 0.06

32 PNG images

50%

10.65 ± 0.12

5.36 ± 0.33

5.01 ± 0.02

5.18 ± 0.06

32 PNG images

75%

22.34 ± 0.53

9.87 ± 0.16

7.98 ± 0.09

5.20 ± 0.05

32 PNG images

90%

63.56 ± 3.25

9.88 ± 0.13

8.51 ± 0.13

5.16 ± 0.06

32 PNG images

100%

839.74 ± 24.37

10.03 ± 0.04

9.32 ± 0.03

5.10 ± 0.02

video windows

0%

3.39 ± 0.01

3.46 ± 0.02

3.24 ± 0.02

3.46 ± 0.01

video windows

10%

3.64 ± 0.03

3.48 ± 0.02

3.29 ± 0.04

3.45 ± 0.01

video windows

25%

4.54 ± 0.07

3.69 ± 0.10

3.34 ± 0.02

3.48 ± 0.03

video windows

50%

6.93 ± 0.08

5.05 ± 0.03

5.02 ± 0.02

3.48 ± 0.04

video windows

75%

12.44 ± 0.46

7.40 ± 0.11

6.76 ± 0.12

3.44 ± 0.03

video windows

90%

29.96 ± 1.71

7.60 ± 0.10

7.12 ± 0.23

3.42 ± 0.03

video windows

100%

81.04 ± 4.39

8.91 ± 0.03

8.63 ± 0.05

3.42 ± 0.03

load_audio

0%

1.62 ± 0.01

1.72 ± 0.01

1.59 ± 0.01

1.64 ± 0.01

load_audio

10%

1.71 ± 0.03

1.75 ± 0.03

1.65 ± 0.01

1.64 ± 0.01

load_audio

25%

2.11 ± 0.08

2.05 ± 0.05

2.01 ± 0.03

1.65 ± 0.02

load_audio

50%

3.29 ± 0.11

3.40 ± 0.06

2.99 ± 0.18

1.64 ± 0.01

load_audio

75%

6.55 ± 0.05

3.92 ± 0.07

3.50 ± 0.12

1.63 ± 0.01

load_audio

90%

16.04 ± 1.49

5.03 ± 0.00

5.03 ± 0.00

1.66 ± 0.02

load_audio

100%

36.86 ± 1.63

7.15 ± 0.08

6.98 ± 0.06

1.63 ± 0.01

metadata used

0%

2.45 ± 0.01

2.61 ± 0.01

2.61 ± 0.01

2.48 ± 0.02

metadata used

10%

2.52 ± 0.03

2.70 ± 0.02

2.71 ± 0.02

2.47 ± 0.04

metadata used

25%

3.08 ± 0.07

3.36 ± 0.05

3.35 ± 0.05

2.46 ± 0.03

metadata used

50%

4.91 ± 0.10

5.09 ± 0.08

5.04 ± 0.02

2.46 ± 0.02

metadata used

75%

9.62 ± 0.22

10.01 ± 0.04

10.04 ± 0.03

2.46 ± 0.02

metadata used

90%

15.33 ± 2.09

10.07 ± 0.00

10.07 ± 0.00

2.46 ± 0.02

metadata used

100%

33.72 ± 0.98

18.69 ± 0.09

18.67 ± 0.07

2.47 ± 0.04

See Fusing IO Calls for the details and guidelines.

Added in version 0.9.0.

Parameters:
  • fn – A function that loads media with SPDL IO functions.

  • cache –

    Optional: If True, after a call executed the recorded operations as one native call, the next call with the same argument structure records the program by replacing the source arguments (str, bytes and other buffer objects) with placeholders. The program is used for the subsequent calls with the same argument structure, without executing the function. The argument structure is the nesting of lists, tuples, namedtuples and dicts (exact types), and the values of non-source arguments. The programs for the last 128 argument structures are kept. This saves the cost of executing the function in Python, but it is only correct if the function depends only on its arguments. (If an argument classified as a source is used in other ways, e.g. formatted into a string, the function is not cached.)

    Compared to the gain of fusing itself, the difference that cache=True makes is marginal (see the table above). At 100% GIL contention, fusing saves 15 to 830 ms per call, and the cache saves a further 0.2 to 0.7 ms. Without contention, the cache saves 0.1 to 0.7 ms.

Returns:

A function with the same signature as fn. (If fn is omitted, a decorator.)

Example

@spdl.io.fuse
def load_batch(srcs: list[bytes]) -> spdl.io.CPUBuffer:
    frames = [
        spdl.io.decode_packets(spdl.io.demux_image(src))
        for src in srcs
    ]
    return spdl.io.convert_frames(frames)

buffer = load_batch([data1, data2])  # One call into C++