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

pre_train_model_test.py 9.3 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
  1. # # Copyright (c) 2022, NVIDIA CORPORATION.
  2. # # SPDX-License-Identifier: Apache-2.0
  3. # # Licensed under the Apache License, Version 2.0 (the "License");
  4. # # you may not use this file except in compliance with the License.
  5. # # You may obtain a copy of the License at
  6. # #
  7. # # http://www.apache.org/licenses/LICENSE-2.0
  8. # #
  9. # # Unless required by applicable law or agreed to in writing, software
  10. # # distributed under the License is distributed on an "AS IS" BASIS,
  11. # # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # # See the License for the specific language governing permissions and
  13. # # limitations under the License.
  14. # import pytest
  15. # import random
  16. # import torch
  17. # from nemo_chem.decoder import DecodeSampler
  18. # from nemo_chem.tokenizer import MolEncTokenizer, MolEncTokenizerFromSmilesConfig
  19. # from nemo_chem.models import MegaMolBARTModel, MegatronBARTConfig
  20. # from nemo.collections.nlp.modules.common.megatron.megatron_init import initialize_model_parallel_for_nemo
  21. # # TODO cleanup model tests
  22. # # Use dummy SMILES strings
  23. # react_data = [
  24. # "CCO.C",
  25. # "CCCl",
  26. # "C(=O)CBr"
  27. # ]
  28. # prod_data = [
  29. # "cc",
  30. # "CCl",
  31. # "CBr"
  32. # ]
  33. # random.seed(a=1)
  34. # torch.manual_seed(1)
  35. # initialize_model_parallel_for_nemo(
  36. # world_size=1,
  37. # global_rank=0,
  38. # local_rank=0,
  39. # tensor_model_parallel_size=1,
  40. # seed=1234,
  41. # )
  42. # TEST_MODEL_CONFIG = MegatronBARTConfig()
  43. # TEST_PERCEIVER_CONFIG = MegatronBARTConfig(encoder_type='perceiver')
  44. # @pytest.mark.skip(reason="Model tests are currently deprecated")
  45. # @pytest.fixture(params=[TEST_MODEL_CONFIG, TEST_PERCEIVER_CONFIG])
  46. # def args(request):
  47. # _args = request.param
  48. # return _args
  49. # @pytest.mark.skip(reason="Model tests are currently deprecated")
  50. # @pytest.fixture
  51. # def tokenizer():
  52. # cfg = MolEncTokenizerFromSmilesConfig({'smiles': react_data + prod_data})
  53. # _tokenizer = MolEncTokenizer.from_smiles(
  54. # cfg.smiles["smiles"], cfg.regex, mask_scheme="replace")
  55. # return _tokenizer
  56. # @pytest.mark.skip(reason="Model tests are currently deprecated")
  57. # @pytest.fixture
  58. # def model(args, tokenizer, sampler):
  59. # pad_token_idx = tokenizer.vocab[tokenizer.pad_token]
  60. # vocab_size = len(tokenizer)
  61. # _model = MegatronBART(sampler,
  62. # args.encoder_type,
  63. # pad_token_idx,
  64. # vocab_size,
  65. # args.blocks_model,
  66. # args.steps_model,
  67. # args.d_model,
  68. # args.num_layers,
  69. # args.num_heads,
  70. # args.d_feedforward,
  71. # args.seq_len,
  72. # dropout=0.1)
  73. # return _model.cuda()
  74. # @pytest.mark.skip(reason="Model tests are currently deprecated")
  75. # def test_pos_emb_shape(model, sampler, tokenizer, args):
  76. # pos_embs = model._positional_embs()
  77. # assert pos_embs.shape[0] == args.seq_len
  78. # assert pos_embs.shape[1] == model.d_model # hidden size
  79. # @pytest.mark.skip(reason="Model tests are currently deprecated")
  80. # def test_construct_input_shape(model, sampler, tokenizer, args):
  81. # token_output = tokenizer.tokenize(react_data, sents2=prod_data, pad=True)
  82. # tokens = token_output["original_tokens"]
  83. # sent_masks = token_output["sentence_masks"]
  84. # token_ids = torch.tensor(
  85. # tokenizer.convert_tokens_to_ids(tokens)).transpose(0, 1).cuda()
  86. # sent_masks = torch.tensor(sent_masks).transpose(0, 1).cuda()
  87. # emb = model._construct_input(token_ids, sent_masks)
  88. # assert emb.shape[0] == max([len(ts) for ts in tokens])
  89. # assert emb.shape[1] == 3
  90. # assert emb.shape[2] == args.d_model
  91. # @pytest.mark.skip(reason="Model tests are currently deprecated")
  92. # def test_bart_forward_shape(model, sampler, tokenizer, args):
  93. # react_token_output = tokenizer.tokenize(react_data, mask=True, pad=True)
  94. # react_tokens = react_token_output["masked_tokens"]
  95. # react_pad_mask = react_token_output["masked_pad_masks"]
  96. # react_ids = torch.tensor(tokenizer.convert_tokens_to_ids(react_tokens)).T
  97. # react_mask = torch.tensor(react_pad_mask).T
  98. # prod_token_output = tokenizer.tokenize(prod_data, pad=True)
  99. # prod_tokens = prod_token_output["original_tokens"]
  100. # prod_pad_mask = prod_token_output["original_pad_masks"]
  101. # prod_ids = torch.tensor(tokenizer.convert_tokens_to_ids(prod_tokens)).T
  102. # prod_mask = torch.tensor(prod_pad_mask).T
  103. # batch_input = {
  104. # "encoder_input": react_ids.cuda(),
  105. # "encoder_pad_mask": react_mask.cuda(),
  106. # "decoder_input": prod_ids.cuda(),
  107. # "decoder_pad_mask": prod_mask.cuda()
  108. # }
  109. # output = model(batch_input)
  110. # model_output = output["model_output"]
  111. # token_output = output["token_output"]
  112. # exp_seq_len = 4 # From expected tokenized length of prod data
  113. # exp_batch_size = len(prod_data)
  114. # exp_dim = args.d_model # hidden_size
  115. # exp_vocab_size = len(tokenizer)
  116. # assert tuple(model_output.shape) == (exp_seq_len, exp_batch_size, exp_dim)
  117. # assert tuple(token_output.shape) == (exp_seq_len, exp_batch_size, exp_vocab_size)
  118. # @pytest.mark.skip(reason="Model tests are currently deprecated")
  119. # def test_bart_encode_shape(model, sampler, tokenizer, args):
  120. # react_token_output = tokenizer.tokenize(react_data, mask=True, pad=True)
  121. # react_tokens = react_token_output["masked_tokens"]
  122. # react_pad_mask = react_token_output["masked_pad_masks"]
  123. # react_ids = torch.tensor(tokenizer.convert_tokens_to_ids(react_tokens)).T
  124. # react_mask = torch.tensor(react_pad_mask).T
  125. # batch_input = {
  126. # "encoder_input": react_ids.cuda(),
  127. # "encoder_pad_mask": react_mask.cuda()
  128. # }
  129. # output = model.encode(batch_input)
  130. # if args.encoder_type == 'seq2seq':
  131. # exp_seq_len = 9 # From expected tokenized length of react data
  132. # elif args.encoder_type == 'perceiver':
  133. # exp_seq_len = args.steps_model # From expected num_hidden_steps of the Perceiver encoder
  134. # exp_batch_size = len(react_data)
  135. # exp_dim = args.d_model # hidden_size
  136. # assert tuple(output.shape) == (exp_seq_len, exp_batch_size, exp_dim)
  137. # @pytest.mark.skip(reason="Model tests are currently deprecated")
  138. # def test_bart_decode_shape(model, sampler, tokenizer, args):
  139. # react_token_output = tokenizer.tokenize(react_data, mask=True, pad=True)
  140. # react_tokens = react_token_output["masked_tokens"]
  141. # react_pad_mask = react_token_output["masked_pad_masks"]
  142. # react_ids = torch.tensor(tokenizer.convert_tokens_to_ids(react_tokens)).T
  143. # react_mask = torch.tensor(react_pad_mask).T
  144. # encode_input = {
  145. # "encoder_input": react_ids.cuda(),
  146. # "encoder_pad_mask": react_mask.cuda()
  147. # }
  148. # memory = model.encode(encode_input)
  149. # prod_token_output = tokenizer.tokenize(prod_data, pad=True)
  150. # prod_tokens = prod_token_output["original_tokens"]
  151. # prod_pad_mask = prod_token_output["original_pad_masks"]
  152. # prod_ids = torch.tensor(tokenizer.convert_tokens_to_ids(prod_tokens)).T
  153. # prod_mask = torch.tensor(prod_pad_mask).T
  154. # if args.encoder_type == "perceiver":
  155. # react_mask = torch.zeros(
  156. # (memory.shape[0:2]), dtype=react_mask.dtype, device=react_mask.device)
  157. # batch_input = {
  158. # "decoder_input": prod_ids.cuda(),
  159. # "decoder_pad_mask": prod_mask.cuda(),
  160. # "memory_input": memory.cuda(),
  161. # "memory_pad_mask": react_mask.cuda()
  162. # }
  163. # output = model.decode(batch_input)
  164. # exp_seq_len = 4 # From expected tokenized length of prod data
  165. # exp_batch_size = len(react_data)
  166. # exp_vocab_size = len(tokenizer)
  167. # assert tuple(output.shape) == (exp_seq_len, exp_batch_size, exp_vocab_size)
  168. # @pytest.mark.skip(reason="Model tests are currently deprecated")
  169. # def test_calc_char_acc(model, sampler, tokenizer, args):
  170. # react_token_output = tokenizer.tokenize(react_data[1:], pad=True)
  171. # react_tokens = react_token_output["original_tokens"]
  172. # react_pad_mask = react_token_output["original_pad_masks"]
  173. # target_ids = torch.tensor(
  174. # tokenizer.convert_tokens_to_ids(react_tokens)).T[1:, :]
  175. # target_mask = torch.tensor(react_pad_mask).T[1:, :]
  176. # # 9 is expected seq len of react data when padded
  177. # token_output = torch.rand([8, len(react_data[1:]), len(tokenizer)])
  178. # """
  179. # Expected outputs
  180. # CCCl
  181. # C(=O)CBr
  182. # Vocab:
  183. # 0 <PAD>
  184. # 3 &
  185. # 6 C
  186. # 7 O
  187. # 8 .
  188. # 9 Cl
  189. # 10 (
  190. # 11 =
  191. # 12 )
  192. # 13 Br
  193. # """
  194. # # Batch element 0
  195. # token_output[0, 0, 6] += 1
  196. # token_output[1, 0, 6] -= 1
  197. # token_output[2, 0, 9] += 1
  198. # token_output[3, 0, 3] += 1
  199. # token_output[4, 0, 0] += 1
  200. # token_output[5, 0, 0] -= 1
  201. # # Batch element 1
  202. # token_output[0, 1, 6] += 1
  203. # token_output[1, 1, 10] += 1
  204. # token_output[2, 1, 11] += 1
  205. # token_output[3, 1, 7] += 1
  206. # token_output[4, 1, 12] -= 1
  207. # token_output[5, 1, 6] += 1
  208. # token_output[6, 1, 13] -= 1
  209. # token_output[7, 1, 3] += 1
  210. # batch_input = {
  211. # "target": target_ids.cuda(),
  212. # "target_pad_mask": target_mask.cuda()
  213. # }
  214. # model_output = {
  215. # "token_output": token_output.cuda()
  216. # }
  217. # token_acc = model._calc_char_acc(batch_input, model_output)
  218. # exp_token_acc = (3 + 6) / (4 + 8)
  219. # assert exp_token_acc == token_acc
Tip!

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

Comments

Loading...