Skip to content

Commit abc9ee5

Browse files
Alayshah/lea 182 deepspeed for dpo (#2)
* naming for example datasets * changed setup_training call * dpo configs updated * sft configs updated * logging / deepspeed import changes --------- Co-authored-by: EdoardoMosca <edoardo@liquid.ai>
1 parent 043d149 commit abc9ee5

5 files changed

Lines changed: 57 additions & 24 deletions

File tree

config.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@
1919
subset: Dataset subset to use for HuggingFace datasets, default None
2020
"""
2121

22-
smoltalk_dataset = DatasetLoader(
22+
example_sft_dataset = DatasetLoader(
2323
"HuggingFaceTB/smoltalk", "sft", limit=1000, test_size=0.2, subset="all"
2424
)
2525

26+
example_dpo_dataset = DatasetLoader(
27+
"mlabonne/orpo-dpo-mix-40k", "dpo", limit=1000, test_size=0.2, subset="default"
28+
)
29+
2630

2731
#################################
2832
# Training Config #
@@ -72,10 +76,10 @@
7276
"""
7377

7478
JOB_CONFIG = JobConfig(
75-
job_name="my_output_dir",
79+
job_name="my_job",
7680
model_name="LFM2-1.2B",
7781
training_type="sft",
78-
dataset=smoltalk_dataset,
82+
dataset=example_sft_dataset,
7983
training_config=training_config,
8084
peft_config=peft_config,
8185
)

src/leap_finetune/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import sys
22

3-
from leap_finetune.trainer import ray_trainer
43
from leap_finetune.utils.logging import setup_training_environment
5-
from leap_finetune.utils.constants import LEAP_FINETUNE_DIR
64

7-
# Set Ray Core logging before importing Ray
85
setup_training_environment()
96

7+
from leap_finetune.trainer import ray_trainer # noqa
8+
from leap_finetune.utils.constants import LEAP_FINETUNE_DIR # noqa
9+
1010

1111
def main() -> None:
12+
# Set Ray Core logging before importing Ray
13+
1214
print("Launching leap-finetune ✅")
1315

1416
# Import config only in main process, not Ray workers
@@ -29,7 +31,7 @@ def main() -> None:
2931
"JOB_CONFIG not found in config.py. Please define JOB_CONFIG in your config.py file."
3032
)
3133
except ValueError as e:
32-
raise ValueError(f"Invalid JOB_CONFIG: {e}")
34+
raise ValueError(f"Issue with JOB_CONFIG: {e}")
3335
finally:
3436
sys.path.remove(root_path)
3537

src/leap_finetune/configs/dpo_configs.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@
1515
"train_micro_batch_size_per_gpu": "auto",
1616
"gradient_clipping": "auto",
1717
"gradient_accumulation_steps": "auto",
18-
"optimizer": {
19-
"type": "AdamW",
20-
"params": {
21-
"lr": "auto", # Uses learning_rate from training config
22-
"betas": "auto", # DEFAULT: (0.9, 0.999)
23-
"eps": "auto", # DEFAULT: 1e-8
24-
"weight_decay": "auto", # DEFAULT: 0.01
25-
},
26-
},
2718
"bf16": {"enabled": "auto"},
2819
"activation_checkpointing": {
2920
"partition_activations": False,
@@ -54,5 +45,6 @@
5445
"save_strategy": "epoch",
5546
"eval_strategy": "epoch",
5647
"load_best_model_at_end": True,
57-
# "deepspeed": DEEPSEED_CONFIG,
48+
"ddp_find_unused_parameters": False,
49+
"deepspeed": DEEPSEED_CONFIG,
5850
}

src/leap_finetune/configs/sft_configs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@
5454
"save_strategy": "epoch",
5555
"eval_strategy": "epoch",
5656
"load_best_model_at_end": True,
57+
"ddp_find_unused_parameters": False,
5758
"deepspeed": DEEPSEED_CONFIG,
5859
}

src/leap_finetune/utils/logging.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,48 @@
11
import os
2+
import warnings
3+
import logging
4+
5+
_ENV_DONE = False
26

37

48
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
943

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")
1346

1447
print("Training environment configured ✅")
48+
_ENV_DONE = True

0 commit comments

Comments
 (0)