web-rwkv 0.10.20

An implementation of the RWKV language model in pure WebGPU.
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
use std::{borrow::Cow, collections::BTreeMap, sync::Arc};

use futures::Future;
use thiserror::Error;
use wasm_bindgen::prelude::wasm_bindgen;
use web_rwkv_derive::{Deref, DerefMut};
use wgpu::{
    util::{BufferInitDescriptor, DeviceExt},
    Adapter, BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout,
    BindGroupLayoutDescriptor, BindGroupLayoutEntry, Buffer, BufferDescriptor, BufferUsages,
    ComputePipeline, ComputePipelineDescriptor, Device, DeviceDescriptor, ExperimentalFeatures,
    Features, Instance, Limits, MemoryHints, PipelineLayoutDescriptor, PowerPreference, Queue,
    RequestAdapterOptions, ShaderModuleDescriptor, Trace,
};

use crate::tensor::{
    cache::{ResourceCache, SharedResourceCache},
    shape::{IntoBytes, Shape},
    ResourceKey, TensorResource, View,
};

pub trait InstanceExt {
    fn adapter(
        &self,
        power_preference: PowerPreference,
    ) -> impl Future<Output = Result<Adapter, ContextError>>;
}

impl InstanceExt for Instance {
    async fn adapter(&self, power_preference: PowerPreference) -> Result<Adapter, ContextError> {
        self.request_adapter(&RequestAdapterOptions {
            power_preference,
            force_fallback_adapter: false,
            compatible_surface: None,
        })
        .await
        .or(Err(ContextError::RequestAdapterFailed))
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ContextId;

#[cfg(not(target_arch = "wasm32"))]
pub struct ContextEvent {
    pub buffer: Arc<Buffer>,
    pub sender: flume::Sender<Box<[u8]>>,
}

#[derive(Debug, Clone)]
pub struct Context {
    pub id: uid::Id<ContextId>,
    pub adapter: Adapter,
    pub device: Device,
    pub queue: Queue,

    pipelines: SharedResourceCache<PipelineKey, CachedPipeline>,
    shapes: ResourceCache<View, Buffer>,
    buffers: ResourceCache<BufferKey, Buffer>,
    bindings: SharedResourceCache<BindGroupKey, BindGroup>,

    #[cfg(not(target_arch = "wasm32"))]
    event: flume::Sender<ContextEvent>,
}

#[cfg(not(target_arch = "wasm32"))]
impl Drop for Context {
    fn drop(&mut self) {
        if self.event.sender_count() <= 1 {
            self.clear_buffers();
            self.queue.submit(None);
            _ = self.device.poll(wgpu::PollType::Wait {
                submission_index: None,
                timeout: None,
            });
        }
    }
}

impl PartialEq for Context {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

pub struct ContextBuilder {
    pub adapter: Adapter,
    pub features: Features,
    pub limits: Limits,
}

#[wasm_bindgen]
#[derive(Debug, Error)]
pub enum ContextError {
    #[error("failed to request adaptor")]
    RequestAdapterFailed,
    #[error("failed to request device")]
    RequestDeviceFailed,
}

impl ContextBuilder {
    pub fn new(adapter: Adapter) -> Self {
        let features = Features::empty();
        #[cfg(feature = "subgroup-ops")]
        let features = features | Features::SUBGROUP;
        Self {
            adapter,
            features,
            limits: Default::default(),
        }
    }

    pub async fn build(self) -> Result<Context, ContextError> {
        let Self {
            adapter,
            features,
            limits,
        } = self;

        let (device, queue) = adapter
            .request_device(&DeviceDescriptor {
                label: None,
                required_features: features,
                required_limits: limits,
                memory_hints: MemoryHints::Performance,
                trace: Trace::Off,
                experimental_features: ExperimentalFeatures::disabled(),
            })
            .await
            .map_err(|_| ContextError::RequestDeviceFailed)?;

        #[cfg(not(target_arch = "wasm32"))]
        let (event, receiver) = flume::unbounded();

        let context = Context {
            id: uid::Id::new(),
            adapter,
            device,
            queue,
            pipelines: Default::default(),
            shapes: Default::default(),
            buffers: ResourceCache::new(4),
            bindings: SharedResourceCache::new(64),
            #[cfg(not(target_arch = "wasm32"))]
            event,
        };

        // start a thread for reading back buffers
        #[cfg(not(target_arch = "wasm32"))]
        {
            let id = context.id;
            let device = context.device.clone();
            std::thread::spawn(move || {
                while let Ok(ContextEvent { buffer, sender }) = receiver.recv() {
                    #[cfg(feature = "trace")]
                    let _span = tracing::trace_span!("device").entered();
                    let data = read_back_buffer(&device, &buffer);
                    let _ = sender.send(data);
                }
                log::info!("context dropped: {id}");
            });
        }

        Ok(context)
    }

    pub fn limits(mut self, limits: Limits) -> Self {
        self.limits = limits;
        self
    }

    pub fn update_limits(mut self, f: impl FnOnce(&mut Limits)) -> Self {
        f(&mut self.limits);
        self
    }

    pub fn features(mut self, features: Features) -> Self {
        self.features = features;
        self
    }

    pub fn update_features(mut self, f: impl FnOnce(&mut Features)) -> Self {
        f(&mut self.features);
        self
    }
}

/// A container of macro definitions in shader.
#[derive(Debug, Default, Clone, Deref, DerefMut, PartialEq, Eq, Hash)]
pub struct Macros(BTreeMap<String, String>);

impl Macros {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn compile(self) -> Vec<(String, String)> {
        self.0.into_iter().collect()
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PipelineKey {
    name: String,
    entry_point: String,
    macros: Vec<(String, String)>,
}

impl PipelineKey {
    pub fn new(name: impl AsRef<str>, entry_point: impl AsRef<str>, macros: Macros) -> Self {
        let name = name.as_ref().into();
        let entry_point = entry_point.as_ref().into();
        let macros = macros.compile();
        Self {
            name,
            entry_point,
            macros,
        }
    }
}

#[derive(Debug, Clone)]
pub struct CachedPipeline {
    pub pipeline: ComputePipeline,
    pub layout: BindGroupLayout,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct BufferKey {
    size: usize,
    usage: BufferUsages,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct BindGroupKey {
    pipeline: PipelineKey,
    bindings: Vec<(u32, ResourceKey)>,
}

pub struct BindGroupBuilder<'a, 'b> {
    context: &'b Context,
    layout: &'b BindGroupLayout,
    key: BindGroupKey,
    entries: Vec<BindGroupEntry<'a>>,
}

impl<'a, 'b> BindGroupBuilder<'a, 'b> {
    pub fn new(key: &PipelineKey, context: &'b Context, layout: &'b BindGroupLayout) -> Self {
        Self {
            context,
            layout,
            key: BindGroupKey {
                pipeline: key.clone(),
                bindings: vec![],
            },
            entries: vec![],
        }
    }

    /// Mark a resource as being touched.
    /// How resources are touched determines whether the bind group can be found in cache.
    pub fn touch(mut self, binding: u32, tensor: &'a impl TensorResource) -> Self {
        let key = tensor.resource_key();
        self.key.bindings.push((binding, key));
        self
    }

    /// Insert an entry into the bind group.
    pub fn bind(mut self, binding: u32, tensor: &'a impl TensorResource) -> Self {
        let resource = tensor.binding();
        self.entries.push(BindGroupEntry { binding, resource });
        self.touch(binding, tensor)
    }

    /// Insert an entry into the bind group.
    pub fn bind_meta(mut self, binding: u32, tensor: &'a impl TensorResource) -> Self {
        let resource = tensor.meta_binding();
        self.entries.push(BindGroupEntry { binding, resource });
        // self.touch(binding, tensor)
        self
    }

    pub fn build(self) -> Arc<BindGroup> {
        let name = self.key.pipeline.name.clone();
        self.context.bindings.checkout(self.key, || {
            self.context.device.create_bind_group(&BindGroupDescriptor {
                label: Some(&name),
                layout: self.layout,
                entries: &self.entries,
            })
        })
    }
}

impl Eq for Context {}

impl Context {
    pub fn checkout_pipeline(
        &self,
        key: &PipelineKey,
        source: impl AsRef<str>,
        entries: &[BindGroupLayoutEntry],
    ) -> Arc<CachedPipeline> {
        self.pipelines.checkout(key.clone(), || {
            use gpp::{process_str, Context};
            let mut context = Context::new();
            context.macros = key.macros.iter().cloned().collect();

            let shader = process_str(source.as_ref(), &mut context).unwrap();
            let module = &self.device.create_shader_module(ShaderModuleDescriptor {
                label: Some(&key.name),
                source: wgpu::ShaderSource::Wgsl(Cow::from(shader)),
            });

            let layout = self
                .device
                .create_bind_group_layout(&BindGroupLayoutDescriptor {
                    label: Some(&key.name),
                    entries,
                });
            let pipeline_layout = self
                .device
                .create_pipeline_layout(&PipelineLayoutDescriptor {
                    label: Some(&key.name),
                    bind_group_layouts: &[Some(&layout)],
                    immediate_size: 0,
                });

            let pipeline = self
                .device
                .create_compute_pipeline(&ComputePipelineDescriptor {
                    label: Some(&key.name),
                    layout: Some(&pipeline_layout),
                    module,
                    entry_point: Some(&key.entry_point),
                    compilation_options: Default::default(),
                    cache: None,
                });
            CachedPipeline { pipeline, layout }
        })
    }

    pub(crate) fn checkout_shape_uniform(&self, shape: Shape) -> Arc<Buffer> {
        let view = View {
            shape,
            stride: shape,
            offset: Shape::new(0, 0, 0, 0),
        };
        let desc = BufferInitDescriptor {
            label: None,
            contents: &view.into_bytes(),
            usage: BufferUsages::UNIFORM,
        };
        self.shapes
            .checkout(view, || self.device.create_buffer_init(&desc))
    }

    pub(crate) fn checkout_view_uniform(&self, view: View) -> Arc<Buffer> {
        let desc = BufferInitDescriptor {
            label: None,
            contents: &view.into_bytes(),
            usage: BufferUsages::UNIFORM,
        };
        self.shapes
            .checkout(view, || self.device.create_buffer_init(&desc))
    }

    pub(crate) fn checkout_buffer_init(&self, contents: &[u8], usage: BufferUsages) -> Arc<Buffer> {
        let size = std::mem::size_of_val(contents);
        let _key = BufferKey { size, usage };
        let desc = BufferInitDescriptor {
            label: None,
            contents,
            usage,
        };
        // self.buffer_cache.checkout(
        //     key,
        //     || self.device.create_buffer_init(&desc),
        //     |buffer| self.queue.write_buffer(buffer, 0, contents),
        // )
        self.device.create_buffer_init(&desc).into()
    }

    pub(crate) fn checkout_buffer(&self, size: usize, usage: BufferUsages) -> Arc<Buffer> {
        let key = BufferKey { size, usage };
        let desc = BufferDescriptor {
            label: None,
            size: size as u64,
            usage,
            mapped_at_creation: false,
        };
        self.buffers
            .checkout(key, || self.device.create_buffer(&desc))
    }

    // pub(crate) fn checkout_buffer_uncached(&self, size: usize, usage: BufferUsages) -> Arc<Buffer> {
    //     self.device
    //         .create_buffer(&BufferDescriptor {
    //             label: None,
    //             size: size as u64,
    //             usage,
    //             mapped_at_creation: false,
    //         })
    //         .into()
    // }

    /// Maintain resource caches.
    #[inline]
    pub fn maintain(&self) {
        self.pipelines.maintain();
        self.shapes.maintain();
        self.buffers.maintain();
        self.bindings.maintain();
    }

    /// Clear resource caches.
    #[inline]
    pub fn clear_buffers(&self) {
        self.shapes.clear();
        self.buffers.clear();
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) fn event(&self) -> flume::Sender<ContextEvent> {
        self.event.clone()
    }

    #[cfg(feature = "subgroup-ops")]
    pub fn min_subgroup_size(&self) -> u32 {
        self.adapter.get_info().subgroup_min_size
    }

    #[cfg(feature = "subgroup-ops")]
    pub fn max_subgroup_size(&self) -> u32 {
        self.adapter.get_info().subgroup_max_size
    }
}

#[cfg(not(target_arch = "wasm32"))]
fn read_back_buffer(device: &Device, buffer: &Buffer) -> Box<[u8]> {
    assert!(buffer.usage().contains(BufferUsages::MAP_READ));

    let (sender, receiver) = flume::bounded(1);
    let slice = buffer.slice(..);
    slice.map_async(wgpu::MapMode::Read, move |v| sender.send(v).unwrap());

    _ = device.poll(wgpu::PollType::Wait {
        submission_index: None,
        timeout: None,
    });
    receiver
        .recv()
        .expect("failed to receive read back buffer")
        .expect("failed to map buffer");

    let data = {
        let map = slice.get_mapped_range();
        let len = map.len();
        let size = std::mem::size_of::<u32>();
        let data = vec![0u32; len.div_ceil(size)].into_boxed_slice();
        unsafe {
            let data = Box::leak(data);
            let data: &mut [u8] = bytemuck::cast_slice_mut(data);
            data.copy_from_slice(&map);
            Box::from_raw(data)
        }
    };
    buffer.unmap();
    data
}