Fabrice-TIERCELIN
commited on
Commit
•
ed33c51
1
Parent(s):
50b940f
Rewrite old function from modeling_opt.py
Browse files
llava/model/language_model/mpt/hf_prefixlm_converter.py
CHANGED
@@ -18,8 +18,6 @@ from transformers.models.gpt_neo.modeling_gpt_neo import GPTNeoForCausalLM
|
|
18 |
from transformers.models.gpt_neox.modeling_gpt_neox import GPTNeoXForCausalLM
|
19 |
from transformers.models.gptj.modeling_gptj import GPTJForCausalLM
|
20 |
from transformers.models.opt.modeling_opt import OPTForCausalLM
|
21 |
-
from transformers.models.opt.modeling_opt import _expand_mask as _expand_mask_opt
|
22 |
-
from transformers.models.opt.modeling_opt import _make_causal_mask as _make_causal_mask_opt
|
23 |
logger = logging.get_logger(__name__)
|
24 |
_SUPPORTED_GPT_MODELS = (GPT2LMHeadModel, GPTJForCausalLM, GPTNeoForCausalLM, GPTNeoXForCausalLM)
|
25 |
CAUSAL_GPT_TYPES = Union[GPT2LMHeadModel, GPTJForCausalLM, GPTNeoForCausalLM, GPTNeoXForCausalLM]
|
@@ -52,6 +50,36 @@ def _expand_mask_bloom(mask: torch.Tensor, tgt_length: int) -> torch.BoolTensor:
|
|
52 |
expanded_mask = ~(mask[:, None, None, :].to(torch.bool))
|
53 |
return expanded_mask.expand(batch_size, 1, tgt_length, src_length)
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
def _convert_gpt_causal_lm_to_prefix_lm(model: CAUSAL_GPT_TYPES) -> CAUSAL_GPT_TYPES:
|
56 |
"""Converts a GPT-style Causal LM to a Prefix LM.
|
57 |
|
|
|
18 |
from transformers.models.gpt_neox.modeling_gpt_neox import GPTNeoXForCausalLM
|
19 |
from transformers.models.gptj.modeling_gptj import GPTJForCausalLM
|
20 |
from transformers.models.opt.modeling_opt import OPTForCausalLM
|
|
|
|
|
21 |
logger = logging.get_logger(__name__)
|
22 |
_SUPPORTED_GPT_MODELS = (GPT2LMHeadModel, GPTJForCausalLM, GPTNeoForCausalLM, GPTNeoXForCausalLM)
|
23 |
CAUSAL_GPT_TYPES = Union[GPT2LMHeadModel, GPTJForCausalLM, GPTNeoForCausalLM, GPTNeoXForCausalLM]
|
|
|
50 |
expanded_mask = ~(mask[:, None, None, :].to(torch.bool))
|
51 |
return expanded_mask.expand(batch_size, 1, tgt_length, src_length)
|
52 |
|
53 |
+
def _make_causal_mask_opt(
|
54 |
+
input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0
|
55 |
+
):
|
56 |
+
"""
|
57 |
+
Make causal mask used for bi-directional self-attention.
|
58 |
+
"""
|
59 |
+
bsz, tgt_len = input_ids_shape
|
60 |
+
mask = torch.full((tgt_len, tgt_len), torch.finfo(dtype).min, device=device)
|
61 |
+
mask_cond = torch.arange(mask.size(-1), device=device)
|
62 |
+
mask.masked_fill_(mask_cond < (mask_cond + 1).view(mask.size(-1), 1), 0)
|
63 |
+
mask = mask.to(dtype)
|
64 |
+
|
65 |
+
if past_key_values_length > 0:
|
66 |
+
mask = torch.cat([torch.zeros(tgt_len, past_key_values_length, dtype=dtype, device=device), mask], dim=-1)
|
67 |
+
return mask[None, None, :, :].expand(bsz, 1, tgt_len, tgt_len + past_key_values_length)
|
68 |
+
|
69 |
+
|
70 |
+
def _expand_mask_opt(mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None):
|
71 |
+
"""
|
72 |
+
Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`.
|
73 |
+
"""
|
74 |
+
bsz, src_len = mask.size()
|
75 |
+
tgt_len = tgt_len if tgt_len is not None else src_len
|
76 |
+
|
77 |
+
expanded_mask = mask[:, None, None, :].expand(bsz, 1, tgt_len, src_len).to(dtype)
|
78 |
+
|
79 |
+
inverted_mask = 1.0 - expanded_mask
|
80 |
+
|
81 |
+
return inverted_mask.masked_fill(inverted_mask.to(torch.bool), torch.finfo(dtype).min)
|
82 |
+
|
83 |
def _convert_gpt_causal_lm_to_prefix_lm(model: CAUSAL_GPT_TYPES) -> CAUSAL_GPT_TYPES:
|
84 |
"""Converts a GPT-style Causal LM to a Prefix LM.
|
85 |
|