Hugging Face 支持 PyTorch/XLA TPU 训练

原标题:Hugging Face on PyTorch / XLA TPUs
45AI 研判分

Hugging Face 与 Google 合作,在 Transformers 库中正式集成对 Cloud TPUs 的 PyTorch/XLA 支持。开发者无需修改代码即可通过原有 Trainer 接口在 TPU 上高效训练模型,旨在降低算力成本并提升扩展性。

关联知识实体

报道正文

本文目录12 个章节

返回文章列表

Hugging Face 在 PyTorch / XLA TPU 上:更快、更便宜的训练

发布于 2021 年 2 月 9 日 在 GitHub 上更新 点赞 4

ds jysohn23 客座作者 Lysandre lysandre

使用 PyTorch / XLA 在 Cloud TPUs 上训练你喜欢的 Transformers

PyTorch-TPU 项目最初是 Facebook PyTorch 团队和 Google TPU 团队合作的成果,并于 2019 年 PyTorch 开发者大会(PyTorch Developer Conference 2019)上正式发布。自那时起,我们与 Hugging Face 团队合作,为使用 PyTorch / XLA 在 Cloud TPUs 上进行训练提供一流的支持。这一新的集成使 PyTorch 用户能够在 Cloud TPUs 上运行并扩展其模型,同时保持与 Hugging Face trainers 接口完全一致。

本文概述了 Hugging Face 库中所做的更改、PyTorch / XLA 库的功能、一个帮助你开始在 Cloud TPUs 上训练你喜欢的 transformers 的示例,以及一些性能基准测试。如果你迫不及待想开始使用 TPUs,请直接跳到“在 Cloud TPUs 上训练你的 Transformer”部分——我们在 Trainer 模块中为你处理所有 PyTorch / XLA 机制!

XLA:TPU 设备类型

PyTorch / XLA 向 PyTorch 添加了一种新的 xla 设备类型。这种设备类型的工作方式与其他 PyTorch 设备类型相同。例如,以下是创建和打印 XLA tensor 的方法:

PYTHON
import torch
import torch_xla
import torch_xla.core.xla_model as xm

t = torch.randn(2, 2, device=xm.xla_device())
print(t.device)
print(t)

这段代码看起来应该很熟悉。PyTorch / XLA 使用与普通 PyTorch 相同的接口,并增加了一些功能。导入 torch_xla 会初始化 PyTorch / XLA,而 xm.xla_device() 返回当前的 XLA 设备。根据你的环境,这可能是一个 CPU、GPU 或 TPU,但在本文中我们将主要关注 TPU。

Trainer 模块利用 TrainingArguments 数据类来定义训练的具体细节。它处理多种参数,包括批大小、学习率、梯度累积以及其他参数,还有所使用的设备。基于上述内容,在 TrainingArguments._setup_devices() 中,当使用 XLA:TPU 设备时,我们只需返回供 Trainer 使用的 TPU 设备:

PYTHON
@dataclass
class TrainingArguments:
    ...
    @cached_property
    @torch_required
    def _setup_devices(self) -> Tuple["torch.device", int]:
        ...
        elif is_torch_tpu_available():
            device = xm.xla_device()
            n_gpu = 0
        ...

return device, n_gpu

XLA 设备步数计算

在典型的 XLA:TPU 训练场景中,我们在多个 TPU 核心上并行进行训练(单个 Cloud TPU 设备包含 8 个 TPU 核心)。因此,我们需要确保所有梯度通过整合梯度并执行优化器步骤,在数据并行副本之间进行交换。为此,我们提供了 xm.optimizer_step(optimizer),该函数负责梯度整合和步骤执行。相应地,在 Hugging Face trainer 中,我们更新训练步骤以使用 PyTorch / XLA API:

PYTHON
class Trainer:
…
   def train(self, *args, **kwargs):
       ...
                    if is_torch_tpu_available():
                        xm.optimizer_step(self.optimizer)

PyTorch / XLA 输入流水线

运行 PyTorch / XLA 模型主要有两个部分:(1) 惰性追踪和执行你的模型图(请参阅下方 “PyTorch / XLA Library” 章节以获取更详细的解释)以及 (2) 为你的模型提供输入。如果没有优化,模型的追踪/执行和输入提供将串行执行,导致主机 CPU 和 TPU 加速器分别出现空闲时间段。为了避免这种情况,我们提供了一个 API,将这两个过程流水线化,从而能够在第 n 步仍在执行时重叠第 n+1 步的追踪。

alt text

PYTHON
import torch_xla.distributed.parallel_loader as pl
...
  dataloader = pl.MpDeviceLoader(dataloader, device)

Checkpoint 写入与加载

When a tensor is checkpointed from a XLA device and then loaded back from the checkpoint, it will be loaded back to the original device. Before checkpointing tensors in your model, you want to ensure that all of your tensors are on CPU devices instead of XLA devices. This way, when you load back the tensors, you’ll load them through CPU devices and then have the opportunity to place them on whatever XLA devices you desire. We provide the xm.save() API for this, which already takes care of only writing to storage location from only one process on each host (or one globally if using a shared file system across hosts).

PYTHON
class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin):
…
    def save_pretrained(self, save_directory):
        ...
        if getattr(self.config, "xla_device", False):
            import torch_xla.core.xla_model as xm

if xm.is_master_ordinal():

model_to_save.config.save_pretrained(save_directory)

xm.save(state_dict, output_model_file)
PYTHON
class Trainer:
…
   def train(self, *args, **kwargs):
       ...
       if is_torch_tpu_available():
           xm.rendezvous("saving_optimizer_states")
           xm.save(self.optimizer.state_dict(),
                   os.path.join(output_dir, "optimizer.pt"))
           xm.save(self.lr_scheduler.state_dict(),
                   os.path.join(output_dir, "scheduler.pt"))

PyTorch / XLA Library

PyTorch / XLA is a Python package that uses the XLA linear algebra compiler to connect the PyTorch deep learning framework with XLA devices, which includes CPU, GPU, and Cloud TPUs. Part of the following content is also available in our API_GUIDE.md.

PyTorch / XLA Tensors are Lazy

Using XLA tensors and devices requires changing only a few lines of code. However, even though XLA tensors act a lot like CPU and CUDA tensors, their internals are different. CPU and CUDA tensors launch operations immediately or eagerly. XLA tensors, on the other hand, are lazy. They record operations in a graph until the results are needed. Deferring execution like this lets XLA optimize it. A graph of multiple separate operations might be fused into a single optimized operation.

Lazy execution is generally invisible to the caller. PyTorch / XLA automatically constructs the graphs, sends them to XLA devices, and synchronizes when copying data between an XLA device and the CPU. Inserting a barrier when taking an optimizer step explicitly synchronizes the CPU and the XLA device.

This means that when you call model(input) forward pass, calculate your loss loss.backward(), and take an optimization step xm.optimizer_step(optimizer), the graph of all operations is being built in the background. Only when you either explicitly evaluate the tensor (ex. Printing the tensor or moving it to a CPU device) or mark a step (this will be done by the MpDeviceLoader everytime you iterate through it), does the full step get executed.

Trace, Compile, Execute, and Repeat

From a user’s point of view, a typical training regimen for a model running on PyTorch / XLA involves running a forward pass, backward pass, and optimizer step. From the PyTorch / XLA library point of view, things look a little different.

While a user runs their forward and backward passes, an intermediate representation (IR) graph is traced on the fly. The IR graph leading to each root/output tensor can be inspected as following:

PYTHON
>>> import torch
>>> import torch_xla
>>> import torch_xla.core.xla_model as xm
>>> t = torch.tensor(1, device=xm.xla_device())
>>> s = t*t
>>> print(torch_xla._XLAC._get_xla_tensors_text([s]))
IR {
  %0 = s64[] prim::Constant(), value=1
  %1 = s64[] prim::Constant(), value=0
  %2 = s64[] xla::as_strided_view_update(%1, %0), size=(), stride=(), storage_offset=0
  %3 = s64[] aten::as_strided(%2), size=(), stride=(), storage_offset=0
  %4 = s64[] aten::mul(%3, %3), ROOT=0
}

This live graph is accumulated while the forward and backward passes are run on the user's program, and once xm.mark_step() is called (indirectly by pl.MpDeviceLoader), the graph of live tensors is cut. This truncation marks the completion of one step and subsequently we lower the IR graph into XLA Higher Level Operations (HLO), which is the IR language for XLA.

[注:本文篇幅超长,以上为核心前篇精译,后续内容保留原文呈现]

This HLO graph then gets compiled into a TPU binary and subsequently executed on the TPU devices. However, this compilation step can be costly, typically taking longer than a single step, so if we were to compile the user’s program every single step, overhead would be high. To avoid this, we have caches that store compiled TPU binaries keyed by their HLO graphs’ unique hash identifiers. So once this TPU binary cache has been populated on the first step, subsequent steps will typically not have to re-compile new TPU binaries; instead, they can simply look up the necessary binaries from the cache.

Since TPU compilations are typically much slower than the step execution time, this means that if the graph keeps changing in shape, we’ll have cache misses and compile too frequently. To minimize compilation costs, we recommend keeping tensor shapes static whenever possible. Hugging Face library’s shapes are already static for the most part with input tokens being padded appropriately, so throughout training the cache should be consistently hit. This can be checked using the debugging tools that PyTorch / XLA provides. In the example below, you can see that compilation only happened 5 times (CompileTime) whereas execution happened during each of 1220 steps (ExecuteTime):

PYTHON
>>> import torch_xla.debug.metrics as met
>>> print(met.metrics_report())
Metric: CompileTime
  TotalSamples: 5
  Accumulator: 28s920ms153.731us
  ValueRate: 092ms152.037us / second
  Rate: 0.0165028 / second
  Percentiles: 1%=428ms053.505us; 5%=428ms053.505us; 10%=428ms053.505us; 20%=03s640ms888.060us; 50%=03s650ms126.150us; 80%=11s110ms545.595us; 90%=11s110ms545.595us; 95%=11s110ms545.595us; 99%=11s110ms545.595us
Metric: DeviceLockWait
  TotalSamples: 1281
  Accumulator: 38s195ms476.007us
  ValueRate: 151ms051.277us / second
  Rate: 4.54374 / second
  Percentiles: 1%=002.895us; 5%=002.989us; 10%=003.094us; 20%=003.243us; 50%=003.654us; 80%=038ms978.659us; 90%=192ms495.718us; 95%=208ms893.403us; 99%=221ms394.520us
Metric: ExecuteTime
  TotalSamples: 1220
  Accumulator: 04m22s555ms668.071us
  ValueRate: 923ms872.877us / second
  Rate: 4.33049 / second
  Percentiles: 1%=045ms041.018us; 5%=213ms379.757us; 10%=215ms434.912us; 20%=217ms036.764us; 50%=219ms206.894us; 80%=222ms335.146us; 90%=227ms592.924us; 95%=231ms814.500us; 99%=239ms691.472us
Counter: CachedCompile
  Value: 1215
Counter: CreateCompileHandles
  Value: 5
...

Train Your Transformer on Cloud TPUs

To configure your VM and Cloud TPUs, please follow “Set up a Compute Engine instance” and “Launch a Cloud TPU resource” (pytorch-1.7 version as of writing) sections. Once you have your VM and Cloud TPU created, using them is as simple as SSHing to your GCE VM and running the following commands to get bert-large-uncased training kicked off (batch size is for v3-8 device, may OOM on v2-8):

BASH
conda activate torch-xla-1.7
export TPU_IP_ADDRESS="ENTER_YOUR_TPU_IP_ADDRESS"  
export XRT_TPU_CONFIG="tpu_worker;0;$TPU_IP_ADDRESS:8470"
git clone -b v4.2.2 https://github.com/huggingface/transformers.git
cd transformers && pip install .
pip install datasets==1.2.1
python examples/xla_spawn.py \
  --num_cores 8 \
  examples/language-modeling/run_mlm.py \
  --dataset_name wikitext \
  --dataset_config_name wikitext-103-raw-v1 \
  --max_seq_length 512 \
  --pad_to_max_length \
  --logging_dir ./tensorboard-metrics \
  --cache_dir ./cache_dir \
  --do_train \
  --do_eval \
  --overwrite_output_dir \
  --output_dir language-modeling \
  --overwrite_cache \
  --tpu_metrics_debug \
  --model_name_or_path bert-large-uncased \
  --num_train_epochs 3 \
  --per_device_train_batch_size 8 \
  --per_device_eval_batch_size 8 \
  --save_steps 500000

The above should complete training in roughly less than 200 minutes with an eval perplexity of ~3.25.

Performance Benchmarking

The following table shows the performance of training bert-large-uncased on a v3-8 Cloud TPU system (containing 4 TPU v3 chips) running PyTorch / XLA. The dataset used for all benchmarking measurements is the WikiText103 dataset, and we use the run_mlm.py script provided in Hugging Face examples. To ensure that the workloads are not host-CPU-bound, we use the n1-standard-96 CPU configuration for these tests, but you may be able to use smaller configurations as well without impacting performance.

NameDatasetHardwareGlobal Batch SizePrecisionTraining Time (mins)
bert-large-uncasedWikiText1034 TPUv3 chips (i.e. v3-8)64FP32178.4
bert-large-uncasedWikiText1034 TPUv3 chips (i.e. v3-8)128BF16106.4

Get Started with PyTorch / XLA on TPUs

See the “Running on TPUs” section under the Hugging Face examples to get started. For a more detailed description of our APIs, check out our API_GUIDE, and for performance best practices, take a look at our TROUBLESHOOTING guide. For generic PyTorch / XLA examples, run the following Colab Notebooks we offer with free Cloud TPU access. To run directly on GCP, please see our tutorials labeled “PyTorch” on our documentation site.

Have any other questions or issues? Please open an issue or question at https://github.com/huggingface/transformers/issues or directly at https://github.com/pytorch/xla/issues.