Datasets:
Upload train_ears_granite.py with huggingface_hub
Browse files- train_ears_granite.py +26 -54
train_ears_granite.py
CHANGED
|
@@ -44,9 +44,9 @@ class Collator:
|
|
| 44 |
import torch
|
| 45 |
import soundfile as sf
|
| 46 |
tok = self.processor.tokenizer
|
|
|
|
| 47 |
sr_t = self.processor.audio_processor.sampling_rate
|
| 48 |
-
|
| 49 |
-
ids_b, lab_b, attn_b, feat_b, fmask_b = [], [], [], [], []
|
| 50 |
for ex in examples:
|
| 51 |
try:
|
| 52 |
arr, sr = sf.read(io.BytesIO(ex["audio"]["bytes"]), dtype="float32")
|
|
@@ -58,62 +58,34 @@ class Collator:
|
|
| 58 |
import librosa
|
| 59 |
arr = librosa.resample(arr, orig_sr=sr, target_sr=sr_t)
|
| 60 |
arr = arr[: int(self.max_s * sr_t)]
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
[{"role": "user", "content": INSTR}], tokenize=False,
|
| 64 |
-
add_generation_prompt=True)
|
| 65 |
-
try:
|
| 66 |
-
out = self.processor(prompt, wav, return_tensors="pt")
|
| 67 |
-
except Exception as exc:
|
| 68 |
-
print("[collator] processor error:", exc)
|
| 69 |
continue
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
lab[iid.shape[0]:] = tgt
|
| 79 |
-
fattn = torch.cat([am, torch.ones(tgt.shape[0], dtype=am.dtype)])
|
| 80 |
-
ids_b.append(full); lab_b.append(lab); attn_b.append(fattn)
|
| 81 |
-
if "input_features" in out:
|
| 82 |
-
feat_b.append(out["input_features"].squeeze(0))
|
| 83 |
-
if "input_features_mask" in out:
|
| 84 |
-
fmask_b.append(out["input_features_mask"].squeeze(0))
|
| 85 |
-
import torch
|
| 86 |
-
if not ids_b:
|
| 87 |
return {"input_ids": torch.zeros((0, 1), dtype=torch.long),
|
| 88 |
"attention_mask": torch.zeros((0, 1), dtype=torch.long),
|
| 89 |
"labels": torch.full((0, 1), -100, dtype=torch.long)}
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
pf = torch.zeros((len(feat_b), fmax, fdim), dtype=feat_b[0].dtype)
|
| 105 |
-
pm = torch.zeros((len(feat_b), fmax), dtype=torch.long)
|
| 106 |
-
for i, f in enumerate(feat_b):
|
| 107 |
-
t = f.shape[0]
|
| 108 |
-
pf[i, :t] = f
|
| 109 |
-
if i < len(fmask_b):
|
| 110 |
-
m = fmask_b[i].reshape(-1)
|
| 111 |
-
pm[i, : m.shape[0]] = m.to(torch.long)
|
| 112 |
-
else:
|
| 113 |
-
pm[i, :t] = 1
|
| 114 |
-
r["input_features"] = pf
|
| 115 |
-
r["input_features_mask"] = pm
|
| 116 |
-
return r
|
| 117 |
|
| 118 |
|
| 119 |
def main():
|
|
|
|
| 44 |
import torch
|
| 45 |
import soundfile as sf
|
| 46 |
tok = self.processor.tokenizer
|
| 47 |
+
tok.padding_side = "right"
|
| 48 |
sr_t = self.processor.audio_processor.sampling_rate
|
| 49 |
+
prompts, fulls, wavs = [], [], []
|
|
|
|
| 50 |
for ex in examples:
|
| 51 |
try:
|
| 52 |
arr, sr = sf.read(io.BytesIO(ex["audio"]["bytes"]), dtype="float32")
|
|
|
|
| 58 |
import librosa
|
| 59 |
arr = librosa.resample(arr, orig_sr=sr, target_sr=sr_t)
|
| 60 |
arr = arr[: int(self.max_s * sr_t)]
|
| 61 |
+
txt = (ex.get("text") or "").strip()
|
| 62 |
+
if not txt:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
continue
|
| 64 |
+
wavs.append(arr)
|
| 65 |
+
prompts.append(tok.apply_chat_template(
|
| 66 |
+
[{"role": "user", "content": INSTR}],
|
| 67 |
+
tokenize=False, add_generation_prompt=True))
|
| 68 |
+
fulls.append(tok.apply_chat_template(
|
| 69 |
+
[{"role": "user", "content": INSTR},
|
| 70 |
+
{"role": "assistant", "content": txt}], tokenize=False))
|
| 71 |
+
if not wavs:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
return {"input_ids": torch.zeros((0, 1), dtype=torch.long),
|
| 73 |
"attention_mask": torch.zeros((0, 1), dtype=torch.long),
|
| 74 |
"labels": torch.full((0, 1), -100, dtype=torch.long)}
|
| 75 |
+
# Let the processor batch + pad input_features and expand the <|audio|>
|
| 76 |
+
# placeholders consistently — hand-padding features desyncs the audio-token
|
| 77 |
+
# count from the placeholders (the 165-vs-4 failure). Recover labels by
|
| 78 |
+
# masking the prompt span, sized from a prompt-only pass (same audio → same
|
| 79 |
+
# placeholder expansion, right-padding keeps the prompt a clean prefix).
|
| 80 |
+
out = self.processor(text=fulls, audio=wavs, return_tensors="pt", padding=True)
|
| 81 |
+
pout = self.processor(text=prompts, audio=wavs, return_tensors="pt", padding=True)
|
| 82 |
+
labels = out["input_ids"].clone()
|
| 83 |
+
labels[out["attention_mask"] == 0] = -100
|
| 84 |
+
for i in range(len(fulls)):
|
| 85 |
+
plen = int(pout["attention_mask"][i].sum().item())
|
| 86 |
+
labels[i, :plen] = -100
|
| 87 |
+
out["labels"] = labels
|
| 88 |
+
return dict(out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
def main():
|