Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
25b8dd7
update lora_config.yaml + LORA.md + lora.py
Goekdeniz-Guelmez Mar 14, 2025
d723843
Merge branch 'ml-explore:main' into adding-report-to-wandb
Goekdeniz-Guelmez Mar 17, 2025
cb7f8a3
Merge branch 'ml-explore:main' into adding-report-to-wandb
Goekdeniz-Guelmez Mar 18, 2025
abbbdca
code formatting
Goekdeniz-Guelmez Mar 18, 2025
32e834f
Merge branch 'ml-explore:main' into adding-report-to-wandb
Goekdeniz-Guelmez Mar 24, 2025
db9e156
Merge branch 'ml-explore:main' into adding-report-to-wandb
Goekdeniz-Guelmez Mar 25, 2025
94a226e
Merge branch 'ml-explore:main' into adding-report-to-wandb
Goekdeniz-Guelmez Mar 27, 2025
c39532f
udpaet Acknowledgements.md
Goekdeniz-Guelmez Mar 31, 2025
a0bbe11
nits
Goekdeniz-Guelmez Mar 31, 2025
becbaec
Merge branch 'ml-explore:main' into adding-report-to-wandb
Goekdeniz-Guelmez Mar 31, 2025
0304b28
Refactor WandB integration in lora.py and trainer.py
Goekdeniz-Guelmez Apr 5, 2025
a3a6d49
Enhance WandBCallback to include log directory in initialization
Goekdeniz-Guelmez Apr 5, 2025
e710205
nits
Goekdeniz-Guelmez Apr 5, 2025
1db8c28
formating
Goekdeniz-Guelmez Apr 9, 2025
4513137
Merge branch 'ml-explore:main' into adding-report-to-wandb
Goekdeniz-Guelmez Apr 17, 2025
6d77293
Merge branch 'main' into adding-report-to-wandb
Goekdeniz-Guelmez Apr 21, 2025
142f316
Merge branch 'ml-explore:main' into adding-report-to-wandb
Goekdeniz-Guelmez Apr 28, 2025
4439694
Merge branch 'main' into adding-report-to-wandb
Goekdeniz-Guelmez May 4, 2025
40682c4
Merge branch 'main' into adding-report-to-wandb
Goekdeniz-Guelmez May 8, 2025
c4e62db
Merge branch 'ml-explore:main' into adding-report-to-wandb
Goekdeniz-Guelmez May 9, 2025
7fd78fd
README.md
Goekdeniz-Guelmez May 9, 2025
35d9d68
update example yaml
Goekdeniz-Guelmez May 9, 2025
e5ed455
nits
Goekdeniz-Guelmez May 9, 2025
325df0e
nits
Goekdeniz-Guelmez May 9, 2025
50f5381
nits in readme
awni May 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ACKNOWLEDGMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ MLX LM was developed with contributions from the following individuals:

- Shunta Saito: Added support for PLaMo models.
- Prince Canuma: Helped add support for `Starcoder2` models.
- Gökdeniz Gülmez: Added support for the following architectures: OpenBMB's `MiniCPM` and `MiniCPM3`, Kyutai's `Helium`, State-Space's`Mamba v1`, Z.ai & THUKEG's `GLM4`, and Allenai's `OLMoE`; Added support for the following training algorithms: `full-fine-tuning`; Added support for the following other features: `Multiple Optimizers to choose for training`.
- Gökdeniz Gülmez: Added support for the following architectures: OpenBMB's `MiniCPM` and `MiniCPM3`, Kyutai's `Helium`, State-Space's`Mamba v1`, Z.ai & THUKEG's `GLM4`, and Allenai's `OLMoE`; Added support for the following training algorithms: `full-fine-tuning`; Added support for the following other features: `Multiple Optimizers to choose for training`, and `reporting training metrics to WandB (Weights & Biases)`.
5 changes: 5 additions & 0 deletions mlx_lm/LORA.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ You can specify the output location with `--adapter-path`.
You can resume fine-tuning with an existing adapter with
`--resume-adapter-file <path_to_adapters.safetensors>`.

#### Logging

You can log training metrics to Weights & Biases by passing a project name with
the `--wandb` flag. Make sure to install wandb with `pip install wandb`.

#### Prompt Masking

The default training computes a loss for every token in the sample. You can
Expand Down
3 changes: 3 additions & 0 deletions mlx_lm/examples/lora_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ val_batches: 25
# Adam learning rate.
learning_rate: 1e-5

# Whether to report the logs to WandB
Comment thread
Goekdeniz-Guelmez marked this conversation as resolved.
# wand: 'wandb-project"

# Number of training steps between loss reporting.
steps_per_report: 10

Expand Down
18 changes: 16 additions & 2 deletions mlx_lm/lora.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Copyright © 2024 Apple Inc.

import argparse
import math
import os
Expand All @@ -13,6 +11,7 @@
import numpy as np
import yaml

from .tuner.callbacks import WandBCallback
from .tuner.datasets import CacheDataset, load_dataset
from .tuner.trainer import TrainingArgs, TrainingCallback, evaluate, train
from .tuner.utils import (
Expand Down Expand Up @@ -68,6 +67,7 @@
"lr_schedule": None,
"lora_parameters": {"rank": 8, "dropout": 0.0, "scale": 10.0},
"mask_prompt": False,
"wandb": None,
}


Expand Down Expand Up @@ -179,6 +179,12 @@ def build_parser():
help="Use gradient checkpointing to reduce memory use.",
default=None,
)
parser.add_argument(
"--wandb",
type=str,
default=None,
help="WandB project name to report training metrics. Disabled if None.",
)
parser.add_argument("--seed", type=int, help="The PRNG seed")
return parser

Expand Down Expand Up @@ -281,6 +287,14 @@ def evaluate_model(args, model: nn.Module, test_set):
def run(args, training_callback: TrainingCallback = None):
np.random.seed(args.seed)

if args.wandb is not None:
training_callback = WandBCallback(
project_name=args.wandb,
log_dir=args.adapter_path,
config=vars(args),
wrapped_callback=training_callback,
)

print("Loading pretrained model")
model, tokenizer = load(args.model)

Expand Down
41 changes: 41 additions & 0 deletions mlx_lm/tuner/callbacks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
try:
import wandb
except ImportError:
wandb = None


class TrainingCallback:

def on_train_loss_report(self, train_info: dict):
"""Called to report training loss at specified intervals."""
pass

def on_val_loss_report(self, val_info: dict):
"""Called to report validation loss at specified intervals or the beginning."""
pass


class WandBCallback(TrainingCallback):
def __init__(
self,
project_name: str,
log_dir: str,
config: dict,
wrapped_callback: TrainingCallback = None,
):
if wandb is None:
raise ImportError(
"wandb is not installed. Please install it to use WandBCallback."
)
self.wrapped_callback = wrapped_callback
wandb.init(project=project_name, dir=log_dir, config=config)

def on_train_loss_report(self, train_info: dict):
wandb.log(train_info)
if self.wrapped_callback:
self.wrapped_callback.on_train_loss_report(train_info)

def on_val_loss_report(self, val_info: dict):
wandb.log(val_info)
if self.wrapped_callback:
self.wrapped_callback.on_val_loss_report(val_info)
17 changes: 2 additions & 15 deletions mlx_lm/tuner/trainer.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
# Copyright © 2024 Apple Inc.

import glob
import shutil

import time
from dataclasses import dataclass, field
from functools import partial
from pathlib import Path
from typing import List, Optional, Tuple

import mlx.core as mx
import mlx.nn as nn
import numpy as np
from mlx.nn.utils import average_gradients
from mlx.utils import tree_flatten
from transformers import PreTrainedTokenizer

from .callbacks import TrainingCallback
from .datasets import CacheDataset


Expand Down Expand Up @@ -183,17 +181,6 @@ def evaluate(
return (all_losses / ntokens).item()


class TrainingCallback:

def on_train_loss_report(self, train_info: dict):
"""Called to report training loss at specified intervals."""
pass

def on_val_loss_report(self, val_info: dict):
"""Called to report validation loss at specified intervals or the beginning."""
pass


def train(
model,
optimizer,
Expand Down