nncombinator 0.9.0

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
//! Implementation of various loss functions
use std::marker::PhantomData;
use libc::{c_int, c_void};
use crate::cuda::{AsKernelPtr, CudaConstPtr, CudaTensor1dPtr, CudaTensor1dPtrView, CudaVec, CudaVecView, DataTypeInfo, Kernel, KernelArgs};
use crate::ope::UnitValue;

extern "C" {
    fn loss_linear_batch_mse_derive_float(r: *const f32, t: *const f32, output: *mut f32, nlen: c_int, batch_size: c_int) -> c_void;
    fn loss_linear_batch_mse_derive_double(r: *const f64, t: *const f64, output: *mut f64, nlen: c_int, batch_size: c_int) -> c_void;
    fn loss_linear_batch_cross_entropy_derive_float(r: *const f32, t: *const f32, output: *mut f32, nlen: c_int, batch_size: c_int) -> c_void;
    fn loss_linear_batch_cross_entropy_derive_double(r: *const f64, t: *const f64, output: *mut f64, nlen: c_int, batch_size: c_int) -> c_void;
    fn loss_linear_batch_cross_entropy_multiclass_derive_float(r: *const f32, t: *const f32, output: *mut f32, nlen: c_int, batch_size: c_int) -> c_void;
    fn loss_linear_batch_cross_entropy_multiclass_derive_double(r: *const f64, t: *const f64, output: *mut f64, nlen: c_int, batch_size: c_int) -> c_void;
}
/// Define a list to be passed to the cuda kernel function during mini-batch execution as the argument of mse.
pub struct LinearBatchMseArgs<'a,T,const N:usize> where T: DataTypeInfo + UnitValue<T> {
    /// expected value
    expected: CudaConstPtr<'a,CudaVecView<'a,T,CudaTensor1dPtr<T,N>>>,
    /// actual value
    actual: CudaConstPtr<'a,CudaVecView<'a,T,CudaTensor1dPtr<T,N>>>,
    pub output: CudaVec<T,CudaTensor1dPtr<T,N>>,
    out_len: usize,
    batch_len: usize,
}
/// Create an instance of an object representing the list of arguments to
/// compute the loss function mse during mini-batch execution.
impl<'a,T,const N:usize> LinearBatchMseArgs<'a,T,N> where T: DataTypeInfo + UnitValue<T> {
    /// Create a LinearBatchMseArgs instance
    /// # Arguments
    /// * `expected` - Expected Value
    /// * `actual` - Actual Value
    /// * `out_len` - Number of scalar values in output
    /// * `batch_len` - batch count
    pub fn new(t:&'a CudaVecView<'a,T,CudaTensor1dPtr<T,N>>,r:&'a CudaVecView<'a,T,CudaTensor1dPtr<T,N>>,
               output: CudaVec<T,CudaTensor1dPtr<T,N>>,out_len:usize,batch_len:usize) -> LinearBatchMseArgs<'a,T,N> {
        LinearBatchMseArgs {
            expected: CudaConstPtr::new(t),
            actual: CudaConstPtr::new(r),
            output: output,
            out_len: out_len,
            batch_len: batch_len
        }
    }
}
impl<'a,T,const N:usize> KernelArgs for LinearBatchMseArgs<'a,T,N> where T: DataTypeInfo + UnitValue<T> {
    fn as_vec(&mut self) -> Vec<&mut dyn AsKernelPtr> {
        vec![
            &mut self.expected,
            &mut self.actual,
            &mut self.output,
            &mut self.out_len,
            &mut self.batch_len
        ]
    }
}
pub struct LinearBatchMse<'a,T,const N:usize> where T: DataTypeInfo + UnitValue<T> {
    t:PhantomData<T>,
    n:PhantomData<[();N]>,
    l:PhantomData<&'a ()>
}
impl<'a,T,const N:usize> LinearBatchMse<'a,T,N> where T: DataTypeInfo + UnitValue<T> {
    /// Create a LinearBatchMse instance
    pub fn new() -> LinearBatchMse<'a,T,N> {
        LinearBatchMse {
            t: PhantomData::<T>,
            n: PhantomData::<[();N]>,
            l: PhantomData::<&'a ()>
        }
    }
}
impl<'a,const N:usize> Kernel for LinearBatchMse<'a,f32,N> {
    const FUNC_PTR: *const c_void = loss_linear_batch_mse_derive_float as *const c_void;
    type Args = LinearBatchMseArgs<'a,f32,N>;
}
impl<'a,const N:usize> Kernel for LinearBatchMse<'a,f64,N> {
    const FUNC_PTR: *const c_void = loss_linear_batch_mse_derive_double as *const c_void;
    type Args = LinearBatchMseArgs<'a,f64,N>;
}
/// Defines the list passed to the cuda kernel function as the argument of mse.
pub struct LinearMseArgs<'a,T,const N:usize> where T: DataTypeInfo + UnitValue<T> {
    /// expected value
    expected: CudaConstPtr<'a,CudaTensor1dPtrView<'a,T,N>>,
    /// actual value
    actual: CudaConstPtr<'a,CudaTensor1dPtrView<'a,T,N>>,
    pub output: CudaTensor1dPtr<T,N>,
    out_len: usize,
    batch_len: usize,
}
/// Create an instance of an object representing the argument list for computing the loss function mse.
impl<'a,T,const N:usize> LinearMseArgs<'a,T,N> where T: DataTypeInfo + UnitValue<T> {
    /// Create a LinearMseArgs instance
    /// # Arguments
    /// * `expected` - Expected Value
    /// * `actual` - Actual Value
    /// * `out_len` - Number of scalar values in output
    pub fn new(t:&'a CudaTensor1dPtrView<'a,T,N>,
               r:&'a CudaTensor1dPtrView<'a,T,N>,
               output: CudaTensor1dPtr<T,N>,
               out_len:usize) -> LinearMseArgs<'a,T,N> {
        LinearMseArgs {
            expected: CudaConstPtr::new(t),
            actual: CudaConstPtr::new(r),
            output: output,
            out_len: out_len,
            batch_len: 1
        }
    }
}
impl<'a,T,const N:usize> KernelArgs for LinearMseArgs<'a,T,N> where T: DataTypeInfo + UnitValue<T> {
    fn as_vec(&mut self) -> Vec<&mut dyn AsKernelPtr> {
        vec![
            &mut self.expected,
            &mut self.actual,
            &mut self.output,
            &mut self.out_len,
            &mut self.batch_len
        ]
    }
}
pub struct LinearMse<'a,T,const N:usize> where T: DataTypeInfo + UnitValue<T> {
    t:PhantomData<T>,
    n:PhantomData<[();N]>,
    l:PhantomData<&'a ()>
}
impl<'a,T,const N:usize> LinearMse<'a,T,N> where T: DataTypeInfo + UnitValue<T> {
    /// Create a LinearMse instance
    pub fn new() -> LinearMse<'a,T,N> {
        LinearMse {
            t: PhantomData::<T>,
            n: PhantomData::<[();N]>,
            l: PhantomData::<&'a ()>
        }
    }
}
impl<'a,const N:usize> Kernel for LinearMse<'a,f32,N> {
    const FUNC_PTR: *const c_void = loss_linear_batch_mse_derive_float as *const c_void;
    type Args = LinearMseArgs<'a,f32,N>;
}
impl<'a,const N:usize> Kernel for LinearMse<'a,f64,N> {
    const FUNC_PTR: *const c_void = loss_linear_batch_mse_derive_double as *const c_void;
    type Args = LinearMseArgs<'a,f64,N>;
}
/// Defines the list that is passed to the cuda kernel function as cross-entropy arguments during mini-batch execution.
pub struct LinearBatchCrossEntropyArgs<'a,T,const N:usize> where T: DataTypeInfo + UnitValue<T> {
    /// expected value
    expected: CudaConstPtr<'a,CudaVecView<'a,T,CudaTensor1dPtr<T,N>>>,
    /// actual value
    actual: CudaConstPtr<'a,CudaVecView<'a,T,CudaTensor1dPtr<T,N>>>,
    pub output: CudaVec<T,CudaTensor1dPtr<T,N>>,
    out_len: usize,
    batch_len: usize,
}
/// Create an instance of an object representing a list of arguments to calculate
/// the result of passing a mini-batch to the loss function cross entropy.
impl<'a,T,const N:usize> LinearBatchCrossEntropyArgs<'a,T,N> where T: DataTypeInfo + UnitValue<T> {
    /// Create a LinearBatchCrossEntropyArgs instance
    /// # Arguments
    /// * `expected` - Expected Value
    /// * `actual` - Actual Value
    /// * `out_len` - Number of scalar values in output
    /// * `batch_len` - batch count
    pub fn new(t:&'a CudaVecView<'a,T,CudaTensor1dPtr<T,N>>,
               r:&'a CudaVecView<'a,T,CudaTensor1dPtr<T,N>>,
               output: CudaVec<T,CudaTensor1dPtr<T,N>>,
               out_len:usize,batch_len:usize) -> LinearBatchCrossEntropyArgs<'a,T,N> {
        LinearBatchCrossEntropyArgs {
            expected: CudaConstPtr::new(t),
            actual: CudaConstPtr::new(r),
            output: output,
            out_len: out_len,
            batch_len: batch_len
        }
    }
}
impl<'a,T,const N:usize> KernelArgs for LinearBatchCrossEntropyArgs<'a,T,N> where T: DataTypeInfo + UnitValue<T> {
    fn as_vec(&mut self) -> Vec<&mut dyn AsKernelPtr> {
        vec![
            &mut self.expected,
            &mut self.actual,
            &mut self.output,
            &mut self.out_len,
            &mut self.batch_len
        ]
    }
}
pub struct LinearBatchCrossEntropy<'a,T,const N:usize> where T: DataTypeInfo + UnitValue<T> {
    t:PhantomData<T>,
    n:PhantomData<[();N]>,
    l:PhantomData<&'a ()>
}
impl<'a,T,const N:usize> LinearBatchCrossEntropy<'a,T,N> where T: DataTypeInfo + UnitValue<T> {
    /// Create a LinearBatchCrossEntropy instance
    pub fn new() -> LinearBatchCrossEntropy<'a,T,N> {
        LinearBatchCrossEntropy {
            t: PhantomData::<T>,
            n: PhantomData::<[();N]>,
            l: PhantomData::<&'a ()>
        }
    }
}
impl<'a,const N:usize> Kernel for LinearBatchCrossEntropy<'a,f32,N> {
    const FUNC_PTR: *const c_void = loss_linear_batch_cross_entropy_derive_float as *const c_void;
    type Args = LinearBatchCrossEntropyArgs<'a,f32,N>;
}
impl<'a,const N:usize> Kernel for LinearBatchCrossEntropy<'a,f64,N> {
    const FUNC_PTR: *const c_void = loss_linear_batch_cross_entropy_derive_double as *const c_void;
    type Args = LinearBatchCrossEntropyArgs<'a,f64,N>;
}
/// Defines the list passed to the cuda kernel function as the argument of cross entropy.
pub struct LinearCrossEntropyArgs<'a,T,const N:usize> where T: DataTypeInfo + UnitValue<T> {
    /// expected value
    expected: CudaConstPtr<'a,CudaTensor1dPtrView<'a,T,N>>,
    /// actual value
    actual: CudaConstPtr<'a,CudaTensor1dPtrView<'a,T,N>>,
    pub output: CudaTensor1dPtr<T,N>,
    out_len: usize,
    batch_len: usize,
}
/// Create an instance of an object representing the argument list for computing the loss function cross entropy.
impl<'a,T,const N:usize> LinearCrossEntropyArgs<'a,T,N> where T: DataTypeInfo + UnitValue<T> {
    /// Create a LinearCrossEntropyArgs instance
    /// # Arguments
    /// * `expected` - Expected Value
    /// * `actual` - Actual Value
    /// * `out_len` - Number of scalar values in output
    pub fn new(t:&'a CudaTensor1dPtrView<'a,T,N>,
               r:&'a CudaTensor1dPtrView<'a,T,N>,
               output: CudaTensor1dPtr<T,N>,
               out_len:usize) -> LinearCrossEntropyArgs<'a, T, N> {
        LinearCrossEntropyArgs {
            expected: CudaConstPtr::new(t),
            actual: CudaConstPtr::new(r),
            output: output,
            out_len: out_len,
            batch_len: 1
        }
    }
}
impl<'a,T,const N:usize> KernelArgs for LinearCrossEntropyArgs<'a,T,N> where T: DataTypeInfo + UnitValue<T> {
    fn as_vec(&mut self) -> Vec<&mut dyn AsKernelPtr> {
        vec![
            &mut self.expected,
            &mut self.actual,
            &mut self.output,
            &mut self.out_len,
            &mut self.batch_len
        ]
    }
}
pub struct LinearCrossEntropy<'a,T,const N:usize> where T: DataTypeInfo + UnitValue<T> {
    t:PhantomData<T>,
    n:PhantomData<[();N]>,
    l:PhantomData<&'a ()>
}
impl<'a,T,const N:usize> LinearCrossEntropy<'a,T,N> where T: DataTypeInfo + UnitValue<T> {
    /// Create a LinearCrossEntropy instance
    pub fn new() -> LinearCrossEntropy<'a,T,N> {
        LinearCrossEntropy {
            t: PhantomData::<T>,
            n: PhantomData::<[();N]>,
            l: PhantomData::<&'a ()>
        }
    }
}
impl<'a,const N:usize> Kernel for LinearCrossEntropy<'a,f32,N> {
    const FUNC_PTR: *const c_void = loss_linear_batch_cross_entropy_derive_float as *const c_void;
    type Args = LinearCrossEntropyArgs<'a,f32,N>;
}
impl<'a,const N:usize> Kernel for LinearCrossEntropy<'a,f64,N> {
    const FUNC_PTR: *const c_void = loss_linear_batch_cross_entropy_derive_double as *const c_void;
    type Args = LinearCrossEntropyArgs<'a,f64,N>;
}
/// Defines the list that is passed to the cuda kernel function as arguments
/// to the croos entropy multiclass during mini-batch execution.
pub struct LinearBatchCrossEntropyMulticlassArgs<'a,T,const N:usize> where T: DataTypeInfo + UnitValue<T> {
    /// expected value
    expected: CudaConstPtr<'a,CudaVecView<'a,T,CudaTensor1dPtr<T,N>>>,
    /// actual value
    actual: CudaConstPtr<'a,CudaVecView<'a,T,CudaTensor1dPtr<T,N>>>,
    pub output: CudaVec<T,CudaTensor1dPtr<T,N>>,
    out_len: usize,
    batch_len: usize,
}
/// Create an instance of an object representing a list of arguments to compute the result of passing a mini-batch
/// to the loss function cross entropy multiclass.
impl<'a,T,const N:usize> LinearBatchCrossEntropyMulticlassArgs<'a,T,N> where T: DataTypeInfo + UnitValue<T> {
    /// Create a LinearBatchCrossEntropyMulticlassArgs instance
    /// # Arguments
    /// * `expected` - Expected Value
    /// * `actual` - Actual Value
    /// * `out_len` - Number of scalar values in output
    /// * `batch_len` - batch count
    pub fn new(t:&'a CudaVecView<'a,T,CudaTensor1dPtr<T,N>>,
               r:&'a CudaVecView<'a,T,CudaTensor1dPtr<T,N>>,
               output: CudaVec<T,CudaTensor1dPtr<T,N>>,
               out_len:usize,batch_len:usize) -> LinearBatchCrossEntropyMulticlassArgs<'a,T,N> {
        LinearBatchCrossEntropyMulticlassArgs {
            expected: CudaConstPtr::new(t),
            actual: CudaConstPtr::new(r),
            output: output,
            out_len: out_len,
            batch_len: batch_len
        }
    }
}
impl<'a,T,const N:usize> KernelArgs for LinearBatchCrossEntropyMulticlassArgs<'a,T,N> where T: DataTypeInfo + UnitValue<T> {
    fn as_vec(&mut self) -> Vec<&mut dyn AsKernelPtr> {
        vec![
            &mut self.expected,
            &mut self.actual,
            &mut self.output,
            &mut self.out_len,
            &mut self.batch_len
        ]
    }
}
pub struct LinearBatchCrossEntropyMulticlass<'a,T,const N:usize> where T: DataTypeInfo + UnitValue<T> {
    t:PhantomData<T>,
    n:PhantomData<[();N]>,
    l:PhantomData<&'a ()>
}
impl<'a,T,const N:usize> LinearBatchCrossEntropyMulticlass<'a,T,N> where T: DataTypeInfo + UnitValue<T> {
    /// Create a LinearBatchCrossEntropyMulticlass instance
    pub fn new() -> LinearBatchCrossEntropyMulticlass<'a,T,N> {
        LinearBatchCrossEntropyMulticlass {
            t: PhantomData::<T>,
            n: PhantomData::<[();N]>,
            l: PhantomData::<&'a ()>
        }
    }
}
impl<'a,const N:usize> Kernel for LinearBatchCrossEntropyMulticlass<'a,f32,N> {
    const FUNC_PTR: *const c_void = loss_linear_batch_cross_entropy_multiclass_derive_float as *const c_void;
    type Args = LinearBatchCrossEntropyMulticlassArgs<'a,f32,N>;
}
impl<'a,const N:usize> Kernel for LinearBatchCrossEntropyMulticlass<'a,f64,N> {
    const FUNC_PTR: *const c_void = loss_linear_batch_cross_entropy_multiclass_derive_double as *const c_void;
    type Args = LinearBatchCrossEntropyMulticlassArgs<'a,f64,N>;
}
/// Defines the list passed to the cuda kernel function as the argument of croos entropy multiclass
pub struct LinearCrossEntropyMulticlassArgs<'a,T,const N:usize> where T: DataTypeInfo + UnitValue<T> {
    /// expected value
    expected: CudaConstPtr<'a,CudaTensor1dPtrView<'a,T,N>>,
    /// actual value
    actual: CudaConstPtr<'a,CudaTensor1dPtrView<'a,T,N>>,
    pub output: CudaTensor1dPtr<T,N>,
    out_len: usize,
    batch_len: usize,
}
/// Create an instance of an object representing the argument list for computing the loss function cross entropy multiclass.
impl<'a,T,const N:usize> LinearCrossEntropyMulticlassArgs<'a,T,N> where T: DataTypeInfo + UnitValue<T> {
    /// Create a LinearCrossEntropyMulticlassArgs instance
    /// # Arguments
    /// * `expected` - Expected Value
    /// * `actual` - Actual Value
    /// * `out_len` - Number of scalar values in output
    pub fn new(t:&'a CudaTensor1dPtrView<T,N>,
               r:&'a CudaTensor1dPtrView<'a,T,N>,
               output: CudaTensor1dPtr<T,N>,
               out_len:usize) -> LinearCrossEntropyMulticlassArgs<'a,T,N> {
        LinearCrossEntropyMulticlassArgs {
            expected: CudaConstPtr::new(t),
            actual: CudaConstPtr::new(r),
            output: output,
            out_len: out_len,
            batch_len: 1
        }
    }
}
impl<'a,T,const N:usize> KernelArgs for LinearCrossEntropyMulticlassArgs<'a,T,N> where T: DataTypeInfo + UnitValue<T> {
    fn as_vec(&mut self) -> Vec<&mut dyn AsKernelPtr> {
        vec![
            &mut self.expected,
            &mut self.actual,
            &mut self.output,
            &mut self.out_len,
            &mut self.batch_len
        ]
    }
}
pub struct LinearCrossEntropyMulticlass<'a,T,const N:usize> where T: DataTypeInfo + UnitValue<T> {
    t:PhantomData<T>,
    n:PhantomData<[();N]>,
    l:PhantomData<&'a ()>
}
impl<'a,T,const N:usize> LinearCrossEntropyMulticlass<'a,T,N> where T: DataTypeInfo + UnitValue<T> {
    /// Create a LinearCrossEntropyMulticlass instance
    pub fn new() -> LinearCrossEntropyMulticlass<'a,T,N> {
        LinearCrossEntropyMulticlass {
            t: PhantomData::<T>,
            n: PhantomData::<[();N]>,
            l: PhantomData::<&'a ()>
        }
    }
}
impl<'a,const N:usize> Kernel for LinearCrossEntropyMulticlass<'a,f32,N> {
    const FUNC_PTR: *const c_void = loss_linear_batch_cross_entropy_multiclass_derive_float as *const c_void;
    type Args = LinearCrossEntropyMulticlassArgs<'a, f32, N>;
}
impl<'a,const N:usize> Kernel for LinearCrossEntropyMulticlass<'a,f64,N> {
    const FUNC_PTR: *const c_void = loss_linear_batch_cross_entropy_multiclass_derive_double as *const c_void;
    type Args = LinearCrossEntropyMulticlassArgs<'a,f64,N>;
}