slicktune¶
SlickML fine-tuning toolkit for LLMs.
Submodules¶
Attributes¶
Classes¶
AdaLoRA: adaptive-rank LoRA that prunes toward |
|
Direct Preference Optimization (TRL |
|
DoRA (Weight-Decomposed LoRA) PEFT strategy. |
|
Result of a completed |
|
Full fine-tuning: update all model parameters. |
|
Holdout negative-log-likelihood / perplexity summary. |
|
Aggregate judge outcomes. |
|
Outcome of judging one generation. |
|
Kahneman–Tversky Optimization (TRL |
|
Ask an LLM to score a completion from 0–10, normalized to |
|
LoRA PEFT strategy (default production PEFT path). |
|
Collect and persist metrics across a run. |
|
Odds Ratio Preference Optimization (TRL experimental ORPO). |
|
QLoRA strategy: 4-bit quantized base + LoRA adapters. |
|
Supervised fine-tuning on instruction / chat pairs. |
|
Deterministic judge: pass if |
|
Snapshot of metrics for one training run. |
|
Composable fine-tuning entry point. |
Functions¶
|
Compute mean NLL and perplexity on a holdout SFT JSONL / dataset. |
|
Generate replies for probe prompts and score them with |
Package Contents¶
- class slicktune.AdaLoRAStrategy[source]¶
Bases:
slicktune.types.StrategyAdaLoRA: adaptive-rank LoRA that prunes toward
target_r.- Parameters:
target_r (int, optional) – Average target rank budget, by default 8.
init_r (int, optional) – Initial rank before pruning, by default 12.
alpha (int, optional) – LoRA scaling alpha, by default 32.
dropout (float, optional) – LoRA dropout probability, by default 0.05.
target_modules (list[str] | str | None, optional) – Modules to adapt, by default
"all-linear".bias ({“none”, “all”, “lora_only”}, optional) – Bias training mode, by default
"none".tinit (int, optional) – Warmup steps before rank allocation, by default 0.
tfinal (int, optional) – Final fine-tuning steps at fixed rank, by default 0.
deltaT (int, optional) – Rank allocation interval, by default 10.
total_step (int, optional) – Expected optimizer steps (required by PEFT AdaLoRA), by default 1000. Prefer setting this close to
steps_per_epoch * epochs.
- __slots__ = ()¶
- apply(model: Any) Any[source]¶
Attach AdaLoRA adapters to
model.- Parameters:
model (Any) – Hugging Face causal LM.
- Returns:
Any – PEFT-wrapped AdaLoRA model.
- Raises:
ValueError – If
total_stepis not positive.
- bias: BiasType = 'none'¶
- class slicktune.DPOObjective[source]¶
Bases:
slicktune.types.ObjectiveDirect Preference Optimization (TRL
DPOTrainer).- Parameters:
beta (float, optional) – KL penalty coefficient, by default 0.1.
loss_type (str, optional) – TRL DPO loss type, by default
"sigmoid".
- __slots__ = ()¶
- class slicktune.DoRAStrategy[source]¶
Bases:
slicktune.types.StrategyDoRA (Weight-Decomposed LoRA) PEFT strategy.
Same knobs as LoRA with
use_dora=Truefor magnitude/direction decomposition.- Parameters:
r (int, optional) – LoRA rank, by default 16.
alpha (int, optional) – LoRA scaling alpha, by default 32.
dropout (float, optional) – LoRA dropout probability, by default 0.05.
target_modules (list[str] | str | None, optional) – Modules to adapt, by default
"all-linear".bias ({“none”, “all”, “lora_only”}, optional) – Bias training mode, by default
"none".
- __slots__ = ()¶
- apply(model: Any) Any[source]¶
Attach DoRA adapters to
model.- Parameters:
model (Any) – Hugging Face causal LM.
- Returns:
Any – PEFT-wrapped DoRA model.
- bias: BiasType = 'none'¶
- class slicktune.FitResult[source]¶
Result of a completed
Tuner.fit()call.- Parameters:
output_dir (Path) – Directory containing the saved adapter / model and metrics.
metrics (TrainingMetrics) – Collected training metrics.
model (Any) – Trained model (PEFT or full).
tokenizer (PreTrainedTokenizerBase) – Tokenizer used during training.
- metrics: slicktune.metrics.TrainingMetrics¶
- model: Any¶
- output_dir: pathlib.Path¶
- tokenizer: transformers.PreTrainedTokenizerBase¶
- class slicktune.FullStrategy[source]¶
Bases:
slicktune.types.StrategyFull fine-tuning: update all model parameters.
- Parameters:
None
- __slots__ = ()¶
- apply(model: Any) Any[source]¶
Enable gradients on all parameters.
- Parameters:
model (Any) – Hugging Face causal LM.
- Returns:
Any – The same model with
requires_grad=Trueon all params.
- class slicktune.HoldoutEvalResult[source]¶
Holdout negative-log-likelihood / perplexity summary.
- Parameters:
eval_loss (float) – Mean token NLL on the holdout set.
perplexity (float) –
exp(eval_loss).num_examples (int) – Number of evaluated examples.
- class slicktune.JudgeReport[source]¶
Aggregate judge outcomes.
- Parameters:
results (list[JudgeResult]) – Per-example judgments.
- property mean_score: float¶
Return mean score across judged examples.
- Returns:
float – Mean in
[0, 1], or0.0when empty.
- results: list[JudgeResult] = []¶
- class slicktune.JudgeResult[source]¶
Outcome of judging one generation.
- Parameters:
prompt (str) – User prompt.
generation (str) – Model completion.
score (float) – Score in
[0, 1].rationale (str) – Short explanation from the judge.
- class slicktune.KTOObjective[source]¶
Bases:
slicktune.types.ObjectiveKahneman–Tversky Optimization (TRL
KTOTrainer).- Parameters:
beta (float, optional) – KL penalty coefficient, by default 0.1.
desirable_weight (float, optional) – Weight for desirable (
label=True) examples, by default 1.0.undesirable_weight (float, optional) – Weight for undesirable (
label=False) examples, by default 1.0.
- __slots__ = ()¶
- class slicktune.LLMJudge[source]¶
Bases:
JudgeAsk an LLM to score a completion from 0–10, normalized to
[0, 1].Uses digit-constrained decoding so small models cannot wander into
Yes/Trueinstead of a numeric score.- Parameters:
model (Any) – Causal LM used as the judge (often the same trained model).
tokenizer (PreTrainedTokenizerBase) – Matching tokenizer.
max_new_tokens (int, optional) – Unused for constrained scoring (kept for API compatibility), by default 2.
- __slots__ = ()¶
- judge(*, prompt: str, generation: str, **context: Any) JudgeResult[source]¶
Score via an LLM rubric prompt.
- Parameters:
prompt (str) – User prompt.
generation (str) – Model completion.
**context (Any) – Optional
criteriastring.
- Returns:
JudgeResult – Normalized score parsed from the judge reply.
- model: Any¶
- tokenizer: transformers.PreTrainedTokenizerBase¶
- class slicktune.LoRAStrategy[source]¶
Bases:
slicktune.types.StrategyLoRA PEFT strategy (default production PEFT path).
- Parameters:
r (int, optional) – LoRA rank, by default 16.
alpha (int, optional) – LoRA scaling alpha, by default 32.
dropout (float, optional) – LoRA dropout probability, by default 0.05.
target_modules (list[str] | str | None, optional) – Modules to adapt.
"all-linear"lets PEFT discover linear layers, by default"all-linear".bias ({“none”, “all”, “lora_only”}, optional) – Bias training mode passed to PEFT, by default
"none".
- __slots__ = ()¶
- apply(model: Any) Any[source]¶
Attach LoRA adapters to
model.- Parameters:
model (Any) – Hugging Face causal LM.
- Returns:
Any – PEFT-wrapped model.
- bias: BiasType = 'none'¶
- class slicktune.MetricsTracker[source]¶
Collect and persist metrics across a run.
- Parameters:
output_dir (str or Path) – Directory where
metrics.jsonis written.
- load() TrainingMetrics[source]¶
Load metrics previously written by
save().- Returns:
TrainingMetrics – Restored metrics object.
- Raises:
FileNotFoundError – If
metrics.jsonis missing.
- output_dir: pathlib.Path¶
- save(metrics: TrainingMetrics) pathlib.Path[source]¶
Write metrics to
metrics.json.- Parameters:
metrics (TrainingMetrics) – Metrics snapshot to persist.
- Returns:
Path – Path to the written JSON file.
- class slicktune.ORPOObjective[source]¶
Bases:
slicktune.types.ObjectiveOdds Ratio Preference Optimization (TRL experimental ORPO).
- Parameters:
beta (float, optional) – Odds-ratio penalty coefficient, by default 0.1.
- __slots__ = ()¶
- class slicktune.QLoRAStrategy[source]¶
Bases:
slicktune.types.StrategyQLoRA strategy: 4-bit quantized base + LoRA adapters.
Requires CUDA and optional extra
slicktune[qlora](bitsandbytes).- Parameters:
r (int, optional) – LoRA rank, by default 16.
alpha (int, optional) – LoRA scaling alpha, by default 32.
dropout (float, optional) – LoRA dropout probability, by default 0.05.
target_modules (list[str] | str | None, optional) – Modules to adapt, by default
"all-linear".bias ({“none”, “all”, “lora_only”}, optional) – Bias training mode, by default
"none".
- __slots__ = ()¶
- apply(model: Any) Any[source]¶
Prepare a 4-bit model and attach LoRA adapters.
- Parameters:
model (Any) – Quantized Hugging Face causal LM.
- Returns:
Any – PEFT-wrapped QLoRA model.
- bias: BiasType = 'none'¶
- load_kwargs() dict[str, Any][source]¶
Return bitsandbytes 4-bit quantization config for model load.
- Returns:
dict[str, Any] –
quantization_configsuitable forfrom_pretrained.- Raises:
ImportError – If
bitsandbytesis not installed.RuntimeError – If CUDA is not available.
- class slicktune.SFTObjective[source]¶
Bases:
slicktune.types.ObjectiveSupervised fine-tuning on instruction / chat pairs.
- __slots__ = ()¶
- class slicktune.SubstringJudge[source]¶
Bases:
JudgeDeterministic judge: pass if
must_containappears in the generation.- __slots__ = ()¶
- judge(*, prompt: str, generation: str, **context: Any) JudgeResult[source]¶
Score via substring match.
- Parameters:
prompt (str) – User prompt.
generation (str) – Model completion.
**context (Any) – Must include
must_contain.
- Returns:
JudgeResult – Score
1.0or0.0.
- class slicktune.TrainingMetrics[source]¶
Snapshot of metrics for one training run.
- Parameters:
strategy (str) – Parameter strategy name (e.g.
"lora").objective (str) – Objective name (e.g.
"sft").model_id (str) – Base model id or path.
train_loss (float | None, optional) – Final reported training loss, by default None.
eval_loss (float | None, optional) – Final evaluation loss if computed, by default None.
train_runtime_sec (float | None, optional) – Wall-clock training time in seconds, by default None.
train_samples_per_second (float | None, optional) – Throughput reported by the trainer, by default None.
trainable_params (int | None, optional) – Number of trainable parameters, by default None.
total_params (int | None, optional) – Total parameter count, by default None.
probe_pass_rate (float | None, optional) – Fraction of probe checks that passed after training, by default None.
eval_perplexity (float | None, optional) – Holdout perplexity from Phase-2 eval, by default None.
judge_score (float | None, optional) – Mean judge score in
[0, 1], by default None.extras (dict[str, Any], optional) – Additional key/value metrics, by default empty.
- class slicktune.Tuner[source]¶
Composable fine-tuning entry point.
- Parameters:
model_id (str) – Hugging Face model id or local path.
strategy (Strategy) – Parameter-update strategy (LoRA, DoRA, AdaLoRA, QLoRA, full, …).
objective (Objective) – Training objective (SFT, DPO, ORPO, or KTO).
output_dir (str or Path) – Where checkpoints, adapter weights, and metrics are written.
max_seq_length (int, optional) – Maximum sequence length, by default 512.
num_train_epochs (float, optional) – Number of epochs, by default 3.0.
per_device_train_batch_size (int, optional) – Batch size per device, by default 1.
gradient_accumulation_steps (int, optional) – Gradient accumulation steps, by default 4.
learning_rate (float, optional) – Learning rate, by default 2e-4.
logging_steps (int, optional) – Logging frequency, by default 1.
save_steps (int, optional) – Checkpoint frequency, by default 50.
seed (int, optional) – Random seed, by default 42.
eval_data (str or Path or Dataset or None, optional) – Optional holdout SFT JSONL/dataset for perplexity after fit.
probe_path (str or Path or None, optional) – Optional probe JSONL judged after fit (substring or custom judge).
judge (Judge or None, optional) – Judge used with
probe_path; defaults toSubstringJudge.
- eval_data: str | pathlib.Path | datasets.Dataset | None = None¶
- fit(data: str | pathlib.Path | datasets.Dataset) FitResult[source]¶
Run fine-tuning for the configured objective.
- Parameters:
data (str or Path or Dataset) – Path to objective-specific JSONL, or an in-memory dataset.
- Returns:
FitResult – Trained artifacts and metrics.
- Raises:
TypeError – If the objective is not supported.
ValueError – If required dataset columns are missing.
- judge: slicktune.eval.Judge | None = None¶
- objective: slicktune.types.Objective¶
- output_dir: str | pathlib.Path¶
- probe_path: str | pathlib.Path | None = None¶
- strategy: slicktune.types.Strategy¶
- slicktune.__version__¶
- slicktune.compute_holdout_perplexity(*, model: Any, tokenizer: transformers.PreTrainedTokenizerBase, data: str | pathlib.Path | datasets.Dataset, max_length: int = 512) HoldoutEvalResult[source]¶
Compute mean NLL and perplexity on a holdout SFT JSONL / dataset.
- Parameters:
model (Any) – Causal LM (base or PEFT).
tokenizer (PreTrainedTokenizerBase) – Tokenizer.
data (str or Path or Dataset) – Holdout SFT data with
messages(or loadable JSONL).max_length (int, optional) – Truncation length, by default 512.
- Returns:
HoldoutEvalResult – Loss / perplexity summary.
- Raises:
ValueError – If no examples are available.
- slicktune.run_judge_on_probes(*, model: Any, tokenizer: transformers.PreTrainedTokenizerBase, probe_path: str | pathlib.Path, judge: Judge, max_new_tokens: int = 128) JudgeReport[source]¶
Generate replies for probe prompts and score them with
judge.- Parameters:
model (Any) – Model under evaluation.
tokenizer (PreTrainedTokenizerBase) – Tokenizer.
probe_path (str or Path) – Probe JSONL (
prompt,must_contain).judge (Judge) – Scoring strategy.
max_new_tokens (int, optional) – Generation length, by default 128.
- Returns:
JudgeReport – Aggregate scores.