| 1 |
# Copyright 2023 The HuggingFace Team. All rights reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
import argparse |
| 15 |
|
| 16 |
import runhouse as rh |
| 17 |
import torch |
| 18 |
from nlp_example import training_function |
| 19 |
|
| 20 |
from accelerate.utils import PrepareForLaunch, patch_environment |
| 21 |
|
| 22 |
|
| 23 |
def launch_train(*args): |
| 24 |
num_processes = torch.cuda.device_count() |
| 25 |
print(f"Device count: {num_processes}") |
| 26 |
with patch_environment( |
| 27 |
world_size=num_processes, master_addr="127.0.0.1", master_port="29500", mixed_precision=args[1].mixed_precision |
| 28 |
): |
| 29 |
launcher = PrepareForLaunch(training_function, distributed_type="MULTI_GPU") |
| 30 |
torch.multiprocessing.start_processes(launcher, args=args, nprocs=num_processes, start_method="spawn") |
| 31 |
|
| 32 |
|
| 33 |
if __name__ == "__main__": |
| 34 |
# Refer to https://runhouse-docs.readthedocs-hosted.com/en/main/rh_primitives/cluster.html#hardware-setup |
| 35 |
# for cloud access setup instructions (if using on-demand hardware), and for API specifications. |
| 36 |
|
| 37 |
# on-demand GPU |
| 38 |
# gpu = rh.cluster(name='rh-cluster', instance_type='V100:1', provider='cheapest', use_spot=False) # single GPU |
| 39 |
gpu = rh.cluster(name="rh-cluster", instance_type="V100:4", provider="cheapest", use_spot=False) # multi GPU |
| 40 |
gpu.up_if_not() |
| 41 |
|
| 42 |
# on-prem GPU |
| 43 |
# gpu = rh.cluster( |
| 44 |
# ips=["ip_addr"], ssh_creds={ssh_user:"<username>", ssh_private_key:"<key_path>"}, name="rh-cluster" |
| 45 |
# ) |
| 46 |
|
| 47 |
# Set up remote function |
| 48 |
reqs = [ |
| 49 |
"pip:./", |
| 50 |
"transformers", |
| 51 |
"datasets", |
| 52 |
"evaluate", |
| 53 |
"tqdm", |
| 54 |
"scipy", |
| 55 |
"scikit-learn", |
| 56 |
"tensorboard", |
| 57 |
"torch --upgrade --extra-index-url https://download.pytorch.org/whl/cu117", |
| 58 |
] |
| 59 |
launch_train_gpu = rh.function(fn=launch_train, system=gpu, reqs=reqs, name="train_bert_glue") |
| 60 |
|
| 61 |
# Define train args/config, run train function |
| 62 |
train_args = argparse.Namespace(cpu=False, mixed_precision="fp16") |
| 63 |
config = {"lr": 2e-5, "num_epochs": 3, "seed": 42, "batch_size": 16} |
| 64 |
launch_train_gpu(config, train_args, stream_logs=True) |
| 65 |
|
| 66 |
# Alternatively, we can just run as instructed in the README (but only because there's already a wrapper CLI): |
| 67 |
# gpu.install_packages(reqs) |
| 68 |
# gpu.run(['accelerate launch --multi_gpu accelerate/examples/nlp_example.py']) |