Welcome to mirror list, hosted at ThFree Co, Russian Federation.

aliases.cpp « common « src - github.com/marian-nmt/marian.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0be26a8c80bc0ead341100b7741310bc38627dc2 (plain)
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
#include "common/config_parser.h"
#include "common/definitions.h"

namespace marian {

/**
 * Add all aliases
 *
 * An alias is a command line option name and value pair that sets multiple other non-alias 
 * (standard) command line options. And example would be `--task transformer-big` which -- 
 * as a whole -- is an alias for setting options and hyperparameters that would be reasonable 
 * for training a Google-style Transformer-Big model. Below key-value pairs 
 * ("task", "transformer-base") and ("task", "transformer-big") are different aliases that result
 * in different option sets to be defined.
 * 
 * The alias option has to be first defined using cli.add<T>(). Defining
 * multiple aliases for the same option name but with different values is allowed.
 *
 * As aliases are key-value pairs by default, values are compared as std::string. 
 * If the command line option corresponding to the alias is a vector, the alias 
 * will be triggered if the requested value exists in that vector at least once.
 *
 * @see CLIWrapper::alias()
 *
 * The order of alias definitions *does* matter: options from an alias defined later override
 * options defined in earlier aliases regardless of their order in the command line or config file.
 */
void ConfigParser::addAliases(cli::CLIWrapper& cli) {
  cli.alias("fp16", "true", [&](YAML::Node& config) {
    if(mode_ == cli::mode::training) {
      config["precision"] = std::vector<std::string>({"float16", "float32"}); // inference type, optimization type, save type
      // scaling factor (power of 2), frequency, multiplier at increase, tolerance, range, minium factor
      config["cost-scaling"] = std::vector<std::string>({"0", "1000", "2", "0.05", "10", "1e-5"}); 
    } else {
      config["precision"] = std::vector<std::string>({"float16"}); // for inference we do not need the other types
    }
  });

  if(mode_ == cli::mode::training) {
    // for backwards-compatibility with older version, "--no-shuffle" maps to "--shuffle none"
    cli.alias("no-shuffle", "true", [](YAML::Node& config) {
      config["shuffle"] = "none";
    });

    // Options setting the BiDeep architecture proposed in http://www.aclweb.org/anthology/W17-4710
    cli.alias("best-deep", "true", [](YAML::Node& config) {
      config["layer-normalization"] = true;
      config["tied-embeddings"] = true;
      config["enc-type"] = "alternating";
      config["enc-cell-depth"] = 2;
      config["enc-depth"] = 4;
      config["dec-cell-base-depth"] = 4;
      config["dec-cell-high-depth"] = 2;
      config["dec-depth"] = 4;
      config["skip"] = true;

      // Training specific options
      config["learn-rate"] = 0.0003;
      config["cost-type"] = "ce-mean-words";
      config["lr-decay-inv-sqrt"] = 16000;
      config["label-smoothing"] = 0.1;
      config["clip-norm"] = 0;
      config["sync-sgd"] = true;
      config["exponential-smoothing"] = 1e-4;
      config["mini-batch-fit"] = true;
      config["mini-batch"] = 1000;
      config["maxi-batch"] = 1000;
      // config["workspace"] = 6500;
    });

    // Architecture and proposed training settings for a Transformer "base" model introduced in
    // https://papers.nips.cc/paper/7181-attention-is-all-you-need.pdf
    cli.alias("task", "transformer-base", [](YAML::Node& config) {
      // Model options
      config["type"] = "transformer";
      config["enc-depth"] = 6;
      config["dec-depth"] = 6;
      config["dim-emb"] = 512;
      config["tied-embeddings-all"] = true;
      config["transformer-dim-ffn"] = 2048;
      config["transformer-heads"] = 8;
      config["transformer-postprocess"] = "dan";
      config["transformer-preprocess"] = "";
      config["transformer-ffn-activation"] = "relu";
      config["transformer-dropout"] = 0.1;

      // Training specific options
      config["learn-rate"] = 0.0003;
      config["cost-type"] = "ce-mean-words";
      config["lr-warmup"] = 16000;
      config["lr-decay-inv-sqrt"] = 16000;
      config["label-smoothing"] = 0.1;
      config["clip-norm"] = 0;
      config["sync-sgd"] = true;
      config["exponential-smoothing"] = 1e-4;
      config["max-length"] = 100;
      config["mini-batch-fit"] = true;
      config["mini-batch"] = 1000;
      config["maxi-batch"] = 1000;
      config["workspace"] = 9500;
      config["optimizer-params"] = std::vector<float>({0.9f, 0.98f, 1e-09f});

      // Validation specific options
      config["beam-size"] = 8;
      config["valid-mini-batch"] = 16;
      config["normalize"] = 1.0;
    });

    // Architecture and proposed training settings for a Transformer "big" model introduced in
    // https://papers.nips.cc/paper/7181-attention-is-all-you-need.pdf
    cli.alias("task", "transformer-big", [](YAML::Node& config) {
      // Model options
      config["type"] = "transformer";
      config["enc-depth"] = 6;
      config["dec-depth"] = 6;
      config["dim-emb"] = 1024;
      config["tied-embeddings-all"] = true;
      config["transformer-dim-ffn"] = 4096;
      config["transformer-heads"] = 16;
      config["transformer-postprocess"] = "dan";
      config["transformer-preprocess"] = "";
      config["transformer-ffn-activation"] = "relu";
      config["transformer-dropout"] = 0.1;

      // Training specific options
      config["learn-rate"] = 0.0002;
      config["cost-type"] = "ce-mean-words";
      config["lr-warmup"] = 8000;
      config["lr-decay-inv-sqrt"] = 8000;
      config["label-smoothing"] = 0.1;
      config["clip-norm"] = 0;
      config["sync-sgd"] = true;
      config["exponential-smoothing"] = 1e-4;
      config["max-length"] = 100;
      config["mini-batch-fit"] = true;
      config["mini-batch"] = 1000;
      config["maxi-batch"] = 1000;
      config["workspace"] = 13000;
      config["optimizer-params"] = std::vector<float>({0.9f, 0.998f, 1e-09f});

      // Validation specific options
      config["beam-size"] = 8;
      config["valid-mini-batch"] = 8;
      config["normalize"] = 1.0;
    });

    // Transformer base variant with "prenorm" (i.e. the layer normalization is performed as the first block-wise
    // preprocessing step). This also requires to normalize the final output of a transformer stack to avoid the 
    // activations to blow up. This blow up is particularly nasty with mixed precision training.
    // See implementation and comments in tensor2tensor: 
    // * https://github.com/tensorflow/tensor2tensor/blob/95d021477272c10af15cd62f25b595ad16ad514e/tensor2tensor/models/transformer.py#L1845
    // * https://github.com/tensorflow/tensor2tensor/commit/f5c9b17e617ea9179b7d84d36b1e8162cb369f25#diff-4e58a582cf11ca649e76b4362d69e405R78
    cli.alias("task", "transformer-base-prenorm", [](YAML::Node& config) {
      // Model options
      config["type"] = "transformer";
      config["enc-depth"] = 6;
      config["dec-depth"] = 6;
      config["dim-emb"] = 512;
      config["tied-embeddings-all"] = true;
      config["transformer-dim-ffn"] = 2048;
      config["transformer-heads"] = 8;
      config["transformer-postprocess"] = "da";     // change from transformer-base is "dan" -> "da"
      config["transformer-preprocess"] = "n";       // change from transformer-base is "" -> "n"
      config["transformer-postprocess-top"] = "n";  // change from transformer-base is "" -> "n"
      config["transformer-ffn-activation"] = "relu";
      config["transformer-dropout"] = 0.1;

      // Training specific options
      config["learn-rate"] = 0.0003;
      config["cost-type"] = "ce-mean-words";
      config["lr-warmup"] = 16000;
      config["lr-decay-inv-sqrt"] = 16000;
      config["label-smoothing"] = 0.1;
      config["clip-norm"] = 0;
      config["sync-sgd"] = true;
      config["exponential-smoothing"] = 1e-4;
      config["max-length"] = 100;
      config["mini-batch-fit"] = true;
      config["mini-batch"] = 1000;
      config["maxi-batch"] = 1000;
      config["workspace"] = 9500;
      config["optimizer-params"] = std::vector<float>({0.9f, 0.98f, 1e-09f});

      // Validation specific options
      config["beam-size"] = 8;
      config["valid-mini-batch"] = 16;
      config["normalize"] = 1.0;
    });

    // Transformer big variant with "prenorm". Same changes as above.
    cli.alias("task", "transformer-big-prenorm", [](YAML::Node& config) {
      // Model options
      config["type"] = "transformer";
      config["enc-depth"] = 6;
      config["dec-depth"] = 6;
      config["dim-emb"] = 1024;
      config["tied-embeddings-all"] = true;
      config["transformer-dim-ffn"] = 4096;
      config["transformer-heads"] = 16;
      config["transformer-postprocess"] = "da";     // change from transformer-big is "dan" -> "da"
      config["transformer-preprocess"] = "n";       // change from transformer-big is "" -> "n"
      config["transformer-postprocess-top"] = "n";  // change from transformer-big is "" -> "n"
      config["transformer-ffn-activation"] = "relu";
      config["transformer-dropout"] = 0.1;

      // Training specific options
      config["learn-rate"] = 0.0002;
      config["cost-type"] = "ce-mean-words";
      config["lr-warmup"] = 8000;
      config["lr-decay-inv-sqrt"] = 8000;
      config["label-smoothing"] = 0.1;
      config["clip-norm"] = 0;
      config["sync-sgd"] = true;
      config["exponential-smoothing"] = 1e-4;
      config["max-length"] = 100;
      config["mini-batch-fit"] = true;
      config["mini-batch"] = 1000;
      config["maxi-batch"] = 1000;
      config["workspace"] = 13000;
      config["optimizer-params"] = std::vector<float>({0.9f, 0.998f, 1e-09f});

      // Validation specific options
      config["beam-size"] = 8;
      config["valid-mini-batch"] = 8;
      config["normalize"] = 1.0;
    });
  }
}

}  // namespace marian