-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/airaria/TextPruner
- Loading branch information
Showing
14 changed files
with
482 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from .utils import DefaultModelVocabResizer | ||
from .model_structure import ModelStructure | ||
import torch | ||
from torch import nn | ||
class BartVocabResizer(DefaultModelVocabResizer): | ||
model_name : str = 'bart' | ||
|
||
@classmethod | ||
def set_embeddings(cls, model, token_ids): | ||
def _prun(old_weight, token_ids): | ||
pruned_word_embeddings_weight = torch.index_select( | ||
old_weight, 0, index=torch.LongTensor(token_ids).to(old_weight.device)) | ||
return pruned_word_embeddings_weight | ||
|
||
old_word_embeddings_shared, old_word_embeddings_encoder, old_word_embeddings_decoder = \ | ||
model.shared, model.encoder.embed_tokens, model.decoder.embed_tokens | ||
|
||
old_word_embeddings_shared_weight, old_word_embeddings_encoder_weight, old_word_embeddings_decoder_weight = \ | ||
old_word_embeddings_shared.weight, old_word_embeddings_encoder.weight, old_word_embeddings_decoder.weight | ||
|
||
pruned_word_embeddings_shared_weight, pruned_word_embeddings_encoder_weight, pruned_word_embeddings_decoder_weight = \ | ||
_prun(old_word_embeddings_shared_weight, token_ids), _prun(old_word_embeddings_encoder_weight, token_ids), _prun(old_word_embeddings_decoder_weight, token_ids) | ||
|
||
pruned_num_tokens, embedding_dim = pruned_word_embeddings_shared_weight.shape | ||
|
||
pruned_word_embeddings_shared = nn.Embedding( | ||
pruned_num_tokens, embedding_dim).to(old_word_embeddings_shared_weight.device) | ||
pruned_word_embeddings_shared.weight.data[:] = pruned_word_embeddings_shared_weight[:] | ||
|
||
pruned_word_embeddings_encoder = nn.Embedding( | ||
pruned_num_tokens, embedding_dim).to(old_word_embeddings_shared_weight.device) | ||
pruned_word_embeddings_encoder.weight.data[:] = pruned_word_embeddings_encoder_weight[:] | ||
|
||
pruned_word_embeddings_decoder = nn.Embedding( | ||
pruned_num_tokens, embedding_dim).to(old_word_embeddings_shared_weight.device) | ||
pruned_word_embeddings_decoder.weight.data[:] = pruned_word_embeddings_decoder_weight[:] | ||
|
||
model.shared = pruned_word_embeddings_shared | ||
model.encoder.embed_tokens = pruned_word_embeddings_encoder | ||
model.decoder.embed_tokens = pruned_word_embeddings_decoder | ||
|
||
class BartStructure(ModelStructure): | ||
MODEL_PREFIX: str = "model." | ||
ENCODER_PREFIX: str = r"encoder.layers.[0-9]+\." | ||
LAYER_PATTERNS = dict( | ||
query="self_attn.q_proj", | ||
key="self_attn.k_proj", | ||
value="self_attn.v_proj", | ||
att_dense="self_attn.out_proj", | ||
interm_dense="fc1", | ||
output_dense="fc2", | ||
) | ||
ATTENTION_PREFIX = ("self_attn",) | ||
ATTENTION_LAYERS = ("q_proj", "k_proj", "v_proj") | ||
MHA_LAYERS = ATTENTION_LAYERS + ("att_dense",) | ||
NAME_CONFIG = dict( | ||
hidden_size="d_model", | ||
intermediate_size="encoder_ffn_dim", | ||
num_hidden_layers="encoder_layers", | ||
num_attention_heads="num_attention_heads", | ||
attention_head_size="", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from .utils import DefaultModelVocabResizer | ||
from .model_structure import ModelStructure | ||
import torch | ||
from torch import nn | ||
class MT5VocabResizer(DefaultModelVocabResizer): | ||
model_name : str = 'mt5' | ||
|
||
@classmethod | ||
def set_embeddings(cls, model, token_ids): | ||
def _prun(old_weight, token_ids): | ||
pruned_word_embeddings_weight = torch.index_select( | ||
old_weight, 0, index=torch.LongTensor(token_ids).to(old_weight.device)) | ||
return pruned_word_embeddings_weight | ||
|
||
|
||
vocab_size = model.shared.weight.shape[0] | ||
max_token_ids = token_ids[-1] | ||
tokens_in_embed_notin_tokenizer_ids = list(range(max_token_ids+1, vocab_size)) | ||
token_ids_temp = token_ids[:] | ||
token_ids_temp.extend(tokens_in_embed_notin_tokenizer_ids) | ||
|
||
|
||
model.config.vocab_size = len(token_ids_temp) | ||
|
||
old_word_embeddings_shared, old_word_embeddings_encoder, old_word_embeddings_decoder = \ | ||
model.shared, model.encoder.embed_tokens, model.decoder.embed_tokens | ||
|
||
old_word_embeddings_shared_weight, old_word_embeddings_encoder_weight, old_word_embeddings_decoder_weight = \ | ||
old_word_embeddings_shared.weight, old_word_embeddings_encoder.weight, old_word_embeddings_decoder.weight | ||
|
||
pruned_word_embeddings_shared_weight, pruned_word_embeddings_encoder_weight, pruned_word_embeddings_decoder_weight = \ | ||
_prun(old_word_embeddings_shared_weight, token_ids_temp), _prun(old_word_embeddings_encoder_weight, token_ids_temp), _prun(old_word_embeddings_decoder_weight, token_ids_temp) | ||
|
||
pruned_num_tokens, embedding_dim = pruned_word_embeddings_shared_weight.shape | ||
|
||
pruned_word_embeddings_shared = nn.Embedding( | ||
pruned_num_tokens, embedding_dim).to(old_word_embeddings_shared_weight.device) | ||
pruned_word_embeddings_shared.weight.data[:] = pruned_word_embeddings_shared_weight[:] | ||
|
||
pruned_word_embeddings_encoder = nn.Embedding( | ||
pruned_num_tokens, embedding_dim).to(old_word_embeddings_shared_weight.device) | ||
pruned_word_embeddings_encoder.weight.data[:] = pruned_word_embeddings_encoder_weight[:] | ||
|
||
pruned_word_embeddings_decoder = nn.Embedding( | ||
pruned_num_tokens, embedding_dim).to(old_word_embeddings_shared_weight.device) | ||
pruned_word_embeddings_decoder.weight.data[:] = pruned_word_embeddings_decoder_weight[:] | ||
|
||
model.shared = pruned_word_embeddings_shared | ||
model.encoder.embed_tokens = pruned_word_embeddings_encoder | ||
model.decoder.embed_tokens = pruned_word_embeddings_decoder | ||
|
||
class MT5Structure(ModelStructure): | ||
MODEL_PREFIX: str = "transformer." | ||
ENCODER_PREFIX: str = r"encoder.block.[0-9]+\.layer." | ||
LAYER_PATTERNS = dict( | ||
query="0.SelfAttention.q", | ||
key="0.SelfAttention.k", | ||
value="0.SelfAttention.v", | ||
att_dense="0.SelfAttention.o", | ||
interm_dense="1.DenseReluDense.wi", | ||
output_dense="1.DenseReluDense.wo", | ||
) | ||
ATTENTION_PREFIX = ("0.SelfAttention",) | ||
ATTENTION_LAYERS = ("q", "k", "v") | ||
MHA_LAYERS = ATTENTION_LAYERS + ("att_dense",) | ||
NAME_CONFIG = dict( | ||
hidden_size="d_model", | ||
intermediate_size="d_ff", | ||
num_hidden_layers="num_layers", | ||
num_attention_heads="num_heads", | ||
attention_head_size="", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from .utils import DefaultModelVocabResizer | ||
from .model_structure import ModelStructure | ||
import torch | ||
from torch import nn | ||
class T5VocabResizer(DefaultModelVocabResizer): | ||
model_name : str = 't5' | ||
|
||
@classmethod | ||
def set_embeddings(cls, model, token_ids): | ||
def _prun(old_weight, token_ids): | ||
pruned_word_embeddings_weight = torch.index_select( | ||
old_weight, 0, index=torch.LongTensor(token_ids).to(old_weight.device)) | ||
return pruned_word_embeddings_weight | ||
|
||
vocab_size = model.shared.weight.shape[0] | ||
max_token_ids = token_ids[-1] | ||
tokens_in_embed_notin_tokenizer_ids = list(range(max_token_ids+1, vocab_size)) | ||
token_ids_temp = token_ids[:] | ||
token_ids_temp.extend(tokens_in_embed_notin_tokenizer_ids) | ||
|
||
model.config.vocab_size = len(token_ids_temp) | ||
|
||
old_word_embeddings_shared, old_word_embeddings_encoder, old_word_embeddings_decoder = \ | ||
model.shared, model.encoder.embed_tokens, model.decoder.embed_tokens | ||
|
||
old_word_embeddings_shared_weight, old_word_embeddings_encoder_weight, old_word_embeddings_decoder_weight = \ | ||
old_word_embeddings_shared.weight, old_word_embeddings_encoder.weight, old_word_embeddings_decoder.weight | ||
|
||
pruned_word_embeddings_shared_weight, pruned_word_embeddings_encoder_weight, pruned_word_embeddings_decoder_weight = \ | ||
_prun(old_word_embeddings_shared_weight, token_ids_temp), _prun(old_word_embeddings_encoder_weight, token_ids_temp), _prun(old_word_embeddings_decoder_weight, token_ids_temp) | ||
|
||
pruned_num_tokens, embedding_dim = pruned_word_embeddings_shared_weight.shape | ||
|
||
pruned_word_embeddings_shared = nn.Embedding( | ||
pruned_num_tokens, embedding_dim).to(old_word_embeddings_shared_weight.device) | ||
pruned_word_embeddings_shared.weight.data[:] = pruned_word_embeddings_shared_weight[:] | ||
|
||
pruned_word_embeddings_encoder = nn.Embedding( | ||
pruned_num_tokens, embedding_dim).to(old_word_embeddings_shared_weight.device) | ||
pruned_word_embeddings_encoder.weight.data[:] = pruned_word_embeddings_encoder_weight[:] | ||
|
||
pruned_word_embeddings_decoder = nn.Embedding( | ||
pruned_num_tokens, embedding_dim).to(old_word_embeddings_shared_weight.device) | ||
pruned_word_embeddings_decoder.weight.data[:] = pruned_word_embeddings_decoder_weight[:] | ||
|
||
model.shared = pruned_word_embeddings_shared | ||
model.encoder.embed_tokens = pruned_word_embeddings_encoder | ||
model.decoder.embed_tokens = pruned_word_embeddings_decoder | ||
|
||
class T5Structure(ModelStructure): | ||
MODEL_PREFIX: str = "transformer." | ||
ENCODER_PREFIX: str = r"encoder.block.[0-9]+\.layer." | ||
LAYER_PATTERNS = dict( | ||
query="0.SelfAttention.q", | ||
key="0.SelfAttention.k", | ||
value="0.SelfAttention.v", | ||
att_dense="0.SelfAttention.o", | ||
interm_dense="1.DenseReluDense.wi", | ||
output_dense="1.DenseReluDense.wo", | ||
) | ||
ATTENTION_PREFIX = ("0.SelfAttention",) | ||
ATTENTION_LAYERS = ("q", "k", "v") | ||
MHA_LAYERS = ATTENTION_LAYERS + ("att_dense",) | ||
NAME_CONFIG = dict( | ||
hidden_size="d_model", | ||
intermediate_size="d_ff", | ||
num_hidden_layers="num_layers", | ||
num_attention_heads="num_heads", | ||
attention_head_size="", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from .utils import DefaultModelVocabResizer | ||
from .model_structure import ModelStructure | ||
import torch | ||
from torch import nn | ||
class XLMVocabResizer(DefaultModelVocabResizer): | ||
model_name : str = 'xlm' | ||
|
||
@classmethod | ||
def set_embeddings(cls, model, token_ids): | ||
# self.model.get_input_embeddings() | ||
|
||
if hasattr(model.embeddings, 'word_embeddings'): #XLM | ||
old_word_embeddings = model.embeddings.word_embeddings | ||
else: | ||
old_word_embeddings = model.embeddings | ||
|
||
|
||
|
||
# old_word_embeddings = model.embeddings.word_embeddings | ||
old_word_embeddings_weight = old_word_embeddings.weight | ||
|
||
pruned_word_embeddings_weight = torch.index_select( | ||
old_word_embeddings_weight, 0, index=torch.LongTensor(token_ids).to(old_word_embeddings_weight.device)) | ||
pruned_num_tokens, embedding_dim = pruned_word_embeddings_weight.shape | ||
|
||
pruned_word_embeddings = nn.Embedding( | ||
pruned_num_tokens, embedding_dim).to(old_word_embeddings_weight.device) | ||
pruned_word_embeddings.weight.data[:] = pruned_word_embeddings_weight[:] | ||
|
||
|
||
if hasattr(model.embeddings, 'word_embeddings'): | ||
model.embeddings.word_embeddings = pruned_word_embeddings | ||
else: | ||
model.embeddings = pruned_word_embeddings | ||
|
||
|
||
|
||
|
||
class XLMStructure(ModelStructure): | ||
MODEL_PREFIX: str = "transformer." | ||
ENCODER_PREFIX: str = r"attention.[0-9]+\." | ||
LAYER_PATTERNS = dict( | ||
query=r"attentions\.[0-9]+\.q_lin", | ||
key=r"attentions\.[0-9]+\.k_lin", | ||
value=r"attentions\.[0-9]+\.v_lin", | ||
att_dense=r"attentions\.[0-9]+\.out_lin", | ||
interm_dense=r"ffns\.[0-9]+\.lin1", | ||
output_dense=r"ffns\.[0-9]+\.lin2", | ||
) | ||
ATTENTION_PREFIX = (r"attentions\.[0-9]",) | ||
ATTENTION_LAYERS = ("q_lin", "k_lin", "v_lin") | ||
MHA_LAYERS = ATTENTION_LAYERS + ("att_dense",) | ||
NAME_CONFIG = dict( | ||
hidden_size="emb_dim", | ||
intermediate_size="emb_dim", | ||
num_hidden_layers="n_layers", | ||
num_attention_heads="n_heads", | ||
attention_head_size="attention_head_size", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.