2020-02-11 04:55:22 +00:00
|
|
|
16-bit training
|
|
|
|
=================
|
2020-02-17 21:01:20 +00:00
|
|
|
Lightning offers 16-bit training for CPUs, GPUs and TPUs.
|
|
|
|
|
|
|
|
GPU 16-bit
|
|
|
|
-----------
|
2020-02-11 04:55:22 +00:00
|
|
|
Lightning uses NVIDIA apex to handle 16-bit precision training.
|
|
|
|
|
|
|
|
To use 16-bit precision, do two things:
|
2020-02-17 21:01:20 +00:00
|
|
|
|
2020-02-11 04:55:22 +00:00
|
|
|
1. Install Apex
|
2020-02-17 21:01:20 +00:00
|
|
|
2. Set the "precision" trainer flag.
|
2020-02-11 04:55:22 +00:00
|
|
|
|
|
|
|
Install apex
|
2020-02-17 21:01:20 +00:00
|
|
|
^^^^^^^^^^^^
|
2020-02-11 04:55:22 +00:00
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
$ git clone https://github.com/NVIDIA/apex
|
|
|
|
$ cd apex
|
|
|
|
|
|
|
|
# ------------------------
|
|
|
|
# OPTIONAL: on your cluster you might need to load cuda 10 or 9
|
|
|
|
# depending on how you installed PyTorch
|
|
|
|
|
|
|
|
# see available modules
|
|
|
|
module avail
|
|
|
|
|
|
|
|
# load correct cuda before install
|
|
|
|
module load cuda-10.0
|
|
|
|
# ------------------------
|
|
|
|
|
|
|
|
# make sure you've loaded a cuda version > 4.0 and < 7.0
|
|
|
|
module load gcc-6.1.0
|
|
|
|
|
|
|
|
$ pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" ./
|
|
|
|
|
|
|
|
|
|
|
|
Enable 16-bit
|
2020-02-17 21:01:20 +00:00
|
|
|
^^^^^^^^^^^^^
|
2020-02-11 04:55:22 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2020-02-17 21:01:20 +00:00
|
|
|
# turn on 16-bit
|
|
|
|
trainer = Trainer(amp_level='O1', precision=16)
|
2020-02-11 04:55:22 +00:00
|
|
|
|
|
|
|
If you need to configure the apex init for your particular use case or want to use a different way of doing
|
2020-02-17 21:01:20 +00:00
|
|
|
16-bit training, override :meth:`pytorch_lightning.core.LightningModule.configure_apex`.
|
|
|
|
|
|
|
|
TPU 16-bit
|
|
|
|
----------
|
|
|
|
16-bit on TPus is much simpler. To use 16-bit with TPUs set precision to 16 when using the tpu flag
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# DEFAULT
|
|
|
|
trainer = Trainer(num_tpu_cores=8, precision=32)
|
|
|
|
|
|
|
|
# turn on 16-bit
|
|
|
|
trainer = Trainer(num_tpu_cores=8, precision=16)
|