Skip to content

Commit

Permalink
Remove pin memory from binding (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
mthrok authored Oct 4, 2024
1 parent 7efbedc commit 5dffc1b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 44 deletions.
50 changes: 10 additions & 40 deletions src/binding/core/conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@

#include "gil.h"

#include <cstring>

namespace nb = nanobind;

namespace spdl::core {
namespace {
template <MediaType media_type>
CPUBufferPtr convert(
const FFmpegFramesPtr<media_type>&& frames,
bool pin_memory) {
CPUBufferPtr convert(const FFmpegFramesPtr<media_type>&& frames) {
RELEASE_GIL();
return convert_frames(frames.get(), pin_memory);
return convert_frames(frames.get());
}

template <MediaType media_type>
Expand All @@ -42,48 +38,22 @@ std::vector<const spdl::core::FFmpegFrames<media_type>*> _ref(
}

template <MediaType media_type>
CPUBufferPtr batch_convert(
std::vector<FFmpegFramesPtr<media_type>>&& frames,
bool pin_memory) {
CPUBufferPtr batch_convert(std::vector<FFmpegFramesPtr<media_type>>&& frames) {
RELEASE_GIL();
return convert_frames(_ref(frames), pin_memory);
return convert_frames(_ref(frames));
}
} // namespace

void register_conversion(nb::module_& m) {
////////////////////////////////////////////////////////////////////////////////
// Frame conversion
////////////////////////////////////////////////////////////////////////////////
m.def(
"convert_frames",
&convert<MediaType::Audio>,
nb::arg("frames"),
nb::arg("pin_memory") = false);
m.def(
"convert_frames",
&convert<MediaType::Video>,
nb::arg("frames"),
nb::arg("pin_memory") = false);
m.def(
"convert_frames",
&convert<MediaType::Image>,
nb::arg("frames"),
nb::arg("pin_memory") = false);
m.def("convert_frames", &convert<MediaType::Audio>, nb::arg("frames"));
m.def("convert_frames", &convert<MediaType::Video>, nb::arg("frames"));
m.def("convert_frames", &convert<MediaType::Image>, nb::arg("frames"));

m.def(
"convert_frames",
&batch_convert<MediaType::Audio>,
nb::arg("frames"),
nb::arg("pin_memory") = false);
m.def(
"convert_frames",
&batch_convert<MediaType::Video>,
nb::arg("frames"),
nb::arg("pin_memory") = false);
m.def(
"convert_frames",
&batch_convert<MediaType::Image>,
nb::arg("frames"),
nb::arg("pin_memory") = false);
m.def("convert_frames", &batch_convert<MediaType::Audio>, nb::arg("frames"));
m.def("convert_frames", &batch_convert<MediaType::Video>, nb::arg("frames"));
m.def("convert_frames", &batch_convert<MediaType::Image>, nb::arg("frames"));
}
} // namespace spdl::core
12 changes: 8 additions & 4 deletions src/spdl/io/_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@ async def async_load_image_batch(
decode_config: DecodeConfig | None = None,
filter_desc: str | None = _FILTER_DESC_DEFAULT,
device_config: None = None,
pin_memory: bool = False,
strict: bool = True,
**kwargs,
) -> CPUBuffer: ...
Expand All @@ -437,7 +436,6 @@ async def async_load_image_batch(
decode_config: DecodeConfig | None = None,
filter_desc: str | None = _FILTER_DESC_DEFAULT,
device_config: CUDAConfig,
pin_memory: bool = False,
strict: bool = True,
**kwargs,
) -> CUDABuffer: ...
Expand All @@ -453,7 +451,6 @@ async def async_load_image_batch(
decode_config=None,
filter_desc=_FILTER_DESC_DEFAULT,
device_config=None,
pin_memory=False,
strict=True,
**kwargs,
):
Expand Down Expand Up @@ -544,6 +541,13 @@ async def async_load_image_batch(
if not srcs:
raise ValueError("`srcs` must not be empty.")

if "pin_memory" in kwargs:
warnings.warn(
"`pin_memory` argument has been removed. Use `storage` instead.",
stacklevel=2,
)
kwargs.pop("pin_memory")

if device_config is None and "cuda_config" in kwargs:
warnings.warn(
"The `cuda_config` argument has ben renamed to `device_config`.",
Expand Down Expand Up @@ -582,7 +586,7 @@ async def async_load_image_batch(
if not frames:
raise RuntimeError("Failed to load all the images.")

buffer = await _core.async_convert_frames(frames, pin_memory=pin_memory)
buffer = await _core.async_convert_frames(frames)

if device_config is not None:
buffer = await _core.async_transfer_buffer(buffer, device_config=device_config)
Expand Down
12 changes: 12 additions & 0 deletions src/spdl/io/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,12 @@ def convert_frames(
- ``W``: width
- ``H``: height
"""
if "pin_memory" in kwargs:
warnings.warn(
"`pin_memory` argument has been removed. Use `storage` instead.",
stacklevel=2,
)
kwargs.pop("pin_memory")
return _libspdl.convert_frames(frames, **kwargs)


Expand All @@ -567,6 +573,12 @@ async def async_convert_frames(
**kwargs,
) -> CPUBuffer:
"""Async version of :py:func:`~spdl.io.convert_frames`."""
if "pin_memory" in kwargs:
warnings.warn(
"`pin_memory` argument has been removed. Use `storage` instead.",
stacklevel=2,
)
kwargs.pop("pin_memory")
return await run_async(convert_frames, frames, **kwargs)


Expand Down

0 comments on commit 5dffc1b

Please sign in to comment.