Skip to content

Commit

Permalink
Update tpu tp share same logic with ttp
Browse files Browse the repository at this point in the history
  • Loading branch information
four4fish committed Nov 30, 2021
1 parent 38ed26e commit fdbbc08
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 20 deletions.
11 changes: 0 additions & 11 deletions pytorch_lightning/plugins/training_type/single_tpu.py
Expand Up @@ -14,15 +14,12 @@
import os
from typing import Any, Dict, Optional

import torch

import pytorch_lightning as pl
from pytorch_lightning.plugins.io.checkpoint_plugin import CheckpointIO
from pytorch_lightning.plugins.io.xla_plugin import XLACheckpointIO
from pytorch_lightning.plugins.precision import PrecisionPlugin
from pytorch_lightning.plugins.training_type.single_device import SingleDevicePlugin
from pytorch_lightning.utilities import _TPU_AVAILABLE, find_shared_parameters, set_shared_parameters
from pytorch_lightning.utilities.apply_func import apply_to_collection, move_data_to_device
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities.model_helpers import is_overridden
from pytorch_lightning.utilities.types import _PATH
Expand Down Expand Up @@ -66,14 +63,6 @@ def setup(self, trainer: "pl.Trainer") -> None:
self.setup_optimizers(trainer)
self.setup_precision_plugin()

def _move_optimizer_state(self, device: Optional[torch.device] = None) -> None:
"""Moves the state of the optimizers to the TPU if needed."""
# TODO: `self.root_device` would raise error if called outside the spawn process
# while training on 8 and more cores.
for opt in self.optimizers:
for p, v in opt.state.items():
opt.state[p] = apply_to_collection(v, torch.Tensor, move_data_to_device, self.root_device)

def model_to_device(self) -> None:
self.model.to(self.root_device)

Expand Down
10 changes: 1 addition & 9 deletions pytorch_lightning/plugins/training_type/tpu_spawn.py
Expand Up @@ -32,7 +32,7 @@
from pytorch_lightning.trainer.connectors.data_connector import DataConnector
from pytorch_lightning.trainer.states import TrainerFn
from pytorch_lightning.utilities import _TPU_AVAILABLE, find_shared_parameters, rank_zero_warn, set_shared_parameters
from pytorch_lightning.utilities.apply_func import apply_to_collection, move_data_to_device
from pytorch_lightning.utilities.apply_func import move_data_to_device
from pytorch_lightning.utilities.data import has_len
from pytorch_lightning.utilities.distributed import rank_zero_only, ReduceOp
from pytorch_lightning.utilities.exceptions import MisconfigurationException
Expand Down Expand Up @@ -127,14 +127,6 @@ def setup(self, trainer: "pl.Trainer") -> None:
self.setup_optimizers(trainer)
self.setup_precision_plugin()

def _move_optimizer_state(self, device: Optional[torch.device] = None) -> None:
"""Moves the state of the optimizers to the TPU if needed."""
# TODO: `self.root_device` would raise error if called outside the spawn process
# while training on 8 and more cores.
for opt in self.optimizers:
for p, v in opt.state.items():
opt.state[p] = apply_to_collection(v, torch.Tensor, move_data_to_device, self.root_device)

def _setup_model(self, model: Module) -> Module:
return model

Expand Down

0 comments on commit fdbbc08

Please sign in to comment.