huggingface_backend
huggingface_backend
¶
HuggingFace Trainer backend for LoRA fine-tuning.
Classes:
| Name | Description |
|---|---|
HuggingFaceBackend |
Training backend built on the HuggingFace |
Functions:
| Name | Description |
|---|---|
preprocess_logits_for_metrics |
Reduce logits to argmax predictions to avoid OOM during evaluation. |
compute_metrics |
Compute evaluation metrics from forward-pass losses. |
HuggingFaceBackend(*args, **kwargs)
¶
Bases: TrainingBackend
Training backend built on the HuggingFace Trainer.
Handles model loading (AutoModelForCausalLM), LoRA/QLoRA wrapping,
RoPE scaling, optional differential-privacy training via
OpacusDPTrainer,
and artifact persistence (adapter, schema, metadata).
Methods:
| Name | Description |
|---|---|
prepare_config |
Set common model arguments for initializing a model. |
maybe_quantize |
Apply LoRA wrapping (and optional k-bit quantization) to the model. |
load_model |
Load an |
prepare_params |
Prepare training parameters and create the trainer. |
prepare_training_data |
Validate, preprocess, and tokenize the training dataset. |
train |
Run the full training pipeline and populate |
save_model |
Save the fine-tuning adapter and related artifacts to the given path. |
delete_trainable_model |
Delete the trainable model, trainer, and clean up GPU memory and distributed resources. |
info |
Print a summary of key trainer attributes to stdout. |
Source code in src/nemo_safe_synthesizer/training/huggingface_backend.py
prepare_config(add_max_memory=True, **kwargs)
¶
Set common model arguments for initializing a model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
add_max_memory
|
bool
|
Whether to add max_memory to the model arguments. |
True
|
**kwargs
|
Additional keyword arguments, overriding default arguments when set. |
{}
|
Source code in src/nemo_safe_synthesizer/training/huggingface_backend.py
maybe_quantize(**quant_params)
¶
Apply LoRA wrapping (and optional k-bit quantization) to the model.
Source code in src/nemo_safe_synthesizer/training/huggingface_backend.py
load_model(**model_args)
¶
Load an AutoModelForCausalLM instance with specified arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**model_args
|
Additional keyword arguments for model configuration,
passed directly to |
{}
|
Source code in src/nemo_safe_synthesizer/training/huggingface_backend.py
prepare_params(**training_args)
¶
Prepare training parameters and create the trainer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**training_args
|
Additional training arguments (currently unused but kept for API compatibility). |
{}
|
Source code in src/nemo_safe_synthesizer/training/huggingface_backend.py
prepare_training_data()
¶
Validate, preprocess, and tokenize the training dataset.
Runs auto-config resolution, time-series processing, groupby /
orderby validation, and assembles tokenized training examples.
Populates training_examples, dataset_schema,
df_train, and data_fraction.
Raises:
| Type | Description |
|---|---|
DataError
|
If the training dataset is missing or malformed. |
Source code in src/nemo_safe_synthesizer/training/huggingface_backend.py
train(**training_args)
¶
Run the full training pipeline and populate results.
Sequentially calls prepare_training_data,
prepare_params, trains the model, and saves artifacts.
Source code in src/nemo_safe_synthesizer/training/huggingface_backend.py
save_model(delete_trainable_model=True)
¶
Save the fine-tuning adapter and related artifacts to the given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
delete_trainable_model
|
bool
|
If True, delete the model from memory after saving. |
True
|
Source code in src/nemo_safe_synthesizer/training/huggingface_backend.py
delete_trainable_model()
¶
Delete the trainable model, trainer, and clean up GPU memory and distributed resources.
Source code in src/nemo_safe_synthesizer/training/huggingface_backend.py
info()
¶
Print a summary of key trainer attributes to stdout.
Source code in src/nemo_safe_synthesizer/training/huggingface_backend.py
preprocess_logits_for_metrics(logits, labels)
¶
Reduce logits to argmax predictions to avoid OOM during evaluation.
The default Trainer stores full logit tensors across evaluation batches, which can exhaust GPU memory on large datasets. This callback replaces them with predicted token IDs immediately after the forward pass.
See: https://discuss.huggingface.co/t/cuda-out-of-memory-when-using-trainer-with-compute-metrics/2941/13
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits
|
tuple[Tensor, ...]
|
Tuple of logits tensors from the model output. |
required |
labels
|
Tensor
|
Ground truth labels tensor. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor]
|
Tuple of |
Source code in src/nemo_safe_synthesizer/training/huggingface_backend.py
compute_metrics(eval_preds)
¶
Compute evaluation metrics from forward-pass losses.
Metrics returned:
- mean cross-entropy loss (
eval_loss) -- average of per-batch losses collected during the evaluation loop.
The per-batch losses are pre-computed during the forward pass
(via include_for_metrics).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eval_preds
|
EvalPrediction
|
Evaluation predictions object whose |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Dictionary mapping metric names to values. |