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
use axis::Axis;
use shape::{Shape, ShapeInner};
use device::DeviceDescriptor;
use function::Function;
use std::borrow::Borrow;
use std::ptr;

cpp! {{
  #include <CNTKLibrary.h>
  #include <cstdio>
  #include <vector>

  using namespace CNTK;
  using namespace std;
}}

type ParameterInitializerInner = [u64; 2usize];

pub struct ParameterInitializer {
    payload: ParameterInitializerInner
}

impl ParameterInitializer {
    pub fn constant(value: f64) -> ParameterInitializer {
        ParameterInitializer {payload: unsafe {
            cpp!([value as "double"] -> ParameterInitializerInner as "ParameterInitializer" {
                return ConstantInitializer(value);
            })
        }}
    }

    pub fn uniform(scale: f64) -> ParameterInitializer {
        ParameterInitializer {payload: unsafe {
            cpp!([scale as "double"] -> ParameterInitializerInner as "ParameterInitializer" {
                return UniformInitializer(scale);
            })
        }}
    }

    pub fn normal(scale: f64) -> ParameterInitializer {
        ParameterInitializer {payload: unsafe {
            cpp!([scale as "double"] -> ParameterInitializerInner as "ParameterInitializer" {
                return NormalInitializer(scale);
            })
        }}
    }

    pub fn xavier() -> ParameterInitializer {
        ParameterInitializer {payload: unsafe {
            cpp!([] -> ParameterInitializerInner as "ParameterInitializer" {
                return XavierInitializer();
            })
        }}
    }

    pub fn glorot_uniform() -> ParameterInitializer {
        ParameterInitializer {payload: unsafe {
            cpp!([] -> ParameterInitializerInner as "ParameterInitializer" {
                return GlorotUniformInitializer();
            })
        }}
    }

    pub fn glorot_normal() -> ParameterInitializer {
        ParameterInitializer {payload: unsafe {
            cpp!([] -> ParameterInitializerInner as "ParameterInitializer" {
                return GlorotNormalInitializer();
            })
        }}
    }

    pub fn he_uniform() -> ParameterInitializer {
        ParameterInitializer {payload: unsafe {
            cpp!([] -> ParameterInitializerInner as "ParameterInitializer" {
                return HeUniformInitializer();
            })
        }}
    }

    pub fn he_normal() -> ParameterInitializer {
        ParameterInitializer {payload: unsafe {
            cpp!([] -> ParameterInitializerInner as "ParameterInitializer" {
                return HeNormalInitializer();
            })
        }}
    }
}

pub(super) type VariableInner = [u64; 5usize];

#[derive(Debug)]
pub struct Variable {
    pub(super) payload: VariableInner
}

impl Variable {
    pub fn create<A: Borrow<Axis>>(shape: &Shape, is_sparse: bool, needs_gradient: bool, name: &str, dynamic_axes: &[A]) -> Variable {
        let spayload = shape.payload;
        let name_ptr = name.as_ptr();
        let name_len = name.len();
        let axis_payloads = dynamic_axes.iter().map(|x| x.borrow().payload).collect::<Vec<_>>();
        let axis_ptr = axis_payloads.as_ptr();
        let axis_len = axis_payloads.len();
        Variable { payload: unsafe {
            cpp!([spayload as "NDShape", name_ptr as "char*", name_len as "size_t", is_sparse as "bool", needs_gradient as "bool", axis_ptr as "Axis*", axis_len as "size_t"] -> VariableInner as "Variable" {
                string name(name_ptr, name_ptr + name_len);
                wstring wname;
                wname.assign(name.begin(), name.end());
                return InputVariable(spayload, is_sparse, DataType::Float, needs_gradient, wname, vector<Axis>(axis_ptr, axis_ptr + axis_len));
            })
        }}
    }

    pub fn input_variable(shape: &Shape) -> Variable {
        let spayload = shape.payload;
        Variable { payload: unsafe {
            cpp!([spayload as "NDShape"] -> VariableInner as "Variable" {
                return InputVariable(spayload, DataType::Float);
            })
        }}
    }

    pub fn sparse_input_variable(shape: &Shape) -> Variable {
        let spayload = shape.payload;
        Variable { payload: unsafe {
            cpp!([spayload as "NDShape"] -> VariableInner as "Variable" {
                return InputVariable(spayload, true, DataType::Float);
            })
        }}
    }

    pub fn input_variable_with_name(shape: &Shape, name: &str) -> Variable {
        let spayload = shape.payload;
        let name_ptr = name.as_ptr();
        let name_len = name.len();
        Variable { payload: unsafe {
            cpp!([spayload as "NDShape", name_ptr as "char*", name_len as "size_t"] -> VariableInner as "Variable" {
                string name(name_ptr, name_ptr + name_len);
                wstring wname;
                wname.assign(name.begin(), name.end());
                return InputVariable(spayload, DataType::Float, wname);
            })
        }}
    }

    pub fn input_variable_with_gradient(shape: &Shape) -> Variable {
        let spayload = shape.payload;
        Variable { payload: unsafe {
            cpp!([spayload as "NDShape"] -> VariableInner as "Variable" {
                return InputVariable(spayload, DataType::Float, true);
            })
        }}
    }

    pub fn parameter(shape: &Shape, initializer: &ParameterInitializer, device: DeviceDescriptor) -> Variable {
        let spayload = shape.payload;
        let dpayload = device.payload;
        let initializerpayload = initializer.payload;
        Variable { payload: unsafe {
            cpp!([spayload as "NDShape", dpayload as "DeviceDescriptor", initializerpayload as "ParameterInitializer"] -> VariableInner as "Variable" {
                return Parameter(spayload, DataType::Float, initializerpayload, dpayload);
            })
        }}
    }

    pub fn placeholder(shape: &Shape) -> Variable {
        let spayload = shape.payload;
        Variable { payload: unsafe {
            cpp!([spayload as "NDShape"] -> VariableInner as "Variable" {
                return PlaceholderVariable(spayload);
            })
        }}
    }

    pub fn constant_scalar(value: f32) -> Variable {
        Variable { payload: unsafe {
            cpp!([value as "float"] -> VariableInner as "Variable" {
                return Constant::Scalar(DataType::Float, value);
            })
        }}
    }

    pub fn constant_repeat(shape: &Shape, value: f32) -> Variable {
        let spayload = shape.payload;
        Variable { payload: unsafe {
            cpp!([spayload as "NDShape", value as "float"] -> VariableInner as "Variable" {
                return Constant(spayload, DataType::Float, value);
            })
        }}
    }

    pub fn constant_from_slice(shape: &Shape, value: &[f32], device: DeviceDescriptor) -> Variable {
        let spayload = shape.payload;
        let value_ptr = value.as_ptr();
        let value_len = value.len();
        let dpayload = device.payload;
        Variable { payload: unsafe {
            cpp!([spayload as "NDShape", value_ptr as "float*", value_len as "size_t", dpayload as "DeviceDescriptor"] -> VariableInner as "Variable" {
                return Constant(MakeSharedObject<NDArrayView>(spayload, value_ptr, value_len, dpayload, true)->DeepClone());
            })
        }}
    }

    pub fn shape(&self) -> Shape {
        let payload = self.payload;
        Shape { payload: unsafe {
            cpp!([payload as "Variable"] -> ShapeInner as "NDShape" {
                return payload.Shape();
            })
        }}
    }

    pub fn is_parameter(&self) -> bool {
        let payload = self.payload;
        unsafe {
            cpp!([payload as "Variable"] -> bool as "bool" {
                return payload.IsParameter();
            })
        }
    }

    pub fn name(&self) -> String {
        let payload = self.payload;
        let name_size = unsafe {
            cpp!([payload as "Variable"] -> usize as "size_t" {
                auto wname = payload.Name();
                string name(wname.begin(), wname.end());
                return name.size();
            })
        };
        let mut bytes = Vec::with_capacity(name_size);
        unsafe {
            bytes.set_len(name_size);
            let mut ptr = bytes.as_mut_ptr();
            cpp!([payload as "Variable", mut ptr as "char*"] {
                auto wname = payload.Name();
                string name(wname.begin(), wname.end());
                copy(name.begin(), name.end(), ptr);
            })
        }
        String::from_utf8(bytes).unwrap()
    }

    pub fn normal_random(x: &Shape, mean: f64, scale: f64) -> Variable {
        let xpayload = x.payload;
        let payload = unsafe {
            cpp!([xpayload as "NDShape", mean as "double", scale as "double"] -> VariableInner as "Variable" {
            return NormalRandom(xpayload, DataType::Float, mean, scale);
        })
        };
        Variable {payload}
    }

    pub fn bernoulli_random(x: &Shape, mean: f64) -> Variable {
        let xpayload = x.payload;
        let payload = unsafe {
            cpp!([xpayload as "NDShape", mean as "double"] -> VariableInner as "Variable" {
            return BernoulliRandom(xpayload, DataType::Float, mean);
        })
        };
        Variable {payload}
    }

    pub fn uniform_random(x: &Shape, low: f64, high: f64) -> Variable {
        let xpayload = x.payload;
        let payload = unsafe {
            cpp!([xpayload as "NDShape", low as "double", high as "double"] -> VariableInner as "Variable" {
            return UniformRandom(xpayload, DataType::Float, low, high);
        })
        };
        Variable {payload}
    }

    pub fn gumbel_random(x: &Shape, loc: f64, scale: f64) -> Variable {
        let xpayload = x.payload;
        let payload = unsafe {
            cpp!([xpayload as "NDShape", loc as "double", scale as "double"] -> VariableInner as "Variable" {
            return GumbelRandom(xpayload, DataType::Float, loc, scale);
        })
        };
        Variable {payload}
    }

    pub fn parameter_to_vec(&self) -> Vec<f32> {
        assert!(self.is_parameter());
        let payload = self.payload;
        let total_size = unsafe {
            cpp!([payload as "Parameter"] -> usize as "size_t" {
                return payload.Value()->Shape().TotalSize();
            })
        };
        let mut buffer: Vec<f32> = Vec::with_capacity(total_size);
        unsafe { buffer.set_len(total_size); }
        let data = unsafe {
            cpp!([payload as "Parameter"] -> *const f32 as "const float*" {
              return payload.Value()->DataBuffer<float>();
            })
        };
        unsafe {
            ptr::copy(data, buffer.as_mut_ptr(), total_size);
        }
        buffer
    }
}

impl Clone for Variable {
    fn clone(&self) -> Self {
        let xpayload = self.payload;
        let payload = unsafe {
            cpp!([xpayload as "Variable"] -> VariableInner as "Variable" {
                return xpayload;
            })
        };
        Variable {payload}
    }
}

impl Drop for Variable {
    fn drop(&mut self) {
        let payload = self.payload;
        unsafe {
            cpp!([payload as "Variable"] {
                payload.~Variable();
            })
        };
    }
}

impl Drop for ParameterInitializer {
    fn drop(&mut self) {
        let payload = self.payload;
        unsafe {
            cpp!([payload as "ParameterInitializer"] {
                payload.~ParameterInitializer();
            })
        };
    }
}

impl<T: Borrow<Function>> From<T> for Variable {
    fn from(f: T) -> Variable {
        f.borrow().to_variable().unwrap()
    }
}

impl<'a> From<&'a Variable> for Variable {
    fn from(f: &'a Variable) -> Variable {
        f.clone()
    }
}