fix: minor patches for multimodal (#2441)
* fix: update chat_template * fix: handle gemma3 showing a lot of no content for turn 0 * fix: remove unknown config from examples * fix: test * fix: temporary disable gemma2 test * fix: stop overwriting config.text_config unnecessarily * fix: handling of set cache to the text_config section * feat: add liger gemma support and bump liger to 0.5.5 * fix: add double use_cache setting * fix: add support for final_logit_softcap in CCE for gemma2/3 * fix: set use_cache before model load * feat: add missing layernorm override * fix: handle gemma3 rmsnorm * fix: use wrapper to pass dim as hidden_size * fix: change dim to positional * fix: patch with wrong mlp * chore: refactor use_cache handling * fix import issues * fix tests.e2e.utils import --------- Co-authored-by: Wing Lian <wing@axolotl.ai>
This commit is contained in:
2
.github/workflows/tests-nightly.yml
vendored
2
.github/workflows/tests-nightly.yml
vendored
@@ -136,4 +136,4 @@ jobs:
|
||||
echo "NIGHTLY_BUILD=${{ matrix.nightly_build }}" >> $GITHUB_ENV
|
||||
- name: Run tests job on Modal
|
||||
run: |
|
||||
modal run cicd.tests
|
||||
modal run cicd.e2e_tests
|
||||
|
||||
4
.github/workflows/tests.yml
vendored
4
.github/workflows/tests.yml
vendored
@@ -232,7 +232,7 @@ jobs:
|
||||
echo "N_GPUS=${{ matrix.num_gpus }}" >> $GITHUB_ENV
|
||||
- name: Run tests job on Modal
|
||||
run: |
|
||||
modal run cicd.tests
|
||||
modal run cicd.e2e_tests
|
||||
|
||||
docker-e2e-tests:
|
||||
if: github.repository_owner == 'axolotl-ai-cloud'
|
||||
@@ -279,4 +279,4 @@ jobs:
|
||||
echo "N_GPUS=${{ matrix.num_gpus }}" >> $GITHUB_ENV
|
||||
- name: Run tests job on Modal
|
||||
run: |
|
||||
modal run cicd.tests
|
||||
modal run cicd.e2e_tests
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
[settings]
|
||||
profile=black
|
||||
known_third_party=wandb,comet_ml
|
||||
known_local_folder=src,tests
|
||||
|
||||
@@ -10,7 +10,7 @@ load_in_4bit: true
|
||||
strict: false
|
||||
|
||||
# huggingface repo
|
||||
chat_template: gemma3_text
|
||||
chat_template: gemma3
|
||||
datasets:
|
||||
- path: cgato/SlimOrcaDedupCleaned
|
||||
type: chat_template
|
||||
@@ -19,7 +19,6 @@ val_set_size: 0.0
|
||||
output_dir: ./outputs/lora-out
|
||||
|
||||
dataset_exact_deduplication: true
|
||||
test_value: true
|
||||
|
||||
sequence_len: 4096
|
||||
sample_packing: true
|
||||
|
||||
@@ -25,8 +25,8 @@ import torch
|
||||
|
||||
from axolotl.integrations.base import BasePlugin
|
||||
from axolotl.utils import get_pytorch_version
|
||||
from axolotl.utils.distributed import zero_only
|
||||
|
||||
from ...utils.distributed import zero_only
|
||||
from .args import CutCrossEntropyArgs # pylint: disable=unused-import. # noqa: F401
|
||||
|
||||
LOG = logging.getLogger("axolotl.integrations.cut_cross_entropy")
|
||||
|
||||
@@ -15,7 +15,6 @@ import transformers
|
||||
from cut_cross_entropy.transformers.utils import (
|
||||
PatchOptions,
|
||||
TransformersModelT,
|
||||
apply_lce,
|
||||
)
|
||||
from torch import nn
|
||||
from transformers.cache_utils import Cache, HybridCache
|
||||
@@ -33,6 +32,8 @@ from transformers.utils import (
|
||||
)
|
||||
from transformers.utils.deprecation import deprecate_kwarg
|
||||
|
||||
from axolotl.integrations.cut_cross_entropy.monkeypatch.utils import apply_lce
|
||||
|
||||
_PATCH_OPTS: PatchOptions | None = None
|
||||
|
||||
|
||||
@@ -134,25 +135,17 @@ def cce_forward(
|
||||
|
||||
if _PATCH_OPTS is not None and _PATCH_OPTS.use_lce(labels, self.training):
|
||||
assert labels is not None
|
||||
if self.config.final_logit_softcapping is not None:
|
||||
logger.warning_once(
|
||||
"final_logit_softcapping is not supported for gemma3_text with CCE. Disabling."
|
||||
)
|
||||
loss = apply_lce(
|
||||
hidden_states[:, slice_indices, :],
|
||||
self.lm_head.weight,
|
||||
labels,
|
||||
_PATCH_OPTS,
|
||||
softcap=getattr(self.config, "final_logit_softcapping", None),
|
||||
**loss_kwargs,
|
||||
)
|
||||
elif _PATCH_OPTS is not None and defer_logits_calculation:
|
||||
# defer logits calculation to the ConditionalGeneration forward
|
||||
logits = hidden_states[:, slice_indices, :]
|
||||
|
||||
if self.config.final_logit_softcapping is not None:
|
||||
logger.warning_once(
|
||||
"final_logit_softcapping is not supported for gemma3 with CCE. Disabling."
|
||||
)
|
||||
else:
|
||||
logits = self.lm_head(hidden_states[:, slice_indices, :])
|
||||
if self.config.final_logit_softcapping is not None:
|
||||
@@ -353,6 +346,7 @@ def cce_forward_multimodal(
|
||||
self.language_model.lm_head.weight,
|
||||
labels,
|
||||
_PATCH_OPTS,
|
||||
softcap=getattr(self.config, "final_logit_softcapping", None),
|
||||
**lm_kwargs,
|
||||
)
|
||||
else:
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
# Copyright (C) 2024 Apple Inc. All Rights Reserved.
|
||||
|
||||
"""Monkeypatch for apply_lce to add softcap."""
|
||||
|
||||
import torch
|
||||
from cut_cross_entropy import linear_cross_entropy
|
||||
from cut_cross_entropy.transformers.utils import PatchOptions
|
||||
|
||||
|
||||
def apply_lce(
|
||||
e: torch.Tensor,
|
||||
c: torch.Tensor,
|
||||
labels: torch.Tensor,
|
||||
opts: PatchOptions,
|
||||
bias: torch.Tensor | None = None,
|
||||
softcap: float | None = None,
|
||||
**loss_kwargs,
|
||||
) -> torch.Tensor:
|
||||
"""Monkey patch for apply_lce to support softcap kwarg."""
|
||||
num_items_in_batch = loss_kwargs.get("num_items_in_batch", None)
|
||||
cce_kwargs = opts.to_kwargs()
|
||||
if num_items_in_batch is not None and cce_kwargs["reduction"] == "mean":
|
||||
cce_kwargs["reduction"] = "sum"
|
||||
else:
|
||||
num_items_in_batch = None
|
||||
|
||||
loss = linear_cross_entropy(
|
||||
e,
|
||||
c,
|
||||
labels.to(e.device),
|
||||
bias=bias,
|
||||
shift=True,
|
||||
softcap=softcap,
|
||||
**cce_kwargs,
|
||||
)
|
||||
|
||||
if num_items_in_batch is not None:
|
||||
loss = loss / num_items_in_batch
|
||||
|
||||
return loss
|
||||
@@ -20,6 +20,26 @@ liger_layer_norm: true
|
||||
liger_fused_linear_cross_entropy: true
|
||||
```
|
||||
|
||||
## Supported Models
|
||||
|
||||
- deepseek_v2
|
||||
- gemma
|
||||
- gemma2
|
||||
- gemma3 (partial support, no support for FLCE yet)
|
||||
- granite
|
||||
- jamba
|
||||
- llama
|
||||
- mistral
|
||||
- mixtral
|
||||
- mllama
|
||||
- mllama_text_model
|
||||
- olmo2
|
||||
- paligemma
|
||||
- phi3
|
||||
- qwen2
|
||||
- qwen2_5_vl
|
||||
- qwen2_vl
|
||||
|
||||
## Citation
|
||||
|
||||
```bib
|
||||
|
||||
@@ -21,6 +21,7 @@ It is designed to be performant, correct, and light-weight.
|
||||
import inspect
|
||||
import logging
|
||||
import sys
|
||||
from functools import partial
|
||||
|
||||
from axolotl.integrations.base import BasePlugin
|
||||
|
||||
@@ -41,11 +42,18 @@ class LigerPlugin(BasePlugin):
|
||||
def pre_model_load(self, cfg):
|
||||
from liger_kernel.transformers.cross_entropy import LigerCrossEntropyLoss
|
||||
from liger_kernel.transformers.functional import liger_cross_entropy
|
||||
from liger_kernel.transformers.geglu import LigerGEGLUMLP
|
||||
from liger_kernel.transformers.layer_norm import LigerLayerNorm
|
||||
from liger_kernel.transformers.monkey_patch import MODEL_TYPE_TO_APPLY_LIGER_FN
|
||||
from liger_kernel.transformers.rms_norm import LigerRMSNorm
|
||||
from liger_kernel.transformers.rope import liger_rotary_pos_emb
|
||||
from liger_kernel.transformers.swiglu import LigerSwiGLUMLP
|
||||
|
||||
if cfg.liger_cross_entropy and cfg.liger_fused_linear_cross_entropy:
|
||||
raise ValueError(
|
||||
"Cannot have both `liger_cross_entropy` and `liger_fused_linear_cross_entropy` set."
|
||||
)
|
||||
|
||||
if cfg.model_config_type in MODEL_TYPE_TO_APPLY_LIGER_FN:
|
||||
apply_liger_fn = MODEL_TYPE_TO_APPLY_LIGER_FN[cfg.model_config_type]
|
||||
liger_fn_sig = inspect.signature(apply_liger_fn)
|
||||
@@ -82,6 +90,8 @@ class LigerPlugin(BasePlugin):
|
||||
modeling_jamba.JambaRMSNorm = LigerRMSNorm
|
||||
if cfg.liger_glu_activation:
|
||||
modeling_jamba.JambaMLP = LigerSwiGLUMLP
|
||||
if cfg.liger_layer_norm:
|
||||
modeling_jamba.nn.LayerNorm = LigerLayerNorm
|
||||
if cfg.liger_cross_entropy:
|
||||
from transformers.loss.loss_utils import nn
|
||||
|
||||
@@ -104,15 +114,51 @@ class LigerPlugin(BasePlugin):
|
||||
# The DeepseekV2 version of RoPE is different than upstream LLaMA.
|
||||
# See https://github.com/linkedin/Liger-Kernel/issues/129#issuecomment-2313763528
|
||||
logging.warning("Fused liger_rope is not supported for DeepseekV2.")
|
||||
if cfg.liger_glu_activation:
|
||||
logging.warning("liger_glu_activation is not supported for DeepseekV2.")
|
||||
if cfg.liger_rms_norm:
|
||||
modeling_mod.DeepseekV2RMSNorm = LigerRMSNorm
|
||||
if cfg.liger_glu_activation:
|
||||
modeling_mod.DeepseekV2MLP.forward = LigerSwiGLUMLP.forward
|
||||
if cfg.liger_layer_norm:
|
||||
modeling_mod.DeepseekV2MLP.forward = LigerLayerNorm.forward
|
||||
if cfg.liger_cross_entropy:
|
||||
# We do not patch `nn.functional.cross_entropy` for DeepseekV2 as it still uses
|
||||
# nn.CrossEntropyLoss in the forward method.
|
||||
modeling_mod.CrossEntropyLoss = LigerCrossEntropyLoss
|
||||
if cfg.liger_fused_linear_cross_entropy:
|
||||
modeling_mod.DeepseekV2ForCausalLM.forward = deepseekv2_lce_forward
|
||||
elif cfg.model_config_type in ["gemma3_text", "deepseek_v3"]:
|
||||
elif cfg.model_config_type in ["gemma3", "gemma3_text"]:
|
||||
from transformers.models.gemma3 import modeling_gemma3
|
||||
|
||||
if cfg.liger_rope:
|
||||
modeling_gemma3.apply_rotary_pos_emb = liger_rotary_pos_emb
|
||||
if cfg.liger_rms_norm:
|
||||
|
||||
def _liger_rms_norm_wrapper(dim, **kwargs):
|
||||
"Convert 'dim' keyword to 'hidden_size' to pass to LigerRMSNorm"
|
||||
return LigerRMSNorm(hidden_size=dim, **kwargs)
|
||||
|
||||
modeling_gemma3.Gemma3RMSNorm = partial(
|
||||
_liger_rms_norm_wrapper,
|
||||
offset=1.0,
|
||||
casting_mode="gemma",
|
||||
init_fn="zeros",
|
||||
in_place=False,
|
||||
)
|
||||
if cfg.liger_glu_activation:
|
||||
modeling_gemma3.Gemma3MLP = LigerGEGLUMLP
|
||||
if cfg.liger_layer_norm:
|
||||
modeling_gemma3.nn.LayerNorm = LigerLayerNorm
|
||||
|
||||
if cfg.liger_cross_entropy:
|
||||
from transformers.loss.loss_utils import nn
|
||||
|
||||
nn.functional.cross_entropy = liger_cross_entropy
|
||||
|
||||
if cfg.liger_fused_linear_cross_entropy:
|
||||
raise NotImplementedError(
|
||||
"Fused linear cross entropy is not yet supported for Gemma3."
|
||||
)
|
||||
elif cfg.model_config_type in ["deepseek_v3"]:
|
||||
raise ValueError(f"Unsupported model config type: {cfg.model_config_type}")
|
||||
|
||||
@@ -411,11 +411,15 @@ class ChatTemplateStrategy(PromptTokenizingStrategy):
|
||||
if turn_idx >= len(turns):
|
||||
raise ValueError(f"Turn index {turn_idx} out of range")
|
||||
|
||||
# mistral does not output message if it contains only system message
|
||||
# mistral/gemma3 does not output message if it contains only system message
|
||||
if (
|
||||
turn_idx == 0
|
||||
and turns[0].get("role") == "system"
|
||||
and "mistral" in self.tokenizer.name_or_path.lower()
|
||||
and (
|
||||
"mistral" in self.tokenizer.name_or_path.lower()
|
||||
# gemma3 uses gemma tokenizer
|
||||
or "gemma" in self.tokenizer.name_or_path.lower()
|
||||
)
|
||||
):
|
||||
return -1, -1
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import math
|
||||
import os
|
||||
import types
|
||||
from functools import cached_property
|
||||
from typing import Any, Dict, Optional, Tuple, Union # noqa: F401
|
||||
from typing import Any, Dict, Optional, Tuple
|
||||
|
||||
import addict
|
||||
import bitsandbytes as bnb
|
||||
@@ -25,7 +25,7 @@ from peft import (
|
||||
prepare_model_for_kbit_training,
|
||||
)
|
||||
from torch import nn
|
||||
from transformers import ( # noqa: F401
|
||||
from transformers import (
|
||||
AddedToken,
|
||||
AutoConfig,
|
||||
AutoModelForCausalLM,
|
||||
@@ -39,6 +39,7 @@ from transformers import ( # noqa: F401
|
||||
LlavaForConditionalGeneration,
|
||||
Mistral3ForConditionalGeneration,
|
||||
MllamaForConditionalGeneration,
|
||||
PretrainedConfig,
|
||||
PreTrainedModel,
|
||||
PreTrainedTokenizerBase,
|
||||
ProcessorMixin,
|
||||
@@ -107,14 +108,21 @@ def get_module_class_from_name(module, name):
|
||||
return None
|
||||
|
||||
|
||||
def check_model_config(cfg: DictDefault, model_config: Union[AutoConfig, DictDefault]):
|
||||
def check_model_config(cfg: DictDefault, model_config: PretrainedConfig):
|
||||
# Set use_cache to False
|
||||
if hasattr(model_config, "use_cache"):
|
||||
model_config.use_cache = False
|
||||
|
||||
if cfg.is_multimodal:
|
||||
if hasattr(model_config, "text_config"):
|
||||
model_config = model_config.text_config
|
||||
model_config.use_cache = False
|
||||
elif hasattr(model_config, "get_text_config"):
|
||||
model_config = model_config.get_text_config()
|
||||
model_config.use_cache = False
|
||||
# For multimodal configs, use_cache is set in the text_config
|
||||
if hasattr(model_config, "get_text_config"):
|
||||
text_config = model_config.get_text_config()
|
||||
if hasattr(text_config, "use_cache"):
|
||||
text_config.use_cache = False
|
||||
else:
|
||||
raise ValueError(
|
||||
"No text config found for multimodal model. Please raise an Issue with model details."
|
||||
)
|
||||
|
||||
# check if image_size is not set and load image size from model config if available
|
||||
if (
|
||||
@@ -523,14 +531,6 @@ class ModelLoader:
|
||||
|
||||
# init model config
|
||||
self.model_config = load_model_config(cfg)
|
||||
if cfg.is_multimodal:
|
||||
if hasattr(self.model_config, "text_config"):
|
||||
self.text_model_config = self.model_config.text_config
|
||||
else:
|
||||
# for qwen2_vl
|
||||
self.text_model_config = self.model_config.get_text_config()
|
||||
else:
|
||||
self.text_model_config = self.model_config
|
||||
|
||||
self.auto_model_loader = AutoModelForCausalLM # pylint: disable=invalid-name
|
||||
|
||||
@@ -947,8 +947,6 @@ class ModelLoader:
|
||||
quantization_config = (
|
||||
quantization_config or self.model_kwargs["quantization_config"]
|
||||
)
|
||||
if self.cfg.is_multimodal:
|
||||
self.model_config.text_config = self.text_model_config
|
||||
self.model = load_sharded_model_quant(
|
||||
self.base_model,
|
||||
self.model_config,
|
||||
@@ -969,9 +967,6 @@ class ModelLoader:
|
||||
|
||||
_ = _configure_zero3_memory_efficient_loading()
|
||||
|
||||
if self.cfg.is_multimodal:
|
||||
self.model_config.text_config = self.text_model_config
|
||||
|
||||
# Load model with random initialization if specified
|
||||
if self.cfg.random_init_weights:
|
||||
# AutoModel classes support the from_config method
|
||||
@@ -1026,8 +1021,6 @@ class ModelLoader:
|
||||
and self.model_type != "AutoModelForCausalLM"
|
||||
and not self.cfg.trust_remote_code
|
||||
):
|
||||
if self.cfg.is_multimodal:
|
||||
self.model_config.text_config = self.text_model_config
|
||||
if self.cfg.gptq:
|
||||
self.model = self.auto_model_loader.from_pretrained(
|
||||
self.base_model,
|
||||
@@ -1043,25 +1036,7 @@ class ModelLoader:
|
||||
**self.model_kwargs,
|
||||
)
|
||||
else:
|
||||
# Shouldn't be a problem most of the time. will obviously error if the model doesn't support this
|
||||
# when training starts
|
||||
if (
|
||||
hasattr(self.text_model_config, "max_seq_len")
|
||||
and self.text_model_config.max_seq_len
|
||||
and self.cfg.sequence_len > self.text_model_config.max_seq_len
|
||||
):
|
||||
self.text_model_config.max_seq_len = self.cfg.sequence_len
|
||||
LOG.warning(f"increasing context length to {self.cfg.sequence_len}")
|
||||
elif (
|
||||
hasattr(self.text_model_config, "max_sequence_length")
|
||||
and self.text_model_config.max_sequence_length
|
||||
and self.cfg.sequence_len > self.text_model_config.max_sequence_length
|
||||
):
|
||||
self.text_model_config.max_sequence_length = self.cfg.sequence_len
|
||||
LOG.warning(f"increasing context length to {self.cfg.sequence_len}")
|
||||
if self.cfg.gptq:
|
||||
if self.cfg.is_multimodal:
|
||||
self.model_config.text_config = self.text_model_config
|
||||
self.model = self.auto_model_loader.from_pretrained(
|
||||
self.base_model,
|
||||
config=self.model_config,
|
||||
@@ -1080,8 +1055,6 @@ class ModelLoader:
|
||||
|
||||
_ = _configure_zero3_memory_efficient_loading()
|
||||
|
||||
if self.cfg.is_multimodal:
|
||||
self.model_config.text_config = self.text_model_config
|
||||
self.model = self.auto_model_loader.from_pretrained(
|
||||
self.base_model,
|
||||
config=self.model_config,
|
||||
@@ -1346,8 +1319,6 @@ class ModelLoader:
|
||||
requires_grad.append(f"{name}: {param.requires_grad}")
|
||||
if len(requires_grad) == 0:
|
||||
LOG.warning("there are no parameters that require gradient updates")
|
||||
if hasattr(self.model, "config"):
|
||||
self.model.config.use_cache = False
|
||||
|
||||
if self.cfg.flash_optimum:
|
||||
from optimum.bettertransformer import BetterTransformer
|
||||
|
||||
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
@@ -14,7 +14,8 @@ import requests
|
||||
from datasets import load_dataset
|
||||
from huggingface_hub import snapshot_download
|
||||
from transformers import AutoTokenizer
|
||||
from utils import disable_hf_offline, enable_hf_offline
|
||||
|
||||
from tests.hf_offline_utils import disable_hf_offline, enable_hf_offline
|
||||
|
||||
|
||||
def retry_on_request_exceptions(max_retries=3, delay=1):
|
||||
|
||||
@@ -6,11 +6,12 @@ import unittest
|
||||
|
||||
import pytest
|
||||
from transformers import AddedToken, AutoTokenizer
|
||||
from utils import enable_hf_offline
|
||||
|
||||
from axolotl.core.chat.format.chatml import format_message
|
||||
from axolotl.core.chat.messages import ChatFormattedChats, Chats
|
||||
|
||||
from tests.hf_offline_utils import enable_hf_offline # noqa
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", name="llama_tokenizer")
|
||||
@enable_hf_offline
|
||||
|
||||
@@ -5,7 +5,6 @@ e2e tests for kd trainer support in Axolotl
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from e2e.utils import check_tensorboard, require_torch_2_5_1
|
||||
|
||||
from axolotl.cli.args import TrainerCliArgs
|
||||
from axolotl.common.datasets import load_datasets
|
||||
@@ -13,6 +12,8 @@ from axolotl.train import train
|
||||
from axolotl.utils.config import normalize_config, prepare_plugins, validate_config
|
||||
from axolotl.utils.dict import DictDefault
|
||||
|
||||
from tests.e2e.utils import check_tensorboard, require_torch_2_5_1
|
||||
|
||||
|
||||
@pytest.fixture(name="kd_min_cfg")
|
||||
def min_cfg(temp_dir):
|
||||
|
||||
@@ -2,15 +2,13 @@
|
||||
Simple end-to-end test for Liger integration
|
||||
"""
|
||||
|
||||
from e2e.utils import require_torch_2_4_1
|
||||
|
||||
from axolotl.cli.args import TrainerCliArgs
|
||||
from axolotl.common.datasets import load_datasets
|
||||
from axolotl.train import train
|
||||
from axolotl.utils.config import normalize_config, prepare_plugins
|
||||
from axolotl.utils.dict import DictDefault
|
||||
|
||||
from ..utils import check_model_output_exists
|
||||
from tests.e2e.utils import check_model_output_exists, require_torch_2_4_1
|
||||
|
||||
|
||||
class LigerIntegrationTestCase:
|
||||
|
||||
@@ -8,11 +8,12 @@ from pathlib import Path
|
||||
import pytest
|
||||
import yaml
|
||||
from accelerate.test_utils import execute_subprocess_async
|
||||
from e2e.utils import require_vllm
|
||||
from transformers.testing_utils import get_torch_dist_unique_port
|
||||
|
||||
from axolotl.utils.dict import DictDefault
|
||||
|
||||
from tests.e2e.utils import require_vllm
|
||||
|
||||
|
||||
class TestGRPO:
|
||||
"""
|
||||
|
||||
@@ -9,12 +9,13 @@ from pathlib import Path
|
||||
import pytest
|
||||
import yaml
|
||||
from accelerate.test_utils import execute_subprocess_async
|
||||
from e2e.utils import check_tensorboard
|
||||
from huggingface_hub import snapshot_download
|
||||
from transformers.testing_utils import get_torch_dist_unique_port
|
||||
|
||||
from axolotl.utils.dict import DictDefault
|
||||
|
||||
from tests.e2e.utils import check_tensorboard
|
||||
|
||||
LOG = logging.getLogger("axolotl.tests.e2e.multigpu")
|
||||
os.environ["WANDB_DISABLED"] = "true"
|
||||
|
||||
|
||||
@@ -9,10 +9,11 @@ from pathlib import Path
|
||||
import pytest
|
||||
import yaml
|
||||
from accelerate.test_utils import execute_subprocess_async
|
||||
from e2e.utils import check_tensorboard, require_torch_lt_2_6_0
|
||||
|
||||
from axolotl.utils.dict import DictDefault
|
||||
|
||||
from tests.e2e.utils import check_tensorboard, require_torch_lt_2_6_0
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
os.environ["WANDB_DISABLED"] = "true"
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from utils import enable_hf_offline
|
||||
|
||||
from axolotl.cli.args import TrainerCliArgs
|
||||
from axolotl.common.datasets import load_datasets
|
||||
@@ -15,6 +14,8 @@ from axolotl.train import train
|
||||
from axolotl.utils.config import normalize_config, validate_config
|
||||
from axolotl.utils.dict import DictDefault
|
||||
|
||||
from tests.hf_offline_utils import enable_hf_offline
|
||||
|
||||
LOG = logging.getLogger("axolotl.tests.e2e")
|
||||
os.environ["WANDB_DISABLED"] = "true"
|
||||
|
||||
|
||||
@@ -5,14 +5,14 @@ E2E tests for llama
|
||||
import logging
|
||||
import os
|
||||
|
||||
from e2e.utils import check_model_output_exists
|
||||
|
||||
from axolotl.cli.args import TrainerCliArgs
|
||||
from axolotl.common.datasets import load_datasets
|
||||
from axolotl.train import train
|
||||
from axolotl.utils.config import normalize_config, validate_config
|
||||
from axolotl.utils.dict import DictDefault
|
||||
|
||||
from tests.e2e.utils import check_model_output_exists
|
||||
|
||||
LOG = logging.getLogger("axolotl.tests.e2e")
|
||||
os.environ["WANDB_DISABLED"] = "true"
|
||||
|
||||
|
||||
85
tests/hf_offline_utils.py
Normal file
85
tests/hf_offline_utils.py
Normal file
@@ -0,0 +1,85 @@
|
||||
"""
|
||||
test utils for helpers and decorators
|
||||
"""
|
||||
|
||||
import os
|
||||
from functools import wraps
|
||||
|
||||
from huggingface_hub.utils import reset_sessions
|
||||
|
||||
|
||||
def reload_modules(hf_hub_offline):
|
||||
# Force reload of the modules that check this variable
|
||||
import importlib
|
||||
|
||||
import datasets
|
||||
import huggingface_hub.constants
|
||||
|
||||
# Reload the constants module first, as others depend on it
|
||||
importlib.reload(huggingface_hub.constants)
|
||||
huggingface_hub.constants.HF_HUB_OFFLINE = hf_hub_offline
|
||||
importlib.reload(datasets.config)
|
||||
setattr(datasets.config, "HF_HUB_OFFLINE", hf_hub_offline)
|
||||
reset_sessions()
|
||||
|
||||
|
||||
def enable_hf_offline(test_func):
|
||||
"""
|
||||
test decorator that sets HF_HUB_OFFLINE environment variable to True and restores it after the test even if the test fails.
|
||||
:param test_func:
|
||||
:return:
|
||||
"""
|
||||
|
||||
@wraps(test_func)
|
||||
def wrapper(*args, **kwargs):
|
||||
# Save the original value of HF_HUB_OFFLINE environment variable
|
||||
original_hf_offline = os.getenv("HF_HUB_OFFLINE")
|
||||
|
||||
# Set HF_OFFLINE environment variable to True
|
||||
os.environ["HF_HUB_OFFLINE"] = "1"
|
||||
|
||||
reload_modules(True)
|
||||
try:
|
||||
# Run the test function
|
||||
return test_func(*args, **kwargs)
|
||||
finally:
|
||||
# Restore the original value of HF_HUB_OFFLINE environment variable
|
||||
if original_hf_offline is not None:
|
||||
os.environ["HF_HUB_OFFLINE"] = original_hf_offline
|
||||
reload_modules(bool(original_hf_offline))
|
||||
else:
|
||||
del os.environ["HF_HUB_OFFLINE"]
|
||||
reload_modules(False)
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
def disable_hf_offline(test_func):
|
||||
"""
|
||||
test decorator that sets HF_HUB_OFFLINE environment variable to False and restores it after the wrapped func
|
||||
:param test_func:
|
||||
:return:
|
||||
"""
|
||||
|
||||
@wraps(test_func)
|
||||
def wrapper(*args, **kwargs):
|
||||
# Save the original value of HF_HUB_OFFLINE environment variable
|
||||
original_hf_offline = os.getenv("HF_HUB_OFFLINE")
|
||||
|
||||
# Set HF_OFFLINE environment variable to True
|
||||
os.environ["HF_HUB_OFFLINE"] = "0"
|
||||
|
||||
reload_modules(False)
|
||||
try:
|
||||
# Run the test function
|
||||
return test_func(*args, **kwargs)
|
||||
finally:
|
||||
# Restore the original value of HF_HUB_OFFLINE environment variable
|
||||
if original_hf_offline is not None:
|
||||
os.environ["HF_HUB_OFFLINE"] = original_hf_offline
|
||||
reload_modules(bool(original_hf_offline))
|
||||
else:
|
||||
del os.environ["HF_HUB_OFFLINE"]
|
||||
reload_modules(False)
|
||||
|
||||
return wrapper
|
||||
@@ -5,11 +5,12 @@ shared fixtures for prompt strategies tests
|
||||
import pytest
|
||||
from datasets import Dataset
|
||||
from transformers import AutoTokenizer
|
||||
from utils import enable_hf_offline
|
||||
|
||||
from axolotl.prompt_strategies.jinja_template_analyzer import JinjaTemplateAnalyzer
|
||||
from axolotl.utils.chat_templates import _CHAT_TEMPLATES
|
||||
|
||||
from tests.hf_offline_utils import enable_hf_offline
|
||||
|
||||
|
||||
@pytest.fixture(name="assistant_dataset")
|
||||
def fixture_assistant_dataset():
|
||||
|
||||
@@ -6,12 +6,13 @@ import pytest
|
||||
from datasets import Dataset
|
||||
from tokenizers import AddedToken
|
||||
from transformers import AutoTokenizer
|
||||
from utils import enable_hf_offline
|
||||
|
||||
from axolotl.datasets import TokenizedPromptDataset
|
||||
from axolotl.prompt_tokenizers import AlpacaPromptTokenizingStrategy
|
||||
from axolotl.prompters import AlpacaPrompter, PromptStyle
|
||||
|
||||
from tests.hf_offline_utils import enable_hf_offline
|
||||
|
||||
|
||||
@pytest.fixture(name="alpaca_dataset")
|
||||
def fixture_alpaca_dataset():
|
||||
|
||||
@@ -6,7 +6,6 @@ import unittest
|
||||
|
||||
import pytest
|
||||
from transformers import AutoTokenizer
|
||||
from utils import enable_hf_offline
|
||||
|
||||
from axolotl.utils.chat_templates import (
|
||||
_CHAT_TEMPLATES,
|
||||
@@ -14,6 +13,8 @@ from axolotl.utils.chat_templates import (
|
||||
get_chat_template,
|
||||
)
|
||||
|
||||
from tests.hf_offline_utils import enable_hf_offline
|
||||
|
||||
|
||||
@pytest.fixture(name="llama3_tokenizer")
|
||||
@enable_hf_offline
|
||||
|
||||
@@ -9,7 +9,6 @@ import pytest
|
||||
from datasets import Dataset
|
||||
from tokenizers import AddedToken
|
||||
from transformers import PreTrainedTokenizer
|
||||
from utils import enable_hf_offline
|
||||
|
||||
from axolotl.prompt_strategies.chat_template import (
|
||||
ChatTemplatePrompter,
|
||||
@@ -18,6 +17,8 @@ from axolotl.prompt_strategies.chat_template import (
|
||||
from axolotl.prompters import IGNORE_TOKEN_ID
|
||||
from axolotl.utils.chat_templates import get_chat_template
|
||||
|
||||
from tests.hf_offline_utils import enable_hf_offline
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
LOG = logging.getLogger("axolotl")
|
||||
|
||||
@@ -31,12 +32,14 @@ PARAMETRIZE_PARAMS = [
|
||||
"mistralv03_tokenizer_chat_template_jinja",
|
||||
"[/INST]",
|
||||
),
|
||||
(
|
||||
"gemma2_tokenizer",
|
||||
"jinja",
|
||||
"gemma2_tokenizer_chat_template_jinja",
|
||||
"<end_of_turn>",
|
||||
),
|
||||
# TODO: temporarily skip gemma due to gemma3 template
|
||||
# Re-enable on new chat_template implementation for perf
|
||||
# (
|
||||
# "gemma2_tokenizer",
|
||||
# "jinja",
|
||||
# "gemma2_tokenizer_chat_template_jinja",
|
||||
# "<end_of_turn>",
|
||||
# ),
|
||||
("phi35_tokenizer", "phi_35", None, "<|end|>"),
|
||||
]
|
||||
|
||||
@@ -94,7 +97,11 @@ class TestChatTemplateConfigurations:
|
||||
if (
|
||||
turn_idx == 0
|
||||
and turn.get("from") in ["system", "context"]
|
||||
and "mistral" in tokenizer.name_or_path.lower()
|
||||
and (
|
||||
"mistral" in tokenizer.name_or_path.lower()
|
||||
or "gemma"
|
||||
in tokenizer.name_or_path.lower() # temporarily skip gemma due to gemma3 template
|
||||
)
|
||||
):
|
||||
assert (
|
||||
start_idx == -1 and end_idx == -1
|
||||
|
||||
@@ -7,11 +7,12 @@ import unittest
|
||||
import pytest
|
||||
from datasets import Dataset
|
||||
from transformers import AutoTokenizer
|
||||
from utils import enable_hf_offline
|
||||
|
||||
from axolotl.prompt_strategies.dpo.chat_template import default
|
||||
from axolotl.utils.dict import DictDefault
|
||||
|
||||
from tests.hf_offline_utils import enable_hf_offline
|
||||
|
||||
|
||||
@pytest.fixture(name="assistant_dataset")
|
||||
def fixture_assistant_dataset():
|
||||
|
||||
@@ -5,12 +5,13 @@ Tests for loading DPO preference datasets with chatml formatting
|
||||
import unittest
|
||||
|
||||
import pytest
|
||||
from utils import enable_hf_offline
|
||||
|
||||
from axolotl.prompt_strategies.dpo import load as load_dpo
|
||||
from axolotl.utils.data.rl import load_prepare_preference_datasets
|
||||
from axolotl.utils.dict import DictDefault
|
||||
|
||||
from tests.hf_offline_utils import enable_hf_offline
|
||||
|
||||
|
||||
@pytest.fixture(name="minimal_dpo_cfg")
|
||||
def fixture_cfg():
|
||||
|
||||
@@ -5,10 +5,11 @@ test module for the axolotl.utils.data module
|
||||
import unittest
|
||||
|
||||
from transformers import LlamaTokenizer
|
||||
from utils import enable_hf_offline
|
||||
|
||||
from axolotl.utils.data import encode_pretraining, md5
|
||||
|
||||
from tests.hf_offline_utils import enable_hf_offline
|
||||
|
||||
|
||||
class TestEncodePretraining(unittest.TestCase):
|
||||
"""
|
||||
|
||||
@@ -8,20 +8,21 @@ from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from constants import (
|
||||
ALPACA_MESSAGES_CONFIG_OG,
|
||||
ALPACA_MESSAGES_CONFIG_REVISION,
|
||||
SPECIAL_TOKENS,
|
||||
)
|
||||
from datasets import Dataset
|
||||
from huggingface_hub import snapshot_download
|
||||
from transformers import PreTrainedTokenizer
|
||||
from utils import enable_hf_offline
|
||||
|
||||
from axolotl.utils.data import load_tokenized_prepared_datasets
|
||||
from axolotl.utils.data.rl import load_prepare_preference_datasets
|
||||
from axolotl.utils.dict import DictDefault
|
||||
|
||||
from tests.constants import (
|
||||
ALPACA_MESSAGES_CONFIG_OG,
|
||||
ALPACA_MESSAGES_CONFIG_REVISION,
|
||||
SPECIAL_TOKENS,
|
||||
)
|
||||
from tests.hf_offline_utils import enable_hf_offline
|
||||
|
||||
|
||||
class TestDatasetPreparation:
|
||||
"""Test a configured dataloader."""
|
||||
|
||||
@@ -9,9 +9,7 @@ import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from constants import ALPACA_MESSAGES_CONFIG_REVISION
|
||||
from datasets import Dataset
|
||||
from utils import enable_hf_offline
|
||||
|
||||
from axolotl.utils.config import normalize_config
|
||||
from axolotl.utils.data import prepare_dataset
|
||||
@@ -20,6 +18,9 @@ from axolotl.utils.data.utils import deduplicate_and_log_datasets
|
||||
from axolotl.utils.dict import DictDefault
|
||||
from axolotl.utils.models import load_processor, load_tokenizer
|
||||
|
||||
from tests.constants import ALPACA_MESSAGES_CONFIG_REVISION
|
||||
from tests.hf_offline_utils import enable_hf_offline
|
||||
|
||||
|
||||
def verify_deduplication(actual_dataset, expected_dataset, dataset_name):
|
||||
"""
|
||||
|
||||
@@ -4,7 +4,6 @@ import pytest
|
||||
from datasets import concatenate_datasets, load_dataset
|
||||
from torch.utils.data import DataLoader, RandomSampler
|
||||
from transformers import AutoTokenizer
|
||||
from utils import enable_hf_offline
|
||||
|
||||
from axolotl.datasets import TokenizedPromptDataset
|
||||
from axolotl.prompt_strategies.completion import load
|
||||
@@ -13,6 +12,8 @@ from axolotl.utils.data.utils import drop_long_seq_in_dataset
|
||||
from axolotl.utils.dict import DictDefault
|
||||
from axolotl.utils.samplers import MultipackBatchSampler, get_dataset_lengths
|
||||
|
||||
from tests.hf_offline_utils import enable_hf_offline
|
||||
|
||||
|
||||
@pytest.fixture(name="tokenizer")
|
||||
def fixture_tokenizer():
|
||||
|
||||
@@ -5,12 +5,13 @@ from pathlib import Path
|
||||
|
||||
from datasets import Dataset, load_dataset
|
||||
from transformers import AutoTokenizer
|
||||
from utils import enable_hf_offline
|
||||
|
||||
from axolotl.datasets import ConstantLengthDataset, TokenizedPromptDataset
|
||||
from axolotl.prompt_tokenizers import AlpacaPromptTokenizingStrategy
|
||||
from axolotl.prompters import AlpacaPrompter
|
||||
|
||||
from tests.hf_offline_utils import enable_hf_offline
|
||||
|
||||
|
||||
class TestPacking(unittest.TestCase):
|
||||
"""
|
||||
|
||||
@@ -8,7 +8,6 @@ from pathlib import Path
|
||||
import pytest
|
||||
from datasets import load_dataset
|
||||
from transformers import AddedToken, AutoTokenizer, LlamaTokenizer
|
||||
from utils import enable_hf_offline
|
||||
|
||||
from axolotl.prompt_strategies.alpaca_chat import NoSystemPrompter
|
||||
from axolotl.prompt_strategies.alpaca_w_system import (
|
||||
@@ -24,6 +23,8 @@ from axolotl.prompt_tokenizers import AlpacaPromptTokenizingStrategy
|
||||
from axolotl.prompters import AlpacaPrompter, PromptStyle
|
||||
from axolotl.utils.dict import DictDefault
|
||||
|
||||
from tests.hf_offline_utils import enable_hf_offline
|
||||
|
||||
LOG = logging.getLogger("axolotl")
|
||||
|
||||
test_data = {
|
||||
|
||||
@@ -5,11 +5,12 @@ Test cases for the tokenizer loading
|
||||
import unittest
|
||||
|
||||
import pytest
|
||||
from utils import enable_hf_offline
|
||||
|
||||
from axolotl.utils.dict import DictDefault
|
||||
from axolotl.utils.models import load_tokenizer
|
||||
|
||||
from tests.hf_offline_utils import enable_hf_offline
|
||||
|
||||
|
||||
class TestTokenizers:
|
||||
"""
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
"""
|
||||
test utils for helpers and decorators
|
||||
"""
|
||||
|
||||
import os
|
||||
from functools import wraps
|
||||
|
||||
from huggingface_hub.utils import reset_sessions
|
||||
|
||||
|
||||
def reload_modules(hf_hub_offline):
|
||||
# Force reload of the modules that check this variable
|
||||
import importlib
|
||||
|
||||
import datasets
|
||||
import huggingface_hub.constants
|
||||
|
||||
# Reload the constants module first, as others depend on it
|
||||
importlib.reload(huggingface_hub.constants)
|
||||
huggingface_hub.constants.HF_HUB_OFFLINE = hf_hub_offline
|
||||
importlib.reload(datasets.config)
|
||||
setattr(datasets.config, "HF_HUB_OFFLINE", hf_hub_offline)
|
||||
reset_sessions()
|
||||
|
||||
|
||||
def enable_hf_offline(test_func):
|
||||
"""
|
||||
test decorator that sets HF_HUB_OFFLINE environment variable to True and restores it after the test even if the test fails.
|
||||
:param test_func:
|
||||
:return:
|
||||
"""
|
||||
|
||||
@wraps(test_func)
|
||||
def wrapper(*args, **kwargs):
|
||||
# Save the original value of HF_HUB_OFFLINE environment variable
|
||||
original_hf_offline = os.getenv("HF_HUB_OFFLINE")
|
||||
|
||||
# Set HF_OFFLINE environment variable to True
|
||||
os.environ["HF_HUB_OFFLINE"] = "1"
|
||||
|
||||
reload_modules(True)
|
||||
try:
|
||||
# Run the test function
|
||||
return test_func(*args, **kwargs)
|
||||
finally:
|
||||
# Restore the original value of HF_HUB_OFFLINE environment variable
|
||||
if original_hf_offline is not None:
|
||||
os.environ["HF_HUB_OFFLINE"] = original_hf_offline
|
||||
reload_modules(bool(original_hf_offline))
|
||||
else:
|
||||
del os.environ["HF_HUB_OFFLINE"]
|
||||
reload_modules(False)
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
def disable_hf_offline(test_func):
|
||||
"""
|
||||
test decorator that sets HF_HUB_OFFLINE environment variable to False and restores it after the wrapped func
|
||||
:param test_func:
|
||||
:return:
|
||||
"""
|
||||
|
||||
@wraps(test_func)
|
||||
def wrapper(*args, **kwargs):
|
||||
# Save the original value of HF_HUB_OFFLINE environment variable
|
||||
original_hf_offline = os.getenv("HF_HUB_OFFLINE")
|
||||
|
||||
# Set HF_OFFLINE environment variable to True
|
||||
os.environ["HF_HUB_OFFLINE"] = "0"
|
||||
|
||||
reload_modules(False)
|
||||
try:
|
||||
# Run the test function
|
||||
return test_func(*args, **kwargs)
|
||||
finally:
|
||||
# Restore the original value of HF_HUB_OFFLINE environment variable
|
||||
if original_hf_offline is not None:
|
||||
os.environ["HF_HUB_OFFLINE"] = original_hf_offline
|
||||
reload_modules(bool(original_hf_offline))
|
||||
else:
|
||||
del os.environ["HF_HUB_OFFLINE"]
|
||||
reload_modules(False)
|
||||
|
||||
return wrapper
|
||||
|
||||
Reference in New Issue
Block a user