Skip to content

Commit aa10ed1

Browse files
committed
Avoid depending on numpy for bytes conversion
1 parent 78276a2 commit aa10ed1

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

src/torchcodec/_core/ops.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7+
import ctypes
78
import io
89
import json
910
import warnings
@@ -173,15 +174,18 @@ def encode_audio_to_file_like(
173174
"""
174175
assert _pybind_ops is not None
175176

176-
# Convert tensor to raw bytes and shape info for pybind
177+
# Enforce float32 dtype requirement
178+
if samples.dtype != torch.float32:
179+
raise ValueError(f"samples must have dtype torch.float32, got {samples.dtype}")
180+
177181
samples_contiguous = samples.contiguous()
178-
samples_numpy = samples_contiguous.detach().cpu().numpy()
179-
samples_bytes = samples_numpy.tobytes()
180-
samples_shape = tuple(samples_contiguous.shape)
182+
183+
data_ptr = samples_contiguous.data_ptr()
184+
shape = list(samples_contiguous.shape)
181185

182186
_pybind_ops.encode_audio_to_file_like(
183-
samples_bytes,
184-
samples_shape,
187+
data_ptr,
188+
shape,
185189
sample_rate,
186190
format,
187191
file_like,

src/torchcodec/_core/pybind_ops.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,20 @@ int64_t create_from_file_like(
4343
}
4444

4545
int64_t encode_audio_to_file_like(
46-
py::bytes samples_data,
47-
py::tuple samples_shape,
46+
uintptr_t data_ptr,
47+
py::list shape,
4848
int64_t sample_rate,
4949
const std::string& format,
5050
py::object file_like,
5151
std::optional<int64_t> bit_rate = std::nullopt,
5252
std::optional<int64_t> num_channels = std::nullopt) {
53-
// Convert Python data back to tensor
54-
auto shape_vec = samples_shape.cast<std::vector<int64_t>>();
55-
std::string samples_str = samples_data;
53+
// Convert Python list to vector
54+
auto shape_vec = shape.cast<std::vector<int64_t>>();
5655

57-
// Create tensor from raw data
56+
// Create tensor from existing data pointer (enforcing float32)
5857
auto tensor_options = torch::TensorOptions().dtype(torch::kFloat32);
59-
auto samples =
60-
torch::from_blob(
61-
const_cast<void*>(static_cast<const void*>(samples_str.data())),
62-
shape_vec,
63-
tensor_options)
64-
.clone(); // Clone to ensure memory ownership
58+
auto samples = torch::from_blob(
59+
reinterpret_cast<void*>(data_ptr), shape_vec, tensor_options);
6560

6661
AudioStreamOptions audioStreamOptions;
6762
audioStreamOptions.bitRate = bit_rate;
@@ -86,8 +81,8 @@ PYBIND11_MODULE(decoder_core_pybind_ops, m) {
8681
m.def(
8782
"encode_audio_to_file_like",
8883
&encode_audio_to_file_like,
89-
"samples_data"_a,
90-
"samples_shape"_a,
84+
"data_ptr"_a,
85+
"shape"_a,
9186
"sample_rate"_a,
9287
"format"_a,
9388
"file_like"_a,

0 commit comments

Comments
 (0)