-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathREADME.html
More file actions
611 lines (593 loc) · 62.8 KB
/
README.html
File metadata and controls
611 lines (593 loc) · 62.8 KB
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.3.450">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>readme</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>
<script src="README_files/libs/clipboard/clipboard.min.js"></script>
<script src="README_files/libs/quarto-html/quarto.js"></script>
<script src="README_files/libs/quarto-html/popper.min.js"></script>
<script src="README_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="README_files/libs/quarto-html/anchor.min.js"></script>
<link href="README_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="README_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="README_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="README_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="README_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
</head>
<body class="fullcontent">
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<main class="content" id="quarto-document-content">
<section id="protmamba" class="level1">
<h1>ProtMamba</h1>
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
<p><strong>A Homology-Aware but Alignment-Free Protein State Space Model</strong>: <a href="https://www.biorxiv.org/content/early/2024/05/25/2024.05.24.595730">ProtMamba</a> is a novel protein language model designed to facilitate protein design. Unlike traditional models that rely on multiple sequence alignments (MSAs) to capture evolutionary information, ProtMamba can use unaligned homologous sequences, avoiding the imperfections associated with MSAs.</p>
<p><img src="data/logo.jpeg" width="512"></p>
<section id="features" class="level3">
<h3 class="anchored" data-anchor-id="features">Features</h3>
<p>ProtMamba is based on the Mamba architecture, a state space model that efficiently handles very long sequences. The model uses a fill-in-the-middle (FIM) training objective, combining autoregressive modeling and masked language modeling to predict amino acids conditioned on the given context sequences. This makes ProtMamba particularly well-suited for generating novel protein sequences, filling in specific regions of sequences, and predicting the fitness of protein variants.</p>
<ul>
<li><strong>Homology-Aware but Alignment-Free</strong>: Captures evolutionary information without relying on MSAs and use it to condition the generation process.</li>
<li><strong>Efficient Long-Context handling</strong>: Uses Mamba blocks to handle long sequences with linear memory scaling.</li>
<li><strong>Different training objective</strong>: Combines autoregressive and masked language modeling through a fill-in-the-middle objective.</li>
<li><strong>Sequence-level positional embeddings</strong>: Enhances the model’s ability to reason about in-sequence dependencies and allow for precise inpainting.</li>
</ul>
</section>
<section id="applications" class="level3">
<h3 class="anchored" data-anchor-id="applications">Applications</h3>
<ul>
<li><strong>Sequence Generation</strong>: Generate novel protein sequences from scratch conditioned on specific homologs.</li>
<li><strong>Sequence Inpainting</strong>: Fill in specific masked regions within a sequence for targeted protein design.</li>
<li><strong>Fitness Prediction</strong>: Predict the probability distribution of mutations to assess the functional impact of variants.</li>
</ul>
</section>
<section id="repository-structure" class="level3">
<h3 class="anchored" data-anchor-id="repository-structure">Repository Structure</h3>
<p><code>configs/</code>: Configuration files for model training and evaluation.</p>
<p><code>data/</code>: Example dataset.</p>
<p><code>nbs/</code>: Implementation of the ProtMamba model architecture in jupyter notebooks.</p>
<p><code>ProtMamba_ssm/</code>: Implementation of the ProtMamba model architecture.</p>
<p><code>tests/</code>: Scripts to sample from ProtMamba and evaluate the model’s performance.</p>
</section>
<section id="model-weights" class="level3">
<h3 class="anchored" data-anchor-id="model-weights">Model weights</h3>
<p>The model weights are available in</p>
</section>
<section id="install-repository" class="level2">
<h2 class="anchored" data-anchor-id="install-repository">Install Repository</h2>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="ex">pip</span> install <span class="at">-e</span> .</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="how-to-tokenize-all-your-msas-to-make-a-training-dataset" class="level2">
<h2 class="anchored" data-anchor-id="how-to-tokenize-all-your-msas-to-make-a-training-dataset">How to tokenize all your MSAs to make a training dataset</h2>
<p>IMPORTANT: the sequences should be in <code>a3m</code> files but they do not need to be aligned.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pickle</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>msa_paths <span class="op">=</span> {<span class="st">"name-of-msa"</span> : <span class="st">"../data/example_msa.a3m"</span>}</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="co"># path saving directory</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>filepath <span class="op">=</span> <span class="bu">input</span>(<span class="st">"What is the path to the folder where you want to save the dataset?"</span>) <span class="co"># example: "../data/"</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a>dataset_name <span class="op">=</span> <span class="bu">input</span>(<span class="st">"How do you want to name the dataset file?"</span>) <span class="co"># example: "encoded_MSAs_train.pkl"</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>dataset_dictionary <span class="op">=</span> {}</span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> msa_name, msa_path <span class="kw">in</span> msa_paths.items():</span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># Load an a3m file with all the context sequences</span></span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a> msa <span class="op">=</span> load_from_file(msa_path)</span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a> <span class="co"># Tokenize the sequences and concatenate them into a single array</span></span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a> tokens <span class="op">=</span> tokenizer(msa, concatenate<span class="op">=</span><span class="va">True</span>)</span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a> tokens <span class="op">=</span> tokens.numpy()[<span class="dv">0</span>]</span>
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a> dataset_dictionary[msa_name] <span class="op">=</span> tokens</span>
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> <span class="bu">open</span>(filepath<span class="op">+</span>dataset_name, <span class="st">"wb"</span>) <span class="im">as</span> f:</span>
<span id="cb2-17"><a href="#cb2-17" aria-hidden="true" tabindex="-1"></a> pickle.dump(dataset_dictionary, f)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="how-to-train" class="level2">
<h2 class="anchored" data-anchor-id="how-to-train">How to train</h2>
<div class="sourceCode" id="cb3"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> yaml</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="bu">int</span>(use_one_gpu) <span class="op">>=</span> <span class="dv">0</span>:</span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"Using gpu </span><span class="sc">{</span>use_one_gpu<span class="sc">}</span><span class="ss">"</span>)</span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Number of gpus used: "</span>, torch.cuda.device_count())</span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="co"># Load the default config file (change it and add the path to the training dataset)</span></span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> <span class="bu">open</span>(<span class="st">"../configs/default_config.yaml"</span>, <span class="st">"r"</span>) <span class="im">as</span> <span class="bu">file</span>:</span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a> defaultconfig <span class="op">=</span> yaml.safe_load(<span class="bu">file</span>) </span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a>namedir <span class="op">=</span> <span class="bu">input</span>(<span class="st">"Enter name of directory to save results: "</span>)</span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a>finetune_path <span class="op">=</span> <span class="bu">input</span>(<span class="st">"If you want to finetune a model, enter the relative path to the model's checkpoint, otherwise press enter:"</span>)</span>
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a>finetune_path <span class="op">=</span> finetune_path <span class="cf">if</span> finetune_path <span class="cf">else</span> <span class="va">None</span></span>
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a><span class="co"># Run the trainer with the selected training configuration</span></span>
<span id="cb3-16"><a href="#cb3-16" aria-hidden="true" tabindex="-1"></a>trainer <span class="op">=</span> run(defaultconfig, namedir, finetune_model_path<span class="op">=</span>finetune_path)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="how-to-sample-from-a-pretrained-model" class="level2">
<h2 class="anchored" data-anchor-id="how-to-sample-from-a-pretrained-model">How to sample from a pretrained model</h2>
<div class="sourceCode" id="cb4"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>use_custom <span class="op">=</span> <span class="bu">input</span>(<span class="st">"Do you want to use a custom MSA? (y/n): "</span>)</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> use_custom <span class="op">==</span> <span class="st">"y"</span>:</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># Load an a3m file with all the context sequences</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a> msa <span class="op">=</span> load_from_file(<span class="st">"../data/example_msa.a3m"</span>)</span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a> target_sequence <span class="op">=</span> msa[:<span class="dv">1</span>]</span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a> context_msa <span class="op">=</span> msa[<span class="dv">1</span>:]</span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a> <span class="co"># Tokenize the sequences and concatenate them into a single array</span></span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a> target <span class="op">=</span> tokenizer(target_sequence, concatenate<span class="op">=</span><span class="va">True</span>)</span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a> tokens <span class="op">=</span> tokenizer(context_msa, concatenate<span class="op">=</span><span class="va">True</span>)</span>
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a> fim_gen <span class="op">=</span> <span class="bu">input</span>(<span class="st">"Do you want to generate using FIM? (y/n): "</span>)</span>
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> fim_gen<span class="op">==</span><span class="st">"n"</span>:</span>
<span id="cb4-12"><a href="#cb4-12" aria-hidden="true" tabindex="-1"></a> <span class="co"># AUTOREGRESSIVE, no-FIM generation</span></span>
<span id="cb4-13"><a href="#cb4-13" aria-hidden="true" tabindex="-1"></a> <span class="co"># generate the full sequence autoregressively starting from residue 10 in the sequence `target`</span></span>
<span id="cb4-14"><a href="#cb4-14" aria-hidden="true" tabindex="-1"></a> gen_dictionary <span class="op">=</span> {<span class="st">"<cls>"</span>: <span class="dv">10</span>}</span>
<span id="cb4-15"><a href="#cb4-15" aria-hidden="true" tabindex="-1"></a> input_seq, targ_pos <span class="op">=</span> prepare_target(target, use_fim<span class="op">=</span>{<span class="st">"<cls>"</span>: <span class="dv">10</span>})</span>
<span id="cb4-16"><a href="#cb4-16" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> fim_gen<span class="op">==</span><span class="st">"y"</span>:</span>
<span id="cb4-17"><a href="#cb4-17" aria-hidden="true" tabindex="-1"></a> <span class="co"># FIM generation</span></span>
<span id="cb4-18"><a href="#cb4-18" aria-hidden="true" tabindex="-1"></a> <span class="co"># mask_dictionary is a dictionary of the positions in the sequence that you want to mask, in this example there will be</span></span>
<span id="cb4-19"><a href="#cb4-19" aria-hidden="true" tabindex="-1"></a> <span class="co"># - a mask that covers the residues 4,5,6 and the model will fill it by sampling 10 residues</span></span>
<span id="cb4-20"><a href="#cb4-20" aria-hidden="true" tabindex="-1"></a> <span class="co"># - a mask that covers the residues 30,31,32,33,34 and the model will fill it by sampling 3 residues</span></span>
<span id="cb4-21"><a href="#cb4-21" aria-hidden="true" tabindex="-1"></a> mask_dictionary <span class="op">=</span> {<span class="st">"<mask-1>"</span>: ((<span class="dv">4</span>,<span class="dv">7</span>),<span class="dv">10</span>),<span class="st">"<mask-2>"</span>: ((<span class="dv">30</span>,<span class="dv">35</span>),<span class="dv">3</span>)}</span>
<span id="cb4-22"><a href="#cb4-22" aria-hidden="true" tabindex="-1"></a> input_seq, targ_pos, is_fim_dict <span class="op">=</span> prepare_target(target, use_fim<span class="op">=</span>mask_dictionary)</span>
<span id="cb4-23"><a href="#cb4-23" aria-hidden="true" tabindex="-1"></a> context_tokens, context_pos_ids <span class="op">=</span> prepare_tokens(tokens,</span>
<span id="cb4-24"><a href="#cb4-24" aria-hidden="true" tabindex="-1"></a> target_tokens<span class="op">=</span>input_seq,</span>
<span id="cb4-25"><a href="#cb4-25" aria-hidden="true" tabindex="-1"></a> target_pos_ids<span class="op">=</span>targ_pos,</span>
<span id="cb4-26"><a href="#cb4-26" aria-hidden="true" tabindex="-1"></a> DatasetClass<span class="op">=</span>Uniclust30_Dataset,</span>
<span id="cb4-27"><a href="#cb4-27" aria-hidden="true" tabindex="-1"></a> num_sequences<span class="op">=</span><span class="dv">50</span>,</span>
<span id="cb4-28"><a href="#cb4-28" aria-hidden="true" tabindex="-1"></a> fim_strategy<span class="op">=</span><span class="st">"multiple_span"</span>,</span>
<span id="cb4-29"><a href="#cb4-29" aria-hidden="true" tabindex="-1"></a> mask_fraction<span class="op">=</span><span class="fl">0.2</span>,</span>
<span id="cb4-30"><a href="#cb4-30" aria-hidden="true" tabindex="-1"></a> max_patches<span class="op">=</span><span class="dv">5</span>,</span>
<span id="cb4-31"><a href="#cb4-31" aria-hidden="true" tabindex="-1"></a> add_position_ids<span class="op">=</span><span class="st">"1d"</span>)</span>
<span id="cb4-32"><a href="#cb4-32" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> use_custom <span class="op">==</span> <span class="st">"n"</span>:</span>
<span id="cb4-33"><a href="#cb4-33" aria-hidden="true" tabindex="-1"></a> is_fim <span class="op">=</span> <span class="bu">input</span>(<span class="st">"Do you want to use FIM? (y/n): "</span>)</span>
<span id="cb4-34"><a href="#cb4-34" aria-hidden="true" tabindex="-1"></a> filepath <span class="op">=</span> <span class="bu">input</span>(<span class="st">"What is the path to the folder with the dataset?"</span>) </span>
<span id="cb4-35"><a href="#cb4-35" aria-hidden="true" tabindex="-1"></a> is_fim <span class="op">=</span> <span class="va">True</span> <span class="cf">if</span> is_fim <span class="op">==</span> <span class="st">"y"</span> <span class="cf">else</span> <span class="va">False</span></span>
<span id="cb4-36"><a href="#cb4-36" aria-hidden="true" tabindex="-1"></a> <span class="co"># Load the dataset used for training</span></span>
<span id="cb4-37"><a href="#cb4-37" aria-hidden="true" tabindex="-1"></a> dataset_name <span class="op">=</span> <span class="bu">input</span>(<span class="st">"What is the name of the dataset file?"</span>) <span class="co"># example: "encoded_MSAs_subset-100.pkl", "encoded_MSAs_train.pkl"</span></span>
<span id="cb4-38"><a href="#cb4-38" aria-hidden="true" tabindex="-1"></a> fim_strategy <span class="op">=</span> <span class="st">"multiple_span"</span> <span class="cf">if</span> is_fim <span class="cf">else</span> <span class="st">"no-scramble"</span></span>
<span id="cb4-39"><a href="#cb4-39" aria-hidden="true" tabindex="-1"></a> dataset <span class="op">=</span> Uniclust30_Dataset(filename<span class="op">=</span>dataset_name,</span>
<span id="cb4-40"><a href="#cb4-40" aria-hidden="true" tabindex="-1"></a> filepath<span class="op">=</span>filepath,</span>
<span id="cb4-41"><a href="#cb4-41" aria-hidden="true" tabindex="-1"></a> sample<span class="op">=</span><span class="va">False</span>,</span>
<span id="cb4-42"><a href="#cb4-42" aria-hidden="true" tabindex="-1"></a> mask_fraction<span class="op">=</span><span class="fl">0.2</span>,</span>
<span id="cb4-43"><a href="#cb4-43" aria-hidden="true" tabindex="-1"></a> fim_strategy<span class="op">=</span>fim_strategy,</span>
<span id="cb4-44"><a href="#cb4-44" aria-hidden="true" tabindex="-1"></a> max_position_embeddings<span class="op">=</span><span class="dv">2048</span>,</span>
<span id="cb4-45"><a href="#cb4-45" aria-hidden="true" tabindex="-1"></a> add_position_ids<span class="op">=</span><span class="st">"1d"</span>)</span>
<span id="cb4-46"><a href="#cb4-46" aria-hidden="true" tabindex="-1"></a> <span class="co"># Select a sample of the dataset to be the input</span></span>
<span id="cb4-47"><a href="#cb4-47" aria-hidden="true" tabindex="-1"></a> data <span class="op">=</span> dataset[<span class="dv">1</span>]</span>
<span id="cb4-48"><a href="#cb4-48" aria-hidden="true" tabindex="-1"></a> tokens <span class="op">=</span> data[<span class="st">"input_ids"</span>][<span class="va">None</span>,:].to(<span class="st">"cuda"</span>)</span>
<span id="cb4-49"><a href="#cb4-49" aria-hidden="true" tabindex="-1"></a> pos_ids <span class="op">=</span> data[<span class="st">"position_ids"</span>][<span class="va">None</span>,:].to(<span class="st">"cuda"</span>)</span>
<span id="cb4-50"><a href="#cb4-50" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-51"><a href="#cb4-51" aria-hidden="true" tabindex="-1"></a>model_name <span class="op">=</span> <span class="bu">input</span>(<span class="st">"What is the path to the folder with the checkpoint of the model?"</span>) <span class="co"># example: "results/train_100M_FIM_restart-spikes_merged/checkpoint_131k-750"</span></span>
<span id="cb4-52"><a href="#cb4-52" aria-hidden="true" tabindex="-1"></a><span class="co"># Load pretrained model</span></span>
<span id="cb4-53"><a href="#cb4-53" aria-hidden="true" tabindex="-1"></a>model <span class="op">=</span> load_model(model_name,</span>
<span id="cb4-54"><a href="#cb4-54" aria-hidden="true" tabindex="-1"></a> model_class<span class="op">=</span>MambaLMHeadModelwithPosids,</span>
<span id="cb4-55"><a href="#cb4-55" aria-hidden="true" tabindex="-1"></a> device<span class="op">=</span><span class="st">"cuda"</span>,</span>
<span id="cb4-56"><a href="#cb4-56" aria-hidden="true" tabindex="-1"></a> dtype<span class="op">=</span>torch.bfloat16,</span>
<span id="cb4-57"><a href="#cb4-57" aria-hidden="true" tabindex="-1"></a> checkpoint_mixer<span class="op">=</span><span class="va">False</span> <span class="co"># Must be False when using model for Inference</span></span>
<span id="cb4-58"><a href="#cb4-58" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<section id="generate-new-sequences-starting-from-a-custom-msa" class="level3">
<h3 class="anchored" data-anchor-id="generate-new-sequences-starting-from-a-custom-msa">Generate new sequences starting from a custom MSA</h3>
<div class="sourceCode" id="cb5"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Number of tokens in the MSA (target, context): "</span>, <span class="bu">len</span>(input_seq[<span class="dv">0</span>]), <span class="st">","</span>, <span class="bu">len</span>(tokens[<span class="dv">0</span>]))</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Target:"</span>)</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"sequence:"</span>, decode_sequence(input_seq[<span class="dv">0</span>].numpy()))</span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"original sequence:"</span>, decode_sequence(target[<span class="dv">0</span>].numpy()))</span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"pos ids:"</span>, <span class="bu">list</span>(targ_pos[<span class="dv">0</span>].numpy()))</span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Context:"</span>)</span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"sequence:"</span>, decode_sequence(context_tokens[<span class="dv">0</span>].numpy()))</span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"pos ids:"</span>, <span class="bu">list</span>(context_pos_ids[<span class="dv">0</span>].numpy()))</span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Mask positions:"</span>, is_fim_dict)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode" id="cb6"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Generate the new sequence</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>output <span class="op">=</span> generate_sequence(model,</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> context_tokens,</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> position_ids<span class="op">=</span>context_pos_ids,</span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a> is_fim<span class="op">=</span>is_fim_dict,</span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a> max_length<span class="op">=</span><span class="dv">20000</span>,</span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a> temperature<span class="op">=</span><span class="fl">1.</span>,</span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a> top_k<span class="op">=</span><span class="dv">3</span>,</span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a> top_p<span class="op">=</span><span class="fl">0.0</span>,</span>
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a> return_dict_in_generate<span class="op">=</span><span class="va">True</span>,</span>
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a> output_scores<span class="op">=</span><span class="va">True</span>,</span>
<span id="cb6-12"><a href="#cb6-12" aria-hidden="true" tabindex="-1"></a> eos_token_id<span class="op">=</span>AA_TO_ID[<span class="st">"<cls>"</span>],</span>
<span id="cb6-13"><a href="#cb6-13" aria-hidden="true" tabindex="-1"></a> device<span class="op">=</span><span class="st">"cuda"</span>)</span>
<span id="cb6-14"><a href="#cb6-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-15"><a href="#cb6-15" aria-hidden="true" tabindex="-1"></a>input_seq, output_seq <span class="op">=</span> output[<span class="st">"input"</span>], output[<span class="st">"generated"</span>]</span>
<span id="cb6-16"><a href="#cb6-16" aria-hidden="true" tabindex="-1"></a>logits <span class="op">=</span> output[<span class="st">"scores"</span>]</span>
<span id="cb6-17"><a href="#cb6-17" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"All input context (len = </span><span class="sc">{</span><span class="bu">len</span>(input_seq[<span class="dv">0</span>])<span class="sc">}</span><span class="ss">):</span><span class="ch">\n</span><span class="ss">"</span>, input_seq[<span class="dv">0</span>])</span>
<span id="cb6-18"><a href="#cb6-18" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Last sequence where the masked parts should be predicted:</span><span class="ch">\n</span><span class="st">"</span>, input_seq[<span class="dv">0</span>].split(<span class="st">"<cls>"</span>)[<span class="op">-</span><span class="dv">1</span>])</span>
<span id="cb6-19"><a href="#cb6-19" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Generated (len = </span><span class="sc">{</span><span class="bu">len</span>(output_seq[<span class="dv">0</span>])<span class="sc">}</span><span class="ss">):</span><span class="ch">\n</span><span class="ss">"</span>, output_seq[<span class="dv">0</span>])</span>
<span id="cb6-20"><a href="#cb6-20" aria-hidden="true" tabindex="-1"></a>input_continuation <span class="op">=</span> decode_sequence(target[<span class="dv">0</span>].numpy())<span class="op">+</span><span class="st">"<cls>"</span></span>
<span id="cb6-21"><a href="#cb6-21" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Continuation of input:</span><span class="ch">\n</span><span class="ss">"</span>, input_continuation)</span>
<span id="cb6-22"><a href="#cb6-22" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"</span><span class="ch">\n</span><span class="ss">Logits (shape = </span><span class="sc">{</span>logits<span class="sc">.</span>shape<span class="sc">}</span><span class="ss">)"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="generate-new-sequences-using-the-dataset-and-by-filling-in-the-masked-parts-fim" class="level3">
<h3 class="anchored" data-anchor-id="generate-new-sequences-using-the-dataset-and-by-filling-in-the-masked-parts-fim">Generate new sequences using the Dataset and by filling in the masked parts (FIM)</h3>
<div class="sourceCode" id="cb7"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>context_tokens, context_pos_ids, tokens_fim, pos_ids_fim, is_fim_dict <span class="op">=</span> prepare_dataset_for_fim_generation(tokens, pos_ids)</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Length of context information"</span>, context_tokens.shape[<span class="dv">1</span>])</span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Masked part of the sequence to predict and its position indices:</span><span class="ch">\n</span><span class="st">"</span>, tokens_fim, <span class="st">"</span><span class="ch">\n</span><span class="st">"</span>, pos_ids_fim)</span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Masked tokens and their positions in the input sequence:"</span>, is_fim_dict)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode" id="cb8"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Generate the new sequence</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>output <span class="op">=</span> generate_sequence(model,</span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> context_tokens,</span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> position_ids<span class="op">=</span>context_pos_ids,</span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a> is_fim<span class="op">=</span>is_fim_dict,</span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a> max_length<span class="op">=</span><span class="dv">1570</span>,</span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a> temperature<span class="op">=</span><span class="fl">1.</span>,</span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a> top_k<span class="op">=</span><span class="dv">3</span>,</span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a> top_p<span class="op">=</span><span class="fl">0.0</span>,</span>
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a> return_dict_in_generate<span class="op">=</span><span class="va">True</span>,</span>
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a> output_scores<span class="op">=</span><span class="va">True</span>,</span>
<span id="cb8-12"><a href="#cb8-12" aria-hidden="true" tabindex="-1"></a> eos_token_id<span class="op">=</span>AA_TO_ID[<span class="st">"<cls>"</span>],</span>
<span id="cb8-13"><a href="#cb8-13" aria-hidden="true" tabindex="-1"></a> device<span class="op">=</span><span class="st">"cuda"</span>)</span>
<span id="cb8-14"><a href="#cb8-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-15"><a href="#cb8-15" aria-hidden="true" tabindex="-1"></a>input_seq, output_seq <span class="op">=</span> output[<span class="st">"input"</span>], output[<span class="st">"generated"</span>]</span>
<span id="cb8-16"><a href="#cb8-16" aria-hidden="true" tabindex="-1"></a>logits <span class="op">=</span> output[<span class="st">"scores"</span>]</span>
<span id="cb8-17"><a href="#cb8-17" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"All input context (len = </span><span class="sc">{</span><span class="bu">len</span>(input_seq[<span class="dv">0</span>])<span class="sc">}</span><span class="ss">):</span><span class="ch">\n</span><span class="ss">"</span>, input_seq[<span class="dv">0</span>])</span>
<span id="cb8-18"><a href="#cb8-18" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Last sequence where the masked parts should be predicted:</span><span class="ch">\n</span><span class="st">"</span>, input_seq[<span class="dv">0</span>].split(<span class="st">"<cls>"</span>)[<span class="op">-</span><span class="dv">1</span>])</span>
<span id="cb8-19"><a href="#cb8-19" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Generated (len = </span><span class="sc">{</span><span class="bu">len</span>(output_seq[<span class="dv">0</span>])<span class="sc">}</span><span class="ss">):</span><span class="ch">\n</span><span class="ss">"</span>, output_seq[<span class="dv">0</span>])</span>
<span id="cb8-20"><a href="#cb8-20" aria-hidden="true" tabindex="-1"></a>input_continuation <span class="op">=</span> decode_sequence(tokens_fim[<span class="dv">0</span>].cpu().numpy())<span class="op">+</span><span class="st">"<cls>"</span></span>
<span id="cb8-21"><a href="#cb8-21" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Continuation of input:</span><span class="ch">\n</span><span class="ss">"</span>, input_continuation)</span>
<span id="cb8-22"><a href="#cb8-22" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"</span><span class="ch">\n</span><span class="ss">Logits (shape = </span><span class="sc">{</span>logits<span class="sc">.</span>shape<span class="sc">}</span><span class="ss">)"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode" id="cb9"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>total <span class="op">=</span> <span class="bu">len</span>(tokens_fim[<span class="dv">0</span>])<span class="op">+</span><span class="dv">1</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> total<span class="op">></span><span class="dv">4</span>:</span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> fig, axs <span class="op">=</span> plt.subplots(total<span class="op">//</span><span class="dv">4</span>,<span class="dv">4</span>, figsize<span class="op">=</span>(<span class="dv">20</span>,<span class="dv">5</span><span class="op">*</span>total<span class="op">//</span><span class="dv">4</span>))</span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a><span class="cf">else</span>:</span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a> fig, axs <span class="op">=</span> plt.subplots(<span class="dv">1</span>,total, figsize<span class="op">=</span>(<span class="dv">20</span>,total,<span class="dv">5</span>))</span>
<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a> axs <span class="op">=</span> axs[<span class="va">None</span>,:]</span>
<span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> el <span class="kw">in</span> <span class="bu">range</span>(total):</span>
<span id="cb9-8"><a href="#cb9-8" aria-hidden="true" tabindex="-1"></a> ax <span class="op">=</span> axs[el<span class="op">//</span><span class="dv">4</span>,el<span class="op">%</span><span class="dv">4</span>]</span>
<span id="cb9-9"><a href="#cb9-9" aria-hidden="true" tabindex="-1"></a> ax.bar(np.arange(logits.shape[<span class="op">-</span><span class="dv">1</span>]),</span>
<span id="cb9-10"><a href="#cb9-10" aria-hidden="true" tabindex="-1"></a> torch.softmax(torch.from_numpy(logits[<span class="dv">0</span>,el,:]), dim<span class="op">=</span><span class="dv">0</span>))</span>
<span id="cb9-11"><a href="#cb9-11" aria-hidden="true" tabindex="-1"></a> ax.axvline(output[<span class="st">"generated_tokens"</span>][<span class="dv">0</span>][el], color<span class="op">=</span><span class="st">"red"</span>, label<span class="op">=</span><span class="st">"Prediction: "</span><span class="op">+</span>ID_TO_AA[output[<span class="st">"generated_tokens"</span>][<span class="dv">0</span>][el]] <span class="op">+</span> <span class="ss">f" (</span><span class="sc">{</span>output[<span class="st">'generated_tokens'</span>][<span class="dv">0</span>][el]<span class="sc">}</span><span class="ss">)"</span>, linewidth<span class="op">=</span><span class="fl">0.5</span>)</span>
<span id="cb9-12"><a href="#cb9-12" aria-hidden="true" tabindex="-1"></a> <span class="co"># ax.axvline(tokens_fim[0,el].cpu().numpy(), color="k",label="Original: "+ID_TO_AA[tokens_fim[0,el].cpu().numpy()] +f" ({tokens_fim[0,el].cpu().numpy()})", linewidth=0.5)</span></span>
<span id="cb9-13"><a href="#cb9-13" aria-hidden="true" tabindex="-1"></a> ax.legend()</span>
<span id="cb9-14"><a href="#cb9-14" aria-hidden="true" tabindex="-1"></a>fig.suptitle(<span class="ss">f"Real sequence: </span><span class="sc">{</span>input_continuation<span class="sc">}</span><span class="ch">\n</span><span class="ss">Pred sequence: </span><span class="sc">{</span>output_seq[<span class="dv">0</span>]<span class="sc">}</span><span class="ss">"</span>)</span>
<span id="cb9-15"><a href="#cb9-15" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="generate-new-sequences-using-the-dataset-and-by-sampling-amino-acids-autoregressively-from-cls" class="level3">
<h3 class="anchored" data-anchor-id="generate-new-sequences-using-the-dataset-and-by-sampling-amino-acids-autoregressively-from-cls">Generate new sequences using the Dataset and by sampling amino acids autoregressively from <code><cls></code></h3>
<div class="sourceCode" id="cb10"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Generate the new sequence</span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a>L <span class="op">=</span> <span class="dv">650</span><span class="co">#628#</span></span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a>output <span class="op">=</span> generate_sequence(model,</span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a> tokens[:,:L],</span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a> position_ids<span class="op">=</span>pos_ids[:,:L],</span>
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a> is_fim<span class="op">=</span><span class="va">False</span>,</span>
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a> max_length<span class="op">=</span><span class="dv">1570</span>,</span>
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a> temperature<span class="op">=</span><span class="fl">1.</span>,</span>
<span id="cb10-9"><a href="#cb10-9" aria-hidden="true" tabindex="-1"></a> top_k<span class="op">=</span><span class="dv">10</span>,</span>
<span id="cb10-10"><a href="#cb10-10" aria-hidden="true" tabindex="-1"></a> top_p<span class="op">=</span><span class="fl">0.0</span>,</span>
<span id="cb10-11"><a href="#cb10-11" aria-hidden="true" tabindex="-1"></a> return_dict_in_generate<span class="op">=</span><span class="va">True</span>,</span>
<span id="cb10-12"><a href="#cb10-12" aria-hidden="true" tabindex="-1"></a> output_scores<span class="op">=</span><span class="va">True</span>,</span>
<span id="cb10-13"><a href="#cb10-13" aria-hidden="true" tabindex="-1"></a> eos_token_id<span class="op">=</span>torch.tensor([AA_TO_ID[<span class="st">"<cls>"</span>],AA_TO_ID[<span class="st">"<mask-1>"</span>], AA_TO_ID[<span class="st">"<mask-2>"</span>], AA_TO_ID[<span class="st">"<mask-3>"</span>],</span>
<span id="cb10-14"><a href="#cb10-14" aria-hidden="true" tabindex="-1"></a> AA_TO_ID[<span class="st">"<mask-4>"</span>], AA_TO_ID[<span class="st">"<mask-5>"</span>]]).to(<span class="st">"cuda"</span>),</span>
<span id="cb10-15"><a href="#cb10-15" aria-hidden="true" tabindex="-1"></a> device<span class="op">=</span><span class="st">"cuda"</span>)</span>
<span id="cb10-16"><a href="#cb10-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-17"><a href="#cb10-17" aria-hidden="true" tabindex="-1"></a>input_seq, output_seq <span class="op">=</span> output[<span class="st">"input"</span>], output[<span class="st">"generated"</span>]</span>
<span id="cb10-18"><a href="#cb10-18" aria-hidden="true" tabindex="-1"></a>logits <span class="op">=</span> output[<span class="st">"scores"</span>]</span>
<span id="cb10-19"><a href="#cb10-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-20"><a href="#cb10-20" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"All input context (len = </span><span class="sc">{</span><span class="bu">len</span>(input_seq[<span class="dv">0</span>])<span class="sc">}</span><span class="ss">):</span><span class="ch">\n</span><span class="ss">"</span>, input_seq[<span class="dv">0</span>])</span>
<span id="cb10-21"><a href="#cb10-21" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Last sequence where the masked parts should be predicted:</span><span class="ch">\n</span><span class="st">"</span>, input_seq[<span class="dv">0</span>].split(<span class="st">"<cls>"</span>)[<span class="op">-</span><span class="dv">1</span>])</span>
<span id="cb10-22"><a href="#cb10-22" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Generated (len = </span><span class="sc">{</span><span class="bu">len</span>(output_seq[<span class="dv">0</span>])<span class="sc">}</span><span class="ss">):</span><span class="ch">\n</span><span class="ss">"</span>, output_seq[<span class="dv">0</span>])</span>
<span id="cb10-23"><a href="#cb10-23" aria-hidden="true" tabindex="-1"></a>input_continuation <span class="op">=</span> decode_sequence(tokens[<span class="dv">0</span>,L:].cpu().numpy()).split(<span class="st">"<cls>"</span>)[<span class="dv">0</span>]<span class="op">+</span><span class="st">"<cls>"</span></span>
<span id="cb10-24"><a href="#cb10-24" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Continuation of input:</span><span class="ch">\n</span><span class="ss">"</span>, input_continuation)</span>
<span id="cb10-25"><a href="#cb10-25" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"</span><span class="ch">\n</span><span class="ss">Logits (shape = </span><span class="sc">{</span>logits<span class="sc">.</span>shape<span class="sc">}</span><span class="ss">)"</span>)</span>
<span id="cb10-26"><a href="#cb10-26" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"</span><span class="ch">\n</span><span class="st">Stops if the model predicts a mask token"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode" id="cb11"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>fig, axs <span class="op">=</span> plt.subplots(<span class="dv">4</span>,<span class="dv">4</span>, figsize<span class="op">=</span>(<span class="dv">20</span>,<span class="dv">10</span>))</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> el <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">16</span>):</span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a> ax <span class="op">=</span> axs[el<span class="op">//</span><span class="dv">4</span>,el<span class="op">%</span><span class="dv">4</span>]</span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a> ax.bar(np.arange(logits.shape[<span class="op">-</span><span class="dv">1</span>]),</span>
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a> torch.softmax(torch.from_numpy(logits[<span class="dv">0</span>,el,:]), dim<span class="op">=</span><span class="dv">0</span>))</span>
<span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a> ax.axvline(output[<span class="st">"generated_tokens"</span>][<span class="dv">0</span>][el], color<span class="op">=</span><span class="st">"red"</span>, label<span class="op">=</span><span class="st">"Prediction: "</span><span class="op">+</span>output_seq[<span class="dv">0</span>][el] <span class="op">+</span> <span class="ss">f" (</span><span class="sc">{</span>AA_TO_ID[output_seq[<span class="dv">0</span>][el]]<span class="sc">}</span><span class="ss">)"</span>, linewidth<span class="op">=</span><span class="fl">0.5</span>)</span>
<span id="cb11-7"><a href="#cb11-7" aria-hidden="true" tabindex="-1"></a> ax.axvline(tokens[<span class="dv">0</span>,L<span class="op">+</span>el].cpu().numpy(), color<span class="op">=</span><span class="st">"k"</span>,label<span class="op">=</span><span class="st">"Original: "</span><span class="op">+</span>input_continuation[el] <span class="op">+</span><span class="ss">f" (</span><span class="sc">{</span>AA_TO_ID[input_continuation[el]]<span class="sc">}</span><span class="ss">)"</span>, linewidth<span class="op">=</span><span class="fl">0.5</span>)</span>
<span id="cb11-8"><a href="#cb11-8" aria-hidden="true" tabindex="-1"></a> ax.legend()</span>
<span id="cb11-9"><a href="#cb11-9" aria-hidden="true" tabindex="-1"></a>fig.suptitle(<span class="ss">f"Real sequence: </span><span class="sc">{</span>input_continuation[:<span class="dv">16</span>]<span class="sc">}</span><span class="ch">\n</span><span class="ss">Pred sequence: </span><span class="sc">{</span>output_seq[<span class="dv">0</span>][:<span class="dv">16</span>]<span class="sc">}</span><span class="ss">"</span>)</span>
<span id="cb11-10"><a href="#cb11-10" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="get-the-hidden-states-at-each-layer" class="level3">
<h3 class="anchored" data-anchor-id="get-the-hidden-states-at-each-layer">Get the hidden states at each layer</h3>
<div class="sourceCode" id="cb12"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>which_layers <span class="op">=</span> <span class="bu">list</span>(<span class="bu">range</span>(<span class="dv">1</span>,model.config.n_layer<span class="op">+</span><span class="dv">1</span>))</span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> get_hidden_states(model, tokens, which_layers, position_ids<span class="op">=</span><span class="va">None</span>, seq_position_ids<span class="op">=</span><span class="va">None</span>):</span>
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a> hidden_states <span class="op">=</span> model(input_ids<span class="op">=</span>tokens,</span>
<span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a> save_layer<span class="op">=</span>which_layers,</span>
<span id="cb12-6"><a href="#cb12-6" aria-hidden="true" tabindex="-1"></a> position_ids<span class="op">=</span>position_ids,</span>
<span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a> seq_position_ids<span class="op">=</span>seq_position_ids)</span>
<span id="cb12-8"><a href="#cb12-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> hidden_states</span>
<span id="cb12-9"><a href="#cb12-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-10"><a href="#cb12-10" aria-hidden="true" tabindex="-1"></a>hidden_states <span class="op">=</span> get_hidden_states(model, tokens[:,:<span class="dv">10</span>], which_layers, pos_ids[:,:<span class="dv">10</span>])</span>
<span id="cb12-11"><a href="#cb12-11" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Saved hidden states for layers: </span><span class="sc">{</span>hidden_states<span class="sc">.</span>keys()<span class="sc">}</span><span class="ss">"</span>)</span>
<span id="cb12-12"><a href="#cb12-12" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Shape of hidden state of layer 1: </span><span class="sc">{</span>hidden_states[<span class="dv">1</span>]<span class="sc">.</span>shape<span class="sc">}</span><span class="ss">"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
</section>
<section id="citation" class="level2">
<h2 class="anchored" data-anchor-id="citation">Citation</h2>
<p>To cite this work, please refer to the following publication:</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode bibtex code-with-copy"><code class="sourceCode bibtex"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="va">@article</span>{<span class="ot">sgarbossa2024protmamba</span>,</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a> <span class="dt">title</span>={{ProtMamba}: -- a homology-aware but alignment-free protein state space model},</span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a> <span class="dt">author</span>={Damiano Sgarbossa and Cyril Malbranke and Anne-Florence Bitbol},</span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a> <span class="dt">journal</span>={bioRxiv},</span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a> <span class="dt">doi</span> = {10.1101/2024.05.24.595730},</span>
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a> <span class="dt">year</span>={2024},</span>
<span id="cb13-7"><a href="#cb13-7" aria-hidden="true" tabindex="-1"></a> <span class="dt">url</span>={https://www.biorxiv.org/content/early/2024/05/25/2024.05.24.595730}</span>
<span id="cb13-8"><a href="#cb13-8" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</section>
<section id="nbdev" class="level2">
<h2 class="anchored" data-anchor-id="nbdev">nbdev</h2>
<p>Project developed using <a href="https://nbdev.fast.ai/">nbdev</a>.</p>
</section>
</section>
</main>
<!-- /main column -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const isCodeAnnotation = (el) => {
for (const clz of el.classList) {
if (clz.startsWith('code-annotation-')) {
return true;
}
}
return false;
}
const clipboard = new window.ClipboardJS('.code-copy-button', {
text: function(trigger) {
const codeEl = trigger.previousElementSibling.cloneNode(true);
for (const childEl of codeEl.children) {
if (isCodeAnnotation(childEl)) {
childEl.remove();
}
}
return codeEl.innerText;
}
});
clipboard.on('success', function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
let tooltip;
if (window.bootstrap) {
button.setAttribute("data-bs-toggle", "tooltip");
button.setAttribute("data-bs-placement", "left");
button.setAttribute("data-bs-title", "Copied!");
tooltip = new bootstrap.Tooltip(button,
{ trigger: "manual",
customClass: "code-copy-button-tooltip",
offset: [0, -8]});
tooltip.show();
}
setTimeout(function() {
if (tooltip) {
tooltip.hide();
button.removeAttribute("data-bs-title");
button.removeAttribute("data-bs-toggle");
button.removeAttribute("data-bs-placement");
}
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
function tippyHover(el, contentFn) {
const config = {
allowHTML: true,
content: contentFn,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start'
};
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
// use id or data attribute instead here
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
return note.innerHTML;
});
}
let selectedAnnoteEl;
const selectorForAnnotation = ( cell, annotation) => {
let cellAttr = 'data-code-cell="' + cell + '"';
let lineAttr = 'data-code-annotation="' + annotation + '"';
const selector = 'span[' + cellAttr + '][' + lineAttr + ']';
return selector;
}
const selectCodeLines = (annoteEl) => {
const doc = window.document;
const targetCell = annoteEl.getAttribute("data-target-cell");
const targetAnnotation = annoteEl.getAttribute("data-target-annotation");
const annoteSpan = window.document.querySelector(selectorForAnnotation(targetCell, targetAnnotation));
const lines = annoteSpan.getAttribute("data-code-lines").split(",");
const lineIds = lines.map((line) => {
return targetCell + "-" + line;
})
let top = null;
let height = null;
let parent = null;
if (lineIds.length > 0) {
//compute the position of the single el (top and bottom and make a div)
const el = window.document.getElementById(lineIds[0]);
top = el.offsetTop;
height = el.offsetHeight;
parent = el.parentElement.parentElement;
if (lineIds.length > 1) {
const lastEl = window.document.getElementById(lineIds[lineIds.length - 1]);
const bottom = lastEl.offsetTop + lastEl.offsetHeight;
height = bottom - top;
}
if (top !== null && height !== null && parent !== null) {
// cook up a div (if necessary) and position it
let div = window.document.getElementById("code-annotation-line-highlight");
if (div === null) {
div = window.document.createElement("div");
div.setAttribute("id", "code-annotation-line-highlight");
div.style.position = 'absolute';
parent.appendChild(div);
}
div.style.top = top - 2 + "px";
div.style.height = height + 4 + "px";
let gutterDiv = window.document.getElementById("code-annotation-line-highlight-gutter");
if (gutterDiv === null) {
gutterDiv = window.document.createElement("div");
gutterDiv.setAttribute("id", "code-annotation-line-highlight-gutter");
gutterDiv.style.position = 'absolute';
const codeCell = window.document.getElementById(targetCell);
const gutter = codeCell.querySelector('.code-annotation-gutter');
gutter.appendChild(gutterDiv);
}
gutterDiv.style.top = top - 2 + "px";
gutterDiv.style.height = height + 4 + "px";
}
selectedAnnoteEl = annoteEl;
}
};
const unselectCodeLines = () => {
const elementsIds = ["code-annotation-line-highlight", "code-annotation-line-highlight-gutter"];
elementsIds.forEach((elId) => {
const div = window.document.getElementById(elId);
if (div) {
div.remove();
}
});
selectedAnnoteEl = undefined;
};
// Attach click handler to the DT
const annoteDls = window.document.querySelectorAll('dt[data-target-cell]');
for (const annoteDlNode of annoteDls) {
annoteDlNode.addEventListener('click', (event) => {
const clickedEl = event.target;
if (clickedEl !== selectedAnnoteEl) {
unselectCodeLines();
const activeEl = window.document.querySelector('dt[data-target-cell].code-annotation-active');
if (activeEl) {
activeEl.classList.remove('code-annotation-active');
}
selectCodeLines(clickedEl);
clickedEl.classList.add('code-annotation-active');
} else {
// Unselect the line
unselectCodeLines();
clickedEl.classList.remove('code-annotation-active');
}
});
}
const findCites = (el) => {
const parentEl = el.parentElement;
if (parentEl) {
const cites = parentEl.dataset.cites;
if (cites) {
return {
el,
cites: cites.split(' ')
};
} else {
return findCites(el.parentElement)
}
} else {
return undefined;
}
};
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const citeInfo = findCites(ref);
if (citeInfo) {
tippyHover(citeInfo.el, function() {
var popup = window.document.createElement('div');
citeInfo.cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});
}
}
});
</script>
</div> <!-- /content -->
</body></html>