| 1 |
<!--Copyright 2024 The HuggingFace Team. All rights reserved. |
| 2 |
|
| 3 |
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 |
the License. You may obtain a copy of the License at |
| 5 |
|
| 6 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
|
| 8 |
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 |
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 |
specific language governing permissions and limitations under the License. |
| 11 |
|
| 12 |
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be |
| 13 |
rendered properly in your Markdown viewer. |
| 14 |
--> |
| 15 |
|
| 16 |
# TPU training |
| 17 |
|
| 18 |
A [TPU (Tensor Processing Unit)](https://cloud.google.com/tpu/docs/intro-to-tpu) is a type of hardware specifically designed for training models efficiently. Accelerate supports TPU training, but there are a few things you should be aware of, namely graph compilation. This tutorial briefly discusses compilation, and for more details, take a look at the [Training on TPUs with Accelerate](../concept_guides/training_tpu) guide. |
| 19 |
|
| 20 |
## Compilation |
| 21 |
|
| 22 |
A TPU creates a graph of all the operations in the training step such as the forward pass, backward pass and optimizer step. This is why the first training step always takes a while because building and compiling this graph takes time. But once compilation is complete, it is cached and all subsequent steps are much faster. |
| 23 |
|
| 24 |
The key is to avoid compiling your code again or else training is super slow. This means all your operations must be exactly the same: |
| 25 |
|
| 26 |
* all tensors in your batches must have the same length (for example, no dynamic padding for NLP tasks) |
| 27 |
* your code must be static (for example, no layers with for loops that have different lengths depending on the input such as a LSTM) |
| 28 |
|
| 29 |
## Weight tying |
| 30 |
|
| 31 |
A common language model design is to tie the weights of the embedding and softmax layers. However, moving the model to a TPU (either yourself or passing it to the [`~Accelerator.prepare`] method) breaks the weight tying and you'll need to retie the weights. |
| 32 |
|
| 33 |
To add special behavior (like weight tying) in your script for TPUs, set [`~Accelerator.distributed_type`] to `DistributedType.TPU` first. Then you can use the [`~transformers.PreTrainedModel.tie_weights`] method to tie the weights. |
| 34 |
|
| 35 |
```py |
| 36 |
if accelerator.distributed_type == DistributedType.TPU: |
| 37 |
model.tie_weights() |
| 38 |
``` |