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 wgpu memory corruption #83

Merged
merged 1 commit into from
Aug 26, 2024
Merged
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
43 changes: 32 additions & 11 deletions crates/cubecl-wgpu/src/compute/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cubecl_common::{reader::Reader, sync_type::SyncType};
use cubecl_core::{compute::DebugInformation, prelude::*, server::Handle, FeatureSet, KernelId};
use cubecl_runtime::{
debug::DebugLogger,
memory_management::MemoryManagement,
memory_management::{MemoryHandle, MemoryManagement},
server::{self, ComputeServer},
storage::{ComputeStorage, StorageId},
ExecutionMode,
Expand All @@ -24,6 +24,7 @@ pub struct WgpuServer<MM: MemoryManagement<WgpuStorage>> {
current_pass: Option<ComputePass<'static>>,
tasks_count: usize,
compute_storage_used: Vec<StorageId>,
copy_handles_used: Vec<(StorageId, u32)>,
pipelines: HashMap<KernelId, Arc<ComputePipeline>>,
tasks_max: usize,
logger: DebugLogger,
Expand Down Expand Up @@ -54,6 +55,7 @@ where
current_pass: None,
tasks_count: 0,
compute_storage_used: Vec::new(),
copy_handles_used: Vec::new(),
pipelines: HashMap::new(),
tasks_max,
logger: DebugLogger::new(),
Expand Down Expand Up @@ -188,8 +190,7 @@ where
&mut self,
binding: server::Binding<Self>,
) -> <Self::Storage as cubecl_runtime::storage::ComputeStorage>::Resource {
let handle = self.memory_management.get(binding.memory);
self.memory_management.storage().get(&handle)
self.memory_management.get_resource(binding.memory)
}

/// When we create a new handle from existing data, we use custom allocations so that we don't
Expand All @@ -198,15 +199,24 @@ where
/// This is important, otherwise the compute passes are going to be too small and we won't be able to
/// fully utilize the GPU.
fn create(&mut self, data: &[u8]) -> server::Handle<Self> {
// Reserve memory on some storage we haven't yet used this command queue.
let memory = self
.memory_management
.reserve(data.len(), &self.compute_storage_used);

let handle = Handle::new(memory);
// Reserve memory on some storage we haven't yet used this command queue for compute
// or copying.
let total_handles = self
.compute_storage_used
.iter()
.copied()
.chain(self.copy_handles_used.iter().map(|x| x.0))
.collect::<Vec<_>>();
let memory = self.memory_management.reserve(data.len(), &total_handles);

if let Some(len) = NonZero::new(data.len() as u64) {
let resource = self.get_resource(handle.clone().binding());
let resource_handle = self.memory_management.get(memory.clone().binding());

// Dont re-use this handle for writing until the queue is flushed. All writes
// would happen at the start of the submission.
self.copy_handles_used.push((resource_handle.id, 0));

let resource = self.memory_management.storage().get(&resource_handle);

// Write to the staging buffer. Next queue submission this will copy the data to the GPU.
self.queue
Expand All @@ -215,7 +225,7 @@ where
.copy_from_slice(data);
}

handle
Handle::new(memory)
}

fn empty(&mut self, size: usize) -> server::Handle<Self> {
Expand Down Expand Up @@ -309,6 +319,17 @@ where
self.tasks_count = 0;
self.compute_storage_used.clear();

self.copy_handles_used.retain_mut(|x| {
// For some unknown reason, we have to make sure
// a buffer isn't used more than once not just in the current
// submission, but also in the next one.
//
// This really needs a better explanation of why this is, or
// some investigation, maybe it's a wgpu bug.
x.1 += 1;
x.1 < 2
});

if sync_type == SyncType::Wait {
self.device.poll(wgpu::Maintain::Wait);
}
Expand Down
Loading