File size: 2,367 Bytes
e76b79a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

def _test_esm1b(alphabet):
    import torch

    batch_converter = alphabet.get_batch_converter()

    data = [
        ("protein1", "MKTVRQG"),
        ("protein2 with mask", "KALTA<mask>ISQP"),
        ("protein3", "K A <mask> I S Q"),
    ]
    _, _, batch_tokens = batch_converter(data)
    expected_tokens = torch.tensor(
        [
            [0, 20, 15, 11, 7, 10, 16, 6, 2, 1, 1, 1],
            [0, 15, 5, 4, 11, 5, 32, 12, 8, 16, 14, 2],
            [0, 15, 5, 32, 12, 8, 16, 2, 1, 1, 1, 1],
        ]
    )
    assert torch.allclose(batch_tokens, expected_tokens)


def _test_esm1b_truncation(alphabet):
    import torch

    batch_converter = alphabet.get_batch_converter(truncation_seq_length=10)

    data = [
        ("protein1", "MKTVRQGMKTVRQG"),
        ("protein2 with mask", "KALTA<mask>ISQPISQP"),
        ("protein3", "K A <mask> I S Q"),
    ]
    _, _, batch_tokens = batch_converter(data)
    expected_tokens = torch.tensor(
        [
            [0, 20, 15, 11, 7, 10, 16, 6, 20, 15, 11, 2],
            [0, 15, 5, 4, 11, 5, 32, 12, 8, 16, 14, 2],
            [0, 15, 5, 32, 12, 8, 16, 2, 1, 1, 1, 1],
        ]
    )
    assert torch.allclose(batch_tokens, expected_tokens)


def test_esm1b_alphabet():
    import esm

    _, alphabet = esm.pretrained.esm1b_t33_650M_UR50S()
    _test_esm1b(alphabet)
    _test_esm1b_truncation(alphabet)


def test_esm1v_alphabet():
    import esm

    _, alphabet = esm.pretrained.esm1v_t33_650M_UR90S_1()
    _test_esm1b(alphabet)
    _test_esm1b_truncation(alphabet)


def test_esm1_msa1b_alphabet():
    import torch
    import esm

    # Load ESM-1b model
    _, alphabet = esm.pretrained.esm_msa1b_t12_100M_UR50S()
    batch_converter = alphabet.get_batch_converter()

    data = [
        ("protein1", "MKTVRQG"),
        ("protein2", "KALTRAI"),
        ("protein3", "KAAISQQ"),
    ]
    _, _, batch_tokens = batch_converter(data)
    expected_tokens = torch.tensor(
        [
            [
                [0, 20, 15, 11, 7, 10, 16, 6],
                [0, 15, 5, 4, 11, 10, 5, 12],
                [0, 15, 5, 5, 12, 8, 16, 16],
            ]
        ]
    )
    assert torch.allclose(batch_tokens, expected_tokens)