run_on_remote.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. # Copyright 2021 The HuggingFace Inc. team. All rights reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import argparse
  17. import shlex
  18. import runhouse as rh
  19. if __name__ == "__main__":
  20. # Refer to https://runhouse-docs.readthedocs-hosted.com/en/latest/api/python/cluster.html#hardware-setup for cloud access
  21. # setup instructions, if using on-demand hardware
  22. # If user passes --user <user> --host <host> --key_path <key_path> <example> <args>, fill them in as BYO cluster
  23. # If user passes --instance <instance> --provider <provider> <example> <args>, fill them in as on-demand cluster
  24. # Throw an error if user passes both BYO and on-demand cluster args
  25. # Otherwise, use default values
  26. parser = argparse.ArgumentParser()
  27. parser.add_argument("--user", type=str, default="ubuntu")
  28. parser.add_argument("--host", type=str, default="localhost")
  29. parser.add_argument("--key_path", type=str, default=None)
  30. parser.add_argument("--instance", type=str, default="V100:1")
  31. parser.add_argument("--provider", type=str, default="cheapest")
  32. parser.add_argument("--use_spot", type=bool, default=False)
  33. parser.add_argument("--example", type=str, default="pytorch/text-generation/run_generation.py")
  34. args, unknown = parser.parse_known_args()
  35. if args.host != "localhost":
  36. if args.instance != "V100:1" or args.provider != "cheapest":
  37. raise ValueError("Cannot specify both BYO and on-demand cluster args")
  38. cluster = rh.cluster(
  39. name="rh-cluster", ips=[args.host], ssh_creds={"ssh_user": args.user, "ssh_private_key": args.key_path}
  40. )
  41. else:
  42. cluster = rh.cluster(
  43. name="rh-cluster", instance_type=args.instance, provider=args.provider, use_spot=args.use_spot
  44. )
  45. example_dir = args.example.rsplit("/", 1)[0]
  46. # Set up remote environment
  47. cluster.install_packages(["pip:./"]) # Installs transformers from local source
  48. # Note transformers is copied into the home directory on the remote machine, so we can install from there
  49. cluster.run([f"pip install -r transformers/examples/{example_dir}/requirements.txt"])
  50. cluster.run(["pip install torch --upgrade --extra-index-url https://download.pytorch.org/whl/cu117"])
  51. # Run example. You can bypass the CLI wrapper and paste your own code here.
  52. cluster.run([f'python transformers/examples/{args.example} {" ".join(shlex.quote(arg) for arg in unknown)}'])
  53. # Alternatively, we can just import and run a training function (especially if there's no wrapper CLI):
  54. # from my_script... import train
  55. # reqs = ['pip:./', 'torch', 'datasets', 'accelerate', 'evaluate', 'tqdm', 'scipy', 'scikit-learn', 'tensorboard']
  56. # launch_train_gpu = rh.function(fn=train,
  57. # system=gpu,
  58. # reqs=reqs,
  59. # name='train_bert_glue')
  60. #
  61. # We can pass in arguments just like we would to a function:
  62. # launch_train_gpu(num_epochs = 3, lr = 2e-5, seed = 42, batch_size = 16
  63. # stream_logs=True)