forked from mosaicml/composer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial support for neuron devices (mosaicml#3049)
* add initial support for neuron devices * add missing device_neuron.py * remove unused imports * update neuron flops * formatting * formatting * formatting * documentation * formatting
- Loading branch information
Showing
8 changed files
with
129 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Copyright 2024 MosaicML Composer authors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
"""The Neuron device used for training.""" | ||
|
||
from __future__ import annotations | ||
|
||
import logging | ||
import os | ||
from typing import Any, Dict, TypeVar | ||
|
||
import torch | ||
|
||
from composer.devices.device import Device | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
__all__ = ['DeviceNeuron'] | ||
|
||
T_nnModule = TypeVar('T_nnModule', bound=torch.nn.Module) | ||
|
||
|
||
class DeviceNeuron(Device): | ||
"""An extension of :class:`~composer.devices.device.Device` for Neuron devices (Trn, Inf). | ||
When running on Trn, we automatically set `export PJRT_DEVICE=NEURON`. | ||
""" | ||
|
||
name = 'neuron' | ||
dist_backend = 'xla' | ||
|
||
def __init__(self): | ||
import torch_xla.core.xla_model as xm | ||
|
||
# Turn off compiler based mixed precision (we use torch's amp support) | ||
# https://awsdocs-neuron.readthedocs-hosted.com/en/latest/general/appnotes/neuronx-cc/neuronx-cc-training-mixed-precision.html | ||
os.environ['NEURON_CC_FLAGS'] = '--auto-cast=none' | ||
os.environ['PJRT_DEVICE'] = 'NEURON' | ||
self._device = xm.xla_device() | ||
|
||
def module_to_device(self, module: T_nnModule) -> T_nnModule: | ||
return module.to(self._device) | ||
|
||
def tensor_to_device(self, tensor: torch.Tensor) -> torch.Tensor: | ||
return tensor.to(self._device) | ||
|
||
def state_dict(self) -> Dict[str, Any]: | ||
return {} | ||
|
||
def load_state_dict(self, state: Dict[str, Any]) -> None: | ||
if len(state) != 0: | ||
raise ValueError('Neuron device has no state.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters