この比較の残りの部分を読み込んでいます。

Yi vs llama

作成日 差分は期限切れになりません
511 削除
623 行
409 追加
538 行
# coding=utf-8
# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
#
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
# and OPT implementations in this library. It has been modified from its
# original forms to accommodate minor architectural differences compared
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
#
# 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.
""" PyTorch Yi model."""
""" PyTorch LLaMA model."""
import math
import math
import warnings
from typing import List, Optional, Tuple, Union
from typing import List, Optional, Tuple, Union


import torch
import torch.nn.functional as F
import torch.utils.checkpoint
import torch.utils.checkpoint
from einops import repeat
from torch import nn
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from transformers.activations import ACT2FN
from ...activations import ACT2FN
from transformers.modeling_outputs import (
from ...modeling_attn_mask_utils import AttentionMaskConverter, _prepare_4d_causal_attention_mask
BaseModelOutputWithPast,
from ...modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, SequenceClassifierOutputWithPast
CausalLMOutputWithPast,
SequenceClassifierOutputWithPast,
)
from transformers.modeling_utils import PreTrainedModel
from ...modeling_utils import PreTrainedModel
from transformers.pytorch_utils import ALL_LAYERNORM_LAYERS
from ...pytorch_utils import ALL_LAYERNORM_LAYERS
from transformers.utils import (
from ...utils import (
add_start_docstrings,
add_start_docstrings,
add_start_docstrings_to_model_forward,
add_start_docstrings_to_model_forward,
is_flash_attn_2_available,
logging,
logging,
replace_return_docstrings,
replace_return_docstrings,
)
)
from ...utils.import_utils import is_torch_fx_available
from .configuration_llama import LlamaConfig


from .configuration_yi import YiConfig


is_flash_attn_available = True
if is_flash_attn_2_available():
try:
from flash_attn import flash_attn_func
from flash_attn import flash_attn_func, flash_attn_varlen_func
except Exception:
is_flash_attn_available = False
from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input # noqa


# This makes `_prepare_4d_causal_attention_mask` a leaf function in the FX graph.
# It means that the function will not be traced through and simply appear as a node in the graph.
if is_torch_fx_available():
_prepare_4d_causal_attention_mask = torch.fx.wrap(_prepare_4d_causal_attention_mask)



logger = logging.get_logger(__name__)
logger = logging.get_logger(__name__)


_CONFIG_FOR_DOC = "YiConfig"
_CONFIG_FOR_DOC = "LlamaConfig"




# Copied from transformers.models.bart.modeling_bart._make_causal_mask
def _get_unpad_data(attention_mask):
def _make_causal_mask(
input_ids_shape: torch.Size,
dtype: torch.dtype,
device: torch.device,
past_key_values_length: int = 0,
):
"""
seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
Make causal mask used for bi-directional self-attention.
indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
"""
max_seqlen_in_batch = seqlens_in_batch.max().item()
bsz, tgt_len = input_ids_shape
cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.torch.int32), (1, 0))
mask = torch.full(
return (
(tgt_len, tgt_len),
indices,
torch.tensor(torch.finfo(dtype).min, device=device),
cu_seqlens,
device=device,
max_seqlen_in_batch,
)
mask_cond = torch.arange(mask.size(-1), device=device)
mask.masked_fill_(mask_cond < (mask_cond + 1).view(mask.size(-1), 1), 0)
mask = mask.to(dtype)

if past_key_values_length > 0:
mask = torch.cat(
[
torch.zeros(
tgt_len, past_key_values_length, dtype=dtype, device=device
),
mask,
],
dim=-1,
)
return mask[None, None, :, :].expand(