（空气呼啸） (air whooshing)
- 什么是动态填充？ - What is dynamic padding?
在 “一起批量输入” 视频中， In the "Batching Inputs together" video,
我们已经看到为了能够对（不同长度的同批次）输入进行分组 we have seen that to be able to group inputs
（同一批不同长度的）， of different lengths in the same batch,
我们需要向所有短输入添加填充标记 we need to add padding tokens to all the short inputs
直到它们的长度都相同。 until they are all of the same length.
例如，这里最长的句子是第三句， Here, for instance, the longest sentence is the third one,
我们需要添加五个、两个或七个填充标记 and we need to add five, two, or seven pad tokens
到其他句子使得四个句子具有 to the other sentences to have four sentences
相同的长度。 of the same lengths.
在处理整个数据集时， When dealing with a whole dataset,
我们可以应用各种填充策略。 there are various padding strategies we can apply.
最明显的一种是填充整个数据集所有的样本 The most obvious one is to pad all the elements
达到相同的长度： of the dataset to the same length:
最长样本的长度。 the length of the longest sample.
我们得到具有相同形状的批次 This will then give us batches
that all have the same shape
（其长度）由最大序列长度决定。 determined by the maximum sequence length.
缺点是（如果）批次样本由短句组成 The downside is that batches composed from short sentences
将带来很多填充符号 will have a lot of padding tokens
并且在模型中引入更多不必要的计算。 which will introduce more computations in the model
we ultimately don't need.
为了避免这种情况，另一种策略是填充（短样本）符号 To avoid this, another strategy is to pad the elements
当把它们放在一批时， when we batch them together,
达到本批次中最长句子的长度。 to the longest sentence inside the batch.
这样，由短样本输入组成的批次大小 This way, batches composed of short inputs will be smaller
会比按整个数据集最长句子的长度（补齐）批次更小 than the batch containing the longest sentence
in the dataset.
这将在 CPU 和 GPU 上产生一些不错的加速。 This will yield some nice speedup on CPU and GPU.
缺点是所有批次 The downside is that all batches
会有不同的形状， will then have different shapes,
这会减慢 TPU 等加速器的训练速度。 which slows down training on accelerators like TPUs.
让我们看看如何在实践中应用这两种策略。 Let's see how to apply both strategies in practice.
我们实际上已经知道了如何使用固定填充 We have actually seen how to apply fixed padding
在数据集概述视频中， in the Datasets Overview video,
当我们预处理 MRPC 数据集时： when we preprocessed the MRPC dataset:
加载数据集和分词器后， after loading the dataset and tokenizer,
我们将符号化应用于所有数据集 we applied the tokenization to all the dataset
包括填充和截断 with padding and truncation
保证所有样本的长度为 128 。 to make all samples of length 128.
最后，如果我们传递这个数据集 As a result, if we pass this dataset
到 PyTorch DataLoader， to a PyTorch DataLoader,
我们得到形状为 batch_size 乘以 16 乘以 128 的批次。 we get batches of shape batch size, here 16, by 128.
要应用动态填充， To apply dynamic padding,
我们必须将填充推迟到批量准备， we must defer the padding to the batch preparation,
所以我们从标记函数中删除了那部分。 so we remove that part from our tokenize function.
我们仍然保留截断部分 We still leave the truncation part
以便大于最大长度的输入 so that inputs that are bigger than the maximum length
被模型接受，通常是 512， accepted by the model, usually 512,
被截断到那个长度。 get truncated to that length.
然后我们动态地填充我们的样本 Then we pad our samples dynamically
通过使用数据整理器。 by using a data collator.
Transformers 库中的那些类 Those classes in the Transformers library
负责应用所有的最终处理 are responsible for applying all the final processing
在形成批次之前需要， needed before forming a batch,
这里 DataCollatorWithPadding 将填充样本 here DataCollatorWithPadding will pad the samples
到这批句子中的最大长度。 to the maximum length inside the batch of sentences.
我们将它作为整理函数传递给 PyTorch DataLoader， We pass it to the PyTorch DataLoader as a collate function,
然后观察到生成的批次 then observe that the batches generated
有不同的长度， have various lengths,
一直低于之前的 128。 all way below the 128 from before.
动态批处理几乎在 CPU 和 GPU 上更快， Dynamic batching will almost always be faster
所以如果可以的话你应该应用它。 on CPUs and GPUs, so you should apply it if you can.
但是，请记住切换回固定填充 Remember to switch back to fixed padding, however,
如果你在 TPU 上运行你的训练脚本 if you run your training script on TPU
或者需要固定形状的批次输入。 or need batches of fixed shapes.
（空气呼啸） (air whooshing)