Skip to content

Commit

Permalink
Add GPU suport for wasi-nn/pytorch (#10204)
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdola-de-cartola authored Feb 7, 2025
1 parent 48fe3bb commit d1014b1
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions crates/wasi-nn/src/backend/pytorch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl BackendInner for PytorchBackend {

let graph = PytorchGraph {
module: Arc::new(Mutex::new(compiled_module)),
target,
};
let box_: Box<dyn BackendGraph> = Box::new(graph);
Ok(box_.into())
Expand All @@ -60,6 +61,7 @@ impl BackendFromDir for PytorchBackend {
)?;
let graph = PytorchGraph {
module: Arc::new(Mutex::new(compiled_module)),
target,
};
let box_: Box<dyn BackendGraph> = Box::new(graph);
Ok(box_.into())
Expand All @@ -68,6 +70,7 @@ impl BackendFromDir for PytorchBackend {

struct PytorchGraph {
module: Arc<Mutex<tch::CModule>>,
target: ExecutionTarget,
}

unsafe impl Send for PytorchGraph {}
Expand All @@ -80,7 +83,9 @@ impl BackendGraph for PytorchGraph {
inputs: Vec::new(),
output: TchTensor::new(),
id_type: None,
target: self.target,
});

Ok(box_.into())
}
}
Expand All @@ -91,6 +96,7 @@ struct PytorchExecutionContext {
inputs: Vec<Option<tch::Tensor>>,
output: tch::Tensor,
id_type: Option<Id>,
target: ExecutionTarget,
}

/// `set_input` supports multiple positional parameters with `Id::Index`, and a single named parameter with `Id::Name`.
Expand All @@ -104,7 +110,8 @@ impl BackendExecutionContext for PytorchExecutionContext {
.iter()
.map(|&dim| dim as i64)
.collect::<Vec<_>>();
let tensor = TchTensor::from_data_size(&input_tensor.data, &dimensions, kind);
let tensor = TchTensor::from_data_size(&input_tensor.data, &dimensions, kind)
.to_device(map_execution_target_to_string(self.target));
match id {
Id::Index(i) => {
// Check if id_type is already set and if it matches the current id type
Expand Down Expand Up @@ -181,9 +188,7 @@ impl BackendExecutionContext for PytorchExecutionContext {
fn map_execution_target_to_string(target: ExecutionTarget) -> Device {
match target {
ExecutionTarget::Cpu => Device::Cpu,
ExecutionTarget::Gpu => {
unimplemented!("the pytorch backend does not yet support GPU execution targets")
}
ExecutionTarget::Gpu => Device::Cuda(0),
ExecutionTarget::Tpu => {
unimplemented!("the pytorch backend does not yet support TPU execution targets")
}
Expand Down

0 comments on commit d1014b1

Please sign in to comment.