loongson/pypi/: accelerate-0.20.3 metadata and description

Homepage Simple index

Accelerate

author The HuggingFace team
author_email sylvain@huggingface.co
classifiers
  • Development Status :: 5 - Production/Stable
  • Intended Audience :: Developers
  • Intended Audience :: Education
  • Intended Audience :: Science/Research
  • License :: OSI Approved :: Apache Software License
  • Operating System :: OS Independent
  • Programming Language :: Python :: 3
  • Programming Language :: Python :: 3.7
  • Topic :: Scientific/Engineering :: Artificial Intelligence
description_content_type text/markdown
keywords deep learning
license Apache
platform
  • UNKNOWN
provides_extras testing
requires_dist
  • numpy (>=1.17)
  • packaging (>=20.0)
  • psutil
  • pyyaml
  • torch (>=1.6.0)
  • black (~=23.1) ; extra == 'dev'
  • ruff (>=0.0.241) ; extra == 'dev'
  • hf-doc-builder (>=0.3.0) ; extra == 'dev'
  • urllib3 (<2.0.0) ; extra == 'dev'
  • pytest ; extra == 'dev'
  • pytest-xdist ; extra == 'dev'
  • pytest-subtests ; extra == 'dev'
  • parameterized ; extra == 'dev'
  • datasets ; extra == 'dev'
  • evaluate ; extra == 'dev'
  • transformers ; extra == 'dev'
  • scipy ; extra == 'dev'
  • scikit-learn ; extra == 'dev'
  • deepspeed ; extra == 'dev'
  • tqdm ; extra == 'dev'
  • rich ; extra == 'dev'
  • black (~=23.1) ; extra == 'quality'
  • ruff (>=0.0.241) ; extra == 'quality'
  • hf-doc-builder (>=0.3.0) ; extra == 'quality'
  • urllib3 (<2.0.0) ; extra == 'quality'
  • rich ; extra == 'rich'
  • sagemaker ; extra == 'sagemaker'
  • datasets ; extra == 'test_dev'
  • evaluate ; extra == 'test_dev'
  • transformers ; extra == 'test_dev'
  • scipy ; extra == 'test_dev'
  • scikit-learn ; extra == 'test_dev'
  • deepspeed ; extra == 'test_dev'
  • tqdm ; extra == 'test_dev'
  • pytest ; extra == 'test_prod'
  • pytest-xdist ; extra == 'test_prod'
  • pytest-subtests ; extra == 'test_prod'
  • parameterized ; extra == 'test_prod'
  • wandb ; extra == 'test_trackers'
  • comet-ml ; extra == 'test_trackers'
  • tensorboard ; extra == 'test_trackers'
  • pytest ; extra == 'testing'
  • pytest-xdist ; extra == 'testing'
  • pytest-subtests ; extra == 'testing'
  • parameterized ; extra == 'testing'
  • datasets ; extra == 'testing'
  • evaluate ; extra == 'testing'
  • transformers ; extra == 'testing'
  • scipy ; extra == 'testing'
  • scikit-learn ; extra == 'testing'
  • deepspeed ; extra == 'testing'
  • tqdm ; extra == 'testing'
requires_python >=3.7.0

Because this project isn't in the mirror_whitelist, no releases from root/pypi are included.

File Tox results History
accelerate-0.20.3-py3-none-any.whl
Size
222 KB
Type
Python Wheel
Python
3



License Documentation GitHub release Contributor Covenant

Run your *raw* PyTorch training script on any kind of device

Easy to integrate

🤗 Accelerate was created for PyTorch users who like to write the training loop of PyTorch models but are reluctant to write and maintain the boilerplate code needed to use multi-GPUs/TPU/fp16.

🤗 Accelerate abstracts exactly and only the boilerplate code related to multi-GPUs/TPU/fp16 and leaves the rest of your code unchanged.

Here is an example:

  import torch
  import torch.nn.functional as F
  from datasets import load_dataset
+ from accelerate import Accelerator

+ accelerator = Accelerator()
- device = 'cpu'
+ device = accelerator.device

  model = torch.nn.Transformer().to(device)
  optimizer = torch.optim.Adam(model.parameters())

  dataset = load_dataset('my_dataset')
  data = torch.utils.data.DataLoader(dataset, shuffle=True)

+ model, optimizer, data = accelerator.prepare(model, optimizer, data)

  model.train()
  for epoch in range(10):
      for source, targets in data:
          source = source.to(device)
          targets = targets.to(device)

          optimizer.zero_grad()

          output = model(source)
          loss = F.cross_entropy(output, targets)

-         loss.backward()
+         accelerator.backward(loss)

          optimizer.step()

As you can see in this example, by adding 5-lines to any standard PyTorch training script you can now run on any kind of single or distributed node setting (single CPU, single GPU, multi-GPUs and TPUs) as well as with or without mixed precision (fp16).

In particular, the same code can then be run without modification on your local machine for debugging or your training environment.

🤗 Accelerate even handles the device placement for you (which requires a few more changes to your code, but is safer in general), so you can even simplify your training loop further:

  import torch
  import torch.nn.functional as F
  from datasets import load_dataset
+ from accelerate import Accelerator

- device = 'cpu'
+ accelerator = Accelerator()

- model = torch.nn.Transformer().to(device)
+ model = torch.nn.Transformer()
  optimizer = torch.optim.Adam(model.parameters())

  dataset = load_dataset('my_dataset')
  data = torch.utils.data.DataLoader(dataset, shuffle=True)

+ model, optimizer, data = accelerator.prepare(model, optimizer, data)

  model.train()
  for epoch in range(10):
      for source, targets in data:
-         source = source.to(device)
-         targets = targets.to(device)

          optimizer.zero_grad()

          output = model(source)
          loss = F.cross_entropy(output, targets)

-         loss.backward()
+         accelerator.backward(loss)

          optimizer.step()

Want to learn more? Check out the documentation or have look at our examples.

Launching script

🤗 Accelerate also provides an optional CLI tool that allows you to quickly configure and test your training environment before launching the scripts. No need to remember how to use torch.distributed.run or to write a specific launcher for TPU training! On your machine(s) just run:

accelerate config

and answer the questions asked. This will generate a config file that will be used automatically to properly set the default options when doing

accelerate launch my_script.py --args_to_my_script

For instance, here is how you would run the GLUE example on the MRPC task (from the root of the repo):

accelerate launch examples/nlp_example.py

This CLI tool is optional, and you can still use python my_script.py or python -m torchrun my_script.py at your convenance.

Launching multi-CPU run using MPI

🤗 Here is another way to launch multi-CPU run using MPI. You can learn how to install Open MPI on this page. You can use Intel MPI or MVAPICH as well. Once you have MPI setup on your cluster, just run:

mpirun -np 2 python examples/nlp_example.py

Launching training using DeepSpeed

🤗 Accelerate supports training on single/multiple GPUs using DeepSpeed. To use it, you don't need to change anything in your training code; you can set everything using just accelerate config. However, if you desire to tweak your DeepSpeed related args from your python script, we provide you the DeepSpeedPlugin.

from accelerate import Accelerator, DeepSpeedPlugin

# deepspeed needs to know your gradient accumulation steps before hand, so don't forget to pass it
# Remember you still need to do gradient accumulation by yourself, just like you would have done without deepspeed
deepspeed_plugin = DeepSpeedPlugin(zero_stage=2, gradient_accumulation_steps=2)
accelerator = Accelerator(mixed_precision='fp16', deepspeed_plugin=deepspeed_plugin)

# How to save your 🤗 Transformer?
accelerator.wait_for_everyone()
unwrapped_model = accelerator.unwrap_model(model)
unwrapped_model.save_pretrained(save_dir, save_function=accelerator.save, state_dict=accelerator.get_state_dict(model))

Note: DeepSpeed support is experimental for now. In case you get into some problem, please open an issue.

Launching your training from a notebook

🤗 Accelerate also provides a notebook_launcher function you can use in a notebook to launch a distributed training. This is especially useful for Colab or Kaggle notebooks with a TPU backend. Just define your training loop in a training_function then in your last cell, add:

from accelerate import notebook_launcher

notebook_launcher(training_function)

An example can be found in this notebook. Open In Colab

Why should I use 🤗 Accelerate?

You should use 🤗 Accelerate when you want to easily run your training scripts in a distributed environment without having to renounce full control over your training loop. This is not a high-level framework above PyTorch, just a thin wrapper so you don't have to learn a new library, In fact the whole API of 🤗 Accelerate is in one class, the Accelerator object.

Why shouldn't I use 🤗 Accelerate?

You shouldn't use 🤗 Accelerate if you don't want to write a training loop yourself. There are plenty of high-level libraries above PyTorch that will offer you that, 🤗 Accelerate is not one of them.

Frameworks using 🤗 Accelerate

If you like the simplicity of 🤗 Accelerate but would prefer a higher-level abstraction around its capabilities, some frameworks and libraries that are built on top of 🤗 Accelerate are listed below:

Installation

This repository is tested on Python 3.7+ and PyTorch 1.4.0+

You should install 🤗 Accelerate in a virtual environment. If you're unfamiliar with Python virtual environments, check out the user guide.

First, create a virtual environment with the version of Python you're going to use and activate it.

Then, you will need to install PyTorch: refer to the official installation page regarding the specific install command for your platform. Then 🤗 Accelerate can be installed using pip as follows:

pip install accelerate

Supported integrations

Citing 🤗 Accelerate

If you use 🤗 Accelerate in your publication, please cite it by using the following BibTeX entry.

@Misc{accelerate,
  title =        {Accelerate: Training and inference at scale made simple, efficient and adaptable.},
  author =       {Sylvain Gugger, Lysandre Debut, Thomas Wolf, Philipp Schmid, Zachary Mueller, Sourab Mangrulkar},
  howpublished = {\url{https://github.com/huggingface/accelerate}},
  year =         {2022}
}