Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

monit-runpod.py 4.0 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  1. import importlib
  2. import fire
  3. import yaml
  4. import logging
  5. import os
  6. import sys
  7. from pathlib import Path
  8. from addict import Dict
  9. import json
  10. import time
  11. import runpod
  12. from datetime import datetime, timezone, timedelta
  13. import signal
  14. from tqdm import tqdm
  15. import re
  16. from discord import SyncWebhook
  17. import pexpect
  18. AXOLOTL_RUNPOD_IMAGE = 'winglian/axolotl-runpod:main-py3.9-cu118-2.0.0'
  19. AXOLOTL_RUNPOD_IMAGE_SIZE_IN_GB = 12.5
  20. AXOLOTL_RUNPOD_IMAGE_SIZE = AXOLOTL_RUNPOD_IMAGE_SIZE_IN_GB * 1024 # In MB
  21. BITS_PER_BYTE = 8
  22. COMPRESSION_RATIO = 0.2
  23. DEFAULT_TEMPLATE_ID = '758uq6u5fc'
  24. MAX_BID_PER_GPU = 2.0
  25. POLL_PERIOD = 5 # 5 seconds
  26. MAX_WAIT_TIME = 60 * 10 # 10 minutes
  27. DEFAULT_STOP_AFTER = 60 * 15 # 15 minutes to prevent accidental starting a pod and forgot to stop
  28. DEFAULT_TERMINATE_AFTER = 60 * 60 * 24 # 24 hours to prevent accidental starting a pod and forgot to terminate
  29. class DictDefault(Dict):
  30. """
  31. A Dict that returns None instead of returning empty Dict for missing keys.
  32. Borrowed from https://github.com/utensil/axolotl/blob/local_dataset/src/axolotl/utils/dict.py
  33. """
  34. def __missing__(self, key):
  35. return None
  36. project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
  37. os.chdir(project_root)
  38. # src_dir = os.path.join(project_root, "src")
  39. # sys.path.insert(0, src_dir)
  40. logging.basicConfig(level=os.getenv("LOG_LEVEL", "DEBUG"))
  41. # os.environ["RUNPOD_DEBUG"] = 'true'
  42. def notify_discord(msg):
  43. webhook = SyncWebhook.from_url(os.getenv("DISCORD_WEBHOOK_URL"))
  44. webhook.send(msg)
  45. def log_info(msg):
  46. logging.info(msg)
  47. notify_discord(msg)
  48. def log_error(msg, exc_info=None):
  49. logging.error(msg, exc_info=exc_info)
  50. if exc_info is not None:
  51. notify_discord(f'{msg}: {exc_info}')
  52. else:
  53. notify_discord(msg)
  54. def terminate(pod):
  55. runpod.terminate_pod(pod['id'])
  56. log_info(f"Pod {pod['id']} terminated")
  57. def monit_runpod(**kwargs):
  58. runpod_api_key = os.getenv("RUNPOD_API_KEY")
  59. if runpod_api_key is None:
  60. raise ValueError("No RUNPOD_API_KEY environment variable found")
  61. runpod.api_key = runpod_api_key
  62. try:
  63. myself = runpod.get_myself()['myself']
  64. logging.info(f"RunPod overview: {myself}")
  65. if myself is not None:
  66. msg = ""
  67. pods = myself['pods']
  68. if len(pods) > 0:
  69. msg = f"{len(pods)} pods running, spending ${myself['currentSpendPerHr']} per hour:\n```\n"
  70. msg += "STATUS\tID \tCPU%\tMEM%\tGPU%\tVRAM%\tUptime\n"
  71. idle_count = 0
  72. for pod in pods:
  73. id = pod['id']
  74. runtime = pod['runtime']
  75. if runtime is not None:
  76. uptime = runtime['uptimeInSeconds']
  77. stat = pod['latestTelemetry']
  78. cpu = stat['cpuUtilization']
  79. ram = stat['memoryUtilization']
  80. gpu = stat['averageGpuMetrics']['percentUtilization']
  81. vram = stat['averageGpuMetrics']['memoryUtilization']
  82. if gpu > 0.8 and vram > 0.5:
  83. status = 'Train'
  84. elif cpu > 0.5:
  85. status = 'Load'
  86. else:
  87. status = 'Idle'
  88. idle_count += 1
  89. msg += f'{status}\t{id}\t{cpu}%\t{ram}%\t{gpu}%\t{vram}%\t{uptime / 60.0:.2f} min\n'
  90. else:
  91. msg += f'Booting\t{id}\n'
  92. msg += "```"
  93. logging.info(msg)
  94. if idle_count > 0:
  95. log_info(msg)
  96. else:
  97. log_info('No pod running, disabling monit')
  98. pexpect.run('gh workflow disable monit.yml')
  99. except Exception as ex:
  100. log_error(f"Something went wrong with monit_runpod", exc_info=ex)
  101. if __name__ == "__main__":
  102. fire.Fire(monit_runpod)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...