Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: CUDA segfault when slice ptr is dropped before executed #64

Merged
merged 2 commits into from
Aug 12, 2024
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
3 changes: 1 addition & 2 deletions crates/cubecl-cuda/src/compute/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ impl<MM: MemoryManagement<CudaStorage>> CudaContext<MM> {
unsafe {
cudarc::driver::result::stream::synchronize(self.stream).unwrap();
};
self.memory_management.storage().flush();
}

fn compile_kernel(
Expand Down Expand Up @@ -279,8 +280,6 @@ impl<MM: MemoryManagement<CudaStorage>> CudaContext<MM> {
)
.unwrap();
};

self.memory_management.storage().flush(resources)
}
}

Expand Down
22 changes: 3 additions & 19 deletions crates/cubecl-cuda/src/compute/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub struct CudaStorage {
deallocations: Vec<StorageId>,
stream: cudarc::driver::sys::CUstream,
activate_slices: HashMap<ActiveResource, cudarc::driver::sys::CUdeviceptr>,
activate_slices_count: HashMap<ActiveResource, usize>,
}

#[derive(new, Debug, Hash, PartialEq, Eq, Clone)]
Expand All @@ -34,7 +33,6 @@ impl CudaStorage {
deallocations: Vec::new(),
stream,
activate_slices: HashMap::new(),
activate_slices_count: HashMap::new(),
}
}

Expand All @@ -49,17 +47,8 @@ impl CudaStorage {
}
}

pub fn flush(&mut self, resources: Vec<CudaResource>) {
for resource in resources {
let key = ActiveResource::new(resource.ptr, resource.kind);
if let Some(count) = self.activate_slices_count.remove(&key) {
if count == 1 {
self.activate_slices.remove(&key);
} else {
self.activate_slices_count.insert(key, count - 1);
}
}
}
pub fn flush(&mut self) {
self.activate_slices.clear();
}
}

Expand Down Expand Up @@ -129,12 +118,7 @@ impl ComputeStorage for CudaStorage {
let kind = CudaResourceKind::Slice { size, offset };
let key = ActiveResource::new(ptr, kind.clone());

if let Some(count) = self.activate_slices_count.get_mut(&key) {
*count += 1;
} else {
self.activate_slices.insert(key.clone(), ptr);
self.activate_slices_count.insert(key.clone(), 1);
}
self.activate_slices.insert(key.clone(), ptr);

// The ptr needs to stay alive until we send the task to the server.
let ptr = self.activate_slices.get(&key).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/cubecl-cuda/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Runtime for CudaRuntime {
)
.unwrap();
let storage = CudaStorage::new(stream);
let options = DynamicMemoryManagementOptions::preset(2048 * 1024 * 1024, 32);
let options = DynamicMemoryManagementOptions::preset(2048 + 512 * 1024 * 1024, 32);
let memory_management = DynamicMemoryManagement::new(storage, options);
CudaContext::new(memory_management, stream, ctx)
}
Expand Down
Loading