Skip to content

Commit 27c9293

Browse files
committed
refac
1 parent cb83101 commit 27c9293

6 files changed

Lines changed: 26 additions & 29 deletions

File tree

src/torchcodec/_core/ColorConverter.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@
1515

1616
namespace facebook::torchcodec {
1717

18+
namespace {
19+
// Only ever used to name a device in the error message below.
20+
std::string printable(const StableDevice& device) {
21+
std::string name = device_type_name(device.type());
22+
if (device.type() != kStableCPU && device.index() >= 0) {
23+
name += ":" + std::to_string(device.index());
24+
}
25+
return name;
26+
}
27+
} // namespace
28+
1829
ColorConverter::ColorConverter(
1930
const StableDevice& device,
2031
OutputDtypeConfig output_dtype_config)
@@ -51,13 +62,13 @@ void ColorConverter::maybe_initialize_interface(OutputDtype output_dtype) {
5162

5263
torch::stable::Tensor ColorConverter::convert(
5364
const AVFrame& av_frame,
54-
const std::string& frame_device) {
65+
const StableDevice& frame_device) {
5566
STD_TORCH_CHECK(
56-
StableDevice(frame_device) == device_,
67+
frame_device == device_,
5768
"This ColorConverter is on ",
58-
device_to_string(device_),
69+
printable(device_),
5970
" but the frame's samples are on ",
60-
frame_device,
71+
printable(frame_device),
6172
". A ColorConverter only converts frames that are already on its own "
6273
"device: create one per device, or move the RGB output afterwards.");
6374

src/torchcodec/_core/ColorConverter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class FORCE_PUBLIC_VISIBILITY ColorConverter {
2828
// converter's device: we refuse to move samples around behind your back.
2929
torch::stable::Tensor convert(
3030
const AVFrame& av_frame,
31-
const std::string& frame_device);
31+
const StableDevice& frame_device);
3232

3333
private:
3434
void maybe_initialize_interface(OutputDtype output_dtype);

src/torchcodec/_core/DeviceInterface.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,6 @@ StableDeviceType parse_device_type(const std::string& device_type) {
4747

4848
} // namespace
4949

50-
std::string device_to_string(const StableDevice& device) {
51-
std::string name = device_type_name(device.type());
52-
// A negative index means "unspecified" (e.g. device was just "cuda"); leave
53-
// it off so the string round-trips and resolves to the current device.
54-
if (device.type() != kStableCPU && device.index() >= 0) {
55-
name += ":" + std::to_string(device.index());
56-
}
57-
return name;
58-
}
59-
6050
bool register_device_interface(
6151
const DeviceInterfaceKey& key,
6252
CreateDeviceInterfaceFn create_interface) {

src/torchcodec/_core/DeviceInterface.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,6 @@ FORCE_PUBLIC_VISIBILITY void validate_device_interface(
229229
const std::string& device,
230230
const std::string& variant = "default");
231231

232-
// "cpu", "cuda", "cuda:1"...
233-
FORCE_PUBLIC_VISIBILITY std::string device_to_string(
234-
const StableDevice& device);
235-
236232
TORCHCODEC_THIRD_PARTY_API std::unique_ptr<DeviceInterface>
237233
create_device_interface(
238234
const StableDevice& device,

src/torchcodec/_core/custom_ops.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ STABLE_TORCH_LIBRARY_FRAGMENT(torchcodec_ns, m) {
8888
m.def("_blocks_packet_decoder_send_eof(Tensor(a!) decoder) -> int");
8989
m.def("_blocks_packet_decoder_reset(Tensor(a!) decoder) -> ()");
9090
m.def(
91-
"_blocks_packet_decoder_receive_frame(Tensor(a!) decoder) -> (Tensor, int, float, float, str, Tensor)");
91+
"_blocks_packet_decoder_receive_frame(Tensor(a!) decoder) -> (Tensor, int, float, float, Device, Tensor)");
9292
m.def(
9393
"_blocks_create_color_converter(str device=\"cpu\", str output_dtype=\"uint8\") -> Tensor");
9494
m.def(
95-
"_blocks_convert_frame(Tensor(a!) converter, Tensor frame, str device) -> Tensor");
95+
"_blocks_convert_frame(Tensor(a!) converter, Tensor frame, Device device) -> Tensor");
9696
m.def(
9797
"_blocks_frame_metadata(Tensor frame) -> (str, str, str, int, int, int, float)");
9898
m.def(
99-
"_blocks_frame_planes(Tensor frame, str device) -> (Tensor, Tensor, Tensor, Tensor)");
99+
"_blocks_frame_planes(Tensor frame, Device device) -> (Tensor, Tensor, Tensor, Tensor)");
100100
m.def("_get_key_frame_indices(Tensor(a!) decoder) -> Tensor");
101101
m.def("get_json_metadata(Tensor(a!) decoder) -> str");
102102
m.def("get_container_json_metadata(Tensor(a!) decoder) -> str");
@@ -904,7 +904,7 @@ using OpsReceiveFrameOutput = std::tuple<
904904
int64_t,
905905
double,
906906
double,
907-
std::string,
907+
StableDevice,
908908
torch::stable::Tensor>;
909909

910910
OpsReceiveFrameOutput _blocks_packet_decoder_receive_frame(
@@ -919,7 +919,7 @@ OpsReceiveFrameOutput _blocks_packet_decoder_receive_frame(
919919
static_cast<int64_t>(status),
920920
0.0,
921921
0.0,
922-
std::string("cpu"),
922+
StableDevice(kStableCPU),
923923
torch::stable::empty({int64_t(0)}, kStableUInt8));
924924
}
925925
AVRational time_base = decoder_ptr->time_base();
@@ -929,7 +929,7 @@ OpsReceiveFrameOutput _blocks_packet_decoder_receive_frame(
929929
// ones a CUDA decoder had to decode on the CPU and upload.
930930
// TODO_API_BREAKDOWN DESIGN P1: Not sure we need to return the device at all,
931931
// the device should always match the device parameter now.
932-
std::string device = device_to_string(decoder_ptr->device());
932+
StableDevice device = decoder_ptr->device();
933933
torch::stable::Tensor storage =
934934
decoder_ptr->get_frame_storage(*av_frame).value_or(
935935
torch::stable::empty({int64_t(0)}, kStableUInt8));
@@ -956,7 +956,7 @@ torch::stable::Tensor _blocks_create_color_converter(
956956
torch::stable::Tensor _blocks_convert_frame(
957957
torch::stable::Tensor& converter,
958958
torch::stable::Tensor& frame,
959-
std::string device) {
959+
StableDevice device) {
960960
ColorConverter* converter_ptr =
961961
unwrap_tensor_to_pointer<ColorConverter>(converter);
962962
return converter_ptr->convert(
@@ -994,10 +994,10 @@ using OpsFramePlanesOutput = std::tuple<
994994

995995
OpsFramePlanesOutput _blocks_frame_planes(
996996
torch::stable::Tensor& tensor_handle,
997-
std::string device) {
997+
StableDevice device) {
998998
AVFrame* av_frame = unwrap_tensor_to_pointer<AVFrame>(tensor_handle);
999999
std::vector<torch::stable::Tensor> planes =
1000-
frame_planes(*av_frame, StableDevice(device), tensor_handle);
1000+
frame_planes(*av_frame, device, tensor_handle);
10011001

10021002
// Op schema wants a fixed number of planes, so we pad with empty tensors that
10031003
// then get removed at the Python level.

src/torchcodec/decoders/_blocks/_frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def __init__(
7070
handle: torch.Tensor,
7171
pts_seconds: float,
7272
duration_seconds: float,
73-
device: str = "cpu",
73+
device: torch.device = torch.device("cpu"),
7474
storage: torch.Tensor | None = None,
7575
):
7676
self._handle = handle

0 commit comments

Comments
 (0)