* Fix left-padding masks and positions in batched decode/prefill * Fix batched generation with left padding * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix attention mask handling, padding_idx zeroing, and Mistral batched generation 1. attention_dispatch.py: Fall back from flash/xformers to SDPA when an attention_mask is present, since flash attention only supports causal masking via flag and cannot consume arbitrary padding masks. 2. gemma2.py: Apply attention_mask during decode inference for bsz > 1. Guard against boolean SWA/GA flags with isinstance check. Slice mask to match K/V length when sliding window is active. Remove dead commented-out SDPA branch (SDPA does not support softcapping). 3. granite.py: Apply attention_mask during decode inference for bsz > 1. Remove dead commented-out SDPA branch and misleading comment. 4. mistral.py: Fix 2D-to-4D padding mask conversion -- convert 0/1 mask to additive format (0 for keep, -inf for mask) before combining with the causal mask. Force SDPA backend when attention_mask is present. 5. llama.py: Skip zeroing embed_tokens.weight[padding_idx] when the embedding is weight-tied to lm_head, since zeroing the shared weight forces logit(pad) = 0 which is higher than real token logits in models like Gemma, causing the decoder to emit pad tokens as gibberish. Also add eos != pad guard, clean up unused _seq_length variable, and fix get_max_cache_shape handling. 6. vision.py: Same padding_idx fix as llama.py for the vision model loading path. Tested on gemma-2b-it, gemma-2-2b-it, Llama-3.2-1B, Mistral-7B-v0.3, Qwen2.5-0.5B, Qwen3-0.6B with flash-attn 2.8.3 active. All outputs coherent, zero crashes, zero resize warnings. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Inference path optimizations: eliminate per-layer GPU-CPU sync, cache inspect.signature, add Granite SDPA split * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * More inference path optimizations across model files - gemma: hoist rotary_seq_len computation to model level (eliminates N per-layer GPU-CPU syncs from position_ids.max().item()), pre-convert attention mask to bool once for all layers, use scalar float multiply instead of torch.tensor allocation for embedding scaling - gemma2: use in-place tanh_() for softcap attention, use scalar float multiply for embedding scaling - granite: pre-convert attention mask to bool once for all layers - cohere: use in-place neg_() for rotary embedding (consistent with all other model files) - falcon_h1: use in-place mul_() for key_multiplier scaling - llama: use in-place tanh_() for logit softcapping * Revert scalar multiply for Gemma/Gemma2 embedding scaling The original torch.tensor(..., dtype=hidden_states.dtype) is intentional: sqrt(3072) rounds to 55.5 in bfloat16 vs 55.4256 in float32. A plain scalar multiply may compute at higher precision internally, producing different results. Restore the explicit dtype-cast tensor to match the training path in LlamaModel_fast_forward. * Fix hardcoded cuda:0 device strings and add Cohere .eq(0) bool mask Replace 15 hardcoded "cuda:0" with f"{DEVICE_TYPE_TORCH}:0" across gemma.py, gemma2.py, cohere.py, and falcon_h1.py to support multi-GPU and non-CUDA devices (XPU, etc.). Add .eq(0) bool mask pre-conversion in CohereModel_fast_forward_inference for batched inference consistency with llama.py, granite.py, and gemma.py. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Disable flex_attention for Mllama (Llama 3.2 Vision) Mllama's _update_causal_mask uses the deprecated make_flex_block_causal_mask which creates a BlockMask with Q_LEN=KV_LEN=total_seq_len. During decode with KV cache, q_len=1 but the block_mask still has Q_LEN=total_seq_len, causing a ValueError. This is an upstream transformers issue -- newer models use flex_attention_mask from masking_utils which handles decode correctly via cache_position, but mllama has not been updated yet. Add mllama to the exclusion list in prefer_flex_attn_if_supported alongside gpt_oss so it falls back to sdpa, which works correctly for both training and inference. * Fix off-by-one in sliding window K/V slicing for gemma2, qwen3, falcon_h1, cohere The old formula `slicing_tokens = 1 - sliding_window` uses negative indexing that keeps `sliding_window - 1` tokens instead of `sliding_window`. For example with sliding_window=32 and kv_seq_len=100, `1-32 = -31` keeps indices 69..99 (31 tokens) instead of the correct 68..99 (32 tokens). Replace with `start = kv_seq_len - sliding_window` to match the fix already applied in llama.py and the canonical definition in transformers masking_utils (sliding_window_overlay: kv_idx > q_idx - W, which keeps exactly W tokens). Also add attention_mask slicing after K/V trim in qwen3, falcon_h1, and cohere to prevent mask/K dimension mismatch during batched SDPA inference, matching the pattern already used in llama.py. Currently only gemma2 (sliding_window=4096) is actively affected. The other three models have sliding_window=None in their configs so the code path is not triggered, but this keeps it correct for any future models that set it. * Fix Gemma2 softcapping order: apply mask after softcap, not before The attention mask must be applied AFTER logit softcapping, not before. Both the Google DeepMind reference implementation (google-deepmind/gemma, gm/nn/_modules.py lines 254-277) and transformers' eager_attention_forward (gemma2/modeling_gemma2.py lines 187-193) use this order: 1. logits = Q @ K^T * scale 2. logits = tanh(logits / softcap) * softcap # softcap first 3. logits = logits + mask # mask after 4. probs = softmax(logits) The PR had the mask addition before softcapping, which causes tanh to clamp the -inf mask values to -softcap instead of preserving them as -inf for softmax. While the practical impact is small (masked positions get ~1e-23 probability instead of exact zero), this should match upstream. * Clarify GQA condition precedence and remove stale comments Add explicit parentheses to grouped query attention conditions in llama.py, qwen3.py, granite.py to make operator precedence clear. The expression `bsz == 1 or not X and Y` relies on Python binding `not` > `and` > `or` which is correct but easy to misread. Remove dead commented-out code (`# else: # Knn, Vnn = Knn, Vnn`) and stale mask comments (`# if attention_mask ...`) from the bsz==1 fast path in llama, qwen3, cohere, falcon_h1, gemma2 inference functions. These were leftover from the pre-batched-inference structure and no longer apply. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel Han <danielhanchen@gmail.com>
535 lines
19 KiB
Python
535 lines
19 KiB
Python
# Copyright 2023-present Daniel Han-Chen & the Unsloth 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 .llama import *
|
|
from ._utils import __version__
|
|
from unsloth_zoo.hf_utils import dtype_from_config
|
|
from unsloth_zoo.utils import _get_dtype, Version
|
|
from ..utils.packing import get_packed_info_from_kwargs
|
|
from ..utils.attention_dispatch import (
|
|
AttentionConfig,
|
|
AttentionContext,
|
|
run_attention,
|
|
select_attention_backend,
|
|
)
|
|
|
|
try:
|
|
from transformers.models.cohere.modeling_cohere import (
|
|
CohereAttention,
|
|
CohereDecoderLayer,
|
|
CohereModel,
|
|
CohereForCausalLM,
|
|
CohereRotaryEmbedding,
|
|
apply_rotary_pos_emb,
|
|
repeat_kv,
|
|
)
|
|
except:
|
|
transformers_version = Version(transformers_version)
|
|
if not transformers_version >= Version("4.42"):
|
|
raise ImportError(
|
|
f"Unsloth: Your transformers version of {transformers_version} does not support Cohere.\n"
|
|
f"The minimum required version is 4.42.3.\n"
|
|
f'Try `pip install --upgrade "transformers>=4.42.3"`\n'
|
|
f"to obtain the latest transformers build, then restart this session."
|
|
)
|
|
|
|
from transformers.modeling_attn_mask_utils import (
|
|
_prepare_4d_causal_attention_mask_for_sdpa,
|
|
)
|
|
|
|
# For Pytorch 2.1.1
|
|
try:
|
|
from transformers.models.cohere.modeling_cohere import (
|
|
CohereSdpaAttention,
|
|
CohereFlashAttention2,
|
|
)
|
|
except:
|
|
CohereSdpaAttention = CohereAttention
|
|
CohereFlashAttention2 = CohereAttention
|
|
|
|
|
|
def fast_layernorm_inference(self, X, out_weight = None):
|
|
XX = X.to(torch.float32, copy = True)
|
|
XX -= X.mean(-1, keepdim = True)
|
|
variance = XX.square().mean(-1, keepdim = True)
|
|
variance += self.variance_epsilon
|
|
XX *= variance.rsqrt_()
|
|
out_weight[:] = self.weight
|
|
XX *= out_weight
|
|
return XX.to(X.dtype)
|
|
|
|
|
|
# QK norm in Cohere
|
|
def CohereAttention_fast_forward(
|
|
self,
|
|
hidden_states: torch.Tensor,
|
|
causal_mask: Optional[BlockDiagonalCausalMask] = None,
|
|
attention_mask: Optional[torch.Tensor] = None,
|
|
position_ids: Optional[torch.LongTensor] = None,
|
|
past_key_value: Optional[Tuple[torch.Tensor]] = None,
|
|
output_attentions: bool = False,
|
|
use_cache: bool = False,
|
|
padding_mask: Optional[torch.LongTensor] = None,
|
|
position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
|
|
*args,
|
|
**kwargs,
|
|
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
|
# Clear inference
|
|
if hasattr(self, "paged_attention"):
|
|
del self.paged_attention_K
|
|
del self.paged_attention_V
|
|
del self.paged_attention
|
|
del self.temp_QA
|
|
del self.temp_KV
|
|
del self.RH_Q
|
|
del self.attention
|
|
del self.q_norm_out_weight
|
|
del self.k_norm_out_weight
|
|
|
|
bsz, q_len, _ = hidden_states.size()
|
|
|
|
n_heads = self.config.num_attention_heads
|
|
n_groups = self.num_key_value_groups
|
|
n_kv_heads = self.config.num_key_value_heads
|
|
head_dim = self.head_dim
|
|
assert n_kv_heads * n_groups == n_heads
|
|
|
|
Q, K, V = self.apply_qkv(self, hidden_states)
|
|
Q = Q.view(bsz, q_len, n_heads, head_dim).transpose(1, 2)
|
|
K = K.view(bsz, q_len, n_kv_heads, head_dim).transpose(1, 2)
|
|
V = V.view(bsz, q_len, n_kv_heads, head_dim).transpose(1, 2)
|
|
seq_info = get_packed_info_from_kwargs(kwargs, Q.device)
|
|
if self.use_qk_norm:
|
|
Q = fast_layernorm_compiled(self.q_norm, Q)
|
|
K = fast_layernorm_compiled(self.k_norm, K)
|
|
|
|
kv_seq_len = K.shape[-2]
|
|
if past_key_value is not None:
|
|
kv_seq_len += past_key_value[0].shape[-2]
|
|
|
|
# Extend RoPE dynamically to fit in VRAM
|
|
if position_embeddings:
|
|
cos, sin = position_embeddings
|
|
else:
|
|
cos, sin = self.rotary_emb.get_cached(kv_seq_len, Q.device.index)
|
|
|
|
rope_position_ids = (
|
|
position_ids if position_ids is not None else kwargs.get("position_ids")
|
|
)
|
|
# Useful for LongRoPE
|
|
Q, K = fast_rope_embedding(Q, K, cos, sin, rope_position_ids)
|
|
|
|
if past_key_value is not None:
|
|
K = torch.cat([past_key_value[0], K], dim = 2)
|
|
V = torch.cat([past_key_value[1], V], dim = 2)
|
|
past_key_value = (K, V) if use_cache else None
|
|
|
|
# Attention module
|
|
use_varlen = seq_info is not None and past_key_value is None
|
|
backend = select_attention_backend(use_varlen)
|
|
attention_config = AttentionConfig(
|
|
backend = backend,
|
|
n_kv_heads = n_kv_heads,
|
|
n_groups = n_groups,
|
|
flash_dense_kwargs = {"causal": True},
|
|
flash_varlen_kwargs = {
|
|
"dropout_p": 0.0,
|
|
"causal": True,
|
|
"softmax_scale": getattr(self, "softmax_scale", None),
|
|
},
|
|
)
|
|
context = AttentionContext(
|
|
bsz = bsz,
|
|
q_len = q_len,
|
|
kv_seq_len = kv_seq_len,
|
|
n_heads = n_heads,
|
|
head_dim = head_dim,
|
|
requires_grad = hidden_states.requires_grad,
|
|
seq_info = seq_info,
|
|
attention_mask = attention_mask,
|
|
causal_mask = causal_mask,
|
|
)
|
|
|
|
A = run_attention(config = attention_config, context = context, Q = Q, K = K, V = V)
|
|
|
|
attn_output = A.reshape(bsz, q_len, n_heads * head_dim)
|
|
attn_output = self.apply_o(self, attn_output)
|
|
attn_weights = None
|
|
return attn_output, attn_weights, past_key_value
|
|
|
|
|
|
# https://github.com/huggingface/transformers/blob/main/src/transformers/models/llama/modeling_llama.py#L590
|
|
def CohereDecoderLayer_fast_forward(
|
|
self,
|
|
hidden_states: torch.Tensor,
|
|
causal_mask: Optional[BlockDiagonalCausalMask] = None,
|
|
attention_mask: Optional[torch.Tensor] = None,
|
|
position_ids: Optional[torch.LongTensor] = None,
|
|
past_key_value: Optional[Tuple[torch.Tensor]] = None,
|
|
output_attentions: Optional[bool] = False,
|
|
use_cache: Optional[bool] = False,
|
|
padding_mask: Optional[torch.LongTensor] = None,
|
|
position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
|
|
*args,
|
|
**kwargs,
|
|
):
|
|
if use_cache and hasattr(
|
|
self, "_flag_for_generation"
|
|
): # past_key_value is not None:
|
|
out_weight = torch.empty(
|
|
self.input_layernorm.weight.shape,
|
|
dtype = torch.float32,
|
|
device = f"{DEVICE_TYPE_TORCH}:0",
|
|
)
|
|
|
|
# Self Attention
|
|
residual = hidden_states
|
|
hidden_states = fast_layernorm_inference(
|
|
self.input_layernorm, hidden_states, out_weight
|
|
)
|
|
hidden_states_attention, self_attn_weights, present_key_value = self.self_attn(
|
|
hidden_states = hidden_states,
|
|
causal_mask = causal_mask,
|
|
attention_mask = attention_mask,
|
|
position_ids = position_ids,
|
|
past_key_value = past_key_value,
|
|
output_attentions = output_attentions,
|
|
use_cache = use_cache,
|
|
padding_mask = padding_mask,
|
|
**kwargs,
|
|
)
|
|
|
|
# Fully Connected
|
|
hidden_states_mlp = fast_swiglu_inference(self.mlp, hidden_states)
|
|
residual += hidden_states_attention
|
|
residual += hidden_states_mlp
|
|
hidden_states = residual
|
|
else:
|
|
residual = hidden_states
|
|
hidden_states = fast_layernorm_compiled(self.input_layernorm, hidden_states)
|
|
hidden_states_attention, self_attn_weights, present_key_value = self.self_attn(
|
|
hidden_states = hidden_states,
|
|
causal_mask = causal_mask,
|
|
attention_mask = attention_mask,
|
|
position_ids = position_ids,
|
|
past_key_value = past_key_value,
|
|
output_attentions = output_attentions,
|
|
use_cache = use_cache,
|
|
padding_mask = padding_mask,
|
|
**kwargs,
|
|
)
|
|
|
|
# Fully Connected
|
|
hidden_states_mlp = self.mlp(hidden_states)
|
|
hidden_states = residual + hidden_states_attention + hidden_states_mlp
|
|
|
|
outputs = (hidden_states,)
|
|
if output_attentions:
|
|
outputs += (self_attn_weights,)
|
|
if use_cache:
|
|
outputs += (present_key_value,)
|
|
return outputs
|
|
|
|
|
|
from math import sqrt as math_sqrt
|
|
|
|
KV_CACHE_INCREMENT = 256 # KV Cache update size
|
|
torch_nn_functional_softmax = torch.nn.functional.softmax
|
|
torch_matmul = torch.matmul
|
|
|
|
|
|
def CohereAttention_fast_forward_inference(
|
|
self,
|
|
hidden_states: torch.Tensor,
|
|
past_key_value: Optional[Tuple[torch.Tensor]],
|
|
position_ids,
|
|
do_prefill = False,
|
|
attention_mask = None,
|
|
**kwargs,
|
|
):
|
|
Xn = hidden_states
|
|
bsz, _, hd = hidden_states.size()
|
|
K1, V1 = past_key_value
|
|
dtype = Xn.dtype
|
|
|
|
n_heads = self.config.num_attention_heads
|
|
n_groups = self.num_key_value_groups
|
|
n_kv_heads = self.config.num_key_value_heads
|
|
head_dim = self.head_dim
|
|
# assert(n_kv_heads * n_groups == n_heads)
|
|
|
|
hidden_size = self.config.hidden_size
|
|
attention_size = n_heads * head_dim
|
|
seq_len = K1.shape[-2]
|
|
kv_seq_len = seq_len + 1
|
|
|
|
# Prefill phase
|
|
# if not hasattr(self, "paged_attention"):
|
|
if do_prefill:
|
|
self.paged_attention = torch.empty(
|
|
(KV_CACHE_INCREMENT + seq_len + 1, 2, bsz, n_kv_heads, head_dim),
|
|
dtype = dtype,
|
|
device = f"{DEVICE_TYPE_TORCH}:0",
|
|
)
|
|
self.paged_attention_K = self.paged_attention[:, 0]
|
|
self.paged_attention_V = self.paged_attention[:, 1]
|
|
self.paged_attention_K[:seq_len] = K1.permute(2, 0, 1, 3)
|
|
self.paged_attention_V[:seq_len] = V1.permute(2, 0, 1, 3)
|
|
self.temp_QA = torch.empty(
|
|
(2, bsz, 1, attention_size), dtype = dtype, device = f"{DEVICE_TYPE_TORCH}:0"
|
|
)
|
|
self.temp_KV = torch.empty(
|
|
(2, bsz, 1, n_kv_heads * head_dim),
|
|
dtype = dtype,
|
|
device = f"{DEVICE_TYPE_TORCH}:0",
|
|
)
|
|
self.RH_Q = torch.empty(
|
|
(bsz, n_heads, 1, head_dim), dtype = dtype, device = f"{DEVICE_TYPE_TORCH}:0"
|
|
)
|
|
|
|
# Mistral Nemo 12b has weird dimensions
|
|
if attention_size != hidden_size:
|
|
self.temp_O = torch.empty(
|
|
(bsz, 1, hidden_size), dtype = dtype, device = f"{DEVICE_TYPE_TORCH}:0"
|
|
)
|
|
else:
|
|
self.temp_O = self.temp_QA[1][:, :, :hidden_size]
|
|
|
|
self.attention = torch.empty(
|
|
(bsz, n_heads, 1, KV_CACHE_INCREMENT + seq_len),
|
|
dtype = dtype,
|
|
device = f"{DEVICE_TYPE_TORCH}:0",
|
|
)
|
|
self.scalar = 1.0 / math_sqrt(self.head_dim)
|
|
self.half_head_dim = head_dim // 2
|
|
# Cohere has QK layernorms
|
|
if self.use_qk_norm:
|
|
self.q_norm_out_weight = torch.empty(
|
|
self.q_norm.weight.shape,
|
|
dtype = torch.float32,
|
|
device = f"{DEVICE_TYPE_TORCH}:0",
|
|
)
|
|
self.k_norm_out_weight = torch.empty(
|
|
self.k_norm.weight.shape,
|
|
dtype = torch.float32,
|
|
device = f"{DEVICE_TYPE_TORCH}:0",
|
|
)
|
|
else:
|
|
self.q_norm_out_weight = None
|
|
self.k_norm_out_weight = None
|
|
elif kv_seq_len >= self.paged_attention.shape[0]:
|
|
self.paged_attention.resize_(
|
|
(
|
|
self.paged_attention.shape[0] + KV_CACHE_INCREMENT,
|
|
2,
|
|
bsz,
|
|
n_kv_heads,
|
|
head_dim,
|
|
)
|
|
)
|
|
self.paged_attention_K = self.paged_attention[:, 0]
|
|
self.paged_attention_V = self.paged_attention[:, 1]
|
|
self.attention.resize_(
|
|
(bsz, n_heads, 1, self.attention.shape[-1] + KV_CACHE_INCREMENT)
|
|
)
|
|
|
|
Qn = fast_linear_forward(self.q_proj, Xn, out = self.temp_QA[0])
|
|
Kn = fast_linear_forward(self.k_proj, Xn, out = self.temp_KV[0])
|
|
Vn = fast_linear_forward(self.v_proj, Xn, out = self.temp_KV[1])
|
|
Qn = Qn.view(bsz, 1, n_heads, head_dim).transpose(1, 2)
|
|
Kn = Kn.view(bsz, 1, n_kv_heads, head_dim).transpose(1, 2)
|
|
Vn = Vn.view(bsz, 1, n_kv_heads, head_dim).transpose(1, 2)
|
|
if self.use_qk_norm:
|
|
Qn = fast_layernorm_inference(self.q_norm, Qn, self.q_norm_out_weight)
|
|
Kn = fast_layernorm_inference(self.k_norm, Kn, self.k_norm_out_weight)
|
|
|
|
# cos, sin = self.rotary_emb(Vn, seq_len = kv_seq_len)
|
|
# Qn, Kn = inplace_rope_embedding(Qn, Kn, cos, sin, position_ids)
|
|
cos, sin = self.rotary_emb.get_cached(kv_seq_len, Qn.device.index)
|
|
cos = cos[position_ids].unsqueeze(1)
|
|
sin = sin[position_ids].unsqueeze(1)
|
|
h = self.half_head_dim
|
|
|
|
RH_Q = self.RH_Q
|
|
RH_Q[:, :, :, :h] = Qn[:, :, :, h:]
|
|
RH_Q[:, :, :, h:] = Qn[:, :, :, :h]
|
|
RH_Q[:, :, :, :h].neg_()
|
|
Qn *= cos
|
|
Qn.addcmul_(RH_Q, sin)
|
|
|
|
RH_K = RH_Q[
|
|
:, :n_kv_heads, :, :
|
|
] # torch.empty((n_kv_heads, 1, head_dim), dtype = dtype, device = "cuda:0")
|
|
RH_K[:, :, :, :h] = Kn[:, :, :, h:]
|
|
RH_K[:, :, :, h:] = Kn[:, :, :, :h]
|
|
RH_K[:, :, :, :h].neg_()
|
|
Kn *= cos
|
|
Kn.addcmul_(RH_K, sin)
|
|
|
|
# New KV cache
|
|
# Kn = torch.cat([K1, Kn], dim = 2)
|
|
# Vn = torch.cat([V1, Vn], dim = 2)
|
|
self.paged_attention_K[seq_len] = Kn.permute(2, 0, 1, 3)
|
|
self.paged_attention_V[seq_len] = Vn.permute(2, 0, 1, 3)
|
|
Kn = self.paged_attention_K[:kv_seq_len].permute(1, 2, 0, 3)
|
|
Vn = self.paged_attention_V[:kv_seq_len].permute(1, 2, 0, 3)
|
|
|
|
# Handle sliding windows
|
|
sliding_window = getattr(self.config, "sliding_window", None)
|
|
if sliding_window is not None and kv_seq_len > sliding_window:
|
|
start = kv_seq_len - sliding_window
|
|
Knn = Kn[:, :, start:, :] # .contiguous()
|
|
Vnn = Vn[:, :, start:, :] # .contiguous()
|
|
if attention_mask is not None:
|
|
attention_mask = attention_mask[..., start:]
|
|
else:
|
|
Knn, Vnn = Kn, Vn
|
|
|
|
# Grouped query attention
|
|
_, _, cached_len, _ = Knn.shape
|
|
if n_groups != 1:
|
|
Knn = Knn[:, :, None, :, :].expand(
|
|
bsz, n_kv_heads, n_groups, cached_len, head_dim
|
|
)
|
|
Vnn = Vnn[:, :, None, :, :].expand(
|
|
bsz, n_kv_heads, n_groups, cached_len, head_dim
|
|
)
|
|
Knn = Knn.reshape(bsz, n_heads, cached_len, head_dim)
|
|
Vnn = Vnn.reshape(bsz, n_heads, cached_len, head_dim)
|
|
|
|
# Attention
|
|
if bsz == 1:
|
|
Qn *= self.scalar # See https://github.com/ggerganov/llama.cpp/issues/7805#issuecomment-2153349963
|
|
# It seems like doing (Q * scalar) @ K is better than (Q @ K) * scalar to stop overflows
|
|
A = torch_matmul(
|
|
Qn, Knn.transpose(2, 3), out = self.attention[:, :, :, :cached_len]
|
|
)
|
|
A[:] = torch_nn_functional_softmax(
|
|
A, dim = -1, dtype = torch.float32
|
|
) # .to(A.dtype)
|
|
A = torch_matmul(A, Vnn, out = Qn)
|
|
else:
|
|
A = scaled_dot_product_attention(
|
|
Qn, Knn, Vnn, attn_mask = attention_mask, is_causal = False
|
|
)
|
|
A = A.transpose(1, 2)
|
|
A = A.reshape(bsz, 1, attention_size)
|
|
A = fast_linear_forward(self.o_proj, A, out = self.temp_O)
|
|
return A, (Kn, Vn)
|
|
|
|
|
|
# https://github.com/huggingface/transformers/blob/main/src/transformers/models/llama/modeling_llama.py#L825
|
|
# @torch.inference_mode
|
|
def CohereModel_fast_forward_inference(
|
|
self,
|
|
input_ids,
|
|
past_key_values,
|
|
position_ids,
|
|
attention_mask = None,
|
|
):
|
|
out_weights = tuple(
|
|
torch.empty_like(
|
|
self.model.layers[0].input_layernorm.weight,
|
|
dtype = torch.float32,
|
|
device = torch.device(x),
|
|
)
|
|
for x in range(DEVICE_COUNT)
|
|
)
|
|
input_ids = input_ids[:, : self.max_seq_length]
|
|
hidden_states = self.model.embed_tokens(input_ids)
|
|
hidden_states = hidden_states.to(_get_dtype(dtype_from_config(self.config)))
|
|
bsz, q_len, hd = hidden_states.shape
|
|
seq_len = past_key_values[0][0].shape[-2]
|
|
if bsz != 1:
|
|
attention_mask = _prepare_4d_causal_attention_mask_for_sdpa(
|
|
attention_mask,
|
|
(bsz, q_len),
|
|
hidden_states,
|
|
seq_len,
|
|
sliding_window = getattr(self.config, "sliding_window", None),
|
|
)
|
|
# Pre-convert to bool once for all layers (avoids per-layer .eq(0))
|
|
if attention_mask is not None and attention_mask.dtype != torch.bool:
|
|
attention_mask = attention_mask.eq(0)
|
|
else:
|
|
attention_mask = None
|
|
|
|
next_decoder_cache = []
|
|
for idx, decoder_layer in enumerate(self.model.layers):
|
|
device_index = getattr(decoder_layer, "_per_layer_device_index", 0)
|
|
hidden_states, position_ids = move_to_device(
|
|
device_index, hidden_states, position_ids
|
|
)
|
|
residual = hidden_states
|
|
hidden_states = fast_layernorm_inference(
|
|
decoder_layer.input_layernorm, hidden_states, out_weights[device_index]
|
|
)
|
|
hidden_states_attention, present_key_value = (
|
|
CohereAttention_fast_forward_inference(
|
|
decoder_layer.self_attn,
|
|
hidden_states = hidden_states,
|
|
past_key_value = past_key_values[idx],
|
|
position_ids = position_ids,
|
|
attention_mask = attention_mask,
|
|
do_prefill = not hasattr(decoder_layer.self_attn, "paged_attention"),
|
|
)
|
|
)
|
|
|
|
hidden_states_mlp = fast_swiglu_inference(decoder_layer.mlp, hidden_states)
|
|
residual += hidden_states_attention
|
|
residual += hidden_states_mlp
|
|
hidden_states = residual
|
|
|
|
next_decoder_cache.append(present_key_value)
|
|
hidden_states = fast_layernorm_inference(
|
|
self.model.norm, hidden_states, out_weights[device_index]
|
|
)
|
|
|
|
return BaseModelOutputWithPast(
|
|
last_hidden_state = hidden_states,
|
|
past_key_values = next_decoder_cache,
|
|
hidden_states = [],
|
|
attentions = [],
|
|
)
|
|
|
|
|
|
class FastCohereModel(FastLlamaModel):
|
|
@staticmethod
|
|
def pre_patch():
|
|
init_name, function = patch_linear_scaling(
|
|
model_name = "cohere",
|
|
rope_module = LlamaRotaryEmbedding,
|
|
scaled_rope_module = LlamaLinearScalingRotaryEmbedding,
|
|
attention_module = CohereAttention,
|
|
)
|
|
if init_name is not None:
|
|
exec(function, globals())
|
|
CohereAttention.__init__ = eval(init_name)
|
|
CohereAttention.forward = CohereAttention_fast_forward
|
|
CohereSdpaAttention.forward = CohereAttention_fast_forward
|
|
CohereFlashAttention2.forward = CohereAttention_fast_forward
|
|
CohereDecoderLayer.forward = CohereDecoderLayer_fast_forward
|
|
CohereModel.forward = LlamaModel_fast_forward
|
|
CohereForCausalLM.forward = CausalLM_fast_forward(
|
|
CohereModel_fast_forward_inference
|
|
)
|
|
PeftModelForCausalLM.forward = PeftModel_fast_forward
|
|
fix_prepare_inputs_for_generation(CohereForCausalLM)
|
|
|
|
import transformers.models.cohere.modeling_cohere
|
|
|
|
transformers.models.cohere.modeling_cohere.CohereRotaryEmbedding = (
|
|
LlamaRotaryEmbedding
|
|
)
|
|
return
|