forked from huggingface/optimum-intel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodeling_utils.py
184 lines (148 loc) · 6.69 KB
/
modeling_utils.py
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
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
# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Tuple
import torch
from transformers.modeling_utils import PreTrainedModel
MULTI_QUERY_ATTN_MODELS = {"falcon", "gpt_bigcode"}
# Modified from transformers.models.bloom.modeling_bloom._make_causal_mask
def _make_causal_mask(
input_ids_shape: torch.Size,
device: torch.device,
past_key_values_length: int,
dtype: torch.dtype = torch.bool,
) -> torch.BoolTensor:
"""
Make causal mask used for bi-directional self-attention.
"""
batch_size, target_length = input_ids_shape
mask = torch.zeros((target_length, target_length + past_key_values_length), dtype=dtype, device=device)
seq_ids = torch.arange(target_length, device=device)
mask[:, past_key_values_length:] = (
(seq_ids[:, None] < seq_ids[None, :]) * torch.finfo(dtype).min
if torch.is_floating_point(mask)
else seq_ids[:, None] < seq_ids[None, :]
)
return mask[None, None, :, :].expand(batch_size, 1, target_length, target_length + past_key_values_length)
# Modified from transformers.models..bloom.modeling_bloom._prepare_attn_mask
def _prepare_attn_mask(
attention_mask: torch.Tensor, input_shape: Tuple[int, int], past_key_values_length: int
) -> torch.BoolTensor:
from transformers.models.bloom.modeling_bloom import _expand_mask
# create causal mask
# [batch_size, seq_length] -> [batch_size, 1, tgt_length, src_length]
combined_attention_mask = None
device = attention_mask.device
_, src_length = input_shape
combined_attention_mask = _make_causal_mask(
input_shape, device=device, past_key_values_length=past_key_values_length
)
# [batch_size, seq_length] -> [batch_size, 1, tgt_length, src_length]_prepare_decoder_attention_mask
expanded_attn_mask = _expand_mask(attention_mask, tgt_length=src_length)
combined_attention_mask = (
expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask | combined_attention_mask
)
return combined_attention_mask
# Modified from transformers.models.llama.modeling_llama._prepare_decoder_attention_mask
def _prepare_decoder_attention_mask(attention_mask, input_shape, inputs_embeds, past_key_values_length):
from transformers.models.llama.modeling_llama import _expand_mask
# create causal mask
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
combined_attention_mask = None
combined_attention_mask = _make_causal_mask(
input_shape,
device=inputs_embeds.device,
past_key_values_length=past_key_values_length,
dtype=inputs_embeds.dtype,
)
if attention_mask is not None:
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
expanded_attn_mask = _expand_mask(attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]).to(
inputs_embeds.device
)
combined_attention_mask = (
expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask + combined_attention_mask
)
return combined_attention_mask
# Modified from transformers.models.mistral.modeling_mistral._prepare_decoder_sliding_window_attention_mask
def _prepare_decoder_sliding_window_attention_mask(
attention_mask: torch.Tensor,
input_shape: Tuple[int, int],
inputs_embeds: torch.Tensor,
past_key_values_length: int,
sliding_window: int,
):
from transformers.models.mistral.modeling_mistral import _expand_mask, _make_sliding_window_causal_mask
# create causal mask
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
combined_attention_mask = None
combined_attention_mask = _make_sliding_window_causal_mask(
input_shape,
device=inputs_embeds.device,
dtype=inputs_embeds.dtype,
past_key_values_length=past_key_values_length,
sliding_window=sliding_window,
)
if attention_mask is not None:
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
expanded_attn_mask = _expand_mask(attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]).to(
inputs_embeds.device
)
combined_attention_mask = (
expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask + combined_attention_mask
)
return combined_attention_mask
def patch_decoder_attention_mask(model: "PreTrainedModel"):
"""
Apply patch on decoder with past model forward to resolve first inference based on model architecture
Args:
model (PretrainedModel): The model to patch.
Returns:
model with applied patch
"""
if model.config.model_type in {"bloom", "mpt"}:
model.transformer._prepare_attn_mask = _prepare_attn_mask
elif model.config.model_type == "llama":
model.model._prepare_decoder_attention_mask = _prepare_decoder_attention_mask
elif model.config.model_type == "mistral":
model.model._prepare_decoder_attention_mask = _prepare_decoder_sliding_window_attention_mask
elif model.config.model_type in {"blenderbot-small", "blenderbot", "opt", "pegasus", "bart"}:
model.model.decoder._prepare_decoder_attention_mask = _prepare_decoder_attention_mask
return model
def get_model_device(model: torch.nn.Module) -> torch.device:
"""
Determines the device on which a PyTorch model is currently residing.
Args:
model: The PyTorch model to query.
Returns:
torch.device: The device where the model's parameters are located.
Raises:
StopIteration: If the model has no parameters.
"""
try:
device = next(model.parameters()).device
except StopIteration:
# The model had no parameters at all, doesn't matter which device to choose
device = torch.device("cpu")
return device
def recursive_to_device(value, device):
"""
Recursivley move the tensor element in `value` to `device`
"""
if isinstance(value, (tuple, list)):
return type(value)(recursive_to_device(v, device) for v in value)
elif isinstance(value, dict):
return {k: recursive_to_device(v, device) for k, v in value.items()}
elif isinstance(value, torch.Tensor):
return value.to(device)
return value