fused_gpu 0.1.0-beta.1

GPU graph compilation to fuse linear tensor operations
Documentation
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
//! Full dispatch of math operations and GPU contVec;

use crate::{
    dispatch::backend::{
        Graph, MetaId, NodeId, OptimState, Param, StateDim,
        kernel::{Dependencies, RawKernel, Redirect, SaveIndicator},
    },
    errors::Error,
    tensor::{Tensor, ToBuffer, build_dims},
};
use briny::{
    raw::cast::{slice_to_bytes, slice_to_bytes_mut},
    traits::Pod,
};
use core::fmt::Debug;
use half::slice::HalfFloatSliceExt;
use std::{vec, vec::Vec};

pub mod backend;

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum PollStatus {
    QueueEmpty,
    Failed,
    Pending,
    Ready,
}

#[derive(Hash, PartialEq, Eq)]
pub struct CompilationOptions {
    pub target: TargetCompilationOptions,
    pub debug: DebugCompilationOptions,
    pub opt: OptCompilationOptions,
}

#[derive(Debug, Hash, PartialEq, Eq)]
pub struct TargetCompilationOptions {
    pub flags: TargetFlags,
}

bitflags::bitflags! {
    #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
    pub struct TargetFlags: u8 {
        /// Support for linear algebra accelerator (e.g. tensor core via `wmma`)
        const LIN_ACC = 1 << 0;

        /// Links a BLAS library (e.g. `cuBLAS`)
        const BLAS_LIB = 1 << 1;

        /// Support for asynchronous memory load
        const ASYNC_MEM_LOAD = 1 << 2;

        /// Support for asynchronous memory store
        const ASYNC_MEM_STORE = 1 << 3;
    }
}

bitflags::bitflags! {
    #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
    pub struct DebugCompilationOptions: u8 {
        /// Formats intermediate representation
        const PRETTY_PRINT_IR = 1 << 0;
    }
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct OptCompilationOptions {
    /// Sets tile size or block size.
    pub tile_size: u32,

    /// Sets the amount of passes to optimize each kernel.
    ///
    /// The compiler will quickly run out of optimizations if this is set too high.
    pub passes: u8,

    /// Defines the set of optimizations the compiler will run each pass.
    pub flags: OptFlags,
}

impl Default for OptCompilationOptions {
    fn default() -> Self {
        Self {
            tile_size: 16,
            passes: 3,
            flags: OptFlags::all(),
        }
    }
}

bitflags::bitflags! {
    #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
    pub struct OptFlags: u16 {
        /// Enables or disables dead code elimination.
        ///
        /// `let useless = 1234;` -> nothing
        const DEAD_CODE = 1 << 0;

        /// Enables or disables constant folding.
        ///
        /// `2*2` -> `4`
        const CONST_FOLD = 1 << 1;

        /// Converts assignments to op-assignments.
        ///
        /// `a = b + a;` -> `a += b;`
        const OP_ASSIGN = 1 << 2;

        /// Applies the identity prperty to arithmetic.
        ///
        /// `x*1` or `x/1` or `x+0` or `x-0` -> `x`
        const IDENTITY = 1 << 3;

        /// Converts constant division statements to multiplication.
        ///
        /// `x/2` -> `x*0.5`
        const DIV_CONST = 1 << 4;

        /// Fuses multiply-add operations.
        ///
        /// `a*b+c` -> `fma(a,b,c)`
        const MUL_ADD = 1 << 5;

        /// Moves operations that dont depend on the state of a loop to before the loop started.
        ///
        /// `loop { a = b; }` -> `a = b; loop {  }`
        const LOOP_STATELESS = 1 << 6;

        /// Removes empty scopes (loop, if) to simplify code.
        ///
        /// `loop {  }` -> nothing
        const EMPTY_SCOPE = 1 << 7;

        /// Changes mutable variables to immutable variables if they are never mutated.
        ///
        /// `let mut a = 123;` -> `let a = 123;`
        const UNUSED_MUT = 1 << 8;

        /// Removes copies of immutable variables, replacing their usages directly with the original variable.
        ///
        /// `let a = 123; let b = a; let c = b - 1;` -> `let a = 123; let c = a - 1;`
        const COPY_IMMUT = 1 << 9;

        /// Removes needless late intialization of mutable variables.
        ///
        /// `let mut a; a = 2;` -> `let mut a = 2;`
        const LATE_INIT = 1 << 10;
    }
}

pub trait GpuBufferBackend: Debug {
    fn size(&self) -> u32;

    fn size_bytes(&self) -> u32;
}

pub trait GpuKernelBackend {
    fn iteration_space(&self) -> &[MetaId];
    fn block(&self) -> &[u32; 3];
}

pub trait GpuBackend: Sized {
    type Buffer: GpuBufferBackend;
    type Kernel: GpuKernelBackend;
    type Batcher<'a>;
    type BatchState;
    type SubmissionIndex;
    type ParamLayout;
    type Schedule;
    type SyncSubmissions;

    /// The target-specific configuration for the compiler.
    fn target_spec(&self) -> TargetCompilationOptions;

    /// Allocates an uninitialized GPU buffer with the size `len` in bytes.
    fn alloc(&self, len: usize) -> Self::Buffer;

    /// Allocates a slice of `u8` (to be reinterpreted as larger datatypes).
    fn alloc_init(&self, data: &[u8]) -> Self::Buffer;

    /// Allocates a slice of `u32` on the GPU.
    ///
    /// This allocation is often small and uniform.
    fn alloc_meta(&self, data: &[u32]) -> Self::Buffer;

    /// Copies the content of a CPU buffer to a GPU buffer at offset `dst_off`.
    ///
    /// # Errors
    ///
    /// Should return an [`ErrorKind::FailedBufferCopy`](`crate::errors::ErrorKind::FailedBufferCopy`).
    fn upload(
        &self,
        buffer: &Self::Buffer,
        data: &[u8],
        dst_off: u32,
    ) -> Result<Self::SubmissionIndex, Error>;

    /// Copies the content of one buffer to another.
    ///
    /// # Errors
    ///
    /// Should return an [`ErrorKind::FailedBufferCopy`](`crate::errors::ErrorKind::FailedBufferCopy`).
    fn pipe(&self, src: &Self::Buffer, dst: &Self::Buffer) -> Result<Self::SubmissionIndex, Error>;

    /// Copies the content of a GPU buffer to a CPU buffer.
    ///
    /// # Errors
    ///
    /// Should return an [`ErrorKind::FailedBufferCopy`](`crate::errors::ErrorKind::FailedBufferCopy`).
    fn download(&self, buffer: &Self::Buffer, out: &mut [u8]) -> Result<(), Error>;

    /// Compiles the `RawKernel` (containing ops+vars) to a GPU kernel ([`Self::Kernel`]).
    ///
    /// # Errors
    ///
    /// For an invalid kernel, param layout, or compilation options and all related errors.
    fn compile(
        &self,
        src: &RawKernel,
        params: &[Param],
        options: &CompilationOptions,
    ) -> Result<Self::Kernel, Error>;

    fn schedule(
        &self,
        kernels: Vec<Dependencies<Redirect<(Self::Kernel, NodeId, &[bool])>>>,
        bindings: &[&Self::Buffer],
        meta: &[u32],
    ) -> Result<Self::Schedule, Error>;

    fn dispatch_kernel(
        &self,
        batcher: &mut Self::Batcher<'_>,
        kernel: &Self::Kernel,
        wg: [u32; 3],
        bindings: &[&Self::Buffer],
    );

    fn dispatch_schedule(&self, batcher: &mut Self::Batcher<'_>, schedule: &Self::Schedule);

    fn sync(&self, submission_index: Self::SubmissionIndex);

    fn prepare_batch(&self) -> Self::BatchState;

    fn start_batch<'a>(&self, state: &'a mut Self::BatchState) -> Self::Batcher<'a>;

    fn encode(&self, state: Self::BatchState) -> Self::SyncSubmissions;

    fn submit(&self, submission: Self::SyncSubmissions) -> Self::SubmissionIndex;

    fn poll(&self) -> PollStatus;
}

/// Allocate a buffer on the GPU.
pub fn gpu_alloc<B: GpuBackend>(context: &B, len: usize) -> GpuBuffer<B> {
    GpuBuffer {
        inner: context.alloc(len),
    }
}

/// Allocate a buffer on the GPU and copy CPU memory into it.
pub fn gpu_alloc_init<T: Pod, B: GpuBackend>(context: &B, data: &[T]) -> GpuBuffer<B> {
    GpuBuffer {
        inner: context.alloc_init(slice_to_bytes(data)),
    }
}

/// Generic GPU buffer for any backend.
#[repr(transparent)]
#[derive(Debug, Clone)]
pub struct GpuBuffer<B: GpuBackend = backend::GpuContext> {
    pub(crate) inner: B::Buffer,
}

impl<B: GpuBackend> GpuBuffer<B> {
    /// Requests the length of the buffer in 32-bit chunks (`f32`, `u32`, etc.).
    pub fn size(&self) -> u32 {
        self.inner.size()
    }

    /// Requests the length of the buffer in bytes.
    pub fn size_bytes(&self) -> u32 {
        self.inner.size_bytes()
    }
}

/// A group of compiled kernels including all required metadata.
///
/// It includes the kernels of the forward, backward, and loss passes, the order
/// in which to execute them, and the parameters they use.
#[derive(Debug)]
pub struct KernelGroup<'a, B: GpuBackend = backend::GpuContext> {
    pub(crate) forward: Vec<Dependencies<Redirect<(B::Kernel, usize, &'a [bool])>>>,
    pub(crate) backward: Vec<Dependencies<Redirect<(B::Kernel, usize, &'a [bool])>>>,
    pub(crate) loss: B::Kernel,
    pub(crate) optim: B::Kernel,
}

/// Handle to a series of GPU submissions.
#[must_use]
pub struct SubmissionIndex<'a, B: GpuBackend = backend::GpuContext>(
    B::SubmissionIndex,
    &'a GpuContext<B>,
);

impl<B: GpuBackend> SubmissionIndex<'_, B> {
    pub fn sync(self) {
        self.1.inner.sync(self.0);
    }
}

/// Handle to a series of unsubmitted GPU kernels.
#[must_use]
pub struct SyncSubmission<'a, B: GpuBackend = backend::GpuContext>(
    B::SyncSubmissions,
    &'a GpuContext<B>,
);

impl<'a, B: GpuBackend> SyncSubmission<'a, B> {
    pub fn submit(self) -> SubmissionIndex<'a, B> {
        SubmissionIndex(self.1.inner.submit(self.0), self.1)
    }
}

#[must_use]
pub struct BatchState<'a, B: GpuBackend = backend::GpuContext>(B::BatchState, &'a GpuContext<B>);

impl<'a, B: GpuBackend> BatchState<'a, B> {
    pub fn encode(self) -> SyncSubmission<'a, B> {
        SyncSubmission(self.1.inner.encode(self.0), self.1)
    }
}

/// Schedule used to improve performance by caching critical launch information.
pub struct Schedule<'a, B: GpuBackend = backend::GpuContext> {
    forward: B::Schedule,
    backward: B::Schedule,
    loss: LossSchedule<'a, B>,
    optim: OptimSchedule<'a, B>,
}

struct LossSchedule<'a, B: GpuBackend> {
    grid: [u32; 3],
    bindings: [&'a B::Buffer; 5],
    kernel: B::Kernel,
}

struct OptimSchedule<'a, B: GpuBackend> {
    bindings: Vec<&'a B::Buffer>,
    kernel: B::Kernel,
    state: &'a [B::Buffer],
}

/// Generic GPU context storing a handle to the device and shaders.
///
/// Perhaps most important structure in all of Fused GPU this is. Without it,
/// you couldn't compile kernels, execute kernels, create tensors, copy data.
#[repr(transparent)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GpuContext<B: GpuBackend = backend::GpuContext> {
    pub(crate) inner: B,
}

impl GpuContext<backend::GpuContext> {
    /// Blocking creation of the context by searching for GPU.
    ///
    /// # Errors
    ///
    /// Failure is platform-specific and backend-dependent. It is likely a result of
    /// not finding a supported device. Errors must be handled properly in critical code.
    pub fn new() -> Result<Self, Error> {
        Ok(Self {
            inner: pollster::block_on(backend::GpuContext::new())?,
        })
    }

    /// Non-blocking creation of the context by searching for GPU.
    ///
    /// # Errors
    ///
    /// Failure is platform-specific and backend-dependent. It is likely a result of
    /// not finding a supported device. Errors must be handled properly in critical code.
    pub async fn new_nonblocking() -> Result<Self, Error> {
        Ok(Self {
            inner: backend::GpuContext::new().await?,
        })
    }
}

impl<B: GpuBackend> GpuContext<B> {
    /// Wrapping the inner context.
    pub const fn new_with_context(ctx: B) -> Self {
        Self { inner: ctx }
    }

    pub fn detect_target(&self) -> TargetCompilationOptions {
        self.inner.target_spec()
    }

    /// Compiles a graph into a kernels.
    ///
    /// # Errors
    ///
    /// Failure is platform-specific and backend-dependent but often a result of invalid input.
    /// Regardless, errors must be handled properly in critical code.
    pub fn compile<'a>(
        &self,
        ir: &'a backend::kernel::KernelGroup<'a>,
        options: &CompilationOptions,
    ) -> Result<KernelGroup<'a, B>, Error> {
        let forward = ir
            .forward
            .kernels
            .iter()
            .map(|kernel| {
                let dep = kernel.dep.clone();

                let compiled = match &kernel.val {
                    Redirect::Unmasked(kernel) => {
                        let params = ir
                            .forward
                            .params
                            .iter()
                            .enumerate()
                            .filter_map(
                                |(i, param)| if kernel.params[i] { Some(*param) } else { None },
                            )
                            .collect::<Vec<_>>();

                        Redirect::Unmasked((
                            self.inner.compile(&kernel.raw, &params, options)?,
                            kernel.raw.root,
                            kernel.params.as_slice(),
                        ))
                    }
                    Redirect::Redirected(idx) => Redirect::Redirected(*idx),
                };

                Ok(Dependencies { val: compiled, dep })
            })
            .collect::<Result<
                Vec<Dependencies<Redirect<(<B as GpuBackend>::Kernel, usize, &'a [bool])>>>,
                Error,
            >>()?;

        let backward = ir
            .backward
            .kernels
            .iter()
            .map(|kernel| {
                let dep = kernel.dep.clone();

                let compiled = match &kernel.val {
                    Redirect::Unmasked(kernel) => {
                        let params = ir
                            .backward
                            .params
                            .iter()
                            .enumerate()
                            .filter_map(
                                |(i, param)| if kernel.params[i] { Some(*param) } else { None },
                            )
                            .collect::<Vec<_>>();

                        Redirect::Unmasked((
                            self.inner.compile(&kernel.raw, &params, options)?,
                            kernel.raw.root,
                            kernel.params.as_slice(),
                        ))
                    }
                    Redirect::Redirected(idx) => Redirect::Redirected(*idx),
                };

                Ok(Dependencies { val: compiled, dep })
            })
            .collect::<Result<
                Vec<Dependencies<Redirect<(<B as GpuBackend>::Kernel, usize, &'a [bool])>>>,
                Error,
            >>()?;

        let loss = self.inner.compile(&ir.loss.raw, &ir.loss.params, options)?;
        let optim = self
            .inner
            .compile(&ir.optim.raw, &ir.optim.params, options)?;

        Ok(KernelGroup {
            forward,
            backward,
            loss,
            optim,
        })
    }

    /// Copies the content of a GPU buffer into a CPU buffer.
    ///
    /// # Errors
    ///
    /// Failure is platform-specific and backend-dependent. It might only return an error
    /// if buffer lengths are unequal, but its behavior should not be assumed. Errors
    /// must be handled properly in critical code.
    pub fn download<T: Pod, S: ToBuffer<B>>(&self, tensor: &S, dst: &mut [T]) -> Result<(), Error> {
        self.inner
            .download(tensor.as_buffer(), slice_to_bytes_mut(dst))
    }

    /// Copies the content of a CPU buffer into GPU buffer.
    ///
    /// # Errors
    ///
    /// Failure is platform-specific and backend-dependent. It might only return an error
    /// if buffer lengths are unequal, but its behavior should not be assumed. Errors
    /// must be handled properly in critical code.
    pub fn upload<T: Pod, S: ToBuffer<B>>(
        &self,
        tensor: &S,
        dst: &[T],
        dst_off: u32,
    ) -> Result<SubmissionIndex<'_, B>, Error> {
        self.inner
            .upload(tensor.as_buffer(), slice_to_bytes(dst), dst_off)
            .map(|x| SubmissionIndex(x, self))
    }

    /// Copies the content of one buffer to another without mutating the source.
    ///
    /// This is faster than chaining [`Self::download`] into [`Self::upload`] because it
    /// bypasses CPU memory.
    ///
    /// # Errors
    ///
    /// Failure is platform-specific and backend-dependent. It might only return an error
    /// if buffer lengths are unequal, but its behavior should not be assumed. Errors
    /// must be handled properly in critical code.
    pub fn pipe<S1: ToBuffer<B>, S2: ToBuffer<B>>(
        &self,
        src: &S1,
        dst: &S2,
    ) -> Result<SubmissionIndex<'_, B>, Error> {
        self.inner
            .pipe(src.as_buffer(), dst.as_buffer())
            .map(|x| SubmissionIndex(x, self))
    }

    pub fn prepare_batch(&self) -> BatchState<'_, B> {
        BatchState(self.inner.prepare_batch(), self)
    }

    pub fn start_batch<'a>(&'a self, state: &'a mut BatchState<B>) -> Batcher<'a, B> {
        Batcher(self.inner.start_batch(&mut state.0), self)
    }

    pub fn alloc_tensors<const N: usize>(
        &self,
        graph: &Graph<'_>,
        saved: &[SaveIndicator],
        meta: &[u32],
        state: &OptimState<N>,
    ) -> AllocTensors<B> {
        let mut forward_saved = Vec::new();
        let mut grad_tensors = Vec::new();
        let mut state_tensors = Vec::new();

        for (idx, save) in saved.iter().enumerate() {
            let node = &graph.nodes[idx];
            let node_shape = &node.shape;
            let num_shape = build_dims(node_shape, meta);

            let len = (num_shape.iter().product::<u32>() as usize) * node.dtype.size();

            if save.is_defined_in_forward() {
                let buf = self.inner.alloc(len);
                forward_saved.push(buf);
            }

            if save.is_defined_in_backward() {
                let buf = self.inner.alloc(len);
                grad_tensors.push(buf);

                state_tensors.reserve(N);

                for state_t in 0..N {
                    let len = match state.shapes[state_t] {
                        StateDim::Const(value) => value as usize,
                        StateDim::GradRelative(value) => len * (value as usize),
                    };
                    let buf = self.inner.alloc(len);

                    state_tensors.push(buf);
                }
            }
        }

        let meta_buffer = self.inner.alloc_meta(meta);

        let node_shape = &graph.nodes[graph.nodes.len() - 1].shape;
        let node_dtype = &graph.nodes[graph.nodes.len() - 1].dtype;
        let shape = build_dims(node_shape, meta);
        let len = shape.iter().product::<u32>() as usize * node_dtype.size();

        let forward_out = self.inner.alloc(len);
        let loss_t = self.inner.alloc(len);
        let seed = self.inner.alloc(len);

        AllocTensors {
            meta: meta_buffer,
            forward_saved,
            grad_tensors,
            forward_out,
            seed,
            state: state_tensors,
            loss_t: Tensor {
                shape,
                data: GpuBuffer { inner: loss_t },
            },
        }
    }

    pub fn new_tensor<const N: u32>(&self, shape: Vec<u32>) -> Tensor<B> {
        let len = (shape.iter().product::<u32>() * const { N / 8 }) as usize;
        let data = self.inner.alloc(len);
        Tensor {
            shape,
            data: GpuBuffer { inner: data },
        }
    }

    pub fn init_tensor_f64(&self, shape: Vec<u32>, data: &[f64]) -> Tensor<B> {
        debug_assert_eq!(
            shape.iter().product::<u32>(),
            data.len() as u32,
            "shape product (left) and data length (right) mismatch"
        );

        let data = gpu_alloc_init(&self.inner, data);
        Tensor { shape, data }
    }

    pub fn init_tensor_f32(&self, shape: Vec<u32>, data: &[f32]) -> Tensor<B> {
        debug_assert_eq!(
            shape.iter().product::<u32>(),
            data.len() as u32,
            "shape product (left) and data length (right) mismatch"
        );

        let data = gpu_alloc_init(&self.inner, data);
        Tensor { shape, data }
    }

    pub fn init_tensor_f16(&self, shape: Vec<u32>, data: &[half::f16]) -> Tensor<B> {
        debug_assert_eq!(
            shape.iter().product::<u32>(),
            data.len() as u32,
            "shape product (left) and data length (right) mismatch"
        );

        let data_u16 = data.reinterpret_cast();
        let data = gpu_alloc_init(&self.inner, data_u16);

        Tensor { shape, data }
    }

    pub fn init_tensor_bf16(&self, shape: Vec<u32>, data: &[half::bf16]) -> Tensor<B> {
        debug_assert_eq!(
            shape.iter().product::<u32>(),
            data.len() as u32,
            "shape product (left) and data length (right) mismatch"
        );

        let data_u16 = data.reinterpret_cast();
        let data = gpu_alloc_init(&self.inner, data_u16);

        Tensor { shape, data }
    }

    /// Allocates an empty one-hot vector.
    pub fn new_onehot(&self, classes: u32) -> Tensor<B> {
        let data = gpu_alloc(&self.inner, classes as usize);
        Tensor {
            data,
            shape: vec![classes],
        }
    }

    /// Allocates a new one-hot vector with the defined classes.
    pub fn new_onehot_init(&self, indices: &[u32]) -> Tensor<B> {
        let data = gpu_alloc_init(&self.inner, indices);
        Tensor {
            data,
            shape: vec![indices.len() as u32],
        }
    }

    pub fn schedule<'a, const N: usize>(
        &self,
        kernels: KernelGroup<B>,
        meta: &[u32],
        in_tensors: &'a [Tensor<B>],
        alloc_tensors: &'a AllocTensors<B>,
        state: &'a [B::Buffer],
    ) -> Result<Schedule<'a, B>, Error>
    where
        B::Buffer: Sized,
    {
        let mut bindings = Vec::new();

        bindings.push(&alloc_tensors.meta);

        alloc_tensors
            .forward_saved
            .iter()
            .for_each(|t| bindings.push(t));
        for t in in_tensors {
            bindings.push(&t.data.inner);
        }

        bindings.push(&alloc_tensors.forward_out);

        let forward = self.inner.schedule(kernels.forward, &bindings, meta)?;

        bindings.truncate(1);

        bindings.push(&alloc_tensors.seed);

        alloc_tensors
            .grad_tensors
            .iter()
            .for_each(|t| bindings.push(t));
        for t in in_tensors {
            bindings.push(&t.data.inner);
        }
        alloc_tensors
            .forward_saved
            .iter()
            .for_each(|t| bindings.push(t));

        let backward = self.inner.schedule(kernels.backward, &bindings, meta)?;

        let grid = alloc_tensors.loss_t.calc_grid(*kernels.loss.block());

        let bindings = [
            &alloc_tensors.meta,
            &alloc_tensors.loss_t.data.inner,
            &alloc_tensors.seed,
            &alloc_tensors.forward_out,
            &alloc_tensors.seed,
        ];

        let loss = LossSchedule {
            grid,
            bindings,
            kernel: kernels.loss,
        };

        let mut bindings = vec![
            &alloc_tensors.meta,
            &alloc_tensors.meta,
            &alloc_tensors.meta,
        ];

        for _ in 0..N {
            bindings.push(&alloc_tensors.meta);
        }

        let optim = OptimSchedule {
            bindings,
            kernel: kernels.optim,
            state,
        };

        Ok(Schedule {
            forward,
            backward,
            loss,
            optim,
        })
    }

    #[cfg(feature = "io")]
    pub fn save_tensors<P: AsRef<std::path::Path>>(
        &self,
        path: P,
        tensors: &[Tensor<B>],
        header: crate::io::BpatHeader,
    ) -> Result<(), Error<crate::io::SerialTensorError>> {
        crate::io::save_tensors(path, self, tensors, header)
    }

    #[cfg(feature = "io")]
    pub fn load_tensors<P: AsRef<std::path::Path>>(
        &self,
        path: P,
    ) -> Result<Vec<Tensor<B>>, Error<crate::io::SerialTensorError>> {
        crate::io::load_tensors(path, self)
    }
}

#[must_use]
pub struct Batcher<'a, B: GpuBackend = backend::GpuContext>(B::Batcher<'a>, &'a GpuContext<B>);

impl<B: GpuBackend> Batcher<'_, B> {
    pub fn dispatch_forward(&mut self, schedule: &Schedule<'_, B>) {
        self.1
            .inner
            .dispatch_schedule(&mut self.0, &schedule.forward);
    }

    pub fn dispatch_backward(&mut self, schedule: &Schedule<'_, B>) {
        self.1
            .inner
            .dispatch_schedule(&mut self.0, &schedule.backward);
    }

    pub fn dispatch_loss<T: ToBuffer<B>>(&mut self, schedule: &mut Schedule<'_, B>, target: &T) {
        let mut bindings = schedule.loss.bindings;
        bindings[4] = target.as_buffer();

        self.1.inner.dispatch_kernel(
            &mut self.0,
            &schedule.loss.kernel,
            schedule.loss.grid,
            &bindings,
        );
    }

    pub fn dispatch_optim<const N: usize>(
        &mut self,
        schedule: &mut Schedule<'_, B>,
        weight: &Tensor<B>,
        grad: usize,
        tensors: &AllocTensors<B>,
    ) {
        let grid = weight.calc_grid(*schedule.optim.kernel.block());

        let mut bindings = schedule.optim.bindings.clone();

        bindings[1] = weight.as_buffer();
        bindings[2] = &tensors.grad_tensors[grad];

        for state_t in 0..N {
            bindings[3 + state_t] = &schedule.optim.state[state_t];
        }

        self.1
            .inner
            .dispatch_kernel(&mut self.0, &schedule.optim.kernel, grid, &bindings);
    }
}

#[derive(Debug)]
pub struct AllocTensors<B: GpuBackend = backend::GpuContext> {
    pub meta: B::Buffer,
    pub forward_saved: Vec<B::Buffer>,
    pub grad_tensors: Vec<B::Buffer>,
    pub forward_out: B::Buffer,
    pub seed: B::Buffer,

    pub state: Vec<B::Buffer>,

    pub loss_t: Tensor<B>,
}