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. |
Attributes:
| Name | Type | Description |
|---|---|---|
FIXED_RUNTIME_TRAINING_ARGS |
Training arguments fixed by Safe Synthesizer at runtime. |
FIXED_RUNTIME_TRAINING_ARGS = {'num_train_epochs': 1, 'save_strategy': IntervalStrategy.EPOCH, 'logging_steps': 1, 'logging_strategy': IntervalStrategy.STEPS, 'per_device_eval_batch_size': 2, 'optim': 'paged_adamw_32bit', 'bf16': True, 'group_by_length': False, 'ddp_find_unused_parameters': False}
module-attribute
¶
Training arguments fixed by Safe Synthesizer at runtime.
Training duration is controlled by num_input_records_to_sample and the
assembled data_fraction, not by epochs. These values keep the HuggingFace
Trainer behavior stable across CLI and SDK entry points.
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).
Quantized training prepares the model with
prepare_model_for_kbit_training before applying LoRA. Non-quantized
training enables gradient checkpointing, input gradients, and disables
use_cache before wrapping the model.
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 under |
teardown |
Release GPU memory, distributed resources, and trainer state. Idempotent -- safe to call multiple times. |
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
|
Any
|
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
|
Any
|
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
|
Any
|
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.
Validates groupby/orderby columns, resolves auto-config values,
runs time-series preprocessing, and assembles tokenized training examples.
Populates training_examples, dataset_schema,
training_df, 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()
¶
Save the fine-tuning adapter and related artifacts under self.workdir.
Does not release resources -- callers should invoke :meth:teardown
explicitly when they are done with the backend (the abstract contract
on :class:TrainingBackend keeps save and teardown as separate
lifecycle events).
Source code in src/nemo_safe_synthesizer/training/huggingface_backend.py
teardown()
¶
Release GPU memory, distributed resources, and trainer state. Idempotent -- safe to call multiple times.
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. |