nncombinator 0.9.8

A library of neural networks that can be written type-safely
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
//! Implementation of various optimizers using Cuda

use core::fmt::Debug;
use std::ffi::c_uint;
use std::marker::PhantomData;
use cuda_runtime_sys::dim3;
use libc::{size_t, c_void};
use crate::cuda::{AsKernelPtr, CudaConstPtr, CudaPtr, CudaMutPtr, Kernel, KernelArgs, AsMutKernelPtr, KernelLaunchConfig};
use crate::cuda::allocator::CudaAllocator;

extern "C" {
    fn update_with_sgd_float(weight: *mut f32, grad: *const f32, size: size_t, a: f32, weight_decay: f32);
    fn update_with_sgd_double(weight: *mut f64, grad: *const f64, size: size_t, a: f64, weight_decay: f64);
    fn update_with_momentum_sgd_float(weight: *mut f32, grad: *const f32, size: size_t, a: f32, mu: f32, weight_decay: f32, vt: *mut f32);
    fn update_with_momentum_sgd_double(weight: *mut f64, grad: *const f64, size: size_t, a: f64, mu: f64, weight_decay: f64, vt: *mut f64);
    fn update_with_adagrad_float(weight: *mut f32, grad: *const f32, size: size_t, a: f32, weight_decay: f32, eps: f32, gt: *mut f32);
    fn update_with_adagrad_double(weight: *mut f64, grad: *const f64, size: size_t, a: f64, weight_decay: f64, eps: f64, gt: *mut f64);
    fn update_with_rmsprop_float(weight: *mut f32, grad: *const f32, size: size_t, a: f32, alpha: f32, mu: f32, eps: f32, gt: *mut f32, bt: *mut f32);
    fn update_with_rmsprop_double(weight: *mut f64, grad: *const f64, size: size_t, a: f64, alpha: f64, mu: f64, eps: f64, gt: *mut f64, bt: *mut f64);
    fn update_with_adam_float(weight: *mut f32, grad: *const f32, size: size_t, a: f32, weight_decay: f32, eps: f32, mt: *mut f32, vt: *mut f32, b1: f32, b2: f32, b1t: f32, b2t: f32);
    fn update_with_adam_double(weight: *mut f64, grad: *const f64, size: size_t, a: f64, weight_decay: f64, eps: f64, mt: *mut f64, vt: *mut f64, b1: f64, b2: f64, b1t: f64, b2t: f64);
    fn update_with_adamw_float(weight: *mut f32, grad: *const f32, size: size_t, a: f32, weight_decay: f32, eps: f32, mt: *mut f32, vt: *mut f32, b1: f32, b2: f32, b1t: f32, b2t: f32);
    fn update_with_adamw_double(weight: *mut f64, grad: *const f64, size: size_t, a: f64, weight_decay: f64, eps: f64, mt: *mut f64, vt: *mut f64, b1: f64, b2: f64, b1t: f64, b2t: f64);
}
/// Defines the list passed to the cuda kernel function as arguments to the SGD optimizer.
pub struct SGDArgs<'a,T,A> where T: Debug, A: CudaAllocator {
    weight: &'a mut CudaMutPtr<'a,T,A>,
    grad: CudaConstPtr<'a,CudaPtr<T,A>>,
    size: usize,
    a: T,
    weight_decay: T
}
/// Create an instance of an object representing the argument list of the SGD optimizer.
impl<'a,T,A> SGDArgs<'a,T,A>
    where T: Debug, A: CudaAllocator,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    /// Create a SGDArgs instance
    /// # Arguments
    /// * `weight` - unit weight
    /// * `grad` - gradient
    /// * `size` - number of weights to be updated
    /// * `a` - learning rate
    /// * `weight_decay` - Weight decay
    pub fn new(weight: &'a mut CudaMutPtr<'a,T,A>, grad: &'a CudaPtr<T,A>, size: usize, a: T, weight_decay: T) -> SGDArgs<'a,T,A> {
        SGDArgs {
            weight,
            grad: CudaConstPtr::new(grad),
            size,
            a,
            weight_decay
        }
    }
}
impl<'a,T,A> KernelArgs for SGDArgs<'a,T,A>
    where T: AsKernelPtr + Debug,
          A: CudaAllocator,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    fn as_vec(&mut self) -> Vec<&mut dyn AsKernelPtr> {
        vec![
            self.weight,
            &mut self.grad,
            &mut self.size,
            &mut self.a,
            &mut self.weight_decay
        ]
    }
}
/// Implementation SGD optimizer
pub struct SGD<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    t:PhantomData<T>,
    a:PhantomData<A>,
    l:PhantomData<&'a ()>
}
impl<'a,T,A> SGD<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    /// Create a SGD optimizer instance
    pub fn new() -> SGD<'a,T,A> {
        SGD {
            t: PhantomData::<T>,
            a: PhantomData::<A>,
            l: PhantomData::<&'a ()>
        }
    }
}
impl<'a,A> Kernel for SGD<'a,f32,A>
    where A: CudaAllocator + 'a,
          CudaMutPtr<'a,f32,A>: AsMutKernelPtr {
    const FUNC_PTR: *const c_void = update_with_sgd_float as *const c_void;
    type Args = SGDArgs<'a,f32,A>;

    fn launch_config(&self, args: &Self::Args) -> KernelLaunchConfig {
        KernelLaunchConfig {
            grid_dim: dim3 { x: (args.size + 1023) as c_uint / 1024, y: 1, z: 1 },
            block_dim: dim3 { x: 1024, y: 1, z: 1 },
            shared_memory_size: 0
        }
    }
}
impl<'a,A> Kernel for SGD<'a,f64,A>
    where A: CudaAllocator + 'a,
          CudaMutPtr<'a,f32,A>: AsMutKernelPtr {
    const FUNC_PTR: *const c_void = update_with_sgd_double as *const c_void;
    type Args = SGDArgs<'a,f64,A>;

    fn launch_config(&self, args: &Self::Args) -> KernelLaunchConfig {
        KernelLaunchConfig {
            grid_dim: dim3 { x: (args.size + 1023) as c_uint / 1024, y: 1, z: 1 },
            block_dim: dim3 { x: 1024, y: 1, z: 1 },
            shared_memory_size: 0
        }
    }
}
/// Defines the list passed to the cuda kernel function as arguments to the Momentum SGD optimizer.
pub struct MomentumSGDArgs<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    weight: &'a mut CudaMutPtr<'a,T,A>,
    grad: CudaConstPtr<'a,CudaPtr<T,A>>,
    size: usize,
    a: T,
    mu: T,
    weight_decay: T,
    vt: &'a mut CudaPtr<T,A>
}
/// Create an instance of an object representing the argument list of the Momentum SGD optimizer.
impl<'a,T,A> MomentumSGDArgs<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    /// Create a MomentumSGDArgs instance
    /// # Arguments
    /// * `weight` - unit weight
    /// * `grad` - gradient
    /// * `size` - number of weights to be updated
    /// * `a` - learning rate
    /// * `mu` - mu
    /// * `weight_decay` - Weight decay
    /// * `vt` - vt
    pub fn new(weight: &'a mut CudaMutPtr<'a,T,A>, grad: &'a CudaPtr<T,A>,
               size: usize, a: T, mu: T, weight_decay: T,
               vt: &'a mut CudaPtr<T,A>) -> MomentumSGDArgs<'a,T,A> {
        MomentumSGDArgs {
            weight,
            grad: CudaConstPtr::new(grad),
            size,
            a,
            mu,
            weight_decay,
            vt
        }
    }
}
impl<'a,T,A> KernelArgs for MomentumSGDArgs<'a,T,A>
    where T: AsKernelPtr + Debug,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    fn as_vec(&mut self) -> Vec<&mut dyn AsKernelPtr> {
        vec![
            self.weight,
            &mut self.grad,
            &mut self.size,
            &mut self.a,
            &mut self.mu,
            &mut self.weight_decay,
            self.vt
        ]
    }
}
/// Implementation Momentum SGD optimizer
pub struct MomentumSGD<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    t:PhantomData<T>,
    a:PhantomData<A>,
    l:PhantomData<&'a ()>
}
impl<'a,T,A> MomentumSGD<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    /// Create a Momentum SGD optimizer instance
    pub fn new() -> MomentumSGD<'a,T,A> {
        MomentumSGD {
            t: PhantomData::<T>,
            a: PhantomData::<A>,
            l: PhantomData::<&'a ()>
        }
    }
}
impl<'a,A> Kernel for MomentumSGD<'a,f32,A>
    where A: CudaAllocator + 'a,
          CudaMutPtr<'a,f32,A>: AsMutKernelPtr {
    const FUNC_PTR: *const c_void = update_with_momentum_sgd_float as *const c_void;
    type Args = MomentumSGDArgs<'a,f32,A>;

    fn launch_config(&self, args: &Self::Args) -> KernelLaunchConfig {
        KernelLaunchConfig {
            grid_dim: dim3 { x: (args.size + 1023) as c_uint / 1024, y: 1, z: 1 },
            block_dim: dim3 { x: 1024, y: 1, z: 1 },
            shared_memory_size: 0
        }
    }
}
impl<'a,A> Kernel for MomentumSGD<'a,f64,A>
    where A: CudaAllocator + 'a,
          CudaMutPtr<'a,f64,A>: AsMutKernelPtr {
    const FUNC_PTR: *const c_void = update_with_momentum_sgd_double as *const c_void;
    type Args = MomentumSGDArgs<'a,f64,A>;

    fn launch_config(&self, args: &Self::Args) -> KernelLaunchConfig {
        KernelLaunchConfig {
            grid_dim: dim3 { x: (args.size + 1023) as c_uint / 1024, y: 1, z: 1 },
            block_dim: dim3 { x: 1024, y: 1, z: 1 },
            shared_memory_size: 0
        }
    }
}
/// Defines the list passed to the cuda kernel function as arguments to the Adagrad optimizer.
pub struct AdagradArgs<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    weight: &'a mut CudaMutPtr<'a,T,A>,
    grad: CudaConstPtr<'a,CudaPtr<T,A>>,
    size: usize,
    a: T,
    weight_decay: T,
    eps: T,
    gt: &'a mut CudaPtr<T,A>
}
/// Create an instance of an object representing the argument list of the Adagrad optimizer.
impl<'a,T,A> AdagradArgs<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    /// Create a AdagradArgs instance
    /// # Arguments
    /// * `weight` - unit weight
    /// * `grad` - gradient
    /// * `size` - number of weights to be updated
    /// * `a` - learning rate
    /// * `weight_decay` - Weight decay
    /// * `eps` - Correction value to prevent zero division
    /// * `gt` - gt
    pub fn new(weight: &'a mut CudaMutPtr<'a,T,A>, grad: &'a CudaPtr<T,A>,
               size: usize, a: T, weight_decay: T, eps: T,
               gt: &'a mut CudaPtr<T,A>) -> AdagradArgs<'a,T,A> {
        AdagradArgs {
            weight,
            grad: CudaConstPtr::new(grad),
            size,
            a,
            weight_decay,
            eps,
            gt
        }
    }
}
impl<'a,T,A> KernelArgs for AdagradArgs<'a,T,A>
    where T: AsKernelPtr + Debug,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    fn as_vec(&mut self) -> Vec<&mut dyn AsKernelPtr> {
        vec![
            self.weight,
            &mut self.grad,
            &mut self.size,
            &mut self.a,
            &mut self.weight_decay,
            &mut self.eps,
            self.gt
        ]
    }
}
/// Implementation Adagrad optimizer
pub struct Adagrad<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    t:PhantomData<T>,
    a:PhantomData<A>,
    l:PhantomData<&'a ()>
}
impl<'a,T,A> Adagrad<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    /// Create a Adagrad optimizer instance
    pub fn new() -> Adagrad<'a,T,A> {
        Adagrad {
            t: PhantomData::<T>,
            a: PhantomData::<A>,
            l: PhantomData::<&'a ()>
        }
    }
}
impl<'a,A> Kernel for Adagrad<'a,f32,A>
    where A: CudaAllocator + 'a,
          CudaMutPtr<'a,f32,A>: AsMutKernelPtr {
    const FUNC_PTR: *const c_void = update_with_adagrad_float as *const c_void;
    type Args = AdagradArgs<'a,f32,A>;

    fn launch_config(&self, args: &Self::Args) -> KernelLaunchConfig {
        KernelLaunchConfig {
            grid_dim: dim3 { x: (args.size + 1023) as c_uint / 1024, y: 1, z: 1 },
            block_dim: dim3 { x: 1024, y: 1, z: 1 },
            shared_memory_size: 0
        }
    }
}
impl<'a,A> Kernel for Adagrad<'a,f64,A>
    where A: CudaAllocator + 'a,
          CudaMutPtr<'a,f64,A>: AsMutKernelPtr {
    const FUNC_PTR: *const c_void = update_with_adagrad_double as *const c_void;
    type Args = AdagradArgs<'a,f64,A>;

    fn launch_config(&self, args: &Self::Args) -> KernelLaunchConfig {
        KernelLaunchConfig {
            grid_dim: dim3 { x: (args.size + 1023) as c_uint / 1024, y: 1, z: 1 },
            block_dim: dim3 { x: 1024, y: 1, z: 1 },
            shared_memory_size: 0
        }
    }
}
/// Defines the list passed to the cuda kernel function as arguments to the Rmsprop optimizer.
pub struct RMSpropArgs<'a,T,A>
    where T: Debug + 'a, A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    weight: &'a mut CudaMutPtr<'a,T,A>,
    grad: CudaConstPtr<'a,CudaPtr<T,A>>,
    size: usize,
    lr: T,
    weight_decay: T,
    alpha:T,
    mu: T,
    eps: T,
    gt: &'a mut CudaPtr<T,A>,
    bt: &'a mut CudaPtr<T,A>
}
/// Create an instance of an object representing the argument list of the Rmsprop optimizer.
impl<'a,T,A> RMSpropArgs<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    /// Create a RmspropArgs instance
    /// # Arguments
    /// * `weight` - unit weight
    /// * `grad` - gradient
    /// * `size` - number of weights to be updated
    /// * `a` - learning rate
    /// * `mu` - mu
    /// * `weight_decay` - Weight Decay
    /// * `eps` - Correction value to prevent zero division
    /// * `gt` - gt
    /// * `bt` - bt
    pub fn new(weight: &'a mut CudaMutPtr<'a,T,A>, grad: &'a CudaPtr<T,A>,
               size: usize, lr: T, weight_decay: T, alpha: T, mu: T, eps: T,
               gt: &'a mut CudaPtr<T,A>,
               bt: &'a mut CudaPtr<T,A>) -> RMSpropArgs<'a,T,A> {
        RMSpropArgs {
            weight,
            grad: CudaConstPtr::new(grad),
            size,
            lr,
            weight_decay,
            alpha,
            mu,
            eps,
            gt,
            bt
        }
    }
}
impl<'a,T,A> KernelArgs for RMSpropArgs<'a,T,A>
    where T: AsKernelPtr + Debug,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    fn as_vec(&mut self) -> Vec<&mut dyn AsKernelPtr> {
        vec![
            self.weight,
            &mut self.grad,
            &mut self.size,
            &mut self.lr,
            &mut self.weight_decay,
            &mut self.alpha,
            &mut self.mu,
            &mut self.eps,
            self.gt,
            self.bt
        ]
    }
}
/// Implementation Rmsprop optimizer
pub struct RMSprop<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    t:PhantomData<T>,
    a:PhantomData<A>,
    l:PhantomData<&'a ()>
}
impl<'a,T,A> RMSprop<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    /// Create a Rmsprop optimizer instance
    pub fn new() -> RMSprop<'a,T,A> {
        RMSprop {
            t: PhantomData::<T>,
            a: PhantomData::<A>,
            l: PhantomData::<&'a ()>
        }
    }
}
impl<'a,A> Kernel for RMSprop<'a,f32,A>
    where A: CudaAllocator + 'a,
          CudaMutPtr<'a,f32,A>: AsMutKernelPtr {
    const FUNC_PTR: *const c_void = update_with_rmsprop_float as *const c_void;
    type Args = RMSpropArgs<'a,f32,A>;

    fn launch_config(&self, args: &Self::Args) -> KernelLaunchConfig {
        KernelLaunchConfig {
            grid_dim: dim3 { x: (args.size + 1023) as c_uint / 1024, y: 1, z: 1 },
            block_dim: dim3 { x: 1024, y: 1, z: 1 },
            shared_memory_size: 0
        }
    }
}
impl<'a,A> Kernel for RMSprop<'a,f64,A>
    where A: CudaAllocator + 'a,
          CudaMutPtr<'a,f64,A>: AsMutKernelPtr {
    const FUNC_PTR: *const c_void = update_with_rmsprop_double as *const c_void;
    type Args = RMSpropArgs<'a,f64,A>;

    fn launch_config(&self, args: &Self::Args) -> KernelLaunchConfig {
        KernelLaunchConfig {
            grid_dim: dim3 { x: (args.size + 1023) as c_uint / 1024, y: 1, z: 1 },
            block_dim: dim3 { x: 1024, y: 1, z: 1 },
            shared_memory_size: 0
        }
    }
}
/// Defines the list passed to the cuda kernel function as arguments to the Adam optimizer.
pub struct AdamArgs<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    weight: &'a mut CudaMutPtr<'a,T,A>,
    grad: CudaConstPtr<'a,CudaPtr<T,A>>,
    size: usize,
    a: T,
    weight_decay: T,
    eps: T,
    mt: &'a mut CudaPtr<T,A>,
    vt: &'a mut CudaPtr<T,A>,
    b1: T,
    b2: T,
    b1t: T,
    b2t: T
}
/// Create an instance of an object representing the argument list of the Adam optimizer.
impl<'a,T,A> AdamArgs<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    /// Create a AdamArgs instance
    /// # Arguments
    /// * `weight` - unit weight
    /// * `grad` - gradient
    /// * `size` - number of weights to be updated
    /// * `a` - learning rate
    /// * `weight_decay` - Weight Decay
    /// * `eps` - Correction value to prevent zero division
    /// * `mt` - mt
    /// * `vt` - vt
    /// * `b1` - b1
    /// * `b2` - b2
    /// * `b1t` - b1t
    /// * `b2t` - b2t
    pub fn new(weight: &'a mut CudaMutPtr<'a,T,A>, grad: &'a CudaPtr<T,A>,
               size: usize, a: T, weight_decay: T, eps: T,
               mt: &'a mut CudaPtr<T,A>,
               vt: &'a mut CudaPtr<T,A>,b1: T, b2: T, b1t: T, b2t: T) -> AdamArgs<'a,T,A> {
        AdamArgs {
            weight,
            grad: CudaConstPtr::new(grad),
            size,
            a,
            weight_decay,
            eps,
            mt,
            vt,
            b1,
            b2,
            b1t,
            b2t
        }
    }
}
impl<'a,T,A> KernelArgs for AdamArgs<'a,T,A>
    where T: AsKernelPtr + Debug,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    fn as_vec(&mut self) -> Vec<&mut dyn AsKernelPtr> {
        vec![
            self.weight,
            &mut self.grad,
            &mut self.size,
            &mut self.a,
            &mut self.weight_decay,
            &mut self.eps,
            self.mt,
            self.vt,
            &mut self.b1,
            &mut self.b2,
            &mut self.b1t,
            &mut self.b2t
        ]
    }
}
/// Implementation Adam optimizer
pub struct Adam<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    t:PhantomData<T>,
    a:PhantomData<A>,
    l:PhantomData<&'a ()>
}
impl<'a,T,A> Adam<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    /// Create a Adam optimizer instance
    pub fn new() -> Adam<'a,T,A> {
        Adam {
            t: PhantomData::<T>,
            a: PhantomData::<A>,
            l: PhantomData::<&'a ()>
        }
    }
}
impl<'a,A> Kernel for Adam<'a,f32,A>
    where A: CudaAllocator + 'a,
          CudaMutPtr<'a,f32,A>: AsMutKernelPtr {
    const FUNC_PTR: *const c_void = update_with_adam_float as *const c_void;
    type Args = AdamArgs<'a,f32,A>;

    fn launch_config(&self, args: &Self::Args) -> KernelLaunchConfig {
        KernelLaunchConfig {
            grid_dim: dim3 { x: (args.size + 1023) as c_uint / 1024, y: 1, z: 1 },
            block_dim: dim3 { x: 1024, y: 1, z: 1 },
            shared_memory_size: 0
        }
    }
}
impl<'a,A> Kernel for Adam<'a,f64,A>
    where A: CudaAllocator + 'a,
          CudaMutPtr<'a,f64,A>: AsMutKernelPtr {
    const FUNC_PTR: *const c_void = update_with_adam_double as *const c_void;
    type Args = AdamArgs<'a,f64,A>;

    fn launch_config(&self, args: &Self::Args) -> KernelLaunchConfig {
        KernelLaunchConfig {
            grid_dim: dim3 { x: (args.size + 1023) as c_uint / 1024, y: 1, z: 1 },
            block_dim: dim3 { x: 1024, y: 1, z: 1 },
            shared_memory_size: 0
        }
    }
}

/// Defines the list passed to the cuda kernel function as arguments to the AdamW optimizer.
pub struct AdamWArgs<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    weight: &'a mut CudaMutPtr<'a,T,A>,
    grad: CudaConstPtr<'a,CudaPtr<T,A>>,
    size: usize,
    a: T,
    weight_decay: T,
    eps: T,
    mt: &'a mut CudaPtr<T,A>,
    vt: &'a mut CudaPtr<T,A>,
    b1: T,
    b2: T,
    b1t: T,
    b2t: T
}
/// Create an instance of an object representing the argument list of the AdamW optimizer.
impl<'a,T,A> AdamWArgs<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    /// Create a AdamWArgs instance
    /// # Arguments
    /// * `weight` - unit weight
    /// * `grad` - gradient
    /// * `size` - number of weights to be updated
    /// * `a` - learning rate
    /// * `weight_decay` - Weight Decay
    /// * `eps` - Correction value to prevent zero division
    /// * `mt` - mt
    /// * `vt` - vt
    /// * `b1` - b1
    /// * `b2` - b2
    /// * `b1t` - b1t
    /// * `b2t` - b2t
    pub fn new(weight: &'a mut CudaMutPtr<'a,T,A>, grad: &'a CudaPtr<T,A>,
               size: usize, a: T, weight_decay: T, eps: T,
               mt: &'a mut CudaPtr<T,A>,
               vt: &'a mut CudaPtr<T,A>,b1: T, b2: T, b1t: T, b2t: T) -> AdamWArgs<'a,T,A> {
        AdamWArgs {
            weight,
            grad: CudaConstPtr::new(grad),
            size,
            a,
            weight_decay,
            eps,
            mt,
            vt,
            b1,
            b2,
            b1t,
            b2t
        }
    }
}
impl<'a,T,A> KernelArgs for AdamWArgs<'a,T,A>
    where T: AsKernelPtr + Debug,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    fn as_vec(&mut self) -> Vec<&mut dyn AsKernelPtr> {
        vec![
            self.weight,
            &mut self.grad,
            &mut self.size,
            &mut self.a,
            &mut self.weight_decay,
            &mut self.eps,
            self.mt,
            self.vt,
            &mut self.b1,
            &mut self.b2,
            &mut self.b1t,
            &mut self.b2t
        ]
    }
}
/// Implementation AdamW optimizer
pub struct AdamW<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    t:PhantomData<T>,
    a:PhantomData<A>,
    l:PhantomData<&'a ()>
}
impl<'a,T,A> AdamW<'a,T,A>
    where T: Debug + 'a,
          A: CudaAllocator + 'a,
          CudaMutPtr<'a,T,A>: AsMutKernelPtr {
    /// Create a AdamW optimizer instance
    pub fn new() -> AdamW<'a,T,A> {
        AdamW {
            t: PhantomData::<T>,
            a: PhantomData::<A>,
            l: PhantomData::<&'a ()>
        }
    }
}
impl<'a,A> Kernel for AdamW<'a,f32,A>
    where A: CudaAllocator + 'a,
          CudaMutPtr<'a,f32,A>: AsMutKernelPtr {
    const FUNC_PTR: *const c_void = update_with_adamw_float as *const c_void;
    type Args = AdamWArgs<'a,f32,A>;

    fn launch_config(&self, args: &Self::Args) -> KernelLaunchConfig {
        KernelLaunchConfig {
            grid_dim: dim3 { x: (args.size + 1023) as c_uint / 1024, y: 1, z: 1 },
            block_dim: dim3 { x: 1024, y: 1, z: 1 },
            shared_memory_size: 0
        }
    }
}
impl<'a,A> Kernel for AdamW<'a,f64,A>
    where A: CudaAllocator + 'a,
          CudaMutPtr<'a,f64,A>: AsMutKernelPtr {
    const FUNC_PTR: *const c_void = update_with_adamw_double as *const c_void;
    type Args = AdamWArgs<'a,f64,A>;

    fn launch_config(&self, args: &Self::Args) -> KernelLaunchConfig {
        KernelLaunchConfig {
            grid_dim: dim3 { x: (args.size + 1023) as c_uint / 1024, y: 1, z: 1 },
            block_dim: dim3 { x: 1024, y: 1, z: 1 },
            shared_memory_size: 0
        }
    }
}