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 3.7 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
  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. from io import StringIO
  8. import os
  9. import random
  10. import sys
  11. import tempfile
  12. import unittest
  13. import torch
  14. from fairseq import options
  15. import preprocess
  16. import train
  17. import generate
  18. import interactive
  19. class TestBinaries(unittest.TestCase):
  20. def test_binaries(self):
  21. # comment this out to debug the unittest if it's failing
  22. self.mock_stdout()
  23. with tempfile.TemporaryDirectory() as data_dir:
  24. self.create_dummy_data(data_dir)
  25. self.preprocess_data(data_dir)
  26. self.train_model(data_dir)
  27. self.generate(data_dir)
  28. self.unmock_stdout()
  29. def create_dummy_data(self, data_dir, num_examples=1000, maxlen=20):
  30. def _create_dummy_data(filename):
  31. data = torch.rand(num_examples * maxlen)
  32. data = 97 + torch.floor(26 * data).int()
  33. with open(os.path.join(data_dir, filename), 'w') as h:
  34. offset = 0
  35. for _ in range(num_examples):
  36. ex_len = random.randint(1, maxlen)
  37. ex_str = ' '.join(map(chr, data[offset:offset+ex_len]))
  38. print(ex_str, file=h)
  39. offset += ex_len
  40. _create_dummy_data('train.in')
  41. _create_dummy_data('train.out')
  42. _create_dummy_data('valid.in')
  43. _create_dummy_data('valid.out')
  44. _create_dummy_data('test.in')
  45. _create_dummy_data('test.out')
  46. def preprocess_data(self, data_dir):
  47. preprocess_parser = preprocess.get_parser()
  48. preprocess_args = preprocess_parser.parse_args([
  49. '--source-lang', 'in',
  50. '--target-lang', 'out',
  51. '--trainpref', os.path.join(data_dir, 'train'),
  52. '--validpref', os.path.join(data_dir, 'valid'),
  53. '--testpref', os.path.join(data_dir, 'test'),
  54. '--thresholdtgt', '0',
  55. '--thresholdsrc', '0',
  56. '--destdir', data_dir,
  57. ])
  58. preprocess.main(preprocess_args)
  59. def train_model(self, data_dir):
  60. train_parser = options.get_training_parser()
  61. train_args = options.parse_args_and_arch(
  62. train_parser,
  63. [
  64. data_dir,
  65. '--arch', 'fconv_iwslt_de_en',
  66. '--optimizer', 'nag',
  67. '--lr', '0.05',
  68. '--max-tokens', '500',
  69. '--save-dir', data_dir,
  70. '--max-epoch', '1',
  71. '--no-progress-bar',
  72. '--distributed-world-size', '1',
  73. ],
  74. )
  75. train.main(train_args)
  76. def generate(self, data_dir):
  77. generate_parser = options.get_generation_parser()
  78. generate_args = generate_parser.parse_args([
  79. data_dir,
  80. '--path', os.path.join(data_dir, 'checkpoint_best.pt'),
  81. '--beam', '5',
  82. '--batch-size', '32',
  83. '--gen-subset', 'valid',
  84. '--no-progress-bar',
  85. ])
  86. # evaluate model in batch mode
  87. generate.main(generate_args)
  88. # evaluate model interactively
  89. generate_args.max_sentences = None
  90. orig_stdin = sys.stdin
  91. sys.stdin = StringIO('h e l l o\n')
  92. interactive.main(generate_args)
  93. sys.stdin = orig_stdin
  94. def mock_stdout(self):
  95. self._orig_stdout = sys.stdout
  96. sys.stdout = StringIO()
  97. def unmock_stdout(self):
  98. if hasattr(self, '_orig_stdout'):
  99. sys.stdout = self._orig_stdout
  100. if __name__ == '__main__':
  101. unittest.main()
Tip!

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

Comments

Loading...