bertuner 0.1.1__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: bertuner
3
- Version: 0.1.1
3
+ Version: 0.2.0
4
4
  Summary: Hyperparameter optimization and fine-tuning for BERT-style text classifiers (Optuna + MLflow), with long-context ModernBERT support
5
5
  Author-email: elemets <alafunnell@gmail.com>
6
6
  License: MIT
@@ -24,6 +24,7 @@ Requires-Dist: torch>=2.0
24
24
  Requires-Dist: transformers>=4.48
25
25
  Requires-Dist: numpy>=1.24
26
26
  Requires-Dist: pandas>=2.0
27
+ Requires-Dist: matplotlib>=3.7
27
28
  Requires-Dist: scikit-learn>=1.3
28
29
  Requires-Dist: optuna>=3.0
29
30
  Requires-Dist: mlflow>=2.9
@@ -81,6 +82,9 @@ classifier = BERTuneClassifier(
81
82
  text_feature="text_col", # column containing the text
82
83
  target_cols=["label_col"], # one column = single-label
83
84
  max_length=512,
85
+ precision="auto", # BF16/FP16 on supported CUDA, else FP32
86
+ retry_nonfinite_in_fp32=True, # retry verified NaN/Inf failures once
87
+ max_grad_norm=1.0, # explicit gradient clipping threshold
84
88
  )
85
89
 
86
90
  # 2. Configure (optional: uses defaults if called without arguments)
@@ -105,6 +109,24 @@ Multi-label classification: pass several target columns — `target_cols=["l1",
105
109
 
106
110
  Grouped data (e.g. multiple notes per patient): pass `group_key="patient_id"` and the train/val/test split guarantees no group leaks across splits.
107
111
 
112
+ ### Numerical-stability recovery
113
+
114
+ Every attempt checks logits, loss, gradients before the optimizer step, and evaluation predictions for NaN/Inf. When a mixed-precision attempt becomes non-finite, BERTuner deletes its checkpoints, reloads the pretrained model, resets the seed, and retries the same Optuna trial and hyperparameters once in FP32. A second numerical failure prunes an optimization trial; final-model training raises `NonFiniteTrainingError`. Invalid labels, class weights, OOM errors, and other exceptions never trigger the retry.
115
+
116
+ `precision` accepts `"auto"`, `"fp32"`, `"bf16"`, or `"fp16"`. Explicit unsupported precision raises before training. Set `retry_nonfinite_in_fp32=False` to disable recovery, `max_grad_norm=None` to disable clipping, or `class_weight_warning_threshold=None` to disable warnings for large finite class weights. Saved `bertuner_config.json` records requested/effective precision and fallback status.
117
+
118
+ ### Metrics logged to MLflow
119
+
120
+ Final runs log one canonical metric set for both `Validation_*` and `Test_*`:
121
+
122
+ - **Binary:** accuracy, balanced accuracy, precision, recall, specificity, F1, Matthews correlation coefficient (MCC), average precision, AUROC, Brier score, and log loss.
123
+ - **Multiclass:** accuracy, balanced accuracy, macro and weighted precision/recall/F1, MCC, macro and weighted average precision/AUROC, and log loss.
124
+ - **Multi-label:** subset accuracy, Hamming loss/accuracy, micro/macro/sample precision/recall/F1 and Jaccard, micro MCC, micro/macro average precision and AUROC, Brier score, and log loss.
125
+
126
+ The optimized binary or per-label decision threshold is logged as a parameter. Multiclass runs log `decision_rule=argmax`. Training-time `eval_*` metrics are not duplicated in MLflow; their losses remain visible in the `plots/training_vs_evaluation_loss.png` artifact.
127
+
128
+ When optimizing a lower-is-better metric such as `log_loss`, pass `greater_is_better=False`; Optuna and best-checkpoint selection will both minimize it.
129
+
108
130
  ## Customizing the hyperparameter search
109
131
 
110
132
  Two things are configurable: **which models** are searched and **which hyperparameters** with what ranges.
@@ -41,6 +41,9 @@ classifier = BERTuneClassifier(
41
41
  text_feature="text_col", # column containing the text
42
42
  target_cols=["label_col"], # one column = single-label
43
43
  max_length=512,
44
+ precision="auto", # BF16/FP16 on supported CUDA, else FP32
45
+ retry_nonfinite_in_fp32=True, # retry verified NaN/Inf failures once
46
+ max_grad_norm=1.0, # explicit gradient clipping threshold
44
47
  )
45
48
 
46
49
  # 2. Configure (optional: uses defaults if called without arguments)
@@ -65,6 +68,24 @@ Multi-label classification: pass several target columns — `target_cols=["l1",
65
68
 
66
69
  Grouped data (e.g. multiple notes per patient): pass `group_key="patient_id"` and the train/val/test split guarantees no group leaks across splits.
67
70
 
71
+ ### Numerical-stability recovery
72
+
73
+ Every attempt checks logits, loss, gradients before the optimizer step, and evaluation predictions for NaN/Inf. When a mixed-precision attempt becomes non-finite, BERTuner deletes its checkpoints, reloads the pretrained model, resets the seed, and retries the same Optuna trial and hyperparameters once in FP32. A second numerical failure prunes an optimization trial; final-model training raises `NonFiniteTrainingError`. Invalid labels, class weights, OOM errors, and other exceptions never trigger the retry.
74
+
75
+ `precision` accepts `"auto"`, `"fp32"`, `"bf16"`, or `"fp16"`. Explicit unsupported precision raises before training. Set `retry_nonfinite_in_fp32=False` to disable recovery, `max_grad_norm=None` to disable clipping, or `class_weight_warning_threshold=None` to disable warnings for large finite class weights. Saved `bertuner_config.json` records requested/effective precision and fallback status.
76
+
77
+ ### Metrics logged to MLflow
78
+
79
+ Final runs log one canonical metric set for both `Validation_*` and `Test_*`:
80
+
81
+ - **Binary:** accuracy, balanced accuracy, precision, recall, specificity, F1, Matthews correlation coefficient (MCC), average precision, AUROC, Brier score, and log loss.
82
+ - **Multiclass:** accuracy, balanced accuracy, macro and weighted precision/recall/F1, MCC, macro and weighted average precision/AUROC, and log loss.
83
+ - **Multi-label:** subset accuracy, Hamming loss/accuracy, micro/macro/sample precision/recall/F1 and Jaccard, micro MCC, micro/macro average precision and AUROC, Brier score, and log loss.
84
+
85
+ The optimized binary or per-label decision threshold is logged as a parameter. Multiclass runs log `decision_rule=argmax`. Training-time `eval_*` metrics are not duplicated in MLflow; their losses remain visible in the `plots/training_vs_evaluation_loss.png` artifact.
86
+
87
+ When optimizing a lower-is-better metric such as `log_loss`, pass `greater_is_better=False`; Optuna and best-checkpoint selection will both minimize it.
88
+
68
89
  ## Customizing the hyperparameter search
69
90
 
70
91
  Two things are configurable: **which models** are searched and **which hyperparameters** with what ranges.