Upload oss-jev-train for hf jobs uv run
Browse files- configs/jobs-a10g-score-kl.yaml +40 -0
- oss_jev_train/__init__.py +1 -1
- oss_jev_train/code_loss.py +89 -4
- oss_jev_train/dataset.py +26 -5
- oss_jev_train/export_ckpt.py +12 -0
- oss_jev_train/train.py +70 -3
- pyproject.toml +1 -1
configs/jobs-a10g-score-kl.yaml
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Score KL + ordinal continue from noul-bin (v0.1.8). Distinct Hub id —
|
| 2 |
+
# never overwrite main / 10k / noul-aug / noul-bin / noul-v2.
|
| 3 |
+
# Soft-KL to Jev full level dist + ordinal EMD/MAE (off-by-3 > 1).
|
| 4 |
+
# Light Noul hold (no heavy aug / binary head) so Score is the lever.
|
| 5 |
+
model_id: gyanbhambhani/oss-jev-qwen05-decision-noul-bin
|
| 6 |
+
mode: one_token
|
| 7 |
+
max_length: 1024
|
| 8 |
+
batch_size: 2
|
| 9 |
+
learning_rate: 1.0e-5
|
| 10 |
+
# Same short-continue budget as noul-aug / noul-bin
|
| 11 |
+
max_steps: 2000
|
| 12 |
+
max_train_rows: null
|
| 13 |
+
seed: 42
|
| 14 |
+
fp16: false
|
| 15 |
+
bf16: true
|
| 16 |
+
logging_steps: 50
|
| 17 |
+
save_total_limit: 2
|
| 18 |
+
report_to: none
|
| 19 |
+
# Soft CE = KL to teacher probs (Score force-soft below)
|
| 20 |
+
use_soft_ce: true
|
| 21 |
+
balance_noul: true
|
| 22 |
+
constrained_codes: true
|
| 23 |
+
# Mild Noul hold from noul-bin (do not re-run noul-v2 levers)
|
| 24 |
+
noul_n_frac: 0.50
|
| 25 |
+
noul_mix_weight: 1.0
|
| 26 |
+
noul_hard_ce: false
|
| 27 |
+
noul_loss_weight: 1.0
|
| 28 |
+
noul_n_sample_weight: 1.0
|
| 29 |
+
noul_logit_bias_n: 0.0
|
| 30 |
+
noul_random_codes: false
|
| 31 |
+
noul_negation_pairs: false
|
| 32 |
+
noul_consistency_weight: 0.0
|
| 33 |
+
noul_binary_head: false
|
| 34 |
+
noul_binary_head_weight: 1.0
|
| 35 |
+
noul_binary_balanced: false
|
| 36 |
+
# Score KL + ordinal (NEW)
|
| 37 |
+
score_force_soft: true
|
| 38 |
+
score_ordinal_weight: 1.0
|
| 39 |
+
score_loss_weight: 2.0
|
| 40 |
+
score_mix_weight: 3.0
|
oss_jev_train/__init__.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
"""OSS Jev one-token / decision-head training (HF Jobs ready)."""
|
| 2 |
|
| 3 |
-
__version__ = "0.1.
|
| 4 |
SCHEMA_VERSION = "oss-jev-distill-v1"
|
| 5 |
# Default smoke base; prefer manifest.base_tokenizer_hint for real runs.
|
| 6 |
DEFAULT_MODEL_ID = "Qwen/Qwen2.5-0.5B-Instruct"
|
|
|
|
| 1 |
"""OSS Jev one-token / decision-head training (HF Jobs ready)."""
|
| 2 |
|
| 3 |
+
__version__ = "0.1.8"
|
| 4 |
SCHEMA_VERSION = "oss-jev-distill-v1"
|
| 5 |
# Default smoke base; prefer manifest.base_tokenizer_hint for real runs.
|
| 6 |
DEFAULT_MODEL_ID = "Qwen/Qwen2.5-0.5B-Instruct"
|
oss_jev_train/code_loss.py
CHANGED
|
@@ -4,8 +4,8 @@ Full-vocab next-token CE left relative Y/N logits dominated by the
|
|
| 4 |
base prior (eval collapse: all Noul → Y). Scoring only code tokens
|
| 5 |
aligns train with infer.
|
| 6 |
|
| 7 |
-
Also: Noul consistency (P(yes|Q)+P(yes|¬Q)→1)
|
| 8 |
-
head CE on last-token hidden.
|
| 9 |
"""
|
| 10 |
|
| 11 |
from __future__ import annotations
|
|
@@ -32,11 +32,14 @@ def code_token_loss(
|
|
| 32 |
*,
|
| 33 |
use_soft: bool,
|
| 34 |
sample_weight: torch.Tensor | None = None,
|
|
|
|
| 35 |
) -> torch.Tensor:
|
| 36 |
-
"""Softmax CE over allowed code ids at the answer position.
|
| 37 |
|
| 38 |
``logits``: [B, T, V]; ``labels``: [B, T] with -100 mask (gold at
|
| 39 |
the answer index). HF shift: predict token t from logits[:, t-1].
|
|
|
|
|
|
|
| 40 |
"""
|
| 41 |
shift_logits = logits[:, :-1, :].contiguous()
|
| 42 |
shift_labels = labels[:, 1:].contiguous()
|
|
@@ -53,7 +56,10 @@ def code_token_loss(
|
|
| 53 |
continue
|
| 54 |
ids = code_token_ids[b, :k].long()
|
| 55 |
code_logits = shift_logits[b, p, ids]
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
target = soft_labels[b, :k].float()
|
| 58 |
target = target / target.sum().clamp_min(1e-8)
|
| 59 |
log_p = F.log_softmax(code_logits, dim=-1)
|
|
@@ -83,6 +89,63 @@ def code_token_loss(
|
|
| 83 |
return (stacked * w).sum() / w.sum().clamp_min(1e-8)
|
| 84 |
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
def code_yes_probs(
|
| 87 |
logits: torch.Tensor,
|
| 88 |
labels: torch.Tensor,
|
|
@@ -158,6 +221,7 @@ def _build_trainer_class() -> type:
|
|
| 158 |
binary_head: Any | None = None,
|
| 159 |
binary_head_weight: float = 0.0,
|
| 160 |
binary_head_balanced: bool = False,
|
|
|
|
| 161 |
**kwargs: Any,
|
| 162 |
) -> None:
|
| 163 |
self._custom_train_sampler = train_sampler
|
|
@@ -167,6 +231,7 @@ def _build_trainer_class() -> type:
|
|
| 167 |
self.binary_head = binary_head
|
| 168 |
self.binary_head_weight = float(binary_head_weight)
|
| 169 |
self.binary_head_balanced = bool(binary_head_balanced)
|
|
|
|
| 170 |
|
| 171 |
def _get_train_sampler(self, train_dataset: Any = None):
|
| 172 |
if self._custom_train_sampler is not None:
|
|
@@ -196,6 +261,8 @@ def _build_trainer_class() -> type:
|
|
| 196 |
inputs.pop("label_id", None)
|
| 197 |
inputs.pop("has_soft", None)
|
| 198 |
is_noul = inputs.pop("is_noul", None)
|
|
|
|
|
|
|
| 199 |
|
| 200 |
labels = inputs.get("labels")
|
| 201 |
outputs = model(**inputs)
|
|
@@ -207,8 +274,26 @@ def _build_trainer_class() -> type:
|
|
| 207 |
soft_labels,
|
| 208 |
use_soft=self.use_soft_ce,
|
| 209 |
sample_weight=sample_weight,
|
|
|
|
| 210 |
)
|
| 211 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
# Negation consistency + CE on ¬Q (dual forward).
|
| 213 |
if (
|
| 214 |
self.consistency_weight > 0
|
|
|
|
| 4 |
base prior (eval collapse: all Noul → Y). Scoring only code tokens
|
| 5 |
aligns train with infer.
|
| 6 |
|
| 7 |
+
Also: Noul consistency (P(yes|Q)+P(yes|¬Q)→1), optional binary
|
| 8 |
+
head CE on last-token hidden, and Score ordinal EMD (levels ordered).
|
| 9 |
"""
|
| 10 |
|
| 11 |
from __future__ import annotations
|
|
|
|
| 32 |
*,
|
| 33 |
use_soft: bool,
|
| 34 |
sample_weight: torch.Tensor | None = None,
|
| 35 |
+
force_soft_mask: torch.Tensor | None = None,
|
| 36 |
) -> torch.Tensor:
|
| 37 |
+
"""Softmax CE / soft-KL over allowed code ids at the answer position.
|
| 38 |
|
| 39 |
``logits``: [B, T, V]; ``labels``: [B, T] with -100 mask (gold at
|
| 40 |
the answer index). HF shift: predict token t from logits[:, t-1].
|
| 41 |
+
Soft path is KL(teacher‖student) up to H(teacher).
|
| 42 |
+
``force_soft_mask``: per-row override to soft-KL (Score rows).
|
| 43 |
"""
|
| 44 |
shift_logits = logits[:, :-1, :].contiguous()
|
| 45 |
shift_labels = labels[:, 1:].contiguous()
|
|
|
|
| 56 |
continue
|
| 57 |
ids = code_token_ids[b, :k].long()
|
| 58 |
code_logits = shift_logits[b, p, ids]
|
| 59 |
+
row_soft = use_soft
|
| 60 |
+
if force_soft_mask is not None and int(force_soft_mask[b].item()) > 0:
|
| 61 |
+
row_soft = True
|
| 62 |
+
if row_soft:
|
| 63 |
target = soft_labels[b, :k].float()
|
| 64 |
target = target / target.sum().clamp_min(1e-8)
|
| 65 |
log_p = F.log_softmax(code_logits, dim=-1)
|
|
|
|
| 89 |
return (stacked * w).sum() / w.sum().clamp_min(1e-8)
|
| 90 |
|
| 91 |
|
| 92 |
+
def ordinal_emd_loss(
|
| 93 |
+
logits: torch.Tensor,
|
| 94 |
+
labels: torch.Tensor,
|
| 95 |
+
code_token_ids: torch.Tensor,
|
| 96 |
+
n_codes: torch.Tensor,
|
| 97 |
+
soft_labels: torch.Tensor,
|
| 98 |
+
is_score: torch.Tensor,
|
| 99 |
+
*,
|
| 100 |
+
sample_weight: torch.Tensor | None = None,
|
| 101 |
+
) -> torch.Tensor:
|
| 102 |
+
"""1D Earth-mover distance on ordered Score levels.
|
| 103 |
+
|
| 104 |
+
Penalizes mass 3 levels off more than 1: W1 = Σ|CDF_p − CDF_t|.
|
| 105 |
+
Only rows with ``is_score>0`` contribute.
|
| 106 |
+
"""
|
| 107 |
+
shift_logits = logits[:, :-1, :].contiguous()
|
| 108 |
+
shift_labels = labels[:, 1:].contiguous()
|
| 109 |
+
batch = shift_logits.size(0)
|
| 110 |
+
losses: list[torch.Tensor] = []
|
| 111 |
+
weights: list[torch.Tensor] = []
|
| 112 |
+
|
| 113 |
+
for b in range(batch):
|
| 114 |
+
if int(is_score[b].item()) <= 0:
|
| 115 |
+
continue
|
| 116 |
+
p = _answer_pos(shift_labels[b])
|
| 117 |
+
if p is None:
|
| 118 |
+
continue
|
| 119 |
+
k = int(n_codes[b].item())
|
| 120 |
+
if k < 2:
|
| 121 |
+
continue
|
| 122 |
+
ids = code_token_ids[b, :k].long()
|
| 123 |
+
code_logits = shift_logits[b, p, ids]
|
| 124 |
+
pred = F.softmax(code_logits.float(), dim=-1)
|
| 125 |
+
target = soft_labels[b, :k].float()
|
| 126 |
+
target = target / target.sum().clamp_min(1e-8)
|
| 127 |
+
# Also expected |level−gold| so hard-off-by-3 is explicit.
|
| 128 |
+
levels = torch.arange(
|
| 129 |
+
k, device=code_logits.device, dtype=pred.dtype
|
| 130 |
+
)
|
| 131 |
+
gold = float((target * levels).sum().item())
|
| 132 |
+
mae = (pred * (levels - gold).abs()).sum()
|
| 133 |
+
emd = (pred.cumsum(0) - target.cumsum(0)).abs().sum()
|
| 134 |
+
losses.append(emd + mae)
|
| 135 |
+
if sample_weight is not None:
|
| 136 |
+
weights.append(sample_weight[b].float())
|
| 137 |
+
else:
|
| 138 |
+
weights.append(
|
| 139 |
+
torch.ones((), device=code_logits.device)
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
if not losses:
|
| 143 |
+
return logits.sum() * 0.0
|
| 144 |
+
stacked = torch.stack(losses)
|
| 145 |
+
w = torch.stack(weights)
|
| 146 |
+
return (stacked * w).sum() / w.sum().clamp_min(1e-8)
|
| 147 |
+
|
| 148 |
+
|
| 149 |
def code_yes_probs(
|
| 150 |
logits: torch.Tensor,
|
| 151 |
labels: torch.Tensor,
|
|
|
|
| 221 |
binary_head: Any | None = None,
|
| 222 |
binary_head_weight: float = 0.0,
|
| 223 |
binary_head_balanced: bool = False,
|
| 224 |
+
score_ordinal_weight: float = 0.0,
|
| 225 |
**kwargs: Any,
|
| 226 |
) -> None:
|
| 227 |
self._custom_train_sampler = train_sampler
|
|
|
|
| 231 |
self.binary_head = binary_head
|
| 232 |
self.binary_head_weight = float(binary_head_weight)
|
| 233 |
self.binary_head_balanced = bool(binary_head_balanced)
|
| 234 |
+
self.score_ordinal_weight = float(score_ordinal_weight)
|
| 235 |
|
| 236 |
def _get_train_sampler(self, train_dataset: Any = None):
|
| 237 |
if self._custom_train_sampler is not None:
|
|
|
|
| 261 |
inputs.pop("label_id", None)
|
| 262 |
inputs.pop("has_soft", None)
|
| 263 |
is_noul = inputs.pop("is_noul", None)
|
| 264 |
+
is_score = inputs.pop("is_score", None)
|
| 265 |
+
force_soft = inputs.pop("force_soft", None)
|
| 266 |
|
| 267 |
labels = inputs.get("labels")
|
| 268 |
outputs = model(**inputs)
|
|
|
|
| 274 |
soft_labels,
|
| 275 |
use_soft=self.use_soft_ce,
|
| 276 |
sample_weight=sample_weight,
|
| 277 |
+
force_soft_mask=force_soft,
|
| 278 |
)
|
| 279 |
|
| 280 |
+
# Score ordinal: EMD + expected |level−teacher| (off-by-3 > 1).
|
| 281 |
+
if (
|
| 282 |
+
self.score_ordinal_weight > 0
|
| 283 |
+
and is_score is not None
|
| 284 |
+
and (is_score > 0).any()
|
| 285 |
+
):
|
| 286 |
+
ord_loss = ordinal_emd_loss(
|
| 287 |
+
outputs.logits,
|
| 288 |
+
labels,
|
| 289 |
+
code_token_ids,
|
| 290 |
+
n_codes,
|
| 291 |
+
soft_labels,
|
| 292 |
+
is_score,
|
| 293 |
+
sample_weight=sample_weight,
|
| 294 |
+
)
|
| 295 |
+
loss = loss + self.score_ordinal_weight * ord_loss
|
| 296 |
+
|
| 297 |
# Negation consistency + CE on ¬Q (dual forward).
|
| 298 |
if (
|
| 299 |
self.consistency_weight > 0
|
oss_jev_train/dataset.py
CHANGED
|
@@ -70,14 +70,16 @@ def noul_balance_weights(
|
|
| 70 |
*,
|
| 71 |
n_target_frac: float = 0.80,
|
| 72 |
noul_mix_weight: float = 1.0,
|
|
|
|
| 73 |
) -> list[float]:
|
| 74 |
"""Sampler weights: among noul draws, target ``n_target_frac`` on N.
|
| 75 |
|
| 76 |
v0.1.1 equal Y/N mass *upweighted Y* (train is already ~65% N) and
|
| 77 |
eval stayed all-Y. v0.1.2 defaults to heavy N oversampling (~80% N
|
| 78 |
-
mass among noul ≈ 4:1 N:Y).
|
| 79 |
``noul_mix_weight`` (>1) upsamples all noul vs choice/score
|
| 80 |
-
(noul-heavy continue).
|
|
|
|
| 81 |
"""
|
| 82 |
n_y = sum(
|
| 83 |
1
|
|
@@ -94,12 +96,15 @@ def noul_balance_weights(
|
|
| 94 |
frac = float(n_target_frac)
|
| 95 |
frac = min(max(frac, 0.05), 0.95)
|
| 96 |
mix = max(float(noul_mix_weight), 0.05)
|
|
|
|
| 97 |
# w_y * n_y / total = 1-frac; w_n * n_n / total = frac; set w_y=1
|
| 98 |
w_y = 1.0 * mix
|
| 99 |
w_n = (frac / (1.0 - frac)) * (n_y / n_n) * mix
|
| 100 |
out: list[float] = []
|
| 101 |
for ex in examples:
|
| 102 |
-
if ex.primitive
|
|
|
|
|
|
|
| 103 |
out.append(1.0)
|
| 104 |
elif _noul_is_yes_gold(ex):
|
| 105 |
out.append(float(w_y))
|
|
@@ -115,11 +120,13 @@ def make_noul_balanced_sampler(
|
|
| 115 |
seed: int = 42,
|
| 116 |
n_target_frac: float = 0.80,
|
| 117 |
noul_mix_weight: float = 1.0,
|
|
|
|
| 118 |
) -> WeightedRandomSampler:
|
| 119 |
weights = noul_balance_weights(
|
| 120 |
examples,
|
| 121 |
n_target_frac=n_target_frac,
|
| 122 |
noul_mix_weight=noul_mix_weight,
|
|
|
|
| 123 |
)
|
| 124 |
n = num_samples if num_samples is not None else len(examples)
|
| 125 |
generator = None
|
|
@@ -152,6 +159,8 @@ class OneTokenDataset(Dataset):
|
|
| 152 |
noul_loss_weight: float = 2.0,
|
| 153 |
noul_n_sample_weight: float = 2.0,
|
| 154 |
noul_consistency: bool = False,
|
|
|
|
|
|
|
| 155 |
) -> None:
|
| 156 |
self.examples = examples
|
| 157 |
self.tokenizer = tokenizer
|
|
@@ -164,6 +173,9 @@ class OneTokenDataset(Dataset):
|
|
| 164 |
self.noul_n_sample_weight = float(noul_n_sample_weight)
|
| 165 |
# Dual-forward consistency on Q and ¬Q (same item).
|
| 166 |
self.noul_consistency = bool(noul_consistency)
|
|
|
|
|
|
|
|
|
|
| 167 |
if tokenizer.pad_token_id is None:
|
| 168 |
tokenizer.pad_token = tokenizer.eos_token
|
| 169 |
# Cache code → token id (verified single-token upstream).
|
|
@@ -250,8 +262,12 @@ class OneTokenDataset(Dataset):
|
|
| 250 |
for i, code in enumerate(ex.codes):
|
| 251 |
code_token_ids[i] = self._code_id[code]
|
| 252 |
|
| 253 |
-
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
)
|
| 256 |
soft = soft_targets(ex) if use_soft_row else None
|
| 257 |
soft_t = torch.zeros(MAX_CODES, dtype=torch.float)
|
|
@@ -273,6 +289,9 @@ class OneTokenDataset(Dataset):
|
|
| 273 |
else:
|
| 274 |
sample_weight = base
|
| 275 |
yes_idx = int(noul_yes_index(ex))
|
|
|
|
|
|
|
|
|
|
| 276 |
else:
|
| 277 |
sample_weight = 1.0
|
| 278 |
yes_idx = -1
|
|
@@ -288,6 +307,8 @@ class OneTokenDataset(Dataset):
|
|
| 288 |
"has_soft": has_soft,
|
| 289 |
"sample_weight": float(sample_weight),
|
| 290 |
"is_noul": 1 if ex.primitive == "noul" else 0,
|
|
|
|
|
|
|
| 291 |
"yes_idx": yes_idx,
|
| 292 |
}
|
| 293 |
|
|
|
|
| 70 |
*,
|
| 71 |
n_target_frac: float = 0.80,
|
| 72 |
noul_mix_weight: float = 1.0,
|
| 73 |
+
score_mix_weight: float = 1.0,
|
| 74 |
) -> list[float]:
|
| 75 |
"""Sampler weights: among noul draws, target ``n_target_frac`` on N.
|
| 76 |
|
| 77 |
v0.1.1 equal Y/N mass *upweighted Y* (train is already ~65% N) and
|
| 78 |
eval stayed all-Y. v0.1.2 defaults to heavy N oversampling (~80% N
|
| 79 |
+
mass among noul ≈ 4:1 N:Y). Choice keeps weight 1.0.
|
| 80 |
``noul_mix_weight`` (>1) upsamples all noul vs choice/score
|
| 81 |
+
(noul-heavy continue). ``score_mix_weight`` (>1) upsamples Score
|
| 82 |
+
(score-kl continue). Works with randomized yes/no codes.
|
| 83 |
"""
|
| 84 |
n_y = sum(
|
| 85 |
1
|
|
|
|
| 96 |
frac = float(n_target_frac)
|
| 97 |
frac = min(max(frac, 0.05), 0.95)
|
| 98 |
mix = max(float(noul_mix_weight), 0.05)
|
| 99 |
+
score_mix = max(float(score_mix_weight), 0.05)
|
| 100 |
# w_y * n_y / total = 1-frac; w_n * n_n / total = frac; set w_y=1
|
| 101 |
w_y = 1.0 * mix
|
| 102 |
w_n = (frac / (1.0 - frac)) * (n_y / n_n) * mix
|
| 103 |
out: list[float] = []
|
| 104 |
for ex in examples:
|
| 105 |
+
if ex.primitive == "score":
|
| 106 |
+
out.append(float(score_mix))
|
| 107 |
+
elif ex.primitive != "noul":
|
| 108 |
out.append(1.0)
|
| 109 |
elif _noul_is_yes_gold(ex):
|
| 110 |
out.append(float(w_y))
|
|
|
|
| 120 |
seed: int = 42,
|
| 121 |
n_target_frac: float = 0.80,
|
| 122 |
noul_mix_weight: float = 1.0,
|
| 123 |
+
score_mix_weight: float = 1.0,
|
| 124 |
) -> WeightedRandomSampler:
|
| 125 |
weights = noul_balance_weights(
|
| 126 |
examples,
|
| 127 |
n_target_frac=n_target_frac,
|
| 128 |
noul_mix_weight=noul_mix_weight,
|
| 129 |
+
score_mix_weight=score_mix_weight,
|
| 130 |
)
|
| 131 |
n = num_samples if num_samples is not None else len(examples)
|
| 132 |
generator = None
|
|
|
|
| 159 |
noul_loss_weight: float = 2.0,
|
| 160 |
noul_n_sample_weight: float = 2.0,
|
| 161 |
noul_consistency: bool = False,
|
| 162 |
+
score_force_soft: bool = False,
|
| 163 |
+
score_loss_weight: float = 1.0,
|
| 164 |
) -> None:
|
| 165 |
self.examples = examples
|
| 166 |
self.tokenizer = tokenizer
|
|
|
|
| 173 |
self.noul_n_sample_weight = float(noul_n_sample_weight)
|
| 174 |
# Dual-forward consistency on Q and ¬Q (same item).
|
| 175 |
self.noul_consistency = bool(noul_consistency)
|
| 176 |
+
# Score KL: force soft teacher level dist + row loss weight.
|
| 177 |
+
self.score_force_soft = bool(score_force_soft)
|
| 178 |
+
self.score_loss_weight = float(score_loss_weight)
|
| 179 |
if tokenizer.pad_token_id is None:
|
| 180 |
tokenizer.pad_token = tokenizer.eos_token
|
| 181 |
# Cache code → token id (verified single-token upstream).
|
|
|
|
| 262 |
for i, code in enumerate(ex.codes):
|
| 263 |
code_token_ids[i] = self._code_id[code]
|
| 264 |
|
| 265 |
+
is_score = ex.primitive == "score"
|
| 266 |
+
use_soft_row = (
|
| 267 |
+
(self.use_soft and not (
|
| 268 |
+
ex.primitive == "noul" and self.noul_hard_ce
|
| 269 |
+
))
|
| 270 |
+
or (is_score and self.score_force_soft)
|
| 271 |
)
|
| 272 |
soft = soft_targets(ex) if use_soft_row else None
|
| 273 |
soft_t = torch.zeros(MAX_CODES, dtype=torch.float)
|
|
|
|
| 289 |
else:
|
| 290 |
sample_weight = base
|
| 291 |
yes_idx = int(noul_yes_index(ex))
|
| 292 |
+
elif is_score:
|
| 293 |
+
sample_weight = float(self.score_loss_weight)
|
| 294 |
+
yes_idx = -1
|
| 295 |
else:
|
| 296 |
sample_weight = 1.0
|
| 297 |
yes_idx = -1
|
|
|
|
| 307 |
"has_soft": has_soft,
|
| 308 |
"sample_weight": float(sample_weight),
|
| 309 |
"is_noul": 1 if ex.primitive == "noul" else 0,
|
| 310 |
+
"is_score": 1 if is_score else 0,
|
| 311 |
+
"force_soft": 1 if (is_score and self.score_force_soft) else 0,
|
| 312 |
"yes_idx": yes_idx,
|
| 313 |
}
|
| 314 |
|
oss_jev_train/export_ckpt.py
CHANGED
|
@@ -53,6 +53,10 @@ def write_checkpoint_sidecar(
|
|
| 53 |
noul_negation_pairs: bool | None = None,
|
| 54 |
noul_consistency_weight: float | None = None,
|
| 55 |
noul_binary_head: bool | None = None,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
) -> Path:
|
| 57 |
"""Write ``decision_head.json`` next to HF weights.
|
| 58 |
|
|
@@ -108,6 +112,14 @@ def write_checkpoint_sidecar(
|
|
| 108 |
)
|
| 109 |
if noul_binary_head is not None:
|
| 110 |
meta["noul_binary_head"] = bool(noul_binary_head)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
path = ckpt_dir / "decision_head.json"
|
| 112 |
path.write_text(json.dumps(meta, indent=2) + "\n", encoding="utf-8")
|
| 113 |
|
|
|
|
| 53 |
noul_negation_pairs: bool | None = None,
|
| 54 |
noul_consistency_weight: float | None = None,
|
| 55 |
noul_binary_head: bool | None = None,
|
| 56 |
+
score_force_soft: bool | None = None,
|
| 57 |
+
score_ordinal_weight: float | None = None,
|
| 58 |
+
score_loss_weight: float | None = None,
|
| 59 |
+
score_mix_weight: float | None = None,
|
| 60 |
) -> Path:
|
| 61 |
"""Write ``decision_head.json`` next to HF weights.
|
| 62 |
|
|
|
|
| 112 |
)
|
| 113 |
if noul_binary_head is not None:
|
| 114 |
meta["noul_binary_head"] = bool(noul_binary_head)
|
| 115 |
+
if score_force_soft is not None:
|
| 116 |
+
meta["score_force_soft"] = bool(score_force_soft)
|
| 117 |
+
if score_ordinal_weight is not None:
|
| 118 |
+
meta["score_ordinal_weight"] = float(score_ordinal_weight)
|
| 119 |
+
if score_loss_weight is not None:
|
| 120 |
+
meta["score_loss_weight"] = float(score_loss_weight)
|
| 121 |
+
if score_mix_weight is not None:
|
| 122 |
+
meta["score_mix_weight"] = float(score_mix_weight)
|
| 123 |
path = ckpt_dir / "decision_head.json"
|
| 124 |
path.write_text(json.dumps(meta, indent=2) + "\n", encoding="utf-8")
|
| 125 |
|
oss_jev_train/train.py
CHANGED
|
@@ -275,6 +275,29 @@ def build_argparser() -> argparse.ArgumentParser:
|
|
| 275 |
default=None,
|
| 276 |
help="Upsample noul vs choice/score in sampler (default 1)",
|
| 277 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
return p
|
| 279 |
|
| 280 |
|
|
@@ -334,6 +357,11 @@ def merge_config(args: argparse.Namespace) -> dict[str, Any]:
|
|
| 334 |
"noul_binary_head_weight": 1.0,
|
| 335 |
"noul_binary_balanced": False,
|
| 336 |
"noul_mix_weight": 1.0,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
}
|
| 338 |
if args.config:
|
| 339 |
with open(args.config, encoding="utf-8") as f:
|
|
@@ -398,6 +426,14 @@ def merge_config(args: argparse.Namespace) -> dict[str, Any]:
|
|
| 398 |
cfg["noul_binary_balanced"] = True
|
| 399 |
if args.noul_mix_weight is not None:
|
| 400 |
cfg["noul_mix_weight"] = float(args.noul_mix_weight)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
env_model = _env("OSS_JEV_MODEL_ID")
|
| 402 |
if env_model and args.model_id is None and not (
|
| 403 |
args.config and cfg.get("model_id")
|
|
@@ -514,6 +550,21 @@ def run_dry(paths: dict[str, Path], cfg: dict[str, Any]) -> int:
|
|
| 514 |
"noul_mix_weight": float(
|
| 515 |
cfg.get("noul_mix_weight", 1.0)
|
| 516 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 517 |
},
|
| 518 |
indent=2,
|
| 519 |
)
|
|
@@ -566,13 +617,19 @@ def train_one_token(
|
|
| 566 |
bin_w = float(cfg.get("noul_binary_head_weight", 1.0))
|
| 567 |
bin_bal = bool(cfg.get("noul_binary_balanced", False))
|
| 568 |
noul_mix = float(cfg.get("noul_mix_weight", 1.0))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 569 |
print(
|
| 570 |
f"one_token soft_ce={use_soft} constrained_codes={constrained} "
|
| 571 |
f"balance_noul={balance} noul_n_frac={noul_n_frac} "
|
| 572 |
f"noul_mix_w={noul_mix} noul_hard_ce={noul_hard} "
|
| 573 |
f"noul_loss_w={noul_lw} noul_n_w={noul_nw} "
|
| 574 |
f"noul_logit_bias_n={noul_bias} cons_w={cons_w} "
|
| 575 |
-
f"binary_head={use_bin} binary_bal={bin_bal}"
|
|
|
|
|
|
|
| 576 |
flush=True,
|
| 577 |
)
|
| 578 |
|
|
@@ -611,6 +668,8 @@ def train_one_token(
|
|
| 611 |
noul_loss_weight=noul_lw,
|
| 612 |
noul_n_sample_weight=noul_nw,
|
| 613 |
noul_consistency=cons_w > 0,
|
|
|
|
|
|
|
| 614 |
)
|
| 615 |
train_ds = OneTokenDataset(examples, tok, **ds_kw)
|
| 616 |
eval_ds = (
|
|
@@ -645,13 +704,14 @@ def train_one_token(
|
|
| 645 |
_ = eval_ds # kept for future optional eval; unused (OOM-safe)
|
| 646 |
|
| 647 |
train_sampler = None
|
| 648 |
-
if balance:
|
| 649 |
train_sampler = make_noul_balanced_sampler(
|
| 650 |
examples,
|
| 651 |
num_samples=len(examples),
|
| 652 |
seed=int(cfg["seed"]),
|
| 653 |
n_target_frac=noul_n_frac,
|
| 654 |
-
noul_mix_weight=noul_mix,
|
|
|
|
| 655 |
)
|
| 656 |
|
| 657 |
trainer_kwargs: dict[str, Any] = {
|
|
@@ -669,6 +729,7 @@ def train_one_token(
|
|
| 669 |
binary_head=binary_head,
|
| 670 |
binary_head_weight=bin_w if use_bin else 0.0,
|
| 671 |
binary_head_balanced=bin_bal if use_bin else False,
|
|
|
|
| 672 |
**trainer_kwargs,
|
| 673 |
)
|
| 674 |
else:
|
|
@@ -698,6 +759,8 @@ def train_one_token(
|
|
| 698 |
"label_id",
|
| 699 |
"has_soft",
|
| 700 |
"is_noul",
|
|
|
|
|
|
|
| 701 |
"yes_idx",
|
| 702 |
"has_neg",
|
| 703 |
"neg_input_ids",
|
|
@@ -732,6 +795,10 @@ def train_one_token(
|
|
| 732 |
),
|
| 733 |
noul_consistency_weight=cons_w,
|
| 734 |
noul_binary_head=use_bin,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 735 |
)
|
| 736 |
if binary_head is not None:
|
| 737 |
from .export_ckpt import (
|
|
|
|
| 275 |
default=None,
|
| 276 |
help="Upsample noul vs choice/score in sampler (default 1)",
|
| 277 |
)
|
| 278 |
+
p.add_argument(
|
| 279 |
+
"--score_force_soft",
|
| 280 |
+
action="store_true",
|
| 281 |
+
help="Force soft-KL to Jev level dist on Score rows",
|
| 282 |
+
)
|
| 283 |
+
p.add_argument(
|
| 284 |
+
"--score_ordinal_weight",
|
| 285 |
+
type=float,
|
| 286 |
+
default=None,
|
| 287 |
+
help="Weight for Score ordinal EMD+MAE (0=off)",
|
| 288 |
+
)
|
| 289 |
+
p.add_argument(
|
| 290 |
+
"--score_loss_weight",
|
| 291 |
+
type=float,
|
| 292 |
+
default=None,
|
| 293 |
+
help="Multiply loss on Score rows (default 1.0)",
|
| 294 |
+
)
|
| 295 |
+
p.add_argument(
|
| 296 |
+
"--score_mix_weight",
|
| 297 |
+
type=float,
|
| 298 |
+
default=None,
|
| 299 |
+
help="Upsample Score vs choice in sampler (default 1)",
|
| 300 |
+
)
|
| 301 |
return p
|
| 302 |
|
| 303 |
|
|
|
|
| 357 |
"noul_binary_head_weight": 1.0,
|
| 358 |
"noul_binary_balanced": False,
|
| 359 |
"noul_mix_weight": 1.0,
|
| 360 |
+
# v0.1.8 Score KL + ordinal (off by default).
|
| 361 |
+
"score_force_soft": False,
|
| 362 |
+
"score_ordinal_weight": 0.0,
|
| 363 |
+
"score_loss_weight": 1.0,
|
| 364 |
+
"score_mix_weight": 1.0,
|
| 365 |
}
|
| 366 |
if args.config:
|
| 367 |
with open(args.config, encoding="utf-8") as f:
|
|
|
|
| 426 |
cfg["noul_binary_balanced"] = True
|
| 427 |
if args.noul_mix_weight is not None:
|
| 428 |
cfg["noul_mix_weight"] = float(args.noul_mix_weight)
|
| 429 |
+
if args.score_force_soft:
|
| 430 |
+
cfg["score_force_soft"] = True
|
| 431 |
+
if args.score_ordinal_weight is not None:
|
| 432 |
+
cfg["score_ordinal_weight"] = float(args.score_ordinal_weight)
|
| 433 |
+
if args.score_loss_weight is not None:
|
| 434 |
+
cfg["score_loss_weight"] = float(args.score_loss_weight)
|
| 435 |
+
if args.score_mix_weight is not None:
|
| 436 |
+
cfg["score_mix_weight"] = float(args.score_mix_weight)
|
| 437 |
env_model = _env("OSS_JEV_MODEL_ID")
|
| 438 |
if env_model and args.model_id is None and not (
|
| 439 |
args.config and cfg.get("model_id")
|
|
|
|
| 550 |
"noul_mix_weight": float(
|
| 551 |
cfg.get("noul_mix_weight", 1.0)
|
| 552 |
),
|
| 553 |
+
"score_force_soft": bool(
|
| 554 |
+
cfg.get("score_force_soft", False)
|
| 555 |
+
),
|
| 556 |
+
"score_ordinal_weight": float(
|
| 557 |
+
cfg.get("score_ordinal_weight", 0.0)
|
| 558 |
+
),
|
| 559 |
+
"score_loss_weight": float(
|
| 560 |
+
cfg.get("score_loss_weight", 1.0)
|
| 561 |
+
),
|
| 562 |
+
"score_mix_weight": float(
|
| 563 |
+
cfg.get("score_mix_weight", 1.0)
|
| 564 |
+
),
|
| 565 |
+
"n_score": sum(
|
| 566 |
+
1 for e in examples if e.primitive == "score"
|
| 567 |
+
),
|
| 568 |
},
|
| 569 |
indent=2,
|
| 570 |
)
|
|
|
|
| 617 |
bin_w = float(cfg.get("noul_binary_head_weight", 1.0))
|
| 618 |
bin_bal = bool(cfg.get("noul_binary_balanced", False))
|
| 619 |
noul_mix = float(cfg.get("noul_mix_weight", 1.0))
|
| 620 |
+
score_force = bool(cfg.get("score_force_soft", False))
|
| 621 |
+
score_ord_w = float(cfg.get("score_ordinal_weight", 0.0))
|
| 622 |
+
score_lw = float(cfg.get("score_loss_weight", 1.0))
|
| 623 |
+
score_mix = float(cfg.get("score_mix_weight", 1.0))
|
| 624 |
print(
|
| 625 |
f"one_token soft_ce={use_soft} constrained_codes={constrained} "
|
| 626 |
f"balance_noul={balance} noul_n_frac={noul_n_frac} "
|
| 627 |
f"noul_mix_w={noul_mix} noul_hard_ce={noul_hard} "
|
| 628 |
f"noul_loss_w={noul_lw} noul_n_w={noul_nw} "
|
| 629 |
f"noul_logit_bias_n={noul_bias} cons_w={cons_w} "
|
| 630 |
+
f"binary_head={use_bin} binary_bal={bin_bal} "
|
| 631 |
+
f"score_force_soft={score_force} score_ord_w={score_ord_w} "
|
| 632 |
+
f"score_loss_w={score_lw} score_mix_w={score_mix}",
|
| 633 |
flush=True,
|
| 634 |
)
|
| 635 |
|
|
|
|
| 668 |
noul_loss_weight=noul_lw,
|
| 669 |
noul_n_sample_weight=noul_nw,
|
| 670 |
noul_consistency=cons_w > 0,
|
| 671 |
+
score_force_soft=score_force,
|
| 672 |
+
score_loss_weight=score_lw,
|
| 673 |
)
|
| 674 |
train_ds = OneTokenDataset(examples, tok, **ds_kw)
|
| 675 |
eval_ds = (
|
|
|
|
| 704 |
_ = eval_ds # kept for future optional eval; unused (OOM-safe)
|
| 705 |
|
| 706 |
train_sampler = None
|
| 707 |
+
if balance or score_mix != 1.0:
|
| 708 |
train_sampler = make_noul_balanced_sampler(
|
| 709 |
examples,
|
| 710 |
num_samples=len(examples),
|
| 711 |
seed=int(cfg["seed"]),
|
| 712 |
n_target_frac=noul_n_frac,
|
| 713 |
+
noul_mix_weight=noul_mix if balance else 1.0,
|
| 714 |
+
score_mix_weight=score_mix,
|
| 715 |
)
|
| 716 |
|
| 717 |
trainer_kwargs: dict[str, Any] = {
|
|
|
|
| 729 |
binary_head=binary_head,
|
| 730 |
binary_head_weight=bin_w if use_bin else 0.0,
|
| 731 |
binary_head_balanced=bin_bal if use_bin else False,
|
| 732 |
+
score_ordinal_weight=score_ord_w,
|
| 733 |
**trainer_kwargs,
|
| 734 |
)
|
| 735 |
else:
|
|
|
|
| 759 |
"label_id",
|
| 760 |
"has_soft",
|
| 761 |
"is_noul",
|
| 762 |
+
"is_score",
|
| 763 |
+
"force_soft",
|
| 764 |
"yes_idx",
|
| 765 |
"has_neg",
|
| 766 |
"neg_input_ids",
|
|
|
|
| 795 |
),
|
| 796 |
noul_consistency_weight=cons_w,
|
| 797 |
noul_binary_head=use_bin,
|
| 798 |
+
score_force_soft=score_force,
|
| 799 |
+
score_ordinal_weight=score_ord_w,
|
| 800 |
+
score_loss_weight=score_lw,
|
| 801 |
+
score_mix_weight=score_mix,
|
| 802 |
)
|
| 803 |
if binary_head is not None:
|
| 804 |
from .export_ckpt import (
|
pyproject.toml
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
[project]
|
| 2 |
name = "oss-jev-train"
|
| 3 |
-
version = "0.1.
|
| 4 |
description = "Qwen one-token decision-head train for OSS Jev distill (HF Jobs image)"
|
| 5 |
readme = "README.md"
|
| 6 |
requires-python = ">=3.10"
|
|
|
|
| 1 |
[project]
|
| 2 |
name = "oss-jev-train"
|
| 3 |
+
version = "0.1.8"
|
| 4 |
description = "Qwen one-token decision-head train for OSS Jev distill (HF Jobs image)"
|
| 5 |
readme = "README.md"
|
| 6 |
requires-python = ">=3.10"
|