mirror of
https://github.com/huggingface/accelerate.git
synced 2025-11-17 08:01:15 +08:00
Compare commits
375 Commits
v0.17.1
...
check-docs
| Author | SHA1 | Date | |
|---|---|---|---|
| 97639da449 | |||
| 658492fb41 | |||
| 80da9cfb09 | |||
| 03deec2a01 | |||
| 629d02c844 | |||
| a87c95da9e | |||
| bbcdbbaffc | |||
| ce53708e0e | |||
| 53209ce6d8 | |||
| bd083ae1bf | |||
| e5452a618d | |||
| 40a73e0ae0 | |||
| 937e08ce75 | |||
| 5d558f21e2 | |||
| d9b5ce60b3 | |||
| 61a87ab946 | |||
| 5dec654aae | |||
| b2a950205e | |||
| ca7b853abc | |||
| 6832aa51a6 | |||
| 4a1d5b1fb6 | |||
| 82369c8314 | |||
| cdb001ca5f | |||
| c72e22419b | |||
| c872c3086f | |||
| cec5ae8e4d | |||
| cd570b2e2a | |||
| 727d624322 | |||
| afed2f75f8 | |||
| 739b135f83 | |||
| 4a9dd1cd82 | |||
| feab09908d | |||
| e0baaa8df0 | |||
| 1b998f1695 | |||
| 7befe580c2 | |||
| cd3d3a37f9 | |||
| 81fffe51fd | |||
| 0b5ac0253e | |||
| a16b843a1b | |||
| bc86a9379f | |||
| 87a096f95e | |||
| 44adf1e14f | |||
| ce870e1ce1 | |||
| 1ace672d3e | |||
| e2ae254008 | |||
| 0fa291e707 | |||
| ba6f11ec3e | |||
| 430ee9df6b | |||
| 409a9df0a4 | |||
| acad5bae5c | |||
| 81b19c4094 | |||
| 3e97a9172b | |||
| 812719644d | |||
| 16e5113f8a | |||
| 3122a6164d | |||
| c8682ae74c | |||
| 0768905f77 | |||
| d087be0156 | |||
| 41caaa56e1 | |||
| 21d127334e | |||
| 3cf7dee576 | |||
| 64c586f5eb | |||
| 0e714f5ba4 | |||
| 92f23e123d | |||
| f67e11afd7 | |||
| 6458058559 | |||
| 4d13e4e474 | |||
| 058a3546ea | |||
| 98ecab2083 | |||
| b30a349078 | |||
| 7cb19ae613 | |||
| 39897a0662 | |||
| aa71bb815a | |||
| f43a08a9c5 | |||
| b42c65b729 | |||
| 7bad726935 | |||
| 29ff7c3911 | |||
| 30eff605df | |||
| fc95663e03 | |||
| 49cb83a423 | |||
| d2b159ea1a | |||
| 40056c69d1 | |||
| 505b5be044 | |||
| a6333f2e7c | |||
| 0dec477985 | |||
| a24189db35 | |||
| a9aee447ee | |||
| d5894ab499 | |||
| 6f14928e28 | |||
| 777334a803 | |||
| c3d82d24e2 | |||
| 6e70e79e4e | |||
| b3fc3c9067 | |||
| a9d79163e5 | |||
| 0b36ca6e64 | |||
| f3b7f9cf25 | |||
| b909bfacb9 | |||
| a2d8f540c3 | |||
| e8ed10ae62 | |||
| a6291e43b0 | |||
| 2a289f6108 | |||
| cafc7f785f | |||
| 39889c7304 | |||
| 12d5a2d0da | |||
| 243288627d | |||
| efc1fa8376 | |||
| 18e3012489 | |||
| daa1952f47 | |||
| 653ba110d3 | |||
| f518b0ab03 | |||
| 3a05e0cf70 | |||
| 299f3ef8ab | |||
| 925a13eb04 | |||
| 4170f395d1 | |||
| bb47344c77 | |||
| 243cd82409 | |||
| 51f5e829a8 | |||
| 5b9c5881b6 | |||
| 0209606364 | |||
| 5909c1a514 | |||
| e7150b0b15 | |||
| e8c64f598b | |||
| a14081ccc5 | |||
| d895809613 | |||
| 02015eb25c | |||
| 19bcd43e14 | |||
| 59f2fff3cf | |||
| c33adecc9f | |||
| 518c206a2a | |||
| 65b5c2cfad | |||
| 7954a28a71 | |||
| 3bdb35abfa | |||
| d58aac2e1e | |||
| a4c2654f50 | |||
| 27d29087b2 | |||
| c7698834fc | |||
| 64d7b58c44 | |||
| e3aae2ac65 | |||
| d0a7991b65 | |||
| 180ef7c415 | |||
| 95bffdec43 | |||
| c74c28c6d1 | |||
| e0f5e03009 | |||
| dfbfbdfea8 | |||
| 24ae624d96 | |||
| 40f822a1e3 | |||
| a0bfe2140c | |||
| c6443f8bd4 | |||
| 3cd02e9340 | |||
| 17ec2ede11 | |||
| e30938700a | |||
| b864946606 | |||
| bc234c040c | |||
| 662a7dd905 | |||
| d3db2d4fe5 | |||
| 96f926a25e | |||
| a9d43cda80 | |||
| effccbdc84 | |||
| d141b4ce79 | |||
| bc49d0f9b3 | |||
| 5ea7c81277 | |||
| efe4481a28 | |||
| df215cc243 | |||
| 5791d949ff | |||
| b76409ba05 | |||
| a25c4eacae | |||
| d8437ae096 | |||
| 2fa22f3342 | |||
| a2ecb58132 | |||
| 73cc944067 | |||
| b16916f447 | |||
| 36f8e48747 | |||
| 790cb8b461 | |||
| 7b4d12623a | |||
| 956c6baf71 | |||
| 485e8c8cb4 | |||
| aaf38c2f35 | |||
| f433457244 | |||
| 535b52cef2 | |||
| e60a424398 | |||
| 32f85ce524 | |||
| 0983a9b9b4 | |||
| e5d0df44f0 | |||
| 50eabe5b1d | |||
| f2d1047059 | |||
| 3e68f1da63 | |||
| f8b0696076 | |||
| 51a2ca5d88 | |||
| 51de46e368 | |||
| e2b0224ec4 | |||
| db11bd5035 | |||
| 543c59af22 | |||
| 81765e6e00 | |||
| a4ebc14fab | |||
| 058f6f70f5 | |||
| 665d5180fc | |||
| d1ea9ab40c | |||
| 632dce67ab | |||
| e41864ce9d | |||
| 979991aa78 | |||
| 7fc1e438d1 | |||
| 040f178569 | |||
| 87c81315a1 | |||
| f1e84decc9 | |||
| eafddf02e3 | |||
| f0029d6f60 | |||
| 3147de9010 | |||
| d448ebaf90 | |||
| 65dd4f2039 | |||
| 7ee2c79da9 | |||
| bbe2e30901 | |||
| 0ab72613a7 | |||
| 6f14e619b2 | |||
| 90e9703d99 | |||
| 5f21cde3c7 | |||
| 76ccfae682 | |||
| 62357f218f | |||
| be1b76e97a | |||
| 3f2b5da094 | |||
| 3f1cb09e7b | |||
| 7a39d928f5 | |||
| 961fe728d9 | |||
| ef0c4bf277 | |||
| de855b3247 | |||
| b9628f13c2 | |||
| 16ca01feea | |||
| 4cbbde8945 | |||
| eba6eb79dc | |||
| 109f3272f5 | |||
| 85901cdcf9 | |||
| 5e74d932b9 | |||
| 090c65cd9d | |||
| b7d5d9072a | |||
| d4262021d5 | |||
| 8ae56dc51d | |||
| c9fbb71e37 | |||
| 4d583ad6a1 | |||
| 70d999ee4a | |||
| 3913fa4dd0 | |||
| f9b2e6769b | |||
| d3f8c52f4c | |||
| af12e7b023 | |||
| 68376babd8 | |||
| 7d24bdefb5 | |||
| bb296348e1 | |||
| 0226f75025 | |||
| 419c9ce22a | |||
| 2249fbde0d | |||
| e0ffea5bc3 | |||
| 9a86a49f72 | |||
| 70920895e8 | |||
| bf3cd30a66 | |||
| bfa74e51d2 | |||
| e6699e6aba | |||
| 0871e93a74 | |||
| 86720fdb11 | |||
| 1deab71e3c | |||
| 5d1cee3d81 | |||
| 5904f56c45 | |||
| 99d790dc34 | |||
| 1760d2dc8c | |||
| b93bfac16d | |||
| 981c6fb8d6 | |||
| 6413f25ba9 | |||
| 39e20d3e55 | |||
| 3a381bfa48 | |||
| bc82d18821 | |||
| 330d60b817 | |||
| 612ecef7b8 | |||
| 9493d7276b | |||
| 40c6e0ca41 | |||
| a28491bc24 | |||
| 435079aafb | |||
| dcde1e93d0 | |||
| ab379793d4 | |||
| b50e75f85d | |||
| f95067bfbf | |||
| d07fd959cc | |||
| 873b39b85b | |||
| da39665055 | |||
| d95d68ec46 | |||
| fafadc5323 | |||
| 145fca5a09 | |||
| 9fe690706d | |||
| 6e81938282 | |||
| e965d590cd | |||
| 6dfcf5b8ef | |||
| e4ea4ed4de | |||
| fa8e1cff91 | |||
| 60856787ac | |||
| 995563fec9 | |||
| 2d62bd1570 | |||
| f8169eaded | |||
| 75ab711993 | |||
| f489a86573 | |||
| 2708c1ae31 | |||
| e30034ed07 | |||
| 78bf8bcb21 | |||
| 57f2cf5fa7 | |||
| e06e7b35e7 | |||
| 5651521833 | |||
| ba0ee8a54d | |||
| c2a162932a | |||
| c29c3c5e70 | |||
| 945085edb3 | |||
| 70388fa44e | |||
| 2fee0c15fd | |||
| c05ed13fc9 | |||
| 5e6351502a | |||
| ee0c587182 | |||
| 43e7229a1a | |||
| 8b96515ed2 | |||
| 9d9ea62785 | |||
| 2106e87d58 | |||
| 40980e8fe8 | |||
| f2f810c536 | |||
| 0a9403f308 | |||
| 75a693c9b4 | |||
| 55691b14c2 | |||
| b757b62325 | |||
| 15dbf9722b | |||
| 419ecf38af | |||
| 3cb9d5fd9c | |||
| f1298b143e | |||
| 07ad358f2d | |||
| 211707857d | |||
| e57d5d0eae | |||
| 92d072043e | |||
| 3d1a0f7e98 | |||
| 8b3e30887a | |||
| 3e304c4a1a | |||
| 1c102f23cc | |||
| 4c0d5a46ba | |||
| d0c17d707f | |||
| b41d8d8228 | |||
| 3a6db664c7 | |||
| 166520feea | |||
| 663f5120c2 | |||
| 23ac55fcab | |||
| 93951ce516 | |||
| ae86a00be0 | |||
| 532da3e342 | |||
| a826e4441d | |||
| 1fe27e7c95 | |||
| c1a6c209df | |||
| 8ebd6ab2ee | |||
| ea9b85477d | |||
| 420ff21c3b | |||
| b1b3312749 | |||
| 6e4e870203 | |||
| a3065e1842 | |||
| 4eaf36e1c4 | |||
| e7bb060c0e | |||
| a15d307426 | |||
| 7e7f3445aa | |||
| 10c674633d | |||
| 82c2665cd6 | |||
| 2930cac698 | |||
| 901ab69a16 | |||
| 780e4aa32a | |||
| e4620984f8 | |||
| 017a98c0e9 | |||
| d1aa558119 | |||
| 41479fe483 | |||
| eac5d13c7b | |||
| b228136cae | |||
| 90deb748c6 | |||
| d942708745 | |||
| 3783180844 | |||
| ea836f3057 | |||
| a4c9476204 | |||
| 3ca8c9a997 | |||
| 2f83b1afef | |||
| b0591c665c | |||
| d9871c0f87 |
@ -15,10 +15,14 @@
|
||||
"remoteEnv": {
|
||||
"PYTHONPATH": "${containerEnv:PATH}:${containerWorkspaceFolder}"
|
||||
},
|
||||
"extensions": [
|
||||
// Ensure we have IntelliSense in VSCode when running inside container
|
||||
"ms-python.python"
|
||||
],
|
||||
"customizations": {
|
||||
"vscode": {
|
||||
"extensions": [
|
||||
// Ensure we have IntelliSense in VSCode when running inside container
|
||||
"ms-python.python"
|
||||
]
|
||||
}
|
||||
},
|
||||
"workspaceFolder": "/workspaces/accelerate",
|
||||
// Need git for VSCode to color code modifications. Only runs when building environment.
|
||||
"onCreateCommand": "apt-get update && apt-get install -y git && pip install -e '.[dev]'"
|
||||
|
||||
7
.github/ISSUE_TEMPLATE/bug-report.yml
vendored
7
.github/ISSUE_TEMPLATE/bug-report.yml
vendored
@ -1,6 +1,12 @@
|
||||
name: "\U0001F41B Bug Report"
|
||||
description: Submit a bug report to help us improve Accelerate
|
||||
body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
Thanks for taking the time to submit a bug report! 🐛
|
||||
If this is not a bug related to the Accelerate library directly, but instead a general question about your code or the library specifically please use the [forums](https://discuss.huggingface.co/c/accelerate/18).
|
||||
|
||||
- type: textarea
|
||||
id: system-info
|
||||
attributes:
|
||||
@ -55,4 +61,3 @@ body:
|
||||
attributes:
|
||||
label: Expected behavior
|
||||
description: "A clear and concise description of what you would expect to happen."
|
||||
render: Shell
|
||||
|
||||
47
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
47
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
# What does this PR do?
|
||||
|
||||
<!--
|
||||
Congratulations! You've made it this far! You're not quite done yet though.
|
||||
|
||||
Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.
|
||||
|
||||
Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.
|
||||
|
||||
Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
|
||||
-->
|
||||
|
||||
<!-- Remove if not applicable -->
|
||||
|
||||
Fixes # (issue)
|
||||
|
||||
|
||||
## Before submitting
|
||||
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
|
||||
- [ ] Did you read the [contributor guideline](https://github.com/huggingface/accelerate/blob/main/CONTRIBUTING.md#submitting-a-pull-request-pr),
|
||||
Pull Request section?
|
||||
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
|
||||
to it if that's the case.
|
||||
- [ ] Did you make sure to update the documentation with your changes? Here are the
|
||||
[documentation guidelines](https://github.com/huggingface/accelerate/tree/main/docs), and
|
||||
[here are tips on formatting docstrings](https://github.com/huggingface/accelerate/tree/main/docs#writing-documentation---specification).
|
||||
- [ ] Did you write any new necessary tests?
|
||||
|
||||
|
||||
## Who can review?
|
||||
|
||||
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
|
||||
members/contributors who may be interested in your PR.
|
||||
|
||||
<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @
|
||||
|
||||
If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
|
||||
|
||||
- Big modeling: @SunMarc
|
||||
- Fully-Sharded Data Parallism: @pacman100
|
||||
- DeepSpeed: @pacman100
|
||||
- Command Line Interface: @muellerzr
|
||||
- Documentation: @muellerzr
|
||||
- Core parts of the library: @muellerzr @BenjaminBossan
|
||||
- Maintained examples: @muellerzr or @pacman100
|
||||
|
||||
-->
|
||||
@ -21,44 +21,40 @@ jobs:
|
||||
|
||||
version-cpu:
|
||||
name: "Latest Accelerate CPU [version]"
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: [self-hosted, docker-gpu, multi-gpu]
|
||||
needs: get-version
|
||||
steps:
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v1
|
||||
- name: Check out code
|
||||
uses: actions/checkout@v2
|
||||
uses: docker/setup-buildx-action@v2
|
||||
- name: Login to DockerHub
|
||||
uses: docker/login-action@v1
|
||||
uses: docker/login-action@v2
|
||||
with:
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||
|
||||
- name: Build and Push CPU
|
||||
uses: docker/build-push-action@v2
|
||||
uses: docker/build-push-action@v4
|
||||
with:
|
||||
context: ./docker/accelerate-cpu
|
||||
file: docker/accelerate-cpu/Dockerfile
|
||||
push: true
|
||||
tags: huggingface/accelerate-cpu:${{needs.get-version.outputs.version}}
|
||||
|
||||
version-cuda:
|
||||
name: "Latest Accelerate GPU [version]"
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: [self-hosted, docker-gpu, multi-gpu]
|
||||
needs: get-version
|
||||
steps:
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v1
|
||||
- name: Check out code
|
||||
uses: actions/checkout@v2
|
||||
uses: docker/setup-buildx-action@v2
|
||||
- name: Login to DockerHub
|
||||
uses: docker/login-action@v1
|
||||
uses: docker/login-action@v2
|
||||
with:
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||
|
||||
- name: Build and Push GPU
|
||||
uses: docker/build-push-action@v2
|
||||
uses: docker/build-push-action@v4
|
||||
with:
|
||||
context: ./docker/accelerate-gpu
|
||||
file: docker/accelerate-gpu/Dockerfile
|
||||
push: true
|
||||
tags: huggingface/accelerate-gpu:${{needs.get-version.outputs.version}}
|
||||
tags: huggingface/accelerate-gpu:${{needs.get-version.outputs.version}}
|
||||
|
||||
7
.github/workflows/build_and_run_tests.yml
vendored
7
.github/workflows/build_and_run_tests.yml
vendored
@ -42,4 +42,9 @@ jobs:
|
||||
run-merge-tests:
|
||||
needs: build-docker-containers
|
||||
if: always()
|
||||
uses: ./.github/workflows/run_merge_tests.yml
|
||||
uses: ./.github/workflows/run_merge_tests.yml
|
||||
|
||||
run-integration-tests:
|
||||
needs: run-merge-tests
|
||||
if: always()
|
||||
uses: ./.github/workflows/self_hosted_integration_tests.yml
|
||||
38
.github/workflows/build_docker_images.yml
vendored
38
.github/workflows/build_docker_images.yml
vendored
@ -11,44 +11,50 @@ concurrency:
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
clean-storage:
|
||||
name: "Clean docker image storage"
|
||||
runs-on: [self-hosted, docker-gpu, multi-gpu]
|
||||
steps:
|
||||
- name: Clean storage
|
||||
run: |
|
||||
docker image prune --all -f --filter "until=48h"
|
||||
docker system prune --all -f --filter "until=48h"
|
||||
|
||||
latest-cpu:
|
||||
name: "Latest Accelerate CPU [dev]"
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: [self-hosted, docker-gpu, multi-gpu]
|
||||
needs: clean-storage
|
||||
steps:
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v1
|
||||
- name: Check out code
|
||||
uses: actions/checkout@v2
|
||||
uses: docker/setup-buildx-action@v2
|
||||
- name: Login to DockerHub
|
||||
uses: docker/login-action@v1
|
||||
uses: docker/login-action@v2
|
||||
with:
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||
|
||||
- name: Build and Push CPU
|
||||
uses: docker/build-push-action@v2
|
||||
uses: docker/build-push-action@v4
|
||||
with:
|
||||
context: ./docker/accelerate-cpu
|
||||
file: docker/accelerate-cpu/Dockerfile
|
||||
push: true
|
||||
tags: huggingface/accelerate-cpu
|
||||
|
||||
latest-cuda:
|
||||
name: "Latest Accelerate GPU [dev]"
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: [self-hosted, docker-gpu, multi-gpu]
|
||||
needs: clean-storage
|
||||
steps:
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v1
|
||||
- name: Check out code
|
||||
uses: actions/checkout@v2
|
||||
uses: docker/setup-buildx-action@v2
|
||||
- name: Login to DockerHub
|
||||
uses: docker/login-action@v1
|
||||
uses: docker/login-action@v2
|
||||
with:
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_PASSWORD }}
|
||||
|
||||
- name: Build and Push GPU
|
||||
uses: docker/build-push-action@v2
|
||||
uses: docker/build-push-action@v4
|
||||
with:
|
||||
context: ./docker/accelerate-gpu
|
||||
file: docker/accelerate-gpu/Dockerfile
|
||||
push: true
|
||||
tags: huggingface/accelerate-gpu
|
||||
tags: huggingface/accelerate-gpu
|
||||
|
||||
1
.github/workflows/build_documentation.yml
vendored
1
.github/workflows/build_documentation.yml
vendored
@ -15,3 +15,4 @@ jobs:
|
||||
package: accelerate
|
||||
secrets:
|
||||
token: ${{ secrets.HUGGINGFACE_PUSH }}
|
||||
hf_token: ${{ secrets.HF_DOC_BUILD_PUSH }}
|
||||
|
||||
13
.github/workflows/delete_doc_comment.yml
vendored
13
.github/workflows/delete_doc_comment.yml
vendored
@ -1,13 +1,14 @@
|
||||
name: Delete dev documentation
|
||||
name: Delete doc comment
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [ closed ]
|
||||
workflow_run:
|
||||
workflows: ["Delete doc comment trigger"]
|
||||
types:
|
||||
- completed
|
||||
|
||||
|
||||
jobs:
|
||||
delete:
|
||||
uses: huggingface/doc-builder/.github/workflows/delete_doc_comment.yml@main
|
||||
with:
|
||||
pr_number: ${{ github.event.number }}
|
||||
package: accelerate
|
||||
secrets:
|
||||
comment_bot_token: ${{ secrets.COMMENT_BOT_TOKEN }}
|
||||
12
.github/workflows/delete_doc_comment_trigger.yml
vendored
Normal file
12
.github/workflows/delete_doc_comment_trigger.yml
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
name: Delete doc comment trigger
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [ closed ]
|
||||
|
||||
|
||||
jobs:
|
||||
delete:
|
||||
uses: huggingface/doc-builder/.github/workflows/delete_doc_comment_trigger.yml@main
|
||||
with:
|
||||
pr_number: ${{ github.event.number }}
|
||||
64
.github/workflows/integration_tests.yml
vendored
Normal file
64
.github/workflows/integration_tests.yml
vendored
Normal file
@ -0,0 +1,64 @@
|
||||
# CI for specifically ensuring integrations work fine (`transformers` mainly)
|
||||
# Useful tips:
|
||||
# - New integrations to test should have its own job, and follow a strategy method where we check both
|
||||
# the pypi and github versions.
|
||||
# - When checking the latest release of the integration, use
|
||||
# git checkout $(git describe --tags `git rev-list --tags --max-count=1`) to get the latest release.
|
||||
|
||||
name: Integration Tests
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
paths:
|
||||
- "src/**"
|
||||
- "tests/**"
|
||||
- ".github/**"
|
||||
- "examples/**"
|
||||
- "setup.py"
|
||||
types: [opened, synchronize, reopened]
|
||||
|
||||
env:
|
||||
HF_HOME: ~/hf_cache
|
||||
|
||||
jobs:
|
||||
run-trainer-tests:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
transformers-version: [
|
||||
pypi,
|
||||
github
|
||||
]
|
||||
steps:
|
||||
- uses: actions/checkout@v3.1.0
|
||||
- name: Set up python 3.8
|
||||
uses: actions/setup-python@v3
|
||||
with:
|
||||
python-version: 3.8
|
||||
|
||||
- name: Install Accelerate from source
|
||||
run: |
|
||||
pip install --upgrade pip
|
||||
pip install -e .
|
||||
|
||||
- name: Clone and install transformers
|
||||
run: |
|
||||
cd ..
|
||||
git clone https://github.com/huggingface/transformers
|
||||
cd transformers
|
||||
if [[ ${{ matrix.transformers-version }} = pypi ]]; then
|
||||
git checkout $(git describe --tags `git rev-list --tags --max-count=1`)
|
||||
fi
|
||||
pip install .[torch,testing]
|
||||
|
||||
- name: Show installed libraries
|
||||
run: |
|
||||
pip freeze
|
||||
|
||||
- name: Run Trainer tests
|
||||
env:
|
||||
WANDB_DISABLED: true
|
||||
run: |
|
||||
cd ../transformers
|
||||
pytest -sv tests/trainer
|
||||
23
.github/workflows/nightly.yml
vendored
23
.github/workflows/nightly.yml
vendored
@ -31,13 +31,15 @@ jobs:
|
||||
git config --global --add safe.directory '*'
|
||||
git fetch && git checkout ${{ github.sha }}
|
||||
pip install -e . --no-deps
|
||||
pip install pytest-reportlog
|
||||
pip install pytest-reportlog tabulate
|
||||
|
||||
- name: Run test on GPUs
|
||||
run: |
|
||||
source activate accelerate
|
||||
make test
|
||||
|
||||
- name: Run examples on GPUs
|
||||
if: always()
|
||||
run: |
|
||||
source activate accelerate
|
||||
pip uninstall comet_ml -y
|
||||
@ -46,7 +48,7 @@ jobs:
|
||||
- name: Generate Report
|
||||
if: always()
|
||||
run: |
|
||||
pip install slack_sdk
|
||||
pip install slack_sdk tabulate
|
||||
python utils/log_reports.py >> $GITHUB_STEP_SUMMARY
|
||||
|
||||
run_all_tests_multi_gpu:
|
||||
@ -68,20 +70,23 @@ jobs:
|
||||
git config --global --add safe.directory '*'
|
||||
git fetch && git checkout ${{ github.sha }}
|
||||
pip install -e . --no-deps
|
||||
pip install pytest-reportlog
|
||||
pip install pytest-reportlog tabulate
|
||||
|
||||
- name: Run core and big modeling tests on GPUs
|
||||
run: |
|
||||
source activate accelerate
|
||||
make test_big_modeling
|
||||
make test_core
|
||||
make test_big_modeling
|
||||
make test_cli
|
||||
|
||||
- name: Run Integration tests on GPUs
|
||||
if: always()
|
||||
run: |
|
||||
source activate accelerate
|
||||
make test_integrations
|
||||
|
||||
- name: Run examples on GPUs
|
||||
if: always()
|
||||
run: |
|
||||
source activate accelerate
|
||||
pip uninstall comet_ml -y
|
||||
@ -90,5 +95,11 @@ jobs:
|
||||
- name: Generate Report
|
||||
if: always()
|
||||
run: |
|
||||
pip install slack_sdk
|
||||
python utils/log_reports.py >> $GITHUB_STEP_SUMMARY
|
||||
pip install slack_sdk tabulate
|
||||
python utils/log_reports.py >> $GITHUB_STEP_SUMMARY
|
||||
|
||||
|
||||
run-integration-tests:
|
||||
needs: [run_all_tests_single_gpu, run_all_tests_multi_gpu]
|
||||
if: always()
|
||||
uses: ./.github/workflows/self_hosted_integration_tests.yml
|
||||
11
.github/workflows/quality.yml
vendored
11
.github/workflows/quality.yml
vendored
@ -7,11 +7,16 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Set up Python 3.7
|
||||
- name: Set up Python 3.8
|
||||
uses: actions/setup-python@v3
|
||||
with:
|
||||
python-version: 3.7
|
||||
python-version: 3.8
|
||||
- name: Install Python dependencies
|
||||
run: pip install -e .[quality]
|
||||
- name: Run Quality check
|
||||
run: make quality
|
||||
run: make quality
|
||||
- name: Check if failure
|
||||
if: ${{ failure() }}
|
||||
run: |
|
||||
echo "Quality check failed. Please ensure the right dependency versions are installed with 'pip install -e .[quality]' and rerun 'make style; make quality;'" >> $GITHUB_STEP_SUMMARY
|
||||
|
||||
|
||||
18
.github/workflows/run_merge_tests.yml
vendored
18
.github/workflows/run_merge_tests.yml
vendored
@ -26,8 +26,8 @@ jobs:
|
||||
source activate accelerate
|
||||
git config --global --add safe.directory '*'
|
||||
git fetch && git checkout ${{ github.sha }}
|
||||
pip install -e .[testing,test_trackers]
|
||||
pip install pytest-reportlog
|
||||
pip install -e .[testing,test_trackers] -U
|
||||
pip install pytest-reportlog tabulate
|
||||
|
||||
- name: Run CLI tests
|
||||
run: |
|
||||
@ -35,10 +35,12 @@ jobs:
|
||||
make test_cli
|
||||
|
||||
- name: Run test on GPUs
|
||||
if: always()
|
||||
run: |
|
||||
source activate accelerate
|
||||
make test
|
||||
- name: Run examples on GPUs
|
||||
if: always()
|
||||
run: |
|
||||
source activate accelerate
|
||||
pip uninstall comet_ml -y
|
||||
@ -47,6 +49,7 @@ jobs:
|
||||
- name: Generate Report
|
||||
if: always()
|
||||
run: |
|
||||
pip install tabulate
|
||||
python utils/log_reports.py >> $GITHUB_STEP_SUMMARY
|
||||
|
||||
run_all_tests_multi_gpu:
|
||||
@ -64,13 +67,8 @@ jobs:
|
||||
source activate accelerate
|
||||
git config --global --add safe.directory '*'
|
||||
git fetch && git checkout ${{ github.sha }}
|
||||
pip install -e .[testing,test_trackers]
|
||||
pip install pytest-reportlog
|
||||
|
||||
- name: Run CLI tests
|
||||
run: |
|
||||
source activate accelerate
|
||||
make test_cli
|
||||
pip install -e .[testing,test_trackers] -U
|
||||
pip install pytest-reportlog tabulate
|
||||
|
||||
- name: Run test on GPUs
|
||||
run: |
|
||||
@ -78,6 +76,7 @@ jobs:
|
||||
make test
|
||||
|
||||
- name: Run examples on GPUs
|
||||
if: always()
|
||||
run: |
|
||||
source activate accelerate
|
||||
pip uninstall comet_ml -y
|
||||
@ -86,4 +85,5 @@ jobs:
|
||||
- name: Generate Report
|
||||
if: always()
|
||||
run: |
|
||||
pip install tabulate
|
||||
python utils/log_reports.py >> $GITHUB_STEP_SUMMARY
|
||||
126
.github/workflows/self_hosted_integration_tests.yml
vendored
Normal file
126
.github/workflows/self_hosted_integration_tests.yml
vendored
Normal file
@ -0,0 +1,126 @@
|
||||
# CI for specifically ensuring integrations work fine (`transformers` mainly) on GPUs
|
||||
# Useful tips:
|
||||
# - `working-directory` should be set to the root of the repo, which is cloned on the actual CI runner.
|
||||
# It follows the directory structure of `actions-runner/_work/{repo_name}/{repo_name}/{cloned_repo} on
|
||||
# prem, but in Actions setting `working-directory` looks just in the `{repo_name}` level.
|
||||
# - New integrations to test should have its own job, and follow a strategy method where we check both
|
||||
# the pypi and github versions.
|
||||
# - Workflow call lets this be called from `build_and_run_tests.yml`
|
||||
# - When using a docker container, it's recommended to set `--shm-size`, we use 16gb.
|
||||
name: Integration Tests (push to "main")
|
||||
|
||||
on:
|
||||
workflow_call:
|
||||
workflow_dispatch:
|
||||
|
||||
env:
|
||||
HF_HOME: ~/hf_cache
|
||||
|
||||
defaults:
|
||||
run:
|
||||
shell: bash
|
||||
|
||||
jobs:
|
||||
run-trainer-tests:
|
||||
container:
|
||||
image: huggingface/accelerate-gpu:latest
|
||||
options: --gpus all --shm-size "16gb"
|
||||
runs-on: [self-hosted, docker-gpu, multi-gpu]
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
transformers-version: [
|
||||
pypi,
|
||||
github
|
||||
]
|
||||
cuda_visible_devices: [
|
||||
"0",
|
||||
"0,1"
|
||||
]
|
||||
steps:
|
||||
- name: Update accelerate clone and pip install
|
||||
working-directory: accelerate/
|
||||
run:
|
||||
source activate accelerate;
|
||||
git config --global --add safe.directory '*';
|
||||
git checkout main && git fetch && git checkout ${{ github.sha }};
|
||||
pip install -e .;
|
||||
|
||||
- name: Update transformers clone & pip install
|
||||
working-directory: transformers/
|
||||
run: |
|
||||
source activate accelerate
|
||||
git config --global --add safe.directory '*'
|
||||
git checkout main && git pull
|
||||
if [[ ${{ matrix.transformers-version }} = pypi ]]; then
|
||||
git checkout $(git describe --tags `git rev-list --tags --max-count=1`)
|
||||
fi
|
||||
pip install .[torch,deepspeed-testing]
|
||||
|
||||
- name: Show installed libraries
|
||||
run: |
|
||||
source activate accelerate;
|
||||
pip freeze
|
||||
|
||||
- name: Run trainer tests
|
||||
working-directory: transformers/
|
||||
env:
|
||||
CUDA_VISIBLE_DEVICES: ${{ matrix.cuda_visible_devices }}
|
||||
WANDB_DISABLED: true
|
||||
run: |
|
||||
source activate accelerate;
|
||||
pytest -sv tests/trainer
|
||||
|
||||
- name: Run deepspeed tests
|
||||
working-directory: transformers/
|
||||
env:
|
||||
CUDA_VISIBLE_DEVICES: ${{ matrix.cuda_visible_devices }}
|
||||
WANDB_DISABLED: true
|
||||
if: always()
|
||||
run: |
|
||||
source activate accelerate;
|
||||
pytest -sv tests/deepspeed
|
||||
|
||||
run-skorch-tests:
|
||||
container:
|
||||
image: huggingface/accelerate-gpu:latest
|
||||
options: --gpus all --shm-size "16gb"
|
||||
runs-on: [self-hosted, docker-gpu, multi-gpu]
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
skorch-version: [
|
||||
pypi,
|
||||
github
|
||||
]
|
||||
steps:
|
||||
- name: Update accelerate clone and pip install
|
||||
working-directory: accelerate/
|
||||
run:
|
||||
source activate accelerate;
|
||||
git config --global --add safe.directory '*';
|
||||
git checkout main && git fetch && git checkout ${{ github.sha }};
|
||||
pip install -e .;
|
||||
|
||||
- name: Update skorch clone & pip install
|
||||
working-directory: skorch/
|
||||
run: |
|
||||
source activate accelerate
|
||||
git config --global --add safe.directory '*'
|
||||
git checkout master && git pull
|
||||
if [[ ${{ matrix.skorch-version }} = pypi ]]; then
|
||||
git checkout $(git describe --tags `git rev-list --tags --max-count=1`)
|
||||
fi
|
||||
pip install .[testing]
|
||||
pip install flaky
|
||||
|
||||
- name: Show installed libraries
|
||||
run: |
|
||||
source activate accelerate;
|
||||
pip freeze
|
||||
|
||||
- name: Run skorch tests
|
||||
working-directory: skorch/
|
||||
run: |
|
||||
source activate accelerate;
|
||||
pytest -sv -k TestAccelerate
|
||||
2
.github/workflows/stale.yml
vendored
2
.github/workflows/stale.yml
vendored
@ -18,7 +18,7 @@ jobs:
|
||||
- name: Setup Python
|
||||
uses: actions/setup-python@v1
|
||||
with:
|
||||
python-version: 3.7
|
||||
python-version: 3.8
|
||||
|
||||
- name: Install requirements
|
||||
run: |
|
||||
|
||||
10
.github/workflows/test.yml
vendored
10
.github/workflows/test.yml
vendored
@ -23,7 +23,7 @@ jobs:
|
||||
matrix:
|
||||
pytorch-version: [
|
||||
latest,
|
||||
minimum
|
||||
minimum,
|
||||
]
|
||||
test-kind: [
|
||||
test_prod,
|
||||
@ -39,10 +39,10 @@ jobs:
|
||||
]
|
||||
steps:
|
||||
- uses: actions/checkout@v3.1.0
|
||||
- name: Set up python 3.7
|
||||
- name: Set up python 3.8
|
||||
uses: actions/setup-python@v3
|
||||
with:
|
||||
python-version: 3.7
|
||||
python-version: 3.8
|
||||
|
||||
- name: Activate python cache
|
||||
uses: actions/cache@v3
|
||||
@ -58,8 +58,8 @@ jobs:
|
||||
if [[ ${{ matrix.test-kind }} = test_prod ]]; then pip install -e .[test_prod]; fi
|
||||
if [[ ${{ matrix.test-kind }} != test_prod ]]; then pip install -e .[testing,test_trackers]; fi
|
||||
if [[ ${{ matrix.test-kind }} = test_rest ]]; then pip uninstall comet_ml -y; fi
|
||||
if [[ ${{ matrix.pytorch-version }} = minimum ]]; then pip install torch==1.6.0; fi
|
||||
pip install pytest-reportlog
|
||||
if [[ ${{ matrix.test-kind }} = minimum ]]; then pip install torch==1.10.0; fi
|
||||
pip install pytest-reportlog tabulate
|
||||
|
||||
- name: Run Tests
|
||||
env:
|
||||
|
||||
16
.github/workflows/upload_pr_documentation.yml
vendored
Normal file
16
.github/workflows/upload_pr_documentation.yml
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
name: Upload PR Documentation
|
||||
|
||||
on:
|
||||
workflow_run:
|
||||
workflows: ["Build PR Documentation"]
|
||||
types:
|
||||
- completed
|
||||
|
||||
jobs:
|
||||
build:
|
||||
uses: huggingface/doc-builder/.github/workflows/upload_pr_documentation.yml@main
|
||||
with:
|
||||
package_name: accelerate
|
||||
secrets:
|
||||
hf_token: ${{ secrets.HF_DOC_BUILD_PUSH }}
|
||||
comment_bot_token: ${{ secrets.COMMENT_BOT_TOKEN }}
|
||||
10
Makefile
10
Makefile
@ -1,6 +1,6 @@
|
||||
.PHONY: quality style test docs
|
||||
.PHONY: quality style test docs utils
|
||||
|
||||
check_dirs := tests src examples benchmarks
|
||||
check_dirs := tests src examples benchmarks utils
|
||||
|
||||
# Check that source code meets quality standards
|
||||
|
||||
@ -12,13 +12,13 @@ extra_quality_checks:
|
||||
|
||||
# this target runs checks on all files
|
||||
quality:
|
||||
black --check $(check_dirs)
|
||||
black --required-version 23 --check $(check_dirs)
|
||||
ruff $(check_dirs)
|
||||
doc-builder style src/accelerate docs/source --max_len 119 --check_only
|
||||
|
||||
# Format source code automatically and check is there are any problems left that need manual fixing
|
||||
style:
|
||||
black $(check_dirs)
|
||||
black --required-version 23 $(check_dirs)
|
||||
ruff $(check_dirs) --fix
|
||||
doc-builder style src/accelerate docs/source --max_len 119
|
||||
|
||||
@ -27,7 +27,7 @@ test:
|
||||
python -m pytest -s -v ./tests/ --ignore=./tests/test_examples.py $(if $(IS_GITHUB_CI),--report-log "$(PYTORCH_VERSION)_all.log",)
|
||||
|
||||
test_big_modeling:
|
||||
python -m pytest -s -v ./tests/test_big_modeling.py $(if $(IS_GITHUB_CI),--report-log "$(PYTORCH_VERSION)_big_modeling.log",)
|
||||
python -m pytest -s -v ./tests/test_big_modeling.py ./tests/test_modeling_utils.py $(if $(IS_GITHUB_CI),--report-log "$(PYTORCH_VERSION)_big_modeling.log",)
|
||||
|
||||
test_core:
|
||||
python -m pytest -s -v ./tests/ --ignore=./tests/test_examples.py --ignore=./tests/deepspeed --ignore=./tests/test_big_modeling.py \
|
||||
|
||||
40
README.md
40
README.md
@ -16,12 +16,12 @@ limitations under the License.
|
||||
|
||||
<p align="center">
|
||||
<br>
|
||||
<img src="docs/source/imgs/accelerate_logo.png" width="400"/>
|
||||
<img src="https://raw.githubusercontent.com/huggingface/accelerate/main/docs/source/imgs/accelerate_logo.png" width="400"/>
|
||||
<br>
|
||||
<p>
|
||||
|
||||
<p align="center">
|
||||
<!-- Uncomment when CircleCI is setup
|
||||
<!-- Uncomment when CircleCI is set up
|
||||
<a href="https://circleci.com/gh/huggingface/accelerate">
|
||||
<img alt="Build" src="https://img.shields.io/circleci/build/github/huggingface/transformers/master">
|
||||
</a>
|
||||
@ -91,7 +91,7 @@ Here is an example:
|
||||
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).
|
||||
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 (fp8, fp16, bf16).
|
||||
|
||||
In particular, the same code can then be run without modification on your local machine for debugging or your training environment.
|
||||
|
||||
@ -132,7 +132,7 @@ In particular, the same code can then be run without modification on your local
|
||||
optimizer.step()
|
||||
```
|
||||
|
||||
Want to learn more? Check out the [documentation](https://huggingface.co/docs/accelerate) or have look at our [examples](https://github.com/huggingface/accelerate/tree/main/examples).
|
||||
Want to learn more? Check out the [documentation](https://huggingface.co/docs/accelerate) or have a look at our [examples](https://github.com/huggingface/accelerate/tree/main/examples).
|
||||
|
||||
## Launching script
|
||||
|
||||
@ -155,7 +155,17 @@ For instance, here is how you would run the GLUE example on the MRPC task (from
|
||||
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.
|
||||
This CLI tool is **optional**, and you can still use `python my_script.py` or `python -m torchrun my_script.py` at your convenience.
|
||||
|
||||
You can also directly pass in the arguments you would to `torchrun` as arguments to `accelerate launch` if you wish to not run` accelerate config`.
|
||||
|
||||
For example, here is how to launch on two GPUs:
|
||||
|
||||
```bash
|
||||
accelerate launch --multi_gpu --num_processes 2 examples/nlp_example.py
|
||||
```
|
||||
|
||||
To learn more, check the CLI documentation available [here](https://huggingface.co/docs/accelerate/package_reference/cli).
|
||||
|
||||
## Launching multi-CPU run using MPI
|
||||
|
||||
@ -168,12 +178,12 @@ 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`.
|
||||
🤗 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`.
|
||||
|
||||
```python
|
||||
from accelerate import Accelerator, DeepSpeedPlugin
|
||||
|
||||
# deepspeed needs to know your gradient accumulation steps before hand, so don't forget to pass it
|
||||
# deepspeed needs to know your gradient accumulation steps beforehand, 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)
|
||||
@ -200,7 +210,7 @@ An example can be found in [this notebook](https://github.com/huggingface/notebo
|
||||
|
||||
## 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.
|
||||
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?
|
||||
|
||||
@ -211,18 +221,21 @@ You shouldn't use 🤗 Accelerate if you don't want to write a training loop you
|
||||
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:
|
||||
|
||||
* [Animus](https://github.com/Scitator/animus) is a minimalistic framework to run machine learning experiments. Animus highlights common "breakpoints" in ML experiments and provides a unified interface for them within [IExperiment](https://github.com/Scitator/animus/blob/main/animus/core.py#L76).
|
||||
* [Catalyst](https://github.com/catalyst-team/catalyst#getting-started) is a PyTorch framework for Deep Learning Research and Development. It focuses on reproducibility, rapid experimentation, and codebase reuse so you can create something new rather than write yet another train loop. Catalyst provides a [Runner](https://catalyst-team.github.io/catalyst/api/core.html#runner) to connect all parts of the experiment: hardware backend, data transformations, model train, and inference logic.
|
||||
* [Catalyst](https://github.com/catalyst-team/catalyst#getting-started) is a PyTorch framework for Deep Learning Research and Development. It focuses on reproducibility, rapid experimentation, and codebase reuse so you can create something new rather than write yet another train loop. Catalyst provides a [Runner](https://catalyst-team.github.io/catalyst/api/core.html#runner) to connect all parts of the experiment: hardware backend, data transformations, model training, and inference logic.
|
||||
* [fastai](https://github.com/fastai/fastai#installing) is a PyTorch framework for Deep Learning that simplifies training fast and accurate neural nets using modern best practices. fastai provides a [Learner](https://docs.fast.ai/learner.html#Learner) to handle the training, fine-tuning, and inference of deep learning algorithms.
|
||||
* [Finetuner](https://github.com/jina-ai/finetuner) is a service that enables models to create higher-quality embeddings for semantic search, visual similarity search, cross-modal text<->image search, recommendation systems, clustering, duplication detection, anomaly detection, or other uses.
|
||||
* [InvokeAI](https://github.com/invoke-ai/InvokeAI) is a creative engine for Stable Diffusion models, offering industry-leading WebUI, terminal usage support, and serves as the foundation for many commercial products.
|
||||
* [Kornia](https://kornia.readthedocs.io/en/latest/get-started/introduction.html) is a differentiable library that allows classical computer vision to be integrated into deep learning models. Kornia provides a [Trainer](https://kornia.readthedocs.io/en/latest/x.html#kornia.x.Trainer) with the specific purpose to train and fine-tune the supported deep learning algorithms within the library.
|
||||
* [Open Assistant](https://projects.laion.ai/Open-Assistant/) is a chat-based assistant that understands tasks, can interact with their party systems, and retrieve information dynamically to do so.
|
||||
* [pytorch-accelerated](https://github.com/Chris-hughes10/pytorch-accelerated) is a lightweight training library, with a streamlined feature set centred around a general-purpose [Trainer](https://pytorch-accelerated.readthedocs.io/en/latest/trainer.html), that places a huge emphasis on simplicity and transparency; enabling users to understand exactly what is going on under the hood, but without having to write and maintain the boilerplate themselves!
|
||||
* [pytorch-accelerated](https://github.com/Chris-hughes10/pytorch-accelerated) is a lightweight training library, with a streamlined feature set centered around a general-purpose [Trainer](https://pytorch-accelerated.readthedocs.io/en/latest/trainer.html), that places a huge emphasis on simplicity and transparency; enabling users to understand exactly what is going on under the hood, but without having to write and maintain the boilerplate themselves!
|
||||
* [Stable Diffusion web UI](https://github.com/AUTOMATIC1111/stable-diffusion-webui) is an open-source browser-based easy-to-use interface based on the Gradio library for Stable Diffusion.
|
||||
* [torchkeras](https://github.com/lyhue1991/torchkeras) is a simple tool for training pytorch model just in a keras style, a dynamic and beautiful plot is provided in notebook to monitor your loss or metric.
|
||||
* [transformers](https://github.com/huggingface/transformers) as a tool for helping train state-of-the-art machine learning models in PyTorch, Tensorflow, and JAX. (Accelerate is the backend for the PyTorch side).
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
This repository is tested on Python 3.6+ and PyTorch 1.4.0+
|
||||
This repository is tested on Python 3.8+ and PyTorch 1.10.0+
|
||||
|
||||
You should install 🤗 Accelerate in a [virtual environment](https://docs.python.org/3/library/venv.html). If you're unfamiliar with Python virtual environments, check out the [user guide](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/).
|
||||
|
||||
@ -243,7 +256,8 @@ pip install accelerate
|
||||
- multi-GPU on one node (machine)
|
||||
- multi-GPU on several nodes (machines)
|
||||
- TPU
|
||||
- FP16 with native AMP (apex on the roadmap)
|
||||
- FP16/BFloat16 mixed precision
|
||||
- FP8 mixed precision with [Transformer Engine](https://github.com/NVIDIA/TransformerEngine)
|
||||
- DeepSpeed support (Experimental)
|
||||
- PyTorch Fully Sharded Data Parallel (FSDP) support (Experimental)
|
||||
- Megatron-LM support (Experimental)
|
||||
@ -255,7 +269,7 @@ If you use 🤗 Accelerate in your publication, please cite it by using the foll
|
||||
```bibtex
|
||||
@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},
|
||||
author = {Sylvain Gugger, Lysandre Debut, Thomas Wolf, Philipp Schmid, Zachary Mueller, Sourab Mangrulkar, Marc Sun, Benjamin Bossan},
|
||||
howpublished = {\url{https://github.com/huggingface/accelerate}},
|
||||
year = {2022}
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
# Builds CPU-only Docker image of PyTorch
|
||||
# Uses multi-staged approach to reduce size
|
||||
# Stage 1
|
||||
FROM python:3.7-slim as compile-image
|
||||
FROM python:3.8-slim as compile-image
|
||||
|
||||
ARG DEBIAN_FRONTEND=noninteractive
|
||||
|
||||
@ -25,7 +25,7 @@ RUN python3 -m pip install --no-cache-dir \
|
||||
--extra-index-url https://download.pytorch.org/whl/cpu
|
||||
|
||||
# Stage 2
|
||||
FROM python:3.7-slim AS build-image
|
||||
FROM python:3.8-slim AS build-image
|
||||
COPY --from=compile-image /opt/venv /opt/venv
|
||||
RUN useradd -ms /bin/bash user
|
||||
USER user
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
# Use base conda image to reduce time
|
||||
FROM continuumio/miniconda3:latest AS compile-image
|
||||
# Specify py version
|
||||
ENV PYTHON_VERSION=3.7.3
|
||||
ENV PYTHON_VERSION=3.8
|
||||
# Install apt libs
|
||||
RUN apt-get update && \
|
||||
apt-get install -y curl git wget && \
|
||||
@ -23,7 +23,7 @@ SHELL ["/bin/bash", "-c"]
|
||||
RUN source activate accelerate && \
|
||||
python3 -m pip install --no-cache-dir \
|
||||
git+https://github.com/huggingface/accelerate#egg=accelerate[testing,test_trackers] \
|
||||
--extra-index-url https://download.pytorch.org/whl/cu113
|
||||
--extra-index-url https://download.pytorch.org/whl/cu117
|
||||
|
||||
RUN python3 -m pip install --no-cache-dir bitsandbytes
|
||||
|
||||
|
||||
@ -67,7 +67,7 @@ doc-builder preview {package_name} {path_to_docs}
|
||||
For example:
|
||||
|
||||
```bash
|
||||
doc-builder preview transformers docs/source/en/
|
||||
doc-builder preview accelerate docs/source/
|
||||
```
|
||||
|
||||
The docs will be viewable at [http://localhost:3000](http://localhost:3000). You can also preview the docs once you have opened a PR. You will see a bot add a comment to a link where the documentation with your changes lives.
|
||||
@ -81,7 +81,7 @@ The `preview` command only works with existing doc files. When you add a complet
|
||||
|
||||
## Adding a new element to the navigation bar
|
||||
|
||||
Accepted files are Markdown (.md or .mdx).
|
||||
Accepted files are Markdown (.md).
|
||||
|
||||
Create a file with its extension and put it in the source directory. You can then link it to the toc-tree by putting
|
||||
the filename without the extension in the [`_toctree.yml`](https://github.com/huggingface/accelerate/blob/main/docs/source/_toctree.yml) file.
|
||||
|
||||
@ -22,13 +22,23 @@
|
||||
- local: usage_guides/training_zoo
|
||||
title: Example Zoo
|
||||
- local: usage_guides/big_modeling
|
||||
title: How perform inference on large models with small resources
|
||||
title: How to perform inference on large models with small resources
|
||||
- local: usage_guides/model_size_estimator
|
||||
title: Knowing how big of a model you can fit into memory
|
||||
- local: usage_guides/quantization
|
||||
title: How to quantize model
|
||||
- local: usage_guides/distributed_inference
|
||||
title: How to perform distributed inference with normal resources
|
||||
- local: usage_guides/gradient_accumulation
|
||||
title: Performing gradient accumulation
|
||||
- local: usage_guides/local_sgd
|
||||
title: Accelerating training with local SGD
|
||||
- local: usage_guides/checkpoint
|
||||
title: Saving and loading training states
|
||||
- local: usage_guides/tracking
|
||||
title: Using experiment trackers
|
||||
- local: usage_guides/debug
|
||||
title: Debugging timeout errors
|
||||
- local: usage_guides/memory
|
||||
title: How to avoid CUDA Out-of-Memory
|
||||
- local: usage_guides/mps
|
||||
@ -41,8 +51,12 @@
|
||||
title: How to use Megatron-LM
|
||||
- local: usage_guides/sagemaker
|
||||
title: How to use 🤗 Accelerate with SageMaker
|
||||
- local: usage_guides/ipex
|
||||
title: How to use 🤗 Accelerate with Intel® Extension for PyTorch for cpu
|
||||
title: How-To Guides
|
||||
- sections:
|
||||
- local: concept_guides/big_model_inference
|
||||
title: Loading big models into memory
|
||||
- local: concept_guides/performance
|
||||
title: Comparing performance across distributed setups
|
||||
- local: concept_guides/deferring_execution
|
||||
@ -77,4 +91,6 @@
|
||||
title: Utility functions and classes
|
||||
- local: package_reference/megatron_lm
|
||||
title: Megatron-LM Utilities
|
||||
title: "Reference"
|
||||
- local: package_reference/fsdp
|
||||
title: Fully Sharded Data Parallelism Utilities
|
||||
title: "Reference"
|
||||
|
||||
@ -8,11 +8,14 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Installation and Configuration
|
||||
|
||||
Before you start, you will need to setup your environment, install the appropriate packages, and configure 🤗 Accelerate. 🤗 Accelerate is tested on **Python 3.7+**.
|
||||
Before you start, you will need to setup your environment, install the appropriate packages, and configure 🤗 Accelerate. 🤗 Accelerate is tested on **Python 3.8+**.
|
||||
|
||||
## Installing 🤗 Accelerate
|
||||
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Launching your 🤗 Accelerate scripts
|
||||
@ -36,7 +39,7 @@ for batch in training_dataloader:
|
||||
|
||||
But how do you run this code and have it utilize the special hardware available to it?
|
||||
|
||||
First you should rewrite the above code into a function, and make it callable as a script. For example:
|
||||
First, you should rewrite the above code into a function, and make it callable as a script. For example:
|
||||
|
||||
```diff
|
||||
from accelerate import Accelerator
|
||||
@ -61,7 +64,7 @@ First you should rewrite the above code into a function, and make it callable as
|
||||
+ main()
|
||||
```
|
||||
|
||||
Next you need to launch it with `accelerate launch`.
|
||||
Next, you need to launch it with `accelerate launch`.
|
||||
|
||||
<Tip warning={true}>
|
||||
|
||||
@ -74,7 +77,7 @@ Next you need to launch it with `accelerate launch`.
|
||||
## Using accelerate launch
|
||||
|
||||
🤗 Accelerate has a special CLI command to help you launch your code in your system through `accelerate launch`.
|
||||
This command wraps around all of the different commands needed to launch your script on various platforms, without you having to remember what each of them are.
|
||||
This command wraps around all of the different commands needed to launch your script on various platforms, without you having to remember what each of them is.
|
||||
|
||||
<Tip>
|
||||
|
||||
@ -88,7 +91,7 @@ You can launch your script quickly by using:
|
||||
accelerate launch {script_name.py} --arg1 --arg2 ...
|
||||
```
|
||||
|
||||
Just put `accelerate launch` at the start of your command, and pass in additional arguments and parameters to your script afterwards like normal!
|
||||
Just put `accelerate launch` at the start of your command, and pass in additional arguments and parameters to your script afterward like normal!
|
||||
|
||||
Since this runs the various torch spawn methods, all of the expected environment variables can be modified here as well.
|
||||
For example, here is how to use `accelerate launch` with a single GPU:
|
||||
@ -105,6 +108,12 @@ Here is how you would use all GPUs and train with mixed precision disabled:
|
||||
accelerate launch --multi_gpu {script_name.py} {--arg1} {--arg2} ...
|
||||
```
|
||||
|
||||
Or by specifying a number of GPUs to use:
|
||||
|
||||
```bash
|
||||
accelerate launch --num_processes=2 {script_name.py} {--arg1} {--arg2} ...
|
||||
```
|
||||
|
||||
To get more specific you should pass in the needed parameters yourself. For instance, here is how you
|
||||
would also launch that same script on two GPUs using mixed precision while avoiding all of the warnings:
|
||||
|
||||
@ -130,6 +139,21 @@ For a visualization of this difference, that earlier `accelerate launch` on mult
|
||||
MIXED_PRECISION="fp16" torchrun --nproc_per_node=2 --num_machines=1 {script_name.py} {--arg1} {--arg2} ...
|
||||
```
|
||||
|
||||
You can also launch your script utilizing the launch CLI as a python module itself, enabling the ability to pass in other python-specific
|
||||
launching behaviors. To do so, use `accelerate.commands.launch` instead of `accelerate launch`:
|
||||
|
||||
```bash
|
||||
python -m accelerate.commands.launch --num_processes=2 {script_name.py} {--arg1} {--arg2}
|
||||
```
|
||||
|
||||
If you want to execute the script with any other python flags, you can pass them in as well similar to `-m`, such as
|
||||
the below example enabling unbuffered stdout and stderr:
|
||||
|
||||
```bash
|
||||
python -u -m accelerate.commands.launch --num_processes=2 {script_name.py} {--arg1} {--arg2}
|
||||
```
|
||||
|
||||
|
||||
## Why you should always use `accelerate config`
|
||||
|
||||
Why is it useful to the point you should **always** run `accelerate config`?
|
||||
@ -175,4 +199,4 @@ use_cpu: false
|
||||
Launching a script from the location of that custom yaml file looks like the following:
|
||||
```bash
|
||||
accelerate launch --config_file {path/to/config/my_config_file.yaml} {script_name.py} {--arg1} {--arg2} ...
|
||||
```
|
||||
```
|
||||
@ -8,13 +8,16 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Migrating your code to 🤗 Accelerate
|
||||
|
||||
This tutorial will detail how to easily convert existing PyTorch code to use 🤗 Accelerate!
|
||||
You'll see that by just changing a few lines of code, 🤗 Accelerate can perform its magic and get you on
|
||||
your way towards running your code on distributed systems with ease!
|
||||
your way toward running your code on distributed systems with ease!
|
||||
|
||||
## The base training loop
|
||||
|
||||
@ -65,7 +68,7 @@ change the definition of `device` to come from [`Accelerator`]:
|
||||
|
||||
### Preparing your objects
|
||||
|
||||
Next you need to pass all of the important objects related to training into [`~Accelerator.prepare`]. 🤗 Accelerate will
|
||||
Next, you need to pass all of the important objects related to training into [`~Accelerator.prepare`]. 🤗 Accelerate will
|
||||
make sure everything is setup in the current environment for you to start training:
|
||||
|
||||
```
|
||||
@ -73,7 +76,7 @@ model, optimizer, training_dataloader, scheduler = accelerator.prepare(
|
||||
model, optimizer, training_dataloader, scheduler
|
||||
)
|
||||
```
|
||||
These objects are returned in the same order they were sent in with. By default when using `device_placement=True`, all of the objects that can be sent to the right device will be.
|
||||
These objects are returned in the same order they were sent in. By default when using `device_placement=True`, all of the objects that can be sent to the right device will be.
|
||||
If you need to work with data that isn't passed to [~Accelerator.prepare] but should be on the active device, you should pass in the `device` you made earlier.
|
||||
|
||||
<Tip warning={true}>
|
||||
@ -121,3 +124,6 @@ for batch in training_dataloader:
|
||||
scheduler.step()
|
||||
```
|
||||
|
||||
## More Resources
|
||||
|
||||
To check out more ways on how to migrate to 🤗 Accelerate, check out our [interactive migration tutorial](https://huggingface.co/docs/accelerate/usage_guides/explore) which showcases other items that need to be watched for when using Accelerate and how to do so quickly.
|
||||
@ -8,9 +8,12 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Launching Multi-Node Training from a Jupyter Environment
|
||||
# Launching Multi-GPU Training from a Jupyter Environment
|
||||
|
||||
This tutorial teaches you how to fine tune a computer vision model with 🤗 Accelerate from a Jupyter Notebook on a distributed system.
|
||||
You will also learn how to setup a few requirements needed for ensuring your environment is configured properly, your data has been prepared properly, and finally how to launch training.
|
||||
@ -35,7 +38,7 @@ The following code will restart Jupyter after writing the configuration, as CUDA
|
||||
|
||||
<Tip warning={true}>
|
||||
|
||||
CUDA can't be initialized more than once on a multi-node system. It's fine to debug in the notebook and have calls to CUDA, but in order to finally train a full cleanup and restart will need to be performed.
|
||||
CUDA can't be initialized more than once on a multi-GPU system. It's fine to debug in the notebook and have calls to CUDA, but in order to finally train a full cleanup and restart will need to be performed.
|
||||
|
||||
</Tip>
|
||||
|
||||
@ -398,6 +401,26 @@ args = ("fp16", 42, 64)
|
||||
notebook_launcher(training_loop, args, num_processes=2)
|
||||
```
|
||||
|
||||
In the case of running on multiple nodes, you need to set up a Jupyter session at each node and run the launching cell at the same time.
|
||||
|
||||
For an environment containing 2 nodes (computers) with 8 GPUs each and the main computer with an IP address of "172.31.43.8", it would look like so:
|
||||
|
||||
```python
|
||||
notebook_launcher(training_loop, args, master_addr="172.31.43.8", node_rank=0, num_nodes=2, num_processes=8)
|
||||
```
|
||||
|
||||
And in the second Jupyter session on the other machine:
|
||||
|
||||
<Tip>
|
||||
|
||||
Notice how the `node_rank` has changed
|
||||
|
||||
</Tip>
|
||||
|
||||
```python
|
||||
notebook_launcher(training_loop, args, master_addr="172.31.43.8", node_rank=1, num_nodes=2, num_processes=8)
|
||||
```
|
||||
|
||||
In the case of running on the TPU, it would look like so:
|
||||
|
||||
```python
|
||||
@ -420,6 +443,13 @@ epoch 4: 94.71
|
||||
|
||||
And that's it!
|
||||
|
||||
## Debugging
|
||||
|
||||
A common issue when running the `notebook_launcher` is receiving a CUDA has already been initialized issue. This usually stems
|
||||
from an import or prior code in the notebook that makes a call to the PyTorch `torch.cuda` sublibrary. To help narrow down what went wrong,
|
||||
you can launch the `notebook_launcher` with `ACCELERATE_DEBUG_MODE=yes` in your environment and an additional check
|
||||
will be made when spawning that a regular process can be created and utilize CUDA without issue. (Your CUDA code can still be ran afterwards).
|
||||
|
||||
## Conclusion
|
||||
|
||||
This notebook showed how to perform distributed training from inside of a Jupyter Notebook. Some key notes to remember:
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Overview
|
||||
@ -8,11 +8,14 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Handling big models for inference
|
||||
|
||||
When loading a pretrained model in PyTorch, the usual workflow looks like this:
|
||||
When loading a pre-trained model in PyTorch, the usual workflow looks like this:
|
||||
|
||||
```py
|
||||
import torch
|
||||
@ -27,11 +30,11 @@ In plain English, those steps are:
|
||||
2. Load the model weights (in a dictionary usually called a state dict) from the disk
|
||||
3. Load those weights inside the model
|
||||
|
||||
While this works very well for regularly sized models, this workflow has some clear limitations when we deal with a huge model: in step 1, we load a full version of the model in RAM, and spend some time randomly initializing the weights (which will be discarded in step 3). In step 2, we load another full version of the model in RAM, with the pretrained weights. If you're loading a model with 6 billions parameters, this means you will need 24GB of RAM for each copy of the model, so 48GB in total (half of it to load the model in FP16).
|
||||
While this works very well for regularly sized models, this workflow has some clear limitations when we deal with a huge model: in step 1, we load a full version of the model in RAM, and spend some time randomly initializing the weights (which will be discarded in step 3). In step 2, we load another full version of the model in RAM, with the pre-trained weights. If you're loading a model with 6 billion parameters, this means you will need 24GB of RAM for each copy of the model, so 48GB in total (half of it to load the model in FP16).
|
||||
|
||||
<Tip warning={true}>
|
||||
|
||||
This API is quite new and still in its experimental stage. While we strive to provide a stable API, it's possible some small parts of the public API will change in the future.
|
||||
This API is quite new and still in its experimental stage. While we strive to provide a stable API, it's possible some small parts of the public API will change in the future.
|
||||
|
||||
</Tip>
|
||||
|
||||
@ -43,7 +46,7 @@ While this works very well for regularly sized models, this workflow has some cl
|
||||
|
||||
### Instantiating an empty model
|
||||
|
||||
The first tool 🤗 Accelerate introduces to help with big models is a context manager [`init_empty_weights`] that helps you initialize a model without using any RAM, so that step 1 can be done on models of any size. Here is how it works:
|
||||
The first tool 🤗 Accelerate introduces to help with big models is a context manager [`init_empty_weights`] that helps you initialize a model without using any RAM so that step 1 can be done on models of any size. Here is how it works:
|
||||
|
||||
```py
|
||||
from accelerate import init_empty_weights
|
||||
@ -59,7 +62,7 @@ with init_empty_weights():
|
||||
model = nn.Sequential(*[nn.Linear(10000, 10000) for _ in range(1000)])
|
||||
```
|
||||
|
||||
initializes an empty model with a bit more than 100B parameters. Behind the scenes, this relies on the meta device introduced in PyTorch 1.9. During the initialization under the context manager, each time a parameter is created, it is instantly moved on that device.
|
||||
initializes an empty model with a bit more than 100B parameters. Behind the scenes, this relies on the meta device introduced in PyTorch 1.9. During the initialization under the context manager, each time a parameter is created, it is instantly moved to that device.
|
||||
|
||||
<Tip warning={true}>
|
||||
|
||||
@ -69,9 +72,9 @@ initializes an empty model with a bit more than 100B parameters. Behind the scen
|
||||
|
||||
### Sharded checkpoints
|
||||
|
||||
It's possible your model is so big that even a single copy won't fit in RAM. That doesn't mean it can't be loaded: if you have one or several GPUs, this is more memory available to store your model. In this case, it's better if your checkpoint is split in several smaller files that we call checkpoint shards.
|
||||
It's possible your model is so big that even a single copy won't fit in RAM. That doesn't mean it can't be loaded: if you have one or several GPUs, this is more memory available to store your model. In this case, it's better if your checkpoint is split into several smaller files that we call checkpoint shards.
|
||||
|
||||
🤗 Accelerate will handle sharded checkpoints as long as you follow the following format: your checkpoint should be in a folder, with several files containing the partial state dicts, and there should be an index in the JSON format that contains a dictionary mapping parameter names to the file containing their weights. For instance we could have a folder containing:
|
||||
🤗 Accelerate will handle sharded checkpoints as long as you follow the following format: your checkpoint should be in a folder, with several files containing the partial state dicts, and there should be an index in the JSON format that contains a dictionary mapping parameter names to the file containing their weights. You can easily shard your model with [`~Accelerator.save_model`]. For instance, we could have a folder containing:
|
||||
|
||||
```bash
|
||||
first_state_dict.bin
|
||||
@ -96,44 +99,65 @@ and `first_state_dict.bin` containing the weights for `"linear1.weight"` and `"l
|
||||
|
||||
The second tool 🤗 Accelerate introduces is a function [`load_checkpoint_and_dispatch`], that will allow you to load a checkpoint inside your empty model. This supports full checkpoints (a single file containing the whole state dict) as well as sharded checkpoints. It will also automatically dispatch those weights across the devices you have available (GPUs, CPU RAM), so if you are loading a sharded checkpoint, the maximum RAM usage will be the size of the biggest shard.
|
||||
|
||||
Here is how we can use this to load the [GPT-J-6B](https://huggingface.co/EleutherAI/gpt-j-6B) model. You clone the sharded version of this model with:
|
||||
If you want to use big model inference with 🤗 Transformers models, check out this [documentation](https://huggingface.co/docs/transformers/main/en/main_classes/model#large-model-loading).
|
||||
|
||||
Here is how we can use this to load the [GPT2-1.5B](https://huggingface.co/marcsun13/gpt2-xl-linear-sharded) model.
|
||||
|
||||
Let's download the sharded version of this model.
|
||||
|
||||
```bash
|
||||
git clone https://huggingface.co/sgugger/sharded-gpt-j-6B
|
||||
cd sharded-gpt-j-6B
|
||||
git-lfs install
|
||||
git lfs pull
|
||||
pip install huggingface_hub
|
||||
```
|
||||
|
||||
then we can initialize the model with
|
||||
```py
|
||||
from huggingface_hub import snapshot_download
|
||||
checkpoint = "marcsun13/gpt2-xl-linear-sharded"
|
||||
weights_location = snapshot_download(repo_id=checkpoint)
|
||||
```
|
||||
|
||||
In order to initialize the model, we will use the library minGPT.
|
||||
|
||||
```bash
|
||||
git clone https://github.com/karpathy/minGPT.git
|
||||
pip install minGPT/
|
||||
```
|
||||
|
||||
```py
|
||||
from accelerate import init_empty_weights
|
||||
from transformers import AutoConfig, AutoModelForCausalLM
|
||||
from mingpt.model import GPT
|
||||
|
||||
checkpoint = "EleutherAI/gpt-j-6B"
|
||||
config = AutoConfig.from_pretrained(checkpoint)
|
||||
model_config = GPT.get_default_config()
|
||||
model_config.model_type = 'gpt2-xl'
|
||||
model_config.vocab_size = 50257
|
||||
model_config.block_size = 1024
|
||||
|
||||
with init_empty_weights():
|
||||
model = AutoModelForCausalLM.from_config(config)
|
||||
model = GPT(model_config)
|
||||
```
|
||||
|
||||
and load the checkpoint we just downloaded with:
|
||||
Then, load the checkpoint we just downloaded with:
|
||||
|
||||
```py
|
||||
from accelerate import load_checkpoint_and_dispatch
|
||||
|
||||
model = load_checkpoint_and_dispatch(
|
||||
model, "sharded-gpt-j-6B", device_map="auto", no_split_module_classes=["GPTJBlock"]
|
||||
model, checkpoint=weights_location, device_map="auto", no_split_module_classes=['Block']
|
||||
)
|
||||
```
|
||||
|
||||
By passing `device_map="auto"`, we tell 🤗 Accelerate to determine automatically where to put each layer of the model depending on the available resources:
|
||||
- first we use the maximum space available on the GPU(s)
|
||||
- first, we use the maximum space available on the GPU(s)
|
||||
- if we still need space, we store the remaining weights on the CPU
|
||||
- if there is not enough RAM, we store the remaining weights on the hard drive as memory-mapped tensors
|
||||
|
||||
`no_split_module_classes=["GPTJBlock"]` indicates that the modules that are `GPTJBlock` should not be split on different devices. You should set here all blocks that include a residual connection of some kind.
|
||||
|
||||
#### `no_split_module_classes`
|
||||
|
||||
This parameter will indicate that some of the modules with the name `"Block"` should not be split across different devices. You should set here all blocks that
|
||||
include a residutal connection of some kind.
|
||||
|
||||
|
||||
#### The `device_map`
|
||||
|
||||
You can see the `device_map` that 🤗 Accelerate picked by accessing the `hf_device_map` attribute of your model:
|
||||
|
||||
@ -143,43 +167,34 @@ model.hf_device_map
|
||||
|
||||
```python out
|
||||
{'transformer.wte': 0,
|
||||
'transformer.wpe': 0,
|
||||
'transformer.drop': 0,
|
||||
'transformer.h.0': 0,
|
||||
'transformer.h.1': 0,
|
||||
'transformer.h.2': 0,
|
||||
'transformer.h.3': 0,
|
||||
'transformer.h.4': 0,
|
||||
'transformer.h.5': 0,
|
||||
'transformer.h.6': 0,
|
||||
'transformer.h.7': 0,
|
||||
'transformer.h.8': 0,
|
||||
'transformer.h.9': 0,
|
||||
'transformer.h.10': 0,
|
||||
'transformer.h.11': 0,
|
||||
'transformer.h.12': 0,
|
||||
'transformer.h.13': 0,
|
||||
'transformer.h.14': 0,
|
||||
'transformer.h.15': 0,
|
||||
'transformer.h.16': 0,
|
||||
'transformer.h.17': 0,
|
||||
'transformer.h.18': 0,
|
||||
'transformer.h.19': 0,
|
||||
'transformer.h.20': 0,
|
||||
'transformer.h.21': 0,
|
||||
'transformer.h.22': 0,
|
||||
'transformer.h.23': 0,
|
||||
...
|
||||
'transformer.h.21': 0,
|
||||
'transformer.h.22': 1,
|
||||
'transformer.h.23': 1,
|
||||
'transformer.h.24': 1,
|
||||
'transformer.h.25': 1,
|
||||
'transformer.h.26': 1,
|
||||
'transformer.h.27': 1,
|
||||
'transformer.ln_f': 1,
|
||||
...
|
||||
'transformer.h.47': 1,
|
||||
'transformer.ln_f': 1,
|
||||
'lm_head': 1}
|
||||
```
|
||||
|
||||
You can also design your `device_map` yourself, if you prefer to explicitly decide where each layer should be. In this case, the command above becomes:
|
||||
It's fully possible to create your own device map for the layers to use as well, specifying the GPU device to use (a number), `"cpu"`, or `"disk"` and pass this in:
|
||||
|
||||
```python
|
||||
device_map = {
|
||||
"transformer.wte": "cpu",
|
||||
"transformer.wpe": 0,
|
||||
"transformer.drop": "cpu",
|
||||
"transformer.h.0": "disk"
|
||||
}
|
||||
|
||||
model = load_checkpoint_and_dispatch(
|
||||
model, checkpoint=weights_location, device_map=device_map
|
||||
)
|
||||
|
||||
```py
|
||||
model = load_checkpoint_and_dispatch(model, "sharded-gpt-j-6B", device_map=my_device_map)
|
||||
```
|
||||
|
||||
### Run the model
|
||||
@ -187,31 +202,30 @@ model = load_checkpoint_and_dispatch(model, "sharded-gpt-j-6B", device_map=my_de
|
||||
Now that we have done this, our model lies across several devices, and maybe the hard drive. But it can still be used as a regular PyTorch model:
|
||||
|
||||
```py
|
||||
from transformers import AutoTokenizer
|
||||
from mingpt.bpe import BPETokenizer
|
||||
tokenizer = BPETokenizer()
|
||||
inputs = tokenizer("Hello, my name is").to(0)
|
||||
|
||||
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
||||
inputs = tokenizer("Hello, my name is", return_tensors="pt")
|
||||
inputs = inputs.to(0)
|
||||
output = model.generate(inputs["input_ids"])
|
||||
tokenizer.decode(output[0].tolist())
|
||||
outputs = model.generate(x1, max_new_tokens=10, do_sample=False)[0]
|
||||
tokenizer.decode(outputs.cpu().squeeze())
|
||||
```
|
||||
|
||||
Behind the scenes, 🤗 Accelerate added hooks to the model, so that:
|
||||
- at each layer, the inputs are put on the right device (so even if your model is spread across several GPUs, it works)
|
||||
- for the weights offloaded on the CPU, they are put on a GPU just before the forward pass, and cleaned up just after
|
||||
- for the weights offloaded on the hard drive, they are loaded in RAM then put on a GPU just before the forward pass, and cleaned up just after
|
||||
- for the weights offloaded on the CPU, they are put on a GPU just before the forward pass and cleaned up just after
|
||||
- for the weights offloaded on the hard drive, they are loaded in RAM then put on a GPU just before the forward pass and cleaned up just after
|
||||
|
||||
This way, you model can run for inference even if it doesn't fit on one of the GPUs or the CPU RAM!
|
||||
This way, your model can run for inference even if it doesn't fit on one of the GPUs or the CPU RAM!
|
||||
|
||||
<Tip warning={true}>
|
||||
|
||||
This only supports inference of your model, not training. Most of the computation happens behind `torch.no_grad()` context managers to avoid spending some GPU memory with intermediate activations.
|
||||
This only supports the inference of your model, not training. Most of the computation happens behind `torch.no_grad()` context managers to avoid spending some GPU memory with intermediate activations.
|
||||
|
||||
</Tip>
|
||||
|
||||
### Designing a device map
|
||||
|
||||
You can let 🤗 Accelerate handle the device map computation by setting `device_map` to one of the supported options (`"auto"`, `"balanced"`, `"balanced_low_0"`, `"sequential"`) or create one yourself, if you want more control over where each layer should go.
|
||||
You can let 🤗 Accelerate handle the device map computation by setting `device_map` to one of the supported options (`"auto"`, `"balanced"`, `"balanced_low_0"`, `"sequential"`) or create one yourself if you want more control over where each layer should go.
|
||||
|
||||
<Tip>
|
||||
|
||||
@ -221,7 +235,7 @@ You can let 🤗 Accelerate handle the device map computation by setting `device
|
||||
|
||||
All the options will produce the same result when you don't have enough GPU memory to accommodate the whole model (which is to fit everything that can on the GPU, then offload weights on the CPU or even on the disk if there is not enough RAM).
|
||||
|
||||
When you have more GPU memory available than the model size, here the difference between each option:
|
||||
When you have more GPU memory available than the model size, here is the difference between each option:
|
||||
- `"auto"` and `"balanced"` evenly split the model on all available GPUs, making it possible for you to use a batch size greater than 1.
|
||||
- `"balanced_low_0"` evenly splits the model on all GPUs except the first one, and only puts on GPU 0 what does not fit on the others. This option is great when you need to use GPU 0 for some processing of the outputs, like when using the `generate` function for Transformers models
|
||||
- `"sequential"` will fit what it can on GPU 0, then move on GPU 1 and so forth (so won't use the last GPUs if it doesn't need to).
|
||||
@ -232,9 +246,9 @@ When you have more GPU memory available than the model size, here the difference
|
||||
|
||||
</Tip>
|
||||
|
||||
First note that you can limit the memory used on each GPU by using the `max_memory` argument (available in [`infer_auto_device_map`] and in all functions using it). When setting `max_memory`, you should pass along a dictionary containing the GPU identifiers (for instance `0`, `1` etc.) and the `"cpu"` key for the maximum RAM you want used for CPU offload. The values can either be an integer (in bytes) or a string representing a number with its unit, such as `"10GiB"` or `"10GB"`.
|
||||
First note that you can limit the memory used on each GPU by using the `max_memory` argument (available in [`infer_auto_device_map`] and in all functions using it). When setting `max_memory`, you should pass along a dictionary containing the GPU identifiers (for instance `0`, `1` etc.) and the `"cpu"` key for the maximum RAM you want to use for CPU offload. The values can either be an integer (in bytes) or a string representing a number with its unit, such as `"10GiB"` or `"10GB"`.
|
||||
|
||||
Here is an example where we don't want to use more than 10GiB on each of two GPUs and no more than 30GiB of CPU RAM for the model weights:
|
||||
Here is an example where we don't want to use more than 10GiB on each of the two GPUs and no more than 30GiB of CPU RAM for the model weights:
|
||||
|
||||
```python
|
||||
from accelerate import infer_auto_device_map
|
||||
@ -246,18 +260,18 @@ device_map = infer_auto_device_map(my_model, max_memory={0: "10GiB", 1: "10GiB",
|
||||
|
||||
When a first allocation happens in PyTorch, it loads CUDA kernels which take about 1-2GB of memory depending on the GPU. Therefore you always have less usable memory than the actual size of the GPU. To see how much memory is actually used do `torch.ones(1).cuda()` and look at the memory usage.
|
||||
|
||||
Therefore when you create memory maps with `max_memory` make sure to adjust the avaialble memory accordingly to avoid out-of-memory errors.
|
||||
Therefore when you create memory maps with `max_memory` make sure to adjust the available memory accordingly to avoid out-of-memory errors.
|
||||
|
||||
</Tip>
|
||||
|
||||
Additionally, if you do some additional operations with your outputs without placing them back on the CPU (for instance inside the `generate` method of Transformers) and if you placed your inputs on a GPU, that GPU will consume more memory than the others (Accelerate always place the output back to the device of the input). Therefore if you would like to optimize the maximum batch size and you have many GPUs, give the first GPU less memory. For example, with BLOOM-176B on 8x80 A100 setup the close to ideal map is:
|
||||
Additionally, if you do some additional operations with your outputs without placing them back on the CPU (for instance inside the `generate` method of Transformers) and if you placed your inputs on a GPU, that GPU will consume more memory than the others (Accelerate always place the output back to the device of the input). Therefore if you would like to optimize the maximum batch size and you have many GPUs, give the first GPU less memory. For example, with BLOOM-176B on 8x80 A100 setup, the close-to-ideal map is:
|
||||
|
||||
```python
|
||||
max_memory = {0: "30GIB", 1: "46GIB", 2: "46GIB", 3: "46GIB", 4: "46GIB", 5: "46GIB", 6: "46GIB", 7: "46GIB"}
|
||||
```
|
||||
as you can see we gave the remaining 7 GPUs ~50% more memory than GPU 0.
|
||||
|
||||
If you opt to fully design the `device_map` yourself, it should be a dictionary with keys being module names of your model and values being a valid device identifier (for instance an integer for the GPUs) or `"cpu"` for CPU offload, `"disk"` for disk offload. The keys need to cover the whole model, you can then define your device map as you wish: for instance if your model has two blocks (let's say `block1` and `block2`) which each contain three linear layers (let's say `linear1`, `linear2` and `linear3`), a valid device map can be:
|
||||
If you opt to fully design the `device_map` yourself, it should be a dictionary with keys being module names of your model and values being a valid device identifier (for instance an integer for the GPUs) or `"cpu"` for CPU offload, `"disk"` for disk offload. The keys need to cover the whole model, you can then define your device map as you wish: for instance, if your model has two blocks (let's say `block1` and `block2`) which each contain three linear layers (let's say `linear1`, `linear2` and `linear3`), a valid device map can be:
|
||||
|
||||
```python
|
||||
device_map = {"block1": 0, "block2": 1}
|
||||
@ -286,7 +300,7 @@ device_map = {"block1": 0, "block2.linear1": 1, "block2.linear2": 1}
|
||||
We are aware of the current limitations in the API:
|
||||
|
||||
- While this could theoretically work on just one CPU with potential disk offload, you need at least one GPU to run this API. This will be fixed in further development.
|
||||
- [`infer_auto_device_map`] (or `device_map="auto"` in [`load_checkpoint_and_dispatch`]) tries to maximize GPU and CPU RAM it sees available when you execute it. While PyTorch is very good at managing GPU RAM efficiently (and giving it back when not needed), it's not entirely true with Python and CPU RAM. Therefore, an automatically computed device map might be too intense on the CPU. Move a few modules to the disk device if you get crashes due to lack of RAM.
|
||||
- [`infer_auto_device_map`] (or `device_map="auto"` in [`load_checkpoint_and_dispatch`]) tries to maximize GPU and CPU RAM it sees available when you execute it. While PyTorch is very good at managing GPU RAM efficiently (and giving it back when not needed), it's not entirely true with Python and CPU RAM. Therefore, an automatically computed device map might be too intense on the CPU. Move a few modules to the disk device if you get crashes due to a lack of RAM.
|
||||
- [`infer_auto_device_map`] (or `device_map="auto"` in [`load_checkpoint_and_dispatch`]) attributes devices sequentially (to avoid moving things back and forth) so if your first layer is bigger than the size of the GPU you have, it will end up with everything on the CPU/Disk.
|
||||
- [`load_checkpoint_and_dispatch`] and [`load_checkpoint_in_model`] do not perform any check on the correctness of your state dict compared to your model at the moment (this will be fixed in a future version), so you may get some weird errors if trying to load a checkpoint with mismatched or missing keys.
|
||||
- The model parallelism used when your model is split on several GPUs is naive and not optimized, meaning that only one GPU works at a given time and the other sits idle.
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Deferring Executions
|
||||
@ -27,7 +30,7 @@ accelerator.wait_for_everyone()
|
||||
This instruction will block all the processes that arrive first until all the other processes have reached that
|
||||
point (if you run your script on just one GPU or CPU, this won't do anything).
|
||||
|
||||
A few example cases for when to use this utility are listed below:
|
||||
A few example cases of when to use this utility are listed below:
|
||||
|
||||
<Tip>
|
||||
|
||||
@ -38,7 +41,7 @@ A few example cases for when to use this utility are listed below:
|
||||
|
||||
## Downloading a Dataset
|
||||
|
||||
When downloading a dataset, you should download it first on the main process and then loading the cached dataset in afterwards
|
||||
When downloading a dataset, you should download it first on the main process and then load the cached dataset afterward
|
||||
|
||||
<Tip>
|
||||
|
||||
@ -104,4 +107,24 @@ with accelerator.main_process_first():
|
||||
batched=True,
|
||||
remove_columns=["idx", "sentence1", "sentence2"],
|
||||
)
|
||||
```
|
||||
|
||||
## Applying checks such as Early Stopping
|
||||
|
||||
To have a check that works with a flag set by a particular process, the `set_trigger` and `check_trigger` API should be used. Useful examples
|
||||
for doing so can include situations such as using early stopping and monitoring the loss (as each loss slightly differs on each process).
|
||||
|
||||
Call [`Accelerator.set_trigger`] when your condition has been met, and [`Accelerator.check_trigger`] when checking if that condition has been met in any process:
|
||||
|
||||
```python
|
||||
for (x,y) in data_loader:
|
||||
logits = model(x)
|
||||
loss = loss_func(logits, y)
|
||||
# Assume `should_do_early_stopping` is a custom defined function that returns a conditional
|
||||
if should_do_early_stopping(loss):
|
||||
accelerator.set_trigger()
|
||||
|
||||
# Later in the training script when we need to check for the breakpoint
|
||||
if accelerator.check_trigger():
|
||||
break
|
||||
```
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Gradient Synchronization
|
||||
@ -45,22 +48,30 @@ training in a distributed setup. But how does this risk slowing down your code?
|
||||
In DDP (distributed data parallel), the specific order in which processes are performed and ran are expected
|
||||
at specific points and these must also occur at roughly the same time before moving on.
|
||||
|
||||
The most direct example is when you update all of the parameters in a model through `.backward()`. All instances of the model
|
||||
need to have updated their gradients, collated, and updated again before moving onto the next batch of data. But when performing
|
||||
gradient accumulation, you accumulate `n` losses and skip `.backward()` until `n` batches have been reached. This
|
||||
can cause a significant slowdown since all the processes need to communicate with them more times than needed. How
|
||||
can you avoid this overhead?
|
||||
The most direct example is when you update model parameters through
|
||||
`optimizer.step()`.
|
||||
Without gradient accumulation, all instances of the model need to have updated
|
||||
their gradients computed, collated, and updated before moving on to the next
|
||||
batch of data.
|
||||
When performing gradient accumulation, you accumulate `n` loss gradients and
|
||||
skip `optimizer.step()` until `n` batches have been reached. As all training
|
||||
processes only need to sychronize by the time `optimizer.step()` is called,
|
||||
without any modification to your training step, this neededless inter-process
|
||||
communication can cause a significant slowdown.
|
||||
|
||||
How can you avoid this overhead?
|
||||
|
||||
## Solving the slowdown problem
|
||||
|
||||
Since you are skipping these batches, their gradients do not need to be synchronized until the point where `.backward()` is actually called.
|
||||
Since you are skipping model parameter updates when training on these batches, their gradients do not need to be synchronized until the point where `optimizer.step()` is actually called.
|
||||
PyTorch cannot automagically tell when you need to do this, but they do provide a tool to help through the [`no_sync`](https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html#torch.nn.parallel.DistributedDataParallel.no_sync) context manager
|
||||
that is added to your model after converting it to DDP.
|
||||
|
||||
Under this context manager, PyTorch will skip synchronizing the gradients when `.backward()` is called, and the first call to `.backward()` outside this
|
||||
Under this context manager, PyTorch will skip synchronizing the gradients when
|
||||
`.backward()` is called, and the first call to `.backward()` outside this
|
||||
context manager will trigger the synchronization. See an example below:
|
||||
```python
|
||||
ddp_model, dataloader = accelerator.prepare(model, dataloader)
|
||||
ddp_model, dataloader, optimizer = accelerator.prepare(model, dataloader, optimizer)
|
||||
|
||||
for index, batch in enumerate(dataloader):
|
||||
inputs, targets = batch
|
||||
@ -76,13 +87,14 @@ for index, batch in enumerate(dataloader):
|
||||
outputs = ddp_model(inputs)
|
||||
loss = loss_func(outputs)
|
||||
accelerator.backward(loss)
|
||||
optimizer.step()
|
||||
```
|
||||
|
||||
In 🤗 Accelerate to make this an API that can be called no matter the training device (though it may not do anything if you are not in a distributed system!),
|
||||
`ddp_model.no_sync` gets replaced with [`~Accelerator.no_sync`] and operates the same way:
|
||||
|
||||
```diff
|
||||
ddp_model, dataloader = accelerator.prepare(model, dataloader)
|
||||
ddp_model, dataloader, optimizer = accelerator.prepare(model, dataloader, optimizer)
|
||||
|
||||
for index, batch in enumerate(dataloader):
|
||||
inputs, targets = batch
|
||||
@ -99,13 +111,15 @@ In 🤗 Accelerate to make this an API that can be called no matter the training
|
||||
outputs = ddp_model(inputs)
|
||||
loss = loss_func(outputs)
|
||||
accelerator.backward(loss)
|
||||
optimizer.step()
|
||||
optimizer.zero_grad()
|
||||
```
|
||||
|
||||
As you may expect, the [`~Accelerator.accumulate`] function wraps around this conditional check by keeping track of the current batch number, leaving you with the final
|
||||
gradient accumulation API:
|
||||
|
||||
```python
|
||||
ddp_model, dataloader = accelerator.prepare(model, dataloader)
|
||||
ddp_model, dataloader, optimizer = accelerator.prepare(model, dataloader, optimizer)
|
||||
|
||||
for batch in dataloader:
|
||||
with accelerator.accumulate(model):
|
||||
@ -114,13 +128,15 @@ for batch in dataloader:
|
||||
outputs = model(inputs)
|
||||
loss = loss_function(outputs, targets)
|
||||
accelerator.backward(loss)
|
||||
optimizer.step()
|
||||
optimizer.zero_grad()
|
||||
```
|
||||
|
||||
As a result, you should either use *`accelerator.accumulate` or `accelerator.no_sync`* when it comes to API choice.
|
||||
|
||||
## Just how much of a slowdown is there, and easy mistakes you can make
|
||||
|
||||
To setup a realistic example, consider the following setup:
|
||||
To set up a realistic example, consider the following setup:
|
||||
|
||||
* Two single-GPU T4 nodes and one node with two GPUs
|
||||
* Each GPU is a T4, and are hosted on GCP
|
||||
@ -130,7 +146,7 @@ To setup a realistic example, consider the following setup:
|
||||
All scripts are available in [this repository](https://github.com/muellerzr/timing_experiments).
|
||||
|
||||
If not careful about gradient synchronization and GPU communication, a *large* amount of time can be wasted
|
||||
from when these GPUs communicate to each other during unnessisary periods.
|
||||
from when these GPUs communicate to each other during unnecessary periods.
|
||||
|
||||
By how much?
|
||||
|
||||
@ -147,7 +163,7 @@ Below are the average seconds per batch iterating over 29 batches of data for ea
|
||||
| Multi-Node | 2±0.01s | 2.13±0.08s | **0.91±0.11s** | **0.91±0.11s** |
|
||||
| Single Node | 0.50±0.01s | 0.50±0.01s | **0.41±0.015s** | **0.41±0.015s** |
|
||||
|
||||
As you can see, if you are not careful about how you setup your gradient synchronization, you can get upwards of more than a 2x slowdown during training!
|
||||
As you can see, if you are not careful about how you set up your gradient synchronization, you can get upwards of more than a 2x slowdown during training!
|
||||
|
||||
If you are worried about making sure everything is done properly, we highly recommend utilizing the [`~Accelerator.accumulate`] function and passing in
|
||||
`gradient_accumulation_steps` to the [`Accelerator`] object so Accelerate can handle this for you.
|
||||
`gradient_accumulation_steps` or `gradient_accumulation_plugin` to the [`Accelerator`] object so Accelerate can handle this for you.
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Comparing performance between different device setups
|
||||
@ -18,7 +21,7 @@ and expect your results to line up.
|
||||
|
||||
But why?
|
||||
|
||||
There's three reasons for this that this tutorial will cover:
|
||||
There are three reasons for this that this tutorial will cover:
|
||||
|
||||
1. **Setting the right seeds**
|
||||
2. **Observed Batch Sizes**
|
||||
@ -26,7 +29,7 @@ There's three reasons for this that this tutorial will cover:
|
||||
|
||||
## Setting the Seed
|
||||
|
||||
While this issue has not come up as much, make sure to use [`utils.set_seed`] to fully set the seed in all distributed cases so training will be reproducable:
|
||||
While this issue has not come up as much, make sure to use [`utils.set_seed`] to fully set the seed in all distributed cases so training will be reproducible:
|
||||
|
||||
```python
|
||||
from accelerate.utils import set_seed
|
||||
@ -58,7 +61,7 @@ The below table can be used as a quick reference to try out different batch size
|
||||
|
||||
<Tip>
|
||||
|
||||
In this example there are two GPUs for "Multi-GPU" and a TPU pod with 8 workers
|
||||
In this example, there are two GPUs for "Multi-GPU" and a TPU pod with 8 workers
|
||||
|
||||
</Tip>
|
||||
|
||||
@ -90,5 +93,11 @@ optimizer = AdamW(params=model.parameters(), lr=learning_rate)
|
||||
```
|
||||
|
||||
You will also find that `accelerate` will step the learning rate based on the number of processes being trained on. This is because
|
||||
of the observed batch size noted earlier. So in a case of 2 GPUs, the learning rate will be stepped twice as often as a single GPU
|
||||
of the observed batch size noted earlier. So in the case of 2 GPUs, the learning rate will be stepped twice as often as a single GPU
|
||||
to account for the batch size being twice as large (if no changes to the batch size on the single GPU instance are made).
|
||||
|
||||
## Gradient Accumulation and Mixed Precision
|
||||
|
||||
When using gradient accumulation and mixed precision, due to how gradient averaging works (accumulation) and the precision loss (mixed precision),
|
||||
some degradation in performance is expected. This will be explicitly seen when comparing the batch-wise loss between different compute
|
||||
setups. However, the overall loss, metric, and general performance at the end of training should be _roughly_ the same.
|
||||
@ -8,11 +8,14 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Training on TPUs with 🤗 Accelerate
|
||||
|
||||
Training on TPUs can be slightly different than training on multi-gpu, even with 🤗 Accelerate. This guide aims to show you
|
||||
Training on TPUs can be slightly different from training on multi-gpu, even with 🤗 Accelerate. This guide aims to show you
|
||||
where you should be careful and why, as well as the best practices in general.
|
||||
|
||||
## Training in a Notebook
|
||||
@ -24,8 +27,8 @@ While on a TPU that last part is not as important, a critical part to understand
|
||||
When launching from the command-line, you perform **spawning**, where a python process is not currently running and you *spawn* a new process in. Since your Jupyter notebook is already
|
||||
utilizing a python process, you need to *fork* a new process from it to launch your code.
|
||||
|
||||
Where this becomes important is in regards to declaring your model. On forked TPU processes, it is recommended that you instantiate your model *once* and pass this into your
|
||||
training function. This is different than training on GPUs where you create `n` models that have their gradients synced and back-propagated at certain moments. Instead one
|
||||
Where this becomes important is in regard to declaring your model. On forked TPU processes, it is recommended that you instantiate your model *once* and pass this into your
|
||||
training function. This is different than training on GPUs where you create `n` models that have their gradients synced and back-propagated at certain moments. Instead, one
|
||||
model instance is shared between all the nodes and it is passed back and forth. This is important especially when training on low-resource TPUs such as those provided in Kaggle kernels or
|
||||
on Google Colaboratory.
|
||||
|
||||
@ -134,7 +137,7 @@ At the base level, this is enabled when passing `mixed_precision="bf16"` to `Acc
|
||||
```python
|
||||
accelerator = Accelerator(mixed_precision="bf16")
|
||||
```
|
||||
By default this will cast `torch.float` and `torch.double` to `bfloat16` on TPUs.
|
||||
By default, this will cast `torch.float` and `torch.double` to `bfloat16` on TPUs.
|
||||
The specific configuration being set is an environmental variable of `XLA_USE_BF16` is set to `1`.
|
||||
|
||||
There is a further configuration you can perform which is setting the `XLA_DOWNCAST_BF16` environmental variable. If set to `1`, then
|
||||
@ -161,4 +164,4 @@ new batch size after the first few iterations.
|
||||
|
||||
Just because the memory is allocated does not mean it will be used or that the batch size will increase when going back to your training dataloader.
|
||||
|
||||
</Tip>
|
||||
</Tip>
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Accelerate
|
||||
@ -8,12 +8,15 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Accelerator
|
||||
|
||||
The [`Accelerator`] is the main class provided by 🤗 Accelerate.
|
||||
It serves at the main entrypoint for the API.
|
||||
It serves at the main entry point for the API.
|
||||
|
||||
## Quick adaptation of your code
|
||||
|
||||
@ -45,7 +48,7 @@ you should search for and replace by the corresponding methods of your `accelera
|
||||
|
||||
### Printing
|
||||
|
||||
`print` statements should be replaced by [`~Accelerator.print`] to be printed once per process
|
||||
`print` statements should be replaced by [`~Accelerator.print`] to be printed once per process:
|
||||
|
||||
```diff
|
||||
- print("My thing I want to print!")
|
||||
@ -113,25 +116,55 @@ def do_my_thing():
|
||||
|
||||
### Synchronicity control
|
||||
|
||||
Use [`~Accelerator.wait_for_everyone`] to make sure all processes join that point before continuing. (Useful before a model save for instance)
|
||||
Use [`~Accelerator.wait_for_everyone`] to make sure all processes join that point before continuing. (Useful before a model save for instance).
|
||||
|
||||
### Saving and loading
|
||||
|
||||
Use [`~Accelerator.unwrap_model`] before saving to remove all special model wrappers added during the distributed process.
|
||||
|
||||
```python
|
||||
model = MyModel()
|
||||
model = accelerator.prepare(model)
|
||||
# Unwrap
|
||||
model = accelerator.unwrap_model(model)
|
||||
```
|
||||
|
||||
Use [`~Accelerator.save`] instead of `torch.save`:
|
||||
Use [`~Accelerator.save_model`] instead of `torch.save` to save a model. It will remove all model wrappers added during the distributed process, get the state_dict of the model and save it. The state_dict will be in the same precision as the model being trained.
|
||||
|
||||
```diff
|
||||
state_dict = model.state_dict()
|
||||
- torch.save(state_dict, "my_state.pkl")
|
||||
+ accelerator.save(state_dict, "my_state.pkl")
|
||||
+ accelerator.save_model(model, save_directory)
|
||||
```
|
||||
|
||||
[`~Accelerator.save_model`] can also save a model into sharded checkpoints or with safetensors format.
|
||||
Here is an example:
|
||||
|
||||
```python
|
||||
accelerator.save_model(model, save_directory, max_shard_size="1GB", safe_serialization=True)
|
||||
```
|
||||
|
||||
#### 🤗 Transformers models
|
||||
|
||||
If you are using models from the [🤗 Transformers](https://huggingface.co/docs/transformers/) library, you can use the `.save_pretrained()` method.
|
||||
|
||||
```python
|
||||
from transformers import AutoModel
|
||||
|
||||
model = AutoModel.from_pretrained("bert-base-cased")
|
||||
model = accelerator.prepare(model)
|
||||
|
||||
# ...fine-tune with PyTorch...
|
||||
|
||||
unwrapped_model = accelerator.unwrap_model(model)
|
||||
unwrapped_model.save_pretrained(
|
||||
"path/to/my_model_directory",
|
||||
is_main_process=accelerator.is_main_process,
|
||||
save_function=accelerator.save,
|
||||
)
|
||||
```
|
||||
|
||||
This will ensure your model stays compatible with other 🤗 Transformers functionality like the `.from_pretrained()` method.
|
||||
|
||||
```python
|
||||
from transformers import AutoModel
|
||||
|
||||
model = AutoModel.from_pretrained("path/to/my_model_directory")
|
||||
```
|
||||
|
||||
### Operations
|
||||
@ -157,7 +190,22 @@ multi-device training, check if the step should actually be performed, and auto-
|
||||
scheduler.step()
|
||||
optimizer.zero_grad()
|
||||
```
|
||||
#### GradientAccumulationPlugin
|
||||
[[autodoc]] utils.GradientAccumulationPlugin
|
||||
|
||||
|
||||
Instead of passing `gradient_accumulation_steps` you can instantiate a GradientAccumulationPlugin and pass it to the [`Accelerator`]'s `__init__`
|
||||
as `gradient_accumulation_plugin`. You can only pass either one of `gradient_accumulation_plugin` or `gradient_accumulation_steps` passing both will raise an error.
|
||||
```diff
|
||||
from accelerate.utils import GradientAccumulationPlugin
|
||||
|
||||
gradient_accumulation_plugin = GradientAccumulationPlugin(num_steps=2)
|
||||
- accelerator = Accelerator()
|
||||
+ accelerator = Accelerator(gradient_accumulation_plugin=gradient_accumulation_plugin)
|
||||
```
|
||||
|
||||
In addition to the number of steps, this also lets you configure whether or not you adjust your learning rate scheduler to account for the change in steps due to accumulation.
|
||||
|
||||
## Overall API documentation:
|
||||
|
||||
[[autodoc]] Accelerator
|
||||
[[autodoc]] Accelerator
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Working with large models
|
||||
@ -19,6 +22,8 @@ specific language governing permissions and limitations under the License.
|
||||
[[autodoc]] big_modeling.disk_offload
|
||||
[[autodoc]] big_modeling.dispatch_model
|
||||
[[autodoc]] big_modeling.load_checkpoint_and_dispatch
|
||||
[[autodoc]] big_modeling.load_checkpoint_in_model
|
||||
[[autodoc]] utils.infer_auto_device_map
|
||||
|
||||
## Model Hooks
|
||||
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# The Command Line
|
||||
@ -83,7 +86,7 @@ accelerate config update [arguments]
|
||||
|
||||
**Command**:
|
||||
|
||||
`accelerate env` or `accelerate-env`
|
||||
`accelerate env` or `accelerate-env` or `python -m accelerate.commands.env`
|
||||
|
||||
Lists the contents of the passed 🤗 Accelerate configuration file. Should always be used when opening an issue on the [GitHub repository](https://github.com/huggingface/accelerate).
|
||||
|
||||
@ -103,7 +106,7 @@ accelerate env [arguments]
|
||||
|
||||
**Command**:
|
||||
|
||||
`accelerate launch` or `accelerate-launch`
|
||||
`accelerate launch` or `accelerate-launch` or `python -m accelerate.commands.launch`
|
||||
|
||||
Launches a specified script on a distributed system with the right parameters.
|
||||
|
||||
@ -135,8 +138,8 @@ values. They can also be passed in manually.
|
||||
|
||||
* `--cpu` (`bool`) -- Whether or not to force the training on the CPU.
|
||||
* `--multi_gpu` (`bool`) -- Whether or not this should launch a distributed GPU training.
|
||||
* `--mps` (`bool`) -- Whether or not this should use MPS-enabled GPU device on MacOS machines.
|
||||
* `--tpu` (`bool`) -- Whether or not this should launch a TPU training.
|
||||
* `--ipex` (`bool`) -- Whether or not this should launch an Intel Pytorch Extension (IPEX) training.
|
||||
|
||||
**Resource Selection Arguments**:
|
||||
|
||||
@ -154,6 +157,7 @@ The following arguments are useful for selecting which training paradigm to use.
|
||||
* `--use_deepspeed` (`bool`) -- Whether or not to use DeepSpeed for training.
|
||||
* `--use_fsdp` (`bool`) -- Whether or not to use FullyShardedDataParallel for training.
|
||||
* `--use_megatron_lm` (`bool`) -- Whether or not to use Megatron-LM for training.
|
||||
* `--use_xpu` (`bool`) -- Whether to use IPEX plugin to speed up training on XPU specifically.
|
||||
|
||||
**Distributed GPU Arguments**:
|
||||
|
||||
@ -164,6 +168,7 @@ The following arguments are only useful when `multi_gpu` is passed or multi-gpu
|
||||
* `--machine_rank MACHINE_RANK` (`int`) -- The rank of the machine on which this script is launched.
|
||||
* `--main_process_ip MAIN_PROCESS_IP` (`str`) -- The IP address of the machine of rank 0.
|
||||
* `--main_process_port MAIN_PROCESS_PORT` (`int`) -- The port to use to communicate with the machine of rank 0.
|
||||
* `--rdzv_backend` (`str`) -- The rendezvous method to use, such as "static" or "c10d"
|
||||
* `--rdzv_conf` (`str`) -- Additional rendezvous configuration (<key1>=<value1>,<key2>=<value2>,...).
|
||||
* `--max_restarts` (`int`) -- Maximum number of worker group restarts before failing.
|
||||
* `--monitor_interval` (`float`) -- Interval, in seconds, to monitor the state of workers.
|
||||
@ -223,6 +228,36 @@ The following arguments are only useful when training in SageMaker
|
||||
* `--aws_access_key_id AWS_ACCESS_KEY_ID` (`str`) -- The AWS_ACCESS_KEY_ID used to launch the Amazon SageMaker training job
|
||||
* `--aws_secret_access_key AWS_SECRET_ACCESS_KEY` (`str`) -- The AWS_SECRET_ACCESS_KEY used to launch the Amazon SageMaker training job
|
||||
|
||||
## accelerate estimate-memory
|
||||
|
||||
**Command**:
|
||||
|
||||
`accelerate estimate-memory` or `accelerate-estimate-memory` or `python -m accelerate.commands.estimate`
|
||||
|
||||
Estimates the total vRAM a particular model hosted on the Hub needs to be loaded in with an estimate for training. Requires that `huggingface_hub` be installed.
|
||||
|
||||
<Tip>
|
||||
|
||||
When performing inference, typically add ≤20% to the result as overall allocation [as referenced here](https://blog.eleuther.ai/transformer-math/). We will have more extensive estimations in the future that will automatically be included in the calculation.
|
||||
|
||||
</Tip>
|
||||
|
||||
**Usage**:
|
||||
|
||||
```bash
|
||||
accelerate estimate-memory {MODEL_NAME} --library_name {LIBRARY_NAME} --dtypes {dtype_1} {dtype_2} ...
|
||||
```
|
||||
|
||||
**Required Arguments**:
|
||||
|
||||
* `MODEL_NAME` (`str`)-- The model name on the Hugging Face Hub
|
||||
|
||||
**Optional Arguments**:
|
||||
|
||||
* `--library_name {timm,transformers}` (`str`) -- The library the model has an integration with, such as `transformers`, needed only if this information is not stored on the Hub
|
||||
* `--dtypes {float32,float16,int8,int4}` (`[{float32,float16,int8,int4} ...]`) -- The dtypes to use for the model, must be one (or many) of `float32`, `float16`, `int8`, and `int4`
|
||||
* `--trust_remote_code` (`bool`) -- Whether or not to allow for custom models defined on the Hub in their own modeling files. This option should only be passed for repositories you trust and in which you have read the code, as it will execute code present on the Hub on your local machine.
|
||||
|
||||
## accelerate tpu-config
|
||||
|
||||
`accelerate tpu-config`
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Utilities for DeepSpeed
|
||||
18
docs/source/package_reference/fsdp.md
Normal file
18
docs/source/package_reference/fsdp.md
Normal file
@ -0,0 +1,18 @@
|
||||
<!--Copyright 2023 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Utilities for Fully Sharded Data Parallelism
|
||||
|
||||
[[autodoc]] utils.FullyShardedDataParallelPlugin
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Kwargs Handlers
|
||||
@ -15,11 +18,18 @@ specific language governing permissions and limitations under the License.
|
||||
The following objects can be passed to the main [`Accelerator`] to customize how some PyTorch objects
|
||||
related to distributed training or mixed precision are created.
|
||||
|
||||
## AutocastKwargs
|
||||
|
||||
[[autodoc]] AutocastKwargs
|
||||
|
||||
## DistributedDataParallelKwargs
|
||||
|
||||
[[autodoc]] DistributedDataParallelKwargs
|
||||
|
||||
## FP8RecipeKwargs
|
||||
|
||||
[[autodoc]] utils.FP8RecipeKwargs
|
||||
|
||||
## GradScalerKwargs
|
||||
|
||||
[[autodoc]] GradScalerKwargs
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Launchers
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Logging with Accelerate
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Utilities for Megatron-LM
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Stateful Classes
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Wrapper classes for torch Dataloaders, Optimizers, and Schedulers
|
||||
@ -18,6 +21,7 @@ when calling [`~Accelerator.prepare`].
|
||||
## Datasets and DataLoaders
|
||||
|
||||
[[autodoc]] data_loader.prepare_data_loader
|
||||
[[autodoc]] data_loader.skip_first_batches
|
||||
|
||||
[[autodoc]] data_loader.BatchSamplerShard
|
||||
[[autodoc]] data_loader.IterableDatasetShard
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Experiment Tracking
|
||||
@ -8,24 +8,64 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Helpful Utilities
|
||||
|
||||
Below are a variety of utility functions that 🤗 Accelerate provides, broken down by use-case.
|
||||
|
||||
## Constants
|
||||
|
||||
Constants used throughout 🤗 Accelerate for reference
|
||||
|
||||
The following are constants used when utilizing [`Accelerator.save_state`]
|
||||
|
||||
`utils.MODEL_NAME`: `"pytorch_model"`
|
||||
`utils.OPTIMIZER_NAME`: `"optimizer"`
|
||||
`utils.RNG_STATE_NAME`: `"random_states"`
|
||||
`utils.SCALER_NAME`: `"scaler.pt`
|
||||
`utils.SCHEDULER_NAME`: `"scheduler`
|
||||
|
||||
The following are constants used when utilizing [`Accelerator.save_model`]
|
||||
|
||||
`utils.WEIGHTS_NAME`: `"pytorch_model.bin"`
|
||||
`utils.SAFE_WEIGHTS_NAME`: `"model.safetensors"`
|
||||
`utils.WEIGHTS_INDEX_NAME`: `"pytorch_model.bin.index.json"`
|
||||
`utils.SAFE_WEIGHTS_INDEX_NAME`: `"model.safetensors.index.json"`
|
||||
|
||||
## Data Classes
|
||||
|
||||
These are basic dataclasses used throughout 🤗 Accelerate and they can be passed in as parameters.
|
||||
|
||||
[[autodoc]] utils.DistributedType
|
||||
|
||||
[[autodoc]] utils.DynamoBackend
|
||||
|
||||
[[autodoc]] utils.LoggerType
|
||||
|
||||
[[autodoc]] utils.PrecisionType
|
||||
|
||||
[[autodoc]] utils.ProjectConfiguration
|
||||
|
||||
## Plugins
|
||||
|
||||
These are plugins that can be passed to the [`Accelerator`] object. While they are defined elsewhere in the documentation,
|
||||
for convience all of them are available to see here:
|
||||
|
||||
[[autodoc]] utils.DeepSpeedPlugin
|
||||
|
||||
[[autodoc]] utils.FullyShardedDataParallelPlugin
|
||||
|
||||
[[autodoc]] utils.GradientAccumulationPlugin
|
||||
|
||||
[[autodoc]] utils.MegatronLMPlugin
|
||||
|
||||
[[autodoc]] utils.TorchDynamoPlugin
|
||||
|
||||
|
||||
## Data Manipulation and Operations
|
||||
|
||||
These include data operations that mimic the same `torch` ops but can be used on distributed processes.
|
||||
@ -48,11 +88,23 @@ These functionalities check the state of the current working environment includi
|
||||
|
||||
[[autodoc]] utils.is_bf16_available
|
||||
|
||||
[[autodoc]] utils.is_ipex_available
|
||||
|
||||
[[autodoc]] utils.is_mps_available
|
||||
|
||||
[[autodoc]] utils.is_npu_available
|
||||
|
||||
[[autodoc]] utils.is_torch_version
|
||||
|
||||
[[autodoc]] utils.is_tpu_available
|
||||
|
||||
## Environment Configuration
|
||||
[[autodoc]] utils.is_xpu_available
|
||||
|
||||
## Environment Manipulation
|
||||
|
||||
[[autodoc]] utils.patch_environment
|
||||
|
||||
[[autodoc]] utils.clear_environment
|
||||
|
||||
[[autodoc]] utils.write_basic_config
|
||||
|
||||
@ -102,3 +154,17 @@ These utilities relate to setting and synchronizing of all the random states.
|
||||
These include utilities that are useful while using PyTorch with XLA.
|
||||
|
||||
[[autodoc]] utils.install_xla
|
||||
|
||||
## Loading model weights
|
||||
|
||||
These include utilities that are useful to load checkpoints.
|
||||
|
||||
[[autodoc]] utils.load_checkpoint_in_model
|
||||
|
||||
## Quantization
|
||||
|
||||
These include utilities that are useful to quantize model.
|
||||
|
||||
[[autodoc]] utils.load_and_quantize_model
|
||||
|
||||
[[autodoc]] utils.BnbQuantizationConfig
|
||||
@ -8,17 +8,27 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Quick tour
|
||||
|
||||
Let's have a look at the 🤗 Accelerate main features and traps to avoid.
|
||||
This guide aims to help you get started with 🤗 Accelerate quickly. It covers the essential steps you need to take to
|
||||
enable distributed training, as well as the adjustments that you need to make in some common scenarios.
|
||||
|
||||
## Main use
|
||||
To help you navigate, the guide is split into two sections:
|
||||
* [Getting Started with 🤗 Accelerate](#getting-started-with--accelerate): start here to learn how to modify your script to enable distributed training with 🤗 Accelerate
|
||||
* [Common adaptations to the base case](#common-adaptations-to-the-base-case): check out this section for common deviations from the baseline scenario and what adjustments may need to be made to support them.
|
||||
|
||||
To use 🤗 Accelerate in your own script, you have to change four things:
|
||||
## Getting started with 🤗 Accelerate
|
||||
|
||||
1. Import the [`Accelerator`] main class and instantiate one in an `accelerator` object:
|
||||
### Enable distributed training in your script
|
||||
|
||||
To use 🤗 Accelerate in your own training script, you have to modify four things:
|
||||
|
||||
1. Import the [`Accelerator`] main class and instantiate one in an `accelerator` object.
|
||||
|
||||
```python
|
||||
from accelerate import Accelerator
|
||||
@ -26,27 +36,27 @@ from accelerate import Accelerator
|
||||
accelerator = Accelerator()
|
||||
```
|
||||
|
||||
This should happen as early as possible in your training script as it will initialize everything necessary for
|
||||
distributed training. You don't need to indicate the kind of environment you are in (just one machine with a GPU, one
|
||||
machines with several GPUs, several machines with multiple GPUs or a TPU), the library will detect this automatically.
|
||||
Add this at the beginning of your training script as it will initialize everything necessary for distributed training.
|
||||
You don't need to indicate the kind of environment you are in (a single machine with a GPU, a machine with several GPUs,
|
||||
or several machines with multiple GPUs or a TPU), the library will detect this automatically.
|
||||
|
||||
2. Remove the call `.to(device)` or `.cuda()` for your model and input data. The `accelerator` object
|
||||
will handle this for you and place all those objects on the right device for you. If you know what you're doing, you
|
||||
can leave those `.to(device)` calls but you should use the device provided by the `accelerator` object:
|
||||
`accelerator.device`.
|
||||
2. Remove the `.to(device)` or `.cuda()` calls for your model and input data.
|
||||
|
||||
To fully deactivate the automatic device placement, pass along `device_placement=False` when initializing your
|
||||
[`Accelerator`].
|
||||
The `accelerator` object will handle placing these objects on the right device for you.
|
||||
If you choose to leave those `.to(device)` calls, make sure to use the device provided by the `accelerator` object: `accelerator.device`.
|
||||
|
||||
<Tip warning={true}>
|
||||
|
||||
If you place your objects manually on the proper device, be careful to create your optimizer after putting your
|
||||
You can fully deactivate the automatic device placement by passing along `device_placement=False` when
|
||||
initializing [`Accelerator`].
|
||||
However, if you place your objects manually on the proper device, be careful to create your optimizer after putting your
|
||||
model on `accelerator.device` or your training will fail on TPU.
|
||||
|
||||
</Tip>
|
||||
|
||||
3. Pass all objects relevant to training (optimizer, model, training dataloader, learning rate scheduler) to the
|
||||
[`~Accelerator.prepare`] method. This will make sure everything is ready for training.
|
||||
[`~Accelerator.prepare`] method as soon as these objects are created, before starting your actual
|
||||
training loop:
|
||||
|
||||
```python
|
||||
model, optimizer, train_dataloader, lr_scheduler = accelerator.prepare(
|
||||
@ -54,58 +64,40 @@ model, optimizer, train_dataloader, lr_scheduler = accelerator.prepare(
|
||||
)
|
||||
```
|
||||
|
||||
In particular, your training dataloader will be sharded across all GPUs/TPU cores available so that each one sees a
|
||||
different portion of the training dataset. Also, the random states of all processes will be synchronized at the
|
||||
beginning of each iteration through your dataloader, to make sure the data is shuffled the same way (if you decided to
|
||||
use `shuffle=True` or any kind of random sampler).
|
||||
**Important notes**:
|
||||
|
||||
* Only pass the learning rate scheduler to [`~Accelerator.prepare`] when the scheduler needs to be stepped at each optimizer step.
|
||||
* While you can send your dataloader to [`~Accelerator.prepare`] on its own, it's best to send it to [`~Accelerator.prepare`] together with the model and optimizer.
|
||||
* If you wish to run distributed evaluation, send your validation dataloader to [`~Accelerator.prepare`] as well. There are some nuances to distributed validation, check the [Distributed evaluation](#add-distributed-evaluation) section of the guide.
|
||||
* Any instruction using your training dataloader length (for instance if you want to log the number of total training
|
||||
steps) should go after the call to [`~Accelerator.prepare`].
|
||||
|
||||
Passing these objects to the [`~Accelerator.prepare`] method ensures that your training dataloader will be sharded across
|
||||
all GPUs/TPU cores available so that each one sees a different portion of the training dataset. Also, the random states
|
||||
of all processes will be synchronized at the beginning of each iteration through your dataloader, to make sure the data
|
||||
is shuffled the same way (if you decided to use `shuffle=True` or any kind of random sampler).
|
||||
|
||||
<Tip>
|
||||
|
||||
The actual batch size for your training will be the number of devices used multiplied by the batch size you set in
|
||||
your script: for instance training on 4 GPUs with a batch size of 16 set when creating the training dataloader will
|
||||
train at an actual batch size of 64.
|
||||
|
||||
</Tip>
|
||||
|
||||
Alternatively, you can use the option `split_batches=True` when creating initializing your
|
||||
[`Accelerator`], in which case the batch size will always stay the same, whether your run your
|
||||
script on 1, 2, 4 or 64 GPUs.
|
||||
|
||||
You should execute this instruction as soon as all objects for training are created, before starting your actual
|
||||
training loop.
|
||||
|
||||
<Tip warning={true}>
|
||||
|
||||
You should only pass the learning rate scheduler to [`~Accelerator.prepare`] when the scheduler needs to be stepped
|
||||
at each optimizer step.
|
||||
|
||||
</Tip>
|
||||
|
||||
<Tip warning={true}>
|
||||
|
||||
your script. For instance, training on 4 GPUs with a batch size of 16 set when creating the training dataloader will
|
||||
train at an actual batch size of 64.
|
||||
If you want the batch size remain the same regardless of how many GPUs the script is run on, you can use the
|
||||
option `split_batches=True` when creating and initializing [`Accelerator`].
|
||||
Your training dataloader may change length when going through this method: if you run on X GPUs, it will have its
|
||||
length divided by X (since your actual batch size will be multiplied by X), unless you set
|
||||
`split_batches=True`.
|
||||
|
||||
</Tip>
|
||||
|
||||
Any instruction using your training dataloader length (for instance if you want to log the number of total training
|
||||
steps) should go after the call to [`~Accelerator.prepare`].
|
||||
|
||||
You can perfectly send your dataloader to [`~Accelerator.prepare`] on its own, but it's best to send the
|
||||
model and optimizer to [`~Accelerator.prepare`] together.
|
||||
|
||||
You may or may not want to send your validation dataloader to [`~Accelerator.prepare`], depending on
|
||||
whether you want to run distributed evaluation or not (see below).
|
||||
|
||||
4. Replace the line `loss.backward()` by `accelerator.backward(loss)`.
|
||||
4. Replace the `loss.backward()` line with `accelerator.backward(loss)`.
|
||||
|
||||
And you're all set! With all these changes, your script will run on your local machine as well as on multiple GPUs or a
|
||||
TPU! You can either use your favorite tool to launch the distributed training, or you can use the 🤗 Accelerate
|
||||
launcher.
|
||||
|
||||
|
||||
## Distributed evaluation
|
||||
### Add distributed evaluation
|
||||
|
||||
You can perform regular evaluation in your training script, if you leave your validation dataloader out of the
|
||||
[`~Accelerator.prepare`] method. In this case, you will need to put the input data on the
|
||||
@ -118,9 +110,9 @@ method:
|
||||
validation_dataloader = accelerator.prepare(validation_dataloader)
|
||||
```
|
||||
|
||||
As for your training dataloader, it will mean that (should you run your script on multiple devices) each device will
|
||||
only see part of the evaluation data. This means you will need to group your predictions together. This is very easy to
|
||||
do with the [`~Accelerator.gather_for_metrics`] method.
|
||||
Same as with your training dataloader, each device will only see part of the evaluation data should you run your script
|
||||
on multiple devices. This means you will need to group your predictions together which you can do with
|
||||
the [`~Accelerator.gather_for_metrics`] method.
|
||||
|
||||
```python
|
||||
for inputs, targets in validation_dataloader:
|
||||
@ -139,11 +131,9 @@ for inputs, targets in validation_dataloader:
|
||||
|
||||
</Tip>
|
||||
|
||||
Any instruction using your training dataloader length (for instance if you need the number of total training steps
|
||||
to create a learning rate scheduler) should go after the call to [`~Accelerator.prepare`].
|
||||
|
||||
Some data at the end of the dataset may be duplicated so the batch can be divided equally among all workers. As a result, metrics
|
||||
should be calculated through the [`~Accelerator.gather_for_metrics`] method to automatically remove the duplicated data while gathering.
|
||||
Some data at the end of the dataset may be duplicated so the batch can be divided equally among all workers. As a result,
|
||||
metrics should be calculated through the [`~Accelerator.gather_for_metrics`] method to automatically remove the duplicated
|
||||
data while gathering.
|
||||
|
||||
<Tip>
|
||||
|
||||
@ -162,36 +152,35 @@ should be calculated through the [`~Accelerator.gather_for_metrics`] method to a
|
||||
|
||||
</Tip>
|
||||
|
||||
## Launching your distributed script
|
||||
### Launch your distributed script
|
||||
|
||||
You can use the regular commands to launch your distributed training (like `torch.distributed.run` for
|
||||
PyTorch), they are fully compatible with 🤗 Accelerate.
|
||||
PyTorch) - they are fully compatible with 🤗 Accelerate.
|
||||
|
||||
🤗 Accelerate also provides a CLI tool that unifies all launchers, so you only have to remember one command. To use it,
|
||||
just run:
|
||||
Alternatively, 🤗 Accelerate provides a CLI tool that unifies all launchers, so you only have to remember one command. \
|
||||
To use it, run a quick configuration setup first on your machine and answer the questions:
|
||||
|
||||
```bash
|
||||
accelerate config
|
||||
```
|
||||
|
||||
on your machine and reply to the questions asked. This will save a *default_config.yaml* file in your cache folder for
|
||||
🤗 Accelerate. That cache folder is (with decreasing order of priority):
|
||||
At the end of the setup, a *default_config.yaml* file will be saved in your cache folder for 🤗 Accelerate. That cache
|
||||
folder is (with decreasing order of priority):
|
||||
|
||||
- The content of your environment variable `HF_HOME` suffixed with *accelerate*.
|
||||
- If it does not exist, the content of your environment variable `XDG_CACHE_HOME` suffixed with
|
||||
*huggingface/accelerate*.
|
||||
- If this does not exist either, the folder *~/.cache/huggingface/accelerate*
|
||||
- If this does not exist either, the folder *~/.cache/huggingface/accelerate*.
|
||||
|
||||
You can also specify with the flag `--config_file` the location of the file you want to save.
|
||||
|
||||
Once this is done, you can test everything is going well on your setup by running:
|
||||
By specifying the `--config_file` flag you can specify an alternative location of the configuration file.
|
||||
Once the configuration setup is complete, you can test your setup by running:
|
||||
|
||||
```bash
|
||||
accelerate test
|
||||
```
|
||||
|
||||
This will launch a short script that will test the distributed environment. If it runs fine, you are ready for the next
|
||||
step!
|
||||
This will launch a short script that will test the distributed environment. If it runs without issues, you are ready for
|
||||
the next step!
|
||||
|
||||
Note that if you specified a location for the config file in the previous step, you need to pass it here as well:
|
||||
|
||||
@ -211,19 +200,23 @@ If you stored the config file in a non-default location, you can indicate it to
|
||||
accelerate launch --config_file path_to_config.yaml path_to_script.py --args_for_the_script
|
||||
```
|
||||
|
||||
You can also override any of the arguments determined by your config file.
|
||||
To see the complete list of parameters that you can pass in, run `accelerate launch -h`.
|
||||
You can override any of the arguments determined by your config file. To see the complete list of parameters that you
|
||||
can pass in, run `accelerate launch -h`.
|
||||
|
||||
Check out the [Launch tutorial](basic_tutorials/launch) for more information about launching your scripts.
|
||||
Check out the [Launch tutorial](basic_tutorials/launch) for more information about launching your scripts.
|
||||
|
||||
## Common modifications of the base case
|
||||
|
||||
## Launching training from a notebook
|
||||
The previous section covers the minimal essential steps to move a training script into a distributed setup with 🤗 Accelerate.
|
||||
Here we describe common modifications/deviations from the base case scenario and the adjustments you need to make to accommodate for them.
|
||||
|
||||
In Accelerate 0.3.0, a new [`notebook_launcher`] has been introduced to help you launch your training
|
||||
function from a notebook. This launcher supports launching a training with TPUs on Colab or Kaggle, as well as training
|
||||
on several GPUs (if the machine on which you are running your notebook has them).
|
||||
### Launch distributed training from a notebook
|
||||
|
||||
Just define a function responsible for your whole training and/or evaluation in a cell of the notebook, then execute a
|
||||
In Accelerate 0.3.0, a new [`notebook_launcher`] has been introduced to help you launch your training function from a
|
||||
notebook. This launcher supports launching a training with TPUs on Colab or Kaggle, as well as training on several GPUs
|
||||
(if the machine on which you are running your notebook has them).
|
||||
|
||||
Define a function responsible for your whole training and/or evaluation in a cell of the notebook, then execute a
|
||||
cell with the following code:
|
||||
|
||||
```python
|
||||
@ -239,10 +232,9 @@ notebook_launcher(training_function)
|
||||
|
||||
</Tip>
|
||||
|
||||
Check out the [Notebook Launcher tutorial](basic_tutorials/notebook) for more information about training on TPUs.
|
||||
Check out the [Notebook Launcher tutorial](basic_tutorials/notebook) for more information about training on TPUs.
|
||||
|
||||
|
||||
## Training on TPU
|
||||
### Specifics of training on TPU
|
||||
|
||||
If you want to launch your script on TPUs, there are a few caveats you should be aware of. Behind the scenes, the TPUs
|
||||
will create a graph of all the operations happening in your training step (forward pass, backward pass and optimizer
|
||||
@ -281,12 +273,7 @@ passed your model to [`~Accelerator.prepare`]) will break the tying. You will ne
|
||||
after. You can find an example of this in the [run_clm_no_trainer](https://github.com/huggingface/transformers/blob/master/examples/pytorch/language-modeling/run_clm.py) script in
|
||||
the Transformers repository.
|
||||
|
||||
Check out the [TPU tutorial](concept_guides/training_tpu) for more information about training on TPUs.
|
||||
|
||||
|
||||
## Other caveats
|
||||
|
||||
We list here all smaller issues you could have in your script conversion and how to resolve them.
|
||||
Check out the [TPU tutorial](concept_guides/training_tpu) for more information about training on TPUs.
|
||||
|
||||
### Execute a statement only on one processes
|
||||
|
||||
@ -320,14 +307,14 @@ For printing statements you only want executed once per machine, you can just re
|
||||
`accelerator.print`.
|
||||
|
||||
|
||||
### Defer execution
|
||||
### Defer execution on multiple GPUs
|
||||
|
||||
When you run your usual script, instructions are executed in order. Using 🤗 Accelerate to deploy your script on several
|
||||
GPUs at the same time introduces a complication: while each process executes all instructions in order, some may be
|
||||
faster than others.
|
||||
|
||||
You might need to wait for all processes to have reached a certain point before executing a given instruction. For
|
||||
instance, you shouldn't save a model before being sure every process is done with training. To do this, just write the
|
||||
instance, you shouldn't save a model before making sure every process is done with training. To do this, add the
|
||||
following line in your code:
|
||||
|
||||
```
|
||||
@ -338,37 +325,54 @@ This instruction will block all the processes that arrive first until all the ot
|
||||
point (if you run your script on just one GPU or CPU, this won't do anything).
|
||||
|
||||
|
||||
### Saving/loading a model
|
||||
### Save/load a model in a distributed setup
|
||||
|
||||
Saving the model you trained might need a bit of adjustment: first you should wait for all processes to reach that
|
||||
point in the script as shown above, and then, you should unwrap your model before saving it. This is because when going
|
||||
through the [`~Accelerator.prepare`] method, your model may have been placed inside a bigger model,
|
||||
which deals with the distributed training. This in turn means that saving your model state dictionary without taking
|
||||
any precaution will take that potential extra layer into account, and you will end up with weights you can't load back
|
||||
in your base model.
|
||||
in your base model. The [`~Accelerator.save_model`] method will help you to achieve that. It will unwrap your model and save
|
||||
the model state dictionary.
|
||||
|
||||
This is why it's recommended to *unwrap* your model first. Here is an example:
|
||||
Here is an example:
|
||||
|
||||
```
|
||||
accelerator.wait_for_everyone()
|
||||
unwrapped_model = accelerator.unwrap_model(model)
|
||||
accelerator.save(unwrapped_model.state_dict(), filename)
|
||||
accelerator.save_model(model, save_directory)
|
||||
```
|
||||
|
||||
The [`~Accelerator.save_model`] method can also save a model into sharded checkpoints or with safetensors format:
|
||||
|
||||
```python
|
||||
accelerator.wait_for_everyone()
|
||||
accelerator.save_model(model, save_directory, max_shard_size="1GB", safe_serialization=True)
|
||||
```
|
||||
|
||||
If your script contains logic to load a checkpoint, we also recommend you load your weights in the unwrapped model
|
||||
(this is only useful if you use the load function after making your model go through
|
||||
[`~Accelerator.prepare`]). Here is an example:
|
||||
|
||||
```
|
||||
```python
|
||||
unwrapped_model = accelerator.unwrap_model(model)
|
||||
unwrapped_model.load_state_dict(torch.load(filename))
|
||||
path_to_checkpoint = os.path.join(save_directory,"pytorch_model.bin")
|
||||
unwrapped_model.load_state_dict(torch.load(path_to_checkpoint))
|
||||
```
|
||||
|
||||
Note that since all the model parameters are references to tensors, this will load your weights inside `model`.
|
||||
|
||||
## Saving/loading entire states
|
||||
If you want to load a sharded checkpoint or a checkpoint with safetensors format into the model with a specific `device`,
|
||||
we recommend you to load it with [`~utils.load_checkpoint_in_model`] function. Here's an example:
|
||||
|
||||
When training your model, you may want to save the current state of the model, optimizer, random generators, and potentially LR schedulers to be restored in the _same script_.
|
||||
```python
|
||||
load_checkpoint_in_model(unwrapped_model, save_directory, device_map={"":device})
|
||||
```
|
||||
|
||||
|
||||
### Save/load entire states
|
||||
|
||||
When training your model, you may want to save the current state of the model, optimizer, random generators, and potentially
|
||||
learning rate schedulers to be restored in the _same script_.
|
||||
You can use [`~Accelerator.save_state`] and [`~Accelerator.load_state`] respectively to do so.
|
||||
|
||||
To further customize where and how states saved through [`~Accelerator.save_state`] the [`~utils.ProjectConfiguration`] class can be used. For example
|
||||
@ -383,19 +387,19 @@ If you have registered any other stateful items to be stored through [`~Accelera
|
||||
</Tip>
|
||||
|
||||
|
||||
### Gradient clipping
|
||||
### Use gradient clipping
|
||||
|
||||
If you are using gradient clipping in your script, you should replace the calls to
|
||||
`torch.nn.utils.clip_grad_norm_` or `torch.nn.utils.clip_grad_value_` with [`~Accelerator.clip_grad_norm_`]
|
||||
and [`~Accelerator.clip_grad_value_`] respectively.
|
||||
|
||||
|
||||
### Mixed Precision training
|
||||
### Train with mixed precision
|
||||
|
||||
If you are running your training in Mixed Precision with 🤗 Accelerate, you will get the best result with your loss being
|
||||
computed inside your model (like in Transformer models for instance). Every computation outside of the model will be
|
||||
executed in full precision (which is generally what you want for loss computation, especially if it involves a
|
||||
softmax). However you might want to put your loss computation inside the *accelerator.autocast* context manager:
|
||||
softmax). However, you might want to put your loss computation inside the [`~Accelerator.autocast`] context manager:
|
||||
|
||||
```
|
||||
with accelerator.autocast():
|
||||
@ -416,7 +420,7 @@ if not accelerator.optimizer_step_was_skipped:
|
||||
lr_scheduler.step()
|
||||
```
|
||||
|
||||
### Gradient Accumulation
|
||||
### Use gradient accumulation
|
||||
|
||||
To perform gradient accumulation use [`~Accelerator.accumulate`] and specify a `gradient_accumulation_steps`.
|
||||
This will also automatically ensure the gradients are synced or unsynced when on multi-device training, check if the step should
|
||||
@ -435,70 +439,3 @@ for input, label in training_dataloader:
|
||||
scheduler.step()
|
||||
optimizer.zero_grad()
|
||||
```
|
||||
|
||||
### DeepSpeed
|
||||
|
||||
DeepSpeed support is experimental, so the underlying API will evolve in the near future and may have some slight
|
||||
breaking changes. In particular, 🤗 Accelerate does not support DeepSpeed config you have written yourself yet, this
|
||||
will be added in a next version.
|
||||
|
||||
<Tip warning={true}>
|
||||
|
||||
The [`notebook_launcher`] does not support the DeepSpeed integration yet.
|
||||
|
||||
</Tip>
|
||||
|
||||
## Internal mechanism
|
||||
|
||||
Internally, the library works by first analyzing the environment in which the script is launched to determine which
|
||||
kind of distributed setup is used, how many different processes there are and which one the current script is in. All
|
||||
that information is stored in the [`~AcceleratorState`].
|
||||
|
||||
This class is initialized the first time you instantiate an [`~Accelerator`] as well as performing any
|
||||
specific initialization your distributed setup needs. Its state is then uniquely shared through all instances of
|
||||
[`~state.AcceleratorState`].
|
||||
|
||||
Then, when calling [`~Accelerator.prepare`], the library:
|
||||
|
||||
- wraps your model(s) in the container adapted for the distributed setup,
|
||||
- wraps your optimizer(s) in a [`~optimizer.AcceleratedOptimizer`],
|
||||
- creates a new version of your dataloader(s) in a [`~data_loader.DataLoaderShard`].
|
||||
|
||||
While the model(s) and optimizer(s) are just put in simple wrappers, the dataloader(s) are re-created. This is mostly
|
||||
because PyTorch does not let the user change the `batch_sampler` of a dataloader once it's been created and the
|
||||
library handles the sharding of your data between processes by changing that `batch_sampler` to yield every other
|
||||
`num_processes` batches.
|
||||
|
||||
The [`~data_loader.DataLoaderShard`] subclasses `DataLoader` to add the following functionality:
|
||||
|
||||
- it synchronizes the appropriate random number generator of all processes at each new iteration, to ensure any
|
||||
randomization (like shuffling) is done the exact same way across processes.
|
||||
- it puts the batches on the proper device before yielding them (unless you have opted out of
|
||||
`device_placement=True`).
|
||||
|
||||
The random number generator synchronization will by default synchronize:
|
||||
|
||||
- the `generator` attribute of a given sampler (like the PyTorch `RandomSampler`) for PyTorch >= 1.6
|
||||
- the main random number generator in PyTorch <=1.5.1
|
||||
|
||||
You can choose which random number generator(s) to synchronize with the `rng_types` argument of the main
|
||||
[`Accelerator`]. In PyTorch >= 1.6, it is recommended to rely on a local `generator` to avoid
|
||||
setting the same seed in the main random number generator in all processes.
|
||||
|
||||
<Tip warning={true}>
|
||||
|
||||
Synchronization of the main torch (or CUDA or XLA) random number generator will affect any other potential random
|
||||
artifacts you could have in your dataset (like random data augmentation) in the sense that all processes will get
|
||||
the same random numbers from the torch random modules (so will apply the same random data augmentation if it's
|
||||
controlled by torch).
|
||||
|
||||
</Tip>
|
||||
|
||||
<Tip>
|
||||
|
||||
The randomization part of your custom sampler, batch sampler or iterable dataset should be done using a local
|
||||
`torch.Generator` object (in PyTorch >= 1.6), see the traditional `RandomSampler`, as an example.
|
||||
|
||||
</Tip>
|
||||
|
||||
For more details about the internals, see the [Internals page](package_reference/torch_wrappers).
|
||||
150
docs/source/usage_guides/big_modeling.md
Normal file
150
docs/source/usage_guides/big_modeling.md
Normal file
@ -0,0 +1,150 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Handling big models for inference
|
||||
|
||||
One of the biggest advancements 🤗 Accelerate provides is the concept of [large model inference](../concept_guides/big_model_inference) wherein you can perform *inference* on models that cannot fully fit on your graphics card.
|
||||
|
||||
This tutorial will be broken down into two parts showcasing how to use both 🤗 Accelerate and 🤗 Transformers (a higher API-level) to make use of this idea.
|
||||
|
||||
## Using 🤗 Accelerate
|
||||
|
||||
For these tutorials, we'll assume a typical workflow for loading your model in such that:
|
||||
|
||||
```py
|
||||
import torch
|
||||
|
||||
my_model = ModelClass(...)
|
||||
state_dict = torch.load(checkpoint_file)
|
||||
my_model.load_state_dict(state_dict)
|
||||
```
|
||||
|
||||
Note that here we assume that `ModelClass` is a model that takes up more video-card memory than what can fit on your device (be it `mps` or `cuda`).
|
||||
|
||||
The first step is to init an empty skeleton of the model which won't take up any RAM using the [`init_empty_weights`] context manager:
|
||||
|
||||
```py
|
||||
from accelerate import init_empty_weights
|
||||
with init_empty_weights():
|
||||
my_model = ModelClass(...)
|
||||
```
|
||||
|
||||
With this `my_model` currently is "parameterless", hence leaving the smaller footprint than what one would normally get loading this onto the CPU directly.
|
||||
|
||||
Next we need to load in the weights to our model so we can perform inference.
|
||||
|
||||
For this we will use [`load_checkpoint_and_dispatch`], which as the name implies will load a checkpoint inside your empty model and dispatch the weights for each layer across all the devices you have available (GPU/MPS and CPU RAM).
|
||||
|
||||
To determine how this `dispatch` can be performed, generally specifying `device_map="auto"` will be good enough as 🤗 Accelerate
|
||||
will attempt to fill all the space in your GPU(s), then loading them to the CPU, and finally if there is not enough RAM it will be loaded to the disk (the absolute slowest option).
|
||||
|
||||
<Tip>
|
||||
|
||||
For more details on desigining your own device map, see this section of the [concept guide](../concept_guide/big_model_inference#desigining-a-device-map)
|
||||
|
||||
</Tip>
|
||||
|
||||
See an example below:
|
||||
|
||||
```py
|
||||
from accelerate import load_checkpoint_and_dispatch
|
||||
|
||||
model = load_checkpoint_and_dispatch(
|
||||
model, checkpoint=checkpoint_file, device_map="auto"
|
||||
)
|
||||
```
|
||||
|
||||
<Tip>
|
||||
|
||||
If there are certain "chunks" of layers that shouldn't be split, you can pass them in as `no_split_module_classes`. Read more about it [here](../concept_guides/big_model_inference#loading-weights)
|
||||
|
||||
</Tip>
|
||||
|
||||
<Tip>
|
||||
|
||||
Also to save on memory (such as if the `state_dict` will not fit in RAM), a model's weights can be divided and split into multiple checkpoint files. Read more about it [here](../concept_guides/big_model_inference#sharded-checkpoints)
|
||||
|
||||
</Tip>
|
||||
|
||||
Now that the model is dispatched fully, you can perform inference as normal with the model:
|
||||
|
||||
```py
|
||||
input = torch.randn(2,3)
|
||||
input = input.to("cuda")
|
||||
output = model(input)
|
||||
```
|
||||
|
||||
What will happen now is each time the input gets passed through a layer, it will be sent from the CPU to the GPU (or disk to CPU to GPU), the output is calculated, and then the layer is pulled back off the GPU going back down the line. While this adds some overhead to the inference being performed, through this method it is possible to run **any size model** on your system, as long as the largest layer is capable of fitting on your GPU.
|
||||
|
||||
<Tip>
|
||||
|
||||
Multiple GPUs can be utilized, however this is considered "model parallism" and as a result only one GPU will be active at a given moment, waiting for the prior one to send it the output. You should launch your script normally with `python`
|
||||
and not need `torchrun`, `accelerate launch`, etc.
|
||||
|
||||
</Tip>
|
||||
|
||||
For a visual representation of this, check out the animation below:
|
||||
|
||||
<Youtube id="MWCSGj9jEAo" />
|
||||
|
||||
### Complete Example
|
||||
|
||||
Below is the full example showcasing what we performed above:
|
||||
|
||||
```py
|
||||
import torch
|
||||
from accelerate import init_empty_weights, load_checkpoint_and_dispatch
|
||||
|
||||
with init_empty_weights():
|
||||
model = MyModel(...)
|
||||
|
||||
model = load_checkpoint_and_dispatch(
|
||||
model, checkpoint=checkpoint_file, device_map="auto"
|
||||
)
|
||||
|
||||
input = torch.randn(2,3)
|
||||
input = input.to("cuda")
|
||||
output = model(input)
|
||||
```
|
||||
|
||||
## Using 🤗 Transformers, 🤗 Diffusers, and other 🤗 Open Source Libraries
|
||||
|
||||
Libraries that support 🤗 Accelerate big model inference include all of the earlier logic in their `from_pretrained` constructors.
|
||||
|
||||
These operate by specifying a string representing the model to download from the [🤗 Hub](https://hf.co/models) and then denoting `device_map="auto"` along with a few extra parameters.
|
||||
|
||||
As a brief example, we will look at using `transformers` and loading in Big Science's T0pp model.
|
||||
|
||||
```py
|
||||
from transformers import AutoModelForSeq2SeqLM
|
||||
|
||||
model = AutoModelForSeq2SeqLM.from_pretrained("bigscience/T0pp", device_map="auto")
|
||||
```
|
||||
|
||||
After loading the model in, the initial steps from before to prepare a model have all been done and the model is fully
|
||||
ready to make use of all the resources in your machine. Through these constructors, you can also save *more* memory by
|
||||
specifying the precision the model is loaded into as well, through the `torch_dtype` parameter, such as:
|
||||
|
||||
```py
|
||||
from transformers import AutoModelForSeq2SeqLM
|
||||
|
||||
model = AutoModelForSeq2SeqLM.from_pretrained("bigscience/T0pp", device_map="auto", torch_dtype=torch.float16)
|
||||
```
|
||||
|
||||
To learn more about this, check out the 🤗 Transformers documentation available [here](https://huggingface.co/docs/transformers/main/en/main_classes/model#large-model-loading).
|
||||
|
||||
## Where to go from here
|
||||
|
||||
For a much more detailed look at big model inference, be sure to check out the [Conceptual Guide on it](../concept_guides/big_model_inference)
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Checkpointing
|
||||
@ -17,7 +20,7 @@ saving and loading the model, optimizer, RNG generators, and the GradScaler. Ins
|
||||
- Use [`~Accelerator.save_state`] for saving everything mentioned above to a folder location
|
||||
- Use [`~Accelerator.load_state`] for loading everything stored from an earlier `save_state`
|
||||
|
||||
To further customize where and how states saved through [`~Accelerator.save_state`] the [`~utils.ProjectConfiguration`] class can be used. For example
|
||||
To further customize where and how states are saved through [`~Accelerator.save_state`] the [`~utils.ProjectConfiguration`] class can be used. For example
|
||||
if `automatic_checkpoint_naming` is enabled each saved checkpoint will be located then at `Accelerator.project_dir/checkpoints/checkpoint_{checkpoint_number}`.
|
||||
|
||||
It should be noted that the expectation is that those states come from the same training script, they should not be from two separate scripts.
|
||||
@ -59,13 +62,13 @@ for epoch in range(num_epochs):
|
||||
my_optimizer.step()
|
||||
my_scheduler.step()
|
||||
|
||||
# Restore previous state
|
||||
# Restore the previous state
|
||||
accelerator.load_state("my/save/path/checkpointing/checkpoint_0")
|
||||
```
|
||||
|
||||
## Restoring the state of the DataLoader
|
||||
|
||||
After resuming from a checkpoint, it may also be desireable to resume from a particular point in the active `DataLoader` if
|
||||
After resuming from a checkpoint, it may also be desirable to resume from a particular point in the active `DataLoader` if
|
||||
the state was saved during the middle of an epoch. You can use [`~Accelerator.skip_first_batches`] to do so.
|
||||
|
||||
```python
|
||||
@ -77,5 +80,17 @@ train_dataloader = accelerator.prepare(train_dataloader)
|
||||
accelerator.load_state("my_state")
|
||||
|
||||
# Assume the checkpoint was saved 100 steps into the epoch
|
||||
accelerator.skip_first_batches(train_dataloader, 100)
|
||||
```
|
||||
skipped_dataloader = accelerator.skip_first_batches(train_dataloader, 100)
|
||||
|
||||
# After the first iteration, go back to `train_dataloader`
|
||||
|
||||
# First epoch
|
||||
for batch in skipped_dataloader:
|
||||
# Do something
|
||||
pass
|
||||
|
||||
# Second epoch
|
||||
for batch in train_dataloader:
|
||||
# Do something
|
||||
pass
|
||||
```
|
||||
93
docs/source/usage_guides/debug.md
Normal file
93
docs/source/usage_guides/debug.md
Normal file
@ -0,0 +1,93 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Debugging Distributed Operations
|
||||
|
||||
When running scripts in a distributed fashion, often functions such as [`Accelerator.gather`] and [`Accelerator.reduce`] (and others) are neccessary to grab tensors across devices and perform certain operations on them. However, if the tensors which are being grabbed are not the proper shapes then this will result in your code hanging forever. The only sign that exists of this truly happening is hitting a timeout exception from `torch.distributed`, but this can get quite costly as usually the timeout is 10 minutes.
|
||||
|
||||
Accelerate now has a `debug` mode which adds a neglible amount of time to each operation, but allows it to verify that the inputs you are bringing in can *actually* perform the operation you want **without** hitting this timeout problem!
|
||||
|
||||
## Visualizing the problem
|
||||
|
||||
To have a tangible example of this issue, let's take the following setup (on 2 GPUs):
|
||||
|
||||
```python
|
||||
from accelerate import PartialState
|
||||
|
||||
state = PartialState()
|
||||
if state.process_index == 0:
|
||||
tensor = torch.tensor([[0.0, 1, 2, 3, 4]]).to(state.device)
|
||||
else:
|
||||
tensor = torch.tensor([[[0.0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]]).to(state.device)
|
||||
|
||||
broadcast_tensor = broadcast(tensor)
|
||||
print(broadcast_tensor)
|
||||
```
|
||||
|
||||
We've created a single tensor on each device, with two radically different shapes. With this setup if we want to perform an operation such as [`utils.broadcast`], we would forever hit a timeout because `torch.distributed` requires that these operations have the **exact same shape** across all processes for it to work.
|
||||
|
||||
If you run this yourself, you will find that `broadcast_tensor` can be printed on the main process, but its results won't quite be right, and then it will just hang never printing it on any of the other processes:
|
||||
|
||||
```
|
||||
>>> tensor([[0, 1, 2, 3, 4]], device='cuda:0')
|
||||
```
|
||||
|
||||
## The solution
|
||||
|
||||
By enabling Accelerate's operational debug mode, Accelerate will properly find and catch errors such as this and provide a very clear traceback immediatly:
|
||||
|
||||
```
|
||||
Traceback (most recent call last):
|
||||
File "/home/zach_mueller_huggingface_co/test.py", line 18, in <module>
|
||||
main()
|
||||
File "/home/zach_mueller_huggingface_co/test.py", line 15, in main
|
||||
main()broadcast_tensor = broadcast(tensor)
|
||||
File "/home/zach_mueller_huggingface_co/accelerate/src/accelerate/utils/operations.py", line 303, in wrapper
|
||||
broadcast_tensor = broadcast(tensor)
|
||||
accelerate.utils.operations.DistributedOperationException: Cannot apply desired operation due to shape mismatches. All shapes across devices must be valid.
|
||||
|
||||
Operation: `accelerate.utils.operations.broadcast`
|
||||
Input shapes:
|
||||
- Process 0: [1, 5]
|
||||
- Process 1: [1, 2, 5]
|
||||
```
|
||||
|
||||
This explains that the shapes across our devices were *not* the same, and that we should ensure that they match properly to be compatible. Typically this means that there is either an extra dimension, or certain dimensions are incompatible with the operation.
|
||||
|
||||
To enable this please do one of the following:
|
||||
|
||||
Enable it through the questionarre during `accelerate config` (recommended)
|
||||
|
||||
From the CLI:
|
||||
|
||||
```
|
||||
accelerate launch --debug {my_script.py} --arg1 --arg2
|
||||
```
|
||||
|
||||
As an environmental variable (which avoids the need for `accelerate launch`):
|
||||
|
||||
```
|
||||
ACCELERATE_DEBUG_MODE="1" accelerate launch {my_script.py} --arg1 --arg2
|
||||
```
|
||||
|
||||
Manually changing the `config.yaml` file:
|
||||
|
||||
```diff
|
||||
compute_environment: LOCAL_MACHINE
|
||||
+debug: true
|
||||
```
|
||||
|
||||
|
||||
|
||||
@ -8,11 +8,14 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# DeepSpeed
|
||||
|
||||
[DeepSpeed](https://github.com/microsoft/DeepSpeed) implements everything described in the [ZeRO paper](https://arxiv.org/abs/1910.02054). Currently it provides full support for:
|
||||
[DeepSpeed](https://github.com/microsoft/DeepSpeed) implements everything described in the [ZeRO paper](https://arxiv.org/abs/1910.02054). Currently, it provides full support for:
|
||||
|
||||
1. Optimizer state partitioning (ZeRO stage 1)
|
||||
2. Gradient partitioning (ZeRO stage 2)
|
||||
@ -26,14 +29,14 @@ Memory Wall for Extreme Scale Deep Learning](https://arxiv.org/abs/2104.07857).
|
||||
|
||||
DeepSpeed ZeRO-2 is primarily used only for training, as its features are of no use to inference.
|
||||
|
||||
DeepSpeed ZeRO-3 can be used for inference as well, since it allows huge models to be loaded on multiple GPUs, which
|
||||
DeepSpeed ZeRO-3 can be used for inference as well since it allows huge models to be loaded on multiple GPUs, which
|
||||
won't be possible on a single GPU.
|
||||
|
||||
🤗 Accelerate integrates [DeepSpeed](https://github.com/microsoft/DeepSpeed) via 2 options:
|
||||
|
||||
1. Integration of the DeepSpeed features via `deepspeed config file` specification in `accelerate config` . You just supply your custom config file or use our template. Most of
|
||||
this document is focused on this feature. This supports all the core features of DeepSpeed and gives user a lot of flexibility.
|
||||
User may have to change few lines of code depending on the config.
|
||||
User may have to change a few lines of code depending on the config.
|
||||
2. Integration via `deepspeed_plugin`.This supports subset of the DeepSpeed features and uses default options for the rest of the configurations.
|
||||
User need not change any code and is good for those who are fine with most of the default settings of DeepSpeed.
|
||||
|
||||
@ -57,7 +60,7 @@ Below is a short description of Data Parallelism using ZeRO - Zero Redundancy Op
|
||||
|
||||
e. **Param Offload**: Offloads the model parameters to CPU/Disk building on top of ZERO Stage 3
|
||||
|
||||
<u>Note</u>: With respect to Disk Offload, the disk should be an NVME for decent speed but it technically work on any Disk
|
||||
<u>Note</u>: With respect to Disk Offload, the disk should be an NVME for decent speed but it technically works on any Disk
|
||||
|
||||
Inference:
|
||||
|
||||
@ -352,8 +355,8 @@ accelerate launch examples/by_feature/deepspeed_with_config_support.py \
|
||||
see the [DeepSpeed Optimizers](https://deepspeed.readthedocs.io/en/latest/optimizers.html) and [DeepSpeed Schedulers](https://deepspeed.readthedocs.io/en/latest/schedulers.html) documentation.
|
||||
We will look at the changes needed in the code when using these.
|
||||
|
||||
a. DS Optim + DS Scheduler: The case when both `optimizer` and `scheduler` keys present in the DeepSpeed config file.
|
||||
In this situation, those will be used and user has to use `accelerate.utils.DummyOptim` and `accelerate.utils.DummyScheduler` to replace the PyTorch/Custom optimizers and schedulers in their code.
|
||||
a. DS Optim + DS Scheduler: The case when both `optimizer` and `scheduler` keys are present in the DeepSpeed config file.
|
||||
In this situation, those will be used and the user has to use `accelerate.utils.DummyOptim` and `accelerate.utils.DummyScheduler` to replace the PyTorch/Custom optimizers and schedulers in their code.
|
||||
Below is the snippet from `examples/by_feature/deepspeed_with_config_support.py` showing this:
|
||||
```python
|
||||
# Creates Dummy Optimizer if `optimizer` was spcified in the config file else creates Adam Optimizer
|
||||
@ -386,7 +389,7 @@ We will look at the changes needed in the code when using these.
|
||||
In the above example we can see that the code remains unchanged if the `optimizer` and `scheduler` keys are absent in the DeepSpeed config file.
|
||||
|
||||
c. Custom Optim + DS Scheduler: The case when only `scheduler` key is present in the DeepSpeed config file.
|
||||
In this situation, user has to use `accelerate.utils.DummyScheduler` to replace the PyTorch/Custom scheduler in their code.
|
||||
In this situation, the user has to use `accelerate.utils.DummyScheduler` to replace the PyTorch/Custom scheduler in their code.
|
||||
|
||||
d. DS Optim + Custom Scheduler: The case when only `optimizer` key is present in the DeepSpeed config file.
|
||||
This will result in an error because you can only use DS Scheduler when using DS Optim.
|
||||
@ -582,8 +585,10 @@ Mixed precision type: fp16
|
||||
ds_config: {'bf16': {'enabled': False}, 'zero_optimization': {'stage': 3, 'stage3_gather_16bit_weights_on_model_save': True, 'offload_optimizer': {'device': 'nvme'}, 'offload_param': {'device': 'cpu'}}, 'gradient_clipping': 1.0, 'train_batch_size': 'auto', 'train_micro_batch_size_per_gpu': 'auto', 'gradient_accumulation_steps': 5, 'steps_per_print': inf, 'fp16': {'enabled': True, 'auto_cast': True}}
|
||||
```
|
||||
|
||||
**Note**: Remaining `"auto"` values are handled in `accelerator.prepare()` call as explained in point 2 of
|
||||
**Note**:
|
||||
1. Remaining `"auto"` values are handled in `accelerator.prepare()` call as explained in point 2 of
|
||||
`Important code changes when using DeepSpeed Config File`.
|
||||
2. Only when `gradient_accumulation_steps` is `auto`, the value passed while creating `Accelerator` object via `Accelerator(gradient_accumulation_steps=k)` will be used. When using DeepSpeed Plugin, the value from it will be used and it will overwrite the value passed while creating Accelerator object.
|
||||
|
||||
## Saving and loading
|
||||
|
||||
@ -679,6 +684,6 @@ Papers:
|
||||
- [ZeRO-Offload: Democratizing Billion-Scale Model Training](https://arxiv.org/abs/2101.06840)
|
||||
- [ZeRO-Infinity: Breaking the GPU Memory Wall for Extreme Scale Deep Learning](https://arxiv.org/abs/2104.07857)
|
||||
|
||||
Finally, please, remember that, 🤗 `Accelerate` only integrates DeepSpeed, therefore if you
|
||||
Finally, please, remember that 🤗 `Accelerate` only integrates DeepSpeed, therefore if you
|
||||
have any problems or questions with regards to DeepSpeed usage, please, file an issue with [DeepSpeed GitHub](https://github.com/microsoft/DeepSpeed/issues).
|
||||
|
||||
136
docs/source/usage_guides/distributed_inference.md
Normal file
136
docs/source/usage_guides/distributed_inference.md
Normal file
@ -0,0 +1,136 @@
|
||||
<!--Copyright 2023 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Distributed Inference with 🤗 Accelerate
|
||||
|
||||
Distributed inference is a common use case, especially with natural language processing (NLP) models. Users often want to
|
||||
send a number of different prompts, each to a different GPU, and then get the results back. This also has other cases
|
||||
outside of just NLP, however for this tutorial we will focus on just this idea of each GPU receiving a different prompt,
|
||||
and then returning the results.
|
||||
|
||||
## The Problem
|
||||
|
||||
Normally when doing this, users send the model to a specific device to load it from the CPU, and then move each prompt to a different device.
|
||||
|
||||
A basic pipeline using the `diffusers` library might look something like so:
|
||||
|
||||
```python
|
||||
import torch
|
||||
import torch.distributed as dist
|
||||
from diffusers import DiffusionPipeline
|
||||
|
||||
pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
|
||||
```
|
||||
Followed then by performing inference based on the specific prompt:
|
||||
|
||||
```python
|
||||
def run_inference(rank, world_size):
|
||||
dist.init_process_group("nccl", rank=rank, world_size=world_size)
|
||||
pipe.to(rank)
|
||||
|
||||
if torch.distributed.get_rank() == 0:
|
||||
prompt = "a dog"
|
||||
elif torch.distributed.get_rank() == 1:
|
||||
prompt = "a cat"
|
||||
|
||||
result = pipe(prompt).images[0]
|
||||
result.save(f"result_{rank}.png")
|
||||
```
|
||||
One will notice how we have to check the rank to know what prompt to send, which can be a bit tedious.
|
||||
|
||||
A user might then also think that with 🤗 Accelerate, using the `Accelerator` to prepare a dataloader for such a task might also be
|
||||
a simple way to manage this. (To learn more, check out the relvent section in the [Quick Tour](../quicktour#distributed-evaluation))
|
||||
|
||||
Can it manage it? Yes. Does it add unneeded extra code however: also yes.
|
||||
|
||||
## The Solution
|
||||
|
||||
With 🤗 Accelerate, we can simplify this process by using the [`Accelerator.split_between_processes`] context manager (which also exists in `PartialState` and `AcceleratorState`).
|
||||
This function will automatically split whatever data you pass to it (be it a prompt, a set of tensors, a dictionary of the prior data, etc.) across all the processes (with a potential
|
||||
to be padded) for you to use right away.
|
||||
|
||||
Let's rewrite the above example using this context manager:
|
||||
|
||||
```python
|
||||
from accelerate import PartialState # Can also be Accelerator or AcceleratorState
|
||||
from diffusers import DiffusionPipeline
|
||||
|
||||
pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
|
||||
distributed_state = PartialState()
|
||||
pipe.to(distributed_state.device)
|
||||
|
||||
# Assume two processes
|
||||
with distributed_state.split_between_processes(["a dog", "a cat"]) as prompt:
|
||||
result = pipe(prompt).images[0]
|
||||
result.save(f"result_{distributed_state.process_index}.png")
|
||||
```
|
||||
|
||||
And then to launch the code, we can use the 🤗 Accelerate:
|
||||
|
||||
If you have generated a config file to be used using `accelerate config`:
|
||||
|
||||
```bash
|
||||
accelerate launch distributed_inference.py
|
||||
```
|
||||
|
||||
If you have a specific config file you want to use:
|
||||
|
||||
```bash
|
||||
accelerate launch --config_file my_config.json distributed_inference.py
|
||||
```
|
||||
|
||||
Or if don't want to make any config files and launch on two GPUs:
|
||||
|
||||
> Note: You will get some warnings about values being guessed based on your system. To remove these you can do `accelerate config default` or go through `accelerate config` to create a config file.
|
||||
|
||||
```bash
|
||||
accelerate launch --num_processes 2 distributed_inference.py
|
||||
```
|
||||
|
||||
We've now reduced the boilerplate code needed to split this data to a few lines of code quite easily.
|
||||
|
||||
But what if we have an odd distribution of prompts to GPUs? For example, what if we have 3 prompts, but only 2 GPUs?
|
||||
|
||||
Under the context manager, the first GPU would receive the first two prompts and the second GPU the third, ensuring that
|
||||
all prompts are split and no overhead is needed.
|
||||
|
||||
*However*, what if we then wanted to do something with the results of *all the GPUs*? (Say gather them all and perform some kind of post processing)
|
||||
You can pass in `apply_padding=True` to ensure that the lists of prompts are padded to the same length, with extra data being taken
|
||||
from the last sample. This way all GPUs will have the same number of prompts, and you can then gather the results.
|
||||
|
||||
<Tip>
|
||||
|
||||
This is only needed when trying to perform an action such as gathering the results, where the data on each device
|
||||
needs to be the same length. Basic inference does not require this.
|
||||
|
||||
</Tip>
|
||||
|
||||
For instance:
|
||||
|
||||
```python
|
||||
from accelerate import PartialState # Can also be Accelerator or AcceleratorState
|
||||
from diffusers import DiffusionPipeline
|
||||
|
||||
pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
|
||||
distributed_state = PartialState()
|
||||
pipe.to(distributed_state.device)
|
||||
|
||||
# Assume two processes
|
||||
with distributed_state.split_between_processes(["a dog", "a cat", "a chicken"], apply_padding=True) as prompt:
|
||||
result = pipe(prompt).images
|
||||
```
|
||||
|
||||
On the first GPU, the prompts will be `["a dog", "a cat"]`, and on the second GPU it will be `["a chicken", "a chicken"]`.
|
||||
Make sure to drop the final sample, as it will be a duplicate of the previous one.
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Learning how to incorporate 🤗 Accelerate features quickly!
|
||||
@ -34,14 +37,14 @@ for batch in dataloader:
|
||||
|
||||
<div class="block dark:hidden">
|
||||
<iframe
|
||||
src="https://muellerzr-accelerate-examples.hf.space?__theme=light"
|
||||
src="https://hf-accelerate-accelerate-examples.hf.space?__theme=light"
|
||||
width="850"
|
||||
height="1600"
|
||||
></iframe>
|
||||
</div>
|
||||
<div class="hidden dark:block">
|
||||
<iframe
|
||||
src="https://muellerzr-accelerate-examples.hf.space?__theme=dark"
|
||||
src="https://hf-accelerate-accelerate-examples.hf.space?__theme=dark"
|
||||
width="850"
|
||||
height="1600"
|
||||
></iframe>
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Fully Sharded Data Parallel
|
||||
@ -46,7 +49,7 @@ fsdp_config:
|
||||
fsdp_offload_params: false
|
||||
fsdp_sharding_strategy: 1
|
||||
fsdp_state_dict_type: FULL_STATE_DICT
|
||||
fsdp_transformer_layer_cls_to_wrap: GPT2Block
|
||||
fsdp_transformer_layer_cls_to_wrap: BertLayer
|
||||
machine_rank: 0
|
||||
main_process_ip: null
|
||||
main_process_port: null
|
||||
@ -64,19 +67,86 @@ accelerate launch examples/nlp_example.py
|
||||
Currently, `Accelerate` supports the following config through the CLI:
|
||||
|
||||
```bash
|
||||
`Sharding Strategy`: [1] FULL_SHARD (shards optimizer states, gradients and parameters), [2] SHARD_GRAD_OP (shards optimizer states and gradients), [3] NO_SHARD
|
||||
`Sharding Strategy`: [1] FULL_SHARD (shards optimizer states, gradients and parameters), [2] SHARD_GRAD_OP (shards optimizer states and gradients), [3] NO_SHARD (DDP), [4] HYBRID_SHARD (shards optimizer states, gradients and parameters within each node while each node has full copy), [5] HYBRID_SHARD_ZERO2 (shards optimizer states and gradients within each node while each node has full copy)
|
||||
|
||||
`Offload Params`: Decides Whether to offload parameters and gradients to CPU
|
||||
`Auto Wrap Policy`: [1] TRANSFORMER_BASED_WRAP, [2] SIZE_BASED_WRAP, [3] NO_WRAP
|
||||
|
||||
`Auto Wrap Policy`: [1] TRANSFORMER_BASED_WRAP, [2] SIZE_BASED_WRAP, [3] NO_WRAP
|
||||
|
||||
`Transformer Layer Class to Wrap`: When using `TRANSFORMER_BASED_WRAP`, user specifies comma-separated string of transformer layer class names (case-sensitive) to wrap ,e.g,
|
||||
`BertLayer`, `GPTJBlock`, `T5Block`, `BertLayer,BertEmbeddings,BertSelfOutput`...
|
||||
This is important because submodules that share weights (e.g., embedding layer) should not end up in different FSDP wrapped units.
|
||||
Using this policy, wrapping happens for each block containing Multi-Head Attention followed by couple of MLP layers.
|
||||
Remaining layers including the shared embeddings are conveniently wrapped in same outermost FSDP unit.
|
||||
Therefore, use this for transformer based models.
|
||||
You can use the `model._no_split_modules` for 🤗 Transformer models by answering `yes` to
|
||||
`Do you want to use the model's `_no_split_modules` to wrap. Only applicable for 🤗 Transformers`.
|
||||
It will try to use `model._no_split_modules` when available.
|
||||
|
||||
`Min Num Params`: minimum number of parameters when using `SIZE_BASED_WRAP`
|
||||
|
||||
`Backward Prefetch`: [1] BACKWARD_PRE, [2] BACKWARD_POST, [3] NO_PREFETCH
|
||||
`State Dict Type`: [1] FULL_STATE_DICT, [2] LOCAL_STATE_DICT, [3] SHARDED_STATE_DICT
|
||||
|
||||
`State Dict Type`: [1] FULL_STATE_DICT, [2] LOCAL_STATE_DICT, [3] SHARDED_STATE_DICT
|
||||
|
||||
`Forward Prefetch`: if True, then FSDP explicitly prefetches the next upcoming
|
||||
all-gather while executing in the forward pass. only use with Static graphs.
|
||||
|
||||
`Use Orig Params`: If True, allows non-uniform `requires_grad` during init, which means support for interspersed frozen and trainable paramteres.
|
||||
Useful in cases such as parameter-efficient fine-tuning.
|
||||
Please refer this [blog](https://dev-discuss.pytorch.org/t/rethinking-pytorch-fully-sharded-data-parallel-fsdp-from-first-principles/1019)
|
||||
|
||||
`Sync Module States`: If True, each individually wrapped FSDP unit will broadcast module parameters from rank 0
|
||||
```
|
||||
|
||||
For additional and more nuanced control, you can specify other FSDP parameters via `FullyShardedDataParallelPlugin`.
|
||||
When creating `FullyShardedDataParallelPlugin` object, pass it the parameters that weren't part of the accelerate config or if you want to override them.
|
||||
The FSDP parameters will be picked based on the accelerate config file or launch command arguments and other parameters that you will pass directly through the `FullyShardedDataParallelPlugin` object will set/override that.
|
||||
|
||||
Below is an example:
|
||||
|
||||
```py
|
||||
from accelerate import FullyShardedDataParallelPlugin
|
||||
from torch.distributed.fsdp.fully_sharded_data_parallel import FullOptimStateDictConfig, FullStateDictConfig
|
||||
|
||||
fsdp_plugin = FullyShardedDataParallelPlugin(
|
||||
state_dict_config=FullStateDictConfig(offload_to_cpu=False, rank0_only=False),
|
||||
optim_state_dict_config=FullOptimStateDictConfig(offload_to_cpu=False, rank0_only=False),
|
||||
)
|
||||
|
||||
accelerator = Accelerator(fsdp_plugin=fsdp_plugin)
|
||||
```
|
||||
|
||||
## Saving and loading
|
||||
|
||||
1. When using transformers `save_pretrained`, pass `state_dict=accelerator.get_state_dict(model)` to save the model state dict.
|
||||
The new recommended way of checkpointing when using FSDP models is to use `SHARDED_STATE_DICT` as `StateDictType` when setting up the accelerate config.
|
||||
Below is the code snippet to save using `save_state` utility of accelerate.
|
||||
|
||||
```py
|
||||
accelerator.save_state("ckpt")
|
||||
```
|
||||
|
||||
Inspect the ckeckpoint folder to see model and optimizer as shards per process:
|
||||
```
|
||||
ls ckpt
|
||||
# optimizer_0 pytorch_model_0 random_states_0.pkl random_states_1.pkl scheduler.bin
|
||||
|
||||
cd ckpt
|
||||
|
||||
ls optimizer_0
|
||||
# __0_0.distcp __1_0.distcp
|
||||
|
||||
ls pytorch_model_0
|
||||
# __0_0.distcp __1_0.distcp
|
||||
```
|
||||
|
||||
To load them back for resuming the training, use the `load_state` utility of accelerate
|
||||
|
||||
```py
|
||||
accelerator.load_state("ckpt")
|
||||
```
|
||||
|
||||
When using transformers `save_pretrained`, pass `state_dict=accelerator.get_state_dict(model)` to save the model state dict.
|
||||
Below is an example:
|
||||
|
||||
```diff
|
||||
@ -88,6 +158,22 @@ Currently, `Accelerate` supports the following config through the CLI:
|
||||
)
|
||||
```
|
||||
|
||||
### State Dict
|
||||
|
||||
`accelerator.get_state_dict` will call the underlying `model.state_dict` implementation. With a model wrapped by FSDP, the default behavior of `state_dict` is to gather all of the state in the rank 0 device. This can cause CUDA out of memory errors if the parameters don't fit on a single GPU.
|
||||
|
||||
To avoid this, PyTorch provides a context manager that adjusts the behavior of `state_dict`. To offload some of the state dict onto CPU, you can use the following code:
|
||||
|
||||
```
|
||||
from torch.distributed.fsdp import FullyShardedDataParallel as FSDP, StateDictType, FullStateDictConfig
|
||||
|
||||
full_state_dict_config = FullStateDictConfig(offload_to_cpu=True, rank0_only=True)
|
||||
with FSDP.state_dict_type(unwrapped_model, StateDictType.FULL_STATE_DICT, full_state_dict_config):
|
||||
state = accelerator.get_state_dict(unwrapped_model)
|
||||
```
|
||||
|
||||
You can then pass `state` into the `save_pretrained` method. There are several modes for `StateDictType` and `FullStateDictConfig` that you can use to control the behavior of `state_dict`. For more information, see the [PyTorch documentation](https://pytorch.org/docs/stable/fsdp.html).
|
||||
|
||||
## A few caveats to be aware of
|
||||
|
||||
- PyTorch FSDP auto wraps sub-modules, flattens the parameters and shards the parameters in place.
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Performing gradient accumulation with 🤗 Accelerate
|
||||
@ -87,6 +90,9 @@ of steps to perform before each call to `step()` and how to automatically adjust
|
||||
+ accelerator = Accelerator(gradient_accumulation_steps=2)
|
||||
```
|
||||
|
||||
Alternatively, you can pass in a `gradient_accumulation_plugin` parameter to the [`Accelerator`] object's `__init__`, which will allow you to further customize the gradient accumulation behavior.
|
||||
Read more about that in the [GradientAccumulationPlugin](../package_reference/accelerator#accelerate.utils.GradientAccumulationPlugin) docs.
|
||||
|
||||
From here you can use the [`~Accelerator.accumulate`] context manager from inside your training loop to automatically perform the gradient accumulation for you!
|
||||
You just wrap it around the entire training part of our code:
|
||||
|
||||
@ -111,11 +117,21 @@ You can remove all the special checks for the step number and the loss adjustmen
|
||||
|
||||
As you can see the [`Accelerator`] is able to keep track of the batch number you are on and it will automatically know whether to step through the prepared optimizer and how to adjust the loss.
|
||||
|
||||
<Tip>
|
||||
Typically with gradient accumulation, you would need to adjust the number of steps to reflect the change in total batches you are
|
||||
training on. 🤗 Accelerate automagically does this for you by default. Behind the scenes we instantiate a GradientAccumulationPlugin configured to do this.
|
||||
</Tip>
|
||||
|
||||
## The finished code
|
||||
|
||||
Below is the finished implementation for performing gradient accumulation with 🤗 Accelerate
|
||||
|
||||
```python
|
||||
from accelerate import Accelerator
|
||||
accelerator = Accelerator(gradient_accumulation_steps=2)
|
||||
model, optimizer, training_dataloader, scheduler = accelerator.prepare(
|
||||
model, optimizer, training_dataloader, scheduler
|
||||
)
|
||||
for batch in training_dataloader:
|
||||
with accelerator.accumulate(model):
|
||||
inputs, targets = batch
|
||||
@ -127,4 +143,74 @@ for batch in training_dataloader:
|
||||
optimizer.zero_grad()
|
||||
```
|
||||
|
||||
<Tip warning={true}>
|
||||
|
||||
It's important that **only one forward/backward** should be done inside the context manager `with accelerator.accumulate(model)`.
|
||||
|
||||
</Tip>
|
||||
|
||||
|
||||
To learn more about what magic this wraps around, read the [Gradient Synchronization concept guide](../concept_guides/gradient_synchronization)
|
||||
|
||||
|
||||
## Self-contained example
|
||||
|
||||
Here is a self-contained example that you can run to see gradient accumulation in action with 🤗 Accelerate:
|
||||
|
||||
```python
|
||||
import torch
|
||||
import copy
|
||||
from accelerate import Accelerator
|
||||
from accelerate.utils import set_seed
|
||||
from torch.utils.data import TensorDataset, DataLoader
|
||||
|
||||
# seed
|
||||
set_seed(0)
|
||||
|
||||
# define toy inputs and labels
|
||||
x = torch.tensor([1., 2., 3., 4., 5., 6., 7., 8.])
|
||||
y = torch.tensor([2., 4., 6., 8., 10., 12., 14., 16.])
|
||||
gradient_accumulation_steps = 4
|
||||
batch_size = len(x) // gradient_accumulation_steps
|
||||
|
||||
# define dataset and dataloader
|
||||
dataset = TensorDataset(x, y)
|
||||
dataloader = DataLoader(dataset, batch_size=batch_size)
|
||||
|
||||
# define model, optimizer and loss function
|
||||
model = torch.zeros((1, 1), requires_grad=True)
|
||||
model_clone = copy.deepcopy(model)
|
||||
criterion = torch.nn.MSELoss()
|
||||
model_optimizer = torch.optim.SGD([model], lr=0.02)
|
||||
accelerator = Accelerator(gradient_accumulation_steps=gradient_accumulation_steps)
|
||||
model, model_optimizer, dataloader = accelerator.prepare(model, model_optimizer, dataloader)
|
||||
model_clone_optimizer = torch.optim.SGD([model_clone], lr=0.02)
|
||||
print(f"initial model weight is {model.mean().item():.5f}")
|
||||
print(f"initial model weight is {model_clone.mean().item():.5f}")
|
||||
for i, (inputs, labels) in enumerate(dataloader):
|
||||
with accelerator.accumulate(model):
|
||||
inputs = inputs.view(-1, 1)
|
||||
print(i, inputs.flatten())
|
||||
labels = labels.view(-1, 1)
|
||||
outputs = inputs @ model
|
||||
loss = criterion(outputs, labels)
|
||||
accelerator.backward(loss)
|
||||
model_optimizer.step()
|
||||
model_optimizer.zero_grad()
|
||||
loss = criterion(x.view(-1, 1) @ model_clone, y.view(-1, 1))
|
||||
model_clone_optimizer.zero_grad()
|
||||
loss.backward()
|
||||
model_clone_optimizer.step()
|
||||
print(f"w/ accumulation, the final model weight is {model.mean().item():.5f}")
|
||||
print(f"w/o accumulation, the final model weight is {model_clone.mean().item():.5f}")
|
||||
```
|
||||
```
|
||||
initial model weight is 0.00000
|
||||
initial model weight is 0.00000
|
||||
0 tensor([1., 2.])
|
||||
1 tensor([3., 4.])
|
||||
2 tensor([5., 6.])
|
||||
3 tensor([7., 8.])
|
||||
w/ accumulation, the final model weight is 2.04000
|
||||
w/o accumulation, the final model weight is 2.04000
|
||||
```
|
||||
174
docs/source/usage_guides/ipex.md
Normal file
174
docs/source/usage_guides/ipex.md
Normal file
@ -0,0 +1,174 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Intel® Extension for PyTorch
|
||||
|
||||
[IPEX](https://github.com/intel/intel-extension-for-pytorch) is optimized for CPUs with AVX-512 or above, and functionally works for CPUs with only AVX2. So, it is expected to bring performance benefit for Intel CPU generations with AVX-512 or above while CPUs with only AVX2 (e.g., AMD CPUs or older Intel CPUs) might result in a better performance under IPEX, but not guaranteed. IPEX provides performance optimizations for CPU training with both Float32 and BFloat16. The usage of BFloat16 is the main focus of the following sections.
|
||||
|
||||
Low precision data type BFloat16 has been natively supported on the 3rd Generation Xeon® Scalable Processors (aka Cooper Lake) with AVX512 instruction set and will be supported on the next generation of Intel® Xeon® Scalable Processors with Intel® Advanced Matrix Extensions (Intel® AMX) instruction set with further boosted performance. The Auto Mixed Precision for CPU backend has been enabled since PyTorch-1.10. At the same time, the support of Auto Mixed Precision with BFloat16 for CPU and BFloat16 optimization of operators has been massively enabled in Intel® Extension for PyTorch, and partially upstreamed to PyTorch master branch. Users can get better performance and user experience with IPEX Auto Mixed Precision.
|
||||
|
||||
## IPEX installation:
|
||||
|
||||
IPEX release is following PyTorch, to install via pip:
|
||||
|
||||
| PyTorch Version | IPEX version |
|
||||
| :---------------: | :----------: |
|
||||
| 2.0 | 2.0.0 |
|
||||
| 1.13 | 1.13.0 |
|
||||
| 1.12 | 1.12.300 |
|
||||
| 1.11 | 1.11.200 |
|
||||
| 1.10 | 1.10.100 |
|
||||
|
||||
```
|
||||
pip install intel_extension_for_pytorch==<version_name> -f https://developer.intel.com/ipex-whl-stable-cpu
|
||||
```
|
||||
|
||||
Check more approaches for [IPEX installation](https://intel.github.io/intel-extension-for-pytorch/cpu/latest/tutorials/installation.html).
|
||||
|
||||
|
||||
## How It Works For Training optimization in CPU
|
||||
|
||||
🤗 Accelerate has integrated [IPEX](https://github.com/intel/intel-extension-for-pytorch), all you need to do is enabling it through the config.
|
||||
|
||||
**Scenario 1**: Acceleration of No distributed CPU training
|
||||
|
||||
Run <u>accelerate config</u> on your machine:
|
||||
|
||||
```bash
|
||||
$ accelerate config
|
||||
-----------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
In which compute environment are you running?
|
||||
This machine
|
||||
-----------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Which type of machine are you using?
|
||||
No distributed training
|
||||
Do you want to run your training on CPU only (even if a GPU / Apple Silicon device is available)? [yes/NO]:yes
|
||||
Do you want to use Intel PyTorch Extension (IPEX) to speed up training on CPU? [yes/NO]:yes
|
||||
Do you wish to optimize your script with torch dynamo?[yes/NO]:NO
|
||||
Do you want to use DeepSpeed? [yes/NO]: NO
|
||||
-----------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Do you wish to use FP16 or BF16 (mixed precision)?
|
||||
bf16
|
||||
```
|
||||
This will generate a config file that will be used automatically to properly set the
|
||||
default options when doing
|
||||
|
||||
```bash
|
||||
accelerate launch my_script.py --args_to_my_script
|
||||
```
|
||||
|
||||
For instance, here is how you would run the NLP example `examples/nlp_example.py` (from the root of the repo) with IPEX enabled.
|
||||
default_config.yaml that is generated after `accelerate config`
|
||||
|
||||
```bash
|
||||
compute_environment: LOCAL_MACHINE
|
||||
distributed_type: 'NO'
|
||||
downcast_bf16: 'no'
|
||||
ipex_config:
|
||||
ipex: true
|
||||
machine_rank: 0
|
||||
main_training_function: main
|
||||
mixed_precision: bf16
|
||||
num_machines: 1
|
||||
num_processes: 1
|
||||
rdzv_backend: static
|
||||
same_network: true
|
||||
tpu_env: []
|
||||
tpu_use_cluster: false
|
||||
tpu_use_sudo: false
|
||||
use_cpu: true
|
||||
```
|
||||
```bash
|
||||
accelerate launch examples/nlp_example.py
|
||||
```
|
||||
|
||||
**Scenario 2**: Acceleration of distributed CPU training
|
||||
we use Intel oneCCL for communication, combined with Intel® MPI library to deliver flexible, efficient, scalable cluster messaging on Intel® architecture. you could refer the [here](https://huggingface.co/docs/transformers/perf_train_cpu_many) for the installation guide
|
||||
|
||||
Run <u>accelerate config</u> on your machine(node0):
|
||||
|
||||
```bash
|
||||
$ accelerate config
|
||||
-----------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
In which compute environment are you running?
|
||||
This machine
|
||||
-----------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Which type of machine are you using?
|
||||
multi-CPU
|
||||
How many different machines will you use (use more than 1 for multi-node training)? [1]: 4
|
||||
-----------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
What is the rank of this machine?
|
||||
0
|
||||
What is the IP address of the machine that will host the main process? 36.112.23.24
|
||||
What is the port you will use to communicate with the main process? 29500
|
||||
Are all the machines on the same local network? Answer `no` if nodes are on the cloud and/or on different network hosts [YES/no]: yes
|
||||
Do you want to use Intel PyTorch Extension (IPEX) to speed up training on CPU? [yes/NO]:yes
|
||||
Do you wish to optimize your script with torch dynamo?[yes/NO]:NO
|
||||
How many CPU(s) should be used for distributed training? [1]:16
|
||||
-----------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Do you wish to use FP16 or BF16 (mixed precision)?
|
||||
bf16
|
||||
```
|
||||
For instance, here is how you would run the NLP example `examples/nlp_example.py` (from the root of the repo) with IPEX enabled for distributed CPU training.
|
||||
|
||||
default_config.yaml that is generated after `accelerate config`
|
||||
```bash
|
||||
compute_environment: LOCAL_MACHINE
|
||||
distributed_type: MULTI_CPU
|
||||
downcast_bf16: 'no'
|
||||
ipex_config:
|
||||
ipex: true
|
||||
machine_rank: 0
|
||||
main_process_ip: 36.112.23.24
|
||||
main_process_port: 29500
|
||||
main_training_function: main
|
||||
mixed_precision: bf16
|
||||
num_machines: 4
|
||||
num_processes: 16
|
||||
rdzv_backend: static
|
||||
same_network: true
|
||||
tpu_env: []
|
||||
tpu_use_cluster: false
|
||||
tpu_use_sudo: false
|
||||
use_cpu: true
|
||||
```
|
||||
|
||||
Set following env and using intel MPI to launch the training
|
||||
|
||||
In node0, you need to create a configuration file which contains the IP addresses of each node (for example hostfile) and pass that configuration file path as an argument.
|
||||
```bash
|
||||
$ cat hostfile
|
||||
xxx.xxx.xxx.xxx #node0 ip
|
||||
xxx.xxx.xxx.xxx #node1 ip
|
||||
xxx.xxx.xxx.xxx #node2 ip
|
||||
xxx.xxx.xxx.xxx #node3 ip
|
||||
```
|
||||
Now, run the following command in node0 and **16DDP** will be enabled in node0,node1,node2,node3 with BF16 mixed precision:
|
||||
```bash
|
||||
oneccl_bindings_for_pytorch_path=$(python -c "from oneccl_bindings_for_pytorch import cwd; print(cwd)")
|
||||
source $oneccl_bindings_for_pytorch_path/env/setvars.sh
|
||||
export CCL_WORKER_COUNT=1
|
||||
export MASTER_ADDR=xxx.xxx.xxx.xxx #node0 ip
|
||||
export CCL_ATL_TRANSPORT=ofi
|
||||
mpirun -f hostfile -n 16 -ppn 4 accelerate launch examples/nlp_example.py
|
||||
```
|
||||
|
||||
## Related Resources
|
||||
|
||||
- [Project's github](https://github.com/intel/intel-extension-for-pytorch)
|
||||
- [API docs](https://intel.github.io/intel-extension-for-pytorch/cpu/latest/tutorials/api_doc.html)
|
||||
- [Tuning guide](https://intel.github.io/intel-extension-for-pytorch/cpu/latest/tutorials/performance_tuning/tuning_guide.html)
|
||||
- [Blogs & Publications](https://intel.github.io/intel-extension-for-pytorch/cpu/latest/tutorials/blogs_publications.html)
|
||||
|
||||
108
docs/source/usage_guides/local_sgd.md
Normal file
108
docs/source/usage_guides/local_sgd.md
Normal file
@ -0,0 +1,108 @@
|
||||
<!--Copyright 2023 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Using Local SGD with 🤗 Accelerate
|
||||
|
||||
Local SGD is a technique for distributed training where gradients are not synchronized every step. Thus, each process updates its own version of the model weights and after a given number of steps these weights are synchronized by averaging across all processes. This improves communication efficiency and can lead to substantial training speed up especially when a computer lacks a faster interconnect such as NVLink.
|
||||
Unlike gradient accumulation (where improving communication efficiency requires increasing the effective batch size), Local SGD does not require changing a batch size or a learning rate / schedule. However, if necessary, Local SGD can be combined with gradient accumulation as well.
|
||||
|
||||
In this tutorial you will see how to quickly setup Local SGD 🤗 Accelerate. Compared to a standard Accelerate setup, this requires only two extra lines of code.
|
||||
|
||||
This example will use a very simplistic PyTorch training loop that performs gradient accumulation every two batches:
|
||||
|
||||
```python
|
||||
device = "cuda"
|
||||
model.to(device)
|
||||
|
||||
gradient_accumulation_steps = 2
|
||||
|
||||
for index, batch in enumerate(training_dataloader):
|
||||
inputs, targets = batch
|
||||
inputs = inputs.to(device)
|
||||
targets = targets.to(device)
|
||||
outputs = model(inputs)
|
||||
loss = loss_function(outputs, targets)
|
||||
loss = loss / gradient_accumulation_steps
|
||||
loss.backward()
|
||||
if (index + 1) % gradient_accumulation_steps == 0:
|
||||
optimizer.step()
|
||||
scheduler.step()
|
||||
optimizer.zero_grad()
|
||||
```
|
||||
|
||||
## Converting it to 🤗 Accelerate
|
||||
|
||||
First the code shown earlier will be converted to use 🤗 Accelerate with neither a LocalSGD or a gradient accumulation helper:
|
||||
|
||||
```diff
|
||||
+ from accelerate import Accelerator
|
||||
+ accelerator = Accelerator()
|
||||
|
||||
+ model, optimizer, training_dataloader, scheduler = accelerator.prepare(
|
||||
+ model, optimizer, training_dataloader, scheduler
|
||||
+ )
|
||||
|
||||
for index, batch in enumerate(training_dataloader):
|
||||
inputs, targets = batch
|
||||
- inputs = inputs.to(device)
|
||||
- targets = targets.to(device)
|
||||
outputs = model(inputs)
|
||||
loss = loss_function(outputs, targets)
|
||||
loss = loss / gradient_accumulation_steps
|
||||
+ accelerator.backward(loss)
|
||||
if (index+1) % gradient_accumulation_steps == 0:
|
||||
optimizer.step()
|
||||
scheduler.step()
|
||||
```
|
||||
|
||||
## Letting 🤗 Accelerate handle model synchronization
|
||||
|
||||
All that is left now is to let 🤗 Accelerate handle model parameter synchronization **and** the gradient accumulation for us. For simplicity let us assume we need to synchronize every 8 steps. This is
|
||||
achieved by adding one `with LocalSGD` statement and one call `local_sgd.step()` after every optimizer step:
|
||||
|
||||
```diff
|
||||
+local_sgd_steps=8
|
||||
|
||||
+with LocalSGD(accelerator=accelerator, model=model, local_sgd_steps=8, enabled=True) as local_sgd:
|
||||
for batch in training_dataloader:
|
||||
with accelerator.accumulate(model):
|
||||
inputs, targets = batch
|
||||
outputs = model(inputs)
|
||||
loss = loss_function(outputs, targets)
|
||||
accelerator.backward(loss)
|
||||
optimizer.step()
|
||||
scheduler.step()
|
||||
optimizer.zero_grad()
|
||||
+ local_sgd.step()
|
||||
```
|
||||
|
||||
Under the hood, the Local SGD code **disables** automatic gradient synchornization (but accumulation still works as expected!). Instead it averages model parameters every `local_sgd_steps` steps (as well as in the end of the training loop).
|
||||
|
||||
## Limitations
|
||||
|
||||
The current implementation works only with basic multi-GPU (or multi-CPU) training without, e.g., [DeepSpeed.](https://github.com/microsoft/DeepSpeed).
|
||||
|
||||
## References
|
||||
|
||||
Although we are not aware of the true origins of this simple approach, the idea of local SGD is quite old and goes
|
||||
back to at least:
|
||||
|
||||
Zhang, J., De Sa, C., Mitliagkas, I., & Ré, C. (2016). [Parallel SGD: When does averaging help?. arXiv preprint
|
||||
arXiv:1606.07365.](https://arxiv.org/abs/1606.07365)
|
||||
|
||||
We credit the term Local SGD to the following paper (but there might be earlier references we are not aware of).
|
||||
|
||||
Stich, Sebastian Urban. ["Local SGD Converges Fast and Communicates Little." ICLR 2019-International Conference on
|
||||
Learning Representations. No. CONF. 2019.](https://arxiv.org/abs/1805.09767)
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
|
||||
@ -115,7 +118,7 @@ An example of thr corresponding questions for using Megatron-LM features is show
|
||||
```bash
|
||||
:~$ accelerate config --config_file "megatron_gpt_config.yaml"
|
||||
In which compute environment are you running? ([0] This machine, [1] AWS (Amazon SageMaker)): 0
|
||||
Which type of machine are you using? ([0] No distributed training, [1] multi-CPU, [2] multi-GPU, [3] TPU [4] MPS): 2
|
||||
Which type of machine are you using? ([0] No distributed training, [1] multi-CPU, [2] multi-GPU, [3] TPU): 2
|
||||
How many different machines will you use (use more than 1 for multi-node training)? [1]:
|
||||
Do you want to use DeepSpeed? [yes/NO]:
|
||||
Do you want to use FullyShardedDataParallel? [yes/NO]:
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Memory Utilities
|
||||
121
docs/source/usage_guides/model_size_estimator.md
Normal file
121
docs/source/usage_guides/model_size_estimator.md
Normal file
@ -0,0 +1,121 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Understanding how big of a model can fit on your machine
|
||||
|
||||
One very difficult aspect when exploring potential models to use on your machine is knowing just how big of a model will *fit* into memory with your current graphics card (such as loading the model onto CUDA).
|
||||
|
||||
To help alleviate this, 🤗 Accelerate has a CLI interface through `accelerate estimate-memory`. This tutorial will
|
||||
help walk you through using it, what to expect, and at the end link to the interactive demo hosted on the 🤗 Hub which will
|
||||
even let you post those results directly on the model repo!
|
||||
|
||||
Currently we support searching for models that can be used in `timm` and `transformers`.
|
||||
|
||||
<Tip>
|
||||
|
||||
This API will load the model into memory on the `meta` device, so we are not actually downloading
|
||||
and loading the full weights of the model into memory, nor do we need to. As a result it's
|
||||
perfectly fine to measure 8 billion parameter models (or more), without having to worry about
|
||||
if your CPU can handle it!
|
||||
|
||||
</Tip>
|
||||
|
||||
## The Command
|
||||
|
||||
When using `accelerate estimate-memory`, you need to pass in the name of the model you want to use, potentially the framework
|
||||
that model utilizing (if it can't be found automatically), and the data types you want the model to be loaded in with.
|
||||
|
||||
For example, here is how we can calculate the memory footprint for `bert-base-cased`:
|
||||
|
||||
```bash
|
||||
accelerate estimate-memory bert-base-cased
|
||||
```
|
||||
|
||||
This will download the `config.json` for `bert-based-cased`, load the model on the `meta` device, and report back how much space
|
||||
it will use:
|
||||
|
||||
Memory Usage for loading `bert-base-cased`:
|
||||
|
||||
| dtype | Largest Layer | Total Size | Training using Adam |
|
||||
|---------|---------------|------------|---------------------|
|
||||
| float32 | 84.95 MB | 418.18 MB | 1.61 GB |
|
||||
| float16 | 42.47 MB | 206.59 MB | 826.36 MB |
|
||||
| int8 | 21.24 MB | 103.29 MB | 413.18 MB |
|
||||
| int4 | 10.62 MB | 51.65 MB | 206.59 MB |
|
||||
|
||||
By default it will return all the supported dtypes (`int4` through `float32`), but if you are interested in specific ones these can be filtered.
|
||||
|
||||
### Specific libraries
|
||||
|
||||
If the source library cannot be determined automatically (like it could in the case of `bert-base-cased`), a library name can
|
||||
be passed in.
|
||||
|
||||
```bash
|
||||
accelerate estimate-memory HuggingFaceM4/idefics-80b-instruct --library_name transformers
|
||||
```
|
||||
|
||||
Memory Usage for loading `HuggingFaceM4/idefics-80b-instruct`:
|
||||
|
||||
| dtype | Largest Layer | Total Size | Training using Adam |
|
||||
|---------|---------------|------------|---------------------|
|
||||
| float32 | 3.02 GB | 297.12 GB | 1.16 TB |
|
||||
| float16 | 1.51 GB | 148.56 GB | 594.24 GB |
|
||||
| int8 | 772.52 MB | 74.28 GB | 297.12 GB |
|
||||
| int4 | 386.26 MB | 37.14 GB | 148.56 GB |
|
||||
|
||||
|
||||
```bash
|
||||
accelerate estimate-memory timm/resnet50.a1_in1k --library_name timm
|
||||
```
|
||||
|
||||
Memory Usage for loading `timm/resnet50.a1_in1k`:
|
||||
|
||||
| dtype | Largest Layer | Total Size | Training using Adam |
|
||||
|---------|---------------|------------|---------------------|
|
||||
| float32 | 9.0 MB | 97.7 MB | 390.78 MB |
|
||||
| float16 | 4.5 MB | 48.85 MB | 195.39 MB |
|
||||
| int8 | 2.25 MB | 24.42 MB | 97.7 MB |
|
||||
| int4 | 1.12 MB | 12.21 MB | 48.85 MB |
|
||||
|
||||
### Specific dtypes
|
||||
|
||||
As mentioned earlier, while we return `int4` through `float32` by default, any dtype can be used from `float32`, `float16`, `int8`, and `int4`.
|
||||
|
||||
To do so, pass them in after specifying `--dtypes`:
|
||||
|
||||
```bash
|
||||
accelerate estimate-memory bert-base-cased --dtypes float32 float16
|
||||
```
|
||||
|
||||
Memory Usage for loading `bert-base-cased`:
|
||||
|
||||
| dtype | Largest Layer | Total Size | Training using Adam |
|
||||
|---------|---------------|------------|---------------------|
|
||||
| float32 | 84.95 MB | 413.18 MB | 1.61 GB |
|
||||
| float16 | 42.47 MB | 206.59 MB | 826.36 MB |
|
||||
|
||||
## Caveats with this calculator
|
||||
|
||||
This calculator will tell you how much memory is needed to purely load the model in, *not* to perform inference.
|
||||
|
||||
This calculation is accurate within a few % of the actual value, so it is a very good view of just how much memory it will take. For instance loading `bert-base-cased` actually takes `413.68 MB` when loaded on CUDA in full precision, and the calculator estimates `413.18 MB`.
|
||||
|
||||
When performing inference you can expect to add up to an additional 20% as found by [EleutherAI](https://blog.eleuther.ai/transformer-math/). We'll be conducting research into finding a more accurate estimate to these values, and will update
|
||||
this calculator once done.
|
||||
|
||||
## Live Gradio Demo
|
||||
|
||||
Lastly, we invite you to try the [live Gradio demo](https://huggingface.co/spaces/hf-accelerate/model-memory-usage) of this utility,
|
||||
which includes an option to post a discussion thread on a models repository with this data. Doing so will help provide access to these numbers in the community faster and help users know what you've learned!
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Accelerated PyTorch Training on Mac
|
||||
136
docs/source/usage_guides/quantization.md
Normal file
136
docs/source/usage_guides/quantization.md
Normal file
@ -0,0 +1,136 @@
|
||||
<!--Copyright 2023 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Quantization
|
||||
|
||||
## `bitsandbytes` Integration
|
||||
|
||||
🤗 Accelerate brings `bitsandbytes` quantization to your model. You can now load any pytorch model in 8-bit or 4-bit with a few lines of code.
|
||||
|
||||
If you want to use 🤗 Transformers models with `bitsandbytes`, you should follow this [documentation](https://huggingface.co/docs/transformers/main_classes/quantization).
|
||||
|
||||
To learn more about how the `bitsandbytes` quantization works, check out the blog posts on [8-bit quantization](https://huggingface.co/blog/hf-bitsandbytes-integration) and [4-bit quantization](https://huggingface.co/blog/4bit-transformers-bitsandbytes).
|
||||
|
||||
### Pre-Requisites
|
||||
You will need to install the following requirements:
|
||||
|
||||
- Install `bitsandbytes` library
|
||||
```bash
|
||||
pip install bitsandbytes
|
||||
```
|
||||
- Install latest `accelerate` from source
|
||||
```bash
|
||||
pip install git+https://github.com/huggingface/accelerate.git
|
||||
```
|
||||
- Install `minGPT` and `huggingface_hub` to run examples
|
||||
```bash
|
||||
git clone https://github.com/karpathy/minGPT.git
|
||||
pip install minGPT/
|
||||
pip install huggingface_hub
|
||||
```
|
||||
|
||||
### How it works
|
||||
|
||||
First, we need to initialize our model. To save memory, we can initialize an empty model using the context manager [`init_empty_weights`].
|
||||
|
||||
Let's take the GPT2 model from minGPT library.
|
||||
```py
|
||||
from accelerate import init_empty_weights
|
||||
from mingpt.model import GPT
|
||||
|
||||
model_config = GPT.get_default_config()
|
||||
model_config.model_type = 'gpt2-xl'
|
||||
model_config.vocab_size = 50257
|
||||
model_config.block_size = 1024
|
||||
|
||||
with init_empty_weights():
|
||||
empty_model = GPT(model_config)
|
||||
```
|
||||
|
||||
Then, we need to get the path to the weights of your model. The path can be the state_dict file (e.g. "pytorch_model.bin") or a folder containing the sharded checkpoints.
|
||||
|
||||
```py
|
||||
from huggingface_hub import snapshot_download
|
||||
weights_location = snapshot_download(repo_id="marcsun13/gpt2-xl-linear-sharded")
|
||||
```
|
||||
|
||||
Finally, you need to set your quantization configuration with [`~utils.BnbQuantizationConfig`].
|
||||
|
||||
Here's an example for 8-bit quantization:
|
||||
```py
|
||||
from accelerate.utils import BnbQuantizationConfig
|
||||
bnb_quantization_config = BnbQuantizationConfig(load_in_8bit=True, llm_int8_threshold = 6)
|
||||
```
|
||||
|
||||
Here's an example for 4-bit quantization:
|
||||
```py
|
||||
from accelerate.utils import BnbQuantizationConfig
|
||||
bnb_quantization_config = BnbQuantizationConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16, bnb_4bit_use_double_quant=True, bnb_4bit_quant_type="nf4")
|
||||
```
|
||||
|
||||
To quantize your empty model with the selected configuration, you need to use [`~utils.load_and_quantize_model`].
|
||||
|
||||
```py
|
||||
from accelerate.utils import load_and_quantize_model
|
||||
quantized_model = load_and_quantize_model(empty_model, weights_location=weights_location, bnb_quantization_config=bnb_quantization_config, device_map = "auto")
|
||||
```
|
||||
|
||||
### Saving and loading 8-bit model
|
||||
|
||||
You can save your 8-bit model with accelerate using [`~Accelerator.save_model`].
|
||||
|
||||
```py
|
||||
from accelerate import Accelerator
|
||||
accelerate = Accelerator()
|
||||
new_weights_location = "path/to/save_directory"
|
||||
accelerate.save_model(quantized_model, new_weights_location)
|
||||
|
||||
quantized_model_from_saved = load_and_quantize_model(empty_model, weights_location=new_weights_location, bnb_quantization_config=bnb_quantization_config, device_map = "auto")
|
||||
```
|
||||
|
||||
Note that 4-bit model serialization is currently not supported.
|
||||
|
||||
### Offload modules to cpu and disk
|
||||
|
||||
You can offload some modules to cpu/disk if you don't have enough space on the GPU to store the entire model on your GPUs.
|
||||
This uses big model inference under the hood. Check this [documentation](https://huggingface.co/docs/accelerate/usage_guides/big_modeling) for more details.
|
||||
|
||||
For 8-bit quantization, the selected modules will be converted to 8-bit precision.
|
||||
|
||||
For 4-bit quantization, the selected modules will be kept in `torch_dtype` that the user passed in `BnbQuantizationConfig`. We will add support to convert these offloaded modules in 4-bit when 4-bit serialization will be possible.
|
||||
|
||||
You just need to pass a custom `device_map` in order to offload modules on cpu/disk. The offload modules will be dispatched on the GPU when needed. Here's an example :
|
||||
|
||||
```py
|
||||
device_map = {
|
||||
"transformer.wte": 0,
|
||||
"transformer.wpe": 0,
|
||||
"transformer.drop": 0,
|
||||
"transformer.h": "cpu",
|
||||
"transformer.ln_f": "disk",
|
||||
"lm_head": "disk",
|
||||
}
|
||||
```
|
||||
### Fine-tune a quantized model
|
||||
|
||||
It is not possible to perform pure 8bit or 4bit training on these models. However, you can train these models by leveraging parameter efficient fine tuning methods (PEFT) and train for example adapters on top of them. Please have a look at [peft](https://github.com/huggingface/peft) library for more details.
|
||||
|
||||
Currently, you can't add adapters on top of any quantized model. However, with the official support of adapters with 🤗 Transformers models, you can fine-tune quantized models. If you want to finetune a 🤗 Transformers model , follow this [documentation](https://huggingface.co/docs/transformers/main_classes/quantization) instead. Check out this [demo](https://colab.research.google.com/drive/1VoYNfYDKcKRQRor98Zbf2-9VQTtGJ24k?usp=sharing) on how to fine-tune a 4-bit 🤗 Transformers model.
|
||||
|
||||
Note that you don’t need to pass `device_map` when loading the model for training. It will automatically load your model on your GPU. Please note that `device_map=auto` should be used for inference only.
|
||||
|
||||
### Example demo - running GPT2 1.5b on a Google Colab
|
||||
|
||||
Check out the Google Colab [demo](https://colab.research.google.com/drive/1T1pOgewAWVpR9gKpaEWw4orOrzPFb3yM?usp=sharing) for running quantized models on a GTP2 model. The GPT2-1.5B model checkpoint is in FP32 which uses 6GB of memory. After quantization, it uses 1.6GB with 8-bit modules and 1.2GB with 4-bit modules.
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Amazon SageMaker
|
||||
@ -8,6 +8,9 @@ http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Tracking
|
||||
@ -83,11 +86,15 @@ for iteration in config["num_iterations"]:
|
||||
accelerator.end_training()
|
||||
```
|
||||
|
||||
If a tracker requires a directory to save data to such as `TensorBoard` then a `logging_dir` or `project_dir` can be passed in. `project_dir` is useful
|
||||
if there are other further configurations such as those which can be combined with the [`~utils.ProjectConfiguration`] dataclass.
|
||||
If a tracker requires a directory to save data to, such as `TensorBoard`, then pass the directory path to `project_dir`. The `project_dir` parameter is useful
|
||||
when there are other configurations to be combined with in the [`~utils.ProjectConfiguration`] data class. For example, you can save the TensorBoard data to `project_dir` and everything else can be logged in the `logging_dir` parameter of [`~utils.ProjectConfiguration`:
|
||||
|
||||
```python
|
||||
accelerator = Accelerator(log_with="tensorboard", logging_dir=".")
|
||||
accelerator = Accelerator(log_with="tensorboard", project_dir=".")
|
||||
|
||||
# use with ProjectConfiguration
|
||||
config = ProjectConfiguration(project_dir=".", logging_dir="another/directory")
|
||||
accelerator = Accelerator(log_with="tensorboard", project_config=config)
|
||||
```
|
||||
|
||||
## Implementing Custom Trackers
|
||||
175
docs/source/usage_guides/training_zoo.md
Normal file
175
docs/source/usage_guides/training_zoo.md
Normal file
@ -0,0 +1,175 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
|
||||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
|
||||
rendered properly in your Markdown viewer.
|
||||
-->
|
||||
|
||||
# Example Zoo
|
||||
|
||||
Below contains a non-exhuastive list of tutorials and scripts showcasing 🤗 Accelerate
|
||||
|
||||
## Official Accelerate Examples:
|
||||
|
||||
### Basic Examples
|
||||
|
||||
These examples showcase the base features of Accelerate and are a great starting point
|
||||
|
||||
- [Barebones NLP example](https://github.com/huggingface/accelerate/blob/main/examples/nlp_example.py)
|
||||
- [Barebones distributed NLP example in a Jupyter Notebook](https://github.com/huggingface/notebooks/blob/main/examples/accelerate_examples/simple_nlp_example.ipynb)
|
||||
- [Barebones computer vision example](https://github.com/huggingface/accelerate/blob/main/examples/cv_example.py)
|
||||
- [Barebones distributed computer vision example in a Jupyter Notebook](https://github.com/huggingface/notebooks/blob/main/examples/accelerate_examples/simple_cv_example.ipynb)
|
||||
- [Using Accelerate in Kaggle](https://www.kaggle.com/code/muellerzr/multi-gpu-and-accelerate)
|
||||
|
||||
### Feature Specific Examples
|
||||
|
||||
These examples showcase specific features that the Accelerate framework offers
|
||||
|
||||
- [Automatic memory-aware gradient accumulation](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/automatic_gradient_accumulation.py)
|
||||
- [Checkpointing states](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/checkpointing.py)
|
||||
- [Cross validation](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/cross_validation.py)
|
||||
- [DeepSpeed](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/deepspeed_with_config_support.py)
|
||||
- [Fully Sharded Data Parallelism](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/fsdp_with_peak_mem_tracking.py)
|
||||
- [Gradient accumulation](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/gradient_accumulation.py)
|
||||
- [Memory-aware batch size finder](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/memory.py)
|
||||
- [Metric Computation](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/multi_process_metrics.py)
|
||||
- [Using Trackers](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/tracking.py)
|
||||
- [Using Megatron-LM](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/megatron_lm_gpt_pretraining.py)
|
||||
|
||||
### Full Examples
|
||||
|
||||
These examples showcase every feature in Accelerate at once that was shown in "Feature Specific Examples"
|
||||
|
||||
- [Complete NLP example](https://github.com/huggingface/accelerate/blob/main/examples/complete_nlp_example.py)
|
||||
- [Complete computer vision example](https://github.com/huggingface/accelerate/blob/main/examples/complete_cv_example.py)
|
||||
- [Very complete and extensible vision example showcasing SLURM, hydra, and a very extensible usage of the framework](https://github.com/yuvalkirstain/PickScore)
|
||||
- [Causal language model fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/language-modeling/run_clm_no_trainer.py)
|
||||
- [Masked language model fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/language-modeling/run_mlm_no_trainer.py)
|
||||
- [Speech pretraining example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/speech-pretraining/run_wav2vec2_pretraining_no_trainer.py)
|
||||
- [Translation fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/translation/run_translation_no_trainer.py)
|
||||
- [Text classification fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue_no_trainer.py)
|
||||
- [Semantic segmentation fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py)
|
||||
- [Question answering fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/question-answering/run_qa_no_trainer.py)
|
||||
- [Beam search question answering fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/question-answering/run_qa_beam_search_no_trainer.py)
|
||||
- [Multiple choice question answering fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/multiple-choice/run_swag_no_trainer.py)
|
||||
- [Named entity recognition fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/token-classification/run_ner_no_trainer.py)
|
||||
- [Image classification fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/image-classification/run_image_classification_no_trainer.py)
|
||||
- [Summarization fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/summarization/run_summarization_no_trainer.py)
|
||||
- [End-to-end examples on how to use AWS SageMaker integration of Accelerate](https://github.com/huggingface/notebooks/blob/main/sagemaker/22_accelerate_sagemaker_examples/README.md)
|
||||
- [Megatron-LM examples for various NLp tasks](https://github.com/pacman100/accelerate-megatron-test)
|
||||
|
||||
## Integration Examples
|
||||
|
||||
These are tutorials from libraries that integrate with 🤗 Accelerate:
|
||||
|
||||
> Don't find your integration here? Make a PR to include it!
|
||||
|
||||
### Catalyst
|
||||
|
||||
- [Distributed training tutorial with Catalyst](https://catalyst-team.github.io/catalyst/tutorials/ddp.html)
|
||||
|
||||
### DALLE2-pytorch
|
||||
|
||||
- [Fine-tuning DALLE2](https://github.com/lucidrains/DALLE2-pytorch#usage)
|
||||
|
||||
### 🤗 diffusers
|
||||
|
||||
- [Performing textual inversion with diffusers](https://github.com/huggingface/diffusers/tree/main/examples/textual_inversion)
|
||||
- [Training DreamBooth with diffusers](https://github.com/huggingface/diffusers/tree/main/examples/dreambooth)
|
||||
|
||||
### fastai
|
||||
|
||||
- [Distributed training from Jupyter Notebooks with fastai](https://docs.fast.ai/tutorial.distributed.html)
|
||||
- [Basic distributed training examples with fastai](https://docs.fast.ai/examples/distributed_app_examples.html)
|
||||
|
||||
### GradsFlow
|
||||
|
||||
- [Auto Image Classification with GradsFlow](https://docs.gradsflow.com/en/latest/examples/nbs/01-ImageClassification/)
|
||||
|
||||
### imagen-pytorch
|
||||
|
||||
- [Fine-tuning Imagen](https://github.com/lucidrains/imagen-pytorch#usage)
|
||||
|
||||
### Kornia
|
||||
|
||||
- [Fine-tuning vision models with Kornia's Trainer](https://kornia.readthedocs.io/en/latest/get-started/training.html)
|
||||
|
||||
### PyTorch Accelerated
|
||||
|
||||
- [Quickstart distributed training tutorial with PyTorch Accelerated](https://pytorch-accelerated.readthedocs.io/en/latest/quickstart.html)
|
||||
|
||||
### PyTorch3D
|
||||
|
||||
- [Perform Deep Learning with 3D data](https://pytorch3d.org/tutorials/)
|
||||
|
||||
### Stable-Dreamfusion
|
||||
|
||||
- [Training with Stable-Dreamfusion to convert text to a 3D model](https://colab.research.google.com/drive/1MXT3yfOFvO0ooKEfiUUvTKwUkrrlCHpF?usp=sharing)
|
||||
|
||||
### Tez
|
||||
|
||||
- [Leaf disease detection with Tez and Accelerate](https://www.kaggle.com/code/abhishek/tez-faster-and-easier-training-for-leaf-detection/notebook)
|
||||
|
||||
### trlx
|
||||
|
||||
- [How to implement a sentiment learning task with trlx](https://github.com/CarperAI/trlx#example-how-to-add-a-task)
|
||||
|
||||
### Comfy-UI
|
||||
|
||||
- [Enabling using large Stable Diffusion Models in low-vram settings using Accelerate](https://github.com/comfyanonymous/ComfyUI/blob/master/comfy/model_management.py#L291-L296)
|
||||
|
||||
|
||||
## In Science
|
||||
|
||||
Below contains a non-exhaustive list of papers utilizing 🤗 Accelerate.
|
||||
|
||||
> Don't find your paper here? Make a PR to include it!
|
||||
|
||||
* Yuval Kirstain, Adam Polyak, Uriel Singer, Shahbuland Matiana, Joe Penna, Omer Levy: “Pick-a-Pic: An Open Dataset of User Preferences for Text-to-Image Generation”, 2023; [arXiv:2305.01569](http://arxiv.org/abs/2305.01569).
|
||||
* Lei Wang, Wanyu Xu, Yihuai Lan, Zhiqiang Hu, Yunshi Lan, Roy Ka-Wei Lee, Ee-Peng Lim: “Plan-and-Solve Prompting: Improving Zero-Shot Chain-of-Thought Reasoning by Large Language Models”, 2023; [arXiv:2305.04091](http://arxiv.org/abs/2305.04091).
|
||||
* Arthur Câmara, Claudia Hauff: “Moving Stuff Around: A study on efficiency of moving documents into memory for Neural IR models”, 2022; [arXiv:2205.08343](http://arxiv.org/abs/2205.08343).
|
||||
* Ying Sheng, Lianmin Zheng, Binhang Yuan, Zhuohan Li, Max Ryabinin, Daniel Y. Fu, Zhiqiang Xie, Beidi Chen, Clark Barrett, Joseph E. Gonzalez, Percy Liang, Christopher Ré, Ion Stoica, Ce Zhang: “High-throughput Generative Inference of Large Language Models with a Single GPU”, 2023; [arXiv:2303.06865](http://arxiv.org/abs/2303.06865).
|
||||
* Peter Melchior, Yan Liang, ChangHoon Hahn, Andy Goulding: “Autoencoding Galaxy Spectra I: Architecture”, 2022; [arXiv:2211.07890](http://arxiv.org/abs/2211.07890).
|
||||
* Jiaao Chen, Aston Zhang, Mu Li, Alex Smola, Diyi Yang: “A Cheaper and Better Diffusion Language Model with Soft-Masked Noise”, 2023; [arXiv:2304.04746](http://arxiv.org/abs/2304.04746).
|
||||
* Ayaan Haque, Matthew Tancik, Alexei A. Efros, Aleksander Holynski, Angjoo Kanazawa: “Instruct-NeRF2NeRF: Editing 3D Scenes with Instructions”, 2023; [arXiv:2303.12789](http://arxiv.org/abs/2303.12789).
|
||||
* Luke Melas-Kyriazi, Christian Rupprecht, Iro Laina, Andrea Vedaldi: “RealFusion: 360° Reconstruction of Any Object from a Single Image”, 2023; [arXiv:2302.10663](http://arxiv.org/abs/2302.10663).
|
||||
* Xiaoshi Wu, Keqiang Sun, Feng Zhu, Rui Zhao, Hongsheng Li: “Better Aligning Text-to-Image Models with Human Preference”, 2023; [arXiv:2303.14420](http://arxiv.org/abs/2303.14420).
|
||||
* Yongliang Shen, Kaitao Song, Xu Tan, Dongsheng Li, Weiming Lu, Yueting Zhuang: “HuggingGPT: Solving AI Tasks with ChatGPT and its Friends in HuggingFace”, 2023; [arXiv:2303.17580](http://arxiv.org/abs/2303.17580).
|
||||
* Yue Yang, Wenlin Yao, Hongming Zhang, Xiaoyang Wang, Dong Yu, Jianshu Chen: “Z-LaVI: Zero-Shot Language Solver Fueled by Visual Imagination”, 2022; [arXiv:2210.12261](http://arxiv.org/abs/2210.12261).
|
||||
* Sheng-Yen Chou, Pin-Yu Chen, Tsung-Yi Ho: “How to Backdoor Diffusion Models?”, 2022; [arXiv:2212.05400](http://arxiv.org/abs/2212.05400).
|
||||
* Junyoung Seo, Wooseok Jang, Min-Seop Kwak, Jaehoon Ko, Hyeonsu Kim, Junho Kim, Jin-Hwa Kim, Jiyoung Lee, Seungryong Kim: “Let 2D Diffusion Model Know 3D-Consistency for Robust Text-to-3D Generation”, 2023; [arXiv:2303.07937](http://arxiv.org/abs/2303.07937).
|
||||
* Or Patashnik, Daniel Garibi, Idan Azuri, Hadar Averbuch-Elor, Daniel Cohen-Or: “Localizing Object-level Shape Variations with Text-to-Image Diffusion Models”, 2023; [arXiv:2303.11306](http://arxiv.org/abs/2303.11306).
|
||||
* Dídac Surís, Sachit Menon, Carl Vondrick: “ViperGPT: Visual Inference via Python Execution for Reasoning”, 2023; [arXiv:2303.08128](http://arxiv.org/abs/2303.08128).
|
||||
* Chenyang Qi, Xiaodong Cun, Yong Zhang, Chenyang Lei, Xintao Wang, Ying Shan, Qifeng Chen: “FateZero: Fusing Attentions for Zero-shot Text-based Video Editing”, 2023; [arXiv:2303.09535](http://arxiv.org/abs/2303.09535).
|
||||
* Sean Welleck, Jiacheng Liu, Ximing Lu, Hannaneh Hajishirzi, Yejin Choi: “NaturalProver: Grounded Mathematical Proof Generation with Language Models”, 2022; [arXiv:2205.12910](http://arxiv.org/abs/2205.12910).
|
||||
* Elad Richardson, Gal Metzer, Yuval Alaluf, Raja Giryes, Daniel Cohen-Or: “TEXTure: Text-Guided Texturing of 3D Shapes”, 2023; [arXiv:2302.01721](http://arxiv.org/abs/2302.01721).
|
||||
* Puijin Cheng, Li Lin, Yijin Huang, Huaqing He, Wenhan Luo, Xiaoying Tang: “Learning Enhancement From Degradation: A Diffusion Model For Fundus Image Enhancement”, 2023; [arXiv:2303.04603](http://arxiv.org/abs/2303.04603).
|
||||
* Shun Shao, Yftah Ziser, Shay Cohen: “Erasure of Unaligned Attributes from Neural Representations”, 2023; [arXiv:2302.02997](http://arxiv.org/abs/2302.02997).
|
||||
* Seonghyeon Ye, Hyeonbin Hwang, Sohee Yang, Hyeongu Yun, Yireun Kim, Minjoon Seo: “In-Context Instruction Learning”, 2023; [arXiv:2302.14691](http://arxiv.org/abs/2302.14691).
|
||||
* Shikun Liu, Linxi Fan, Edward Johns, Zhiding Yu, Chaowei Xiao, Anima Anandkumar: “Prismer: A Vision-Language Model with An Ensemble of Experts”, 2023; [arXiv:2303.02506](http://arxiv.org/abs/2303.02506 ).
|
||||
* Haoyu Chen, Zhihua Wang, Yang Yang, Qilin Sun, Kede Ma: “Learning a Deep Color Difference Metric for Photographic Images”, 2023; [arXiv:2303.14964](http://arxiv.org/abs/2303.14964).
|
||||
* Van-Hoang Le, Hongyu Zhang: “Log Parsing with Prompt-based Few-shot Learning”, 2023; [arXiv:2302.07435](http://arxiv.org/abs/2302.07435).
|
||||
* Keito Kudo, Yoichi Aoki, Tatsuki Kuribayashi, Ana Brassard, Masashi Yoshikawa, Keisuke Sakaguchi, Kentaro Inui: “Do Deep Neural Networks Capture Compositionality in Arithmetic Reasoning?”, 2023; [arXiv:2302.07866](http://arxiv.org/abs/2302.07866).
|
||||
* Ruoyao Wang, Peter Jansen, Marc-Alexandre Côté, Prithviraj Ammanabrolu: “Behavior Cloned Transformers are Neurosymbolic Reasoners”, 2022; [arXiv:2210.07382](http://arxiv.org/abs/2210.07382).
|
||||
* Martin Wessel, Tomáš Horych, Terry Ruas, Akiko Aizawa, Bela Gipp, Timo Spinde: “Introducing MBIB -- the first Media Bias Identification Benchmark Task and Dataset Collection”, 2023; [arXiv:2304.13148](http://arxiv.org/abs/2304.13148 ). DOI: [https://dx.doi.org/10.1145/3539618.3591882 10.1145/3539618.3591882].
|
||||
* Hila Chefer, Yuval Alaluf, Yael Vinker, Lior Wolf, Daniel Cohen-Or: “Attend-and-Excite: Attention-Based Semantic Guidance for Text-to-Image Diffusion Models”, 2023; [arXiv:2301.13826](http://arxiv.org/abs/2301.13826).
|
||||
* Marcio Fonseca, Yftah Ziser, Shay B. Cohen: “Factorizing Content and Budget Decisions in Abstractive Summarization of Long Documents”, 2022; [arXiv:2205.12486](http://arxiv.org/abs/2205.12486).
|
||||
* Elad Richardson, Gal Metzer, Yuval Alaluf, Raja Giryes, Daniel Cohen-Or: “TEXTure: Text-Guided Texturing of 3D Shapes”, 2023; [arXiv:2302.01721](http://arxiv.org/abs/2302.01721).
|
||||
* Tianxing He, Jingyu Zhang, Tianle Wang, Sachin Kumar, Kyunghyun Cho, James Glass, Yulia Tsvetkov: “On the Blind Spots of Model-Based Evaluation Metrics for Text Generation”, 2022; [arXiv:2212.10020](http://arxiv.org/abs/2212.10020).
|
||||
* Ori Ram, Yoav Levine, Itay Dalmedigos, Dor Muhlgay, Amnon Shashua, Kevin Leyton-Brown, Yoav Shoham: “In-Context Retrieval-Augmented Language Models”, 2023; [arXiv:2302.00083](http://arxiv.org/abs/2302.00083).
|
||||
* Dacheng Li, Rulin Shao, Hongyi Wang, Han Guo, Eric P. Xing, Hao Zhang: “MPCFormer: fast, performant and private Transformer inference with MPC”, 2022; [arXiv:2211.01452](http://arxiv.org/abs/2211.01452).
|
||||
* Baolin Peng, Michel Galley, Pengcheng He, Chris Brockett, Lars Liden, Elnaz Nouri, Zhou Yu, Bill Dolan, Jianfeng Gao: “GODEL: Large-Scale Pre-Training for Goal-Directed Dialog”, 2022; [arXiv:2206.11309](http://arxiv.org/abs/2206.11309).
|
||||
* Egil Rønningstad, Erik Velldal, Lilja Øvrelid: “Entity-Level Sentiment Analysis (ELSA): An exploratory task survey”, 2023, Proceedings of the 29th International Conference on Computational Linguistics, 2022, pages 6773-6783; [arXiv:2304.14241](http://arxiv.org/abs/2304.14241).
|
||||
* Charlie Snell, Ilya Kostrikov, Yi Su, Mengjiao Yang, Sergey Levine: “Offline RL for Natural Language Generation with Implicit Language Q Learning”, 2022; [arXiv:2206.11871](http://arxiv.org/abs/2206.11871).
|
||||
* Zhiruo Wang, Shuyan Zhou, Daniel Fried, Graham Neubig: “Execution-Based Evaluation for Open-Domain Code Generation”, 2022; [arXiv:2212.10481](http://arxiv.org/abs/2212.10481).
|
||||
* Minh-Long Luu, Zeyi Huang, Eric P. Xing, Yong Jae Lee, Haohan Wang: “Expeditious Saliency-guided Mix-up through Random Gradient Thresholding”, 2022; [arXiv:2212.04875](http://arxiv.org/abs/2212.04875).
|
||||
* Jun Hao Liew, Hanshu Yan, Daquan Zhou, Jiashi Feng: “MagicMix: Semantic Mixing with Diffusion Models”, 2022; [arXiv:2210.16056](http://arxiv.org/abs/2210.16056).
|
||||
* Yaqing Wang, Subhabrata Mukherjee, Xiaodong Liu, Jing Gao, Ahmed Hassan Awadallah, Jianfeng Gao: “LiST: Lite Prompted Self-training Makes Parameter-Efficient Few-shot Learners”, 2021; [arXiv:2110.06274](http://arxiv.org/abs/2110.06274).
|
||||
@ -1,117 +0,0 @@
|
||||
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
||||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations under the License.
|
||||
-->
|
||||
|
||||
# Example Zoo
|
||||
|
||||
Below contains a non-exhuastive list of tutorials and scripts showcasing Accelerate
|
||||
|
||||
## Official Accelerate Examples:
|
||||
|
||||
### Basic Examples
|
||||
|
||||
These examples showcase the base features of Accelerate and are a great starting point
|
||||
|
||||
- [Barebones NLP example](https://github.com/huggingface/accelerate/blob/main/examples/nlp_example.py)
|
||||
- [Barebones distributed NLP example in a Jupyter Notebook](https://github.com/huggingface/notebooks/blob/main/examples/accelerate_examples/simple_nlp_example.ipynb)
|
||||
- [Barebones computer vision example](https://github.com/huggingface/accelerate/blob/main/examples/cv_example.py)
|
||||
- [Barebones distributed computer vision example in a Jupyter Notebook](https://github.com/huggingface/notebooks/blob/main/examples/accelerate_examples/simple_cv_example.ipynb)
|
||||
- [Using Accelerate in Kaggle](https://www.kaggle.com/code/muellerzr/multi-gpu-and-accelerate)
|
||||
|
||||
### Feature Specific Examples
|
||||
|
||||
These examples showcase specific features that the Accelerate framework offers
|
||||
|
||||
- [Automatic memory-aware gradient accumulation](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/automatic_gradient_accumulation.py)
|
||||
- [Checkpointing states](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/checkpointing.py)
|
||||
- [Cross validation](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/cross_validation.py)
|
||||
- [DeepSpeed](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/deepspeed_with_config_support.py)
|
||||
- [Fully Sharded Data Parallelism](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/fsdp_with_peak_mem_tracking.py)
|
||||
- [Gradient accumulation](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/gradient_accumulation.py)
|
||||
- [Memory-aware batch size finder](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/memory.py)
|
||||
- [Metric Computation](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/multi_process_metrics.py)
|
||||
- [Using Trackers](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/tracking.py)
|
||||
- [Using Megatron-LM](https://github.com/huggingface/accelerate/blob/main/examples/by_feature/megatron_lm_gpt_pretraining.py)
|
||||
|
||||
### Full Examples
|
||||
|
||||
These examples showcase every feature in Accelerate at once that was shown in "Feature Specific Examples"
|
||||
|
||||
- [Complete NLP example](https://github.com/huggingface/accelerate/blob/main/examples/complete_nlp_example.py)
|
||||
- [Complete computer vision example](https://github.com/huggingface/accelerate/blob/main/examples/complete_cv_example.py)
|
||||
- [Causal language model fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/language-modeling/run_clm_no_trainer.py)
|
||||
- [Masked language model fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/language-modeling/run_mlm_no_trainer.py)
|
||||
- [Speech pretraining example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/speech-pretraining/run_wav2vec2_pretraining_no_trainer.py)
|
||||
- [Translation fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/translation/run_translation_no_trainer.py)
|
||||
- [Text classification fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue_no_trainer.py)
|
||||
- [Semantic segmentation fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py)
|
||||
- [Question answering fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/question-answering/run_qa_no_trainer.py)
|
||||
- [Beam search question answering fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/question-answering/run_qa_beam_search_no_trainer.py)
|
||||
- [Multiple choice question answering fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/multiple-choice/run_swag_no_trainer.py)
|
||||
- [Named entity recognition fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/token-classification/run_ner_no_trainer.py)
|
||||
- [Image classification fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/image-classification/run_image_classification_no_trainer.py)
|
||||
- [Summarization fine-tuning example](https://github.com/huggingface/transformers/blob/main/examples/pytorch/summarization/run_summarization_no_trainer.py)
|
||||
- [End-to-end examples on how to use AWS SageMaker integration of Accelerate](https://github.com/huggingface/notebooks/blob/main/sagemaker/22_accelerate_sagemaker_examples/README.md)
|
||||
- [Megatron-LM examples for various NLp tasks](https://github.com/pacman100/accelerate-megatron-test)
|
||||
|
||||
## Integration Examples
|
||||
|
||||
These are tutorials from libraries that integrate with 🤗 Accelerate:
|
||||
|
||||
### Catalyst
|
||||
|
||||
- [Distributed training tutorial with Catalyst](https://catalyst-team.github.io/catalyst/tutorials/ddp.html)
|
||||
|
||||
### DALLE2-pytorch
|
||||
|
||||
- [Fine-tuning DALLE2](https://github.com/lucidrains/DALLE2-pytorch#usage)
|
||||
|
||||
### 🤗 diffusers
|
||||
|
||||
- [Performing textual inversion with diffusers](https://github.com/huggingface/diffusers/tree/main/examples/textual_inversion)
|
||||
- [Training DreamBooth with diffusers](https://github.com/huggingface/diffusers/tree/main/examples/dreambooth)
|
||||
|
||||
### fastai
|
||||
|
||||
- [Distributed training from Jupyter Notebooks with fastai](https://docs.fast.ai/tutorial.distributed.html)
|
||||
- [Basic distributed training examples with fastai](https://docs.fast.ai/examples/distributed_app_examples.html)
|
||||
|
||||
### GradsFlow
|
||||
|
||||
- [Auto Image Classification with GradsFlow](https://docs.gradsflow.com/en/latest/examples/nbs/01-ImageClassification/)
|
||||
|
||||
### imagen-pytorch
|
||||
|
||||
- [Fine-tuning Imagen](https://github.com/lucidrains/imagen-pytorch#usage)
|
||||
|
||||
### Kornia
|
||||
|
||||
- [Fine-tuning vision models with Kornia's Trainer](https://kornia.readthedocs.io/en/latest/get-started/training.html)
|
||||
|
||||
### PyTorch Accelerated
|
||||
|
||||
- [Quickstart distributed training tutorial with PyTorch Accelerated](https://pytorch-accelerated.readthedocs.io/en/latest/quickstart.html)
|
||||
|
||||
### PyTorch3D
|
||||
|
||||
- [Perform Deep Learning with 3D data](https://pytorch3d.org/tutorials/)
|
||||
|
||||
### Stable-Dreamfusion
|
||||
|
||||
- [Training with Stable-Dreamfusion to convert text to a 3D model](https://colab.research.google.com/drive/1MXT3yfOFvO0ooKEfiUUvTKwUkrrlCHpF?usp=sharing)
|
||||
|
||||
### Tez
|
||||
|
||||
- [Leaf disease detection with Tez and Accelerate](https://www.kaggle.com/code/abhishek/tez-faster-and-easier-training-for-leaf-detection/notebook)
|
||||
|
||||
### trlx
|
||||
|
||||
- [How to implement a sentiment learning task with trlx](https://github.com/CarperAI/trlx#example-how-to-add-a-task)
|
||||
@ -51,13 +51,13 @@ To run it in each of these various modes, use the following commands:
|
||||
python ./nlp_example.py # from a server with a GPU
|
||||
```
|
||||
- with fp16 (mixed-precision)
|
||||
* from any server by passing `fp16=True` to the `Accelerator`.
|
||||
* from any server by passing `mixed_precison=fp16` to the `Accelerator`.
|
||||
```bash
|
||||
python ./nlp_example.py --fp16
|
||||
python ./nlp_example.py --mixed_precision fp16
|
||||
```
|
||||
* from any server with Accelerate launcher
|
||||
```bash
|
||||
accelerate launch --fp16 ./nlp_example.py
|
||||
accelerate launch --mixed_precision fp16 ./nlp_example.py
|
||||
- multi GPUs (using PyTorch distributed mode)
|
||||
* With Accelerate config and launcher
|
||||
```bash
|
||||
@ -139,13 +139,13 @@ To run it in each of these various modes, use the following commands:
|
||||
python ./cv_example.py # from a server with a GPU
|
||||
```
|
||||
- with fp16 (mixed-precision)
|
||||
* from any server by passing `fp16=True` to the `Accelerator`.
|
||||
* from any server by passing `mixed_precison=fp16` to the `Accelerator`.
|
||||
```bash
|
||||
python ./cv_example.py --data_dir path_to_data --fp16
|
||||
python ./cv_example.py --data_dir path_to_data --mixed_precison fp16
|
||||
```
|
||||
* from any server with Accelerate launcher
|
||||
```bash
|
||||
accelerate launch --fp16 ./cv_example.py --data_dir path_to_data
|
||||
accelerate launch --mixed_precison fp16 ./cv_example.py --data_dir path_to_data
|
||||
- multi GPUs (using PyTorch distributed mode)
|
||||
* With Accelerate config and launcher
|
||||
```bash
|
||||
@ -190,7 +190,22 @@ To run it in each of these various modes, use the following commands:
|
||||
|
||||
### Using AWS SageMaker integration
|
||||
- [Examples showcasing AWS SageMaker integration of 🤗 Accelerate.](https://github.com/pacman100/accelerate-aws-sagemaker)
|
||||
|
||||
|
||||
|
||||
## Simple Multi-GPU Hardware Launcher
|
||||
|
||||
[multigpu_remote_launcher.py](./multigpu_remote_launcher.py) is a minimal script that demonstrates launching accelerate
|
||||
on multiple remote GPUs, and with automatic hardware environment and dependency setup for reproducibility. You can
|
||||
easily customize the training function used, training arguments, hyperparameters, and type of compute hardware, and then
|
||||
run the script to automatically launch multi GPU training on remote hardware.
|
||||
|
||||
This script uses [Runhouse](https://github.com/run-house/runhouse) to launch on self-hosted hardware (e.g. in your own
|
||||
cloud account or on-premise cluster) but there are other options for running remotely as well. Runhouse can be installed
|
||||
with `pip install runhouse`, and you can refer to
|
||||
[hardware setup](https://runhouse-docs.readthedocs-hosted.com/en/latest/api/python/cluster.html#hardware-setup)
|
||||
for hardware setup instructions, or this
|
||||
[Colab tutorial](https://colab.research.google.com/drive/1qVwYyLTCPYPSdz9ZX7BZl9Qm0A3j7RJe) for a more in-depth walkthrough.
|
||||
|
||||
## Finer Examples
|
||||
|
||||
While the first two scripts are extremely barebones when it comes to what you can do with accelerate, more advanced features are documented in two other locations.
|
||||
|
||||
@ -77,4 +77,15 @@ These arguments should be added at the end of any method for starting the python
|
||||
|
||||
```bash
|
||||
accelerate launch ./gradient_accumulation.py --gradient_accumulation_steps 5
|
||||
```
|
||||
```
|
||||
|
||||
### LocalSGD (`local_sgd.py`)
|
||||
- Shows how to use `Accelerator.no_sync` to prevent gradient averaging in a distributed setup. However, unlike gradient accumulation, this method does not change the effective batch size. Local SGD can be combined with gradient accumulation.
|
||||
|
||||
These arguments should be added at the end of any method for starting the python script (such as `python`, `accelerate launch`, `python -m torchrun`), such as:
|
||||
|
||||
```bash
|
||||
accelerate launch ./local_sgd.py --local_sgd_steps 4
|
||||
```
|
||||
|
||||
|
||||
|
||||
@ -219,9 +219,12 @@ def training_function(config, args):
|
||||
# New Code #
|
||||
if args.resume_from_checkpoint and epoch == starting_epoch and resume_step is not None:
|
||||
# We need to skip steps until we reach the resumed step
|
||||
train_dataloader = accelerator.skip_first_batches(train_dataloader, resume_step)
|
||||
active_dataloader = accelerator.skip_first_batches(train_dataloader, resume_step)
|
||||
overall_step += resume_step
|
||||
for step, batch in enumerate(train_dataloader):
|
||||
else:
|
||||
# After the first iteration though, we need to go back to the original dataloader
|
||||
active_dataloader = train_dataloader
|
||||
for step, batch in enumerate(active_dataloader):
|
||||
# We could avoid this line since we set the accelerator with `device_placement=True`.
|
||||
batch.to(accelerator.device)
|
||||
outputs = model(**batch)
|
||||
|
||||
@ -243,39 +243,6 @@ def parse_args():
|
||||
return args
|
||||
|
||||
|
||||
# New Code #
|
||||
def checkpoint_model(checkpoint_folder, ckpt_id, model, epoch, last_global_step, **kwargs):
|
||||
"""Utility function for checkpointing model + optimizer dictionaries
|
||||
The main purpose for this is to be able to resume training from that instant again
|
||||
"""
|
||||
checkpoint_state_dict = {
|
||||
"epoch": epoch,
|
||||
"last_global_step": last_global_step,
|
||||
}
|
||||
# Add extra kwargs too
|
||||
checkpoint_state_dict.update(kwargs)
|
||||
|
||||
success = model.save_checkpoint(checkpoint_folder, ckpt_id, checkpoint_state_dict)
|
||||
status_msg = f"checkpointing: checkpoint_folder={checkpoint_folder}, ckpt_id={ckpt_id}"
|
||||
if success:
|
||||
logging.info(f"Success {status_msg}")
|
||||
else:
|
||||
logging.warning(f"Failure {status_msg}")
|
||||
return
|
||||
|
||||
|
||||
# New Code #
|
||||
def load_training_checkpoint(model, load_dir, tag=None, **kwargs):
|
||||
"""Utility function for checkpointing model + optimizer dictionaries
|
||||
The main purpose for this is to be able to resume training from that instant again
|
||||
"""
|
||||
_, checkpoint_state_dict = model.load_checkpoint(load_dir, tag=tag, **kwargs)
|
||||
epoch = checkpoint_state_dict["epoch"]
|
||||
last_global_step = checkpoint_state_dict["last_global_step"]
|
||||
del checkpoint_state_dict
|
||||
return (epoch, last_global_step)
|
||||
|
||||
|
||||
# New Code #
|
||||
def evaluate(args, model, eval_dataloader, accelerator, eval_dataset):
|
||||
model.eval()
|
||||
@ -302,9 +269,20 @@ def main():
|
||||
# Initialize the accelerator. We will let the accelerator handle device placement for us in this example.
|
||||
# If we're using tracking, we also need to initialize it here and it will by default pick up all supported trackers
|
||||
# in the environment
|
||||
|
||||
# when using DeepSpeed, the `gradient_accumulation_steps` is properly set from the DeepSpeed plugin/config
|
||||
# or from `accelerate launch` via `--gradient_accumulation_steps` else
|
||||
# defaulting to the passed `args.gradient_accumulation_steps`
|
||||
accelerator = (
|
||||
Accelerator(log_with=args.report_to, logging_dir=args.output_dir) if args.with_tracking else Accelerator()
|
||||
Accelerator(
|
||||
log_with=args.report_to,
|
||||
project_dir=args.output_dir,
|
||||
gradient_accumulation_steps=args.gradient_accumulation_steps,
|
||||
)
|
||||
if args.with_tracking
|
||||
else Accelerator(gradient_accumulation_steps=args.gradient_accumulation_steps)
|
||||
)
|
||||
|
||||
# Make one log on every process with the configuration for debugging.
|
||||
logging.basicConfig(
|
||||
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
|
||||
@ -538,17 +516,11 @@ def main():
|
||||
model.tie_weights()
|
||||
|
||||
# Scheduler and math around the number of training steps.
|
||||
|
||||
# New Code
|
||||
# Get gradient accumulation steps from deepspeed config if available
|
||||
if accelerator.state.deepspeed_plugin is not None:
|
||||
args.gradient_accumulation_steps = accelerator.state.deepspeed_plugin.deepspeed_config[
|
||||
"gradient_accumulation_steps"
|
||||
]
|
||||
|
||||
num_update_steps_per_epoch = math.ceil(len(train_dataloader) / args.gradient_accumulation_steps)
|
||||
num_update_steps_per_epoch = math.ceil(len(train_dataloader) / accelerator.gradient_accumulation_steps)
|
||||
overrode_max_train_steps = False
|
||||
if args.max_train_steps is None:
|
||||
args.max_train_steps = args.num_train_epochs * num_update_steps_per_epoch
|
||||
overrode_max_train_steps = True
|
||||
else:
|
||||
args.num_train_epochs = math.ceil(args.max_train_steps / num_update_steps_per_epoch)
|
||||
|
||||
@ -575,16 +547,16 @@ def main():
|
||||
)
|
||||
|
||||
# We need to recalculate our total training steps as the size of the training dataloader may have changed.
|
||||
num_update_steps_per_epoch = math.ceil(len(train_dataloader) / args.gradient_accumulation_steps)
|
||||
args.max_train_steps = args.num_train_epochs * num_update_steps_per_epoch
|
||||
num_update_steps_per_epoch = math.ceil(len(train_dataloader) / accelerator.gradient_accumulation_steps)
|
||||
if overrode_max_train_steps:
|
||||
args.max_train_steps = args.num_train_epochs * num_update_steps_per_epoch
|
||||
# Afterwards we recalculate our number of training epochs
|
||||
args.num_train_epochs = math.ceil(args.max_train_steps / num_update_steps_per_epoch)
|
||||
|
||||
# Figure out how many steps we should save the Accelerator states
|
||||
if hasattr(args.checkpointing_steps, "isdigit"):
|
||||
checkpointing_steps = args.checkpointing_steps
|
||||
if args.checkpointing_steps.isdigit():
|
||||
checkpointing_steps = int(args.checkpointing_steps)
|
||||
else:
|
||||
checkpointing_steps = None
|
||||
checkpointing_steps = args.checkpointing_steps
|
||||
if checkpointing_steps is not None and checkpointing_steps.isdigit():
|
||||
checkpointing_steps = int(checkpointing_steps)
|
||||
|
||||
# We need to initialize the trackers we use, and also store our configuration.
|
||||
# The trackers initializes automatically on the main process.
|
||||
@ -595,14 +567,16 @@ def main():
|
||||
accelerator.init_trackers("clm_no_trainer", experiment_config)
|
||||
|
||||
# Train!
|
||||
total_batch_size = args.per_device_train_batch_size * accelerator.num_processes * args.gradient_accumulation_steps
|
||||
total_batch_size = (
|
||||
args.per_device_train_batch_size * accelerator.num_processes * accelerator.gradient_accumulation_steps
|
||||
)
|
||||
|
||||
logger.info("***** Running training *****")
|
||||
logger.info(f" Num examples = {len(train_dataset)}")
|
||||
logger.info(f" Num Epochs = {args.num_train_epochs}")
|
||||
logger.info(f" Instantaneous batch size per device = {args.per_device_train_batch_size}")
|
||||
logger.info(f" Total train batch size (w. parallel, distributed & accumulation) = {total_batch_size}")
|
||||
logger.info(f" Gradient Accumulation steps = {args.gradient_accumulation_steps}")
|
||||
logger.info(f" Gradient Accumulation steps = {accelerator.gradient_accumulation_steps}")
|
||||
logger.info(f" Total optimization steps = {args.max_train_steps}")
|
||||
# Only show the progress bar once on each machine.
|
||||
progress_bar = tqdm(range(args.max_train_steps), disable=not accelerator.is_local_main_process)
|
||||
@ -613,45 +587,61 @@ def main():
|
||||
|
||||
# Potentially load in the weights and states from a previous save
|
||||
if args.resume_from_checkpoint:
|
||||
# New Code #
|
||||
# Loads the DeepSpeed checkpoint from the specified path
|
||||
_, last_global_step = load_training_checkpoint(
|
||||
model,
|
||||
args.resume_from_checkpoint,
|
||||
**{"load_optimizer_states": True, "load_lr_scheduler_states": True},
|
||||
)
|
||||
accelerator.load_state(args.resume_from_checkpoint)
|
||||
accelerator.print(f"Resumed from checkpoint: {args.resume_from_checkpoint}")
|
||||
resume_step = last_global_step
|
||||
starting_epoch = resume_step // len(train_dataloader)
|
||||
resume_step -= starting_epoch * len(train_dataloader)
|
||||
path = os.path.basename(args.resume_from_checkpoint)
|
||||
training_difference = os.path.splitext(path)[0]
|
||||
|
||||
if "epoch" in training_difference:
|
||||
starting_epoch = int(training_difference.replace("epoch_", "")) + 1
|
||||
resume_step = None
|
||||
completed_steps = starting_epoch * num_update_steps_per_epoch
|
||||
else:
|
||||
resume_step = int(training_difference.replace("step_", ""))
|
||||
starting_epoch = resume_step // num_update_steps_per_epoch
|
||||
resume_step -= starting_epoch * num_update_steps_per_epoch
|
||||
completed_steps = resume_step
|
||||
|
||||
# update progress bar if resumed from checkpoint
|
||||
progress_bar.update(completed_steps)
|
||||
|
||||
for epoch in range(starting_epoch, args.num_train_epochs):
|
||||
model.train()
|
||||
if args.with_tracking:
|
||||
total_loss = 0
|
||||
for step, batch in enumerate(train_dataloader):
|
||||
|
||||
# skip new `skip_first_batches` to skip the batches when resuming from ckpt
|
||||
if args.resume_from_checkpoint and epoch == starting_epoch and resume_step is not None:
|
||||
# We need to skip steps until we reach the resumed step
|
||||
if args.resume_from_checkpoint and epoch == starting_epoch:
|
||||
if resume_step is not None and step < resume_step:
|
||||
completed_steps += 1
|
||||
continue
|
||||
outputs = model(**batch)
|
||||
loss = outputs.loss
|
||||
# We keep track of the loss at each epoch
|
||||
if args.with_tracking:
|
||||
total_loss += loss.detach().float()
|
||||
loss = loss / args.gradient_accumulation_steps
|
||||
accelerator.backward(loss)
|
||||
if (step + 1) % args.gradient_accumulation_steps == 0 or step == len(train_dataloader) - 1:
|
||||
active_dataloader = accelerator.skip_first_batches(train_dataloader, resume_step)
|
||||
else:
|
||||
# After the first iteration though, we need to go back to the original dataloader
|
||||
active_dataloader = train_dataloader
|
||||
for step, batch in enumerate(active_dataloader):
|
||||
# In particular, DeepSpeed handles `gradient_accumulation` via `DeepSpeedEngine`.
|
||||
# Below, we use `accelerator.accumulate` if the user
|
||||
# wants to switch to other approaches such as plain DDP, PyTorch FSDP ...
|
||||
# This avoids having to change any code as things are all handled across different distributed setups.
|
||||
with accelerator.accumulate(model):
|
||||
outputs = model(**batch)
|
||||
loss = outputs.loss
|
||||
accelerator.backward(loss)
|
||||
optimizer.step()
|
||||
lr_scheduler.step()
|
||||
optimizer.zero_grad()
|
||||
progress_bar.update(1)
|
||||
completed_steps += 1
|
||||
|
||||
if accelerator.sync_gradients:
|
||||
progress_bar.update(1)
|
||||
completed_steps += 1
|
||||
|
||||
# We keep track of the loss at each epoch
|
||||
if args.with_tracking:
|
||||
step_loss = accelerator.reduce(loss.detach().clone()).item()
|
||||
total_loss += step_loss
|
||||
|
||||
if isinstance(checkpointing_steps, int):
|
||||
if completed_steps % checkpointing_steps == 0:
|
||||
output_dir = f"step_{completed_steps }"
|
||||
output_dir = f"step_{completed_steps}"
|
||||
if args.output_dir is not None:
|
||||
output_dir = os.path.join(args.output_dir, output_dir)
|
||||
accelerator.save_state(output_dir)
|
||||
@ -666,34 +656,29 @@ def main():
|
||||
{
|
||||
"perplexity": perplexity,
|
||||
"eval_loss": eval_loss,
|
||||
"train_loss": total_loss.item() / len(train_dataloader),
|
||||
"train_loss": total_loss / len(train_dataloader),
|
||||
"epoch": epoch,
|
||||
"step": completed_steps,
|
||||
},
|
||||
step=completed_steps,
|
||||
)
|
||||
|
||||
# New Code #
|
||||
# Save the DeepSpeed checkpoint to the specified path
|
||||
checkpoint_model(args.output_dir, epoch, model, epoch, completed_steps)
|
||||
if isinstance(checkpointing_steps, str) and checkpointing_steps == "epoch":
|
||||
accelerator.save_state(os.path.join(args.output_dir, f"epoch_{epoch}"))
|
||||
|
||||
# New Code #
|
||||
# Tracks the best checkpoint and best metric
|
||||
if best_metric is None or best_metric > perplexity:
|
||||
best_metric = perplexity
|
||||
best_metric_checkpoint = os.path.join(args.output_dir, str(epoch))
|
||||
best_metric_checkpoint = os.path.join(args.output_dir, "best_checkpoint")
|
||||
accelerator.save_state(best_metric_checkpoint)
|
||||
accelerator.print(f"New best metric: {best_metric} at epoch {epoch}")
|
||||
accelerator.print(f"best_metric_checkpoint: {best_metric_checkpoint}")
|
||||
|
||||
# New Code #
|
||||
# Loads the best checkpoint after the training is finished
|
||||
if args.load_best_model:
|
||||
_, last_global_step = load_training_checkpoint(
|
||||
model,
|
||||
"/".join(best_metric_checkpoint.split("/")[:-1]),
|
||||
tag=best_metric_checkpoint.split("/")[-1],
|
||||
**{"load_optimizer_states": True, "load_lr_scheduler_states": True},
|
||||
)
|
||||
accelerator.load_state(best_metric_checkpoint)
|
||||
|
||||
# New Code #
|
||||
# Evaluates using the best checkpoint
|
||||
|
||||
246
examples/by_feature/early_stopping.py
Normal file
246
examples/by_feature/early_stopping.py
Normal file
@ -0,0 +1,246 @@
|
||||
# coding=utf-8
|
||||
# Copyright 2021 The HuggingFace Inc. team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
import argparse
|
||||
|
||||
import evaluate
|
||||
import torch
|
||||
from datasets import load_dataset
|
||||
from torch.optim import AdamW
|
||||
from torch.utils.data import DataLoader
|
||||
from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed
|
||||
|
||||
from accelerate import Accelerator, DistributedType
|
||||
|
||||
|
||||
########################################################################
|
||||
# This is a fully working simple example to use Accelerate
|
||||
# specifically showcasing how to perform early stopping,
|
||||
# and builds off the `nlp_example.py` script
|
||||
#
|
||||
# This example trains a Bert base model on GLUE MRPC
|
||||
# in any of the following settings (with the same script):
|
||||
# - single CPU or single GPU
|
||||
# - multi GPUS (using PyTorch distributed mode)
|
||||
# - (multi) TPUs
|
||||
# - fp16 (mixed-precision) or fp32 (normal precision)
|
||||
#
|
||||
# To run it in each of these various modes, follow the instructions
|
||||
# in the readme for examples:
|
||||
# https://github.com/huggingface/accelerate/tree/main/examples
|
||||
#
|
||||
########################################################################
|
||||
|
||||
|
||||
MAX_GPU_BATCH_SIZE = 16
|
||||
EVAL_BATCH_SIZE = 32
|
||||
|
||||
|
||||
def get_dataloaders(accelerator: Accelerator, batch_size: int = 16):
|
||||
"""
|
||||
Creates a set of `DataLoader`s for the `glue` dataset,
|
||||
using "bert-base-cased" as the tokenizer.
|
||||
|
||||
Args:
|
||||
accelerator (`Accelerator`):
|
||||
An `Accelerator` object
|
||||
batch_size (`int`, *optional*):
|
||||
The batch size for the train and validation DataLoaders.
|
||||
"""
|
||||
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
|
||||
datasets = load_dataset("glue", "mrpc")
|
||||
|
||||
def tokenize_function(examples):
|
||||
# max_length=None => use the model max length (it's actually the default)
|
||||
outputs = tokenizer(examples["sentence1"], examples["sentence2"], truncation=True, max_length=None)
|
||||
return outputs
|
||||
|
||||
# Apply the method we just defined to all the examples in all the splits of the dataset
|
||||
# starting with the main process first:
|
||||
with accelerator.main_process_first():
|
||||
tokenized_datasets = datasets.map(
|
||||
tokenize_function,
|
||||
batched=True,
|
||||
remove_columns=["idx", "sentence1", "sentence2"],
|
||||
)
|
||||
|
||||
# We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the
|
||||
# transformers library
|
||||
tokenized_datasets = tokenized_datasets.rename_column("label", "labels")
|
||||
|
||||
def collate_fn(examples):
|
||||
# On TPU it's best to pad everything to the same length or training will be very slow.
|
||||
max_length = 128 if accelerator.distributed_type == DistributedType.TPU else None
|
||||
# When using mixed precision we want round multiples of 8/16
|
||||
if accelerator.mixed_precision == "fp8":
|
||||
pad_to_multiple_of = 16
|
||||
elif accelerator.mixed_precision != "no":
|
||||
pad_to_multiple_of = 8
|
||||
else:
|
||||
pad_to_multiple_of = None
|
||||
|
||||
return tokenizer.pad(
|
||||
examples,
|
||||
padding="longest",
|
||||
max_length=max_length,
|
||||
pad_to_multiple_of=pad_to_multiple_of,
|
||||
return_tensors="pt",
|
||||
)
|
||||
|
||||
# Instantiate dataloaders.
|
||||
train_dataloader = DataLoader(
|
||||
tokenized_datasets["train"], shuffle=True, collate_fn=collate_fn, batch_size=batch_size, drop_last=True
|
||||
)
|
||||
eval_dataloader = DataLoader(
|
||||
tokenized_datasets["validation"],
|
||||
shuffle=False,
|
||||
collate_fn=collate_fn,
|
||||
batch_size=EVAL_BATCH_SIZE,
|
||||
drop_last=(accelerator.mixed_precision == "fp8"),
|
||||
)
|
||||
|
||||
return train_dataloader, eval_dataloader
|
||||
|
||||
|
||||
# New code
|
||||
class EarlyStoppingCallback:
|
||||
"A callback class that helps with early stopping"
|
||||
|
||||
def __init__(self, min_delta=0, patience=5):
|
||||
self.min_delta = min_delta
|
||||
self.patience = patience
|
||||
self.counter = 0
|
||||
self.lowest_loss = float("inf")
|
||||
|
||||
def check_early_stopping(self, eval_loss):
|
||||
delta = self.lowest_loss - eval_loss
|
||||
if delta >= self.min_delta:
|
||||
self.lowest_loss = eval_loss
|
||||
self.counter = 0
|
||||
else:
|
||||
self.counter += 1
|
||||
if self.counter >= self.patience:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
callback = EarlyStoppingCallback()
|
||||
|
||||
|
||||
def training_function(config, args):
|
||||
# Initialize accelerator
|
||||
accelerator = Accelerator(cpu=args.cpu, mixed_precision=args.mixed_precision)
|
||||
# Sample hyper-parameters for learning rate, batch size, seed and a few other HPs
|
||||
lr = config["lr"]
|
||||
num_epochs = int(config["num_epochs"])
|
||||
seed = int(config["seed"])
|
||||
batch_size = int(config["batch_size"])
|
||||
|
||||
metric = evaluate.load("glue", "mrpc")
|
||||
|
||||
# If the batch size is too big we use gradient accumulation
|
||||
gradient_accumulation_steps = 1
|
||||
if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU:
|
||||
gradient_accumulation_steps = batch_size // MAX_GPU_BATCH_SIZE
|
||||
batch_size = MAX_GPU_BATCH_SIZE
|
||||
|
||||
set_seed(seed)
|
||||
train_dataloader, eval_dataloader = get_dataloaders(accelerator, batch_size)
|
||||
# Instantiate the model (we build the model here so that the seed also control new weights initialization)
|
||||
model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased", return_dict=True)
|
||||
|
||||
# We could avoid this line since the accelerator is set with `device_placement=True` (default value).
|
||||
# Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer
|
||||
# creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that).
|
||||
model = model.to(accelerator.device)
|
||||
# Instantiate optimizer
|
||||
optimizer = AdamW(params=model.parameters(), lr=lr)
|
||||
|
||||
# Instantiate scheduler
|
||||
lr_scheduler = get_linear_schedule_with_warmup(
|
||||
optimizer=optimizer,
|
||||
num_warmup_steps=100,
|
||||
num_training_steps=(len(train_dataloader) * num_epochs) // gradient_accumulation_steps,
|
||||
)
|
||||
|
||||
# Prepare everything
|
||||
# There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the
|
||||
# prepare method.
|
||||
|
||||
model, optimizer, train_dataloader, eval_dataloader, lr_scheduler = accelerator.prepare(
|
||||
model, optimizer, train_dataloader, eval_dataloader, lr_scheduler
|
||||
)
|
||||
|
||||
# Now we train the model
|
||||
for epoch in range(num_epochs):
|
||||
model.train()
|
||||
for step, batch in enumerate(train_dataloader):
|
||||
# We could avoid this line since we set the accelerator with `device_placement=True`.
|
||||
batch.to(accelerator.device)
|
||||
outputs = model(**batch)
|
||||
loss = outputs.loss
|
||||
loss = loss / gradient_accumulation_steps
|
||||
accelerator.backward(loss)
|
||||
if step % gradient_accumulation_steps == 0:
|
||||
optimizer.step()
|
||||
lr_scheduler.step()
|
||||
optimizer.zero_grad()
|
||||
|
||||
# New code
|
||||
# Check if we should stop the training on any processes
|
||||
if callback.check_early_stopping(loss.item()):
|
||||
accelerator.set_trigger()
|
||||
|
||||
# If so, we break the loop
|
||||
if accelerator.check_trigger():
|
||||
break
|
||||
|
||||
model.eval()
|
||||
for step, batch in enumerate(eval_dataloader):
|
||||
# We could avoid this line since we set the accelerator with `device_placement=True`.
|
||||
batch.to(accelerator.device)
|
||||
with torch.no_grad():
|
||||
outputs = model(**batch)
|
||||
predictions = outputs.logits.argmax(dim=-1)
|
||||
predictions, references = accelerator.gather_for_metrics((predictions, batch["labels"]))
|
||||
metric.add_batch(
|
||||
predictions=predictions,
|
||||
references=references,
|
||||
)
|
||||
|
||||
eval_metric = metric.compute()
|
||||
|
||||
# Use accelerator.print to print only on the main process.
|
||||
accelerator.print(f"epoch {epoch}:", eval_metric)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Simple example of training script.")
|
||||
parser.add_argument(
|
||||
"--mixed_precision",
|
||||
type=str,
|
||||
default=None,
|
||||
choices=["no", "fp16", "bf16", "fp8"],
|
||||
help="Whether to use mixed precision. Choose"
|
||||
"between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10."
|
||||
"and an Nvidia Ampere GPU.",
|
||||
)
|
||||
parser.add_argument("--cpu", action="store_true", help="If passed, will train on the CPU.")
|
||||
args = parser.parse_args()
|
||||
config = {"lr": 2e-5, "num_epochs": 3, "seed": 42, "batch_size": 16}
|
||||
training_function(config, args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@ -15,14 +15,23 @@
|
||||
import argparse
|
||||
import gc
|
||||
import os
|
||||
import threading
|
||||
|
||||
import evaluate
|
||||
import psutil
|
||||
import torch
|
||||
from datasets import load_dataset
|
||||
from torch.distributed.fsdp.fully_sharded_data_parallel import FullOptimStateDictConfig, FullStateDictConfig
|
||||
from torch.utils.data import DataLoader
|
||||
from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed
|
||||
from transformers import (
|
||||
AutoModelForSequenceClassification,
|
||||
AutoTokenizer,
|
||||
get_linear_schedule_with_warmup,
|
||||
set_seed,
|
||||
)
|
||||
|
||||
from accelerate import Accelerator, DistributedType
|
||||
from accelerate import Accelerator, DistributedType, FullyShardedDataParallelPlugin
|
||||
from accelerate.utils import is_npu_available, is_xpu_available
|
||||
|
||||
|
||||
########################################################################
|
||||
@ -60,18 +69,65 @@ def b2mb(x):
|
||||
class TorchTracemalloc:
|
||||
def __enter__(self):
|
||||
gc.collect()
|
||||
torch.cuda.empty_cache()
|
||||
torch.cuda.reset_max_memory_allocated() # reset the peak gauge to zero
|
||||
self.begin = torch.cuda.memory_allocated()
|
||||
if torch.cuda.is_available():
|
||||
torch.cuda.empty_cache()
|
||||
torch.cuda.reset_max_memory_allocated() # reset the peak gauge to zero
|
||||
self.begin = torch.cuda.memory_allocated()
|
||||
elif is_xpu_available():
|
||||
torch.xpu.empty_cache()
|
||||
torch.xpu.reset_max_memory_allocated() # reset the peak gauge to zero
|
||||
self.begin = torch.xpu.memory_allocated()
|
||||
elif is_npu_available():
|
||||
torch.npu.empty_cache()
|
||||
torch.npu.reset_max_memory_allocated() # reset the peak gauge to zero
|
||||
self.begin = torch.npu.memory_allocated()
|
||||
self.process = psutil.Process()
|
||||
|
||||
self.cpu_begin = self.cpu_mem_used()
|
||||
self.peak_monitoring = True
|
||||
peak_monitor_thread = threading.Thread(target=self.peak_monitor_func)
|
||||
peak_monitor_thread.daemon = True
|
||||
peak_monitor_thread.start()
|
||||
return self
|
||||
|
||||
def cpu_mem_used(self):
|
||||
"""get resident set size memory for the current process"""
|
||||
return self.process.memory_info().rss
|
||||
|
||||
def peak_monitor_func(self):
|
||||
self.cpu_peak = -1
|
||||
|
||||
while True:
|
||||
self.cpu_peak = max(self.cpu_mem_used(), self.cpu_peak)
|
||||
|
||||
# can't sleep or will not catch the peak right (this comment is here on purpose)
|
||||
# time.sleep(0.001) # 1msec
|
||||
|
||||
if not self.peak_monitoring:
|
||||
break
|
||||
|
||||
def __exit__(self, *exc):
|
||||
self.peak_monitoring = False
|
||||
|
||||
gc.collect()
|
||||
torch.cuda.empty_cache()
|
||||
self.end = torch.cuda.memory_allocated()
|
||||
self.peak = torch.cuda.max_memory_allocated()
|
||||
if torch.cuda.is_available():
|
||||
torch.cuda.empty_cache()
|
||||
self.end = torch.cuda.memory_allocated()
|
||||
self.peak = torch.cuda.max_memory_allocated()
|
||||
elif is_xpu_available():
|
||||
torch.xpu.empty_cache()
|
||||
self.end = torch.xpu.memory_allocated()
|
||||
self.peak = torch.xpu.max_memory_allocated()
|
||||
elif is_npu_available():
|
||||
torch.npu.empty_cache()
|
||||
self.end = torch.npu.memory_allocated()
|
||||
self.peak = torch.npu.max_memory_allocated()
|
||||
self.used = b2mb(self.end - self.begin)
|
||||
self.peaked = b2mb(self.peak - self.begin)
|
||||
|
||||
self.cpu_end = self.cpu_mem_used()
|
||||
self.cpu_used = b2mb(self.cpu_end - self.cpu_begin)
|
||||
self.cpu_peaked = b2mb(self.cpu_peak - self.cpu_begin)
|
||||
# print(f"delta used/peak {self.used:4d}/{self.peaked:4d}")
|
||||
|
||||
|
||||
@ -86,13 +142,25 @@ def training_function(config, args):
|
||||
# For testing only
|
||||
if os.environ.get("TESTING_MOCKED_DATALOADERS", None) == "1":
|
||||
config["num_epochs"] = 2
|
||||
|
||||
# New Code #
|
||||
# Pass the advanced FSDP settings not part of the accelerate config by creating fsdp_plugin
|
||||
fsdp_plugin = FullyShardedDataParallelPlugin(
|
||||
state_dict_config=FullStateDictConfig(offload_to_cpu=False, rank0_only=False),
|
||||
optim_state_dict_config=FullOptimStateDictConfig(offload_to_cpu=False, rank0_only=False),
|
||||
)
|
||||
|
||||
# Initialize accelerator
|
||||
if args.with_tracking:
|
||||
accelerator = Accelerator(
|
||||
cpu=args.cpu, mixed_precision=args.mixed_precision, log_with="wandb", logging_dir=args.logging_dir
|
||||
cpu=args.cpu,
|
||||
mixed_precision=args.mixed_precision,
|
||||
log_with="wandb",
|
||||
project_dir=args.logging_dir,
|
||||
fsdp_plugin=fsdp_plugin,
|
||||
)
|
||||
else:
|
||||
accelerator = Accelerator()
|
||||
accelerator = Accelerator(fsdp_plugin=fsdp_plugin)
|
||||
accelerator.print(accelerator.distributed_type)
|
||||
|
||||
if hasattr(args.checkpointing_steps, "isdigit"):
|
||||
@ -175,7 +243,10 @@ def training_function(config, args):
|
||||
set_seed(seed)
|
||||
|
||||
# Instantiate the model (we build the model here so that the seed also control new weights initialization)
|
||||
model = AutoModelForSequenceClassification.from_pretrained(args.model_name_or_path, return_dict=True)
|
||||
model = AutoModelForSequenceClassification.from_pretrained(
|
||||
args.model_name_or_path, return_dict=True, low_cpu_mem_usage=True
|
||||
)
|
||||
|
||||
# New Code #
|
||||
# For FSDP feature, it is highly recommended and efficient to prepare the model before creating optimizer
|
||||
model = accelerator.prepare(model)
|
||||
@ -245,7 +316,6 @@ def training_function(config, args):
|
||||
batch.to(accelerator.device)
|
||||
outputs = model(**batch)
|
||||
loss = outputs.loss
|
||||
loss = loss / gradient_accumulation_steps
|
||||
# We keep track of the loss at each epoch
|
||||
if args.with_tracking:
|
||||
total_loss += loss.detach().float()
|
||||
@ -386,7 +456,7 @@ def main():
|
||||
required=True,
|
||||
)
|
||||
args = parser.parse_args()
|
||||
config = {"lr": 2e-5, "num_epochs": 3, "seed": 1, "batch_size": 16}
|
||||
config = {"lr": 2e-5, "num_epochs": 3, "seed": 42, "batch_size": 16}
|
||||
training_function(config, args)
|
||||
|
||||
|
||||
|
||||
238
examples/by_feature/local_sgd.py
Normal file
238
examples/by_feature/local_sgd.py
Normal file
@ -0,0 +1,238 @@
|
||||
# coding=utf-8
|
||||
# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
import argparse
|
||||
import os
|
||||
|
||||
import evaluate
|
||||
import torch
|
||||
from datasets import load_dataset
|
||||
from torch.optim import AdamW
|
||||
from torch.utils.data import DataLoader
|
||||
from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed
|
||||
|
||||
from accelerate import Accelerator, DistributedType
|
||||
from accelerate.local_sgd import LocalSGD
|
||||
|
||||
|
||||
########################################################################
|
||||
# This is a fully working simple example to use Accelerate
|
||||
# with LocalSGD, which is a method to synchronize model
|
||||
# parameters every K batches. It is different, but complementary
|
||||
# to gradient accumulation.
|
||||
#
|
||||
# This example trains a Bert base model on GLUE MRPC
|
||||
# in any of the following settings (with the same script):
|
||||
# - single CPU or single GPU
|
||||
# - multi GPUS (using PyTorch distributed mode)
|
||||
# - (multi) TPUs
|
||||
# - fp16 (mixed-precision) or fp32 (normal precision)
|
||||
#
|
||||
# To run it in each of these various modes, follow the instructions
|
||||
# in the readme for examples:
|
||||
# https://github.com/huggingface/accelerate/tree/main/examples
|
||||
#
|
||||
########################################################################
|
||||
|
||||
|
||||
MAX_GPU_BATCH_SIZE = 16
|
||||
EVAL_BATCH_SIZE = 32
|
||||
|
||||
|
||||
def get_dataloaders(accelerator: Accelerator, batch_size: int = 16):
|
||||
"""
|
||||
Creates a set of `DataLoader`s for the `glue` dataset,
|
||||
using "bert-base-cased" as the tokenizer.
|
||||
|
||||
Args:
|
||||
accelerator (`Accelerator`):
|
||||
An `Accelerator` object
|
||||
batch_size (`int`, *optional*):
|
||||
The batch size for the train and validation DataLoaders.
|
||||
"""
|
||||
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
|
||||
datasets = load_dataset("glue", "mrpc")
|
||||
|
||||
def tokenize_function(examples):
|
||||
# max_length=None => use the model max length (it's actually the default)
|
||||
outputs = tokenizer(examples["sentence1"], examples["sentence2"], truncation=True, max_length=None)
|
||||
return outputs
|
||||
|
||||
# Apply the method we just defined to all the examples in all the splits of the dataset
|
||||
# starting with the main process first:
|
||||
with accelerator.main_process_first():
|
||||
tokenized_datasets = datasets.map(
|
||||
tokenize_function,
|
||||
batched=True,
|
||||
remove_columns=["idx", "sentence1", "sentence2"],
|
||||
)
|
||||
|
||||
# We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the
|
||||
# transformers library
|
||||
tokenized_datasets = tokenized_datasets.rename_column("label", "labels")
|
||||
|
||||
def collate_fn(examples):
|
||||
# On TPU it's best to pad everything to the same length or training will be very slow.
|
||||
max_length = 128 if accelerator.distributed_type == DistributedType.TPU else None
|
||||
# When using mixed precision we want round multiples of 8/16
|
||||
if accelerator.mixed_precision == "fp8":
|
||||
pad_to_multiple_of = 16
|
||||
elif accelerator.mixed_precision != "no":
|
||||
pad_to_multiple_of = 8
|
||||
else:
|
||||
pad_to_multiple_of = None
|
||||
|
||||
return tokenizer.pad(
|
||||
examples,
|
||||
padding="longest",
|
||||
max_length=max_length,
|
||||
pad_to_multiple_of=pad_to_multiple_of,
|
||||
return_tensors="pt",
|
||||
)
|
||||
|
||||
# Instantiate dataloaders.
|
||||
train_dataloader = DataLoader(
|
||||
tokenized_datasets["train"], shuffle=True, collate_fn=collate_fn, batch_size=batch_size
|
||||
)
|
||||
eval_dataloader = DataLoader(
|
||||
tokenized_datasets["validation"], shuffle=False, collate_fn=collate_fn, batch_size=EVAL_BATCH_SIZE
|
||||
)
|
||||
|
||||
return train_dataloader, eval_dataloader
|
||||
|
||||
|
||||
# For testing only
|
||||
if os.environ.get("TESTING_MOCKED_DATALOADERS", None) == "1":
|
||||
from accelerate.test_utils.training import mocked_dataloaders
|
||||
|
||||
get_dataloaders = mocked_dataloaders # noqa: F811
|
||||
|
||||
|
||||
def training_function(config, args):
|
||||
# For testing only
|
||||
if os.environ.get("TESTING_MOCKED_DATALOADERS", None) == "1":
|
||||
config["num_epochs"] = 2
|
||||
# New Code #
|
||||
gradient_accumulation_steps = int(args.gradient_accumulation_steps)
|
||||
local_sgd_steps = int(args.local_sgd_steps)
|
||||
# Initialize accelerator
|
||||
accelerator = Accelerator(
|
||||
cpu=args.cpu, mixed_precision=args.mixed_precision, gradient_accumulation_steps=gradient_accumulation_steps
|
||||
)
|
||||
if accelerator.distributed_type not in [DistributedType.NO, DistributedType.MULTI_CPU, DistributedType.MULTI_GPU]:
|
||||
raise NotImplementedError("LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)")
|
||||
# Sample hyper-parameters for learning rate, batch size, seed and a few other HPs
|
||||
lr = config["lr"]
|
||||
num_epochs = int(config["num_epochs"])
|
||||
seed = int(config["seed"])
|
||||
batch_size = int(config["batch_size"])
|
||||
|
||||
metric = evaluate.load("glue", "mrpc")
|
||||
|
||||
set_seed(seed)
|
||||
train_dataloader, eval_dataloader = get_dataloaders(accelerator, batch_size)
|
||||
# Instantiate the model (we build the model here so that the seed also control new weights initialization)
|
||||
model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased", return_dict=True)
|
||||
|
||||
# We could avoid this line since the accelerator is set with `device_placement=True` (default value).
|
||||
# Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer
|
||||
# creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that).
|
||||
model = model.to(accelerator.device)
|
||||
|
||||
# Instantiate optimizer
|
||||
optimizer = AdamW(params=model.parameters(), lr=lr)
|
||||
|
||||
# Instantiate scheduler
|
||||
lr_scheduler = get_linear_schedule_with_warmup(
|
||||
optimizer=optimizer,
|
||||
num_warmup_steps=100,
|
||||
num_training_steps=(len(train_dataloader) * num_epochs),
|
||||
)
|
||||
|
||||
# Prepare everything
|
||||
# There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the
|
||||
# prepare method.
|
||||
model, optimizer, train_dataloader, eval_dataloader, lr_scheduler = accelerator.prepare(
|
||||
model, optimizer, train_dataloader, eval_dataloader, lr_scheduler
|
||||
)
|
||||
|
||||
# Now we train the model
|
||||
for epoch in range(num_epochs):
|
||||
model.train()
|
||||
with LocalSGD(
|
||||
accelerator=accelerator, model=model, local_sgd_steps=local_sgd_steps, enabled=local_sgd_steps is not None
|
||||
) as local_sgd:
|
||||
for step, batch in enumerate(train_dataloader):
|
||||
# We could avoid this line since we set the accelerator with `device_placement=True`.
|
||||
batch.to(accelerator.device)
|
||||
# New code #
|
||||
# We use the new `accumulate` context manager to perform gradient accumulation
|
||||
# We also currently do not support TPUs nor advise it as bugs were found on the XLA side when running our tests.
|
||||
with accelerator.accumulate(model):
|
||||
output = model(**batch)
|
||||
loss = output.loss
|
||||
accelerator.backward(loss)
|
||||
optimizer.step()
|
||||
lr_scheduler.step()
|
||||
optimizer.zero_grad()
|
||||
# LocalSGD-specific line
|
||||
local_sgd.step()
|
||||
|
||||
model.eval()
|
||||
for step, batch in enumerate(eval_dataloader):
|
||||
# We could avoid this line since we set the accelerator with `device_placement=True`.
|
||||
batch.to(accelerator.device)
|
||||
with torch.no_grad():
|
||||
outputs = model(**batch)
|
||||
predictions = outputs.logits.argmax(dim=-1)
|
||||
predictions, references = accelerator.gather_for_metrics((predictions, batch["labels"]))
|
||||
metric.add_batch(
|
||||
predictions=predictions,
|
||||
references=references,
|
||||
)
|
||||
|
||||
eval_metric = metric.compute()
|
||||
# Use accelerator.print to print only on the main process.
|
||||
accelerator.print(f"epoch {epoch}:", eval_metric)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Simple example of training script.")
|
||||
parser.add_argument(
|
||||
"--mixed_precision",
|
||||
type=str,
|
||||
default=None,
|
||||
choices=["no", "fp16", "bf16", "fp8"],
|
||||
help="Whether to use mixed precision. Choose"
|
||||
"between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10."
|
||||
"and an Nvidia Ampere GPU.",
|
||||
)
|
||||
# New Code #
|
||||
parser.add_argument(
|
||||
"--gradient_accumulation_steps",
|
||||
type=int,
|
||||
default=1,
|
||||
help="The number of minibatches to be ran before gradients are accumulated.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--local_sgd_steps", type=int, default=8, help="Number of local SGD steps or None to disable local SGD"
|
||||
)
|
||||
parser.add_argument("--cpu", action="store_true", help="If passed, will train on the CPU.")
|
||||
args = parser.parse_args()
|
||||
config = {"lr": 2e-5, "num_epochs": 3, "seed": 42, "batch_size": 16}
|
||||
training_function(config, args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@ -133,7 +133,7 @@ def training_function(config, args):
|
||||
# >>> log_with = ["all", MyCustomTrackerClassInstance()]
|
||||
if args.with_tracking:
|
||||
accelerator = Accelerator(
|
||||
cpu=args.cpu, mixed_precision=args.mixed_precision, log_with="all", logging_dir=args.logging_dir
|
||||
cpu=args.cpu, mixed_precision=args.mixed_precision, log_with="all", project_dir=args.project_dir
|
||||
)
|
||||
else:
|
||||
accelerator = Accelerator(cpu=args.cpu, mixed_precision=args.mixed_precision)
|
||||
@ -262,10 +262,10 @@ def main():
|
||||
help="Whether to load in all available experiment trackers from the environment and use them for logging.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--logging_dir",
|
||||
"--project_dir",
|
||||
type=str,
|
||||
default="logs",
|
||||
help="Location on where to store experiment tracking logs`",
|
||||
help="Location on where to store experiment tracking logs` and relevent project information",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
config = {"lr": 2e-5, "num_epochs": 3, "seed": 42, "batch_size": 16}
|
||||
|
||||
@ -75,7 +75,7 @@ def training_function(config, args):
|
||||
# Initialize accelerator
|
||||
if args.with_tracking:
|
||||
accelerator = Accelerator(
|
||||
cpu=args.cpu, mixed_precision=args.mixed_precision, log_with="all", logging_dir=args.logging_dir
|
||||
cpu=args.cpu, mixed_precision=args.mixed_precision, log_with="all", project_dir=args.project_dir
|
||||
)
|
||||
else:
|
||||
accelerator = Accelerator(cpu=args.cpu, mixed_precision=args.mixed_precision)
|
||||
@ -205,9 +205,12 @@ def training_function(config, args):
|
||||
total_loss = 0
|
||||
if args.resume_from_checkpoint and epoch == starting_epoch and resume_step is not None:
|
||||
# We need to skip steps until we reach the resumed step
|
||||
train_dataloader = accelerator.skip_first_batches(train_dataloader, resume_step)
|
||||
active_dataloader = accelerator.skip_first_batches(train_dataloader, resume_step)
|
||||
overall_step += resume_step
|
||||
for batch in train_dataloader:
|
||||
else:
|
||||
# After the first iteration though, we need to go back to the original dataloader
|
||||
active_dataloader = train_dataloader
|
||||
for batch in active_dataloader:
|
||||
# We could avoid this line since we set the accelerator with `device_placement=True`.
|
||||
batch = {k: v.to(accelerator.device) for k, v in batch.items()}
|
||||
inputs = (batch["image"] - mean) / std
|
||||
@ -302,10 +305,10 @@ def main():
|
||||
help="Whether to load in all available experiment trackers from the environment and use them for logging.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--logging_dir",
|
||||
"--project_dir",
|
||||
type=str,
|
||||
default="logs",
|
||||
help="Location on where to store experiment tracking logs`",
|
||||
help="Location on where to store experiment tracking logs` and relevent project information",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
config = {"lr": 3e-2, "num_epochs": 3, "seed": 42, "batch_size": 64, "image_size": 224}
|
||||
|
||||
@ -52,7 +52,7 @@ def training_function(config, args):
|
||||
# Initialize accelerator
|
||||
if args.with_tracking:
|
||||
accelerator = Accelerator(
|
||||
cpu=args.cpu, mixed_precision=args.mixed_precision, log_with="all", logging_dir=args.logging_dir
|
||||
cpu=args.cpu, mixed_precision=args.mixed_precision, log_with="all", project_dir=args.project_dir
|
||||
)
|
||||
else:
|
||||
accelerator = Accelerator(cpu=args.cpu, mixed_precision=args.mixed_precision)
|
||||
@ -195,9 +195,12 @@ def training_function(config, args):
|
||||
total_loss = 0
|
||||
if args.resume_from_checkpoint and epoch == starting_epoch and resume_step is not None:
|
||||
# We need to skip steps until we reach the resumed step
|
||||
train_dataloader = accelerator.skip_first_batches(train_dataloader, resume_step)
|
||||
active_dataloader = accelerator.skip_first_batches(train_dataloader, resume_step)
|
||||
overall_step += resume_step
|
||||
for step, batch in enumerate(train_dataloader):
|
||||
else:
|
||||
# After the first iteration though, we need to go back to the original dataloader
|
||||
active_dataloader = train_dataloader
|
||||
for step, batch in enumerate(active_dataloader):
|
||||
# We could avoid this line since we set the accelerator with `device_placement=True`.
|
||||
batch.to(accelerator.device)
|
||||
outputs = model(**batch)
|
||||
@ -294,10 +297,10 @@ def main():
|
||||
help="Optional save directory where all checkpoint folders will be stored. Default is the current working directory.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--logging_dir",
|
||||
"--project_dir",
|
||||
type=str,
|
||||
default="logs",
|
||||
help="Location on where to store experiment tracking logs`",
|
||||
help="Location on where to store experiment tracking logs` and relevent project information",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
config = {"lr": 2e-5, "num_epochs": 3, "seed": 42, "batch_size": 16}
|
||||
|
||||
55
examples/multigpu_remote_launcher.py
Normal file
55
examples/multigpu_remote_launcher.py
Normal file
@ -0,0 +1,55 @@
|
||||
import argparse
|
||||
|
||||
import runhouse as rh
|
||||
import torch
|
||||
from nlp_example import training_function
|
||||
|
||||
from accelerate.utils import PrepareForLaunch, patch_environment
|
||||
|
||||
|
||||
def launch_train(*args):
|
||||
num_processes = torch.cuda.device_count()
|
||||
print(f"Device count: {num_processes}")
|
||||
with patch_environment(
|
||||
world_size=num_processes, master_addr="127.0.01", master_port="29500", mixed_precision=args[1].mixed_precision
|
||||
):
|
||||
launcher = PrepareForLaunch(training_function, distributed_type="MULTI_GPU")
|
||||
torch.multiprocessing.start_processes(launcher, args=args, nprocs=num_processes, start_method="spawn")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Refer to https://runhouse-docs.readthedocs-hosted.com/en/main/rh_primitives/cluster.html#hardware-setup
|
||||
# for cloud access setup instructions (if using on-demand hardware), and for API specifications.
|
||||
|
||||
# on-demand GPU
|
||||
# gpu = rh.cluster(name='rh-cluster', instance_type='V100:1', provider='cheapest', use_spot=False) # single GPU
|
||||
gpu = rh.cluster(name="rh-cluster", instance_type="V100:4", provider="cheapest", use_spot=False) # multi GPU
|
||||
gpu.up_if_not()
|
||||
|
||||
# on-prem GPU
|
||||
# gpu = rh.cluster(
|
||||
# ips=["ip_addr"], ssh_creds={ssh_user:"<username>", ssh_private_key:"<key_path>"}, name="rh-cluster"
|
||||
# )
|
||||
|
||||
# Set up remote function
|
||||
reqs = [
|
||||
"pip:./",
|
||||
"transformers",
|
||||
"datasets",
|
||||
"evaluate",
|
||||
"tqdm",
|
||||
"scipy",
|
||||
"scikit-learn",
|
||||
"tensorboard",
|
||||
"torch --upgrade --extra-index-url https://download.pytorch.org/whl/cu117",
|
||||
]
|
||||
launch_train_gpu = rh.function(fn=launch_train, system=gpu, reqs=reqs, name="train_bert_glue")
|
||||
|
||||
# Define train args/config, run train function
|
||||
train_args = argparse.Namespace(cpu=False, mixed_precision="fp16")
|
||||
config = {"lr": 2e-5, "num_epochs": 3, "seed": 42, "batch_size": 16}
|
||||
launch_train_gpu(config, train_args, stream_logs=True)
|
||||
|
||||
# Alternatively, we can just run as instructed in the README (but only because there's already a wrapper CLI):
|
||||
# gpu.install_packages(reqs)
|
||||
# gpu.run(['accelerate launch --multi_gpu accelerate/examples/nlp_example.py'])
|
||||
@ -98,10 +98,14 @@ def get_dataloaders(accelerator: Accelerator, batch_size: int = 16):
|
||||
|
||||
# Instantiate dataloaders.
|
||||
train_dataloader = DataLoader(
|
||||
tokenized_datasets["train"], shuffle=True, collate_fn=collate_fn, batch_size=batch_size
|
||||
tokenized_datasets["train"], shuffle=True, collate_fn=collate_fn, batch_size=batch_size, drop_last=True
|
||||
)
|
||||
eval_dataloader = DataLoader(
|
||||
tokenized_datasets["validation"], shuffle=False, collate_fn=collate_fn, batch_size=EVAL_BATCH_SIZE
|
||||
tokenized_datasets["validation"],
|
||||
shuffle=False,
|
||||
collate_fn=collate_fn,
|
||||
batch_size=EVAL_BATCH_SIZE,
|
||||
drop_last=(accelerator.mixed_precision == "fp8"),
|
||||
)
|
||||
|
||||
return train_dataloader, eval_dataloader
|
||||
|
||||
45
setup.py
45
setup.py
@ -16,10 +16,10 @@ from setuptools import setup
|
||||
from setuptools import find_packages
|
||||
|
||||
extras = {}
|
||||
extras["quality"] = ["black ~= 23.1", "ruff >= 0.0.241", "hf-doc-builder >= 0.3.0"]
|
||||
extras["quality"] = ["black ~= 23.1", "ruff >= 0.0.241", "hf-doc-builder >= 0.3.0", "urllib3 < 2.0.0"]
|
||||
extras["docs"] = []
|
||||
extras["test_prod"] = ["pytest", "pytest-xdist", "pytest-subtests", "parameterized"]
|
||||
extras["test_dev"] = ["datasets", "evaluate", "transformers", "scipy", "scikit-learn", "deepspeed", "tqdm"]
|
||||
extras["test_dev"] = ["datasets", "evaluate", "transformers", "scipy", "scikit-learn", "deepspeed", "tqdm", "bitsandbytes", "timm"]
|
||||
extras["testing"] = extras["test_prod"] + extras["test_dev"]
|
||||
extras["rich"] = ["rich"]
|
||||
|
||||
@ -32,7 +32,7 @@ extras["sagemaker"] = [
|
||||
|
||||
setup(
|
||||
name="accelerate",
|
||||
version="0.17.0.dev0",
|
||||
version="0.24.0.dev0",
|
||||
description="Accelerate",
|
||||
long_description=open("README.md", "r", encoding="utf-8").read(),
|
||||
long_description_content_type="text/markdown",
|
||||
@ -47,11 +47,12 @@ setup(
|
||||
"console_scripts": [
|
||||
"accelerate=accelerate.commands.accelerate_cli:main",
|
||||
"accelerate-config=accelerate.commands.config:main",
|
||||
"accelerate-estimate-memory=accelerate.commands.estimate:main",
|
||||
"accelerate-launch=accelerate.commands.launch:main",
|
||||
]
|
||||
},
|
||||
python_requires=">=3.7.0",
|
||||
install_requires=["numpy>=1.17", "packaging>=20.0", "psutil", "pyyaml", "torch>=1.4.0"],
|
||||
python_requires=">=3.8.0",
|
||||
install_requires=["numpy>=1.17", "packaging>=20.0", "psutil", "pyyaml", "torch>=1.10.0", "huggingface_hub"],
|
||||
extras_require=extras,
|
||||
classifiers=[
|
||||
"Development Status :: 5 - Production/Stable",
|
||||
@ -61,27 +62,35 @@ setup(
|
||||
"License :: OSI Approved :: Apache Software License",
|
||||
"Operating System :: OS Independent",
|
||||
"Programming Language :: Python :: 3",
|
||||
"Programming Language :: Python :: 3.7",
|
||||
"Programming Language :: Python :: 3.8",
|
||||
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
||||
],
|
||||
)
|
||||
|
||||
# Release checklist
|
||||
# 1. Change the version in __init__.py and setup.py.
|
||||
# 2. Commit these changes with the message: "Release: VERSION"
|
||||
# 3. Add a tag in git to mark the release: "git tag VERSION -m 'Adds tag VERSION for pypi' "
|
||||
# Push the tag to git: git push --tags origin main
|
||||
# 4. Run the following commands in the top-level directory:
|
||||
# 1. Checkout the release branch (for a patch the current release branch, for a new minor version, create one):
|
||||
# git checkout -b vXX.xx-release
|
||||
# The -b is only necessary for creation (so remove it when doing a patch)
|
||||
# 2. Change the version in __init__.py and setup.py to the proper value.
|
||||
# 3. Commit these changes with the message: "Release: v<VERSION>"
|
||||
# 4. Add a tag in git to mark the release:
|
||||
# git tag v<VERSION> -m 'Adds tag v<VERSION> for pypi'
|
||||
# Push the tag and release commit to git: git push --tags origin vXX.xx-release
|
||||
# 5. Run the following commands in the top-level directory:
|
||||
# rm -rf dist
|
||||
# rm -rf build
|
||||
# python setup.py bdist_wheel
|
||||
# python setup.py sdist
|
||||
# 5. Upload the package to the pypi test server first:
|
||||
# twine upload dist/* -r pypitest
|
||||
# twine upload dist/* -r pypitest --repository-url=https://test.pypi.org/legacy/
|
||||
# 6. Check that you can install it in a virtualenv by running:
|
||||
# 6. Upload the package to the pypi test server first:
|
||||
# twine upload dist/* -r testpypi
|
||||
# 7. Check that you can install it in a virtualenv by running:
|
||||
# pip install accelerate
|
||||
# pip uninstall accelerate
|
||||
# pip install -i https://testpypi.python.org/pypi accelerate
|
||||
# accelerate env
|
||||
# accelerate test
|
||||
# 7. Upload the final version to actual pypi:
|
||||
# 8. Upload the final version to actual pypi:
|
||||
# twine upload dist/* -r pypi
|
||||
# 8. Add release notes to the tag in github once everything is looking hunky-dory.
|
||||
# 9. Update the version in __init__.py, setup.py to the new version "-dev" and push to master
|
||||
# 9. Add release notes to the tag in github once everything is looking hunky-dory.
|
||||
# 10. Go back to the main branch and update the version in __init__.py, setup.py to the new version ".dev" and push to
|
||||
# main.
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
__version__ = "0.17.0.dev0"
|
||||
__version__ = "0.24.0.dev0"
|
||||
|
||||
from .accelerator import Accelerator
|
||||
from .big_modeling import (
|
||||
@ -14,6 +14,7 @@ from .data_loader import skip_first_batches
|
||||
from .launchers import debug_launcher, notebook_launcher
|
||||
from .state import PartialState
|
||||
from .utils import (
|
||||
AutocastKwargs,
|
||||
DeepSpeedPlugin,
|
||||
DistributedDataParallelKwargs,
|
||||
DistributedType,
|
||||
|
||||
1086
src/accelerate/accelerator.py
Normal file → Executable file
1086
src/accelerate/accelerator.py
Normal file → Executable file
File diff suppressed because it is too large
Load Diff
@ -12,8 +12,10 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import logging
|
||||
import os
|
||||
from contextlib import contextmanager
|
||||
from functools import wraps
|
||||
from typing import Dict, List, Optional, Union
|
||||
|
||||
import torch
|
||||
@ -34,21 +36,25 @@ from .utils import (
|
||||
find_tied_parameters,
|
||||
get_balanced_memory,
|
||||
infer_auto_device_map,
|
||||
is_torch_version,
|
||||
load_checkpoint_in_model,
|
||||
offload_state_dict,
|
||||
parse_flag_from_env,
|
||||
retie_parameters,
|
||||
)
|
||||
from .utils.versions import is_torch_version
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def init_empty_weights(include_buffers: bool = False):
|
||||
def init_empty_weights(include_buffers: bool = None):
|
||||
"""
|
||||
A context manager under which models are initialized with all parameters on the meta device, therefore creating an
|
||||
empty model. Useful when just initializing the model would blow the available RAM.
|
||||
|
||||
Args:
|
||||
include_buffers (`bool`, *optional*, defaults to `False`):
|
||||
include_buffers (`bool`, *optional*):
|
||||
Whether or not to also put all buffers on the meta device while initializing.
|
||||
|
||||
Example:
|
||||
@ -69,21 +75,21 @@ def init_empty_weights(include_buffers: bool = False):
|
||||
|
||||
</Tip>
|
||||
"""
|
||||
if not is_torch_version(">=", "1.9.0"):
|
||||
raise NotImplementedError("Initializing empty weights to a meta device requires torch >= 1.9.0")
|
||||
if include_buffers is None:
|
||||
include_buffers = parse_flag_from_env("ACCELERATE_INIT_INCLUDE_BUFFERS", False)
|
||||
with init_on_device(torch.device("meta"), include_buffers=include_buffers) as f:
|
||||
yield f
|
||||
|
||||
|
||||
@contextmanager
|
||||
def init_on_device(device: torch.device, include_buffers: bool = False):
|
||||
def init_on_device(device: torch.device, include_buffers: bool = None):
|
||||
"""
|
||||
A context manager under which models are initialized with all parameters on the specified device.
|
||||
|
||||
Args:
|
||||
device (`torch.device`):
|
||||
Device to initialize all parameters on.
|
||||
include_buffers (`bool`, *optional*, defaults to `False`):
|
||||
include_buffers (`bool`, *optional*):
|
||||
Whether or not to also put all buffers on the meta device while initializing.
|
||||
|
||||
Example:
|
||||
@ -96,6 +102,15 @@ def init_on_device(device: torch.device, include_buffers: bool = False):
|
||||
tst = nn.Liner(100, 100) # on `cuda` device
|
||||
```
|
||||
"""
|
||||
if include_buffers is None:
|
||||
include_buffers = parse_flag_from_env("ACCELERATE_INIT_INCLUDE_BUFFERS", False)
|
||||
|
||||
# TODO(shingjan): remove the torch version check once older versions are deprecated
|
||||
if is_torch_version(">=", "2.0") and include_buffers:
|
||||
with device:
|
||||
yield
|
||||
return
|
||||
|
||||
old_register_parameter = nn.Module.register_parameter
|
||||
if include_buffers:
|
||||
old_register_buffer = nn.Module.register_buffer
|
||||
@ -107,8 +122,8 @@ def init_on_device(device: torch.device, include_buffers: bool = False):
|
||||
kwargs = module._parameters[name].__dict__
|
||||
module._parameters[name] = param_cls(module._parameters[name].to(device), **kwargs)
|
||||
|
||||
def register_empty_buffer(module, name, buffer):
|
||||
old_register_buffer(module, name, buffer)
|
||||
def register_empty_buffer(module, name, buffer, persistent=True):
|
||||
old_register_buffer(module, name, buffer, persistent=persistent)
|
||||
if buffer is not None:
|
||||
module._buffers[name] = module._buffers[name].to(device)
|
||||
|
||||
@ -171,8 +186,6 @@ def cpu_offload(
|
||||
called directly during the forward, for instance if a `dense` linear layer is registered, but at forward,
|
||||
`dense.weight` and `dense.bias` are used in some operations instead of calling `dense` directly.
|
||||
"""
|
||||
if not is_torch_version(">=", "1.9.0"):
|
||||
raise NotImplementedError("CPU offloading requires torch >= 1.9.0")
|
||||
if execution_device is None:
|
||||
execution_device = next(iter(model.parameters())).device
|
||||
if state_dict is None:
|
||||
@ -262,8 +275,6 @@ def disk_offload(
|
||||
called directly during the forward, for instance if a `dense` linear layer is registered, but at forward,
|
||||
`dense.weight` and `dense.bias` are used in some operations instead of calling `dense` directly.
|
||||
"""
|
||||
if not is_torch_version(">=", "1.9.0"):
|
||||
raise NotImplementedError("Disk offloading requires torch >= 1.9.0")
|
||||
if not os.path.isdir(offload_dir) or not os.path.isfile(os.path.join(offload_dir, "index.json")):
|
||||
offload_state_dict(offload_dir, model.state_dict())
|
||||
if execution_device is None:
|
||||
@ -291,7 +302,9 @@ def dispatch_model(
|
||||
offload_dir: Optional[Union[str, os.PathLike]] = None,
|
||||
offload_index: Optional[Dict[str, str]] = None,
|
||||
offload_buffers: bool = False,
|
||||
skip_keys: Optional[Union[str, List[str]]] = None,
|
||||
preload_module_classes: Optional[List[str]] = None,
|
||||
force_hooks: bool = False,
|
||||
):
|
||||
"""
|
||||
Dispatches a model according to a given device map. Layers of the model might be spread across GPUs, offloaded on
|
||||
@ -315,68 +328,106 @@ def dispatch_model(
|
||||
to the index saved in `save_folder`.
|
||||
offload_buffers (`bool`, *optional*, defaults to `False`):
|
||||
Whether or not to offload the buffers with the model parameters.
|
||||
skip_keys (`str` or `List[str]`, *optional*):
|
||||
A list of keys to ignore when moving inputs or outputs between devices.
|
||||
preload_module_classes (`List[str]`, *optional*):
|
||||
A list of classes whose instances should load all their weights (even in the submodules) at the beginning
|
||||
of the forward. This should only be used for classes that have submodules which are registered but not
|
||||
called directly during the forward, for instance if a `dense` linear layer is registered, but at forward,
|
||||
`dense.weight` and `dense.bias` are used in some operations instead of calling `dense` directly.
|
||||
force_hooks (`bool`, *optional*, defaults to `False`):
|
||||
Whether or not to force device hooks to be attached to the model even if all layers are dispatched to a
|
||||
single device.
|
||||
"""
|
||||
if not is_torch_version(">=", "1.9.0"):
|
||||
raise NotImplementedError("Model dispatching requires torch >= 1.9.0")
|
||||
# Error early if the device map is incomplete.
|
||||
check_device_map(model, device_map)
|
||||
|
||||
if main_device is None:
|
||||
if set(device_map.values()) == {"cpu"} or set(device_map.values()) == {"cpu", "disk"}:
|
||||
main_device = "cpu"
|
||||
# for backward compatibility
|
||||
is_bnb_quantized = (
|
||||
getattr(model, "is_quantized", False) or getattr(model, "is_loaded_in_8bit", False)
|
||||
) and getattr(model, "quantization_method", "bitsandbytes") == "bitsandbytes"
|
||||
|
||||
# We attach hooks if the device_map has at least 2 different devices or if
|
||||
# force_hooks is set to `True`. Otherwise, the model in already loaded
|
||||
# in the unique device and the user can decide where to dispatch the model.
|
||||
# If the model is quantized, we always force-dispatch the model
|
||||
if (len(set(device_map.values())) > 1) or is_bnb_quantized or force_hooks:
|
||||
if main_device is None:
|
||||
if set(device_map.values()) == {"cpu"} or set(device_map.values()) == {"cpu", "disk"}:
|
||||
main_device = "cpu"
|
||||
else:
|
||||
main_device = [d for d in device_map.values() if d not in ["cpu", "disk"]][0]
|
||||
|
||||
if main_device != "cpu":
|
||||
cpu_modules = [name for name, device in device_map.items() if device == "cpu"]
|
||||
if state_dict is None and len(cpu_modules) > 0:
|
||||
state_dict = extract_submodules_state_dict(model.state_dict(), cpu_modules)
|
||||
|
||||
disk_modules = [name for name, device in device_map.items() if device == "disk"]
|
||||
if offload_dir is None and offload_index is None and len(disk_modules) > 0:
|
||||
raise ValueError(
|
||||
"We need an `offload_dir` to dispatch this model according to this `device_map`, the following submodules "
|
||||
f"need to be offloaded: {', '.join(disk_modules)}."
|
||||
)
|
||||
if (
|
||||
len(disk_modules) > 0
|
||||
and offload_index is None
|
||||
and (not os.path.isdir(offload_dir) or not os.path.isfile(os.path.join(offload_dir, "index.json")))
|
||||
):
|
||||
disk_state_dict = extract_submodules_state_dict(model.state_dict(), disk_modules)
|
||||
offload_state_dict(offload_dir, disk_state_dict)
|
||||
|
||||
execution_device = {
|
||||
name: main_device if device in ["cpu", "disk"] else device for name, device in device_map.items()
|
||||
}
|
||||
execution_device[""] = main_device
|
||||
offloaded_devices = ["disk"] if main_device == "cpu" or main_device == "mps" else ["cpu", "disk"]
|
||||
offload = {name: device in offloaded_devices for name, device in device_map.items()}
|
||||
save_folder = offload_dir if len(disk_modules) > 0 else None
|
||||
if state_dict is not None or save_folder is not None or offload_index is not None:
|
||||
device = main_device if offload_index is not None else None
|
||||
weights_map = OffloadedWeightsLoader(
|
||||
state_dict=state_dict, save_folder=save_folder, index=offload_index, device=device
|
||||
)
|
||||
else:
|
||||
main_device = [d for d in device_map.values() if d not in ["cpu", "disk"]][0]
|
||||
weights_map = None
|
||||
|
||||
if main_device != "cpu":
|
||||
cpu_modules = [name for name, device in device_map.items() if device == "cpu"]
|
||||
if state_dict is None and len(cpu_modules) > 0:
|
||||
state_dict = extract_submodules_state_dict(model.state_dict(), cpu_modules)
|
||||
|
||||
disk_modules = [name for name, device in device_map.items() if device == "disk"]
|
||||
if offload_dir is None and offload_index is None and len(disk_modules) > 0:
|
||||
raise ValueError(
|
||||
"We need an `offload_dir` to dispatch this model according to this `device_map`, the following submodules "
|
||||
f"need to be offloaded: {', '.join(disk_modules)}."
|
||||
tied_params = find_tied_parameters(model)
|
||||
attach_align_device_hook_on_blocks(
|
||||
model,
|
||||
execution_device=execution_device,
|
||||
offload=offload,
|
||||
offload_buffers=offload_buffers,
|
||||
weights_map=weights_map,
|
||||
skip_keys=skip_keys,
|
||||
preload_module_classes=preload_module_classes,
|
||||
)
|
||||
if (
|
||||
len(disk_modules) > 0
|
||||
and offload_index is None
|
||||
and (not os.path.isdir(offload_dir) or not os.path.isfile(os.path.join(offload_dir, "index.json")))
|
||||
):
|
||||
disk_state_dict = extract_submodules_state_dict(model.state_dict(), disk_modules)
|
||||
offload_state_dict(offload_dir, disk_state_dict)
|
||||
# Attaching the hook may break tied weights, so we retie them
|
||||
retie_parameters(model, tied_params)
|
||||
|
||||
# add warning to cuda and to method
|
||||
def add_warning(fn, model):
|
||||
@wraps(fn)
|
||||
def wrapper(*args, **kwargs):
|
||||
logger.warning("You shouldn't move a model when it is dispatched on multiple devices.")
|
||||
for param in model.parameters():
|
||||
if param.device == torch.device("meta"):
|
||||
raise RuntimeError("You can't move a model that has some modules offloaded to cpu or disk.")
|
||||
return fn(*args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
|
||||
model.to = add_warning(model.to, model)
|
||||
model.cuda = add_warning(model.cuda, model)
|
||||
|
||||
execution_device = {
|
||||
name: main_device if device in ["cpu", "disk"] else device for name, device in device_map.items()
|
||||
}
|
||||
execution_device[""] = main_device
|
||||
offloaded_devices = ["disk"] if main_device == "cpu" else ["cpu", "disk"]
|
||||
offload = {name: device in offloaded_devices for name, device in device_map.items()}
|
||||
save_folder = offload_dir if len(disk_modules) > 0 else None
|
||||
if state_dict is not None or save_folder is not None or offload_index is not None:
|
||||
device = main_device if offload_index is not None else None
|
||||
weights_map = OffloadedWeightsLoader(
|
||||
state_dict=state_dict, save_folder=save_folder, index=offload_index, device=device
|
||||
)
|
||||
else:
|
||||
weights_map = None
|
||||
|
||||
tied_params = find_tied_parameters(model)
|
||||
attach_align_device_hook_on_blocks(
|
||||
model,
|
||||
execution_device=execution_device,
|
||||
offload=offload,
|
||||
offload_buffers=offload_buffers,
|
||||
weights_map=weights_map,
|
||||
preload_module_classes=preload_module_classes,
|
||||
)
|
||||
# Attaching the hook may break tied weights, so we retie them
|
||||
retie_parameters(model, tied_params)
|
||||
device = list(device_map.values())[0]
|
||||
if device != "disk":
|
||||
model.to(device)
|
||||
else:
|
||||
raise ValueError(
|
||||
"You are trying to offload the whole model to the disk. Please use the `disk_offload` function instead."
|
||||
)
|
||||
model.hf_device_map = device_map
|
||||
return model
|
||||
|
||||
@ -391,7 +442,9 @@ def load_checkpoint_and_dispatch(
|
||||
offload_buffers: bool = False,
|
||||
dtype: Optional[Union[str, torch.dtype]] = None,
|
||||
offload_state_dict: Optional[bool] = None,
|
||||
skip_keys: Optional[Union[str, List[str]]] = None,
|
||||
preload_module_classes: Optional[List[str]] = None,
|
||||
force_hooks: bool = False,
|
||||
):
|
||||
"""
|
||||
Loads a (potentially sharded) checkpoint inside a model, potentially sending weights to a given device as they are
|
||||
@ -427,11 +480,16 @@ def load_checkpoint_and_dispatch(
|
||||
If `True`, will temporarily offload the CPU state dict on the hard drive to avoid getting out of CPU RAM if
|
||||
the weight of the CPU state dict + the biggest shard does not fit. Will default to `True` if the device map
|
||||
picked contains `"disk"` values.
|
||||
skip_keys (`str` or `List[str]`, *optional*):
|
||||
A list of keys to ignore when moving inputs or outputs between devices.
|
||||
preload_module_classes (`List[str]`, *optional*):
|
||||
A list of classes whose instances should load all their weights (even in the submodules) at the beginning
|
||||
of the forward. This should only be used for classes that have submodules which are registered but not
|
||||
called directly during the forward, for instance if a `dense` linear layer is registered, but at forward,
|
||||
`dense.weight` and `dense.bias` are used in some operations instead of calling `dense` directly.
|
||||
force_hooks (`bool`, *optional*, defaults to `False`):
|
||||
Whether or not to force device hooks to be attached to the model even if all layers are dispatched to a
|
||||
single device.
|
||||
|
||||
Example:
|
||||
|
||||
@ -455,26 +513,24 @@ def load_checkpoint_and_dispatch(
|
||||
... )
|
||||
```
|
||||
"""
|
||||
if not is_torch_version(">=", "1.9.0"):
|
||||
raise NotImplementedError("Loading and dispatching requires torch >= 1.9.0")
|
||||
if isinstance(device_map, str) and device_map not in ["auto", "balanced", "balanced_low_0", "sequential"]:
|
||||
raise ValueError(
|
||||
"If passing a string for `device_map`, please choose 'auto', 'balanced', 'balanced_low_0' or "
|
||||
"'sequential'."
|
||||
)
|
||||
if device_map != "sequential":
|
||||
max_memory = get_balanced_memory(
|
||||
model,
|
||||
max_memory=max_memory,
|
||||
no_split_module_classes=no_split_module_classes,
|
||||
dtype=dtype,
|
||||
low_zero=(device_map == "balanced_low_0"),
|
||||
)
|
||||
if isinstance(device_map, str):
|
||||
if device_map != "sequential":
|
||||
max_memory = get_balanced_memory(
|
||||
model,
|
||||
max_memory=max_memory,
|
||||
no_split_module_classes=no_split_module_classes,
|
||||
dtype=dtype,
|
||||
low_zero=(device_map == "balanced_low_0"),
|
||||
)
|
||||
device_map = infer_auto_device_map(
|
||||
model, max_memory=max_memory, no_split_module_classes=no_split_module_classes, dtype=dtype
|
||||
)
|
||||
if offload_state_dict is None and "disk" in device_map.values():
|
||||
if offload_state_dict is None and device_map is not None and "disk" in device_map.values():
|
||||
offload_state_dict = True
|
||||
load_checkpoint_in_model(
|
||||
model,
|
||||
@ -492,5 +548,7 @@ def load_checkpoint_and_dispatch(
|
||||
device_map=device_map,
|
||||
offload_dir=offload_folder,
|
||||
offload_buffers=offload_buffers,
|
||||
skip_keys=skip_keys,
|
||||
preload_module_classes=preload_module_classes,
|
||||
force_hooks=force_hooks,
|
||||
)
|
||||
|
||||
@ -29,6 +29,7 @@ from .utils import (
|
||||
SCHEDULER_NAME,
|
||||
get_pretty_name,
|
||||
is_tpu_available,
|
||||
is_xpu_available,
|
||||
save,
|
||||
)
|
||||
|
||||
@ -37,6 +38,7 @@ if is_tpu_available(check_device=False):
|
||||
import torch_xla.core.xla_model as xm
|
||||
|
||||
from .logging import get_logger
|
||||
from .state import PartialState
|
||||
|
||||
|
||||
logger = get_logger(__name__)
|
||||
@ -99,8 +101,10 @@ def save_accelerator_state(
|
||||
states["random_state"] = random.getstate()
|
||||
states["numpy_random_seed"] = np.random.get_state()
|
||||
states["torch_manual_seed"] = torch.get_rng_state()
|
||||
states["torch_cuda_manual_seed"] = torch.cuda.get_rng_state_all()
|
||||
# ^^ safe to call this function even if cuda is not available
|
||||
if is_xpu_available():
|
||||
states["torch_xpu_manual_seed"] = torch.xpu.get_rng_state_all()
|
||||
else:
|
||||
states["torch_cuda_manual_seed"] = torch.cuda.get_rng_state_all()
|
||||
if is_tpu_available():
|
||||
states["xm_seed"] = xm.get_rng_state()
|
||||
output_states_file = os.path.join(output_dir, states_name)
|
||||
@ -110,7 +114,14 @@ def save_accelerator_state(
|
||||
|
||||
|
||||
def load_accelerator_state(
|
||||
input_dir, models, optimizers, schedulers, process_index, scaler=None, **load_model_func_kwargs
|
||||
input_dir,
|
||||
models,
|
||||
optimizers,
|
||||
schedulers,
|
||||
process_index,
|
||||
scaler=None,
|
||||
map_location=None,
|
||||
**load_model_func_kwargs,
|
||||
):
|
||||
"""
|
||||
Loads states of the models, optimizers, scaler, and RNG generators from a given directory.
|
||||
@ -128,21 +139,32 @@ def load_accelerator_state(
|
||||
The current process index in the Accelerator state
|
||||
scaler (`torch.cuda.amp.GradScaler`, *optional*):
|
||||
An optional *GradScaler* instance to load
|
||||
map_location (`str`, *optional*):
|
||||
What device to load the optimizer state onto. Should be one of either "cpu" or "on_device".
|
||||
load_model_func_kwargs (`dict`, *optional*):
|
||||
Additional arguments that can be passed to the model's `load_state_dict` method.
|
||||
"""
|
||||
if map_location not in [None, "cpu", "on_device"]:
|
||||
raise TypeError(
|
||||
"Unsupported optimizer map location passed, please choose one of `None`, `'cpu'`, or `'on_device'`"
|
||||
)
|
||||
if map_location is None:
|
||||
map_location = "cpu"
|
||||
elif map_location == "on_device":
|
||||
map_location = PartialState().device
|
||||
# Model states
|
||||
for i, model in enumerate(models):
|
||||
weights_name = f"{MODEL_NAME}.bin" if i == 0 else f"{MODEL_NAME}_{i}.bin"
|
||||
input_model_file = os.path.join(input_dir, weights_name)
|
||||
models[i].load_state_dict(torch.load(input_model_file, map_location="cpu"), **load_model_func_kwargs)
|
||||
models[i].load_state_dict(torch.load(input_model_file, map_location=map_location), **load_model_func_kwargs)
|
||||
logger.info("All model weights loaded successfully")
|
||||
|
||||
# Optimizer states
|
||||
for i, opt in enumerate(optimizers):
|
||||
optimizer_name = f"{OPTIMIZER_NAME}.bin" if i == 0 else f"{OPTIMIZER_NAME}_{i}.bin"
|
||||
input_optimizer_file = os.path.join(input_dir, optimizer_name)
|
||||
optimizers[i].load_state_dict(torch.load(input_optimizer_file, map_location="cpu"))
|
||||
optimizer_state = torch.load(input_optimizer_file, map_location=map_location)
|
||||
optimizers[i].load_state_dict(optimizer_state)
|
||||
logger.info("All optimizer states loaded successfully")
|
||||
|
||||
# Scheduler states
|
||||
@ -164,8 +186,10 @@ def load_accelerator_state(
|
||||
random.setstate(states["random_state"])
|
||||
np.random.set_state(states["numpy_random_seed"])
|
||||
torch.set_rng_state(states["torch_manual_seed"])
|
||||
torch.cuda.set_rng_state_all(states["torch_cuda_manual_seed"])
|
||||
# ^^ safe to call this function even if cuda is not available
|
||||
if is_xpu_available():
|
||||
torch.xpu.set_rng_state_all(states["torch_xpu_manual_seed"])
|
||||
else:
|
||||
torch.cuda.set_rng_state_all(states["torch_cuda_manual_seed"])
|
||||
if is_tpu_available():
|
||||
xm.set_rng_state(states["xm_seed"])
|
||||
logger.info("All random states loaded successfully")
|
||||
|
||||
@ -18,17 +18,19 @@ from argparse import ArgumentParser
|
||||
|
||||
from accelerate.commands.config import get_config_parser
|
||||
from accelerate.commands.env import env_command_parser
|
||||
from accelerate.commands.estimate import estimate_command_parser
|
||||
from accelerate.commands.launch import launch_command_parser
|
||||
from accelerate.commands.test import test_command_parser
|
||||
from accelerate.commands.tpu import tpu_command_parser
|
||||
|
||||
|
||||
def main():
|
||||
parser = ArgumentParser("Accelerate CLI tool", usage="accelerate <command> [<args>]")
|
||||
parser = ArgumentParser("Accelerate CLI tool", usage="accelerate <command> [<args>]", allow_abbrev=False)
|
||||
subparsers = parser.add_subparsers(help="accelerate command helpers")
|
||||
|
||||
# Register commands
|
||||
get_config_parser(subparsers=subparsers)
|
||||
estimate_command_parser(subparsers=subparsers)
|
||||
env_command_parser(subparsers=subparsers)
|
||||
launch_command_parser(subparsers=subparsers)
|
||||
tpu_command_parser(subparsers=subparsers)
|
||||
|
||||
@ -23,7 +23,7 @@ from .update import update_command_parser
|
||||
|
||||
|
||||
def get_config_parser(subparsers=None):
|
||||
parent_parser = argparse.ArgumentParser(add_help=False)
|
||||
parent_parser = argparse.ArgumentParser(add_help=False, allow_abbrev=False)
|
||||
# The main config parser
|
||||
config_parser = config_command_parser(subparsers)
|
||||
# The subparser to add commands to
|
||||
|
||||
@ -21,7 +21,9 @@ from ...utils import (
|
||||
DistributedType,
|
||||
is_deepspeed_available,
|
||||
is_mps_available,
|
||||
is_npu_available,
|
||||
is_transformers_available,
|
||||
is_xpu_available,
|
||||
)
|
||||
from ...utils.constants import (
|
||||
DEEPSPEED_MULTINODE_LAUNCHERS,
|
||||
@ -46,7 +48,7 @@ from .config_utils import (
|
||||
def get_cluster_input():
|
||||
distributed_type = _ask_options(
|
||||
"Which type of machine are you using?",
|
||||
["No distributed training", "multi-CPU", "multi-GPU", "TPU"],
|
||||
["No distributed training", "multi-CPU", "multi-XPU", "multi-GPU", "multi-NPU", "TPU"],
|
||||
_convert_distributed_mode,
|
||||
)
|
||||
|
||||
@ -58,8 +60,14 @@ def get_cluster_input():
|
||||
main_process_port = None
|
||||
rdzv_backend = "static"
|
||||
same_network = True
|
||||
debug = False
|
||||
|
||||
if distributed_type in [DistributedType.MULTI_GPU, DistributedType.MULTI_CPU]:
|
||||
if distributed_type in [
|
||||
DistributedType.MULTI_GPU,
|
||||
DistributedType.MULTI_NPU,
|
||||
DistributedType.MULTI_XPU,
|
||||
DistributedType.MULTI_CPU,
|
||||
]:
|
||||
num_machines = _ask_field(
|
||||
"How many different machines will you use (use more than 1 for multi-node training)? [1]: ",
|
||||
int,
|
||||
@ -88,10 +96,16 @@ def get_cluster_input():
|
||||
rdzv_backend = _ask_field(
|
||||
"What rendezvous backend will you use? ('static', 'c10d', ...): ", default="static"
|
||||
)
|
||||
debug = _ask_field(
|
||||
"Should distributed operations be checked while running for errors? This can avoid timeout issues but will be slower. [yes/NO]: ",
|
||||
_convert_yes_no_to_bool,
|
||||
default=False,
|
||||
error_message="Please enter yes or no.",
|
||||
)
|
||||
|
||||
if distributed_type == DistributedType.NO:
|
||||
use_cpu = _ask_field(
|
||||
"Do you want to run your training on CPU only (even if a GPU / Apple Silicon device is available)? [yes/NO]:",
|
||||
"Do you want to run your training on CPU only (even if a GPU / Apple Silicon / Ascend NPU device is available)? [yes/NO]:",
|
||||
_convert_yes_no_to_bool,
|
||||
default=False,
|
||||
error_message="Please enter yes or no.",
|
||||
@ -101,6 +115,26 @@ def get_cluster_input():
|
||||
else:
|
||||
use_cpu = False
|
||||
|
||||
ipex_config = {}
|
||||
if use_cpu:
|
||||
ipex_config["ipex"] = _ask_field(
|
||||
"Do you want to use Intel PyTorch Extension (IPEX) to speed up training on CPU? [yes/NO]:",
|
||||
_convert_yes_no_to_bool,
|
||||
default=False,
|
||||
error_message="Please enter yes or no.",
|
||||
)
|
||||
if (
|
||||
not use_cpu
|
||||
and is_xpu_available()
|
||||
and distributed_type not in [DistributedType.MULTI_GPU, DistributedType.MULTI_NPU, DistributedType.TPU]
|
||||
):
|
||||
ipex_config["use_xpu"] = _ask_field(
|
||||
"Do you want to use XPU plugin to speed up training on XPU? [yes/NO]:",
|
||||
_convert_yes_no_to_bool,
|
||||
default=False,
|
||||
error_message="Please enter yes or no.",
|
||||
)
|
||||
|
||||
dynamo_config = {}
|
||||
use_dynamo = _ask_field(
|
||||
"Do you wish to optimize your script with torch dynamo?[yes/NO]:",
|
||||
@ -187,6 +221,18 @@ def get_cluster_input():
|
||||
deepspeed_config["offload_param_device"] = _ask_options(
|
||||
"Where to offload parameters?", deepspeed_devices, lambda x: deepspeed_devices[int(x)]
|
||||
)
|
||||
if deepspeed_config["offload_param_device"] == "nvme":
|
||||
deepspeed_config["offload_param_nvme_path"] = _ask_field(
|
||||
"Nvme Path to offload parameters?",
|
||||
str,
|
||||
default="/nvme",
|
||||
)
|
||||
if deepspeed_config["offload_optimizer_device"] == "nvme":
|
||||
deepspeed_config["offload_optimizer_nvme_path"] = _ask_field(
|
||||
"Nvme Path to offload optimizer states?",
|
||||
str,
|
||||
default="/nvme",
|
||||
)
|
||||
deepspeed_config["gradient_accumulation_steps"] = _ask_field(
|
||||
"How many gradient accumulation steps you're passing in your script? [1]: ",
|
||||
int,
|
||||
@ -267,7 +313,7 @@ def get_cluster_input():
|
||||
)
|
||||
|
||||
fsdp_config = {}
|
||||
if distributed_type in [DistributedType.MULTI_GPU]:
|
||||
if distributed_type in [DistributedType.MULTI_GPU, DistributedType.MULTI_NPU, DistributedType.MULTI_XPU]:
|
||||
use_fsdp = _ask_field(
|
||||
"Do you want to use FullyShardedDataParallel? [yes/NO]: ",
|
||||
_convert_yes_no_to_bool,
|
||||
@ -297,16 +343,23 @@ def get_cluster_input():
|
||||
lambda x: FSDP_AUTO_WRAP_POLICY[int(x)],
|
||||
)
|
||||
if fsdp_config["fsdp_auto_wrap_policy"] == FSDP_AUTO_WRAP_POLICY[0]:
|
||||
fsdp_config["fsdp_transformer_layer_cls_to_wrap"] = _ask_field(
|
||||
"Specify the comma-separated list of transformer layer class names (case-sensitive) to wrap ,e.g, :"
|
||||
"`BertLayer`, `GPTJBlock`, `T5Block`, `BertLayer,BertEmbeddings,BertSelfOutput` ...? : ",
|
||||
str,
|
||||
use_no_split_modules = _ask_field(
|
||||
"Do you want to use the model's `_no_split_modules` to wrap. Only applicable for 🤗 Transformers [yes/NO]: ",
|
||||
_convert_yes_no_to_bool,
|
||||
default=False,
|
||||
error_message="Please enter yes or no.",
|
||||
)
|
||||
if not use_no_split_modules:
|
||||
fsdp_config["fsdp_transformer_layer_cls_to_wrap"] = _ask_field(
|
||||
"Specify the comma-separated list of transformer layer class names (case-sensitive) to wrap ,e.g, :"
|
||||
"`BertLayer`, `GPTJBlock`, `T5Block`, `BertLayer,BertEmbeddings,BertSelfOutput` ...? : ",
|
||||
str,
|
||||
)
|
||||
elif fsdp_config["fsdp_auto_wrap_policy"] == FSDP_AUTO_WRAP_POLICY[1]:
|
||||
fsdp_config["fsdp_min_num_params"] = _ask_field(
|
||||
"What should be your FSDP's minimum number of parameters for Default Auto Wrapping Policy? [1e8]: ",
|
||||
int,
|
||||
default=1e8,
|
||||
default=100000000,
|
||||
)
|
||||
fsdp_backward_prefetch_query = "What should be your FSDP's backward prefetch policy?"
|
||||
fsdp_config["fsdp_backward_prefetch_policy"] = _ask_options(
|
||||
@ -319,6 +372,25 @@ def get_cluster_input():
|
||||
fsdp_state_dict_type_query,
|
||||
FSDP_STATE_DICT_TYPE,
|
||||
lambda x: FSDP_STATE_DICT_TYPE[int(x)],
|
||||
default=2,
|
||||
)
|
||||
fsdp_config["fsdp_forward_prefetch"] = _ask_field(
|
||||
"Do you want to enable FSDP's forward prefetch policy? [yes/NO]: ",
|
||||
_convert_yes_no_to_bool,
|
||||
default=False,
|
||||
error_message="Please enter yes or no.",
|
||||
)
|
||||
fsdp_config["fsdp_use_orig_params"] = _ask_field(
|
||||
"Do you want to enable FSDP's `use_orig_params` feature? [yes/NO]: ",
|
||||
_convert_yes_no_to_bool,
|
||||
default=False,
|
||||
error_message="Please enter yes or no.",
|
||||
)
|
||||
fsdp_config["fsdp_sync_module_states"] = _ask_field(
|
||||
"Do you want each individually wrapped FSDP unit to broadcast module parameters from rank 0 at the start? [YES/no]: ",
|
||||
_convert_yes_no_to_bool,
|
||||
default=True,
|
||||
error_message="Please enter yes or no.",
|
||||
)
|
||||
|
||||
megatron_lm_config = {}
|
||||
@ -392,7 +464,13 @@ def get_cluster_input():
|
||||
tpu_use_sudo = False
|
||||
tpu_use_cluster = False
|
||||
|
||||
if distributed_type in [DistributedType.MULTI_CPU, DistributedType.MULTI_GPU, DistributedType.TPU]:
|
||||
if distributed_type in [
|
||||
DistributedType.MULTI_CPU,
|
||||
DistributedType.MULTI_XPU,
|
||||
DistributedType.MULTI_GPU,
|
||||
DistributedType.MULTI_NPU,
|
||||
DistributedType.TPU,
|
||||
]:
|
||||
machine_type = str(distributed_type).split(".")[1].replace("MULTI_", "")
|
||||
if machine_type == "TPU":
|
||||
machine_type += " cores"
|
||||
@ -414,9 +492,28 @@ def get_cluster_input():
|
||||
else:
|
||||
num_processes = 1
|
||||
|
||||
if distributed_type in [DistributedType.MULTI_GPU, DistributedType.NO] and not use_cpu and not use_mps:
|
||||
if (distributed_type == DistributedType.MULTI_GPU) and (num_machines == 1) and (num_processes == 1):
|
||||
raise ValueError(
|
||||
f"Specified distributed type {distributed_type} but only using 1 GPU on a single machine. Please select `No distributed training` for the type of machine you are using."
|
||||
)
|
||||
|
||||
if (
|
||||
distributed_type
|
||||
in [
|
||||
DistributedType.MULTI_GPU,
|
||||
DistributedType.MULTI_NPU,
|
||||
DistributedType.MULTI_XPU,
|
||||
DistributedType.NO,
|
||||
]
|
||||
and not use_cpu
|
||||
and not use_mps
|
||||
):
|
||||
if is_npu_available():
|
||||
machine_type = "NPU(s)"
|
||||
else:
|
||||
machine_type = "GPU(s)"
|
||||
gpu_ids = _ask_field(
|
||||
"What GPU(s) (by id) should be used for training on this machine as a comma-seperated list? [all]:",
|
||||
f"What {machine_type} (by id) should be used for training on this machine as a comma-seperated list? [all]:",
|
||||
default="all",
|
||||
)
|
||||
|
||||
@ -531,6 +628,7 @@ def get_cluster_input():
|
||||
deepspeed_config=deepspeed_config,
|
||||
fsdp_config=fsdp_config,
|
||||
megatron_lm_config=megatron_lm_config,
|
||||
ipex_config=ipex_config,
|
||||
use_cpu=use_cpu,
|
||||
rdzv_backend=rdzv_backend,
|
||||
same_network=same_network,
|
||||
@ -543,4 +641,5 @@ def get_cluster_input():
|
||||
tpu_use_sudo=tpu_use_sudo,
|
||||
tpu_use_cluster=tpu_use_cluster,
|
||||
dynamo_config=dynamo_config,
|
||||
debug=debug,
|
||||
)
|
||||
|
||||
@ -78,6 +78,7 @@ class BaseConfig:
|
||||
distributed_type: Union[DistributedType, SageMakerDistributedType]
|
||||
mixed_precision: str
|
||||
use_cpu: bool
|
||||
debug: bool
|
||||
|
||||
def to_dict(self):
|
||||
result = self.__dict__
|
||||
@ -85,6 +86,8 @@ class BaseConfig:
|
||||
for key, value in result.items():
|
||||
if isinstance(value, Enum):
|
||||
result[key] = value.value
|
||||
if isinstance(value, dict) and not bool(value):
|
||||
result[key] = None
|
||||
result = {k: v for k, v in result.items() if v is not None}
|
||||
return result
|
||||
|
||||
@ -99,8 +102,20 @@ class BaseConfig:
|
||||
config_dict["mixed_precision"] = "fp16" if ("fp16" in config_dict and config_dict["fp16"]) else None
|
||||
if "fp16" in config_dict: # Convert the config to the new format.
|
||||
del config_dict["fp16"]
|
||||
if "dynamo_backend" in config_dict: # Convert the config to the new format.
|
||||
dynamo_backend = config_dict.pop("dynamo_backend")
|
||||
config_dict["dynamo_config"] = {} if dynamo_backend == "NO" else {"dynamo_backend": dynamo_backend}
|
||||
if "use_cpu" not in config_dict:
|
||||
config_dict["use_cpu"] = False
|
||||
if "debug" not in config_dict:
|
||||
config_dict["debug"] = False
|
||||
extra_keys = sorted(set(config_dict.keys()) - set(cls.__dataclass_fields__.keys()))
|
||||
if len(extra_keys) > 0:
|
||||
raise ValueError(
|
||||
f"The config file at {json_file} had unknown keys ({extra_keys}), please try upgrading your `accelerate`"
|
||||
" version or fix (and potentially remove) these keys from your config file."
|
||||
)
|
||||
|
||||
return cls(**config_dict)
|
||||
|
||||
def to_json_file(self, json_file):
|
||||
@ -115,13 +130,25 @@ class BaseConfig:
|
||||
config_dict = yaml.safe_load(f)
|
||||
if "compute_environment" not in config_dict:
|
||||
config_dict["compute_environment"] = ComputeEnvironment.LOCAL_MACHINE
|
||||
|
||||
if "mixed_precision" not in config_dict:
|
||||
config_dict["mixed_precision"] = "fp16" if ("fp16" in config_dict and config_dict["fp16"]) else None
|
||||
if isinstance(config_dict["mixed_precision"], bool) and not config_dict["mixed_precision"]:
|
||||
config_dict["mixed_precision"] = "no"
|
||||
if "fp16" in config_dict: # Convert the config to the new format.
|
||||
del config_dict["fp16"]
|
||||
if "dynamo_backend" in config_dict: # Convert the config to the new format.
|
||||
dynamo_backend = config_dict.pop("dynamo_backend")
|
||||
config_dict["dynamo_config"] = {} if dynamo_backend == "NO" else {"dynamo_backend": dynamo_backend}
|
||||
if "use_cpu" not in config_dict:
|
||||
config_dict["use_cpu"] = False
|
||||
if "debug" not in config_dict:
|
||||
config_dict["debug"] = False
|
||||
extra_keys = sorted(set(config_dict.keys()) - set(cls.__dataclass_fields__.keys()))
|
||||
if len(extra_keys) > 0:
|
||||
raise ValueError(
|
||||
f"The config file at {yaml_file} had unknown keys ({extra_keys}), please try upgrading your `accelerate`"
|
||||
" version or fix (and potentially remove) these keys from your config file."
|
||||
)
|
||||
return cls(**config_dict)
|
||||
|
||||
def to_yaml_file(self, yaml_file):
|
||||
@ -158,6 +185,8 @@ class ClusterConfig(BaseConfig):
|
||||
fsdp_config: dict = None
|
||||
# args for megatron_lm
|
||||
megatron_lm_config: dict = None
|
||||
# args for ipex
|
||||
ipex_config: dict = None
|
||||
# args for TPU
|
||||
downcast_bf16: bool = False
|
||||
|
||||
@ -181,6 +210,8 @@ class ClusterConfig(BaseConfig):
|
||||
self.fsdp_config = {}
|
||||
if self.megatron_lm_config is None:
|
||||
self.megatron_lm_config = {}
|
||||
if self.ipex_config is None:
|
||||
self.ipex_config = {}
|
||||
return super().__post_init__()
|
||||
|
||||
|
||||
|
||||
@ -66,7 +66,7 @@ def _convert_compute_environment(value):
|
||||
|
||||
def _convert_distributed_mode(value):
|
||||
value = int(value)
|
||||
return DistributedType(["NO", "MULTI_CPU", "MULTI_GPU", "TPU"][value])
|
||||
return DistributedType(["NO", "MULTI_CPU", "MULTI_XPU", "MULTI_GPU", "MULTI_NPU", "TPU"][value])
|
||||
|
||||
|
||||
def _convert_dynamo_backend(value):
|
||||
|
||||
@ -18,6 +18,7 @@ from pathlib import Path
|
||||
|
||||
import torch
|
||||
|
||||
from ...utils import is_npu_available, is_xpu_available
|
||||
from .config_args import ClusterConfig, default_json_config_file
|
||||
from .config_utils import SubcommandHelpFormatter
|
||||
|
||||
@ -25,7 +26,7 @@ from .config_utils import SubcommandHelpFormatter
|
||||
description = "Create a default config file for Accelerate with only a few flags set."
|
||||
|
||||
|
||||
def write_basic_config(mixed_precision="no", save_location: str = default_json_config_file, dynamo_backend="no"):
|
||||
def write_basic_config(mixed_precision="no", save_location: str = default_json_config_file, use_xpu: bool = False):
|
||||
"""
|
||||
Creates and saves a basic cluster config to be used on a local machine with potentially multiple GPUs. Will also
|
||||
set CPU if it is a CPU-only machine.
|
||||
@ -37,6 +38,8 @@ def write_basic_config(mixed_precision="no", save_location: str = default_json_c
|
||||
Optional custom save location. Should be passed to `--config_file` when using `accelerate launch`. Default
|
||||
location is inside the huggingface cache folder (`~/.cache/huggingface`) but can be overriden by setting
|
||||
the `HF_HOME` environmental variable, followed by `accelerate/default_config.yaml`.
|
||||
use_xpu (`bool`, *optional*, defaults to `False`):
|
||||
Whether to use XPU if available.
|
||||
"""
|
||||
path = Path(save_location)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
@ -46,8 +49,10 @@ def write_basic_config(mixed_precision="no", save_location: str = default_json_c
|
||||
)
|
||||
return False
|
||||
mixed_precision = mixed_precision.lower()
|
||||
if mixed_precision not in ["no", "fp16", "bf16"]:
|
||||
raise ValueError(f"`mixed_precision` should be one of 'no', 'fp16', or 'bf16'. Received {mixed_precision}")
|
||||
if mixed_precision not in ["no", "fp16", "bf16", "fp8"]:
|
||||
raise ValueError(
|
||||
f"`mixed_precision` should be one of 'no', 'fp16', 'bf16', or 'fp8'. Received {mixed_precision}"
|
||||
)
|
||||
config = {
|
||||
"compute_environment": "LOCAL_MACHINE",
|
||||
"mixed_precision": mixed_precision,
|
||||
@ -60,11 +65,28 @@ def write_basic_config(mixed_precision="no", save_location: str = default_json_c
|
||||
config["distributed_type"] = "MULTI_GPU"
|
||||
else:
|
||||
config["distributed_type"] = "NO"
|
||||
elif is_xpu_available() and use_xpu:
|
||||
num_xpus = torch.xpu.device_count()
|
||||
config["num_processes"] = num_xpus
|
||||
config["use_cpu"] = False
|
||||
if num_xpus > 1:
|
||||
config["distributed_type"] = "MULTI_XPU"
|
||||
else:
|
||||
config["distributed_type"] = "NO"
|
||||
elif is_npu_available():
|
||||
num_npus = torch.npu.device_count()
|
||||
config["num_processes"] = num_npus
|
||||
config["use_cpu"] = False
|
||||
if num_npus > 1:
|
||||
config["distributed_type"] = "MULTI_NPU"
|
||||
else:
|
||||
config["distributed_type"] = "NO"
|
||||
else:
|
||||
num_gpus = 0
|
||||
num_xpus = 0
|
||||
config["use_cpu"] = True
|
||||
config["num_processes"] = 1
|
||||
config["distributed_type"] = "NO"
|
||||
config["debug"] = False
|
||||
config = ClusterConfig(**config)
|
||||
config.to_json_file(path)
|
||||
return path
|
||||
|
||||
@ -221,6 +221,15 @@ def get_sagemaker_input():
|
||||
ec2_instance_query += "? [ml.p3.2xlarge]:"
|
||||
ec2_instance_type = _ask_field(ec2_instance_query, lambda x: str(x).lower(), default="ml.p3.2xlarge")
|
||||
|
||||
debug = False
|
||||
if distributed_type != SageMakerDistributedType.NO:
|
||||
debug = _ask_field(
|
||||
"Should distributed operations be checked while running for errors? This can avoid timeout issues but will be slower. [yes/NO]: ",
|
||||
_convert_yes_no_to_bool,
|
||||
default=False,
|
||||
error_message="Please enter yes or no.",
|
||||
)
|
||||
|
||||
num_machines = 1
|
||||
if distributed_type in (SageMakerDistributedType.DATA_PARALLEL, SageMakerDistributedType.MODEL_PARALLEL):
|
||||
num_machines = _ask_field(
|
||||
@ -231,7 +240,7 @@ def get_sagemaker_input():
|
||||
|
||||
mixed_precision = _ask_options(
|
||||
"Do you wish to use FP16 or BF16 (mixed precision)?",
|
||||
["no", "fp16", "bf16"],
|
||||
["no", "fp16", "bf16", "fp8"],
|
||||
_convert_mixed_precision,
|
||||
)
|
||||
|
||||
@ -254,4 +263,5 @@ def get_sagemaker_input():
|
||||
num_machines=num_machines,
|
||||
sagemaker_inputs_file=sagemaker_inputs_file,
|
||||
sagemaker_metrics_file=sagemaker_metrics_file,
|
||||
debug=debug,
|
||||
)
|
||||
|
||||
@ -19,11 +19,14 @@ import os
|
||||
import platform
|
||||
|
||||
import numpy as np
|
||||
import psutil
|
||||
import torch
|
||||
|
||||
from accelerate import __version__ as version
|
||||
from accelerate.commands.config import default_config_file, load_config_from_file
|
||||
|
||||
from ..utils import is_npu_available, is_xpu_available
|
||||
|
||||
|
||||
def env_command_parser(subparsers=None):
|
||||
if subparsers is not None:
|
||||
@ -43,6 +46,8 @@ def env_command_parser(subparsers=None):
|
||||
def env_command(args):
|
||||
pt_version = torch.__version__
|
||||
pt_cuda_available = torch.cuda.is_available()
|
||||
pt_xpu_available = is_xpu_available()
|
||||
pt_npu_available = is_npu_available()
|
||||
|
||||
accelerate_config = "Not found"
|
||||
# Get the default from the config file.
|
||||
@ -55,7 +60,12 @@ def env_command(args):
|
||||
"Python version": platform.python_version(),
|
||||
"Numpy version": np.__version__,
|
||||
"PyTorch version (GPU?)": f"{pt_version} ({pt_cuda_available})",
|
||||
"PyTorch XPU available": str(pt_xpu_available),
|
||||
"PyTorch NPU available": str(pt_npu_available),
|
||||
"System RAM": f"{psutil.virtual_memory().total / 1024 ** 3:.2f} GB",
|
||||
}
|
||||
if pt_cuda_available:
|
||||
info["GPU type"] = torch.cuda.get_device_name()
|
||||
|
||||
print("\nCopy-and-paste the text below in your GitHub issue\n")
|
||||
print("\n".join([f"- {prop}: {val}" for prop, val in info.items()]))
|
||||
|
||||
270
src/accelerate/commands/estimate.py
Normal file
270
src/accelerate/commands/estimate.py
Normal file
@ -0,0 +1,270 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# Copyright 2023 The HuggingFace Team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
import argparse
|
||||
|
||||
from huggingface_hub import model_info
|
||||
from huggingface_hub.utils import GatedRepoError, RepositoryNotFoundError
|
||||
|
||||
from accelerate import init_empty_weights
|
||||
from accelerate.utils import (
|
||||
calculate_maximum_sizes,
|
||||
convert_bytes,
|
||||
is_timm_available,
|
||||
is_transformers_available,
|
||||
)
|
||||
|
||||
|
||||
if is_transformers_available():
|
||||
import transformers
|
||||
from transformers import AutoConfig, AutoModel
|
||||
|
||||
if is_timm_available():
|
||||
import timm
|
||||
|
||||
|
||||
def verify_on_hub(repo: str, token: str = None):
|
||||
"Verifies that the model is on the hub and returns the model info."
|
||||
try:
|
||||
return model_info(repo, token=token)
|
||||
except GatedRepoError:
|
||||
return "gated"
|
||||
except RepositoryNotFoundError:
|
||||
return "repo"
|
||||
|
||||
|
||||
def check_has_model(error):
|
||||
"""
|
||||
Checks what library spawned `error` when a model is not found
|
||||
"""
|
||||
if is_timm_available() and isinstance(error, RuntimeError) and "Unknown model" in error.args[0]:
|
||||
return "timm"
|
||||
elif (
|
||||
is_transformers_available()
|
||||
and isinstance(error, OSError)
|
||||
and "does not appear to have a file named" in error.args[0]
|
||||
):
|
||||
return "transformers"
|
||||
else:
|
||||
return "unknown"
|
||||
|
||||
|
||||
def create_empty_model(model_name: str, library_name: str, trust_remote_code: bool = False, access_token: str = None):
|
||||
"""
|
||||
Creates an empty model from its parent library on the `Hub` to calculate the overall memory consumption.
|
||||
|
||||
Args:
|
||||
model_name (`str`):
|
||||
The model name on the Hub
|
||||
library_name (`str`):
|
||||
The library the model has an integration with, such as `transformers`. Will be used if `model_name` has no
|
||||
metadata on the Hub to determine the library.
|
||||
trust_remote_code (`bool`, `optional`, defaults to `False`):
|
||||
Whether or not to allow for custom models defined on the Hub in their own modeling files. This option
|
||||
should only be set to `True` for repositories you trust and in which you have read the code, as it will
|
||||
execute code present on the Hub on your local machine.
|
||||
access_token (`str`, `optional`, defaults to `None`):
|
||||
The access token to use to access private or gated models on the Hub. (for use on the Gradio app)
|
||||
|
||||
Returns:
|
||||
`torch.nn.Module`: The torch model that has been initialized on the `meta` device.
|
||||
|
||||
"""
|
||||
model_info = verify_on_hub(model_name, access_token)
|
||||
# Simplified errors
|
||||
if model_info == "gated":
|
||||
raise GatedRepoError(
|
||||
f"Repo for model `{model_name}` is gated. You must be authenticated to access it. Please run `huggingface-cli login`."
|
||||
)
|
||||
elif model_info == "repo":
|
||||
raise RepositoryNotFoundError(
|
||||
f"Repo for model `{model_name}` does not exist on the Hub. If you are trying to access a private repo,"
|
||||
" make sure you are authenticated via `huggingface-cli login` and have access."
|
||||
)
|
||||
if library_name is None:
|
||||
library_name = getattr(model_info, "library_name", False)
|
||||
if not library_name:
|
||||
raise ValueError(
|
||||
f"Model `{model_name}` does not have any library metadata on the Hub, please manually pass in a `--library_name` to use (such as `transformers`)"
|
||||
)
|
||||
if library_name == "transformers":
|
||||
if not is_transformers_available():
|
||||
raise ImportError(
|
||||
f"To check `{model_name}`, `transformers` must be installed. Please install it via `pip install transformers`"
|
||||
)
|
||||
print(f"Loading pretrained config for `{model_name}` from `transformers`...")
|
||||
|
||||
auto_map = model_info.config.get("auto_map", False)
|
||||
config = AutoConfig.from_pretrained(model_name, trust_remote_code=trust_remote_code)
|
||||
|
||||
with init_empty_weights():
|
||||
# remote code could specify a specific `AutoModel` class in the `auto_map`
|
||||
constructor = AutoModel
|
||||
if isinstance(auto_map, dict):
|
||||
value = None
|
||||
for key in auto_map.keys():
|
||||
if key.startswith("AutoModelFor"):
|
||||
value = key
|
||||
break
|
||||
if value is not None:
|
||||
constructor = getattr(transformers, value)
|
||||
model = constructor.from_config(config, trust_remote_code=trust_remote_code)
|
||||
elif library_name == "timm":
|
||||
if not is_timm_available():
|
||||
raise ImportError(
|
||||
f"To check `{model_name}`, `timm` must be installed. Please install it via `pip install timm`"
|
||||
)
|
||||
print(f"Loading pretrained config for `{model_name}` from `timm`...")
|
||||
with init_empty_weights():
|
||||
model = timm.create_model(model_name, pretrained=False)
|
||||
else:
|
||||
raise ValueError(
|
||||
f"Library `{library_name}` is not supported yet, please open an issue on GitHub for us to add support."
|
||||
)
|
||||
return model
|
||||
|
||||
|
||||
def create_ascii_table(headers: list, rows: list, title: str):
|
||||
"Creates a pretty table from a list of rows, minimal version of `tabulate`."
|
||||
sep_char, in_between = "│", "─"
|
||||
column_widths = []
|
||||
for i in range(len(headers)):
|
||||
column_values = [row[i] for row in rows] + [headers[i]]
|
||||
max_column_width = max(len(value) for value in column_values)
|
||||
column_widths.append(max_column_width)
|
||||
|
||||
formats = [f"%{column_widths[i]}s" for i in range(len(rows[0]))]
|
||||
|
||||
pattern = f"{sep_char}{sep_char.join(formats)}{sep_char}"
|
||||
diff = 0
|
||||
|
||||
def make_row(left_char, middle_char, right_char):
|
||||
return f"{left_char}{middle_char.join([in_between * n for n in column_widths])}{in_between * diff}{right_char}"
|
||||
|
||||
separator = make_row("├", "┼", "┤")
|
||||
if len(title) > sum(column_widths):
|
||||
diff = abs(len(title) - len(separator))
|
||||
column_widths[-1] += diff
|
||||
|
||||
# Update with diff
|
||||
separator = make_row("├", "┼", "┤")
|
||||
initial_rows = [
|
||||
make_row("┌", in_between, "┐"),
|
||||
f"{sep_char}{title.center(len(separator) - 2)}{sep_char}",
|
||||
make_row("├", "┬", "┤"),
|
||||
]
|
||||
table = "\n".join(initial_rows) + "\n"
|
||||
column_widths[-1] += diff
|
||||
centered_line = [text.center(column_widths[i]) for i, text in enumerate(headers)]
|
||||
table += f"{pattern % tuple(centered_line)}\n{separator}\n"
|
||||
for i, line in enumerate(rows):
|
||||
centered_line = [t.center(column_widths[i]) for i, t in enumerate(line)]
|
||||
table += f"{pattern % tuple(centered_line)}\n"
|
||||
table += f'└{"┴".join([in_between * n for n in column_widths])}┘'
|
||||
|
||||
return table
|
||||
|
||||
|
||||
def estimate_command_parser(subparsers=None):
|
||||
if subparsers is not None:
|
||||
parser = subparsers.add_parser("estimate-memory")
|
||||
else:
|
||||
parser = argparse.ArgumentParser(description="Model size estimator for fitting a model onto CUDA memory.")
|
||||
|
||||
parser.add_argument("model_name", type=str, help="The model name on the Hugging Face Hub.")
|
||||
parser.add_argument(
|
||||
"--library_name",
|
||||
type=str,
|
||||
help="The library the model has an integration with, such as `transformers`, needed only if this information is not stored on the Hub.",
|
||||
choices=["timm", "transformers"],
|
||||
)
|
||||
parser.add_argument(
|
||||
"--dtypes",
|
||||
type=str,
|
||||
nargs="+",
|
||||
default=["float32", "float16", "int8", "int4"],
|
||||
help="The dtypes to use for the model, must be one (or many) of `float32`, `float16`, `int8`, and `int4`",
|
||||
choices=["float32", "float16", "int8", "int4"],
|
||||
)
|
||||
parser.add_argument(
|
||||
"--trust_remote_code",
|
||||
action="store_true",
|
||||
help="""Whether or not to allow for custom models defined on the Hub in their own modeling files. This flag
|
||||
should only be used for repositories you trust and in which you have read the code, as it will execute
|
||||
code present on the Hub on your local machine.""",
|
||||
)
|
||||
|
||||
if subparsers is not None:
|
||||
parser.set_defaults(func=estimate_command)
|
||||
return parser
|
||||
|
||||
|
||||
def gather_data(args):
|
||||
"Creates an empty model and gathers the data for the sizes"
|
||||
try:
|
||||
model = create_empty_model(
|
||||
args.model_name, library_name=args.library_name, trust_remote_code=args.trust_remote_code
|
||||
)
|
||||
except (RuntimeError, OSError) as e:
|
||||
library = check_has_model(e)
|
||||
if library != "unknown":
|
||||
raise RuntimeError(
|
||||
f"Tried to load `{args.model_name}` with `{library}` but a possible model to load was not found inside the repo."
|
||||
)
|
||||
raise e
|
||||
|
||||
total_size, largest_layer = calculate_maximum_sizes(model)
|
||||
|
||||
data = []
|
||||
|
||||
for dtype in args.dtypes:
|
||||
dtype_total_size = total_size
|
||||
dtype_largest_layer = largest_layer[0]
|
||||
if dtype == "float16":
|
||||
dtype_total_size /= 2
|
||||
dtype_largest_layer /= 2
|
||||
elif dtype == "int8":
|
||||
dtype_total_size /= 4
|
||||
dtype_largest_layer /= 4
|
||||
elif dtype == "int4":
|
||||
dtype_total_size /= 8
|
||||
dtype_largest_layer /= 8
|
||||
dtype_training_size = dtype_total_size * 4
|
||||
data.append([dtype, dtype_largest_layer, dtype_total_size, dtype_training_size])
|
||||
return data
|
||||
|
||||
|
||||
def estimate_command(args):
|
||||
data = gather_data(args)
|
||||
for row in data:
|
||||
for i, item in enumerate(row):
|
||||
if isinstance(item, (int, float)):
|
||||
row[i] = convert_bytes(item)
|
||||
|
||||
headers = ["dtype", "Largest Layer", "Total Size", "Training using Adam"]
|
||||
|
||||
title = f"Memory Usage for loading `{args.model_name}`"
|
||||
table = create_ascii_table(headers, data, title)
|
||||
print(table)
|
||||
|
||||
|
||||
def main():
|
||||
parser = estimate_command_parser()
|
||||
args = parser.parse_args()
|
||||
estimate_command(args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@ -34,10 +34,14 @@ from accelerate.utils import (
|
||||
DistributedType,
|
||||
PrepareForLaunch,
|
||||
_filter_args,
|
||||
is_bf16_available,
|
||||
is_deepspeed_available,
|
||||
is_npu_available,
|
||||
is_rich_available,
|
||||
is_sagemaker_available,
|
||||
is_torch_version,
|
||||
is_tpu_available,
|
||||
is_xpu_available,
|
||||
patch_environment,
|
||||
prepare_deepspeed_cmd_env,
|
||||
prepare_multi_gpu_env,
|
||||
@ -61,7 +65,6 @@ logger = logging.getLogger(__name__)
|
||||
options_to_group = {
|
||||
"--multi-gpu": "Distributed GPUs",
|
||||
"--tpu": "TPU",
|
||||
"--mps": "MPS",
|
||||
"--use_deepspeed": "DeepSpeed Arguments",
|
||||
"--use_fsdp": "FSDP Arguments",
|
||||
"--use_megatron_lm": "Megatron-LM Arguments",
|
||||
@ -124,9 +127,9 @@ class _CustomHelpAction(argparse._HelpAction):
|
||||
|
||||
def launch_command_parser(subparsers=None):
|
||||
if subparsers is not None:
|
||||
parser = subparsers.add_parser("launch", add_help=False)
|
||||
parser = subparsers.add_parser("launch", add_help=False, allow_abbrev=False)
|
||||
else:
|
||||
parser = argparse.ArgumentParser("Accelerate launch command", add_help=False)
|
||||
parser = argparse.ArgumentParser("Accelerate launch command", add_help=False, allow_abbrev=False)
|
||||
|
||||
parser.register("action", "help", _CustomHelpAction)
|
||||
parser.add_argument("-h", "--help", action="help", help="Show this help message and exit.")
|
||||
@ -147,13 +150,6 @@ def launch_command_parser(subparsers=None):
|
||||
hardware_args.add_argument(
|
||||
"--cpu", default=False, action="store_true", help="Whether or not to force the training on the CPU."
|
||||
)
|
||||
hardware_args.add_argument(
|
||||
"--mps",
|
||||
default=False,
|
||||
action="store_true",
|
||||
help="This argument is deprecated. MPS device will be enabled by default when available and can be disabled via `--cpu`."
|
||||
" Whether or not this should use MPS-enabled GPU device on MacOS machines.",
|
||||
)
|
||||
hardware_args.add_argument(
|
||||
"--multi_gpu",
|
||||
default=False,
|
||||
@ -163,6 +159,12 @@ def launch_command_parser(subparsers=None):
|
||||
hardware_args.add_argument(
|
||||
"--tpu", default=False, action="store_true", help="Whether or not this should launch a TPU training."
|
||||
)
|
||||
hardware_args.add_argument(
|
||||
"--ipex",
|
||||
default=False,
|
||||
action="store_true",
|
||||
help="Whether or not this should launch a Intel PyTorch Extension (IPEX) training.",
|
||||
)
|
||||
|
||||
# Resource selection arguments
|
||||
resource_args = parser.add_argument_group(
|
||||
@ -171,7 +173,7 @@ def launch_command_parser(subparsers=None):
|
||||
resource_args.add_argument(
|
||||
"--mixed_precision",
|
||||
type=str,
|
||||
choices=["no", "fp16", "bf16"],
|
||||
choices=["no", "fp16", "bf16", "fp8"],
|
||||
help="Whether or not to use mixed precision training. "
|
||||
"Choose between FP16 and BF16 (bfloat16) training. "
|
||||
"BF16 training is only supported on Nvidia Ampere GPUs and PyTorch 1.10 or later.",
|
||||
@ -239,6 +241,12 @@ def launch_command_parser(subparsers=None):
|
||||
action="store_true",
|
||||
help="Whether to use Megatron-LM.",
|
||||
)
|
||||
paradigm_args.add_argument(
|
||||
"--use_xpu",
|
||||
default=False,
|
||||
action="store_true",
|
||||
help="Whether to use IPEX plugin to speed up training on XPU specifically.",
|
||||
)
|
||||
|
||||
# distributed GPU training arguments
|
||||
distributed_args = parser.add_argument_group("Distributed GPUs", "Arguments related to distributed GPU training.")
|
||||
@ -279,6 +287,12 @@ def launch_command_parser(subparsers=None):
|
||||
help="User-defined role for the workers.",
|
||||
)
|
||||
# Rendezvous related arguments
|
||||
distributed_args.add_argument(
|
||||
"--rdzv_backend",
|
||||
type=str,
|
||||
default="static",
|
||||
help="The rendezvous method to use, such as 'static' (the default) or 'c10d'",
|
||||
)
|
||||
distributed_args.add_argument(
|
||||
"--rdzv_conf",
|
||||
type=str,
|
||||
@ -314,12 +328,13 @@ def launch_command_parser(subparsers=None):
|
||||
tpu_args.add_argument(
|
||||
"--tpu_cluster",
|
||||
action="store_true",
|
||||
dest="tpu_use_cluster",
|
||||
help="Whether to use a GCP TPU pod for training.",
|
||||
)
|
||||
tpu_args.add_argument(
|
||||
"--no_tpu_cluster",
|
||||
action="store_false",
|
||||
dest="tpu_cluster",
|
||||
dest="tpu_use_cluster",
|
||||
help="Should not be passed explicitly, this is for internal use only.",
|
||||
)
|
||||
tpu_args.add_argument(
|
||||
@ -383,6 +398,20 @@ def launch_command_parser(subparsers=None):
|
||||
help="Decides where (none|cpu|nvme) to offload parameters (useful only when `use_deepspeed` flag is passed). "
|
||||
"If unspecified, will default to 'none'.",
|
||||
)
|
||||
deepspeed_args.add_argument(
|
||||
"--offload_optimizer_nvme_path",
|
||||
default=None,
|
||||
type=str,
|
||||
help="Decides Nvme Path to offload optimizer states (useful only when `use_deepspeed` flag is passed). "
|
||||
"If unspecified, will default to 'none'.",
|
||||
)
|
||||
deepspeed_args.add_argument(
|
||||
"--offload_param_nvme_path",
|
||||
default=None,
|
||||
type=str,
|
||||
help="Decides Nvme Path to offload parameters (useful only when `use_deepspeed` flag is passed). "
|
||||
"If unspecified, will default to 'none'.",
|
||||
)
|
||||
deepspeed_args.add_argument(
|
||||
"--gradient_accumulation_steps",
|
||||
default=None,
|
||||
@ -481,6 +510,27 @@ def launch_command_parser(subparsers=None):
|
||||
type=str,
|
||||
help="FSDP's state dict type. (useful only when `use_fsdp` flag is passed).",
|
||||
)
|
||||
fsdp_args.add_argument(
|
||||
"--fsdp_forward_prefetch",
|
||||
default="false",
|
||||
type=str,
|
||||
help="If True, then FSDP explicitly prefetches the next upcoming "
|
||||
"all-gather while executing in the forward pass (useful only when `use_fsdp` flag is passed).",
|
||||
)
|
||||
fsdp_args.add_argument(
|
||||
"--fsdp_use_orig_params",
|
||||
default="false",
|
||||
type=str,
|
||||
help="If True, allows non-uniform `requires_grad` during init, which means support for interspersed frozen and trainable paramteres."
|
||||
" (useful only when `use_fsdp` flag is passed).",
|
||||
)
|
||||
fsdp_args.add_argument(
|
||||
"--fsdp_sync_module_states",
|
||||
default="true",
|
||||
type=str,
|
||||
help="If True, each individually wrapped FSDP unit will broadcast module parameters from rank 0."
|
||||
" (useful only when `use_fsdp` flag is passed).",
|
||||
)
|
||||
|
||||
# megatron_lm args
|
||||
megatron_lm_args = parser.add_argument_group("Megatron-LM Arguments", "Arguments related to Megatron-LM.")
|
||||
@ -581,10 +631,7 @@ def simple_launcher(args):
|
||||
|
||||
|
||||
def multi_gpu_launcher(args):
|
||||
if is_torch_version(">=", "1.9.1"):
|
||||
import torch.distributed.run as distrib_run
|
||||
else:
|
||||
raise NotImplementedError("Native multi-GPU training requires pytorch>=1.9.1")
|
||||
import torch.distributed.run as distrib_run
|
||||
|
||||
current_env = prepare_multi_gpu_env(args)
|
||||
|
||||
@ -607,8 +654,8 @@ def multi_gpu_launcher(args):
|
||||
|
||||
|
||||
def deepspeed_launcher(args):
|
||||
if is_torch_version(">=", "1.9.1"):
|
||||
import torch.distributed.run as distrib_run
|
||||
import torch.distributed.run as distrib_run
|
||||
|
||||
if not is_deepspeed_available():
|
||||
raise ImportError("DeepSpeed is not installed => run `pip3 install deepspeed` or build it from source.")
|
||||
|
||||
@ -629,9 +676,6 @@ def deepspeed_launcher(args):
|
||||
else:
|
||||
sys.exit(1)
|
||||
else:
|
||||
if is_torch_version("<", "1.9.1"):
|
||||
raise NotImplementedError("Multi-node training requires pytorch>=1.9.1")
|
||||
|
||||
debug = getattr(args, "debug", False)
|
||||
args = _filter_args(
|
||||
args,
|
||||
@ -707,7 +751,7 @@ def tpu_pod_launcher(args):
|
||||
str(1),
|
||||
"--mixed_precision",
|
||||
"no",
|
||||
"--dynmo_backend",
|
||||
"--dynamo_backend",
|
||||
"no",
|
||||
"--num_processes",
|
||||
str(args.num_processes),
|
||||
@ -762,9 +806,9 @@ def sagemaker_launcher(sagemaker_config: SageMakerConfig, args):
|
||||
|
||||
def _validate_launch_command(args):
|
||||
# Sanity checks
|
||||
if sum([args.multi_gpu, args.cpu, args.tpu, args.mps, args.use_deepspeed, args.use_fsdp]) > 1:
|
||||
if sum([args.multi_gpu, args.cpu, args.tpu, args.use_deepspeed, args.use_fsdp]) > 1:
|
||||
raise ValueError(
|
||||
"You can only use one of `--cpu`, `--multi_gpu`, `--mps`, `--tpu`, `--use_deepspeed`, `--use_fsdp` at a time."
|
||||
"You can only use one of `--cpu`, `--multi_gpu`, `--tpu`, `--use_deepspeed`, `--use_fsdp` at a time."
|
||||
)
|
||||
if args.multi_gpu and (args.num_processes is not None) and (args.num_processes < 2):
|
||||
raise ValueError("You need to use at least 2 processes to use `--multi_gpu`.")
|
||||
@ -778,35 +822,36 @@ def _validate_launch_command(args):
|
||||
if (
|
||||
not args.multi_gpu
|
||||
and not args.tpu
|
||||
and not args.tpu_cluster
|
||||
and not args.mps
|
||||
and not args.tpu_use_cluster
|
||||
and not args.use_deepspeed
|
||||
and not args.use_fsdp
|
||||
and not args.use_megatron_lm
|
||||
):
|
||||
args.use_deepspeed = defaults.distributed_type == DistributedType.DEEPSPEED
|
||||
args.multi_gpu = defaults.distributed_type == DistributedType.MULTI_GPU
|
||||
args.multi_gpu = (
|
||||
True
|
||||
if defaults.distributed_type
|
||||
in (DistributedType.MULTI_GPU, DistributedType.MULTI_NPU, DistributedType.MULTI_XPU)
|
||||
else False
|
||||
)
|
||||
args.tpu = defaults.distributed_type == DistributedType.TPU
|
||||
args.use_fsdp = defaults.distributed_type == DistributedType.FSDP
|
||||
args.mps = defaults.distributed_type == DistributedType.MPS
|
||||
args.use_megatron_lm = defaults.distributed_type == DistributedType.MEGATRON_LM
|
||||
args.tpu_cluster = defaults.tpu_cluster if args.tpu else False
|
||||
if not args.mps:
|
||||
if args.gpu_ids is None:
|
||||
if defaults.gpu_ids is not None:
|
||||
args.gpu_ids = defaults.gpu_ids
|
||||
else:
|
||||
args.gpu_ids = "all"
|
||||
if (
|
||||
len(args.gpu_ids.split(",")) < 2
|
||||
and (args.gpu_ids != "all")
|
||||
and args.multi_gpu
|
||||
and args.num_machines <= 1
|
||||
):
|
||||
raise ValueError(
|
||||
"Less than two GPU ids were configured and tried to run on on multiple GPUs. "
|
||||
"Please ensure at least two are specified for `--gpu_ids`, or use `--gpu_ids='all'`."
|
||||
)
|
||||
args.tpu_use_cluster = defaults.tpu_use_cluster if args.tpu else False
|
||||
if args.gpu_ids is None:
|
||||
if defaults.gpu_ids is not None:
|
||||
args.gpu_ids = defaults.gpu_ids
|
||||
else:
|
||||
args.gpu_ids = "all"
|
||||
|
||||
if args.multi_gpu and args.num_machines is None:
|
||||
args.num_machines = defaults.num_machines
|
||||
|
||||
if len(args.gpu_ids.split(",")) < 2 and (args.gpu_ids != "all") and args.multi_gpu and args.num_machines <= 1:
|
||||
raise ValueError(
|
||||
"Less than two GPU ids were configured and tried to run on on multiple GPUs. "
|
||||
"Please ensure at least two are specified for `--gpu_ids`, or use `--gpu_ids='all'`."
|
||||
)
|
||||
if defaults.compute_environment == ComputeEnvironment.LOCAL_MACHINE:
|
||||
# Update args with the defaults
|
||||
for name, attr in defaults.__dict__.items():
|
||||
@ -822,6 +867,8 @@ def _validate_launch_command(args):
|
||||
setattr(args, k, defaults.megatron_lm_config[k])
|
||||
for k in defaults.dynamo_config:
|
||||
setattr(args, k, defaults.dynamo_config[k])
|
||||
for k in defaults.ipex_config:
|
||||
setattr(args, k, defaults.ipex_config[k])
|
||||
continue
|
||||
|
||||
# Those args are handled separately
|
||||
@ -830,26 +877,49 @@ def _validate_launch_command(args):
|
||||
and getattr(args, name, None) is None
|
||||
):
|
||||
setattr(args, name, attr)
|
||||
if not args.debug:
|
||||
args.debug = defaults.debug
|
||||
|
||||
if not args.mixed_precision:
|
||||
if defaults.mixed_precision is None:
|
||||
args.mixed_precision = "no"
|
||||
else:
|
||||
args.mixed_precision = defaults.mixed_precision
|
||||
mp_from_config_flag = True
|
||||
else:
|
||||
native_amp = False
|
||||
err = "{mode} mixed precision requires {requirement}"
|
||||
if args.use_cpu or (args.use_xpu and torch.xpu.is_available()):
|
||||
native_amp = is_torch_version(">=", "1.10")
|
||||
else:
|
||||
native_amp = is_bf16_available(True)
|
||||
if args.mixed_precision == "bf16" and not native_amp and not (args.tpu and is_tpu_available()):
|
||||
raise ValueError(err.format(mode="bf16", requirement="PyTorch >= 1.10 and a supported device."))
|
||||
|
||||
# Silently set the default here
|
||||
if args.dynamo_backend is None:
|
||||
warned.append("\t`--dynamo_backend` was set to a value of `'no'`")
|
||||
args.dynamo_backend = "no"
|
||||
else:
|
||||
if args.num_processes is None:
|
||||
args.num_processes = torch.cuda.device_count()
|
||||
if args.use_xpu and is_xpu_available():
|
||||
args.num_processes = torch.xpu.device_count()
|
||||
elif is_npu_available():
|
||||
args.num_processes = torch.npu.device_count()
|
||||
else:
|
||||
args.num_processes = torch.cuda.device_count()
|
||||
warned.append(f"\t`--num_processes` was set to a value of `{args.num_processes}`")
|
||||
if torch.cuda.device_count() > 1 and not args.multi_gpu:
|
||||
warned.append(
|
||||
"\t\tMore than one GPU was found, enabling multi-GPU training.\n"
|
||||
"\t\tIf this was unintended please pass in `--num_processes=1`."
|
||||
)
|
||||
args.multi_gpu = True
|
||||
if args.debug is None:
|
||||
args.debug = False
|
||||
if not args.multi_gpu and (
|
||||
(args.use_xpu and is_xpu_available() and torch.xpu.device_count() > 1)
|
||||
or (is_npu_available() and torch.npu.device_count() > 1)
|
||||
or (torch.cuda.device_count() > 1)
|
||||
):
|
||||
warned.append(
|
||||
"\t\tMore than one GPU was found, enabling multi-GPU training.\n"
|
||||
"\t\tIf this was unintended please pass in `--num_processes=1`."
|
||||
)
|
||||
args.multi_gpu = True
|
||||
if args.num_machines is None:
|
||||
warned.append("\t`--num_machines` was set to a value of `1`")
|
||||
args.num_machines = 1
|
||||
@ -861,13 +931,15 @@ def _validate_launch_command(args):
|
||||
if args.dynamo_backend is None:
|
||||
warned.append("\t`--dynamo_backend` was set to a value of `'no'`")
|
||||
args.dynamo_backend = "no"
|
||||
if args.debug:
|
||||
logger.debug("Running script in debug mode, expect distributed operations to be slightly slower.")
|
||||
|
||||
is_aws_env_disabled = defaults is None or (
|
||||
defaults is not None and defaults.compute_environment != ComputeEnvironment.AMAZON_SAGEMAKER
|
||||
)
|
||||
if is_aws_env_disabled and args.num_cpu_threads_per_process is None:
|
||||
args.num_cpu_threads_per_process = 1
|
||||
if args.use_cpu and args.num_processes > 1:
|
||||
if args.use_cpu and args.num_processes >= 1:
|
||||
local_size = get_int_from_env(
|
||||
["MPI_LOCALNRANKS", "OMPI_COMM_WORLD_LOCAL_SIZE", "MV2_COMM_WORLD_LOCAL_SIZE"], 1
|
||||
)
|
||||
@ -890,7 +962,6 @@ def _validate_launch_command(args):
|
||||
|
||||
def launch_command(args):
|
||||
args, defaults, mp_from_config_flag = _validate_launch_command(args)
|
||||
|
||||
# Use the proper launcher
|
||||
if args.use_deepspeed and not args.cpu:
|
||||
args.deepspeed_fields_from_accelerate_config = list(defaults.deepspeed_config.keys()) if defaults else []
|
||||
@ -905,7 +976,7 @@ def launch_command(args):
|
||||
elif args.multi_gpu and not args.cpu:
|
||||
multi_gpu_launcher(args)
|
||||
elif args.tpu and not args.cpu:
|
||||
if args.tpu_cluster:
|
||||
if args.tpu_use_cluster:
|
||||
tpu_pod_launcher(args)
|
||||
else:
|
||||
tpu_launcher(args)
|
||||
|
||||
@ -15,13 +15,22 @@
|
||||
"""
|
||||
Main driver for the selection menu, based on https://github.com/bchao1/bullet
|
||||
"""
|
||||
import builtins
|
||||
import sys
|
||||
|
||||
from ...utils.imports import _is_package_available
|
||||
from . import cursor, input
|
||||
from .helpers import Direction, clear_line, forceWrite, linebreak, move_cursor, reset_cursor, writeColor
|
||||
from .keymap import KEYMAP
|
||||
|
||||
|
||||
in_colab = False
|
||||
try:
|
||||
in_colab = _is_package_available("google.colab")
|
||||
except ModuleNotFoundError:
|
||||
pass
|
||||
|
||||
|
||||
@input.register
|
||||
class BulletMenu:
|
||||
"""
|
||||
@ -107,7 +116,10 @@ class BulletMenu:
|
||||
if self.prompt:
|
||||
linebreak()
|
||||
forceWrite(self.prompt, "\n")
|
||||
forceWrite("Please select a choice using the arrow or number keys, and selecting with enter", "\n")
|
||||
if in_colab:
|
||||
forceWrite("Please input a choice index (starting from 0), and press enter", "\n")
|
||||
else:
|
||||
forceWrite("Please select a choice using the arrow or number keys, and selecting with enter", "\n")
|
||||
self.position = default_choice
|
||||
for i in range(len(self.choices)):
|
||||
self.print_choice(i)
|
||||
@ -115,7 +127,13 @@ class BulletMenu:
|
||||
move_cursor(len(self.choices) - self.position, "UP")
|
||||
with cursor.hide():
|
||||
while True:
|
||||
choice = self.handle_input()
|
||||
if in_colab:
|
||||
try:
|
||||
choice = int(builtins.input())
|
||||
except ValueError:
|
||||
choice = default_choice
|
||||
else:
|
||||
choice = self.handle_input()
|
||||
if choice is not None:
|
||||
reset_cursor()
|
||||
for _ in range(len(self.choices) + 1):
|
||||
|
||||
@ -45,10 +45,12 @@ def test_command_parser(subparsers=None):
|
||||
def test_command(args):
|
||||
script_name = os.path.sep.join(__file__.split(os.path.sep)[:-2] + ["test_utils", "scripts", "test_script.py"])
|
||||
|
||||
test_args = f"""
|
||||
--config_file={args.config_file} {script_name}
|
||||
""".split()
|
||||
cmd = ["accelerate-launch"] + test_args
|
||||
if args.config_file is None:
|
||||
test_args = script_name
|
||||
else:
|
||||
test_args = f"--config_file={args.config_file} {script_name}"
|
||||
|
||||
cmd = ["accelerate-launch"] + test_args.split()
|
||||
result = execute_subprocess_async(cmd, env=os.environ.copy())
|
||||
if result.returncode == 0:
|
||||
print("Test is a success! You are ready for your distributed training!")
|
||||
|
||||
@ -14,7 +14,7 @@
|
||||
|
||||
import math
|
||||
from contextlib import suppress
|
||||
from typing import List, Optional, Union
|
||||
from typing import Callable, List, Optional, Union
|
||||
|
||||
import torch
|
||||
from torch.utils.data import BatchSampler, DataLoader, IterableDataset
|
||||
@ -36,31 +36,6 @@ from .utils import (
|
||||
)
|
||||
|
||||
|
||||
if is_tpu_available(check_device=False):
|
||||
import torch_xla.distributed.parallel_loader as xpl
|
||||
|
||||
class MpDeviceLoaderWrapper(xpl.MpDeviceLoader):
|
||||
"""
|
||||
Wrapper for the xpl.MpDeviceLoader class that knows the total batch size.
|
||||
|
||||
**Available attributes:**
|
||||
|
||||
- **total_batch_size** (`int`) -- Total batch size of the dataloader across all processes.
|
||||
Equal to the original batch size when `split_batches=True`; otherwise the original batch size * the total
|
||||
number of processes
|
||||
|
||||
- **total_dataset_length** (`int`) -- Total length of the inner dataset across all processes.
|
||||
"""
|
||||
|
||||
@property
|
||||
def total_batch_size(self):
|
||||
return self._loader.total_batch_size
|
||||
|
||||
@property
|
||||
def total_dataset_length(self):
|
||||
return self._loader.total_dataset_length
|
||||
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
# kwargs of the DataLoader in min version 1.4.0.
|
||||
@ -77,12 +52,12 @@ _PYTORCH_DATALOADER_KWARGS = {
|
||||
"worker_init_fn": None,
|
||||
"multiprocessing_context": None,
|
||||
"generator": None,
|
||||
"prefetch_factor": 2,
|
||||
"persistent_workers": False,
|
||||
}
|
||||
|
||||
# kwargs added after by version
|
||||
_PYTORCH_DATALOADER_ADDITIONAL_KWARGS = {
|
||||
"1.7.0": {"prefetch_factor": 2, "persistent_workers": False},
|
||||
}
|
||||
_PYTORCH_DATALOADER_ADDITIONAL_KWARGS = {}
|
||||
|
||||
for v, additional_kwargs in _PYTORCH_DATALOADER_ADDITIONAL_KWARGS.items():
|
||||
if is_torch_version(">=", v):
|
||||
@ -323,7 +298,42 @@ class IterableDatasetShard(IterableDataset):
|
||||
yield current_batch[i]
|
||||
|
||||
|
||||
class DataLoaderShard(DataLoader):
|
||||
class DataLoaderStateMixin:
|
||||
"""
|
||||
Mixin class that adds a state to a `DataLoader` to keep track of the status inside the dataloader such as at the
|
||||
end of the iteration, the number of items in the dataset in the last batch relative to the batch size, and other
|
||||
useful information that might be needed.
|
||||
|
||||
**Available attributes:**
|
||||
|
||||
- **end_of_dataloader** (`bool`) -- Whether at the last iteration or batch
|
||||
- **remainder** (`int`) -- The number of items that are remaining in the last batch, relative to the total
|
||||
batch size
|
||||
|
||||
"""
|
||||
|
||||
def __init_subclass__(cls, **kwargs):
|
||||
cls.end_of_dataloader = False
|
||||
cls.remainder = -1
|
||||
|
||||
def reset(self):
|
||||
self.end_of_dataloader = False
|
||||
self.remainder = -1
|
||||
|
||||
def begin(self):
|
||||
"Prepares the gradient state for the current dataloader"
|
||||
self.reset()
|
||||
with suppress(Exception):
|
||||
length = getattr(self.dataset, "total_dataset_length", len(self.dataset))
|
||||
self.remainder = length % self.total_batch_size
|
||||
self.gradient_state._add_dataloader(self)
|
||||
|
||||
def end(self):
|
||||
"Cleans up the gradient state after exiting the dataloader"
|
||||
self.gradient_state._remove_dataloader(self)
|
||||
|
||||
|
||||
class DataLoaderShard(DataLoader, DataLoaderStateMixin):
|
||||
"""
|
||||
Subclass of a PyTorch `DataLoader` that will deal with device placement and current distributed setup.
|
||||
|
||||
@ -367,11 +377,7 @@ class DataLoaderShard(DataLoader):
|
||||
def __iter__(self):
|
||||
if self.rng_types is not None:
|
||||
synchronize_rng_states(self.rng_types, self.synchronized_generator)
|
||||
self.gradient_state._add_dataloader(self)
|
||||
# We can safely pass because the default is -1
|
||||
with suppress(Exception):
|
||||
length = getattr(self.dataset, "total_dataset_length", len(self.dataset))
|
||||
self.gradient_state._set_remainder(length % self.total_batch_size)
|
||||
self.begin()
|
||||
dataloader_iter = super().__iter__()
|
||||
# We iterate one batch ahead to check when we are at the end
|
||||
try:
|
||||
@ -391,29 +397,70 @@ class DataLoaderShard(DataLoader):
|
||||
batch_index += 1
|
||||
current_batch = next_batch
|
||||
except StopIteration:
|
||||
self.gradient_state._remove_dataloader(self)
|
||||
self.end_of_dataloader = True
|
||||
if batch_index >= self.skip_batches:
|
||||
yield current_batch
|
||||
break
|
||||
self.end()
|
||||
|
||||
@property
|
||||
def total_batch_size(self):
|
||||
batch_sampler = self.sampler if isinstance(self.sampler, BatchSampler) else self.batch_sampler
|
||||
return (
|
||||
batch_sampler.batch_size
|
||||
if batch_sampler.split_batches
|
||||
else (batch_sampler.batch_size * batch_sampler.num_processes)
|
||||
if getattr(batch_sampler, "split_batches", False)
|
||||
else (batch_sampler.batch_size * getattr(batch_sampler, "num_processes", 1))
|
||||
)
|
||||
|
||||
@property
|
||||
def total_dataset_length(self):
|
||||
if hasattr("total_length", self.dataset):
|
||||
if hasattr(self.dataset, "total_length"):
|
||||
return self.dataset.total_length
|
||||
else:
|
||||
return len(self.dataset)
|
||||
|
||||
|
||||
class DataLoaderDispatcher(DataLoader):
|
||||
if is_tpu_available(check_device=False):
|
||||
import torch_xla.distributed.parallel_loader as xpl
|
||||
|
||||
class MpDeviceLoaderWrapper(xpl.MpDeviceLoader):
|
||||
"""
|
||||
Wrapper for the xpl.MpDeviceLoader class that knows the total batch size.
|
||||
|
||||
XLA preloading threads will all call DataLoaderShard's __iter__(). Remove rng_types from DataLoaderShard to
|
||||
prevent it from using the XLA device in the preloading threads, and synchronize the RNG once from the main
|
||||
thread only.
|
||||
|
||||
**Available attributes:**
|
||||
|
||||
- **total_batch_size** (`int`) -- Total batch size of the dataloader across all processes.
|
||||
Equal to the original batch size when `split_batches=True`; otherwise the original batch size * the total
|
||||
number of processes
|
||||
|
||||
- **total_dataset_length** (`int`) -- Total length of the inner dataset across all processes.
|
||||
"""
|
||||
|
||||
def __init__(self, dataloader: DataLoaderShard, device: torch.device):
|
||||
super().__init__(dataloader, device)
|
||||
self._rng_types = self._loader.rng_types
|
||||
self._loader.rng_types = None
|
||||
|
||||
def __iter__(self):
|
||||
if self._rng_types is not None:
|
||||
synchronize_rng_states(self._rng_types, self._loader.synchronized_generator)
|
||||
|
||||
return super().__iter__()
|
||||
|
||||
@property
|
||||
def total_batch_size(self):
|
||||
return self._loader.total_batch_size
|
||||
|
||||
@property
|
||||
def total_dataset_length(self):
|
||||
return self._loader.total_dataset_length
|
||||
|
||||
|
||||
class DataLoaderDispatcher(DataLoader, DataLoaderStateMixin):
|
||||
"""
|
||||
Subclass of a PyTorch `DataLoader` that will iterate and preprocess on process 0 only, then dispatch on each
|
||||
process their part of the batch.
|
||||
@ -438,7 +485,9 @@ class DataLoaderDispatcher(DataLoader):
|
||||
- **total_dataset_length** (`int`) -- Total length of the inner dataset across all processes.
|
||||
"""
|
||||
|
||||
def __init__(self, dataset, split_batches: bool = False, skip_batches=0, _drop_last: bool = False, **kwargs):
|
||||
def __init__(
|
||||
self, dataset, split_batches: bool = False, skip_batches=0, _drop_last: bool = False, slice_fn=None, **kwargs
|
||||
):
|
||||
shuffle = False
|
||||
if is_torch_version(">=", "1.11.0"):
|
||||
from torch.utils.data.datapipes.iter.combinatorics import ShufflerIterDataPipe
|
||||
@ -448,10 +497,6 @@ class DataLoaderDispatcher(DataLoader):
|
||||
shuffle = dataset._shuffle_enabled
|
||||
super().__init__(dataset, **kwargs)
|
||||
self.split_batches = split_batches
|
||||
if is_torch_version("<", "1.8.0"):
|
||||
raise ImportError(
|
||||
f"Using `DataLoaderDispatcher` requires PyTorch 1.8.0 minimum. You have {torch.__version__}."
|
||||
)
|
||||
if shuffle:
|
||||
torch.utils.data.graph_settings.apply_shuffle_settings(dataset, shuffle=shuffle)
|
||||
|
||||
@ -459,10 +504,8 @@ class DataLoaderDispatcher(DataLoader):
|
||||
self.state = AcceleratorState()
|
||||
self._drop_last = _drop_last
|
||||
self.skip_batches = skip_batches
|
||||
# We can safely pass because the default is -1
|
||||
with suppress(Exception):
|
||||
length = getattr(self.dataset, "total_dataset_length", len(self.dataset))
|
||||
self.gradient_state._set_remainder(length % self.total_batch_size)
|
||||
|
||||
self.slice_fn = slice_tensors if slice_fn is None else slice_fn
|
||||
|
||||
def _fetch_batches(self, iterator):
|
||||
batches, batch = None, None
|
||||
@ -502,10 +545,14 @@ class DataLoaderDispatcher(DataLoader):
|
||||
return batch, batch_info
|
||||
|
||||
def __iter__(self):
|
||||
self.gradient_state._add_dataloader(self)
|
||||
self.begin()
|
||||
main_iterator = None
|
||||
if self.state.process_index == 0:
|
||||
# We only iterate through the DataLoader on process 0.
|
||||
if is_torch_version(">=", "2.0.1"):
|
||||
# NOTE PyTorch DataLoader adds forward compatibilities for DataPipes, which broadcasts
|
||||
# shared seed to all dist processes. Thus, we need to create iterator for all dist processes.
|
||||
# But, we only iterate through the DataLoader on process 0.
|
||||
main_iterator = super().__iter__()
|
||||
elif self.state.process_index == 0:
|
||||
main_iterator = super().__iter__()
|
||||
stop_iteration = False
|
||||
self._stop_iteration = False
|
||||
@ -524,7 +571,17 @@ class DataLoaderDispatcher(DataLoader):
|
||||
|
||||
if not self._drop_last and first_batch is None:
|
||||
# We keep at least num processes elements of the first batch to be able to complete the last batch
|
||||
first_batch = slice_tensors(batch, slice(0, self.state.num_processes))
|
||||
first_batch = self.slice_fn(
|
||||
batch,
|
||||
slice(0, self.state.num_processes),
|
||||
process_index=self.state.process_index,
|
||||
num_processes=self.state.num_processes,
|
||||
)
|
||||
|
||||
if batch is None:
|
||||
raise ValueError(
|
||||
f"Batch does not contain any data (`{batch}`). At the end of all iterable data available before expected stop iteration."
|
||||
)
|
||||
|
||||
observed_batch_size = find_batch_size(batch)
|
||||
batch_size = observed_batch_size // self.state.num_processes
|
||||
@ -545,14 +602,20 @@ class DataLoaderDispatcher(DataLoader):
|
||||
batch_size += 1
|
||||
|
||||
data_slice = slice(self.state.process_index * batch_size, (self.state.process_index + 1) * batch_size)
|
||||
batch = slice_tensors(batch, data_slice)
|
||||
batch = self.slice_fn(
|
||||
batch,
|
||||
data_slice,
|
||||
process_index=self.state.process_index,
|
||||
num_processes=self.state.num_processes,
|
||||
)
|
||||
|
||||
if stop_iteration:
|
||||
self.gradient_state._remove_dataloader(self)
|
||||
self.gradient_state._set_remainder(observed_batch_size)
|
||||
self.end_of_dataloader = True
|
||||
self.remainder = observed_batch_size
|
||||
if batch_index >= self.skip_batches:
|
||||
yield batch
|
||||
batch_index += 1
|
||||
self.end()
|
||||
|
||||
def __len__(self):
|
||||
whole_length = super().__len__()
|
||||
@ -584,6 +647,7 @@ def prepare_data_loader(
|
||||
rng_types: Optional[List[Union[str, RNGType]]] = None,
|
||||
dispatch_batches: Optional[bool] = None,
|
||||
even_batches: bool = True,
|
||||
slice_fn_for_dispatch: Optional[Callable] = None,
|
||||
) -> DataLoader:
|
||||
"""
|
||||
Wraps a PyTorch `DataLoader` to generate batches for one of the processes only.
|
||||
@ -633,6 +697,10 @@ def prepare_data_loader(
|
||||
If set to `True`, in cases where the total batch size across all processes does not exactly divide the
|
||||
dataset, samples at the start of the dataset will be duplicated so the batch can be divided equally among
|
||||
all workers.
|
||||
slice_fn_for_dispatch (`Callable`, *optional*`):
|
||||
If passed, this function will be used to slice tensors across `num_processes`. Will default to
|
||||
[`~utils.slice_tensors`]. This argument is used only when `dispatch_batches` is set to `True` and will be
|
||||
ignored otherwise.
|
||||
|
||||
Returns:
|
||||
`torch.utils.data.dataloader.DataLoader`: A new data loader that will yield the portion of the batches
|
||||
@ -645,7 +713,7 @@ def prepare_data_loader(
|
||||
</Tip>
|
||||
"""
|
||||
if dispatch_batches is None:
|
||||
if is_torch_version("<", "1.8.0") or not put_on_device:
|
||||
if not put_on_device:
|
||||
dispatch_batches = False
|
||||
else:
|
||||
dispatch_batches = isinstance(dataloader.dataset, IterableDataset)
|
||||
@ -737,6 +805,7 @@ def prepare_data_loader(
|
||||
split_batches=split_batches,
|
||||
batch_sampler=new_batch_sampler,
|
||||
_drop_last=dataloader.drop_last,
|
||||
slice_fn=slice_fn_for_dispatch,
|
||||
**kwargs,
|
||||
)
|
||||
elif sampler_is_batch_sampler:
|
||||
|
||||
@ -18,10 +18,10 @@ from typing import Dict, List, Mapping, Optional, Union
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
|
||||
from .state import PartialState
|
||||
from .utils import (
|
||||
PrefixedDataset,
|
||||
find_device,
|
||||
is_mps_available,
|
||||
named_module_tensors,
|
||||
send_to_device,
|
||||
set_module_tensor_to_device,
|
||||
@ -155,17 +155,17 @@ def add_hook_to_module(module: nn.Module, hook: ModelHook, append: bool = False)
|
||||
module = hook.init_hook(module)
|
||||
module._hf_hook = hook
|
||||
|
||||
@functools.wraps(old_forward)
|
||||
def new_forward(*args, **kwargs):
|
||||
def new_forward(module, *args, **kwargs):
|
||||
args, kwargs = module._hf_hook.pre_forward(module, *args, **kwargs)
|
||||
if module._hf_hook.no_grad:
|
||||
with torch.no_grad():
|
||||
output = old_forward(*args, **kwargs)
|
||||
output = module._old_forward(*args, **kwargs)
|
||||
else:
|
||||
output = old_forward(*args, **kwargs)
|
||||
output = module._old_forward(*args, **kwargs)
|
||||
return module._hf_hook.post_forward(module, output)
|
||||
|
||||
module.forward = new_forward
|
||||
module.forward = functools.update_wrapper(functools.partial(new_forward, module), old_forward)
|
||||
|
||||
return module
|
||||
|
||||
|
||||
@ -225,6 +225,7 @@ class AlignDevicesHook(ModelHook):
|
||||
weights_map: Optional[Mapping] = None,
|
||||
offload_buffers: bool = False,
|
||||
place_submodules: bool = False,
|
||||
skip_keys: Optional[Union[str, List[str]]] = None,
|
||||
):
|
||||
self.execution_device = execution_device
|
||||
self.offload = offload
|
||||
@ -232,12 +233,20 @@ class AlignDevicesHook(ModelHook):
|
||||
self.weights_map = weights_map
|
||||
self.offload_buffers = offload_buffers
|
||||
self.place_submodules = place_submodules
|
||||
self.skip_keys = skip_keys
|
||||
|
||||
# Will contain the input device when `io_same_device=True`.
|
||||
self.input_device = None
|
||||
self.param_original_devices = {}
|
||||
self.buffer_original_devices = {}
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"AlignDevicesHook(execution_device={self.execution_device}, offload={self.offload}, "
|
||||
f"io_same_device={self.io_same_device}, offload_buffers={self.offload_buffers}, "
|
||||
f"place_submodules={self.place_submodules}, skip_keys={repr(self.skip_keys)})"
|
||||
)
|
||||
|
||||
def init_hook(self, module):
|
||||
if not self.offload and self.execution_device is not None:
|
||||
for name, _ in named_module_tensors(module, recurse=self.place_submodules):
|
||||
@ -270,9 +279,17 @@ class AlignDevicesHook(ModelHook):
|
||||
for name, _ in named_module_tensors(
|
||||
module, include_buffers=self.offload_buffers, recurse=self.place_submodules
|
||||
):
|
||||
set_module_tensor_to_device(module, name, self.execution_device, value=self.weights_map[name])
|
||||
fp16_statistics = None
|
||||
if "weight" in name and name.replace("weight", "SCB") in self.weights_map.keys():
|
||||
if self.weights_map[name].dtype == torch.int8:
|
||||
fp16_statistics = self.weights_map[name.replace("weight", "SCB")]
|
||||
set_module_tensor_to_device(
|
||||
module, name, self.execution_device, value=self.weights_map[name], fp16_statistics=fp16_statistics
|
||||
)
|
||||
|
||||
return send_to_device(args, self.execution_device), send_to_device(kwargs, self.execution_device)
|
||||
return send_to_device(args, self.execution_device), send_to_device(
|
||||
kwargs, self.execution_device, skip_keys=self.skip_keys
|
||||
)
|
||||
|
||||
def post_forward(self, module, output):
|
||||
if self.offload:
|
||||
@ -280,9 +297,12 @@ class AlignDevicesHook(ModelHook):
|
||||
module, include_buffers=self.offload_buffers, recurse=self.place_submodules
|
||||
):
|
||||
set_module_tensor_to_device(module, name, "meta")
|
||||
if type(module).__name__ == "Linear8bitLt":
|
||||
module.state.SCB = None
|
||||
module.state.CxB = None
|
||||
|
||||
if self.io_same_device and self.input_device is not None:
|
||||
output = send_to_device(output, self.input_device)
|
||||
output = send_to_device(output, self.input_device, skip_keys=self.skip_keys)
|
||||
|
||||
return output
|
||||
|
||||
@ -291,11 +311,13 @@ class AlignDevicesHook(ModelHook):
|
||||
for name, device in self.original_devices.items():
|
||||
if device != torch.device("meta"):
|
||||
set_module_tensor_to_device(module, name, device, value=self.weights_map.get(name, None))
|
||||
return module
|
||||
|
||||
|
||||
def attach_execution_device_hook(
|
||||
module: torch.nn.Module,
|
||||
execution_device: Union[int, str, torch.device],
|
||||
skip_keys: Optional[Union[str, List[str]]] = None,
|
||||
preload_module_classes: Optional[List[str]] = None,
|
||||
):
|
||||
"""
|
||||
@ -307,6 +329,8 @@ def attach_execution_device_hook(
|
||||
The module where we want to attach the hooks.
|
||||
execution_device (`int`, `str` or `torch.device`):
|
||||
The device on which inputs and model weights should be placed before the forward pass.
|
||||
skip_keys (`str` or `List[str]`, *optional*):
|
||||
A list of keys to ignore when moving inputs or outputs between devices.
|
||||
preload_module_classes (`List[str]`, *optional*):
|
||||
A list of classes whose instances should load all their weights (even in the submodules) at the beginning
|
||||
of the forward. This should only be used for classes that have submodules which are registered but not
|
||||
@ -314,7 +338,7 @@ def attach_execution_device_hook(
|
||||
`dense.weight` and `dense.bias` are used in some operations instead of calling `dense` directly.
|
||||
"""
|
||||
if not hasattr(module, "_hf_hook") and len(module.state_dict()) > 0:
|
||||
add_hook_to_module(module, AlignDevicesHook(execution_device))
|
||||
add_hook_to_module(module, AlignDevicesHook(execution_device, skip_keys=skip_keys))
|
||||
|
||||
# Break the recursion if we get to a preload module.
|
||||
if preload_module_classes is not None and module.__class__.__name__ in preload_module_classes:
|
||||
@ -331,6 +355,7 @@ def attach_align_device_hook(
|
||||
weights_map: Optional[Mapping] = None,
|
||||
offload_buffers: bool = False,
|
||||
module_name: str = "",
|
||||
skip_keys: Optional[Union[str, List[str]]] = None,
|
||||
preload_module_classes: Optional[List[str]] = None,
|
||||
):
|
||||
"""
|
||||
@ -350,6 +375,8 @@ def attach_align_device_hook(
|
||||
Whether or not to include the associated module's buffers when offloading.
|
||||
module_name (`str`, *optional*, defaults to `""`):
|
||||
The name of the module.
|
||||
skip_keys (`str` or `List[str]`, *optional*):
|
||||
A list of keys to ignore when moving inputs or outputs between devices.
|
||||
preload_module_classes (`List[str]`, *optional*):
|
||||
A list of classes whose instances should load all their weights (even in the submodules) at the beginning
|
||||
of the forward. This should only be used for classes that have submodules which are registered but not
|
||||
@ -374,6 +401,7 @@ def attach_align_device_hook(
|
||||
weights_map=prefixed_weights_map,
|
||||
offload_buffers=offload_buffers,
|
||||
place_submodules=full_offload,
|
||||
skip_keys=skip_keys,
|
||||
)
|
||||
add_hook_to_module(module, hook, append=True)
|
||||
|
||||
@ -392,6 +420,7 @@ def attach_align_device_hook(
|
||||
offload_buffers=offload_buffers,
|
||||
module_name=child_name,
|
||||
preload_module_classes=preload_module_classes,
|
||||
skip_keys=skip_keys,
|
||||
)
|
||||
|
||||
|
||||
@ -414,6 +443,7 @@ def attach_align_device_hook_on_blocks(
|
||||
weights_map: Mapping = None,
|
||||
offload_buffers: bool = False,
|
||||
module_name: str = "",
|
||||
skip_keys: Optional[Union[str, List[str]]] = None,
|
||||
preload_module_classes: Optional[List[str]] = None,
|
||||
):
|
||||
"""
|
||||
@ -434,6 +464,8 @@ def attach_align_device_hook_on_blocks(
|
||||
Whether or not to include the associated module's buffers when offloading.
|
||||
module_name (`str`, *optional*, defaults to `""`):
|
||||
The name of the module.
|
||||
skip_keys (`str` or `List[str]`, *optional*):
|
||||
A list of keys to ignore when moving inputs or outputs between devices.
|
||||
preload_module_classes (`List[str]`, *optional*):
|
||||
A list of classes whose instances should load all their weights (even in the submodules) at the beginning
|
||||
of the forward. This should only be used for classes that have submodules which are registered but not
|
||||
@ -443,7 +475,9 @@ def attach_align_device_hook_on_blocks(
|
||||
# If one device and one offload, we've got one hook.
|
||||
if not isinstance(execution_device, Mapping) and not isinstance(offload, dict):
|
||||
if not offload:
|
||||
hook = AlignDevicesHook(execution_device=execution_device, io_same_device=True, place_submodules=True)
|
||||
hook = AlignDevicesHook(
|
||||
execution_device=execution_device, io_same_device=True, skip_keys=skip_keys, place_submodules=True
|
||||
)
|
||||
add_hook_to_module(module, hook)
|
||||
else:
|
||||
attach_align_device_hook(
|
||||
@ -453,6 +487,7 @@ def attach_align_device_hook_on_blocks(
|
||||
weights_map=weights_map,
|
||||
offload_buffers=offload_buffers,
|
||||
module_name=module_name,
|
||||
skip_keys=skip_keys,
|
||||
)
|
||||
return
|
||||
|
||||
@ -467,6 +502,7 @@ def attach_align_device_hook_on_blocks(
|
||||
offload_buffers=offload_buffers,
|
||||
io_same_device=(module_name == ""),
|
||||
place_submodules=True,
|
||||
skip_keys=skip_keys,
|
||||
)
|
||||
add_hook_to_module(module, hook)
|
||||
attach_execution_device_hook(module, execution_device[module_name])
|
||||
@ -478,16 +514,22 @@ def attach_align_device_hook_on_blocks(
|
||||
weights_map=weights_map,
|
||||
offload_buffers=offload_buffers,
|
||||
module_name=module_name,
|
||||
skip_keys=skip_keys,
|
||||
preload_module_classes=preload_module_classes,
|
||||
)
|
||||
if not hasattr(module, "_hf_hook"):
|
||||
hook = AlignDevicesHook(execution_device=execution_device[module_name], io_same_device=(module_name == ""))
|
||||
hook = AlignDevicesHook(
|
||||
execution_device=execution_device[module_name], io_same_device=(module_name == ""), skip_keys=skip_keys
|
||||
)
|
||||
add_hook_to_module(module, hook)
|
||||
attach_execution_device_hook(
|
||||
module, execution_device[module_name], preload_module_classes=preload_module_classes
|
||||
module,
|
||||
execution_device[module_name],
|
||||
preload_module_classes=preload_module_classes,
|
||||
skip_keys=skip_keys,
|
||||
)
|
||||
elif module_name == "":
|
||||
hook = AlignDevicesHook(execution_device=execution_device.get(""), io_same_device=True)
|
||||
hook = AlignDevicesHook(execution_device=execution_device.get(""), io_same_device=True, skip_keys=skip_keys)
|
||||
add_hook_to_module(module, hook)
|
||||
|
||||
for child_name, child in module.named_children():
|
||||
@ -500,6 +542,7 @@ def attach_align_device_hook_on_blocks(
|
||||
offload_buffers=offload_buffers,
|
||||
module_name=child_name,
|
||||
preload_module_classes=preload_module_classes,
|
||||
skip_keys=skip_keys,
|
||||
)
|
||||
|
||||
|
||||
@ -525,22 +568,15 @@ class CpuOffload(ModelHook):
|
||||
):
|
||||
self.prev_module_hook = prev_module_hook
|
||||
|
||||
if execution_device is not None:
|
||||
self.execution_device = execution_device
|
||||
elif is_mps_available():
|
||||
self.execution_device = torch.device("mps")
|
||||
elif torch.cuda.is_available():
|
||||
self.execution_device = torch.device(0)
|
||||
else:
|
||||
self.execution_device = torch.device("cpu")
|
||||
self.execution_device = execution_device if execution_device is not None else PartialState().default_device
|
||||
|
||||
def init_hook(self, module):
|
||||
return module.to("cpu")
|
||||
|
||||
def pre_forward(self, module, *args, **kwargs):
|
||||
module.to(self.execution_device)
|
||||
if self.prev_module_hook is not None:
|
||||
self.prev_module_hook.offload()
|
||||
module.to(self.execution_device)
|
||||
return send_to_device(args, self.execution_device), send_to_device(kwargs, self.execution_device)
|
||||
|
||||
|
||||
|
||||
@ -18,20 +18,37 @@ import tempfile
|
||||
|
||||
import torch
|
||||
|
||||
from .state import AcceleratorState
|
||||
from .state import AcceleratorState, PartialState
|
||||
from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment
|
||||
|
||||
|
||||
def notebook_launcher(function, args=(), num_processes=None, mixed_precision="no", use_port="29500"):
|
||||
def test_launch():
|
||||
"Verify a `PartialState` can be initialized."
|
||||
_ = PartialState()
|
||||
|
||||
|
||||
def notebook_launcher(
|
||||
function,
|
||||
args=(),
|
||||
num_processes=None,
|
||||
mixed_precision="no",
|
||||
use_port="29500",
|
||||
master_addr="127.0.0.1",
|
||||
node_rank=0,
|
||||
num_nodes=1,
|
||||
):
|
||||
"""
|
||||
Launches a training function, using several processes if it's possible in the current environment (TPU with
|
||||
multiple cores for instance).
|
||||
Launches a training function, using several processes or multiple nodes if it's possible in the current environment
|
||||
(TPU with multiple cores for instance).
|
||||
|
||||
<Tip warning={true}>
|
||||
|
||||
To use this function absolutely zero calls to a CUDA device must be made in the notebook session before calling. If
|
||||
any have been made, you will need to restart the notebook and make sure no cells use any CUDA capability.
|
||||
|
||||
Setting `ACCELERATE_DEBUG_MODE="1"` in your environment will run a test before truly launching to ensure that none
|
||||
of those calls have been made.
|
||||
|
||||
</Tip>
|
||||
|
||||
Args:
|
||||
@ -47,6 +64,12 @@ def notebook_launcher(function, args=(), num_processes=None, mixed_precision="no
|
||||
If `fp16` or `bf16`, will use mixed precision training on multi-GPU.
|
||||
use_port (`str`, *optional*, defaults to `"29500"`):
|
||||
The port to use to communicate between processes when launching a multi-GPU training.
|
||||
master_addr (`str`, *optional*, defaults to `"127.0.0.1"`):
|
||||
The address to use for communication between processes.
|
||||
node_rank (`int`, *optional*, defaults to 0):
|
||||
The rank of the current node.
|
||||
num_nodes (`int`, *optional*, defaults to 1):
|
||||
The number of nodes to use for training.
|
||||
|
||||
Example:
|
||||
|
||||
@ -106,10 +129,12 @@ def notebook_launcher(function, args=(), num_processes=None, mixed_precision="no
|
||||
raise ValueError(
|
||||
"You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call."
|
||||
)
|
||||
|
||||
if node_rank >= num_nodes:
|
||||
raise ValueError("The node_rank must be less than the number of nodes.")
|
||||
if num_processes > 1:
|
||||
# Multi-GPU launch
|
||||
from torch.multiprocessing import start_processes
|
||||
from torch.multiprocessing.spawn import ProcessRaisedException
|
||||
|
||||
if len(AcceleratorState._shared_state) > 0:
|
||||
raise ValueError(
|
||||
@ -117,23 +142,47 @@ def notebook_launcher(function, args=(), num_processes=None, mixed_precision="no
|
||||
"inside your training function. Restart your notebook and make sure no cells initializes an "
|
||||
"`Accelerator`."
|
||||
)
|
||||
|
||||
if torch.cuda.is_initialized():
|
||||
raise ValueError(
|
||||
"To launch a multi-GPU training from your notebook, you need to avoid running any instruction "
|
||||
"using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA "
|
||||
"function."
|
||||
)
|
||||
|
||||
# torch.distributed will expect a few environment variable to be here. We set the ones common to each
|
||||
# process here (the other ones will be set be the launcher).
|
||||
with patch_environment(
|
||||
world_size=num_processes, master_addr="127.0.01", master_port=use_port, mixed_precision=mixed_precision
|
||||
nproc=num_processes,
|
||||
node_rank=node_rank,
|
||||
world_size=num_nodes * num_processes,
|
||||
master_addr=master_addr,
|
||||
master_port=use_port,
|
||||
mixed_precision=mixed_precision,
|
||||
):
|
||||
# First dummy launch
|
||||
if os.environ.get("ACCELERATE_DEBUG_MODE", "false").lower() == "true":
|
||||
launcher = PrepareForLaunch(test_launch, distributed_type="MULTI_GPU")
|
||||
try:
|
||||
start_processes(launcher, args=(), nprocs=num_processes, start_method="fork")
|
||||
except ProcessRaisedException as e:
|
||||
err = "An issue was found when verifying a stable environment for the notebook launcher."
|
||||
if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]:
|
||||
raise RuntimeError(
|
||||
f"{err}"
|
||||
"This likely stems from an outside import causing issues once the `notebook_launcher()` is called. "
|
||||
"Please review your imports and test them when running the `notebook_launcher()` to identify "
|
||||
"which one is problematic and causing CUDA to be initialized."
|
||||
) from e
|
||||
else:
|
||||
raise RuntimeError(f"{err} The following error was raised: {e}") from e
|
||||
# Now the actual launch
|
||||
launcher = PrepareForLaunch(function, distributed_type="MULTI_GPU")
|
||||
|
||||
print(f"Launching training on {num_processes} GPUs.")
|
||||
start_processes(launcher, args=args, nprocs=num_processes, start_method="fork")
|
||||
try:
|
||||
start_processes(launcher, args=args, nprocs=num_processes, start_method="fork")
|
||||
except ProcessRaisedException as e:
|
||||
if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]:
|
||||
raise RuntimeError(
|
||||
"CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. "
|
||||
"This likely stems from an outside import causing issues once the `notebook_launcher()` is called. "
|
||||
"Please review your imports and test them when running the `notebook_launcher()` to identify "
|
||||
"which one is problematic and causing CUDA to be initialized."
|
||||
) from e
|
||||
else:
|
||||
raise RuntimeError(f"An issue was found when launching the training: {e}") from e
|
||||
|
||||
else:
|
||||
# No need for a distributed launch otherwise as it's either CPU, GPU or MPS.
|
||||
|
||||
100
src/accelerate/local_sgd.py
Normal file
100
src/accelerate/local_sgd.py
Normal file
@ -0,0 +1,100 @@
|
||||
# Copyright 2023 The HuggingFace Team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
import torch
|
||||
|
||||
from accelerate import Accelerator, DistributedType
|
||||
|
||||
|
||||
class LocalSGD:
|
||||
"""
|
||||
A helper class to support local SGD on top of Accelerator. It simply runs a given number of updates independently
|
||||
on each device, and averages model weights every K synchronization step.
|
||||
|
||||
It should be used only in the multi-GPU (or multi-CPU) setup without extensions such as DeepSpeed. In particular,
|
||||
this is a simple implementation that cannot support scenarios such as model parallelism.
|
||||
|
||||
|
||||
Although we are not aware of the true origins of this simple approach, the idea of local SGD is quite old and goes
|
||||
back to at least:
|
||||
|
||||
Zhang, J., De Sa, C., Mitliagkas, I., & Ré, C. (2016). [Parallel SGD: When does averaging help?. arXiv preprint
|
||||
arXiv:1606.07365.](https://arxiv.org/abs/1606.07365)
|
||||
|
||||
We credit the term Local SGD to the following paper (but there might be earlier references we are not aware of).
|
||||
|
||||
Stich, Sebastian Urban. ["Local SGD Converges Fast and Communicates Little." ICLR 2019-International Conference on
|
||||
Learning Representations. No. CONF. 2019.](https://arxiv.org/abs/1805.09767)
|
||||
|
||||
"""
|
||||
|
||||
def __enter__(self):
|
||||
if self.enabled:
|
||||
self.model_sync_obj = self.model.no_sync()
|
||||
self.model_sync_obj.__enter__()
|
||||
|
||||
return self
|
||||
|
||||
def __exit__(self, type, value, tb):
|
||||
if self.enabled:
|
||||
# Average all models on exit
|
||||
self._sync_and_avg_model_params()
|
||||
self.model_sync_obj.__exit__(type, value, tb)
|
||||
|
||||
def __init__(self, accelerator: Accelerator, model: torch.nn.Module, local_sgd_steps: int, enabled: bool = True):
|
||||
"""
|
||||
Constructor.
|
||||
|
||||
Args:
|
||||
model (`torch.nn.Module):
|
||||
The model whose parameters we need to average.
|
||||
accelerator (`Accelerator`):
|
||||
Accelerator object.
|
||||
local_sgd_steps (`int`):
|
||||
A number of local SGD steps (before model parameters are synchronized).
|
||||
enabled (`bool):
|
||||
Local SGD is disabled if this parameter set to `False`.
|
||||
"""
|
||||
if accelerator.distributed_type not in [
|
||||
DistributedType.NO,
|
||||
DistributedType.MULTI_CPU,
|
||||
DistributedType.MULTI_GPU,
|
||||
]:
|
||||
raise NotImplementedError("LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)")
|
||||
self.enabled = enabled and accelerator.distributed_type != DistributedType.NO
|
||||
self.num_steps = 0
|
||||
if self.enabled:
|
||||
self.accelerator = accelerator
|
||||
self.model = model
|
||||
self.local_sgd_steps = local_sgd_steps
|
||||
|
||||
def step(self):
|
||||
"""
|
||||
This function makes a "step" and synchronizes model parameters if necessary.
|
||||
"""
|
||||
self.num_steps += 1
|
||||
if not self.enabled:
|
||||
return
|
||||
|
||||
if self.num_steps % self.local_sgd_steps == 0:
|
||||
self._sync_and_avg_model_params()
|
||||
|
||||
def _sync_and_avg_model_params(self):
|
||||
"""
|
||||
Synchronize + Average model parameters across all GPUs
|
||||
"""
|
||||
|
||||
self.accelerator.wait_for_everyone()
|
||||
with self.accelerator.autocast():
|
||||
for param in self.model.parameters():
|
||||
param.data = self.accelerator.reduce(param.data, reduction="mean")
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user