|
1 | 1 | import os |
| 2 | +import warnings |
| 3 | +import logging |
| 4 | + |
| 5 | +_ENV_DONE = False |
2 | 6 |
|
3 | 7 |
|
4 | 8 | def setup_training_environment() -> None: |
5 | | - """ |
6 | | - Setup the training environment with proper logging configuration. |
7 | | - Call this in the main trainer before ray.init(). |
8 | | - """ |
| 9 | + global _ENV_DONE |
| 10 | + if _ENV_DONE: |
| 11 | + return |
| 12 | + |
| 13 | + os.environ.setdefault("DS_DISABLE_CONFIG_PRINT", "1") |
| 14 | + os.environ.setdefault("DEEPSPEED_LOG_LEVEL", "ERROR") |
| 15 | + warnings.filterwarnings("ignore") # keep only tracebacks |
| 16 | + |
| 17 | + cache = "/dev/shm/triton_cache" |
| 18 | + try: |
| 19 | + os.makedirs(cache, exist_ok=True) |
| 20 | + except OSError: |
| 21 | + cache = "/tmp/triton_cache" |
| 22 | + os.makedirs(cache, exist_ok=True) |
| 23 | + os.environ["TRITON_CACHE_DIR"] = cache |
| 24 | + |
| 25 | + try: |
| 26 | + import deepspeed |
| 27 | + from deepspeed.runtime.bf16_optimizer import BF16_Optimizer |
| 28 | + |
| 29 | + ds_log = deepspeed.utils.logging.logger |
| 30 | + ds_log.setLevel(logging.ERROR) |
| 31 | + for h in ds_log.handlers: |
| 32 | + h.setLevel(logging.ERROR) |
| 33 | + |
| 34 | + _orig_ds_destroy = BF16_Optimizer.destroy |
| 35 | + |
| 36 | + def _safe_ds_destroy(self, *a, **kw): |
| 37 | + try: |
| 38 | + _orig_ds_destroy(self, *a, **kw) |
| 39 | + except IndexError: |
| 40 | + pass |
| 41 | + |
| 42 | + BF16_Optimizer.destroy = _safe_ds_destroy |
9 | 43 |
|
10 | | - triton_cache = os.path.expanduser("~/tmp/triton_cache") |
11 | | - os.makedirs(triton_cache, exist_ok=True) |
12 | | - os.environ["TRITON_CACHE_DIR"] = triton_cache |
| 44 | + except ImportError: |
| 45 | + print("Deepspeed not available in environment; nothing to patch") |
13 | 46 |
|
14 | 47 | print("Training environment configured ✅") |
| 48 | + _ENV_DONE = True |
0 commit comments