Skip to content

Transformer specs

BiobertSpecOption

Bases: str, Enum

Options for the BiobertSpec. The spec defines the architecture of the transformer (BERT) block in the biobert model. This is a str, Enum type so that argparse can use the string names as choices.

Source code in bionemo/llm/model/biobert/transformer_specs.py
47
48
49
50
51
52
53
54
55
56
57
58
class BiobertSpecOption(str, Enum):
    """Options for the BiobertSpec. The spec defines the architecture of the transformer (BERT) block in the biobert model.
    This is a `str, Enum` type so that argparse can use the string names as choices.
    """  # noqa: D205

    bert_layer_local_spec = "bert_layer_local_spec"
    bert_layer_local_spec_with_qk_ln = "bert_layer_local_spec_with_qk_ln"
    bert_layer_with_transformer_engine_spec = "bert_layer_with_transformer_engine_spec"
    bert_layer_with_transformer_engine_and_qk_ln_spec = "bert_layer_with_transformer_engine_and_qk_ln_spec"
    # ESM2 spec
    esm2_bert_layer_local_spec = "esm2_bert_layer_local_spec"
    esm2_bert_layer_with_transformer_engine_spec = "esm2_bert_layer_with_transformer_engine_spec"

get_biobert_spec(biobert_spec_option, qk_layernorm=False, core_attention=None)

Get the spec for the Biobert model.

Parameters:

Name Type Description Default
model_type ModelType

The model type.

required
spec_option BiobertSpecOption

The spec option.

required

Returns:

Name Type Description
TransformerConfig ModuleSpec

The Biobert spec.

Source code in bionemo/llm/model/biobert/transformer_specs.py
 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
def get_biobert_spec(  # noqa: D417
    biobert_spec_option: BiobertSpecOption,
    qk_layernorm: bool = False,
    core_attention: Optional[Type[Module]] = None,
) -> spec_utils.ModuleSpec:
    """Get the spec for the Biobert model.

    Args:
        model_type (ModelType): The model type.
        spec_option (BiobertSpecOption): The spec option.

    Returns:
        TransformerConfig: The Biobert spec.
    """
    #
    # BEGIN define several specs that are a function of `qk_layernorm`
    #

    match biobert_spec_option:
        case BiobertSpecOption.bert_layer_local_spec:
            return bert_layer_specs.bert_layer_local_spec

        case BiobertSpecOption.bert_layer_local_spec_with_qk_ln:
            # Use this spec for an implementation using only modules in megatron core

            if core_attention is None:
                core_attention = DotProductAttention

            bert_layer_local_spec_with_qk_ln = spec_utils.ModuleSpec(
                module=TransformerLayer,
                submodules=TransformerLayerSubmodules(
                    input_layernorm=FusedLayerNorm,
                    self_attention=spec_utils.ModuleSpec(
                        module=SelfAttention,
                        params={"attn_mask_type": AttnMaskType.padding},
                        submodules=SelfAttentionSubmodules(
                            linear_qkv=ColumnParallelLinear,
                            core_attention=core_attention,
                            linear_proj=RowParallelLinear,
                            q_layernorm=FusedLayerNorm if qk_layernorm else IdentityOp,
                            k_layernorm=FusedLayerNorm if qk_layernorm else IdentityOp,
                        ),
                    ),
                    self_attn_bda=get_bias_dropout_add,
                    pre_mlp_layernorm=FusedLayerNorm,
                    mlp=spec_utils.ModuleSpec(
                        module=MLP,
                        submodules=MLPSubmodules(
                            linear_fc1=ColumnParallelLinear,
                            linear_fc2=RowParallelLinear,
                        ),
                    ),
                    mlp_bda=get_bias_dropout_add,
                    sharded_state_dict_keys_map={
                        "input_layernorm.": "self_attention.linear_qkv.layer_norm_",
                        "pre_mlp_layernorm.": "mlp.linear_fc1.layer_norm_",
                    },
                ),
            )
            return bert_layer_local_spec_with_qk_ln

        case BiobertSpecOption.bert_layer_with_transformer_engine_spec:
            return bert_layer_specs.bert_layer_with_transformer_engine_spec

        case BiobertSpecOption.bert_layer_with_transformer_engine_and_qk_ln_spec:
            if core_attention is None:
                core_attention = TEDotProductAttention

            bert_layer_with_transformer_engine_and_qk_ln_spec = spec_utils.ModuleSpec(
                module=TransformerLayer,
                submodules=TransformerLayerSubmodules(
                    self_attention=spec_utils.ModuleSpec(
                        module=SelfAttention,
                        params={"attn_mask_type": AttnMaskType.padding},
                        submodules=SelfAttentionSubmodules(
                            linear_qkv=TELayerNormColumnParallelLinear,
                            core_attention=core_attention,
                            linear_proj=TERowParallelLinear,
                            q_layernorm=TELayerNorm if qk_layernorm else IdentityOp,
                            k_layernorm=TELayerNorm if qk_layernorm else IdentityOp,
                        ),
                    ),
                    self_attn_bda=get_bias_dropout_add,
                    mlp=spec_utils.ModuleSpec(
                        module=MLP,
                        submodules=MLPSubmodules(
                            linear_fc1=TELayerNormColumnParallelLinear,
                            linear_fc2=TERowParallelLinear,
                        ),
                    ),
                    mlp_bda=get_bias_dropout_add,
                ),
            )
            return bert_layer_with_transformer_engine_and_qk_ln_spec

        case BiobertSpecOption.esm2_bert_layer_local_spec:
            if core_attention is None:
                raise ValueError(f"Must supply core_attention with {BiobertSpecOption.esm2_bert_layer_local_spec} !")

            esm2_bert_layer_local_spec = spec_utils.ModuleSpec(
                module=TransformerLayer,
                submodules=TransformerLayerSubmodules(
                    input_layernorm=FusedLayerNorm,
                    self_attention=spec_utils.ModuleSpec(
                        module=SelfAttention,
                        params={"attn_mask_type": AttnMaskType.padding},
                        submodules=SelfAttentionSubmodules(
                            linear_qkv=ColumnParallelLinear,
                            core_attention=core_attention,
                            linear_proj=RowParallelLinear,
                            q_layernorm=ESM2QueryScaling,
                            k_layernorm=IdentityOp,
                        ),
                    ),
                    self_attn_bda=get_bias_dropout_add,
                    pre_mlp_layernorm=FusedLayerNorm,
                    mlp=spec_utils.ModuleSpec(
                        module=MLP,
                        submodules=MLPSubmodules(
                            linear_fc1=ColumnParallelLinear,
                            linear_fc2=RowParallelLinear,
                        ),
                    ),
                    mlp_bda=get_bias_dropout_add,
                    sharded_state_dict_keys_map={
                        "input_layernorm.": "self_attention.linear_qkv.layer_norm_",
                        "pre_mlp_layernorm.": "mlp.linear_fc1.layer_norm_",
                    },
                ),
            )
            return esm2_bert_layer_local_spec

        case BiobertSpecOption.esm2_bert_layer_with_transformer_engine_spec:
            if core_attention is None:
                core_attention = TEDotProductAttention

            esm2_bert_layer_local_spec = spec_utils.ModuleSpec(
                module=TransformerLayer,
                submodules=TransformerLayerSubmodules(
                    self_attention=spec_utils.ModuleSpec(
                        module=SelfAttention,
                        params={"attn_mask_type": AttnMaskType.padding},
                        submodules=SelfAttentionSubmodules(
                            linear_qkv=TELayerNormColumnParallelLinear,
                            core_attention=core_attention,
                            linear_proj=TERowParallelLinear,
                            q_layernorm=ESM2QueryScaling,
                            k_layernorm=IdentityOp,
                        ),
                    ),
                    self_attn_bda=get_bias_dropout_add,
                    mlp=spec_utils.ModuleSpec(
                        module=MLP,
                        submodules=MLPSubmodules(
                            linear_fc1=TELayerNormColumnParallelLinear,
                            linear_fc2=TERowParallelLinear,
                        ),
                    ),
                    mlp_bda=get_bias_dropout_add,
                ),
            )
            return esm2_bert_layer_local_spec

        case _:
            raise NotImplementedError(f"Spec option {biobert_spec_option} not implemented")