forked from tracel-ai/cubecl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream.rs
More file actions
560 lines (496 loc) · 19.8 KB
/
stream.rs
File metadata and controls
560 lines (496 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
use super::{mem_manager::WgpuMemManager, poll::WgpuPoll, timings::QueryProfiler};
use crate::{WgpuResource, controller::WgpuAllocController, schedule::ScheduleTask};
use cubecl_common::{
backtrace::BackTrace,
bytes::Bytes,
profile::{ProfileDuration, TimingMethod},
stream_id::StreamId,
};
use cubecl_core::{
CubeCount, MemoryConfiguration,
future::{self, DynFut},
server::{ExecutionError, Handle, IoError, ProfileError, ProfilingToken},
zspace::Shape,
};
use cubecl_ir::MemoryDeviceProperties;
use cubecl_runtime::{logging::ServerLogger, timestamp_profiler::TimestampProfiler};
use std::{future::Future, num::NonZero, pin::Pin, sync::Arc};
use wgpu::ComputePipeline;
#[derive(Debug)]
enum Timings {
Device(QueryProfiler),
System(TimestampProfiler),
}
#[derive(Debug)]
pub struct WgpuStream {
pub mem_manage: WgpuMemManager,
pub device: wgpu::Device,
compute_pass: Option<wgpu::ComputePass<'static>>,
timings: Timings,
tasks_count: usize,
tasks_max: usize,
queue: wgpu::Queue,
encoder: wgpu::CommandEncoder,
poll: WgpuPoll,
submission_load: SubmissionLoad,
/// Number of consecutive `write_buffer` calls without a `queue.submit()`.
/// Used to prevent wgpu staging buffer pool exhaustion during bulk writes
/// (e.g. model loading with hundreds of tensors).
pending_write_count: usize,
}
impl WgpuStream {
/// Creates a new WGPU stream.
pub fn new(
device: wgpu::Device,
queue: wgpu::Queue,
memory_properties: MemoryDeviceProperties,
memory_config: MemoryConfiguration,
timing_method: TimingMethod,
tasks_max: usize,
logger: Arc<ServerLogger>,
) -> Self {
let timings = if timing_method == TimingMethod::Device {
Timings::Device(QueryProfiler::new(&queue, &device))
} else {
if cfg!(target_family = "wasm") {
// On WASM, there's not much we can do here anymore. This should be very rare however,
// all modern GPU's support timestamp queries.
panic!(
"Cannot profile on web assembly without timestamp_query feature as it requires blocking."
);
}
Timings::System(TimestampProfiler::default())
};
let poll = WgpuPoll::new(device.clone());
#[allow(unused_mut)]
let mut mem_manage =
WgpuMemManager::new(device.clone(), memory_properties, memory_config, logger);
Self {
mem_manage,
compute_pass: None,
timings,
encoder: {
device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("CubeCL Tasks Encoder"),
})
},
device,
queue,
tasks_count: 0,
tasks_max,
poll,
submission_load: SubmissionLoad::default(),
pending_write_count: 0,
}
}
/// Enqueue a [`ScheduleTask`] on this stream.
///
/// # Arguments
///
/// * `task` - The task to execute.
pub fn enqueue_task(&mut self, task: ScheduleTask) {
match task {
ScheduleTask::Write { data, buffer } => {
// It is important to flush before writing, as the write operation is inserted
// into the QUEUE not the encoder. We want to make sure all outstanding work
// happens _before_ the write operation.
self.flush();
self.write_to_buffer(&buffer, &data);
}
ScheduleTask::Execute {
pipeline,
count,
resources,
} => {
let resources = resources.into_resources(self);
self.register_pipeline(pipeline, resources.iter(), &count);
}
}
}
/// Read multiple buffers lazily to [Bytes], potentially using pinned memory.
///
/// # Arguments
///
/// * `self` - The current stream.
/// * `descriptors` - A vector of copy descriptors specifying the source data.
///
/// # Returns
///
/// A [Result] containing a vector of [Bytes] with the copied data, or an [`IoError`] if any copy fails.
pub fn read_resources(
&mut self,
descriptors: Vec<(WgpuResource, Shape, usize)>,
) -> DynFut<Result<Vec<Bytes>, IoError>> {
self.compute_pass = None;
let mut staging_info = Vec::with_capacity(descriptors.len());
let mut callbacks = Vec::with_capacity(descriptors.len());
for (resource, shape, elem_size) in descriptors {
let size = shape.iter().product::<usize>() * elem_size;
// Copying into a buffer has to be 4 byte aligned. We can safely do so, as
// memory is 32 bytes aligned (see WgpuStorage).
let align = wgpu::COPY_BUFFER_ALIGNMENT;
let aligned_len = resource.size.div_ceil(align) * align;
let (staging, binding) = self.mem_manage.reserve_staging(aligned_len).unwrap();
self.tasks_count += 1;
self.encoder.copy_buffer_to_buffer(
&resource.buffer,
resource.offset,
&staging.buffer,
0,
aligned_len,
);
staging_info.push((staging, binding, size));
}
// Flush all commands to the queue, so GPU gets started on copying to the staging buffer.
self.flush();
for (staging, _binding, _size) in staging_info.iter() {
let (sender, receiver) = async_channel::bounded(1);
staging
.buffer
.slice(..)
.map_async(wgpu::MapMode::Read, move |v| {
// This might fail if the channel is closed (eg. the future is dropped).
// This is fine, just means results aren't needed anymore.
let _ = sender.try_send(v);
});
callbacks.push(receiver);
}
let poll = self.poll.start_polling();
Box::pin(async move {
for callback in callbacks {
callback
.recv()
.await
.expect("Unable to receive buffer slice result.")
.expect("Failed to map buffer");
}
// Can stop polling now.
core::mem::drop(poll);
let result = {
staging_info
.into_iter()
.map(|(staging, binding, size)| {
let controller =
Box::new(WgpuAllocController::init(binding, staging.buffer));
// SAFETY: The binding has initialized memory for at least `size` bytes.
unsafe { Bytes::from_controller(controller, size) }
})
.collect()
};
Ok(result)
})
}
// Bit silly but needed to make the borrow checker happy.
fn system_profiler(&mut self) -> &mut TimestampProfiler {
let Timings::System(timing) = &mut self.timings else {
panic!("Unexpected timings type");
};
timing
}
pub fn start_profile(&mut self) -> ProfilingToken {
match &mut self.timings {
Timings::System(_) => {
// Sync before profiling as well to get a cleaner measurement, we don't want to
// include any queued up work so far.
let result = future::block_on(self.sync());
let profiler = self.system_profiler();
if let Err(err) = result {
profiler.error(err.into());
}
profiler.start()
}
Timings::Device(query) => {
// Close the current compute pass so that we start a new one. This keeps
// the timestamps separated.
self.compute_pass = None;
query.start_profile()
}
}
}
pub fn end_profile(&mut self, token: ProfilingToken) -> Result<ProfileDuration, ProfileError> {
match &mut self.timings {
Timings::System(..) => {
// Nb: WASM _has_ to use device timing and will panic here if query timestamps are not supported.
let result = future::block_on(self.sync());
let profiler = self.system_profiler();
if let Err(err) = result {
profiler.error(err.into());
}
profiler.stop(token)
}
Timings::Device(..) => {
let poll = self.poll.start_polling();
self.compute_pass = None;
self.tasks_count += 1;
// Submit commands needed for profiling.
let buffer = {
let Timings::Device(timing) = &mut self.timings else {
panic!("Unexpected timings type");
};
timing.stop_profile_setup(token, &self.device, &mut self.encoder)
};
// Flush commands.
self.flush();
let Timings::Device(timing) = &mut self.timings else {
panic!("Unexpected timings type");
};
timing.stop_profile(buffer, poll)
}
}
}
pub fn sync(
&mut self,
) -> Pin<Box<dyn Future<Output = Result<(), ExecutionError>> + Send + 'static>> {
let error_scope = self.device.push_error_scope(wgpu::ErrorFilter::Internal);
self.flush();
let queue = self.queue.clone();
let error_future = error_scope.pop();
let poll = self.poll.start_polling();
Box::pin(async move {
let (sender, receiver) = async_channel::bounded::<()>(1);
queue.on_submitted_work_done(move || {
// Signal that we're done.
let _ = sender.try_send(());
core::mem::drop(poll);
});
let _ = receiver.recv().await;
if let Some(error) = error_future.await {
return Err(ExecutionError::Generic {
reason: format!("{error}"),
backtrace: BackTrace::capture(),
});
}
Ok(())
})
}
pub fn empty(&mut self, size: u64, stream_id: StreamId) -> Result<Handle, IoError> {
self.mem_manage.reserve(size, stream_id)
}
pub(crate) fn create_uniform(&mut self, data: &[u8]) -> WgpuResource {
let resource = self.mem_manage.reserve_uniform(data.len() as u64);
self.write_to_buffer(&resource, data);
resource
}
// Nb: this function submits a command to the _queue_ not to the encoder,
// so you have to be really careful about the ordering of operations here.
// Any buffer which has outstanding (not yet flushed) compute work should
// NOT be copied to.
fn write_to_buffer(&mut self, resource: &WgpuResource, data: &[u8]) {
// Copying into a buffer has to be 4 byte aligned. We can safely do so, as
// memory is also aligned (see WgpuStorage). Per the WebGPU spec, this
// just has to be a multiple of 4: https://www.w3.org/TR/webgpu/#dom-gpuqueue-writebuffer
let copy_align = wgpu::COPY_BUFFER_ALIGNMENT;
let size = resource.size.next_multiple_of(copy_align);
if size == data.len() as u64 {
// write_buffer is the recommended way to write this data, as:
// - On WebGPU, from WASM, this can save a copy to the JS memory.
// - On devices with unified memory, this could skip the staging buffer entirely.
self.queue
.write_buffer(&resource.buffer, resource.offset, data);
} else {
// For sizes not aligned we need to only write a part of the staging buffer, do this
// with `write_buffer_with`.
let mut buffer = self
.queue
.write_buffer_with(
&resource.buffer,
resource.offset,
NonZero::new(size).unwrap(),
)
.expect("Internal error: Failed to call `write_buffer_with`, this likely means no staging buffer could be allocated.");
buffer[0..data.len()].copy_from_slice(data);
}
self.pending_write_count += 1;
// Prevent wgpu staging buffer pool exhaustion during bulk writes (e.g. model
// loading with hundreds of tensors). queue.write_buffer() is async — wgpu
// copies data into an internal staging buffer, then transfers to GPU on the
// next queue.submit(). Without periodic submits, hundreds of writes accumulate
// and staging buffers get recycled before the GPU copy completes, silently
// corrupting early tensors.
// See: https://github.com/tracel-ai/cubecl/issues/1120
const MAX_PENDING_WRITES: usize = 64;
if self.pending_write_count >= MAX_PENDING_WRITES {
// End any active compute pass before submitting.
self.compute_pass = None;
// Submit the current command buffer to flush all pending write_buffer work.
let encoder = std::mem::replace(
&mut self.encoder,
self.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("CubeCL Write Flush Encoder"),
}),
);
let index = self.queue.submit([encoder.finish()]);
// Wait for the GPU to finish processing these writes before continuing.
if let Err(e) = self.device.poll(wgpu::PollType::Wait {
submission_index: Some(index),
timeout: None,
}) {
log::warn!("wgpu: write flush poll timed out ({e})");
}
self.pending_write_count = 0;
}
}
fn flush_if_needed(&mut self) {
// Flush when there are too many tasks, or when too many handles are locked.
// Locked handles should only accumulate in rare circumstances (where uniforms
// are being created but no work is submitted).
if self.tasks_count >= self.tasks_max {
self.flush();
}
}
pub fn flush(&mut self) {
if self.tasks_count == 0 {
return;
}
// End the current compute pass.
self.compute_pass = None;
// Submit the pending actions to the queue. This will _first_ submit the
// pending uniforms copy operations, then the main tasks.
let tasks_encoder = {
std::mem::replace(&mut self.encoder, {
self.device
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("CubeCL Tasks Encoder"),
})
})
};
// This will _first_ fire off all pending write_buffer work.
let index = self.queue.submit([tasks_encoder.finish()]);
self.submission_load
.regulate(&self.device, self.tasks_count, index);
// Cleanup allocations and deallocations.
self.mem_manage.memory_cleanup(false);
self.mem_manage.release_uniforms();
self.tasks_count = 0;
self.pending_write_count = 0;
}
fn register_pipeline<'a>(
&mut self,
pipeline: Arc<ComputePipeline>,
resources: impl Iterator<Item = &'a WgpuResource>,
dispatch: &CubeCount,
) {
let entries = resources
.enumerate()
.map(|(i, r)| wgpu::BindGroupEntry {
binding: i as u32,
resource: r.as_wgpu_bind_resource(),
})
.collect::<Vec<_>>();
// Start a new compute pass if needed. The forget_lifetime allows
// to store this with a 'static lifetime, but the compute pass must
// be dropped before the encoder. This isn't unsafe - it's still checked at runtime.
let pass = self.compute_pass.get_or_insert_with(|| {
let writes = if let Timings::Device(query_time) = &mut self.timings {
query_time
.register_profile_device(&self.device)
.map(|query_set| wgpu::ComputePassTimestampWrites {
query_set,
beginning_of_pass_write_index: Some(0),
end_of_pass_write_index: Some(1),
})
} else {
None
};
self.encoder
.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: None,
timestamp_writes: writes,
})
.forget_lifetime()
});
self.tasks_count += 1;
let group_layout = pipeline.get_bind_group_layout(0);
let bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: None,
layout: &group_layout,
entries: &entries,
});
pass.set_pipeline(&pipeline);
pass.set_bind_group(0, &bind_group, &[]);
match dispatch.clone() {
CubeCount::Static(x, y, z) => {
pass.dispatch_workgroups(x, y, z);
}
CubeCount::Dynamic(binding) => {
let res = self.mem_manage.get_resource(binding).unwrap();
pass.dispatch_workgroups_indirect(&res.buffer, res.offset);
}
}
self.flush_if_needed();
}
}
#[cfg(not(target_family = "wasm"))]
mod __submission_load {
#[derive(Default, Debug)]
pub enum SubmissionLoad {
Init {
last_index: wgpu::SubmissionIndex,
tasks_count_submitted: usize,
},
#[default]
Empty,
}
impl SubmissionLoad {
pub fn regulate(
&mut self,
device: &wgpu::Device,
tasks_count: usize,
mut index: wgpu::SubmissionIndex,
) {
match self {
SubmissionLoad::Init {
last_index,
tasks_count_submitted,
} => {
*tasks_count_submitted += tasks_count;
// Enough to keep the GPU busy.
//
// - Too much can hang the GPU and create slowdown.
// - Too little and GPU utilization is really bad.
//
// TODO: Could be smarter and dynamic based on stats.
const MAX_TOTAL_TASKS: usize = 512;
if *tasks_count_submitted >= MAX_TOTAL_TASKS {
core::mem::swap(last_index, &mut index);
if let Err(e) = device.poll(wgpu::PollType::Wait {
submission_index: Some(index),
timeout: None,
}) {
log::warn!(
"wgpu: requested wait timed out before the submission was completed during sync. ({e})"
)
}
*tasks_count_submitted = 0;
}
}
SubmissionLoad::Empty => {
*self = Self::Init {
last_index: index,
tasks_count_submitted: 0,
}
}
}
}
}
}
#[cfg(target_family = "wasm")]
mod __submission_load_wasm {
#[derive(Default, Debug)]
pub struct SubmissionLoad;
impl SubmissionLoad {
pub fn regulate(
&mut self,
_device: &wgpu::Device,
_tasks_count: usize,
_index: wgpu::SubmissionIndex,
) {
// Nothing to do.
}
}
}
#[cfg(not(target_family = "wasm"))]
use __submission_load::*;
#[cfg(target_family = "wasm")]
use __submission_load_wasm::*;