ChatGLM2-6B如何从输入到输出-代码解析(二)

出发点

上一篇解析了Chatglm2-6b的模型架构,并和Chatglm-6b进行对比,但是留下了几个问题(哭)这一篇的目的是讲明白attention和rotaryEmbedding,解决问题,并实现整体目标,完全替代modeling_chatglm.py,并将代码缩减到一半儿。

selfattention

selfattention


class SelfAttention(torch.nn.Module):"""Parallel self-attention layer abstract class.Self-attention layer takes input with size [s, b, h]and returns output of the same size."""def __init__(self, config: ChatGLMConfig, layer_number, device=None):super(SelfAttention, self).__init__()self.layer_number = max(1, layer_number)self.projection_size = config.kv_channels * config.num_attention_heads# 128*32=4096 hidden_size# Per attention head and per partition values.self.hidden_size_per_attention_head = self.projection_size // config.num_attention_heads# 128 每个attention头的hidden_sizeself.num_attention_heads_per_partition = config.num_attention_heads# 32 attention头数self.num_multi_query_groups_per_partition = config.multi_query_group_num# 2 分了多少组self.qkv_hidden_size = (self.projection_size + 2 * self.hidden_size_per_attention_head * config.multi_query_group_num)# 4096+2*128*2=4608 qkv对应的hidden_size# 稍微解释一下为什么不是4096*3,因为这里使用了GQA的思想,下文会简单介绍一下self.query_key_value = nn.Linear(config.hidden_size, self.qkv_hidden_size,bias=config.add_bias_linear or config.add_qkv_bias,device=device, **_config_to_kwargs(config))self.core_attention = CoreAttention(config, self.layer_number)# Output.self.dense = nn.Linear(self.projection_size, config.hidden_size, bias=config.add_bias_linear,device=device, **_config_to_kwargs(config))def forward(self, hidden_states, rotary_pos_emb, kv_cache=None, use_cache=True):# hidden_states: [sq, b, h]# =================================================# Pre-allocate memory for key-values for inference.# =================================================# =====================# Query, Key, and Value# =====================# Attention heads [sq, b, h] --> [sq, b, (np * 3 * hn)]mixed_x_layer = self.query_key_value(hidden_states)(query_layer, key_layer, value_layer) = mixed_x_layer.split([self.num_attention_heads_per_partition * self.hidden_size_per_attention_head,self.num_multi_query_groups_per_partition * self.hidden_size_per_attention_head,self.num_multi_query_groups_per_partition * self.hidden_size_per_attention_head,],dim=-1,)query_layer = query_layer.view(query_layer.size()[:-1] + (self.num_attention_heads_per_partition, self.hidden_size_per_attention_head))key_layer = key_layer.view(key_layer.size()[:-1] + (self.num_multi_query_groups_per_partition, self.hidden_size_per_attention_head))value_layer = value_layer.view(value_layer.size()[:-1]+ (self.num_multi_query_groups_per_partition, self.hidden_size_per_attention_head))# apply relative positional encoding (rotary embedding)if rotary_pos_emb is not None:query_layer = apply_rotary_pos_emb(query_layer, rotary_pos_emb)key_layer = apply_rotary_pos_emb(key_layer, rotary_pos_emb)# adjust key and value for inferenceif kv_cache is not None:cache_k, cache_v = kv_cachekey_layer = torch.cat((cache_k, key_layer), dim=0)value_layer = torch.cat((cache_v, value_layer), dim=0)if use_cache:kv_cache = (key_layer, value_layer)else:kv_cache = Nonekey_layer = key_layer.unsqueeze(-2)key_layer = key_layer.expand(-1, -1, -1, self.num_attention_heads_per_partition // self.num_multi_query_groups_per_partition, -1)key_layer = key_layer.contiguous().view(key_layer.size()[:2] + (self.num_attention_heads_per_partition, self.hidden_size_per_attention_head))# GQA的操作:重复多次到原始尺寸,即32,128value_layer = value_layer.unsqueeze(-2)value_layer = value_layer.expand(-1, -1, -1, self.num_attention_heads_per_partition // self.num_multi_query_groups_per_partition, -1)value_layer = value_layer.contiguous().view(value_layer.size()[:2] + (self.num_attention_heads_per_partition, self.hidden_size_per_attention_head))# GQA的操作:重复多次到原始尺寸,即32,128# ==================================# core attention computation# ==================================context_layer = self.core_attention(query_layer, key_layer, value_layer)# 核心操作attention,和Chatglm-6b中attention_fn是一样的# =================# Output. [sq, b, h]# =================output = self.dense(context_layer)return output, kv_cache

GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints
GQA
可以看出来思想也比较朴素,MHA中query、key、value都是一对一的,这样虽然效果好,但是caches太多了。MQA中只有一组key和value,和多个query相对应,caches减少了,但是效果会不好。那GQA则取个平均,有g组key和value,每一组key和value都重复几次和query相对应。
GQA提供了MHA到MQA的自然过渡,当g=h时就是MHA,g=1时就是MQA,当1<g<h时,它只将KV Cache压缩到g/h,压缩率不如MQA,但同时也提供了更大的自由度,效果上更有保证。
这里也贴一下Fast Transformer Decoding: One Write-Head is All You Need
那这里就解决了两个问题:

  • multi_query_group_num是GQA中要分组的数量
  • kv_channels对应的是query、key、value每个头的hidden_size

coreattention

class CoreAttention(torch.nn.Module):def __init__(self, config: ChatGLMConfig, layer_number):super(CoreAttention, self).__init__()self.apply_query_key_layer_scaling = config.apply_query_key_layer_scaling# 对query、key层是否要进行缩放,实际是要缩放的self.attention_softmax_in_fp32 = config.attention_softmax_in_fp32# softmax的精度要使用fp32self.layer_number = max(1, layer_number)# Per attention head and per partition values.self.hidden_size_per_partition = config.kv_channels * config.num_attention_heads# 128*32self.hidden_size_per_attention_head = config.kv_channels# 128self.num_attention_heads_per_partition = config.num_attention_heads# 32self.norm_factor = math.sqrt(self.hidden_size_per_attention_head)# sqrt(d)的操作self.attention_dropout = torch.nn.Dropout(config.attention_dropout)def forward(self, query_layer, key_layer, value_layer):pytorch_major_version = int(torch.__version__.split('.')[0])if pytorch_major_version >= 2:query_layer, key_layer, value_layer = [k.permute(1, 2, 0, 3) for k in [query_layer, key_layer, value_layer]]if query_layer.shape[2] == key_layer.shape[2]:# 只会在生成第一个token的时候,走这条路context_layer = torch.nn.functional.scaled_dot_product_attention(query_layer, key_layer, value_layer,is_causal=True)# 从这里可以看出来Chatglm2-6b完全就是一个decoder only的模型else:# 这时候query的长度是1,key的长度是总token的长度context_layer = torch.nn.functional.scaled_dot_product_attention(query_layer, key_layer, value_layer,None)context_layer = context_layer.permute(2, 0, 1, 3)new_context_layer_shape = context_layer.size()[:-2] + (self.hidden_size_per_partition,)context_layer = context_layer.reshape(*new_context_layer_shape)else:# Raw attention scores# [b, np, sq, sk]output_size = (query_layer.size(1), query_layer.size(2), query_layer.size(0), key_layer.size(0))# [sq, b, np, hn] -> [sq, b * np, hn]query_layer = query_layer.view(output_size[2], output_size[0] * output_size[1], -1)# [sk, b, np, hn] -> [sk, b * np, hn]key_layer = key_layer.view(output_size[3], output_size[0] * output_size[1], -1)# preallocting input tensor: [b * np, sq, sk]matmul_input_buffer = torch.empty(output_size[0] * output_size[1], output_size[2], output_size[3], dtype=query_layer.dtype,device=query_layer.device)# Raw attention scores. [b * np, sq, sk]matmul_result = torch.baddbmm(matmul_input_buffer,query_layer.transpose(0, 1),  # [b * np, sq, hn]key_layer.transpose(0, 1).transpose(1, 2),  # [b * np, hn, sk]beta=0.0,alpha=(1.0 / self.norm_factor),)# Chatglm-6b中将alpha放在了前面,让query单独除了一下,没啥结果上的差别# 关于torch.baddbmm多说一句,因为beta=0,所以input选择empty没啥问题,反正要被跳过# change view to [b, np, sq, sk]attention_scores = matmul_result.view(*output_size)# ===========================# Attention probs and dropout# ===========================# attention scores and attention mask [b, np, sq, sk]if self.attention_softmax_in_fp32:attention_scores = attention_scores.float()if attention_scores.shape[2] == attention_scores.shape[3]:attention_mask = torch.ones(output_size[0], 1, output_size[2], output_size[3],device=attention_scores.device, dtype=torch.bool)attention_mask.tril_()attention_mask = ~attention_maskelse:attention_mask = None"""重点看一下这一小段代码,当sq=sk时(即query长度和key长度一致时,给了一个attention_mask)此时的attention_mask其实就是一个上三角为True、下三角为False的矩阵结合后面的 attention_scores = attention_scores.masked_fill(attention_mask, float("-inf")) 这一句的操作就是将上三角的scores值置为负无穷,这妥妥的就是decoder-only嘛当sq!=sk时,attention_mask即为空,即预测第二个token时,此时query长度为1,而key长度带着之前的cache,所以长度>1,此时不相等,attention_mask为空,后续也就没有啥操作了"""if attention_mask is not None:attention_scores = attention_scores.masked_fill(attention_mask, float("-inf"))attention_probs = F.softmax(attention_scores, dim=-1)attention_probs = attention_probs.type_as(value_layer)# This is actually dropping out entire tokens to attend to, which might# seem a bit unusual, but is taken from the original Transformer paper.attention_probs = self.attention_dropout(attention_probs)# =========================# Context layer. [sq, b, hp]# =========================# value_layer -> context layer.# [sk, b, np, hn] --> [b, np, sq, hn]# context layer shape: [b, np, sq, hn]output_size = (value_layer.size(1), value_layer.size(2), query_layer.size(0), value_layer.size(3))# change view [sk, b * np, hn]value_layer = value_layer.view(value_layer.size(0), output_size[0] * output_size[1], -1)# change view [b * np, sq, sk]attention_probs = attention_probs.view(output_size[0] * output_size[1], output_size[2], -1)# matmul: [b * np, sq, hn]context_layer = torch.bmm(attention_probs, value_layer.transpose(0, 1))# change view [b, np, sq, hn]context_layer = context_layer.view(*output_size)# [b, np, sq, hn] --> [sq, b, np, hn]context_layer = context_layer.permute(2, 0, 1, 3).contiguous()# [sq, b, np, hn] --> [sq, b, hp]new_context_layer_shape = context_layer.size()[:-2] + (self.hidden_size_per_partition,)context_layer = context_layer.view(*new_context_layer_shape)return context_layer

这里多写一句,代码中有关于self.coeff的操作,即layer_number
在代码中self.norm_factor=self.coeff *math.sqrt(self.hidden_size_per_attention_head)
在计算attention_scores中除以了self.coeff *math.sqrt(self.hidden_size_per_attention_head)
然后在计算softmax之前又将attention_scores乘以了self.coeff
那不就相当于只是除以了math.sqrt(self.hidden_size_per_attention_head)嘛????
不知道为什么要有这个操作,感觉怪怪的,最主要的是不知道目的,有了解的可以解释一下,谢谢
之前Chatglm-6b的代码中就有这样的操作,当时没注意到(汗),这里的代码是直接删去了这个操作,完全没影响的。
当然了因为在pytorch_major_version >= 2中其实是没有和layer_number相关的操作,这个时候应该就能明白这个操作是无用的了。

RotaryEmbedding


class RotaryEmbedding(nn.Module):def __init__(self, dim, original_impl=False, device=None, dtype=None):super().__init__()inv_freq = 1.0 / (10000 ** (torch.arange(0, dim, 2, device=device).to(dtype=dtype) / dim))self.register_buffer("inv_freq", inv_freq)self.dim = dimself.original_impl = original_impldef forward_impl(self, seq_len: int, n_elem: int, dtype: torch.dtype, device: torch.device, base: int = 10000):"""Enhanced Transformer with Rotary Position Embedding.Derived from: https://github.com/labmlai/annotated_deep_learning_paper_implementations/blob/master/labml_nn/transformers/rope/__init__.py. MIT License:https://github.com/labmlai/annotated_deep_learning_paper_implementations/blob/master/license."""# $\Theta = {\theta_i = 10000^{\frac{2(i-1)}{d}}, i \in [1, 2, ..., \frac{d}{2}]}$theta = 1.0 / (base ** (torch.arange(0, n_elem, 2, dtype=dtype, device=device) / n_elem))# Create position indexes `[0, 1, ..., seq_len - 1]`seq_idx = torch.arange(seq_len, dtype=dtype, device=device)# Calculate the product of position index and $\theta_i$idx_theta = torch.outer(seq_idx, theta).float()cache = torch.stack([torch.cos(idx_theta), torch.sin(idx_theta)], dim=-1)# this is to mimic the behaviour of complex32, else we will get different resultsif dtype in (torch.float16, torch.bfloat16, torch.int8):cache = cache.bfloat16() if dtype == torch.bfloat16 else cache.half()return cachedef forward(self, max_seq_len, offset=0):return self.forward_impl(max_seq_len, self.dim, dtype=self.inv_freq.dtype, device=self.inv_freq.device)@torch.jit.script
def apply_rotary_pos_emb(x: torch.Tensor, rope_cache: torch.Tensor) -> torch.Tensor:# x: [sq, b, np, hn]sq, b, np, hn = x.size(0), x.size(1), x.size(2), x.size(3)rot_dim = rope_cache.shape[-2] * 2# 32*2x, x_pass = x[..., :rot_dim], x[..., rot_dim:]# [:64],[64:] 将输入根据隐藏层维度,拆分得到两部分,只针对前部分x计算旋转位置信息# truncate to support variable sizesrope_cache = rope_cache[:sq]xshaped = x.reshape(sq, -1, np, rot_dim // 2, 2)# [q_0,q_1][q_2,q_3]rope_cache = rope_cache.view(sq, -1, 1, xshaped.size(3), 2)# [cos0,sin0][cos1,sin1]x_out2 = torch.stack([xshaped[..., 0] * rope_cache[..., 0] - xshaped[..., 1] * rope_cache[..., 1],# 对应复数的实部q_0*cos(m\theta)-q_1*sin(m\theta)# [q0, q2, ] *[cos0, cos1] - [q1, q3, ] *[sin0, sin1]xshaped[..., 1] * rope_cache[..., 0] + xshaped[..., 0] * rope_cache[..., 1],# 对应复数的虚部q_1*cos(m\theta)+q_0*sin(m\theta)# [q1, q3, ] *[cos0, cos1] + [q0, q2, ] *[sin0, sin1]],-1,)# q0cos0-q1sin0# q1cos0+q0sin0# q2cos1-q3sin1# q3cos1+q2sin1x_out2 = x_out2.flatten(3)return torch.cat((x_out2, x_pass), dim=-1)

这里就可以解释位置Embedding中传入的dim为什么是rotary_dim // 2了,因为它只对一半的hidden_size进行了位置编码,这也是很迷的一项操作,我没看到什么很好的解释,有了解原因的,欢迎指导,谢谢

最后一点代码量

到此基本就写完了代码,最后补充上两个函数和一点import

""" PyTorch ChatGLM model. """import math
import copy
import reimport torch
import torch.nn.functional as F
from torch import nn
from torch.nn import LayerNorm
from torch.nn.utils import skip_init
from typing import Optional, Tuple, Union, List, Callable, Dict, Any
from transformers.modeling_utils import PreTrainedModel
from configuration_chatglm import ChatGLMConfigdef _config_to_kwargs(args):common_kwargs = {"dtype": args.torch_dtype,}return common_kwargsclass ChatGLMPreTrainedModel(PreTrainedModel):"""An abstract class to handle weights initialization anda simple interface for downloading and loading pretrained models."""is_parallelizable = Falseconfig_class = ChatGLMConfigbase_model_prefix = "transformer"_no_split_modules = ["GLMBlock"]

把这些代码保存成chatglm.py,放在chatglm2-6b的代码中,就可以正常使用了,使用方法和chatglm-6b是一样的

from chatglm import *
from transformers import AutoTokenizer
model_path = "/usr/downloads/chatglm2-6b"
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = ChatGLMForConditionalGeneration.from_pretrained(model_path, trust_remote_code=True).half().cuda()prompt = '你好'
response = model.chat(tokenizer, prompt)

代码量在650行,原始代码量是1280,减少一半的代码的小目标基本实现(成功)

参数量

简单分析一下参数量,其实从模型结构里就能很明白的看出来了,我这里就是记录一下

# word embedding
65024*4096*2=532676608
# 最后一层后面的LN
4096
# 下面几个是每层都有的
# query_key_value
4608*4096=18874368
# query_key_value.bias
4608
# dense
4096*4096=16777216
# LN
2*4096
# dense_h_to_4h
4096*27392=112197632
# dense_4h_to_h
13696*4096=56098816# 28层
(18874368+4608+16777216+2*4096+112197632+56098816)*28=5710903296
5710903296+532676608+4096=6243584000
# 可以看出来主要的参数还是在word Embedding和dense_h_to_4h

结束语

这次解析了chatglm2-6b的代码,将代码缩减到650行,并分析了与chatglm-6b的区别,其实从结构里就可以看出来,它已经不是GLM的架构了,完全是一个decoder only的结构。改为了使用了RMSNorm、使用了GQA缩减caches、激活函数使用swiglu,基本就是这些了。
补充一点:经过查看代码,发现chatglm3-6b和chatglm2-6b的代码基本一模一样,只有在tokenizer处理输入的时候和返回response的时候有一点不一样,所以就不对chatglm3-6b做单独的介绍了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/71859.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Sublime Text4安装、汉化

-------------2025-02-22可用---------------------- 官方网址下载&#xff1a;https://www.sublimetext.com 打开https://hexed.it 点击打开文件找到软件安装目录下的 ctrlf 查找 8079 0500 0f94 c2右边启用替换替换为:c641 0501 b200 90点击替换按钮 替换完成后 另存为本地…

汽车开放系统架构(AUTOSAR)中运行时环境(RTE)生成过程剖析

一、引言 在当今高度智能化的汽车电子领域&#xff0c;软件系统的复杂性呈指数级增长。为了应对这一挑战&#xff0c;汽车开放系统架构&#xff08;AUTOSAR&#xff09;应运而生&#xff0c;它为汽车电子软件开发提供了标准化的分层架构和开发方法。其中&#xff0c;运行时环境…

基于MATLAB的OFDM通信系统仿真设计

下面将为你详细介绍基于MATLAB的OFDM通信系统仿真设计的步骤和示例代码。 1. OFDM系统原理概述 正交频分复用&#xff08;OFDM&#xff09;是一种多载波调制技术&#xff0c;它将高速数据流通过串并转换&#xff0c;分配到多个正交的子载波上进行传输&#xff0c;这样可以有效…

stm32仿真 74hc238流水灯 数码管动态数字显示

f103c6t6a_hex文件 #include "main.h"![请添加图片描述](https://i-blog.csdnimg.cn/direct/8c0d44b121134cf08f5186df316ea07f.gif)#include "stdlib.h"void SystemClock_Config(void); static void MX_GPIO_Init(void); // 自定义abc引脚 #define A_PIN…

结构型模式 - 代理模式 (Proxy Pattern)

结构型模式 - 代理模式 (Proxy Pattern) 代理模式是一种结构型设计模式&#xff0c;它允许通过代理对象来控制对另一个对象&#xff08;目标对象&#xff09;的访问。代理对象充当目标对象的接口&#xff0c;客户端通过代理对象间接访问目标对象。 分为两大类 静态代理&#…

网络层(IP)

基本概念 子网和局域网是一个概念主机: 配有 IP 地址, 也能进行路由控制的设备;路由器: 即配有 IP 地址, 又能进行路由控制;节点&#xff1a; 路由器和主机的统称。 背景 两主机并不是直接连接的&#xff0c;路径选择问题&#xff1f;为什么&#xff1f; 由网络层&#xff08…

JMeter性能问题

性能测试中TPS上不去的几种原因 性能测试中TPS上不去的几种原因_tps一直上不去-CSDN博客 网络带宽 连接池 垃圾回收机制 压测脚本 通信连接机制 数据库配置 硬件资源 压测机 业务逻辑 系统架构 CPU过高什么原因 性能问题分析-CPU偏高 - 西瓜汁拌面 - 博客园 US C…

创建型模式 - 建造者模式 (Builder Pattern)

创建型模式 - 建造者模式 (Builder Pattern) 建造者模式是一种创建型设计模式&#xff0c;它将一个复杂对象的构建与表示分离&#xff0c;使得同样的构建过程可以创建不同的表示。 需求描述 在游戏开发中&#xff0c;创建一个复杂的游戏角色&#xff0c;角色具有多种属性&…

代码随想录第二十天|二叉树part08--669.修建二叉搜索树、108.将有序数组转换为二叉搜索树、538.把二叉搜索树转换为累加树

刷题小记&#xff1a; 上期学习了二叉搜索树的插入和删除操作&#xff0c;这次学习如何按区间修剪二叉搜索树。还有两题&#xff0c;关于借助二叉搜索树的有序特性进行转换。 669.修剪二叉搜索树&#xff08;669.修剪二叉搜索树&#xff09; 题目分析&#xff1a; 给定一个…

Fisher信息矩阵(Fisher Information Matrix,简称FIM)

Fisher信息矩阵简介 Fisher信息矩阵&#xff08;Fisher Information Matrix&#xff0c;简称FIM&#xff09;是统计学和信息理论中的一个重要概念&#xff0c;广泛应用于参数估计、统计推断和机器学习领域。它以统计学家罗纳德费希尔&#xff08;Ronald Fisher&#xff09;的名…

【初阶数据结构】链表的柔光之美

目录 一、为什么需要链表&#xff1f; 二、链表与数组的对比 三、链表节点定义 四、链表基本操作 1. 创建链表 2. 插入节点 头插法&#xff08;时间复杂度O(1)&#xff09; 尾插法&#xff08;时间复杂度O(n)&#xff09; 3. 删除节点 4. 遍历链表 五、进阶操作 1. 反…

《论湖仓一体架构及其应用》审题技巧 - 系统架构设计师

软考论文写作框架 一、考点概述 “湖仓一体架构及其应用”这一论题&#xff0c;主要考察了考生对现代数据管理系统中湖仓一体架构的理解、应用及问题解决能力。随着5G、大数据、人工智能、物联网等技术的快速发展&#xff0c;企业数据的管理需求正发生深刻变化。传统的数据管…

MybatisPlus-扩展功能-枚举处理器

在Mybatis里有一个叫TypeHandler的类型处理器&#xff0c;我们常见的PO当中的这些成员变量的数据类型&#xff0c;它都有对应的处理器&#xff0c;因此它就能自动实现这些Java数据类型与数据库类型的相互转换。 它里面还有一个叫EnumOrdinalTypeHandler的枚举处理器&#xff0…

北京大学第二弹《DeepSeek提示词工程和落地场景》

大家好&#xff0c;我是吾鳴。 之前给大家分享过北京大学出品的DeepSeek教程《DeepSeek与AIGC应用》&#xff0c;今天吾鳴发现北京大学又出第二版教程了&#xff0c;教程的名称叫做《DeepSeek提示词工程和落地场景》&#xff0c;在此分享给大家。文末有完整版PDF下载地址。 教程…

deepseek自动化代码生成

使用流程 效果第一步&#xff1a;注册生成各种大模型的API第二步&#xff1a;注册成功后生成API第三步&#xff1a;下载vscode在vscode中下载agent&#xff0c;这里推荐使用cline 第四步&#xff1a;安装完成后&#xff0c;设置模型信息第一步选择API provider&#xff1a; Ope…

322.零钱兑换

class Solution(object):def coinChange(self, coins, amount):""":type coins: List[int]:type amount: int:rtype: int"""n len(coins) dp [float(inf)]*(amount 1) # 初始值为正无穷大dp[0] 0 # 一定要初始化为0if amount 0:return 0 …

ARM Cortex-M处理器中的MSP和PSP

在ARM Cortex-M系列处理器中&#xff0c;MSP&#xff08;主堆栈指针&#xff09;和PSP&#xff08;进程堆栈指针&#xff09;是两种不同的堆栈指针&#xff0c;主要用于实现堆栈隔离和提升系统可靠性。以下是它们的核心区别和应用场景&#xff1a; 1. 基本定义 MSP&#xff08;…

交换机与路由器连接方式

交换机和路由器连接的三种主要方式如下&#xff1a; 一、直连连接 这是最简单直接的连接方式。通过一根网线将交换机的一个端口与路由器的一个LAN端口相连。这种连接方式适用于小型网络&#xff0c;其中交换机负责局域网内部的数据交换&#xff0c;而路由器则负责将内部网络连接…

Python代码片段-Excel导入到MongoDB

有一次遇到一个需求&#xff0c;需要把Excel的数据导入到MongoDB中&#xff0c;表面上感觉就是导入数据很简单&#xff0c;但实际操作后&#xff0c;发现是比较麻烦的一个事情&#xff0c;一般图形化的工具对于MongoDB而言&#xff0c;导入选项都是json的&#xff0c;根本没有E…

axios几种请求类型的格式

Axios 是一个基于 Promise 的 HTTP 客户端&#xff0c;广泛用于浏览器和 Node.js 中发送 HTTP 请求。它支持多种请求格式&#xff0c;包括 GET、POST、PUT、DELETE 等。也叫RESTful 目录 一、axios几种请求类型的格式 1、get请求 2、post请求 3、put请求 4、delete请求 二…