WIP: Support table logging for mlflow, too (#1506)
* WIP: Support table logging for mlflow, too Create a `LogPredictionCallback` for both "wandb" and "mlflow" if specified. In `log_prediction_callback_factory`, create a generic table and make it specific only if the newly added `logger` argument is set to "wandb" resp. "mlflow". See https://github.com/OpenAccess-AI-Collective/axolotl/issues/1505 * chore: lint * add additional clause for mlflow as it's optional * Fix circular imports --------- Co-authored-by: Dave Farago <dfarago@innoopract.com> Co-authored-by: Wing Lian <wing.lian@gmail.com>
This commit is contained in:
@@ -36,6 +36,7 @@ from trl.trainer.utils import pad_to_length
|
|||||||
from axolotl.loraplus import create_loraplus_optimizer
|
from axolotl.loraplus import create_loraplus_optimizer
|
||||||
from axolotl.monkeypatch.multipack import SUPPORTED_MULTIPACK_MODEL_TYPES
|
from axolotl.monkeypatch.multipack import SUPPORTED_MULTIPACK_MODEL_TYPES
|
||||||
from axolotl.monkeypatch.relora import ReLoRACallback, ReLoRAScheduler
|
from axolotl.monkeypatch.relora import ReLoRACallback, ReLoRAScheduler
|
||||||
|
from axolotl.utils import is_mlflow_available
|
||||||
from axolotl.utils.callbacks import (
|
from axolotl.utils.callbacks import (
|
||||||
EvalFirstStepCallback,
|
EvalFirstStepCallback,
|
||||||
GPUStatsCallback,
|
GPUStatsCallback,
|
||||||
@@ -71,10 +72,6 @@ except ImportError:
|
|||||||
LOG = logging.getLogger("axolotl.core.trainer_builder")
|
LOG = logging.getLogger("axolotl.core.trainer_builder")
|
||||||
|
|
||||||
|
|
||||||
def is_mlflow_available():
|
|
||||||
return importlib.util.find_spec("mlflow") is not None
|
|
||||||
|
|
||||||
|
|
||||||
def _sanitize_kwargs_for_tagging(tag_names, kwargs=None):
|
def _sanitize_kwargs_for_tagging(tag_names, kwargs=None):
|
||||||
if isinstance(tag_names, str):
|
if isinstance(tag_names, str):
|
||||||
tag_names = [tag_names]
|
tag_names = [tag_names]
|
||||||
@@ -943,7 +940,16 @@ class HFCausalTrainerBuilder(TrainerBuilderBase):
|
|||||||
callbacks = []
|
callbacks = []
|
||||||
if self.cfg.use_wandb and self.cfg.eval_table_size > 0:
|
if self.cfg.use_wandb and self.cfg.eval_table_size > 0:
|
||||||
LogPredictionCallback = log_prediction_callback_factory(
|
LogPredictionCallback = log_prediction_callback_factory(
|
||||||
trainer, self.tokenizer
|
trainer, self.tokenizer, "wandb"
|
||||||
|
)
|
||||||
|
callbacks.append(LogPredictionCallback(self.cfg))
|
||||||
|
if (
|
||||||
|
self.cfg.use_mlflow
|
||||||
|
and is_mlflow_available()
|
||||||
|
and self.cfg.eval_table_size > 0
|
||||||
|
):
|
||||||
|
LogPredictionCallback = log_prediction_callback_factory(
|
||||||
|
trainer, self.tokenizer, "mlflow"
|
||||||
)
|
)
|
||||||
callbacks.append(LogPredictionCallback(self.cfg))
|
callbacks.append(LogPredictionCallback(self.cfg))
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
"""
|
||||||
|
Basic utils for Axolotl
|
||||||
|
"""
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
|
||||||
|
def is_mlflow_available():
|
||||||
|
return importlib.util.find_spec("mlflow") is not None
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import logging
|
|||||||
import os
|
import os
|
||||||
from shutil import copyfile
|
from shutil import copyfile
|
||||||
from tempfile import NamedTemporaryFile
|
from tempfile import NamedTemporaryFile
|
||||||
from typing import TYPE_CHECKING, Dict, List
|
from typing import TYPE_CHECKING, Any, Dict, List
|
||||||
|
|
||||||
import evaluate
|
import evaluate
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@@ -27,7 +27,9 @@ from transformers import (
|
|||||||
)
|
)
|
||||||
from transformers.trainer_utils import PREFIX_CHECKPOINT_DIR, IntervalStrategy
|
from transformers.trainer_utils import PREFIX_CHECKPOINT_DIR, IntervalStrategy
|
||||||
|
|
||||||
|
from axolotl.utils import is_mlflow_available
|
||||||
from axolotl.utils.bench import log_gpu_memory_usage
|
from axolotl.utils.bench import log_gpu_memory_usage
|
||||||
|
from axolotl.utils.config.models.input.v0_4_1 import AxolotlInputConfig
|
||||||
from axolotl.utils.distributed import (
|
from axolotl.utils.distributed import (
|
||||||
barrier,
|
barrier,
|
||||||
broadcast_dict,
|
broadcast_dict,
|
||||||
@@ -540,7 +542,7 @@ def causal_lm_bench_eval_callback_factory(trainer: Trainer, tokenizer):
|
|||||||
return CausalLMBenchEvalCallback
|
return CausalLMBenchEvalCallback
|
||||||
|
|
||||||
|
|
||||||
def log_prediction_callback_factory(trainer: Trainer, tokenizer):
|
def log_prediction_callback_factory(trainer: Trainer, tokenizer, logger: str):
|
||||||
class LogPredictionCallback(TrainerCallback):
|
class LogPredictionCallback(TrainerCallback):
|
||||||
"""Callback to log prediction values during each evaluation"""
|
"""Callback to log prediction values during each evaluation"""
|
||||||
|
|
||||||
@@ -597,15 +599,13 @@ def log_prediction_callback_factory(trainer: Trainer, tokenizer):
|
|||||||
return ranges
|
return ranges
|
||||||
|
|
||||||
def log_table_from_dataloader(name: str, table_dataloader):
|
def log_table_from_dataloader(name: str, table_dataloader):
|
||||||
table = wandb.Table( # type: ignore[attr-defined]
|
table_data: Dict[str, List[Any]] = {
|
||||||
columns=[
|
"id": [],
|
||||||
"id",
|
"Prompt": [],
|
||||||
"Prompt",
|
"Correct Completion": [],
|
||||||
"Correct Completion",
|
"Predicted Completion (model.generate)": [],
|
||||||
"Predicted Completion (model.generate)",
|
"Predicted Completion (trainer.prediction_step)": [],
|
||||||
"Predicted Completion (trainer.prediction_step)",
|
}
|
||||||
]
|
|
||||||
)
|
|
||||||
row_index = 0
|
row_index = 0
|
||||||
|
|
||||||
for batch in tqdm(table_dataloader):
|
for batch in tqdm(table_dataloader):
|
||||||
@@ -709,16 +709,29 @@ def log_prediction_callback_factory(trainer: Trainer, tokenizer):
|
|||||||
) in zip(
|
) in zip(
|
||||||
prompt_texts, completion_texts, predicted_texts, pred_step_texts
|
prompt_texts, completion_texts, predicted_texts, pred_step_texts
|
||||||
):
|
):
|
||||||
table.add_data(
|
table_data["id"].append(row_index)
|
||||||
row_index,
|
table_data["Prompt"].append(prompt_text)
|
||||||
prompt_text,
|
table_data["Correct Completion"].append(completion_text)
|
||||||
completion_text,
|
table_data["Predicted Completion (model.generate)"].append(
|
||||||
prediction_text,
|
prediction_text
|
||||||
pred_step_text,
|
|
||||||
)
|
)
|
||||||
|
table_data[
|
||||||
|
"Predicted Completion (trainer.prediction_step)"
|
||||||
|
].append(pred_step_text)
|
||||||
row_index += 1
|
row_index += 1
|
||||||
|
if logger == "wandb":
|
||||||
|
wandb.run.log({f"{name} - Predictions vs Ground Truth": pd.DataFrame(table_data)}) # type: ignore[attr-defined]
|
||||||
|
elif logger == "mlflow" and is_mlflow_available():
|
||||||
|
import mlflow
|
||||||
|
|
||||||
wandb.run.log({f"{name} - Predictions vs Ground Truth": table}) # type: ignore[attr-defined]
|
tracking_uri = AxolotlInputConfig(
|
||||||
|
**self.cfg.to_dict()
|
||||||
|
).mlflow_tracking_uri
|
||||||
|
mlflow.log_table(
|
||||||
|
data=table_data,
|
||||||
|
artifact_file="PredictionsVsGroundTruth.json",
|
||||||
|
tracking_uri=tracking_uri,
|
||||||
|
)
|
||||||
|
|
||||||
if is_main_process():
|
if is_main_process():
|
||||||
log_table_from_dataloader("Eval", eval_dataloader)
|
log_table_from_dataloader("Eval", eval_dataloader)
|
||||||
|
|||||||
Reference in New Issue
Block a user