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 7.5 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
  1. import pytest
  2. import torch
  3. import random
  4. from molbart.decoder import DecodeSampler
  5. from molbart.tokeniser import MolEncTokeniser
  6. from molbart.models.pre_train import BARTModel
  7. regex = "\[[^\]]+]|Br?|Cl?|N|O|S|P|F|I|b|c|n|o|s|p|\(|\)|\.|=|#|-|\+|\\\\|\/|:|~|@|\?|>|\*|\$|\%[0-9]{2}|[0-9]"
  8. # Use dummy SMILES strings
  9. react_data = [
  10. "CCO.C",
  11. "CCCl",
  12. "C(=O)CBr"
  13. ]
  14. # Use dummy SMILES strings
  15. prod_data = [
  16. "cc",
  17. "CCl",
  18. "CBr"
  19. ]
  20. model_args = {
  21. "d_model": 5,
  22. "num_layers": 2,
  23. "num_heads": 1,
  24. "d_feedforward": 32,
  25. "lr": 0.0001,
  26. "weight_decay": 0.0,
  27. "activation": "gelu",
  28. "num_steps": 1000,
  29. "max_seq_len": 40
  30. }
  31. random.seed(a=1)
  32. torch.manual_seed(1)
  33. def build_tokeniser():
  34. tokeniser = MolEncTokeniser.from_smiles(react_data + prod_data, regex, mask_scheme="replace")
  35. return tokeniser
  36. def test_pos_emb_shape():
  37. tokeniser = build_tokeniser()
  38. pad_token_idx = tokeniser.vocab[tokeniser.pad_token]
  39. sampler = DecodeSampler(tokeniser, model_args["max_seq_len"])
  40. model = BARTModel(sampler, pad_token_idx, len(tokeniser), **model_args)
  41. pos_embs = model._positional_embs()
  42. assert pos_embs.shape[0] == model_args["max_seq_len"]
  43. assert pos_embs.shape[1] == model.d_model
  44. def test_construct_input_shape():
  45. tokeniser = build_tokeniser()
  46. pad_token_idx = tokeniser.vocab[tokeniser.pad_token]
  47. sampler = DecodeSampler(tokeniser, model_args["max_seq_len"])
  48. model = BARTModel(sampler, pad_token_idx, len(tokeniser), **model_args)
  49. token_output = tokeniser.tokenise(react_data, sents2=prod_data, pad=True)
  50. tokens = token_output["original_tokens"]
  51. sent_masks = token_output["sentence_masks"]
  52. token_ids = torch.tensor(tokeniser.convert_tokens_to_ids(tokens)).transpose(0, 1)
  53. sent_masks = torch.tensor(sent_masks).transpose(0, 1)
  54. emb = model._construct_input(token_ids, sent_masks)
  55. assert emb.shape[0] == max([len(ts) for ts in tokens])
  56. assert emb.shape[1] == 3
  57. assert emb.shape[2] == model_args["d_model"]
  58. def test_bart_forward_shape():
  59. tokeniser = build_tokeniser()
  60. pad_token_idx = tokeniser.vocab[tokeniser.pad_token]
  61. sampler = DecodeSampler(tokeniser, model_args["max_seq_len"])
  62. model = BARTModel(sampler, pad_token_idx, len(tokeniser), **model_args)
  63. react_token_output = tokeniser.tokenise(react_data, mask=True, pad=True)
  64. react_tokens = react_token_output["masked_tokens"]
  65. react_pad_mask = react_token_output["masked_pad_masks"]
  66. react_ids = torch.tensor(tokeniser.convert_tokens_to_ids(react_tokens)).T
  67. react_mask = torch.tensor(react_pad_mask).T
  68. prod_token_output = tokeniser.tokenise(prod_data, pad=True)
  69. prod_tokens = prod_token_output["original_tokens"]
  70. prod_pad_mask = prod_token_output["original_pad_masks"]
  71. prod_ids = torch.tensor(tokeniser.convert_tokens_to_ids(prod_tokens)).T
  72. prod_mask = torch.tensor(prod_pad_mask).T
  73. batch_input = {
  74. "encoder_input": react_ids,
  75. "encoder_pad_mask": react_mask,
  76. "decoder_input": prod_ids,
  77. "decoder_pad_mask": prod_mask
  78. }
  79. output = model(batch_input)
  80. model_output = output["model_output"]
  81. token_output = output["token_output"]
  82. exp_seq_len = 4 # From expected tokenised length of prod data
  83. exp_batch_size = len(prod_data)
  84. exp_dim = model_args["d_model"]
  85. exp_vocab_size = len(tokeniser)
  86. assert tuple(model_output.shape) == (exp_seq_len, exp_batch_size, exp_dim)
  87. assert tuple(token_output.shape) == (exp_seq_len, exp_batch_size, exp_vocab_size)
  88. def test_bart_encode_shape():
  89. tokeniser = build_tokeniser()
  90. pad_token_idx = tokeniser.vocab[tokeniser.pad_token]
  91. sampler = DecodeSampler(tokeniser, model_args["max_seq_len"])
  92. model = BARTModel(sampler, pad_token_idx, len(tokeniser), **model_args)
  93. react_token_output = tokeniser.tokenise(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(tokeniser.convert_tokens_to_ids(react_tokens)).T
  97. react_mask = torch.tensor(react_pad_mask).T
  98. batch_input = {
  99. "encoder_input": react_ids,
  100. "encoder_pad_mask": react_mask
  101. }
  102. output = model.encode(batch_input)
  103. exp_seq_len = 9 # From expected tokenised length of react data
  104. exp_batch_size = len(react_data)
  105. exp_dim = model_args["d_model"]
  106. assert tuple(output.shape) == (exp_seq_len, exp_batch_size, exp_dim)
  107. def test_bart_decode_shape():
  108. tokeniser = build_tokeniser()
  109. pad_token_idx = tokeniser.vocab[tokeniser.pad_token]
  110. sampler = DecodeSampler(tokeniser, model_args["max_seq_len"])
  111. model = BARTModel(sampler, pad_token_idx, len(tokeniser), **model_args)
  112. react_token_output = tokeniser.tokenise(react_data, mask=True, pad=True)
  113. react_tokens = react_token_output["masked_tokens"]
  114. react_pad_mask = react_token_output["masked_pad_masks"]
  115. react_ids = torch.tensor(tokeniser.convert_tokens_to_ids(react_tokens)).T
  116. react_mask = torch.tensor(react_pad_mask).T
  117. encode_input = {
  118. "encoder_input": react_ids,
  119. "encoder_pad_mask": react_mask
  120. }
  121. memory = model.encode(encode_input)
  122. prod_token_output = tokeniser.tokenise(prod_data, pad=True)
  123. prod_tokens = prod_token_output["original_tokens"]
  124. prod_pad_mask = prod_token_output["original_pad_masks"]
  125. prod_ids = torch.tensor(tokeniser.convert_tokens_to_ids(prod_tokens)).T
  126. prod_mask = torch.tensor(prod_pad_mask).T
  127. batch_input = {
  128. "decoder_input": prod_ids,
  129. "decoder_pad_mask": prod_mask,
  130. "memory_input": memory,
  131. "memory_pad_mask": react_mask
  132. }
  133. output = model.decode(batch_input)
  134. exp_seq_len = 4 # From expected tokenised length of prod data
  135. exp_batch_size = len(react_data)
  136. exp_vocab_size = len(tokeniser)
  137. assert tuple(output.shape) == (exp_seq_len, exp_batch_size, exp_vocab_size)
  138. def test_calc_token_acc():
  139. tokeniser = build_tokeniser()
  140. pad_token_idx = tokeniser.vocab[tokeniser.pad_token]
  141. sampler = DecodeSampler(tokeniser, model_args["max_seq_len"])
  142. model = BARTModel(sampler, pad_token_idx, len(tokeniser), **model_args)
  143. react_token_output = tokeniser.tokenise(react_data[1:], pad=True)
  144. react_tokens = react_token_output["original_tokens"]
  145. react_pad_mask = react_token_output["original_pad_masks"]
  146. target_ids = torch.tensor(tokeniser.convert_tokens_to_ids(react_tokens)).T[1:, :]
  147. target_mask = torch.tensor(react_pad_mask).T[1:, :]
  148. # 9 is expected seq len of react data when padded
  149. token_output = torch.rand([8, len(react_data[1:]), len(tokeniser)])
  150. """
  151. Expected outputs
  152. CCCl
  153. C(=O)CBr
  154. Vocab:
  155. 0 <PAD>
  156. 3 &
  157. 6 C
  158. 7 O
  159. 8 .
  160. 9 Cl
  161. 10 (
  162. 11 =
  163. 12 )
  164. 13 Br
  165. """
  166. # Batch element 0
  167. token_output[0, 0, 6] += 1
  168. token_output[1, 0, 6] -= 1
  169. token_output[2, 0, 9] += 1
  170. token_output[3, 0, 3] += 1
  171. token_output[4, 0, 0] += 1
  172. token_output[5, 0, 0] -= 1
  173. # Batch element 1
  174. token_output[0, 1, 6] += 1
  175. token_output[1, 1, 10] += 1
  176. token_output[2, 1, 11] += 1
  177. token_output[3, 1, 7] += 1
  178. token_output[4, 1, 12] -= 1
  179. token_output[5, 1, 6] += 1
  180. token_output[6, 1, 13] -= 1
  181. token_output[7, 1, 3] += 1
  182. batch_input = {
  183. "target": target_ids,
  184. "target_mask": target_mask
  185. }
  186. model_output = {
  187. "token_output": token_output
  188. }
  189. token_acc = model._calc_token_acc(batch_input, model_output)
  190. exp_token_acc = (3 + 6) / (4 + 8)
  191. 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...