Guest User

Untitled

a guest
Feb 10th, 2025
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. import os
  2. import subprocess
  3. import modal
  4.  
  5. volume = modal.Volume.from_name("Output", create_if_missing = True)
  6.  
  7. image = (
  8. modal.Image.debian_slim(python_version="3.12")
  9. .apt_install("git", "libgl1", "libglib2.0-0")
  10. .add_local_file(local_path = "config.json", remote_path = "/root/config.json")
  11. .add_local_dir(local_path = "thirddiffusion", remote_path = "/root/thirddiffusion")
  12. )
  13.  
  14. app = modal.App(
  15. name = "OneTrainer",
  16. image = image,
  17. volumes = {"/workspace": volume}
  18. )
  19.  
  20. @app.function(gpu = "A100", timeout = 14400)
  21. def train_lora():
  22.  
  23. WORKDIR = "/workspace"
  24.  
  25. REPODIR = os.path.join(WORKDIR, "OneTrainer")
  26.  
  27. CONFIG_PATH = "/root/config.json"
  28.  
  29. DATA_PATH = "/root/thirddiffusion"
  30.  
  31. try:
  32.  
  33. if not os.path.exists(REPODIR):
  34.  
  35. subprocess.run(
  36. ["git", "clone", "https://github.com/Nerogar/OneTrainer.git", REPODIR],
  37. check = True,
  38. stderr = subprocess.PIPE
  39. )
  40.  
  41. INSTALL_PATH = os.path.join(REPODIR, "install.sh")
  42.  
  43. subprocess.run(
  44. ["bash", INSTALL_PATH],
  45. cwd = REPODIR,
  46. check = True,
  47. env = dict(os.environ, DEBIAN_FRONTEND = "noninteractive")
  48. )
  49.  
  50. else:
  51.  
  52. if os.path.exists(os.path.join(REPODIR, "out")):
  53.  
  54. subprocess.run(["rm", "-rf", os.path.join(REPODIR, "out")], check = True)
  55.  
  56. if os.path.exists(os.path.join(REPODIR, "tmp")):
  57.  
  58. subprocess.run(["rm", "-rf", os.path.join(REPODIR, "tmp")], check = True)
  59.  
  60. PYTHON_PATH = os.path.join(REPODIR, "venv", "bin", "python")
  61.  
  62. #subprocess.run(
  63. # [PYTHON_PATH, "-m", "pip", "install", "-r", os.path.join(REPODIR, "requirements.txt")],
  64. # check = True
  65. #)
  66.  
  67. subprocess.run(
  68. [
  69. PYTHON_PATH,
  70. os.path.join(REPODIR, "scripts", "train.py"),
  71. "--config-path",
  72. CONFIG_PATH
  73. ],
  74. cwd = REPODIR,
  75. check = True,
  76. env = dict(os.environ, CUDA_VISIBLE_DEVICES = "0")
  77. )
  78.  
  79. volume.commit()
  80.  
  81. except subprocess.CalledProcessError as e:
  82.  
  83. raise RuntimeError(f"Command failed: {e.stderr.decode()}") from e
  84.  
  85. @app.local_entrypoint()
  86. def main():
  87.  
  88. try:
  89.  
  90. result = train_lora.remote()
  91.  
  92. print(f"Success: {result}")
  93.  
  94. except modal.exception.ExecutionError as e:
  95.  
  96. print(f"Failed: {e}")
  97.  
  98. print("Check logs with: modal logs " + app.app_id)
  99.  
  100. if __name__ == "__main__":
  101.  
  102. main()
Advertisement
Add Comment
Please, Sign In to add comment