finalfrontier 0.9.4

Train/use word embeddings with subword units
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
use std::cmp;
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter};
use std::path::{Path, PathBuf};
use std::thread;
use std::time::Duration;

use anyhow::{Context, Result};
use clap::{App, Arg, ArgMatches};
use conllu::graph::{Node, Sentence};
use conllu::io::{ReadSentence, Reader, Sentences};
use conllu::proj::{HeadProjectivizer, Projectivize};
use finalfrontier::io::{thread_data_conllu, FileProgress, TrainInfo};
use finalfrontier::{
    BucketIndexerType, CommonConfig, Cutoff, DepembedsConfig, DepembedsTrainer, Dependency,
    DependencyIterator, SimpleVocab, SimpleVocabConfig, SubwordVocab, Vocab, VocabBuilder,
    WriteModelBinary, SGD,
};
use finalfusion::compat::fasttext::FastTextIndexer;
use finalfusion::prelude::VocabWrap;
use finalfusion::subword::FinalfusionHashIndexer;
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;
use serde::Serialize;

use crate::subcommands::{cutoff_from_matches, show_progress, FinalfrontierApp, VocabConfig};

static CONTEXT_MINCOUNT: &str = "context-mincount";
static CONTEXT_TARGET_SIZE: &str = "context-target-size";
static CONTEXT_DISCARD: &str = "context-discard";
static DEPENDENCY_DEPTH: &str = "dependency-depth";
static UNTYPED_DEPS: &str = "untyped";
static NORMALIZE_CONTEXT: &str = "normalize";
static PROJECTIVIZE: &str = "projectivize";
static USE_ROOT: &str = "use-root";

const PROGRESS_UPDATE_INTERVAL: u64 = 200;

/// Dependency embeddings subcommand.
pub struct DepsApp {
    train_info: TrainInfo,
    common_config: CommonConfig,
    depembeds_config: DepembedsConfig,
    input_vocab_config: VocabConfig,
    output_vocab_config: SimpleVocabConfig,
}

impl DepsApp {
    fn depembeds_config_from_matches(matches: &ArgMatches) -> Result<DepembedsConfig> {
        let depth = matches
            .value_of(DEPENDENCY_DEPTH)
            .map(|v| v.parse().context("Cannot parse dependency depth"))
            .transpose()?
            .unwrap();
        let untyped = matches.is_present(UNTYPED_DEPS);
        let normalize = matches.is_present(NORMALIZE_CONTEXT);
        let projectivize = matches.is_present(PROJECTIVIZE);
        let use_root = matches.is_present(USE_ROOT);

        Ok(DepembedsConfig {
            depth,
            untyped,
            normalize,
            projectivize,
            use_root,
        })
    }

    /// Get the corpus path.
    pub fn corpus(&self) -> &str {
        self.train_info().corpus()
    }

    /// Get the output path.
    pub fn output(&self) -> &str {
        self.train_info().output()
    }

    /// Get the number of threads.
    pub fn n_threads(&self) -> usize {
        self.train_info().n_threads()
    }

    /// Get the common config.
    pub fn common_config(&self) -> CommonConfig {
        self.common_config
    }

    /// Get the depembeds config.
    pub fn depembeds_config(&self) -> DepembedsConfig {
        self.depembeds_config
    }

    /// Get the input vocab config.
    pub fn input_vocab_config(&self) -> VocabConfig {
        self.input_vocab_config
    }

    /// Get the output vocab config.
    pub fn output_vocab_config(&self) -> SimpleVocabConfig {
        self.output_vocab_config
    }

    /// Get the train information.
    pub fn train_info(&self) -> &TrainInfo {
        &self.train_info
    }
}

impl FinalfrontierApp for DepsApp {
    fn app() -> App<'static, 'static> {
        Self::common_opts("deps")
            .about("Train a dependency embeddings model")
            .arg(
                Arg::with_name(CONTEXT_DISCARD)
                    .long("context-discard")
                    .value_name("CONTEXT_THRESHOLD")
                    .help("Context discard threshold")
                    .takes_value(true)
                    .default_value("1e-4"),
            )
            .arg(
                Arg::with_name(CONTEXT_MINCOUNT)
                    .long("context-mincount")
                    .value_name("CONTEXT_FREQ")
                    .help("Context mincount. Default: 5")
                    .takes_value(true)
                    .conflicts_with(CONTEXT_TARGET_SIZE),
            )
            .arg(
                Arg::with_name(CONTEXT_TARGET_SIZE)
                    .long("context-target-size")
                    .value_name("CONTEXT_TARGET_SIZE")
                    .help("Context vocab target size")
                    .takes_value(true),
            )
            .arg(
                Arg::with_name(DEPENDENCY_DEPTH)
                    .long("dependency-depth")
                    .value_name("DEPENDENCY_DEPTH")
                    .help("Dependency depth")
                    .takes_value(true)
                    .default_value("1"),
            )
            .arg(
                Arg::with_name(UNTYPED_DEPS)
                    .long("untyped-deps")
                    .help("Don't use dependency relation labels."),
            )
            .arg(
                Arg::with_name(NORMALIZE_CONTEXT)
                    .long("normalize-context")
                    .help("Normalize contexts"),
            )
            .arg(
                Arg::with_name(PROJECTIVIZE)
                    .long("projectivize")
                    .help("Projectivize dependency graphs before training."),
            )
            .arg(
                Arg::with_name(USE_ROOT)
                    .long("use-root")
                    .help("Use root when extracting dependency contexts."),
            )
    }

    fn parse(matches: &ArgMatches) -> Result<Self> {
        let corpus = matches.value_of(Self::CORPUS).unwrap().into();
        let output = matches.value_of(Self::OUTPUT).unwrap().into();
        let n_threads = matches
            .value_of(Self::THREADS)
            .map(|v| v.parse().context("Cannot parse number of threads"))
            .transpose()?
            .unwrap_or_else(|| cmp::min(num_cpus::get() / 2, 20));

        let discard_threshold = matches
            .value_of(CONTEXT_DISCARD)
            .map(|v| v.parse().context("Cannot parse discard threshold"))
            .transpose()?
            .unwrap();
        let cutoff = cutoff_from_matches(matches, CONTEXT_MINCOUNT, CONTEXT_TARGET_SIZE)?
            .unwrap_or_else(|| Cutoff::MinCount(5));

        let output_vocab_config = SimpleVocabConfig {
            cutoff,
            discard_threshold,
        };
        let train_info = TrainInfo::new(corpus, output, n_threads);
        let common_config = Self::parse_common_config(&matches)?;

        Ok(DepsApp {
            train_info,
            common_config,
            depembeds_config: Self::depembeds_config_from_matches(&matches)?,
            input_vocab_config: Self::parse_vocab_config(common_config, &matches)?,
            output_vocab_config,
        })
    }

    fn run(&self) -> Result<()> {
        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        eprintln!("SIMD features: {}", Self::simd_features().join(" "));

        match self.input_vocab_config() {
            VocabConfig::SimpleVocab(config) => {
                let (input_vocab, output_vocab) = build_vocab::<_, SimpleVocab<String>, _>(
                    config,
                    self.output_vocab_config(),
                    self.depembeds_config(),
                    self.corpus(),
                )?;
                train(input_vocab, output_vocab, self)?;
            }
            VocabConfig::SubwordVocab(config) => match config.indexer.indexer_type {
                BucketIndexerType::Finalfusion => {
                    let (input_vocab, output_vocab) =
                        build_vocab::<_, SubwordVocab<_, FinalfusionHashIndexer>, _>(
                            config,
                            self.output_vocab_config(),
                            self.depembeds_config(),
                            self.corpus(),
                        )?;
                    train(input_vocab, output_vocab, self)?
                }
                BucketIndexerType::FastText => {
                    let (input_vocab, output_vocab) =
                        build_vocab::<_, SubwordVocab<_, FastTextIndexer>, _>(
                            config,
                            self.output_vocab_config(),
                            self.depembeds_config(),
                            self.corpus(),
                        )?;
                    train(input_vocab, output_vocab, self)?;
                }
            },
            VocabConfig::NGramVocab(config) => {
                let (input_vocab, output_vocab) = build_vocab::<_, SubwordVocab<_, _>, _>(
                    config,
                    self.output_vocab_config(),
                    self.depembeds_config(),
                    self.corpus(),
                )?;
                train(input_vocab, output_vocab, self)?;
            }
        }

        Ok(())
    }
}

fn train<V>(input_vocab: V, output_vocab: SimpleVocab<Dependency>, app: &DepsApp) -> Result<()>
where
    V: Vocab<VocabType = String> + Into<VocabWrap> + Clone + Send + Sync + 'static,
    V::Config: Serialize,
    for<'a> &'a V::IdxType: IntoIterator<Item = u64>,
{
    let corpus = app.corpus();
    let common_config = app.common_config();
    let n_threads = app.n_threads();

    let mut output_writer =
        BufWriter::new(File::create(app.output()).context("Cannot open output file for writing.")?);
    let trainer = DepembedsTrainer::new(
        input_vocab,
        output_vocab,
        app.common_config(),
        app.depembeds_config(),
        XorShiftRng::from_entropy(),
    );
    let sgd = SGD::new(trainer.into());

    let projectivize = app.depembeds_config().projectivize;
    let mut children = Vec::with_capacity(n_threads);
    for thread in 0..n_threads {
        let corpus = corpus.to_owned();
        let sgd = sgd.clone();

        children.push(thread::spawn(move || {
            do_work(
                corpus,
                sgd,
                thread,
                n_threads,
                common_config.epochs,
                common_config.lr,
                projectivize,
            )
        }));
    }

    show_progress(
        &app.common_config(),
        &sgd,
        Duration::from_millis(PROGRESS_UPDATE_INTERVAL),
    );

    // Wait until all threads have finished.
    for child in children {
        child.join().expect("Thread panicked")?;
    }

    sgd.into_model()
        .write_model_binary(
            &mut output_writer,
            app.train_info().clone(),
            app.common_config.format,
        )
        .context("Cannot write model")
}

fn do_work<P, R, V>(
    corpus_path: P,
    mut sgd: SGD<DepembedsTrainer<R, V>>,
    thread: usize,
    n_threads: usize,
    epochs: u32,
    start_lr: f32,
    projectivize: bool,
) -> Result<()>
where
    P: Into<PathBuf>,
    R: Clone + Rng,
    V: Vocab<VocabType = String>,
    V::Config: Serialize,
    for<'a> &'a V::IdxType: IntoIterator<Item = u64>,
{
    let n_tokens = sgd.model().input_vocab().n_types();

    let f = File::open(corpus_path.into()).context("Cannot open corpus for reading")?;
    let (data, start) =
        thread_data_conllu(&f, thread, n_threads).context("Could not get thread-specific data")?;
    let projectivizer = if projectivize {
        Some(HeadProjectivizer::new())
    } else {
        None
    };

    let mut sentences = SentenceIter::new(BufReader::new(&data[start..]), projectivizer);
    while sgd.n_tokens_processed() < epochs as usize * n_tokens {
        let sentence = sentences
            .next()
            .or_else(|| {
                sentences = SentenceIter::new(BufReader::new(&*data), projectivizer);
                sentences.next()
            })
            .transpose()?
            .context("Cannot read sentence")?;

        let lr = (1.0 - (sgd.n_tokens_processed() as f32 / (epochs as usize * n_tokens) as f32))
            * start_lr;
        sgd.update_sentence(&sentence, lr);
    }

    Ok(())
}

fn build_vocab<P, V, C>(
    input_config: C,
    output_config: SimpleVocabConfig,
    dep_config: DepembedsConfig,
    corpus_path: P,
) -> Result<(V, SimpleVocab<Dependency>)>
where
    P: AsRef<Path>,
    V: Vocab<VocabType = String> + From<VocabBuilder<C, String>>,
    VocabBuilder<C, String>: Into<V>,
{
    let f = File::open(corpus_path).context("Cannot open corpus for reading")?;
    let file_progress = FileProgress::new(f).context("Cannot create progress bar")?;
    let mut input_builder = VocabBuilder::new(input_config);
    let mut output_builder: VocabBuilder<_, Dependency> = VocabBuilder::new(output_config);

    let projectivizer = if dep_config.projectivize {
        Some(HeadProjectivizer::new())
    } else {
        None
    };

    for sentence in SentenceIter::new(BufReader::new(file_progress), projectivizer) {
        let sentence = sentence?;

        for token in sentence.iter().filter_map(Node::token) {
            input_builder.count(token.form());
        }

        for (_, context) in DependencyIterator::new_from_config(&sentence.dep_graph(), dep_config) {
            output_builder.count(context);
        }
    }

    Ok((input_builder.into(), output_builder.into()))
}

struct SentenceIter<P, R>
where
    R: ReadSentence,
{
    inner: Sentences<R>,
    projectivizer: Option<P>,
}

impl<P, R> SentenceIter<P, Reader<R>>
where
    R: BufRead,
{
    fn new(read: R, projectivizer: Option<P>) -> Self {
        SentenceIter {
            inner: Reader::new(read).into_iter(),
            projectivizer,
        }
    }
}

impl<P, R> Iterator for SentenceIter<P, R>
where
    P: Projectivize,
    R: ReadSentence,
{
    type Item = Result<Sentence>;

    fn next(&mut self) -> Option<Self::Item> {
        let sentence = self.inner.next()?;
        let mut sentence = match sentence.context("Cannot read sentence") {
            Ok(sentence) => sentence,
            err @ Err(_) => return Some(err),
        };

        if let Some(proj) = &self.projectivizer {
            // Rewrap error.
            if let Err(err) = proj.projectivize(&mut sentence) {
                return Some(Err(err).context("Cannot projectivize sentence."));
            }
        }

        Some(Ok(sentence))
    }
}