Compare commits
2 Commits
accelerato
...
4f1b5ad29f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4f1b5ad29f | ||
|
|
d6a2532dd7 |
@@ -210,6 +210,8 @@ axolotl lm-eval config.yml
|
|||||||
Configuration options:
|
Configuration options:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
|
lm_eval_model: # model to evaluate (local or hf path)
|
||||||
|
|
||||||
# List of tasks to evaluate
|
# List of tasks to evaluate
|
||||||
lm_eval_tasks:
|
lm_eval_tasks:
|
||||||
- arc_challenge
|
- arc_challenge
|
||||||
@@ -218,7 +220,7 @@ lm_eval_batch_size: # Batch size for evaluation
|
|||||||
output_dir: # Directory to save evaluation results
|
output_dir: # Directory to save evaluation results
|
||||||
```
|
```
|
||||||
|
|
||||||
See [LM Eval Harness](https://github.com/EleutherAI/lm-evaluation-harness) for more details.
|
See [LM Eval Harness integration docs](https://docs.axolotl.ai/docs/custom_integrations.html#language-model-evaluation-harness-lm-eval) for full configuration details.
|
||||||
|
|
||||||
### delinearize-llama4
|
### delinearize-llama4
|
||||||
|
|
||||||
|
|||||||
@@ -258,11 +258,6 @@ class TrainerBuilderBase(abc.ABC):
|
|||||||
bf16 = bf16 if bf16 is not None else False
|
bf16 = bf16 if bf16 is not None else False
|
||||||
training_args_kwargs["bf16"] = bf16
|
training_args_kwargs["bf16"] = bf16
|
||||||
|
|
||||||
if self.cfg.fp8:
|
|
||||||
training_args_kwargs["fp8"] = True
|
|
||||||
if self.cfg.fp8_enable_fsdp_float8_all_gather:
|
|
||||||
training_args_kwargs["enable_fsdp_float8_all_gather:"] = True
|
|
||||||
|
|
||||||
def _configure_scheduler(self, training_args_kwargs: dict):
|
def _configure_scheduler(self, training_args_kwargs: dict):
|
||||||
if self.cfg.lr_scheduler in ["one_cycle", "rex"]:
|
if self.cfg.lr_scheduler in ["one_cycle", "rex"]:
|
||||||
training_args_kwargs["lr_scheduler_type"] = "cosine"
|
training_args_kwargs["lr_scheduler_type"] = "cosine"
|
||||||
|
|||||||
@@ -584,9 +584,11 @@ class AxolotlTrainer(
|
|||||||
|
|
||||||
super().create_accelerator_and_postprocess()
|
super().create_accelerator_and_postprocess()
|
||||||
|
|
||||||
def build_fp8_accelerator_args(self) -> dict[str, Any]:
|
def additional_accelerator_args(
|
||||||
args = {}
|
self, fp8: bool = False, enable_fsdp_float8_all_gather: bool = False, **kwargs
|
||||||
if self.args.fp8:
|
) -> dict[str, Any]:
|
||||||
|
ret_kwargs = {}
|
||||||
|
if fp8:
|
||||||
from accelerate.utils import AORecipeKwargs
|
from accelerate.utils import AORecipeKwargs
|
||||||
from torchao.float8 import Float8LinearConfig
|
from torchao.float8 import Float8LinearConfig
|
||||||
|
|
||||||
@@ -594,22 +596,15 @@ class AxolotlTrainer(
|
|||||||
# scaling strategy. See more details here:
|
# scaling strategy. See more details here:
|
||||||
# https://github.com/pytorch/ao/tree/main/torchao/float8.
|
# https://github.com/pytorch/ao/tree/main/torchao/float8.
|
||||||
config = Float8LinearConfig(
|
config = Float8LinearConfig(
|
||||||
enable_fsdp_float8_all_gather=self.args.enable_fsdp_float8_all_gather,
|
enable_fsdp_float8_all_gather=enable_fsdp_float8_all_gather,
|
||||||
force_recompute_fp8_weight_in_bwd=self.args.enable_fsdp_float8_all_gather
|
force_recompute_fp8_weight_in_bwd=enable_fsdp_float8_all_gather is True,
|
||||||
is True,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
args["mixed_precision"] = "fp8"
|
ret_kwargs["mixed_precision"] = "fp8"
|
||||||
args["kwargs_handlers"] = [AORecipeKwargs(config=config)] # type: ignore
|
ret_kwargs["kwargs_handlers"] = [AORecipeKwargs(config=config)] # type: ignore
|
||||||
os.environ["ACCELERATE_MIXED_PRECISION"] = "fp8"
|
os.environ["ACCELERATE_MIXED_PRECISION"] = "fp8"
|
||||||
|
|
||||||
return args
|
return ret_kwargs
|
||||||
|
|
||||||
def _build_accelerator_args(self, **kwargs) -> dict[str, Any]:
|
|
||||||
args = super().build_accelerator_args(**kwargs)
|
|
||||||
fp8_args = self.build_fp8_accelerator_args()
|
|
||||||
args.update(fp8_args)
|
|
||||||
return args
|
|
||||||
|
|
||||||
def log(self, logs: dict[str, float], start_time: float | None = None) -> None:
|
def log(self, logs: dict[str, float], start_time: float | None = None) -> None:
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -263,13 +263,3 @@ class AxolotlTrainingMixins:
|
|||||||
dion_rank_multiple_of: int | None = field(
|
dion_rank_multiple_of: int | None = field(
|
||||||
default=None,
|
default=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
fp8: bool | None = field(
|
|
||||||
default=None,
|
|
||||||
metadata={"help": "Whether to use FP8 precision for training"},
|
|
||||||
)
|
|
||||||
|
|
||||||
enable_fsdp_float8_all_gather: bool | None = field(
|
|
||||||
default=None,
|
|
||||||
metadata={"help": "Whether to use FSDP with FP8 precision for all_gather"},
|
|
||||||
)
|
|
||||||
|
|||||||
44
src/axolotl/integrations/kernels/README.md
Normal file
44
src/axolotl/integrations/kernels/README.md
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
# Kernels Integration
|
||||||
|
|
||||||
|
MoE (Mixture of Experts) kernels speed up training for MoE layers and reduce VRAM costs. In transformers v5, `batched_mm` and `grouped_mm` were integrated as built-in options via the `experts_implementation` config kwarg:
|
||||||
|
|
||||||
|
```python
|
||||||
|
class ExpertsInterface(GeneralInterface):
|
||||||
|
_global_mapping = {
|
||||||
|
"batched_mm": batched_mm_experts_forward,
|
||||||
|
"grouped_mm": grouped_mm_experts_forward,
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
In our custom integration, we add support for **ScatterMoE**, which is even more efficient and faster than `grouped_mm`.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Add the following to your axolotl YAML config:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
plugins:
|
||||||
|
- axolotl.integrations.kernels.KernelsPlugin
|
||||||
|
|
||||||
|
use_kernels: true
|
||||||
|
use_scattermoe: true
|
||||||
|
```
|
||||||
|
|
||||||
|
**Important:** Setting `experts_implementation` is incompatible with `use_scattermoe`.
|
||||||
|
|
||||||
|
## How It Works
|
||||||
|
|
||||||
|
The `KernelsPlugin` runs before model loading and:
|
||||||
|
|
||||||
|
1. Registers the ScatterMoE kernel from the [`axolotl-ai-co/scattermoe`](https://huggingface.co/axolotl-ai-co/scattermoe) Hub repo.
|
||||||
|
2. Patches the model's `SparseMoeBlock` forward method with the optimized ScatterMoE implementation.
|
||||||
|
|
||||||
|
This works for any MoE model in transformers that uses a `SparseMoeBlock` class (Mixtral, Qwen2-MoE, OLMoE, etc.).
|
||||||
|
|
||||||
|
## Limitations
|
||||||
|
|
||||||
|
ScatterMoE uses a softmax -> topk routing, so results may be different for some model arch as baseline (GPT-OSS, GLM_MOE_DSA).
|
||||||
|
|
||||||
|
## Note on MegaBlocks
|
||||||
|
|
||||||
|
We tested [MegaBlocks](https://huggingface.co/kernels-community/megablocks) but were unable to ensure numerical accuracy, so we did not integrate it. It was also incompatible with many newer model architectures in transformers.
|
||||||
@@ -6,6 +6,12 @@ See https://github.com/EleutherAI/lm-evaluation-harness
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
There are two ways to use the LM Eval integration:
|
||||||
|
|
||||||
|
### 1. Post-Training Evaluation
|
||||||
|
|
||||||
|
When training with the plugin enabled, evaluation runs automatically after training completes:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
plugins:
|
plugins:
|
||||||
- axolotl.integrations.lm_eval.LMEvalPlugin
|
- axolotl.integrations.lm_eval.LMEvalPlugin
|
||||||
@@ -16,9 +22,50 @@ lm_eval_tasks:
|
|||||||
- arc_easy
|
- arc_easy
|
||||||
|
|
||||||
lm_eval_batch_size: # Batch size for evaluation
|
lm_eval_batch_size: # Batch size for evaluation
|
||||||
output_dir: # Directory to save evaluation results
|
|
||||||
|
# Directory to save evaluation results.
|
||||||
|
# The final model is loaded from this directory
|
||||||
|
# unless specified otherwise (see below)
|
||||||
|
output_dir:
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Run training as usual:
|
||||||
|
```bash
|
||||||
|
axolotl train config.yml
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Standalone CLI Evaluation
|
||||||
|
|
||||||
|
Evaluate any model directly without training:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
lm_eval_model: meta-llama/Llama-2-7b-hf
|
||||||
|
|
||||||
|
plugins:
|
||||||
|
- axolotl.integrations.lm_eval.LMEvalPlugin
|
||||||
|
|
||||||
|
lm_eval_tasks:
|
||||||
|
- gsm8k
|
||||||
|
- hellaswag
|
||||||
|
- arc_easy
|
||||||
|
|
||||||
|
lm_eval_batch_size: 8
|
||||||
|
output_dir: ./outputs
|
||||||
|
```
|
||||||
|
|
||||||
|
Run evaluation:
|
||||||
|
```bash
|
||||||
|
axolotl lm-eval config.yml
|
||||||
|
```
|
||||||
|
|
||||||
|
## Model Selection Priority
|
||||||
|
|
||||||
|
The model to evaluate is selected in the following priority order:
|
||||||
|
|
||||||
|
1. **`lm_eval_model`** - Explicit model path or HuggingFace repo (highest priority)
|
||||||
|
2. **`hub_model_id`** - Trained model pushed to HuggingFace Hub
|
||||||
|
3. **`output_dir`** - Local checkpoint directory containing trained model weights
|
||||||
|
|
||||||
## Citation
|
## Citation
|
||||||
|
|
||||||
```bib
|
```bib
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ Module for the Plugin for LM Eval Harness
|
|||||||
import subprocess # nosec
|
import subprocess # nosec
|
||||||
|
|
||||||
from axolotl.integrations.base import BasePlugin
|
from axolotl.integrations.base import BasePlugin
|
||||||
from axolotl.integrations.lm_eval.cli import build_lm_eval_command
|
from axolotl.integrations.lm_eval.cli import build_lm_eval_command, get_model_path
|
||||||
|
|
||||||
from .args import LMEvalArgs as LMEvalArgs
|
from .args import LMEvalArgs as LMEvalArgs
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ class LMEvalPlugin(BasePlugin):
|
|||||||
wandb_project=cfg.wandb_project,
|
wandb_project=cfg.wandb_project,
|
||||||
wandb_entity=cfg.wandb_entity,
|
wandb_entity=cfg.wandb_entity,
|
||||||
wandb_name=cfg.wandb_name,
|
wandb_name=cfg.wandb_name,
|
||||||
model=cfg.lm_eval_model or cfg.hub_model_id,
|
model=get_model_path(cfg),
|
||||||
):
|
):
|
||||||
subprocess.run( # nosec
|
subprocess.run( # nosec
|
||||||
lm_eval_args,
|
lm_eval_args,
|
||||||
|
|||||||
@@ -13,6 +13,21 @@ import yaml
|
|||||||
from axolotl.utils.dict import DictDefault
|
from axolotl.utils.dict import DictDefault
|
||||||
|
|
||||||
|
|
||||||
|
def get_model_path(cfg: DictDefault) -> str | None:
|
||||||
|
"""
|
||||||
|
Determine which model path to use for evaluation.
|
||||||
|
|
||||||
|
Priority order (highest to lowest):
|
||||||
|
1. lm_eval_model - Explicit model path override
|
||||||
|
2. hub_model_id - Model pushed to HuggingFace Hub
|
||||||
|
3. None - Falls back to output_dir in build_lm_eval_command
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Model path string or None to use output_dir fallback
|
||||||
|
"""
|
||||||
|
return cfg.lm_eval_model or cfg.hub_model_id or None
|
||||||
|
|
||||||
|
|
||||||
def build_lm_eval_command(
|
def build_lm_eval_command(
|
||||||
tasks: list[str],
|
tasks: list[str],
|
||||||
bfloat16=True,
|
bfloat16=True,
|
||||||
@@ -108,7 +123,7 @@ def lm_eval(config: str, cloud: Optional[str] = None):
|
|||||||
wandb_project=cfg.wandb_project,
|
wandb_project=cfg.wandb_project,
|
||||||
wandb_entity=cfg.wandb_entity,
|
wandb_entity=cfg.wandb_entity,
|
||||||
wandb_name=cfg.wandb_name,
|
wandb_name=cfg.wandb_name,
|
||||||
model=cfg.lm_eval_model or cfg.hub_model_id,
|
model=get_model_path(cfg),
|
||||||
revision=cfg.revision,
|
revision=cfg.revision,
|
||||||
apply_chat_template=cfg.apply_chat_template,
|
apply_chat_template=cfg.apply_chat_template,
|
||||||
fewshot_as_multiturn=cfg.fewshot_as_multiturn,
|
fewshot_as_multiturn=cfg.fewshot_as_multiturn,
|
||||||
|
|||||||
@@ -100,6 +100,7 @@ class PatchManager:
|
|||||||
self._apply_fsdp_patches()
|
self._apply_fsdp_patches()
|
||||||
self._apply_adapter_patches()
|
self._apply_adapter_patches()
|
||||||
self._apply_model_specific_patches()
|
self._apply_model_specific_patches()
|
||||||
|
self._apply_fp8_patches()
|
||||||
self._apply_flash_attention_peft_patches()
|
self._apply_flash_attention_peft_patches()
|
||||||
self._apply_gradient_checkpointing_patches()
|
self._apply_gradient_checkpointing_patches()
|
||||||
self._patch_attention()
|
self._patch_attention()
|
||||||
@@ -234,6 +235,17 @@ class PatchManager:
|
|||||||
|
|
||||||
patch_kimi_model()
|
patch_kimi_model()
|
||||||
|
|
||||||
|
def _apply_fp8_patches(self):
|
||||||
|
"""Apply patches for FP8 support."""
|
||||||
|
if self.cfg.fp8:
|
||||||
|
from axolotl.monkeypatch.trainer_accelerator_args import (
|
||||||
|
patch_create_accelerate_code_for_fp8,
|
||||||
|
)
|
||||||
|
|
||||||
|
patch_create_accelerate_code_for_fp8(
|
||||||
|
self.cfg.fp8_enable_fsdp_float8_all_gather
|
||||||
|
)
|
||||||
|
|
||||||
def _apply_flash_attention_peft_patches(self):
|
def _apply_flash_attention_peft_patches(self):
|
||||||
"""Apply patches for Flash Attention with PEFT."""
|
"""Apply patches for Flash Attention with PEFT."""
|
||||||
if self.cfg.adapter:
|
if self.cfg.adapter:
|
||||||
|
|||||||
83
src/axolotl/monkeypatch/trainer_accelerator_args.py
Normal file
83
src/axolotl/monkeypatch/trainer_accelerator_args.py
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
"""
|
||||||
|
allow adding additional kwargs to Accelerator init
|
||||||
|
"""
|
||||||
|
|
||||||
|
import inspect
|
||||||
|
|
||||||
|
from transformers import Trainer
|
||||||
|
|
||||||
|
from axolotl.monkeypatch.utils import detab_code
|
||||||
|
from axolotl.utils.logging import get_logger
|
||||||
|
|
||||||
|
LOG = get_logger(__name__)
|
||||||
|
|
||||||
|
ORIGINAL_TRAINER_CODE = """
|
||||||
|
# create accelerator object
|
||||||
|
self.accelerator = Accelerator(**args)
|
||||||
|
"""
|
||||||
|
|
||||||
|
PATCHED_TRAINER_CODE = """
|
||||||
|
if hasattr(self, "additional_accelerator_args"):
|
||||||
|
additional_args = self.additional_accelerator_args(fp8=True, enable_fsdp_float8_all_gather={enable_fsdp_float8_all_gather}, **args)
|
||||||
|
if additional_args:
|
||||||
|
args.update(additional_args)
|
||||||
|
|
||||||
|
# create accelerator object
|
||||||
|
self.accelerator = Accelerator(**args)
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def get_create_accelerate_code() -> str:
|
||||||
|
training_loop = inspect.getsource(Trainer.create_accelerator_and_postprocess)
|
||||||
|
return training_loop
|
||||||
|
|
||||||
|
|
||||||
|
def check_create_accelerate_code_is_patchable() -> bool:
|
||||||
|
create_code = get_create_accelerate_code()
|
||||||
|
create_code, _ = detab_code(create_code)
|
||||||
|
return ORIGINAL_TRAINER_CODE in create_code
|
||||||
|
|
||||||
|
|
||||||
|
def patch_create_accelerate_code_for_fp8(enable_fsdp_float8_all_gather: bool):
|
||||||
|
"""
|
||||||
|
Monkeypatch create_accelerator_and_postprocess so it checks for additional kwargs.
|
||||||
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
create_code = get_create_accelerate_code()
|
||||||
|
except OSError:
|
||||||
|
return
|
||||||
|
Trainer._original_create_accelerator_and_postprocess = create_code
|
||||||
|
create_code, _ = detab_code(create_code)
|
||||||
|
if ORIGINAL_TRAINER_CODE not in create_code:
|
||||||
|
return
|
||||||
|
|
||||||
|
patched_trainer_code = PATCHED_TRAINER_CODE.format(
|
||||||
|
enable_fsdp_float8_all_gather=enable_fsdp_float8_all_gather
|
||||||
|
)
|
||||||
|
create_code = create_code.replace(ORIGINAL_TRAINER_CODE, patched_trainer_code)
|
||||||
|
create_code = create_code.replace(
|
||||||
|
"def create_accelerator_and_postprocess(",
|
||||||
|
"def fixed_create_accelerator_and_postprocess(",
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
|
||||||
|
# load imports necessary
|
||||||
|
import transformers.trainer
|
||||||
|
|
||||||
|
items_to_import = []
|
||||||
|
for item in dir(transformers.trainer):
|
||||||
|
if item in create_code:
|
||||||
|
items_to_import.append(item)
|
||||||
|
|
||||||
|
exec(
|
||||||
|
"from transformers.trainer import ("
|
||||||
|
+ ", ".join(x for x in items_to_import)
|
||||||
|
+ ")",
|
||||||
|
globals(),
|
||||||
|
)
|
||||||
|
exec(create_code, globals())
|
||||||
|
LOG.info("patching create_accelerator_and_postprocess to allow for overrides")
|
||||||
|
Trainer.create_accelerator_and_postprocess = (
|
||||||
|
fixed_create_accelerator_and_postprocess
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user