---
title: PyTorch
description: Learn how to train machine learning models on single nodes using PyTorch.
canonical_url: https://docs.databricks.com/aws/en/machine-learning/train-model/pytorch
---

# PyTorch

[PyTorch project](https://github.com/pytorch) is a Python package that provides GPU accelerated tensor computation and
high level functionalities for building deep learning networks.
For licensing details, see the PyTorch [license doc on GitHub](https://github.com/pytorch/pytorch/blob/a90c259edad1ea4fa1b8773e3cb37240df680d62/LICENSE).

To monitor and debug your PyTorch models, consider using [TensorBoard](https://docs.databricks.com/aws/en/machine-learning/train-model/tensorboard.md).

PyTorch is included in Databricks Runtime for Machine Learning. If you are using Databricks Runtime, see [Install PyTorch](https://docs.databricks.com/aws/en/machine-learning/train-model/pytorch.md#install-pytorch) for instructions on installing PyTorch.

> [!NOTE]
>
> This is not a comprehensive guide to PyTorch. For more information, see the [PyTorch website](https://pytorch.org/).

## Single node and distributed training

To test and migrate single-machine workflows, use a [Single Node cluster](https://docs.databricks.com/aws/en/compute/configure.md#single-node).

For distributed training options for deep learning, see [Distributed training](https://docs.databricks.com/aws/en/machine-learning/train-model/distributed-training/index.md).

## Example notebooks

#### MLflow PyTorch end-to-end model training notebook

[Get notebook](https://docs.databricks.com/notebooks/source/mlflow/mlflow-deep-learning-e2e.html)

#### PyTorch notebook

[Get notebook](https://docs.databricks.com/notebooks/source/deep-learning/pytorch-single-node.html)

#### MLflow PyTorch model training notebook with TensorFlow

[Get notebook](https://docs.databricks.com/notebooks/source/mlflow/mlflow-pytorch-training.html)

## Install PyTorch

### Databricks Runtime for ML

[Databricks Runtime for Machine Learning](https://docs.databricks.com/aws/en/machine-learning/index.md) includes PyTorch so you can create the cluster and start using PyTorch. For the version of PyTorch installed in the Databricks Runtime ML version you are using, see the [release notes](https://docs.databricks.com/aws/en/release-notes/runtime/index.md).

### Databricks Runtime

Databricks recommends that you use the PyTorch included in Databricks Runtime for Machine Learning. However, if you must use [the standard Databricks Runtime](https://docs.databricks.com/aws/en/release-notes/runtime/index.md), PyTorch can be installed as a [Databricks PyPI library](https://docs.databricks.com/aws/en/libraries/index.md). The following example shows how to install PyTorch 1.5.0:

* On GPU clusters, install `pytorch` and `torchvision` by specifying the following:
  * `torch==1.5.0`
  * `torchvision==0.6.0`
* On CPU clusters, install `pytorch` and `torchvision` by using the following Python wheel files:

  ```
  https://download.pytorch.org/whl/cpu/torch-1.5.0%2Bcpu-cp37-cp37m-linux_x86_64.whl

  https://download.pytorch.org/whl/cpu/torchvision-0.6.0%2Bcpu-cp37-cp37m-linux_x86_64.whl
  ```

## <a id="torch-distributed-troubleshooting" />Errors and troubleshooting for distributed PyTorch

The following sections describe common error messages and troubleshooting guidance for the classes: [PyTorch DataParallel](https://pytorch.org/docs/stable/generated/torch.nn.DataParallel.html) or [PyTorch DistributedDataParallel](https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html#torch.nn.parallel.DistributedDataParallel). Most of these errors can likely be resolved with [TorchDistributor](https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.ml.torch.distributor.TorchDistributor.html), which is available on Databricks Runtime ML 13.0 and above. However, if `TorchDistributor` is not a viable solution, recommended solutions are also provided within each section.

The following is an example of how to use TorchDistributor:

```python

from pyspark.ml.torch.distributor import TorchDistributor

def train_fn(learning_rate):
        # ...

num_processes=2
distributor = TorchDistributor(num_processes=num_processes, local_mode=True)

distributor.run(train_fn, 1e-3)
```

### <a id="process-terminated" />process 0 terminated with exit code 1

The following error can occur when using notebooks in Databricks or locally:

```
process 0 terminated with exit code 1
```

To avoid this error, use `torch.multiprocessing.start_processes` with `start_method=fork` instead of `torch.multiprocessing.spawn`.

For example:

```python
import torch

def train_fn(rank, learning_rate):
    # required setup, e.g. setup(rank)
        # ...

num_processes = 2
torch.multiprocessing.start_processes(train_fn, args=(1e-3,), nprocs=num_processes, start_method="fork")
```

### <a id="server-socket-error" />The server socket has failed to bind to port

The following error appears when you restart the distributed training after interrupting the cell during training:

```
The server socket has failed to bind to [::]:{PORT NUMBER} (errno: 98 - Address already in use).
```

To fix the issue, restart the cluster. If restarting does not solve the problem, there might be an error in the training function code.

### CUDA related errors

You can run into additional issues with CUDA since `start_method=”fork”` is [not CUDA-compatible](https://github.com/pytorch/pytorch/blob/master/torch/multiprocessing/spawn.py#L173). Using any `.cuda` commands in any cell might lead to failures. To avoid these errors, add the following check before you call `torch.multiprocessing.start_method`:

```python
if torch.cuda.is_initialized():
    raise Exception("CUDA was initialized; distributed training will fail.") # or something similar
```
