Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
587 changes: 370 additions & 217 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ tower-http = { version = "0.5", features = ["cors"] }
tokio-stream = "0.1"

# Python interop
pyo3 = { version = "0.23", features = ["auto-initialize"] }
pyo3 = { version = "0.29", features = ["auto-initialize"] }

# Utilities
uuid = { version = "1", features = ["v4"] }
zstd = "0.13"
flate2 = "1"
tar = "0.4"
zip = "2"
zip = "9.0.5"
chrono = { version = "0.4", features = ["serde"] }
dirs = "6"
tempfile = "3"
Expand Down
44 changes: 21 additions & 23 deletions crates/inference/src/backend/pytorch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ fn init_worker_from_envelope_blocking(
request_id: &str,
envelope_json: String,
) -> Result<(), BackendError> {
Python::with_gil(|py| {
Python::attach(|py| {
pytorch_worker::ensure_worker_initialised(py).map_err(|e| {
PyTorchBackend::init_worker_failure_from_message(
request_id,
Expand Down Expand Up @@ -473,7 +473,7 @@ fn shutdown_worker_from_envelope_blocking(
request_id: &str,
envelope_json: String,
) -> Result<(), BackendError> {
Python::with_gil(|py| {
Python::attach(|py| {
pytorch_worker::ensure_worker_initialised(py).map_err(|e| {
PyTorchBackend::shutdown_worker_failure_from_message(
request_id,
Expand Down Expand Up @@ -910,7 +910,7 @@ pub async fn active_loaded_model_info() -> Result<LoadedModelInfo, BackendError>
))
})?;
tokio::task::spawn_blocking(move || {
Python::with_gil(|py| -> Result<LoadedModelInfo, BackendError> {
Python::attach(|py| -> Result<LoadedModelInfo, BackendError> {
let worker = pytorch_worker::worker_module(py).map_err(|e| {
kv_worker_failure_from_message(
&request_id,
Expand Down Expand Up @@ -953,7 +953,7 @@ pub async fn save_live_kv_snapshot(path: &Path) -> Result<PyTorchLiveKvInfo, Bac
))
})?;
tokio::task::spawn_blocking(move || {
Python::with_gil(|py| -> Result<PyTorchLiveKvInfo, BackendError> {
Python::attach(|py| -> Result<PyTorchLiveKvInfo, BackendError> {
let worker = pytorch_worker::worker_module(py).map_err(|e| {
kv_worker_failure_from_message(
&request_id,
Expand Down Expand Up @@ -997,7 +997,7 @@ pub async fn restore_live_kv_snapshot(path: &Path) -> Result<PyTorchLiveKvInfo,
))
})?;
tokio::task::spawn_blocking(move || {
Python::with_gil(|py| -> Result<PyTorchLiveKvInfo, BackendError> {
Python::attach(|py| -> Result<PyTorchLiveKvInfo, BackendError> {
let worker = pytorch_worker::worker_module(py).map_err(|e| {
kv_worker_failure_from_message(
&request_id,
Expand Down Expand Up @@ -1039,7 +1039,7 @@ pub async fn clear_live_kv_snapshot() -> Result<(), BackendError> {
))
})?;
tokio::task::spawn_blocking(move || {
Python::with_gil(|py| -> Result<(), BackendError> {
Python::attach(|py| -> Result<(), BackendError> {
let worker = pytorch_worker::worker_module(py).map_err(|e| {
kv_worker_failure_from_message(
&request_id,
Expand Down Expand Up @@ -1394,7 +1394,7 @@ impl PyTorchBackend {
})?;

tokio::task::spawn_blocking(move || {
Python::with_gil(|py| -> Result<AudioTranscriptionResult, BackendError> {
Python::attach(|py| -> Result<AudioTranscriptionResult, BackendError> {
let worker = match &isolated {
Some(worker) => worker.module(py),
None => pytorch_worker::worker_module(py),
Expand Down Expand Up @@ -1451,7 +1451,7 @@ impl PyTorchBackend {
let envelope = shutdown_worker_envelope_json(&request_id)?;
self.selected_audio_residency_possible = true;
tokio::task::spawn_blocking(move || {
Python::with_gil(|py| {
Python::attach(|py| {
let module = worker.module(py).map_err(|error| {
Self::shutdown_worker_failure_from_message(&request_id, error.to_string())
})?;
Expand Down Expand Up @@ -1615,7 +1615,7 @@ impl PyTorchBackend {
}

let info = tokio::task::spawn_blocking(move || {
Python::with_gil(|py| -> Result<LoadedModelInfo, BackendError> {
Python::attach(|py| -> Result<LoadedModelInfo, BackendError> {
let worker = match &isolated {
Some(worker) => worker.module(py),
None => pytorch_worker::worker_module(py),
Expand Down Expand Up @@ -1815,7 +1815,7 @@ impl PyTorchBackend {
});
}

if let Ok(dict) = token_obj.downcast::<pyo3::types::PyDict>() {
if let Ok(dict) = token_obj.cast::<pyo3::types::PyDict>() {
let text_value = dict.get_item("text").map_err(|error| {
Self::stream_worker_failure_from_message(
request_id,
Expand Down Expand Up @@ -1869,14 +1869,12 @@ impl PyTorchBackend {
request_id: &str,
usage_obj: &Bound<'_, PyAny>,
) -> Result<Option<InferenceUsage>, BackendError> {
let usage_dict = usage_obj
.downcast::<pyo3::types::PyDict>()
.map_err(|error| {
Self::stream_worker_failure_from_message(
request_id,
format!("Token usage extraction failed: {}", error),
)
})?;
let usage_dict = usage_obj.cast::<pyo3::types::PyDict>().map_err(|error| {
Self::stream_worker_failure_from_message(
request_id,
format!("Token usage extraction failed: {}", error),
)
})?;
let usage = InferenceUsage {
prompt_tokens: Self::stream_usage_u32_field(request_id, usage_dict, "prompt_tokens")?,
completion_tokens: Self::stream_usage_u32_field(
Expand Down Expand Up @@ -2303,7 +2301,7 @@ impl PyTorchBackend {
request_id: &str,
envelope_json: String,
) -> Result<(), BackendError> {
Python::with_gil(|py| -> Result<(), BackendError> {
Python::attach(|py| -> Result<(), BackendError> {
let worker = pytorch_worker::worker_module(py).map_err(|e| {
Self::unload_worker_failure_from_message(
request_id,
Expand Down Expand Up @@ -2835,7 +2833,7 @@ impl PyTorchBackend {

let mut stream = self.text_jobs.spawn(move |mut producer| {
producer.check()?;
let text = Python::with_gil(|py| -> Result<String, BackendError> {
let text = Python::attach(|py| -> Result<String, BackendError> {
let worker = pytorch_worker::worker_module(py).map_err(|e| {
Self::generate_text_worker_failure_from_message(
&request_id,
Expand Down Expand Up @@ -2945,7 +2943,7 @@ impl PyTorchBackend {

let stream = self.text_jobs.spawn(move |mut producer| {
producer.check()?;
Python::with_gil(|py| -> Result<(), BackendError> {
Python::attach(|py| -> Result<(), BackendError> {
let failure = |error: PyErr| {
Self::stream_worker_failure_from_message(&request_id, error.to_string())
};
Expand Down Expand Up @@ -3043,7 +3041,7 @@ impl InferenceBackend for PyTorchBackend {

// Log the transformers version for diagnostics
let tf_version = tokio::task::spawn_blocking(|| {
Python::with_gil(|py| -> String {
Python::attach(|py| -> String {
py.import("transformers")
.and_then(|m| m.getattr("__version__"))
.and_then(|v| v.extract::<String>())
Expand Down Expand Up @@ -3481,7 +3479,7 @@ impl InferenceBackend for PyTorchBackend {
let truncate_result = tokio::task::spawn_blocking({
let request_id = request_id.clone();
move || {
Python::with_gil(|py| -> Result<PyTorchTruncateKvCacheResult, BackendError> {
Python::attach(|py| -> Result<PyTorchTruncateKvCacheResult, BackendError> {
let worker = pytorch_worker::worker_module(py).map_err(|e| {
kv_truncate_worker_failure_from_message(
&request_id,
Expand Down
8 changes: 4 additions & 4 deletions crates/inference/src/backend/pytorch_cuda_inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl PyTorchBackend {
/// that API's lazy-initialization path. This flag is not proof of custody.
pub async fn observe_cuda_inventory() -> PyTorchCudaInventory {
tokio::task::spawn_blocking(|| {
Python::with_gil(|py| {
Python::attach(|py| {
let Ok(torch) = py.import("torch") else {
return unavailable(PyTorchCudaInventoryUnavailable::PythonProbeFailed);
};
Expand Down Expand Up @@ -208,7 +208,7 @@ cuda = Cuda()
}
#[test]
fn cuda_inventory_observes_owner_ordinals_uuid_and_known_zero_without_promoting_capacity() {
Python::with_gil(|py| {
Python::attach(|py| {
let devices = facts(controlled(py, ""));
assert_eq!(devices[0].device_id.as_str(), "cuda:0");
assert_eq!(devices[1].device_id.as_str(), "cuda:1");
Expand All @@ -227,7 +227,7 @@ cuda = Cuda()
}
#[test]
fn cuda_inventory_missing_malformed_or_duplicate_identity_stays_unavailable() {
Python::with_gil(|py| {
Python::attach(|py| {
for settings in [
"cuda.uuids[0] = []",
"cuda.uuids[0] = [0]*16",
Expand All @@ -251,7 +251,7 @@ cuda = Cuda()
}
#[test]
fn cuda_inventory_rejects_probe_errors_partial_enumeration_hip_and_unbounded_counts() {
Python::with_gil(|py| {
Python::attach(|py| {
for (settings, reason) in [
(
"cuda.available = False",
Expand Down
4 changes: 2 additions & 2 deletions crates/inference/src/backend/pytorch_image_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl PyTorchBackend {
reject_cancelled_image_generation(&context)?;

tokio::task::spawn_blocking(move || {
Python::with_gil(|py| -> Result<ImageGenerationResult, BackendError> {
Python::attach(|py| -> Result<ImageGenerationResult, BackendError> {
let worker = pytorch_worker::worker_module(py).map_err(|error| {
image_worker_failure_from_message(
&request_id,
Expand Down Expand Up @@ -107,7 +107,7 @@ impl PyTorchBackend {

let batch_execution_id = request.batch_execution_id.clone();
tokio::task::spawn_blocking(move || {
Python::with_gil(
Python::attach(
|py| -> Result<ImageGenerationBatchExecutionResponse, BackendError> {
let worker = pytorch_worker::worker_module(py).map_err(|error| {
image_worker_failure_from_message(
Expand Down
Loading
Loading