Skip to content

Commit 13fad10

Browse files
committed
Make device and device_variant have a default instead of being std::optional
1 parent 515deb5 commit 13fad10

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

src/torchcodec/_core/custom_ops.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ TORCH_LIBRARY(torchcodec_ns, m) {
4343
m.def(
4444
"_create_from_file_like(int file_like_context, str? seek_mode=None) -> Tensor");
4545
m.def(
46-
"_add_video_stream(Tensor(a!) decoder, *, int? width=None, int? height=None, int? num_threads=None, str? dimension_order=None, int? stream_index=None, str? device=None, str? device_variant=None, (Tensor, Tensor, Tensor)? custom_frame_mappings=None, str? color_conversion_library=None) -> ()");
46+
"_add_video_stream(Tensor(a!) decoder, *, int? width=None, int? height=None, int? num_threads=None, str? dimension_order=None, int? stream_index=None, str device=\"cpu\", str device_variant=\"default\", (Tensor, Tensor, Tensor)? custom_frame_mappings=None, str? color_conversion_library=None) -> ()");
4747
m.def(
48-
"add_video_stream(Tensor(a!) decoder, *, int? width=None, int? height=None, int? num_threads=None, str? dimension_order=None, int? stream_index=None, str? device=None, str? device_variant=None, (Tensor, Tensor, Tensor)? custom_frame_mappings=None) -> ()");
48+
"add_video_stream(Tensor(a!) decoder, *, int? width=None, int? height=None, int? num_threads=None, str? dimension_order=None, int? stream_index=None, str device=\"cpu\", str device_variant=\"default\", (Tensor, Tensor, Tensor)? custom_frame_mappings=None) -> ()");
4949
m.def(
5050
"add_audio_stream(Tensor(a!) decoder, *, int? stream_index=None, int? sample_rate=None, int? num_channels=None) -> ()");
5151
m.def("seek_to_pts(Tensor(a!) decoder, float seconds) -> ()");
@@ -257,8 +257,8 @@ void _add_video_stream(
257257
std::optional<int64_t> num_threads = std::nullopt,
258258
std::optional<std::string_view> dimension_order = std::nullopt,
259259
std::optional<int64_t> stream_index = std::nullopt,
260-
std::optional<std::string_view> device = std::nullopt,
261-
std::optional<std::string_view> device_variant = std::nullopt,
260+
std::string_view device = "cpu",
261+
std::string_view device_variant = "default",
262262
std::optional<std::tuple<at::Tensor, at::Tensor, at::Tensor>>
263263
custom_frame_mappings = std::nullopt,
264264
std::optional<std::string_view> color_conversion_library = std::nullopt) {
@@ -289,16 +289,10 @@ void _add_video_stream(
289289
}
290290
}
291291

292-
if (!device.has_value()) {
293-
device = "cpu";
294-
}
295-
if (!device_variant.has_value()) {
296-
device_variant = "default";
297-
}
298-
validateDeviceInterface(std::string(*device), std::string(*device_variant));
292+
validateDeviceInterface(std::string(device), std::string(device_variant));
299293

300-
videoStreamOptions.device = torch::Device(std::string(*device));
301-
videoStreamOptions.deviceVariant = *device_variant;
294+
videoStreamOptions.device = torch::Device(std::string(device));
295+
videoStreamOptions.deviceVariant = device_variant;
302296

303297
std::optional<SingleStreamDecoder::FrameMappings> converted_mappings =
304298
custom_frame_mappings.has_value()
@@ -317,8 +311,8 @@ void add_video_stream(
317311
std::optional<int64_t> num_threads = std::nullopt,
318312
std::optional<std::string_view> dimension_order = std::nullopt,
319313
std::optional<int64_t> stream_index = std::nullopt,
320-
std::optional<std::string_view> device = std::nullopt,
321-
std::optional<std::string_view> device_variant = std::nullopt,
314+
std::string_view device = "cpu",
315+
std::string_view device_variant = "default",
322316
const std::optional<std::tuple<at::Tensor, at::Tensor, at::Tensor>>&
323317
custom_frame_mappings = std::nullopt) {
324318
_add_video_stream(

src/torchcodec/_core/ops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def _add_video_stream_abstract(
275275
num_threads: Optional[int] = None,
276276
dimension_order: Optional[str] = None,
277277
stream_index: Optional[int] = None,
278-
device: Optional[str] = None,
278+
device: str = "cpu",
279279
device_variant: str = "default",
280280
custom_frame_mappings: Optional[
281281
tuple[torch.Tensor, torch.Tensor, torch.Tensor]
@@ -294,7 +294,7 @@ def add_video_stream_abstract(
294294
num_threads: Optional[int] = None,
295295
dimension_order: Optional[str] = None,
296296
stream_index: Optional[int] = None,
297-
device: Optional[str] = None,
297+
device: str = "cpu",
298298
device_variant: str = "default",
299299
custom_frame_mappings: Optional[
300300
tuple[torch.Tensor, torch.Tensor, torch.Tensor]

0 commit comments

Comments
 (0)