Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_binaries.py 13 KB

You have to be logged in to leave a comment. Sign In
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
  1. # Copyright (c) 2017-present, Facebook, Inc.
  2. # All rights reserved.
  3. #
  4. # This source code is licensed under the license found in the LICENSE file in
  5. # the root directory of this source tree. An additional grant of patent rights
  6. # can be found in the PATENTS file in the same directory.
  7. import contextlib
  8. from io import StringIO
  9. import os
  10. import random
  11. import sys
  12. import tempfile
  13. import unittest
  14. import torch
  15. from fairseq import options
  16. import preprocess
  17. import train
  18. import generate
  19. import interactive
  20. import eval_lm
  21. class TestTranslation(unittest.TestCase):
  22. def test_fconv(self):
  23. with contextlib.redirect_stdout(StringIO()):
  24. with tempfile.TemporaryDirectory('test_fconv') as data_dir:
  25. create_dummy_data(data_dir)
  26. preprocess_translation_data(data_dir)
  27. train_translation_model(data_dir, 'fconv_iwslt_de_en')
  28. generate_main(data_dir)
  29. def test_raw(self):
  30. with contextlib.redirect_stdout(StringIO()):
  31. with tempfile.TemporaryDirectory('test_fconv_raw') as data_dir:
  32. create_dummy_data(data_dir)
  33. preprocess_translation_data(data_dir, ['--output-format', 'raw'])
  34. train_translation_model(data_dir, 'fconv_iwslt_de_en', ['--raw-text'])
  35. generate_main(data_dir, ['--raw-text'])
  36. def test_fp16(self):
  37. with contextlib.redirect_stdout(StringIO()):
  38. with tempfile.TemporaryDirectory('test_fp16') as data_dir:
  39. create_dummy_data(data_dir)
  40. preprocess_translation_data(data_dir)
  41. train_translation_model(data_dir, 'fconv_iwslt_de_en', ['--fp16'])
  42. generate_main(data_dir)
  43. def test_memory_efficient_fp16(self):
  44. with contextlib.redirect_stdout(StringIO()):
  45. with tempfile.TemporaryDirectory('test_memory_efficient_fp16') as data_dir:
  46. create_dummy_data(data_dir)
  47. preprocess_translation_data(data_dir)
  48. train_translation_model(data_dir, 'fconv_iwslt_de_en', ['--memory-efficient-fp16'])
  49. generate_main(data_dir)
  50. def test_update_freq(self):
  51. with contextlib.redirect_stdout(StringIO()):
  52. with tempfile.TemporaryDirectory('test_update_freq') as data_dir:
  53. create_dummy_data(data_dir)
  54. preprocess_translation_data(data_dir)
  55. train_translation_model(data_dir, 'fconv_iwslt_de_en', ['--update-freq', '3'])
  56. generate_main(data_dir)
  57. def test_max_positions(self):
  58. with contextlib.redirect_stdout(StringIO()):
  59. with tempfile.TemporaryDirectory('test_max_positions') as data_dir:
  60. create_dummy_data(data_dir)
  61. preprocess_translation_data(data_dir)
  62. with self.assertRaises(Exception) as context:
  63. train_translation_model(
  64. data_dir, 'fconv_iwslt_de_en', ['--max-target-positions', '5'],
  65. )
  66. self.assertTrue(
  67. 'skip this example with --skip-invalid-size-inputs-valid-test' in str(context.exception)
  68. )
  69. train_translation_model(
  70. data_dir, 'fconv_iwslt_de_en',
  71. ['--max-target-positions', '5', '--skip-invalid-size-inputs-valid-test'],
  72. )
  73. with self.assertRaises(Exception) as context:
  74. generate_main(data_dir)
  75. generate_main(data_dir, ['--skip-invalid-size-inputs-valid-test'])
  76. def test_generation(self):
  77. with contextlib.redirect_stdout(StringIO()):
  78. with tempfile.TemporaryDirectory('test_sampling') as data_dir:
  79. create_dummy_data(data_dir)
  80. preprocess_translation_data(data_dir)
  81. train_translation_model(data_dir, 'fconv_iwslt_de_en')
  82. generate_main(data_dir, [
  83. '--sampling',
  84. '--sampling-temperature', '2',
  85. '--beam', '2',
  86. '--nbest', '2',
  87. ])
  88. generate_main(data_dir, [
  89. '--sampling',
  90. '--sampling-topk', '3',
  91. '--beam', '2',
  92. '--nbest', '2',
  93. ])
  94. generate_main(data_dir, ['--prefix-size', '2'])
  95. def test_lstm(self):
  96. with contextlib.redirect_stdout(StringIO()):
  97. with tempfile.TemporaryDirectory('test_lstm') as data_dir:
  98. create_dummy_data(data_dir)
  99. preprocess_translation_data(data_dir)
  100. train_translation_model(data_dir, 'lstm_wiseman_iwslt_de_en', [
  101. '--encoder-layers', '2',
  102. '--decoder-layers', '2',
  103. ])
  104. generate_main(data_dir)
  105. def test_lstm_bidirectional(self):
  106. with contextlib.redirect_stdout(StringIO()):
  107. with tempfile.TemporaryDirectory('test_lstm_bidirectional') as data_dir:
  108. create_dummy_data(data_dir)
  109. preprocess_translation_data(data_dir)
  110. train_translation_model(data_dir, 'lstm', [
  111. '--encoder-layers', '2',
  112. '--encoder-bidirectional',
  113. '--encoder-hidden-size', '256',
  114. '--decoder-layers', '2',
  115. ])
  116. generate_main(data_dir)
  117. def test_transformer(self):
  118. with contextlib.redirect_stdout(StringIO()):
  119. with tempfile.TemporaryDirectory('test_transformer') as data_dir:
  120. create_dummy_data(data_dir)
  121. preprocess_translation_data(data_dir)
  122. train_translation_model(data_dir, 'transformer_iwslt_de_en')
  123. generate_main(data_dir)
  124. def test_lightconv(self):
  125. with contextlib.redirect_stdout(StringIO()):
  126. with tempfile.TemporaryDirectory('test_lightconv') as data_dir:
  127. create_dummy_data(data_dir)
  128. preprocess_translation_data(data_dir)
  129. train_translation_model(data_dir, 'lightconv_iwslt_de_en', [
  130. '--encoder-conv-type', 'lightweight',
  131. '--decoder-conv-type', 'lightweight',
  132. ])
  133. generate_main(data_dir)
  134. def test_dynamicconv(self):
  135. with contextlib.redirect_stdout(StringIO()):
  136. with tempfile.TemporaryDirectory('test_dynamicconv') as data_dir:
  137. create_dummy_data(data_dir)
  138. preprocess_translation_data(data_dir)
  139. train_translation_model(data_dir, 'lightconv_iwslt_de_en', [
  140. '--encoder-conv-type', 'dynamic',
  141. '--decoder-conv-type', 'dynamic',
  142. ])
  143. generate_main(data_dir)
  144. class TestStories(unittest.TestCase):
  145. def test_fconv_self_att_wp(self):
  146. with contextlib.redirect_stdout(StringIO()):
  147. with tempfile.TemporaryDirectory('test_fconv_self_att_wp') as data_dir:
  148. create_dummy_data(data_dir)
  149. preprocess_translation_data(data_dir)
  150. config = [
  151. '--encoder-layers', '[(512, 3)] * 2',
  152. '--decoder-layers', '[(512, 3)] * 2',
  153. '--decoder-attention', 'True',
  154. '--encoder-attention', 'False',
  155. '--gated-attention', 'True',
  156. '--self-attention', 'True',
  157. '--project-input', 'True',
  158. ]
  159. train_translation_model(data_dir, 'fconv_self_att_wp', config)
  160. generate_main(data_dir)
  161. # fusion model
  162. os.rename(os.path.join(data_dir, 'checkpoint_last.pt'), os.path.join(data_dir, 'pretrained.pt'))
  163. config.extend([
  164. '--pretrained', 'True',
  165. '--pretrained-checkpoint', os.path.join(data_dir, 'pretrained.pt'),
  166. '--save-dir', os.path.join(data_dir, 'fusion_model'),
  167. ])
  168. train_translation_model(data_dir, 'fconv_self_att_wp', config)
  169. class TestLanguageModeling(unittest.TestCase):
  170. def test_fconv_lm(self):
  171. with contextlib.redirect_stdout(StringIO()):
  172. with tempfile.TemporaryDirectory('test_fconv_lm') as data_dir:
  173. create_dummy_data(data_dir)
  174. preprocess_lm_data(data_dir)
  175. train_language_model(data_dir, 'fconv_lm')
  176. eval_lm_main(data_dir)
  177. def create_dummy_data(data_dir, num_examples=1000, maxlen=20):
  178. def _create_dummy_data(filename):
  179. data = torch.rand(num_examples * maxlen)
  180. data = 97 + torch.floor(26 * data).int()
  181. with open(os.path.join(data_dir, filename), 'w') as h:
  182. offset = 0
  183. for _ in range(num_examples):
  184. ex_len = random.randint(1, maxlen)
  185. ex_str = ' '.join(map(chr, data[offset:offset+ex_len]))
  186. print(ex_str, file=h)
  187. offset += ex_len
  188. _create_dummy_data('train.in')
  189. _create_dummy_data('train.out')
  190. _create_dummy_data('valid.in')
  191. _create_dummy_data('valid.out')
  192. _create_dummy_data('test.in')
  193. _create_dummy_data('test.out')
  194. def preprocess_translation_data(data_dir, extra_flags=None):
  195. preprocess_parser = options.get_preprocessing_parser()
  196. preprocess_args = preprocess_parser.parse_args(
  197. [
  198. '--source-lang', 'in',
  199. '--target-lang', 'out',
  200. '--trainpref', os.path.join(data_dir, 'train'),
  201. '--validpref', os.path.join(data_dir, 'valid'),
  202. '--testpref', os.path.join(data_dir, 'test'),
  203. '--thresholdtgt', '0',
  204. '--thresholdsrc', '0',
  205. '--destdir', data_dir,
  206. ] + (extra_flags or []),
  207. )
  208. preprocess.main(preprocess_args)
  209. def train_translation_model(data_dir, arch, extra_flags=None):
  210. train_parser = options.get_training_parser()
  211. train_args = options.parse_args_and_arch(
  212. train_parser,
  213. [
  214. '--task', 'translation',
  215. data_dir,
  216. '--save-dir', data_dir,
  217. '--arch', arch,
  218. '--optimizer', 'nag',
  219. '--lr', '0.05',
  220. '--max-tokens', '500',
  221. '--max-epoch', '1',
  222. '--no-progress-bar',
  223. '--distributed-world-size', '1',
  224. '--source-lang', 'in',
  225. '--target-lang', 'out',
  226. ] + (extra_flags or []),
  227. )
  228. train.main(train_args)
  229. def generate_main(data_dir, extra_flags=None):
  230. generate_parser = options.get_generation_parser()
  231. generate_args = options.parse_args_and_arch(
  232. generate_parser,
  233. [
  234. data_dir,
  235. '--path', os.path.join(data_dir, 'checkpoint_last.pt'),
  236. '--beam', '3',
  237. '--batch-size', '64',
  238. '--max-len-b', '5',
  239. '--gen-subset', 'valid',
  240. '--no-progress-bar',
  241. '--print-alignment',
  242. ] + (extra_flags or []),
  243. )
  244. # evaluate model in batch mode
  245. generate.main(generate_args)
  246. # evaluate model interactively
  247. generate_args.buffer_size = 0
  248. generate_args.input = '-'
  249. generate_args.max_sentences = None
  250. orig_stdin = sys.stdin
  251. sys.stdin = StringIO('h e l l o\n')
  252. interactive.main(generate_args)
  253. sys.stdin = orig_stdin
  254. def preprocess_lm_data(data_dir):
  255. preprocess_parser = options.get_preprocessing_parser()
  256. preprocess_args = preprocess_parser.parse_args([
  257. '--only-source',
  258. '--trainpref', os.path.join(data_dir, 'train.out'),
  259. '--validpref', os.path.join(data_dir, 'valid.out'),
  260. '--testpref', os.path.join(data_dir, 'test.out'),
  261. '--destdir', data_dir,
  262. ])
  263. preprocess.main(preprocess_args)
  264. def train_language_model(data_dir, arch):
  265. train_parser = options.get_training_parser()
  266. train_args = options.parse_args_and_arch(
  267. train_parser,
  268. [
  269. '--task', 'language_modeling',
  270. data_dir,
  271. '--arch', arch,
  272. '--optimizer', 'nag',
  273. '--lr', '0.1',
  274. '--criterion', 'adaptive_loss',
  275. '--adaptive-softmax-cutoff', '5,10,15',
  276. '--decoder-layers', '[(850, 3)] * 2 + [(1024,4)]',
  277. '--decoder-embed-dim', '280',
  278. '--max-tokens', '500',
  279. '--tokens-per-sample', '500',
  280. '--save-dir', data_dir,
  281. '--max-epoch', '1',
  282. '--no-progress-bar',
  283. '--distributed-world-size', '1',
  284. '--ddp-backend', 'no_c10d',
  285. ],
  286. )
  287. train.main(train_args)
  288. def eval_lm_main(data_dir):
  289. eval_lm_parser = options.get_eval_lm_parser()
  290. eval_lm_args = options.parse_args_and_arch(
  291. eval_lm_parser,
  292. [
  293. data_dir,
  294. '--path', os.path.join(data_dir, 'checkpoint_last.pt'),
  295. '--no-progress-bar',
  296. ],
  297. )
  298. eval_lm.main(eval_lm_args)
  299. if __name__ == '__main__':
  300. unittest.main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...