in optimum/bettertransformer/models/encoder_models.py [0:0]
def __init__(self, bart_layer, config):
r"""
A simple conversion of the `BartEncoderLayer` to its `BetterTransformer` implementation.
Args:
bart_layer (`torch.nn.Module`):
The original `BartEncoderLayer` where the weights needs to be retrieved.
"""
super().__init__(config)
super(BetterTransformerBaseLayer, self).__init__()
# In_proj layer
self.in_proj_weight = nn.Parameter(
torch.cat(
[
bart_layer.self_attn.q_proj.weight,
bart_layer.self_attn.k_proj.weight,
bart_layer.self_attn.v_proj.weight,
]
)
)
self.in_proj_bias = nn.Parameter(
torch.cat(
[
bart_layer.self_attn.q_proj.bias,
bart_layer.self_attn.k_proj.bias,
bart_layer.self_attn.v_proj.bias,
]
)
)
# Out proj layer
self.out_proj_weight = bart_layer.self_attn.out_proj.weight
self.out_proj_bias = bart_layer.self_attn.out_proj.bias
# Linear layer 1
self.linear1_weight = bart_layer.fc1.weight
self.linear1_bias = bart_layer.fc1.bias
# Linear layer 2
self.linear2_weight = bart_layer.fc2.weight
self.linear2_bias = bart_layer.fc2.bias
# Layer norm 1
self.norm1_eps = bart_layer.self_attn_layer_norm.eps
self.norm1_weight = bart_layer.self_attn_layer_norm.weight
self.norm1_bias = bart_layer.self_attn_layer_norm.bias
# Layer norm 2
self.norm2_eps = bart_layer.final_layer_norm.eps
self.norm2_weight = bart_layer.final_layer_norm.weight
self.norm2_bias = bart_layer.final_layer_norm.bias
# Model hyper parameters
self.num_heads = bart_layer.self_attn.num_heads
self.embed_dim = bart_layer.self_attn.embed_dim
# Last step: set the last layer to `False` -> this will be set to `True` when converting the model
self.is_last_layer = False
self.original_layers_mapping = {
"in_proj_weight": ["self_attn.q_proj.weight", "self_attn.k_proj.weight", "self_attn.v_proj.weight"],
"in_proj_bias": ["self_attn.q_proj.bias", "self_attn.k_proj.bias", "self_attn.v_proj.bias"],
"out_proj_weight": "self_attn.out_proj.weight",
"out_proj_bias": "self_attn.out_proj.bias",
"linear1_weight": "fc1.weight",
"linear1_bias": "fc1.bias",
"linear2_weight": "fc2.weight",
"linear2_bias": "fc2.bias",
"norm1_eps": "self_attn_layer_norm.eps",
"norm1_weight": "self_attn_layer_norm.weight",
"norm1_bias": "self_attn_layer_norm.bias",
"norm2_eps": "final_layer_norm.eps",
"norm2_weight": "final_layer_norm.weight",
"norm2_bias": "final_layer_norm.bias",
}
self.dropout = config.attention_dropout
self.activation_dropout = config.activation_dropout
self.attention_head_size = config.d_model // config.encoder_attention_heads
self.act_fn_callable = ACT2FN[self.act_fn]
self.validate_bettertransformer()