当前位置: 首页 > news >正文

深入了解 Adam 优化器对显存的需求:以 LLaMA-2 7B 模型为例 (中英双语)

中文版

深入了解 Adam 优化器对显存的额外需求:模型参数与优化器状态的显存开销分析

在深度学习模型的训练过程中,显存是一个关键的资源,尤其在处理大型语言模型或深度神经网络时。训练时的显存需求不仅包括模型参数本身,还涉及梯度的存储以及优化器状态的额外开销。对于使用 AdamAdamW 优化器的训练过程,优化器的状态会增加额外的显存开销,这部分显存消耗需要单独分析。

本篇博客将重点讨论模型参数和优化器的状态所需的额外显存开销,并探讨如何分开计算这些部分,帮助你更好地理解训练显存的实际需求。

1. 模型训练与推理的显存需求对比

1.1 推理时的显存需求

在推理过程中,模型的显存需求较为简单,主要包括:

  • 模型参数:存储网络中每个参数的数值(例如权重和偏置)。
  • 激活值:在前向传播过程中,计算每层的输出需要存储中间结果(即激活值)。

对于大多数模型来说,推理时的显存需求主要集中在这两部分,它通常是模型参数所占显存的基础。具体来说,推理显存的计算大致为:
推理显存 = 模型参数显存 + 激活值显存(后者一般不考虑) \text{推理显存} = \text{模型参数显存} + \text{激活值显存}(后者一般不考虑) 推理显存=模型参数显存+激活值显存(后者一般不考虑)
推理时如果每次只处理一句话(例如一个短输入或一个 token),激活值显存需求会很小。

1.2 训练时的显存需求

与推理相比,训练时显存需求更为复杂,除了模型参数和激活值外,还要考虑:

  • 梯度存储:每次反向传播时,网络会计算并存储每个参数的梯度。这些梯度是训练中更新模型权重的关键,它们的存储和计算同样需要显存。
  • 优化器状态:如 Adam 等优化器需要存储每个参数的动量(一阶矩)和梯度平方的滑动平均(二阶矩),这部分开销会显著增加显存需求。

在计算训练时的显存需求时,通常可以按以下公式来估算:
训练显存 = 模型参数显存 + 梯度显存 + 优化器状态显存 \text{训练显存} = \text{模型参数显存} + \text{梯度显存} + \text{优化器状态显存} 训练显存=模型参数显存+梯度显存+优化器状态显存

2. 训练时的额外显存开销

训练时的显存需求大于推理时,主要来自于梯度存储和优化器状态的存储。具体来说:

  • 梯度存储:梯度的存储需要额外的显存。每个参数都会有一个对应的梯度,梯度和参数的大小相同,因此梯度所需的显存与模型参数显存相当。
  • 优化器状态存储:Adam 优化器需要为每个参数存储一阶矩和二阶矩(动量和梯度平方的滑动平均)。因此,优化器的显存需求通常是 模型参数显存的 2倍(即每个参数存储三项:参数本身、一阶矩、二阶矩)。
2.1 如何分开计算

在估算训练时显存需求时,可以将模型参数、梯度和优化器状态的显存开销分开计算。具体来说,训练时显存需求大致为:
训练显存 = 1 x ( 模型参数显存 ) + 1 x ( 梯度显存 ) + 1 − 2 x ( 优化器状态显存 ) \text{训练显存} = 1x \, (\text{模型参数显存}) + 1x \, (\text{梯度显存}) + 1-2x \, (\text{优化器状态显存}) 训练显存=1x(模型参数显存)+1x(梯度显存)+12x(优化器状态显存)
这意味着,训练所需的显存通常是推理显存的 4倍,其中:

  • 1x 为模型本身所占的显存。
  • 1x 为梯度的显存(和模型参数的显存相同)。
  • 1-2x 为优化器状态的显存。对于 Adam 优化器,这部分额外的显存开销通常是 模型显存的 2倍,因为 Adam 需要存储一阶和二阶矩(每个参数两项状态)。
2.2 Adam 优化器的显存消耗

Adam 优化器相比于传统的优化器,如 SGD,额外的显存消耗主要来自于两个部分:

  • 一阶矩(( m m m )):动量项,存储每个参数的梯度的指数加权平均。
  • 二阶矩(( v v v )):梯度平方的指数加权平均,类似于 RMSProp。

每个参数需要存储 三项数据:本身的值、一阶矩 ( m m m )、二阶矩 ( v v v ),因此显存需求为:
优化器状态显存 = 2 × 模型参数显存 \text{优化器状态显存} = 2 \times \text{模型参数显存} 优化器状态显存=2×模型参数显存
对于 AdamW,由于它与 Adam 在参数更新策略上有所不同,但在显存需求上几乎没有差异,因此我们可以认为它的额外显存消耗也是 2倍

2.3 不同精度下的显存需求

训练时的显存需求也受到数据类型精度的影响。常见的精度有 float32bfloat16,它们对显存的需求有所不同:

  • float32(32位浮动点数):每个参数、梯度和优化器状态都占 4 字节,因此显存需求相对较高。
  • bfloat16(16位浮动点数):每个参数、梯度和优化器状态仅占 2 字节,因此显存需求较低。

3. 实际计算:以 LLaMA-2 7B 模型为例

假设 LLaMA-2 7B 模型包含 70 亿个参数。我们以 float32 为例来计算训练时显存需求。

3.1 模型参数显存

每个参数占用 4 字节,总参数数量为 70 亿,则模型参数显存为:
模型参数显存 = 7 × 1 0 9 × 4 字节 = 28 GB ( 以1000为进位 ) \text{模型参数显存} = 7 \times 10^9 \times 4 \text{字节} = 28 \, \text{GB} \, (\text{以1000为进位}) 模型参数显存=7×109×4字节=28GB(1000为进位)

3.2 梯度显存

梯度显存与模型参数显存相同,因为每个参数都有一个对应的梯度:
梯度显存 = 28 GB \text{梯度显存} = 28 \, \text{GB} 梯度显存=28GB

3.3 优化器状态显存

Adam 优化器需要存储一阶矩和二阶矩,因此优化器状态的显存需求是模型参数显存的 2倍:
优化器状态显存 = 2 × 28 GB = 56 GB \text{优化器状态显存} = 2 \times 28 \, \text{GB} = 56 \, \text{GB} 优化器状态显存=2×28GB=56GB

3.4 训练显存需求

将模型参数显存、梯度显存和优化器状态显存加总,训练时的显存需求为:
训练显存 = 28 GB + 28 GB + 56 GB = 112 GB \text{训练显存} = 28 \, \text{GB} + 28 \, \text{GB} + 56 \, \text{GB} = 112 \, \text{GB} 训练显存=28GB+28GB+56GB=112GB

具体计算过程如下:

下面的python代码测试7B大模型本身的参数量:以float32计算。进位采用1024,计算得出:7B大模型的参数量为26.08 GB;当进位采用1000时,计算得出28.00 GB。为什么尝试1000,是因为在其他博文中看到28GB这个数字,自己测试一下,发现他们是在以1000为进位的时候测试得出的。参考文章:https://cuiyuhao.com/posts/c87c0f5d/

# 定义参数
num_parameters = 7 * 10**9  # 70 亿个参数
bytes_per_param = 4  # 每个参数占用 4 字节(32 位浮动数)# 计算显存需求(单位:字节)
memory_in_bytes = num_parameters * bytes_per_param# 将字节转换为 GB
memory_in_GB = memory_in_bytes / (1024 ** 3)  # 转换为 GB, 可调为1000print(f"模型需要的显存为: {memory_in_GB:.2f} GB")

以bf16为例,由于它是float32的一半,所以它的参数量为 26.08GB / 2 = 13.04GB (以1024为进位),当以1000进位的时候,28GB / 2 = 14GB

4. 总结

在深度学习模型训练中,除了模型参数显存外,梯度存储和优化器状态(如 Adam 优化器)会显著增加显存需求。通常情况下,训练显存需求是推理显存的 4倍,其中:

  • 1倍用于存储模型参数。
  • 1倍用于存储梯度。
  • 1-2倍用于存储优化器状态。

了解这些显存需求对于选择合适的硬件和优化训练过程至关重要。如果显存有限,可以通过使用混合精度训练(如 bfloat16)来降低显存消耗,或者利用梯度累积等技术来优化显存的使用。

5. 使用 bfloat16 计算显存消耗

与 float32 精度相比,使用 bfloat16 (16 位浮动数)来训练大模型可以显著降低显存消耗。bfloat16 精度的主要优点是相对于 float32,减少了内存的占用量,但保持了足够的数值表示精度,特别适用于训练深度神经网络。

5.1 bfloat16 对模型参数的影响

在使用 bfloat16 时,每个参数占用的内存从 float32 的 4 字节降到 2 字节。以 LLaMA-2 7B 模型为例,假设模型有 7 亿个参数:

  • 模型参数内存(使用 bfloat16):
    模型参数内存 = 7 × 1 0 9 × 2 字节 = 14 GB \text{模型参数内存} = 7 \times 10^9 \times 2 \, \text{字节} = 14 \, \text{GB} 模型参数内存=7×109×2字节=14GB
    因此,模型参数的内存需求降至 14 GB
5.2 bfloat16 对梯度的影响

梯度是通过反向传播计算得到的,用于更新模型参数。梯度的内存占用与模型参数相同,因此使用 bfloat16 计算梯度时,内存占用也将减半:

  • 梯度内存(使用 bfloat16):
    梯度内存 = 14 GB \text{梯度内存} = 14 \, \text{GB} 梯度内存=14GB
5.3 bfloat16 对优化器状态的影响

Adam 优化器的状态包括两个部分:一阶矩( m m m二阶矩( v v v。每个参数都需要存储这两项数据,导致优化器状态的内存占用是模型参数内存的两倍。

在 bfloat16 精度下,优化器状态的内存占用也将减少到 float32 的一半。因此,优化器状态的内存为:

  • 优化器状态内存(使用 bfloat16):
    优化器状态内存 = 2 × 14 GB = 28 GB \text{优化器状态内存} = 2 \times 14 \, \text{GB} = 28 \, \text{GB} 优化器状态内存=2×14GB=28GB
5.4 总训练显存需求

通过将 bfloat16 应用到模型参数、梯度和优化器状态的计算中,总的显存消耗将大幅下降。对于 LLaMA-2 7B 模型,使用 bfloat16 时的训练显存需求为:

训练显存 = 14 GB ( 模型参数 ) + 14 GB ( 梯度 ) + 28 GB ( 优化器状态 ) \text{训练显存} = 14 \, \text{GB} (\text{模型参数}) + 14 \, \text{GB} (\text{梯度}) + 28 \, \text{GB} (\text{优化器状态}) 训练显存=14GB(模型参数)+14GB(梯度)+28GB(优化器状态)
因此,使用 bfloat16 精度时,训练显存需求为 56 GB

5.5 总结
  • float32 精度:LLaMA-2 7B 模型训练需要约 112 GB 显存。
  • bfloat16 精度:LLaMA-2 7B 模型训练需要约 56 GB 显存。

使用 bfloat16 精度能有效减少训练时的显存需求,尤其是对于大模型来说,可以显著降低硬件成本,甚至使得原本无法在某些显卡上训练的大模型得以运行。因此,在显存资源有限的情况下,使用混合精度训练(例如采用 bfloat16)是一种提高训练效率的有效方法。

总之,选择合适的数值精度不仅能降低显存消耗,还能加速训练过程,特别是在面对大规模深度学习模型时,精度选择对训练的性能和可行性有着直接影响。

英文版

Understanding the Additional Memory Requirements of Adam Optimizer: Memory Consumption Breakdown for Model Parameters and Optimizer States

In deep learning model training, memory (or VRAM) is a critical resource, especially when dealing with large models like large language models or deep neural networks. The memory required for training is not only for storing the model parameters but also for the gradients and the additional memory consumed by the optimizer state. For optimizers like Adam or AdamW, the optimizer state introduces significant additional memory consumption, which is often overlooked when estimating memory requirements.

This blog post will focus on the memory overhead introduced by the optimizer and how to break down the memory requirements separately for model parameters and optimizer states. Understanding this is important for efficiently managing memory during training, especially for beginners who may not fully realize the impact of optimizers like Adam on memory consumption.

1. Memory Requirements for Inference vs. Training

1.1 Inference Memory Requirements

During inference (i.e., when you are just running the model and making predictions), the memory requirements are relatively straightforward and mainly include:

  • Model Parameters: Storing the model’s weights and biases.
  • Activations: The intermediate outputs produced during the forward pass that are required for computing the final result.

In most cases, the memory for inference is primarily used by these two components, and the memory formula can be simplified as:
Inference Memory = Model Parameters Memory + Activations Memory \text{Inference Memory} = \text{Model Parameters Memory} + \text{Activations Memory} Inference Memory=Model Parameters Memory+Activations Memory

1.2 Training Memory Requirements

In contrast to inference, training requires more memory due to the following additional components:

  • Gradients: During backpropagation, the gradients for each parameter need to be computed and stored. The gradients are necessary for updating the model parameters, and they take up memory equivalent to the size of the model parameters.
  • Optimizer State: Optimizers like Adam require additional memory to store per-parameter information such as the first moment (momentum) and the second moment (squared gradients). This memory overhead can be substantial, especially for large models.

Thus, the memory formula for training is:
Training Memory = Model Parameters Memory + Gradients Memory + Optimizer States Memory \text{Training Memory} = \text{Model Parameters Memory} + \text{Gradients Memory} + \text{Optimizer States Memory} Training Memory=Model Parameters Memory+Gradients Memory+Optimizer States Memory

2. The Additional Memory Overhead for Optimizer States

Training memory usage is higher than inference memory due to the extra storage required for gradients and optimizer states. Specifically:

  • Gradients: The memory required for storing gradients is the same as the memory for storing model parameters, because every parameter has a corresponding gradient.
  • Optimizer States: For Adam optimizer, each parameter requires storage for both the first and second moment (the running average of gradients and squared gradients), which results in the need for 2 times the memory of the model parameters.
2.1 Breaking Down the Memory Calculation

When estimating the total memory required for training, it is useful to break down the contributions from the model parameters, gradients, and optimizer state. The total memory for training is approximately:
Training Memory = 1 x ( Model Parameters Memory ) + 1 x ( Gradients Memory ) + 1 − 2 x ( Optimizer States Memory ) \text{Training Memory} = 1x \, (\text{Model Parameters Memory}) + 1x \, (\text{Gradients Memory}) + 1-2x \, (\text{Optimizer States Memory}) Training Memory=1x(Model Parameters Memory)+1x(Gradients Memory)+12x(Optimizer States Memory)
This means the memory for training is typically 4 times the memory required for inference:

  • 1x for the model parameters.
  • 1x for the gradients (same memory as model parameters).
  • 1-2x for the optimizer states. For Adam optimizer, the overhead is typically 2 times the model memory because it stores both the first and second moments.
2.2 Memory Consumption of Adam Optimizer

The Adam optimizer consumes extra memory because it needs to store both the first moment (( m m m )) and the second moment (( v v v )) for each parameter. Therefore, the memory consumed by the optimizer is:
Optimizer States Memory = 2 × Model Parameters Memory \text{Optimizer States Memory} = 2 \times \text{Model Parameters Memory} Optimizer States Memory=2×Model Parameters Memory
Similarly, AdamW optimizer, which differs from Adam only in the weight decay application, has almost the same memory consumption as Adam, so its extra memory overhead is also 2 times the model parameters memory.

2.3 Impact of Precision on Memory Requirements

The memory usage for training is also affected by the data type (precision) used. Commonly, float32 and bfloat16 are used for training, and the precision affects memory consumption as follows:

  • float32 (32-bit floating point): Each parameter, gradient, and optimizer state consumes 4 bytes, which results in higher memory usage.
  • bfloat16 (16-bit floating point): Each parameter, gradient, and optimizer state consumes 2 bytes, leading to reduced memory consumption.

Thus, using bfloat16 instead of float32 can significantly reduce the overall memory requirements for both the model parameters and the optimizer states.

3. Practical Example: LLaMA-2 7B Model

Let’s walk through an example of estimating the memory for training a LLaMA-2 7B model, which contains 7 billion parameters. We will first calculate the memory requirements assuming float32 precision.

3.1 Model Parameters Memory

Each parameter in the model requires 4 bytes (for float32), and with 7 billion parameters, the model parameters memory is:
Model Parameters Memory = 7 × 1 0 9 × 4 bytes = 28 GB ( using 1000-based units ) \text{Model Parameters Memory} = 7 \times 10^9 \times 4 \, \text{bytes} = 28 \, \text{GB} \, (\text{using 1000-based units}) Model Parameters Memory=7×109×4bytes=28GB(using 1000-based units)

3.2 Gradients Memory

The gradients memory is the same as the model parameters memory because we need to store one gradient per parameter:
Gradients Memory = 28 GB \text{Gradients Memory} = 28 \, \text{GB} Gradients Memory=28GB

3.3 Optimizer States Memory

Since the Adam optimizer needs to store both the first and second moments, the optimizer states memory will be 2 times the model parameters memory:
Optimizer States Memory = 2 × 28 GB = 56 GB \text{Optimizer States Memory} = 2 \times 28 \, \text{GB} = 56 \, \text{GB} Optimizer States Memory=2×28GB=56GB

3.4 Total Training Memory

The total memory required for training is the sum of model parameters, gradients, and optimizer states:
Training Memory = 28 GB + 28 GB + 56 GB = 112 GB \text{Training Memory} = 28 \, \text{GB} + 28 \, \text{GB} + 56 \, \text{GB} = 112 \, \text{GB} Training Memory=28GB+28GB+56GB=112GB

4. Summary

In summary, when estimating memory for training a deep learning model, especially using optimizers like Adam, you need to consider not only the model parameters but also the gradients and the additional memory consumed by the optimizer state. Typically, the memory required for training is about 4 times the memory required for inference, where:

  • 1x is for the model parameters.
  • 1x is for the gradients.
  • 1-2x is for the optimizer states.

Optimizers like Adam consume significant additional memory because they store both first and second moment estimates for each parameter. For large models, this memory overhead can be substantial, so it’s important to account for it when selecting hardware and configuring training settings. If memory is limited, techniques like mixed-precision training (e.g., using bfloat16) can help reduce memory usage.

5. Memory Consumption with bfloat16 Precision

Using bfloat16 (16-bit floating point) instead of float32 (32-bit floating point) during training can significantly reduce memory consumption. The primary advantage of bfloat16 is that it reduces memory usage while maintaining sufficient numerical precision, making it particularly well-suited for training deep neural networks.

5.1 Impact of bfloat16 on Model Parameters

With bfloat16 precision, each parameter consumes 2 bytes, as opposed to 4 bytes with float32. For example, for the LLaMA-2 7B model, which has 7 billion parameters:

  • Model Parameters Memory (using bfloat16):
    Model Parameters Memory = 7 × 1 0 9 × 2 bytes = 14 GB \text{Model Parameters Memory} = 7 \times 10^9 \times 2 \, \text{bytes} = 14 \, \text{GB} Model Parameters Memory=7×109×2bytes=14GB
    Therefore, the memory required for the model parameters is reduced to 14 GB when using bfloat16.
5.2 Impact of bfloat16 on Gradients

Gradients are computed during backpropagation and are used to update the model parameters. The memory required to store gradients is the same as for the model parameters. Thus, when using bfloat16 for gradients, memory usage is also halved:

  • Gradients Memory (using bfloat16):
    Gradients Memory = 14 GB \text{Gradients Memory} = 14 \, \text{GB} Gradients Memory=14GB
5.3 Impact of bfloat16 on Optimizer States

The Adam optimizer stores two components for each parameter: the first moment ( m m m) and the second moment ( v v v). This results in the optimizer state requiring 2 times the memory of the model parameters.

Since bfloat16 uses 2 bytes per value, the memory overhead for the optimizer state is also halved compared to float32. Thus, the optimizer states’ memory in bfloat16 will be:

  • Optimizer States Memory (using bfloat16):
    Optimizer States Memory = 2 × 14 GB = 28 GB \text{Optimizer States Memory} = 2 \times 14 \, \text{GB} = 28 \, \text{GB} Optimizer States Memory=2×14GB=28GB
5.4 Total Memory Requirement for Training

When applying bfloat16 to model parameters, gradients, and optimizer states, the overall memory consumption for training is significantly reduced. For the LLaMA-2 7B model, the total memory required for training with bfloat16 would be:

Training Memory = 14 GB ( Model Parameters ) + 14 GB ( Gradients ) + 28 GB ( Optimizer States ) \text{Training Memory} = 14 \, \text{GB} (\text{Model Parameters}) + 14 \, \text{GB} (\text{Gradients}) + 28 \, \text{GB} (\text{Optimizer States}) Training Memory=14GB(Model Parameters)+14GB(Gradients)+28GB(Optimizer States)
Thus, the total memory requirement for training the LLaMA-2 7B model using bfloat16 precision would be 56 GB.

5.5 Summary
  • float32 precision: The training memory requirement for LLaMA-2 7B is approximately 112 GB.
  • bfloat16 precision: The training memory requirement for LLaMA-2 7B is approximately 56 GB.

By using bfloat16 precision, the memory consumption for training is reduced by half compared to float32, which can significantly lower hardware costs and make it feasible to train large models on GPUs with limited memory. Therefore, when memory is a constraint, using mixed-precision training (e.g., with bfloat16) is an effective strategy to improve training efficiency.

In summary, choosing the right precision for your training process not only reduces memory usage but also accelerates training. This decision has a direct impact on the performance and feasibility of training large-scale deep learning models, making precision selection critical, especially when working with very large models.

后记

2024年11月29日17点14分于上海,在GPT4o大模型辅助下完成。

相关文章:

深入了解 Adam 优化器对显存的需求:以 LLaMA-2 7B 模型为例 (中英双语)

中文版 深入了解 Adam 优化器对显存的额外需求:模型参数与优化器状态的显存开销分析 在深度学习模型的训练过程中,显存是一个关键的资源,尤其在处理大型语言模型或深度神经网络时。训练时的显存需求不仅包括模型参数本身,还涉及…...

深入理解CSS语法:掌握Web开发的基石

深入理解CSS语法:掌握Web开发的基石 在构建现代网页的过程中,层叠样式表(CSS)扮演着至关重要的角色。它不仅负责定义HTML元素的视觉表现,还极大地增强了Web内容的可访问性和用户体验。本文将深入探讨CSS的语法、书写位…...

基于python爬虫的智慧人才数据分析系统

废话不多说,先看效果图 更多效果图可私信我获取 源码分享 import os import sysdef main():"""Run administrative tasks."""os.environ.setdefault(DJANGO_SETTINGS_MODULE, 智慧人才数据分析系统.settings)try:from django.core.m…...

创建maven私人创库nexus

1.到官网下载nexus-3.74.0-05-unix.tar.gz包,若下载慢可以去这里下载地址 2.上传到liunx的根目录opt文件中,然后解压命令: tar -xzf nexus-3.74.0-05-unix.tar.gz 3.解压后会得到两个文件夹nexus-3.74.0-05 和 sonatype-work ,…...

眼部按摩仪WT2605音频蓝牙语音芯片方案 单芯片实现语音提示及控制/手机无线音频传输功能

随着科技的快速发展,人们的生活方式也在不断改变,智能化、便捷化的产品逐渐成为市场的主流。眼部按摩仪作为一种结合了现代科技与健康生活理念的产品,受到了广大消费者的青睐。而在众多眼部按摩仪中,采用WT2605音频蓝牙芯片的方案…...

【面试题】2025年百度校招Java后端面试题

文章目录 前言一、网络IO1、服务器处理并发请求有哪几种方式?2、说一下select,poll,epoll的区别?3、Java 有一种现代的处理方式,属于异步I/O,是什么?redis,nginx,netty 是…...

Java面向对象. 多态

目录 java多态是什么东西?首先要理解什么是多态 Java多态指同一行为具有多个不同表现形式。如父类引用指向子类对象,调用重写方法时呈现不同结果。 1.多态的概念 一、多态的基本概念 二、多态的实现方式 接口实现 三、多态的好处 提高代码的可扩展…...

Vue 项目中有哪些内存泄漏的场景,以及预防内存泄漏技巧

前言 即便是功能强大的 Vue.js 也无法完全避免内存泄漏的问题,内存泄漏不仅会影响应用的性能,还可能导致浏览器崩溃。因此,识别和解决 Vue 项目中的内存泄漏问题是确保项目稳定性和性能的关键。 本文将通俗易懂地介绍 Vue 项目中常见的内存泄…...

MySQL Inception工具

MySQL Inception是一个强大的数据库变更管理和审计工具,主要用于审核、执行、备份以及回滚数据库操作。它的工作模式和MySQL完全相同,可以直接使用MySQL客户端来连接,但不需要验证权限。Inception相对于应用程序(上层审核流程系统…...

Linux(Centos7)---安装nginx(很简单)

安装 sudo yum install nginx -y开机启动与开启服务 sudo systemctl enable nginx sudo systemctl start nginx...

多数元素

多数元素 给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。 你可以假设数组是非空的,并且给定的数组总是存在多数元素。 示例 1: 输入:nums [3,2,3] 输出&#xff…...

vim编辑器的一些配置和快捷键

记录vim编辑器的一些配置和快捷键,边学边用: yy 复制dd 删除p:粘贴ctrly 取消撤销u:撤销:w 写入:q 退出a/i 插入O: 上方插入一个空行o:下方插入一个空行:e 打开文件编辑 其他配置: 上移一行和下移一行&a…...

SeggisV1.0 遥感影像分割软件【源代码】讲解

在此基础上进行二次开发,开发自己的软件,例如:【1】无人机及个人私有影像识别【2】离线使用【3】变化监测模型集成【4】个人私有分割模型集成等等,不管是您用来个人学习还是公司研发需求,都相当合适,包您满…...

14 —— Webpack解析别名

import {checkPhone, checkCode} from ../src/utils/check.js 这么使用相对路径不安全 —— 在webpack.config.js中配置解析别名来代表src绝对路径...

《C++ Primer Plus》学习笔记|第8章 函数探幽 (24-11-30更新)

文章目录 8.1 内联函数8.2 引用变量8.2.1 创建引用变量8.2.2 将引用用作函数参数8.2.3 引用的属性和特别之处特点1:在计算过程中,传入的形参的值也被改变了。特点2:使用引用的函数参数只接受变量,而不接受变量与数值的运算左值引用…...

vscode的项目给gitlab上传

目录 一.创建gitlab帐号 二.在gitlab创建项目仓库 三.Windows电脑安装Git 四.vscode项目git上传 一.创建gitlab帐号 二.在gitlab创建项目仓库 图来自:Git-Gitlab中如何创建项目、创建Repository、以及如何删除项目_gitlab新建项目-CSDN博客) 三.Windows电脑安…...

【JVM什么时候触发YoungGC和FullGC】

YoungGC 年轻代Eden区满,就会触发YoungGC FullGC 老年代空间不足 经过多次GC后的大年龄对象会被放进老年代,或创建的大对象会直接在老年代分配,此时若老年代空间不足,就会触发FullGC。空间分配担保失败 触发YoungGC的时候会进行…...

Mybatis-批量删除用户

目录 题目: 1、创建Maven项目,在pom.xml中添加相关依赖 2、创建db.properties配置数据库信息 3、在resources下创建mybatis-config.xml核心配置文件 易错点: 4、在java中创建User.java,在java/com/haust/pojo下 5、在java中…...

Python的秘密基地--[章节2]Python核心数据结构

第2章:Python核心数据结构 Python中的数据结构提供了强大的工具来存储和操作数据。理解这些数据结构是Python编程的基础。 2.1 列表(List) 2.1.1 什么是列表 列表是一种有序的可变序列,用于存储一组数据。它支持多种类型的数据…...

TYUT设计模式精华版

七大原则 单一职责原则 职责要单一不能将太多的职责放在一个类中 开闭原则 软件实体对扩展是开放的,但对修改是关闭的 里氏代换原则 一个可以接受基类对象的地方必然可以接受子类 依赖倒转原则 要针对抽象层编程,而不要针对具体类编程 接口隔离原则 …...

责任链模式

07-责任链模式 1 导读 1.1 定义 包含了一些命令对象和一系列处理对象。每个处理对象决定它能处理哪些命令对象,它也知道如何将它不能处理的命令对象传递给该链中的下一个处理对象。 该模式还描述了往该处理链的末尾添加新的处理对象的方法。 精简定义:为…...

2024.11.29(单链表)

思维导图 声明文件 #ifndef __LINKLIST_H__ #define __LINKLIST_H__#include <myhead.h>typedef char datatype; //数据元素类型 //定义节点类型 typedef struct Node {union{int len; //头节点数据域datatype data; //普通节点数据域};struct Node *next; //指针域…...

用Python做数据分析环境搭建及工具使用(Jupyter)

目录 一、Anaconda下载、安装 二、Jupyter 打开 三、Jupyter 常用快捷键 3.1 创建控制台 3.2 命令行模式下的快捷键 3.3 运行模式下快捷键 3.4 代码模式和笔记模式 3.5 编写Python代码 一、Anaconda下载、安装 【最新最全】Anaconda安装python环境_anaconda配置python…...

洛谷P1443 马的遍历

简单的bfs 题目链接 P1443 马的遍历 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题目描述 有一个 nm 的棋盘&#xff0c;在某个点(x,y) 上有一个马&#xff0c;要求你计算出马到达棋盘上任意一个点最少要走几步。 输入格式 输入只有一行四个整数&#xff0c;分别为 n…...

实现一个Vue自定义指令

在 Vue 中&#xff0c;自定义指令允许你为 DOM 元素添加特定的行为或功能。下面是一个实现 Vue 自定义指令的简单示例&#xff0c;展示了如何创建一个指令&#xff0c;使得元素在鼠标悬停时改变背景色。 1. 创建自定义指令 在 Vue 2.x 中&#xff0c;你可以在 Vue.directive …...

【已解决】git push需要输入用户名和密码问题

解决方法&#xff1a; 1&#xff09;查看使用的clone方式&#xff1a; git remote -v 2&#xff09;若为HTTPS&#xff0c;删除原clone方式: git remote rm origin 3&#xff09;添加新的clone方式&#xff1a; git remote add origin gitgithub.com:zludon/git_test.git …...

什么是内存对齐?为什么需要内存对齐?

1. 什么是内存对齐 内存对齐是指将数据存储在内存中时&#xff0c;按照一定的规则让数据排列在规定的地址上。具体来说&#xff0c;每个成员变量会按照其自身所占用的字节数对齐&#xff0c;内存首地址为对齐周期的倍数&#xff0c;而对齐周期指的是数据类型的大小。例如&…...

数据库操作、锁特性

1. DML、DDL和DQL是数据库操作语言的三种主要类型 1.1 DML&#xff08;Data Manipulation Language&#xff09;数据操纵语言 DML是用于检索、插入、更新和删除数据库中数据的SQL语句。 主要的DML语句包括&#xff1a; SELECT&#xff1a;用于查询数据库中的数据。 INSERT&a…...

xiaolin coding 图解网络笔记——TCP篇

1. TCP 头格式有哪些&#xff1f; 序列号&#xff1a;在建立连接时由计算机生成的随机数作为其初始值&#xff0c;通过 SYN 包传给接收端主机&#xff0c;每发送一次数据&#xff0c;就【累加】一次该【数据字节数】的大小。用来解决网络包乱序问题。 确认应答号&#xff1a;指…...

基于大数据python 豆果美食推荐数据可视化系统(源码+LW+部署讲解+数据库+ppt)

&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01; 很对人不知道选题怎么选 不清楚自己适合做哪块内容 都可以免费来问我 避免后期給自己答辩找麻烦 增加难度&#xff08;部分学校只有一次答辩机会 没弄好就延迟…...

ElasticSearch通过es-head插件安装可视化及相关问题

1.es-head下载地址 GitHub - mobz/elasticsearch-head: A web front end for an elastic search cluster 2.启动 建议使用vscode启动&#xff0c;并安装好node.js环境 npm installnpm run start 通过http://localhost:9100就可以看到本地添加的es库 3.相关问题 3.1跨域问…...

JVM系列之OOM观测准备

OOM, 全称 “Out Of Memory”&#xff0c;即内存用完的意思。JVM 因为没有足够的内存来为对象分配空间并且垃圾回收器也已经没有空间可回收时&#xff08;可分配内存大于需要分配的内存&#xff09;, 就会抛出 java.lang.OutOfMemoryError。在实际的生产应用中&#xff0c;一旦…...

基于Java Springboot Vue3图书管理系统

一、作品包含 源码数据库设计文档万字全套环境和工具资源部署教程 二、项目技术 前端技术&#xff1a;Html、Css、Js、Vue3、Element-ui 数据库&#xff1a;MySQL 后端技术&#xff1a;Java、Spring Boot、MyBatis 三、运行环境 开发工具&#xff1a;IDEA 数据库&#x…...

解析生成对抗网络(GAN):原理与应用

目录 一、引言 二、生成对抗网络原理 &#xff08;一&#xff09;基本架构 &#xff08;二&#xff09;训练过程 三、生成对抗网络的应用 &#xff08;一&#xff09;图像生成 无条件图像生成&#xff1a; &#xff08;二&#xff09;数据增强 &#xff08;三&#xff…...

torch.maximum函数介绍

torch.maximum 函数介绍 定义&#xff1a;torch.maximum(input, other) 返回两个张量的逐元素最大值。 输入参数&#xff1a; input: 张量&#xff0c;表示第一个输入。other: 张量或标量&#xff0c;表示第二个输入。若为张量&#xff0c;其形状需要能与 input 广播。输出&a…...

Git | 理解团队合作中Git分支的合并操作

合并操作 团队合作中Git分支的合并操作分支合并过程1.创建feature/A分支的过程2. 创建分支feature/A-COPY3.合并分支查看代码是否改变 团队合作中Git分支的合并操作 需求 假设团队项目中的主分支是main,团队成员A基于主分支main创建了feature/A&#xff0c;而我又在团队成员A创…...

源代码定制编译:构建理想的库 以curl为例

文章目录 源代码curl开发环境下载地址制定理想的库初级进阶如何知道选项名称交叉编译交叉编译工具链配置编译环境设置目标架构库和头文件路径编译代码 源代码 我们在日常中会接触到比较多第三方库&#xff0c;比如 网络库相关&#xff1a; libevent、mongoose、curl图形界面&…...

服务熔断-熔断器设计

文章目录 服务为什么需要熔断熔断器设计思想熔断器代码实现 服务为什么需要熔断 对于服务端采用的保护机制为服务限流。 对于服务调用端是否存在保护机制&#xff1f; 假如要发布一个服务 B&#xff0c;而服务 B 又依赖服务 C&#xff0c;当一个服务 A 来调用服务 B 时&#x…...

生产环境中:Flume 与 Prometheus 集成

在生产环境中&#xff0c;将 Apache Flume 与 Prometheus 集成的过程&#xff0c;需要借助 JMX Exporter 或 HTTP Exporter 来将 Flume 的监控数据转换为 Prometheus 格式。以下是详细的实现方法&#xff0c;连同原理和原因进行逐步解释&#xff0c;让刚接触的初学者也能完成集…...

深度解析MySQL的刷脏机制

前言 今天天气挺好,看大家对MySQL系列这么感兴趣,继续来聊聊MySQL,在MySQL的InnoDB中Buffer Pool和LSN关系紧密相连,尤其在脏页管理和刷新过程中起着至关重要的作用。理解LSN如何同Buffer Pool协同工作,有助于深入掌握 MySQL 的写入优化机制及数据一致性保证。 Buffer P…...

Java NIO 全面详解:初学者入门指南

除了前一篇文章讲的传统的 java.io 模块&#xff0c;Java 还提供了更现代化、更高效的非阻塞 IO 模块&#xff0c;即 java.nio&#xff08;New IO&#xff09;。java.nio 引入了面向缓冲区&#xff08;Buffer&#xff09;的数据处理方式&#xff0c;以及多路复用器&#xff08;…...

优化 Conda 下载速度:详细的代理配置和网络管理策略

优化 Conda 下载速度&#xff1a;详细的代理配置和网络管理策略 为了彻底解决使用 Conda 下载 PyTorch 时遇到的速度问题&#xff0c;并确保下载过程稳定可靠&#xff0c;这需要一个详细、综合的技术方案。让我们更深入地分析问题原因&#xff0c;然后详尽地解释采取的解决策略…...

蓝牙MCU单片机8k高回报率无线应用

随着高端无线产品性能大幅提升&#xff0c;相比常规蓝牙133Hz回报率&#xff0c;8kHz回报率作为市场最高标准&#xff0c;每秒上传8000个数据包。以鼠标为例&#xff0c;位置每秒更新8000次&#xff0c;刷新率较常规蓝牙提升了60倍&#xff0c;超低延迟、极速响应&#xff0c;已…...

Java抛出自定义运行运行

1.重新生成异常的.java文件 Empty&#xff1a;空 Exception&#xff1a;异常 加起来就是 空指针异常的文件 2.打上extends 运行的异常&#xff08;异常的类型&#xff09; 3.点击ctrlo&#xff0c;选着这两个快捷重写 4.在需要抛出异常的地方写上&#xff1a;th…...

JVM 性能调优 -- JVM常用调优工具【jps、jstack、jmap、jstats 命令】

前言&#xff1a; 前面我们分析怎么去预估系统资源&#xff0c;怎么去设置 JVM 参数以及怎么去看 GC 日志&#xff0c;本篇我们分享一些常用的 JVM 调优工具&#xff0c;我们在进行 JVM 调优的时候&#xff0c;通常需要借助一些工具来对系统的进行相关分析&#xff0c;从而确定…...

python+django自动化部署日志采用‌WebSocket前端实时展示

一、开发环境搭建和配置 # channels是一个用于在Django中实现WebSocket、HTTP/2和其他异步协议的库。 pip install channels#channels-redis是一个用于在Django Channels中使用Redis作为后台存储的库。它可以用于处理#WebSocket连接的持久化和消息传递。 pip install channels…...

【Flink-scala】DataStream编程模型之窗口计算-触发器-驱逐器

DataStream API编程模型 1.【Flink-Scala】DataStream编程模型之数据源、数据转换、数据输出 2.【Flink-scala】DataStream编程模型之 窗口的划分-时间概念-窗口计算程序 文章目录 DataStream API编程模型前言1.触发器1.1 代码示例 2.驱逐器2.1 代码示例 总结 前言 本小节我想…...

毕昇入门学习

schemas.py 概述 这段代码主要定义了一系列基于 Pydantic 的数据模型&#xff08;BaseModel&#xff09;&#xff0c;用于数据验证和序列化&#xff0c;通常用于构建 API&#xff08;如使用 FastAPI&#xff09;。这些模型涵盖了用户认证、聊天消息、知识库管理、模型配置等多…...

实时数据开发|Flink实现数据输出--DataSinks操作

哇哦&#xff0c;又是快乐周五&#xff01;今天主管又又又请我们喝奶茶了&#xff0c;是乐乐茶的草莓新品。甜甜的草莓配上糯叽叽的麻薯&#xff0c;喝完好满足。这应该不是什么加班信号吧哈哈哈&#xff0c;不加不加周五要回家。 前几天被不同的bug缠身&#xff0c;今天终于正…...

详解网络代理模式:规则、全局与直连的应用与配置

“详解网络代理模式&#xff1a;规则、全局与直连的应用与配置” 当然&#xff0c;为了提供更深入的理解&#xff0c;让我们对每种代理模式进行更详尽的探讨&#xff0c;包括它们的内部工作机制、具体使用场景以及在实际应用中的优势和局限。 规则模式&#xff08;Rule-based…...