""" PyTorch ChatGLM model. """
import math
import copy
import os
import warnings
import re
import sys
import torch
import torch.utils.checkpoint
import torch.nn.functional as F
from torch import nn
from torch.nn import CrossEntropyLoss, LayerNorm
from torch.nn.utils import skip_init
from typing import Optional, Tuple, Union, List, Callable, Dict, Any
from transformers.utils import (
add_code_sample_docstrings,
add_start_docstrings,
add_start_docstrings_to_model_forward,
)
from transformers.modeling_outputs import (
BaseModelOutputWithPast,
CausalLMOutputWithPast,
BaseModelOutputWithPastAndCrossAttentions,
)
from transformers.modeling_utils import PreTrainedModel
from transformers.utils import logging
from transformers.generation.logits_process import LogitsProcessor
from transformers.generation.utils import LogitsProcessorList, StoppingCriteriaList, GenerationConfig, ModelOutput
from .configuration_chatglm import ChatGLMConfig
# flags required to enable jit fusion kernels
if sys.platform != 'darwin':
torch._C._jit_set_profiling_mode(False)
torch._C._jit_set_profiling_executor(False)
torch._C._jit_override_can_fuse_on_cpu(True)
torch._C._jit_override_can_fuse_on_gpu(True)
logger = logging.get_logger(__name__)
_CHECKPOINT_FOR_DOC = "THUDM/ChatGLM-6B"
_CONFIG_FOR_DOC = "ChatGLM6BConfig"
CHATGLM_6B_PRETRAINED_MODEL_ARCHIVE_LIST = [
"THUDM/chatglm-6b",
# See all ChatGLM-6B models at https://huggingface.co/models?filter=chatglm
]
class InvalidScoreLogitsProcessor(LogitsProcessor):
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
if torch.isnan(scores).any() or torch.isinf(scores).any():
scores.zero_()
scores[..., 5] = 5e4
return scores
def load_tf_weights_in_chatglm_6b(model, config, tf_checkpoint_path):
"""Load tf checkpoints in a pytorch model."""
try:
import re
import numpy as np
import tensorflow as tf
except ImportError:
logger.error(
"Loading a TensorFlow model in PyTorch, requires TensorFlow to be installed. Please see "
"https://www.tensorflow.org/install/ for installation instructions."
)
raise
tf_path = os.path.abspath(tf_checkpoint_path)
logger.info(f"Converting TensorFlow checkpoint from {tf_path}")
# Load weights from TF model
init_vars = tf.train.list_variables(tf_path)
names = []
arrays = []
for name, shape in init_vars:
logger.info(f"Loading TF weight {name} with shape {shape}")
array = tf.train.load_variable(tf_path, name)
names.append(name)
arrays.append(array)
for name, array in zip(names, arrays):
name = name.split("/")
# adam_v and adam_m are variables used in AdamWeightDecayOptimizer to calculated m and v
# which are not required for using pretrained model
if any(
n in ["adam_v", "adam_m", "AdamWeightDecayOptimizer", "AdamWeightDecayOptimizer_1", "global_step"]
for n in name
):
logger.info(f"Skipping {'/'.join(name)}")
continue
pointer = model
for m_name in name:
if re.fullmatch(r"[A-Za-z]+_\d+", m_name):
scope_names = re.split(r"_(\d+)", m_name)
else:
scope_names = [m_name]
if scope_names[0] == "kernel" or scope_names[0] == "gamma":
pointer = getattr(pointer, "weight")
elif scope_names[0] == "output_bias" or scope_names[0] == "beta":
pointer = getattr(pointer, "bias")
elif scope_names[0] == "output_weights":
pointer = getattr(pointer, "weight")
elif scope_names[0] == "squad":
pointer = getattr(pointer, "classifier")
else:
try:
pointer = getattr(pointer, scope_names[0])
except AttributeError:
logger.info(f"Skipping {'/'.join(name)}")
continue
if len(scope_names) >= 2:
num = int(scope_names[1])
pointer = pointer[num]
if m_name[-11:] == "_embeddings":
pointer = getattr(pointer, "weight")
elif m_name == "kernel":
array = np.transpose(array)
try:
assert (
pointer.shape == array.shape
), f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched"
except AssertionError as e:
e.args += (pointer.shape, array.shape)
raise
logger.info(f"Initialize PyTorch weight {name}")
pointer.data = torch.from_numpy(array)
return model
class PrefixEncoder(torch.nn.Module):
"""
The torch.nn model to encode the prefix
Input shape: (batch-size, prefix-length)
Output shape: (batch-size, prefix-length, 2*layers*hidden)
"""
def __init__(self, config):
super().__init__()
self.prefix_projection = config.prefix_projection
if self.prefix_projection:
# Use a two-layer MLP to encode the prefix
self.embedding = torch.nn.Embedding(config.pre_seq_len, config.hidden_size)
self.trans = torch.nn.Sequential(
torch.nn.Linear(config.hidden_size, config.hidden_size),
torch.nn.Tanh(),
torch.nn.Linear(config.hidden_size, config.num_layers * config.hidden_size * 2)
)
else:
self.embedding = torch.nn.Embedding(config.pre_seq_len, config.num_layers * config.hidden_size * 2)
def forward(self, prefix: torch.Tensor):
if self.prefix_projection:
prefix_tokens = self.embedding(prefix)
past_key_values = self.trans(prefix_tokens)
else:
past_key_values = self.embedding(prefix)
return past_key_values
@torch.jit.script
def gelu_impl(x):
"""OpenAI's gelu implementation."""
return 0.5 * x * (1.0 + torch.tanh(0.7978845608028654 * x *
(1.0 + 0.044715 * x * x)))
def gelu(x):
return gelu_impl(x)
class RotaryEmbedding(torch.nn.Module):
def __init__(self, dim, base=10000, precision=torch.half, learnable=False):
super().__init__()
inv_freq = 1. / (base ** (torch.arange(0, dim, 2).float() / dim))
inv_freq = inv_freq.half()
self.learnable = learnable
if learnable:
self.inv_freq = torch.nn.Parameter(inv_freq)
self.max_seq_len_cached = None
else:
self.register_buffer('inv_freq', inv_freq)
self.max_seq_len_cached = None
self.cos_cached = None
self.sin_cached = None
self.precision = precision
def _load_from_state_dict(self, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys,
error_msgs):
pass
def forward(self, x, seq_dim=1, seq_len=None):
if seq_len is None:
seq_len = x.shape[seq_dim]
if self.max_seq_len_cached is None or (seq_len > self.max_seq_len_cached):
self.max_seq_len_cached = None if self.learnable else seq_len
t = torch.arange(seq_len, device=x.device, dtype=self.inv_freq.dtype)
freqs = torch.einsum('i,j->ij', t, self.inv_freq)
# Different from paper, but it uses a different permutation in order to obtain the same calculation
emb = torch.cat((freqs, freqs), dim=-1).to(x.device)
if self.precision == torch.bfloat16:
emb = emb.float()
# [sx, 1 (b * np), hn]
cos_cached = emb.cos()[:, None, :]
sin_cached = emb.sin()[:, None, :]
if self.precision == torch.bfloat16:
cos_cached = cos_cached.bfloat16()
sin_cached = sin_cached.bfloat16()
if self.learna
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
该项目是一款基于Python和Shell的高效易用联邦学习框架设计源码,包含878个文件,包括390个Python脚本、271个JSON配置文件、114个Shell脚本、58个ReStructuredText文档、14个PNG图片、9个Markdown文件、9个Protocol Buffers描述文件、2个模型文件、2个批处理脚本、2个文本文件。此框架旨在提供高性能的联邦学习解决方案,适用于各种联邦学习场景。
资源推荐
资源详情
资源评论
收起资源包目录
基于Python和Shell的XFL高效易用联邦学习框架设计源码 (960个子文件)
make.bat 804B
make.bat 804B
ca-bundle.crt 205KB
ca.crt 2KB
.gitignore 2KB
hhpoetry-3.json 50KB
hhpoetry-1.json 46KB
hhpoetry-2.json 26KB
trainer_config_node-1.json 5KB
trainer_config_node-1.json 5KB
trainer_config_node-1.json 4KB
label_trainer.json 4KB
trainer_config_node-1.json 4KB
trainer_config_node-1.json 4KB
trainer_config_node-1.json 4KB
trainer_config_node-3.json 4KB
trainer_config_node-2.json 4KB
label_trainer.json 3KB
trainer_config_node-1.json 3KB
trainer_config_assist_trainer.json 3KB
trainer_config_assist_trainer.json 3KB
trainer_config_node-1.json 3KB
trainer_config_node-3.json 3KB
trainer_config_node-2.json 3KB
trainer_config_node-1.json 3KB
trainer_config_node-3.json 3KB
trainer_config_node-2.json 3KB
trainer_config_node-1.json 3KB
trainer_config_node-3.json 3KB
trainer_config_node-2.json 3KB
trainer_config_node-1.json 3KB
trainer_config_node-3.json 3KB
trainer_config_node-2.json 3KB
trainer_config_node-1.json 3KB
trainer_config_node-2.json 3KB
trainer_config_node-1.json 3KB
trainer_config_node-3.json 3KB
trainer_config_node-2.json 3KB
trainer_config_node-1.json 3KB
trainer_config_node-2.json 3KB
trainer_config_node-1.json 3KB
trainer_config_node-1.json 3KB
trainer_config_assist_trainer.json 3KB
trainer_config_assist_trainer.json 3KB
trainer_config_assist_trainer.json 3KB
trainer_config_node-2.json 3KB
trainer_config_assist_trainer.json 3KB
trainer_config_assist_trainer.json 3KB
assist_trainer.json 3KB
trainer_config_assist_trainer.json 3KB
trainer_config_assist_trainer.json 3KB
trainer_config_assist_trainer.json 3KB
trainer_config_node-1.json 3KB
trainer_config_node-3.json 3KB
trainer_config_node-2.json 3KB
trainer_config_assist_trainer.json 3KB
trainer_config_assist_trainer.json 3KB
trainer_config_assist_trainer.json 3KB
label_trainer.json 3KB
trainer_config_assist_trainer.json 3KB
trainer_config_node-1.json 3KB
trainer_config_assist_trainer.json 3KB
trainer_config_assist_trainer.json 3KB
trainer.json 3KB
label_trainer.json 2KB
trainer_config_assist_trainer.json 2KB
trainer_config_assist_trainer.json 2KB
assist_trainer.json 2KB
trainer_config_assist_trainer.json 2KB
trainer_config_assist_trainer.json 2KB
trainer.json 2KB
trainer_config_assist_trainer.json 2KB
trainer_config_assist_trainer.json 2KB
trainer_config_assist_trainer.json 2KB
trainer_config_assist_trainer.json 2KB
trainer_config_node-1.json 2KB
trainer_config_node-1.json 2KB
assist_trainer.json 2KB
assist_trainer.json 2KB
assist_trainer.json 2KB
assist_trainer.json 2KB
trainer_config_node-1.json 2KB
trainer_config_node-2.json 2KB
trainer_config_assist_trainer.json 2KB
assist_trainer.json 2KB
trainer_config_assist_trainer.json 2KB
assist_trainer.json 2KB
trainer_config_node-2.json 2KB
assist_trainer.json 2KB
trainer_config_assist_trainer.json 2KB
assist_trainer.json 2KB
trainer.json 2KB
trainer_config_node-1.json 2KB
trainer_config_node-3.json 2KB
trainer_config_node-2.json 2KB
trainer_config_assist_trainer.json 2KB
trainer_config_node-2.json 2KB
label_trainer.json 2KB
assist_trainer.json 2KB
label_trainer.json 2KB
共 960 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论
lly202406
- 粉丝: 3262
- 资源: 5572
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 【年度调薪】年度薪酬预算执行情况报告.xls
- 【年度调薪】调薪考核表.xls
- 【年度调薪】调薪矩阵表(HR总监绝密).xls
- 【年度调薪】度员工调薪登记表.xlsx
- 【年度调薪】薪资等级结构表.xls
- 【年度调薪】调薪调岗流程表格.xls
- 【年度调薪】部门年度薪资调整套级审批表.xlsx
- 【年度调薪】调薪流程.xlsx
- 【年度调薪】年度员工调薪登记表.xlsx
- 【年度调薪】员工调薪评估.xlsx
- 【年度调薪】员工加薪明细表.xlsx
- 【年度调薪】员工调薪记录表.xlsx
- 【年度调薪】HR疑难操作之调岗调薪(实务篇).doc
- 【年度调薪】工资评定调薪方案.doc
- 【年度调薪】年度调薪方案.doc
- 【年度调薪】调岗调薪操作技巧.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功