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
use std::io::{Seek, Write};
use std::sync::Arc;

use failure::{err_msg, Error};
use finalfusion::vocab::VocabWrap;
use finalfusion::{
    embeddings::Embeddings, io::WriteEmbeddings, metadata::Metadata, storage::NdArray,
};
use hogwild::HogwildArray2;
use ndarray::{Array1, Array2, ArrayView1, ArrayView2, ArrayViewMut1, Axis};
use ndarray_rand::RandomExt;
use rand::distributions::Uniform;
use serde::Serialize;
use toml::Value;

use crate::vec_simd::{l2_normalize, scale, scaled_add};
use crate::{CommonConfig, Vocab, WriteModelBinary};

/// Training model.
///
/// Instances of this type represent training models. Training models have
/// an input matrix, an output matrix, and a trainer. The input matrix
/// represents observed inputs, whereas the output matrix represents
/// predicted outputs. The output matrix is typically discarded after
/// training. The trainer holds lexical information, such as word ->
/// index mappings and word discard probabilities. Additionally the trainer
/// provides the logic to transform some input to an iterator of training
/// examples.
///
/// `TrainModel` stores the matrices as `HogwildArray`s to share parameters
/// between clones of the same model. The trainer is also shared between
/// clones due to memory considerations.
#[derive(Clone)]
pub struct TrainModel<T> {
    trainer: T,
    input: HogwildArray2<f32>,
    output: HogwildArray2<f32>,
}

impl<T> From<T> for TrainModel<T>
where
    T: Trainer,
{
    /// Construct a model from a Trainer.
    ///
    /// This randomly initializes the input and output matrices using a
    /// uniform distribution in the range [-1/dims, 1/dims).
    ///
    /// The number of rows of the input matrix is the vocabulary size
    /// plus the number of buckets for subword units. The number of rows
    /// of the output matrix is the number of possible outputs for the model.
    fn from(trainer: T) -> TrainModel<T> {
        let config = *trainer.config();
        let init_bound = 1.0 / config.dims as f32;
        let distribution = Uniform::new_inclusive(-init_bound, init_bound);

        let input = Array2::random(
            (trainer.n_input_types(), config.dims as usize),
            distribution,
        )
        .into();
        let output = Array2::random(
            (trainer.n_output_types(), config.dims as usize),
            distribution,
        )
        .into();
        TrainModel {
            trainer,
            input,
            output,
        }
    }
}

impl<T> TrainModel<T>
where
    T: Trainer,
{
    /// Get the model configuration.
    pub fn config(&self) -> &CommonConfig {
        &self.trainer.config()
    }
}

impl<V, T> TrainModel<T>
where
    T: Trainer<InputVocab = V>,
    V: Vocab,
{
    /// Get this model's input vocabulary.
    pub fn input_vocab(&self) -> &V {
        self.trainer.input_vocab()
    }
}

impl<T> TrainModel<T> {
    /// Get this model's trainer mutably.
    pub fn trainer(&mut self) -> &mut T {
        &mut self.trainer
    }

    /// Get the mean input embedding of the given indices.
    pub(crate) fn mean_input_embedding(&self, indices: &[u64]) -> Array1<f32> {
        Self::mean_embedding(self.input.view(), indices)
    }

    /// Get the mean input embedding of the given indices.
    fn mean_embedding(embeds: ArrayView2<f32>, indices: &[u64]) -> Array1<f32> {
        let mut embed = Array1::zeros((embeds.cols(),));

        for &idx in indices.iter() {
            scaled_add(
                embed.view_mut(),
                embeds.index_axis(Axis(0), idx as usize),
                1.0,
            );
        }

        scale(embed.view_mut(), 1.0 / indices.len() as f32);

        embed
    }

    /// Get the input embedding with the given index.
    #[allow(dead_code)]
    #[inline]
    pub(crate) fn input_embedding(&self, idx: usize) -> ArrayView1<f32> {
        self.input.subview(Axis(0), idx)
    }

    /// Get the input embedding with the given index mutably.
    #[inline]
    pub(crate) fn input_embedding_mut(&mut self, idx: usize) -> ArrayViewMut1<f32> {
        self.input.subview_mut(Axis(0), idx)
    }

    pub(crate) fn into_parts(self) -> Result<(T, Array2<f32>), Error> {
        let input = match Arc::try_unwrap(self.input.into_inner()) {
            Ok(input) => input.into_inner(),
            Err(_) => return Err(err_msg("Cannot unwrap input matrix.")),
        };

        Ok((self.trainer, input))
    }

    /// Get the output embedding with the given index.
    #[inline]
    pub(crate) fn output_embedding(&self, idx: usize) -> ArrayView1<f32> {
        self.output.subview(Axis(0), idx)
    }

    /// Get the output embedding with the given index mutably.
    #[inline]
    pub(crate) fn output_embedding_mut(&mut self, idx: usize) -> ArrayViewMut1<f32> {
        self.output.subview_mut(Axis(0), idx)
    }
}

impl<W, T, V, M> WriteModelBinary<W> for TrainModel<T>
where
    W: Seek + Write,
    T: Trainer<InputVocab = V, Metadata = M>,
    V: Vocab + Into<VocabWrap>,
    M: Serialize,
{
    fn write_model_binary(self, write: &mut W) -> Result<(), Error> {
        let (trainer, mut input_matrix) = self.into_parts()?;

        let metadata = Metadata(Value::try_from(trainer.to_metadata())?);

        // Compute and write word embeddings.
        let mut norms = vec![0f32; trainer.input_vocab().len()];
        for (i, norm) in norms
            .iter_mut()
            .enumerate()
            .take(trainer.input_vocab().len())
        {
            let input = trainer.input_indices(i);
            let mut embed = Self::mean_embedding(input_matrix.view(), &input);
            *norm = l2_normalize(embed.view_mut());
            input_matrix.index_axis_mut(Axis(0), i).assign(&embed);
        }

        let vocab: VocabWrap = trainer.try_into_input_vocab()?.into();
        let storage = NdArray(input_matrix);

        Embeddings::new(Some(metadata), vocab, storage).write_embeddings(write)
    }
}

/// Trainer Trait.
pub trait Trainer {
    type InputVocab: Vocab;
    type Metadata;

    /// Given an input index get all associated indices.
    fn input_indices(&self, idx: usize) -> Vec<u64>;

    /// Get the trainer's input vocabulary.
    fn input_vocab(&self) -> &Self::InputVocab;

    /// Destruct the trainer and get the input vocabulary.
    fn try_into_input_vocab(self) -> Result<Self::InputVocab, Error>;

    /// Get the number of possible input types.
    ///
    /// In a model with subword units this value is calculated as:
    /// `2^n_buckets + input_vocab.len()`.
    fn n_input_types(&self) -> usize;

    /// Get the number of possible outputs.
    ///
    /// In a structured skipgram model this value is calculated as:
    /// `output_vocab.len() * context_size * 2`
    fn n_output_types(&self) -> usize;

    /// Get this Trainer's common hyperparameters.
    fn config(&self) -> &CommonConfig;

    /// Get this Trainer's configuration.
    fn to_metadata(&self) -> Self::Metadata;
}

/// TrainIterFrom.
///
/// This trait defines how some input `&S` is transformed into an iterator of training examples.
pub trait TrainIterFrom<S>
where
    S: ?Sized,
{
    type Iter: Iterator<Item = (usize, Self::Contexts)>;
    type Contexts: Sized + IntoIterator<Item = usize>;

    fn train_iter_from(&mut self, sequence: &S) -> Self::Iter;
}

/// Negative Samples
///
/// This trait defines a method on how to draw a negative sample given some output. The return value
/// should follow the distribution of the underlying output vocabulary.
pub trait NegativeSamples {
    fn negative_sample(&mut self, output: usize) -> usize;
}

#[cfg(test)]
mod tests {
    use ndarray::Array2;
    use rand::FromEntropy;
    use rand_xorshift::XorShiftRng;

    use super::TrainModel;
    use crate::skipgram_trainer::SkipgramTrainer;
    use crate::util::all_close;
    use crate::{
        CommonConfig, LossType, ModelType, SkipGramConfig, SubwordVocab, SubwordVocabConfig,
        VocabBuilder,
    };

    const TEST_COMMON_CONFIG: CommonConfig = CommonConfig {
        dims: 3,
        epochs: 5,
        loss: LossType::LogisticNegativeSampling,
        lr: 0.05,
        negative_samples: 5,
        zipf_exponent: 0.5,
    };

    const TEST_SKIP_CONFIG: SkipGramConfig = SkipGramConfig {
        context_size: 5,
        model: ModelType::SkipGram,
    };

    const VOCAB_CONF: SubwordVocabConfig = SubwordVocabConfig {
        buckets_exp: 21,
        discard_threshold: 1e-4,
        min_count: 2,
        max_n: 6,
        min_n: 3,
    };

    #[test]
    pub fn model_embed_methods() {
        let mut vocab_config = VOCAB_CONF.clone();
        vocab_config.min_count = 1;

        let common_config = TEST_COMMON_CONFIG.clone();
        let skipgram_config = TEST_SKIP_CONFIG.clone();
        // We just need some bogus vocabulary
        let mut builder: VocabBuilder<SubwordVocabConfig, String> = VocabBuilder::new(vocab_config);
        builder.count("bla".to_string());
        let vocab: SubwordVocab = builder.into();

        let input = Array2::from_shape_vec((2, 3), vec![1., 2., 3., 4., 5., 6.])
            .unwrap()
            .into();
        let output = Array2::from_shape_vec((2, 3), vec![-1., -2., -3., -4., -5., -6.])
            .unwrap()
            .into();

        let mut model = TrainModel {
            trainer: SkipgramTrainer::new(
                vocab,
                XorShiftRng::from_entropy(),
                common_config,
                skipgram_config,
            ),
            input,
            output,
        };

        // Input embeddings
        assert!(all_close(
            model.input_embedding(0).as_slice().unwrap(),
            &[1., 2., 3.],
            1e-5
        ));
        assert!(all_close(
            model.input_embedding(1).as_slice().unwrap(),
            &[4., 5., 6.],
            1e-5
        ));

        // Mutable input embeddings
        assert!(all_close(
            model.input_embedding_mut(0).as_slice().unwrap(),
            &[1., 2., 3.],
            1e-5
        ));
        assert!(all_close(
            model.input_embedding_mut(1).as_slice().unwrap(),
            &[4., 5., 6.],
            1e-5
        ));

        // Output embeddings
        assert!(all_close(
            model.output_embedding(0).as_slice().unwrap(),
            &[-1., -2., -3.],
            1e-5
        ));
        assert!(all_close(
            model.output_embedding(1).as_slice().unwrap(),
            &[-4., -5., -6.],
            1e-5
        ));

        // Mutable output embeddings
        assert!(all_close(
            model.output_embedding_mut(0).as_slice().unwrap(),
            &[-1., -2., -3.],
            1e-5
        ));
        assert!(all_close(
            model.output_embedding_mut(1).as_slice().unwrap(),
            &[-4., -5., -6.],
            1e-5
        ));

        // Mean input embedding.
        assert!(all_close(
            model.mean_input_embedding(&[0, 1]).as_slice().unwrap(),
            &[2.5, 3.5, 4.5],
            1e-5
        ));
    }
}