PowerMachine commited on
Commit
1fa0ba0
·
verified ·
1 Parent(s): 9b62d55

V6.7: deprecated → scripts/deprecated/upload_v6_5_7ds_attn_v3.py

Browse files
scripts/deprecated/upload_v6_5_7ds_attn_v3.py ADDED
@@ -0,0 +1,373 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """upload_v6_5_7ds_attn_v2.py — V6.5-attn-v2 upload to HuggingFace.
2
+
3
+ User requirement: "upload dos arquivos testados e os aprimorados corrigidos
4
+ (remover os antigos)"
5
+
6
+ V6.5-attn-v2 = V6.5-attn + META ≥6000 + salvar estados + 3 perguntas reais sem ajuda.
7
+ """
8
+ from __future__ import annotations
9
+ import json, os, sys, time, re
10
+ from pathlib import Path
11
+
12
+ PROJECT_ROOT = Path("/home/z/my-project")
13
+ BIGRU_ROOT = PROJECT_ROOT / "BiGRU_T_version"
14
+
15
+ # Files to REMOVE (old V6.5-7ds files superseded by V6.5-attn + legacy files)
16
+ OLD_FILES_TO_REMOVE = [
17
+ # ── Legacy root reports (v3/v4/v5/v6/v32) ─────────────────────────────────
18
+ "bug_hunt_v3_report.json",
19
+ "v3_report.json",
20
+ "v4_report.json",
21
+ "v5_report.json",
22
+ "v5_upload_report.json",
23
+ "v32_test_report.json",
24
+ "v32_upload_report.json",
25
+ "training_report.json",
26
+ "v6_report.json",
27
+ "v6_upload_report.json",
28
+ "v6_progressive_report.json",
29
+ "v6_progressive_train.log",
30
+ # ── Legacy v6.1/v6.2/v6.3 reports ─────────────────────────────────────────
31
+ "v6_1_report.json",
32
+ "v6_1_upload_report.json",
33
+ "v6_2_report.json",
34
+ "v6_2_upload_report.json",
35
+ "v6_3_report.json",
36
+ "v6_3_training_metrics.json",
37
+ # ── Old V6.5 reports (superseded) ─────────────────────────────────────────
38
+ "v6_5_report.json",
39
+ "v6_5_training_metrics.json",
40
+ "v6_5_module_analysis.json",
41
+ "v6_5_script_activity.json",
42
+ "v6_5_ewc_w8a8_benchmark.json",
43
+ "v6_5_reasoning_eval.json",
44
+ "v6_5_upload_report.json",
45
+ # ── Old V6.5-final reports (superseded) ───────────────────────────────────
46
+ "v6_5_final_report.json",
47
+ "v6_5_final_training_metrics.json",
48
+ "v6_5_final_module_analysis.json",
49
+ "v6_5_final_script_activity.json",
50
+ "v6_5_final_ewc_w8a8_benchmark.json",
51
+ "v6_5_final_reasoning_eval.json",
52
+ "v6_5_final_w8a8_compression.json",
53
+ # ── Old V6.5-7ds reports (superseded by V6.5-attn) ────────────────────────
54
+ "v6_5_7ds_report.json",
55
+ "v6_5_7ds_training_metrics.json",
56
+ "v6_5_7ds_module_analysis.json",
57
+ "v6_5_7ds_script_activity.json",
58
+ "v6_5_7ds_ewc_w8a8_benchmark.json",
59
+ "v6_5_7ds_reasoning_eval.json",
60
+ "v6_5_7ds_w8a8_compression.json",
61
+ "v6_5_7ds_upload_report.json",
62
+ # ── Legacy training scripts ───────────────────────────────────────────────
63
+ "scripts/parse_v6_log.py",
64
+ "scripts/smoke_test.py",
65
+ "scripts/train.py",
66
+ "scripts/train_fast.py",
67
+ "scripts/train_v2.py",
68
+ "scripts/train_v6_1.py",
69
+ "scripts/train_v6_2.py",
70
+ "scripts/train_v6_3.py",
71
+ "scripts/train_v6_progressive.py",
72
+ "scripts/train_v6_5.py",
73
+ "scripts/train_v6_5_final.py",
74
+ "scripts/train_v6_5_7ds.py", # superseded by train_v6_5_7ds_attn.py
75
+ # ── Legacy upload scripts (only upload_v6_5_7ds_attn.py stays) ────────────
76
+ "scripts/upload_to_hf.py",
77
+ "scripts/upload_v6_1_resilient.py",
78
+ "scripts/upload_v6_2_resilient.py",
79
+ "scripts/upload_v6_3_resilient.py",
80
+ "scripts/upload_v6_4_resilient.py",
81
+ "scripts/upload_v6_5_resilient.py",
82
+ "scripts/upload_v6_5_7ds.py", # superseded by upload_v6_5_7ds_attn.py
83
+ # ── Legacy tokenizer/ root config ─────────────────────────────────────────
84
+ "config.json",
85
+ "tokenizer/tokenizer.json",
86
+ # ── Legacy data_augmentation.py ───────────────────────────────────────────
87
+ "src/bigru_t/data/data_augmentation.py",
88
+ # ── Legacy xeon_runtime.py at scripts/ ────────────────────────────────────
89
+ "scripts/xeon_runtime.py",
90
+ # ── Old V6.5-7ds-attn v1 reports (superseded by V6.5-attn-v2) ────────────
91
+ "v6_5_7ds_attn_report.json",
92
+ "v6_5_7ds_attn_training_metrics.json",
93
+ "v6_5_7ds_attn_module_analysis.json",
94
+ "v6_5_7ds_attn_script_activity.json",
95
+ "v6_5_7ds_attn_ewc_w8a8_benchmark.json",
96
+ "v6_5_7ds_attn_reasoning_eval.json",
97
+ "v6_5_7ds_attn_w8a8_compression.json",
98
+ "v6_5_7ds_attn_attention_eval.json",
99
+ "v6_5_7ds_attn_verification.json",
100
+ "v6_5_7ds_attn_upload_report.json",
101
+ # ── Old V6.5-7ds-attn v1 scripts (superseded by v2) ──────────────────────
102
+ "scripts/train_v6_5_7ds_attn.py",
103
+ "scripts/upload_v6_5_7ds_attn.py",
104
+ # ── V6.5-fix: Old V6.5-7ds-attn v2 reports (superseded by v3) ────────────
105
+ # User requirement: "apagar v6_5_7ds_attn_v2_model_states.pt enviado ao HF
106
+ # devido a esse erro informado" (predict() retornava 'gato'/'cachorro'
107
+ # fixos ao invés de rótulos dinâmicos dos datasets).
108
+ "v6_5_7ds_attn_v2_report.json",
109
+ "v6_5_7ds_attn_v2_training_metrics.json",
110
+ "v6_5_7ds_attn_v2_module_analysis.json",
111
+ "v6_5_7ds_attn_v2_script_activity.json",
112
+ "v6_5_7ds_attn_v2_ewc_w8a8_benchmark.json",
113
+ "v6_5_7ds_attn_v2_reasoning_eval.json",
114
+ "v6_5_7ds_attn_v2_w8a8_compression.json",
115
+ "v6_5_7ds_attn_v2_attention_eval.json",
116
+ "v6_5_7ds_attn_v2_user_questions.json",
117
+ "v6_5_7ds_attn_v2_model_states.pt", # ← BUG: predict() retornava gato/cachorro
118
+ "v6_5_7ds_attn_v2_upload_report.json",
119
+ # ── V6.5-fix: Old V6.5-7ds-attn v2 scripts (superseded by v3) ────────────
120
+ "scripts/train_v6_5_7ds_attn_v2.py",
121
+ "scripts/upload_v6_5_7ds_attn_v2.py",
122
+ ]
123
+
124
+ # Critical files that MUST be present after upload
125
+ CRITICAL_FILES_V65_ATTN = [
126
+ # Core model
127
+ "src/bigru_t/model/kohonen_learning_system.py",
128
+ "src/bigru_t/model/__init__.py",
129
+ "src/bigru_t/__init__.py",
130
+ "src/bigru_t/model/hyp_t.py",
131
+ "src/bigru_t/model/vqvae2_hierarchical.py",
132
+ "src/bigru_t/model/vqvae2_hierarchical_flexnet.py",
133
+ "src/bigru_t/model/token_compress.py",
134
+ "src/bigru_t/model/embedding_reconfig.py",
135
+ "src/bigru_t/model/attention_multimodal.py",
136
+ "src/bigru_t/attention/window_context.py",
137
+ # Training
138
+ "src/bigru_t/training/mtp.py",
139
+ "src/bigru_t/training/ewc.py",
140
+ # Quantization
141
+ "src/bigru_t/quantization/smoothquant_compressor.py",
142
+ "src/bigru_t/quantization/w8a8_smoothquant.py",
143
+ "src/bigru_t/quantization/quantized_linear.py",
144
+ # Reasoning
145
+ "src/bigru_t/reasoning/thinking.py",
146
+ "src/bigru_t/reasoning/reasoning_engine.py",
147
+ "src/bigru_t/reasoning/circular_orchestration.py",
148
+ "src/bigru_t/reasoning/tool_agent.py",
149
+ "src/bigru_t/reasoning/distributed_reasoning_system.py",
150
+ "src/bigru_t/reasoning/cyclic_reasoning.py",
151
+ "src/bigru_t/reasoning/consensus_sampling.py",
152
+ # Data + utils
153
+ "src/bigru_t/data/streaming_datasets.py",
154
+ "src/bigru_t/utils/xeon_runtime.py",
155
+ # V6.5-attn-v3 reports (NEW canonical — V6.5-fix: dynamic labels + inference punishment)
156
+ "v6_5_7ds_attn_v3_report.json",
157
+ "v6_5_7ds_attn_v3_training_metrics.json",
158
+ "v6_5_7ds_attn_v3_module_analysis.json",
159
+ "v6_5_7ds_attn_v3_script_activity.json",
160
+ "v6_5_7ds_attn_v3_ewc_w8a8_benchmark.json",
161
+ "v6_5_7ds_attn_v3_reasoning_eval.json",
162
+ "v6_5_7ds_attn_v3_w8a8_compression.json",
163
+ "v6_5_7ds_attn_v3_attention_eval.json",
164
+ "v6_5_7ds_attn_v3_user_questions.json",
165
+ "v6_5_7ds_attn_v3_predict_fix_eval.json",
166
+ "v6_5_7ds_attn_v3_inference_punishment.json",
167
+ "v6_5_7ds_attn_v3_model_states.pt",
168
+ "v6_5_7ds_attn_v3_upload_report.json",
169
+ # V6.4 reports (kept as predecessor baseline)
170
+ "v6_4_report.json",
171
+ "v6_4_training_metrics.json",
172
+ "v6_4_upload_report.json",
173
+ # V6.5-attn-v3 scripts (the new canonicals)
174
+ "scripts/train_v6_5_7ds_attn_v3.py",
175
+ "scripts/upload_v6_5_7ds_attn_v3.py",
176
+ "scripts/train_v6_4.py",
177
+ # Misc
178
+ "requirements.txt",
179
+ "README.md",
180
+ "docs/analysis.md",
181
+ ]
182
+
183
+
184
+ def main() -> int:
185
+ print("=" * 72)
186
+ print("V6.5-attn-v3 — UPLOAD TO HUGGINGFACE (dynamic labels + inference punishment)")
187
+ print("=" * 72)
188
+ hf_token = os.environ.get("HF_TOKEN")
189
+ if not hf_token:
190
+ print("ERROR: HF_TOKEN not set in env")
191
+ return 1
192
+ print(f" HF_TOKEN loaded from env (length={len(hf_token)})")
193
+
194
+ # ── Step 1: Pre-upload safety — scrub any HF token from scripts ──────────
195
+ token_pattern = re.compile(r'hf_[A-Za-z0-9]{32,}')
196
+ print("\n [1/5] Pre-upload safety: scrubbing token from scripts...")
197
+ scrubbed_count = 0
198
+ for script_path in BIGRU_ROOT.glob("scripts/*.py"):
199
+ try:
200
+ content = script_path.read_text()
201
+ if token_pattern.search(content):
202
+ scrubbed = token_pattern.sub("hf_<REDACTED_TOKEN>", content)
203
+ script_path.write_text(scrubbed)
204
+ scrubbed_count += 1
205
+ print(f" SCRUBBED: {script_path.name}")
206
+ except Exception as e:
207
+ print(f" SKIP {script_path.name}: {e}")
208
+ if scrubbed_count == 0:
209
+ print(" (no token leakage found in scripts)")
210
+
211
+ # Also scrub from KLS source files
212
+ print(" Also scrubbing src/bigru_t/...")
213
+ for src_path in (BIGRU_ROOT / "src" / "bigru_t").rglob("*.py"):
214
+ try:
215
+ content = src_path.read_text()
216
+ if token_pattern.search(content):
217
+ scrubbed = token_pattern.sub("hf_<REDACTED_TOKEN>", content)
218
+ src_path.write_text(scrubbed)
219
+ scrubbed_count += 1
220
+ print(f" SCRUBBED: {src_path.relative_to(BIGRU_ROOT)}")
221
+ except Exception:
222
+ pass
223
+
224
+ # ── Step 2: Import HF API + check repo access ────────────────────────────
225
+ print("\n [2/5] Connecting to HuggingFace repo...")
226
+ try:
227
+ from huggingface_hub import HfApi, upload_folder
228
+ except ImportError:
229
+ print("ERROR: huggingface_hub not installed")
230
+ return 1
231
+
232
+ api = HfApi(token=hf_token)
233
+ repo_id = "PowerMachine/BiGRU_T_version"
234
+ try:
235
+ info = api.repo_info(repo_id=repo_id, repo_type="model")
236
+ print(f" Repo: {repo_id} (existing, files={len(info.siblings)})")
237
+ except Exception as e:
238
+ print(f" ERROR: cannot access repo: {e}")
239
+ return 1
240
+
241
+ files_before = set(s.rfilename for s in info.siblings)
242
+ print(f" Files in repo BEFORE upload: {len(files_before)}")
243
+
244
+ # ── Step 3: Upload current BiGRU_T_version tree as single commit ─────────
245
+ print(f"\n [3/5] Uploading {BIGRU_ROOT} as single commit...")
246
+ t0 = time.time()
247
+ try:
248
+ commit_info = upload_folder(
249
+ repo_id=repo_id, repo_type="model",
250
+ folder_path=str(BIGRU_ROOT),
251
+ commit_message=(
252
+ "V6.5-attn-v3: CORREÇÃO MATEMÁTICA do predict() — rótulos dinâmicos "
253
+ "extraídos do label_registry (substitui 'gato'/'cachorro' fixos). "
254
+ "NOVO: punish_during_inference() — mirror do protocolo de punição "
255
+ "do treinamento (1ª → activate_hypothesis, 2ª → set_ewc_reference). "
256
+ "META ≥6000 samples, attention ACTIVE_AND_FUNCTIONAL, SmoothQuant W8A8, "
257
+ "864 neurons, MTP K=6, 3 perguntas reais sem ajuda (Luva de Pedreiro "
258
+ "Távila, Lula reserva valor, Amazonas força-tarefa vítimas) + punição "
259
+ "na inferência via camada de hipótese. "
260
+ "Removed legacy v2 reports/scripts (superseded by v3)."
261
+ ),
262
+ token=hf_token,
263
+ )
264
+ t1 = time.time()
265
+ print(f" Upload completed in {t1 - t0:.2f}s")
266
+ print(f" Commit: {commit_info.oid}")
267
+ print(f" URL: https://huggingface.co/PowerMachine/BiGRU_T_version/commit/{commit_info.oid}")
268
+ except Exception as e:
269
+ print(f" ERROR: upload failed: {e}")
270
+ return 1
271
+
272
+ # ── Step 4: Remove OLD files from repo (single delete commit) ────────────
273
+ print(f"\n [4/5] Removing {len(OLD_FILES_TO_REMOVE)} old files from repo...")
274
+ try:
275
+ info_after = api.repo_info(repo_id=repo_id, repo_type="model")
276
+ files_after_upload = set(s.rfilename for s in info_after.siblings)
277
+ except Exception as e:
278
+ print(f" WARNING: cannot refresh repo info: {e}")
279
+ files_after_upload = files_before
280
+
281
+ old_files_in_repo = [f for f in OLD_FILES_TO_REMOVE if f in files_after_upload]
282
+ old_files_not_in_repo = [f for f in OLD_FILES_TO_REMOVE if f not in files_after_upload]
283
+ print(f" Old files present in repo (will delete): {len(old_files_in_repo)}")
284
+ print(f" Old files NOT in repo (skip): {len(old_files_not_in_repo)}")
285
+
286
+ delete_errors = []
287
+ commit_del = None
288
+ if old_files_in_repo:
289
+ try:
290
+ from huggingface_hub import CommitOperation
291
+ operations = [CommitOperationDelete(path_in_repo=f) for f in old_files_in_repo]
292
+ print(f" Creating batch delete commit ({len(operations)} files)...")
293
+ t_del_start = time.time()
294
+ commit_del = api.create_commit(
295
+ repo_id=repo_id,
296
+ repo_type="model",
297
+ operations=operations,
298
+ commit_message=(
299
+ f"V6.5-attn-v2 cleanup: remove {len(operations)} legacy/superseded files "
300
+ f"(v3/v4/v5/v6_1/v6_2/v6_3/v6_progressive + old v6_5_*/v6_5_final_*/v6_5_7ds_*/v6_5_7ds_attn_* v1 "
301
+ f"reports/scripts) — replaced by V6.5-attn-v2 canonicals"
302
+ ),
303
+ )
304
+ t_del_end = time.time()
305
+ print(f" Delete commit: {commit_del}")
306
+ print(f" Delete completed in {t_del_end - t_del_start:.2f}s")
307
+ print(f" URL: https://huggingface.co/PowerMachine/BiGRU_T_version/commit/{commit_del}")
308
+ except Exception as e:
309
+ print(f" ERROR: batch delete failed: {e}")
310
+ print(f" Falling back to per-file delete...")
311
+ for f in old_files_in_repo:
312
+ try:
313
+ api.delete_file(
314
+ repo_id=repo_id, repo_type="model",
315
+ path_in_repo=f,
316
+ commit_message=f"V6.5-attn-v2 cleanup: remove legacy {f}",
317
+ token=hf_token,
318
+ )
319
+ print(f" DELETED: {f}")
320
+ except Exception as e2:
321
+ delete_errors.append((f, str(e2)[:100]))
322
+ print(f" FAILED: {f} — {str(e2)[:100]}")
323
+
324
+ # ── Step 5: Verify critical files in repo ────────────────────────────────
325
+ print(f"\n [5/5] Verifying critical V6.5-attn files in repo...")
326
+ all_present = True
327
+ files_in_repo = set()
328
+ try:
329
+ info_final = api.repo_info(repo_id=repo_id, repo_type="model")
330
+ files_in_repo = set(s.rfilename for s in info_final.siblings)
331
+ for cf in CRITICAL_FILES_V65_ATTN:
332
+ present = cf in files_in_repo
333
+ mark = "OK" if present else "MISS"
334
+ print(f" [{mark}] {cf}")
335
+ if not present:
336
+ all_present = False
337
+ print(f"\n Files in repo AFTER cleanup: {len(files_in_repo)}")
338
+ if not all_present:
339
+ print(" WARNING: some critical files missing!")
340
+ except Exception as e:
341
+ print(f" WARNING: cannot verify files: {e}")
342
+
343
+ # ── Save upload report ───────────────────────────────────────────────────
344
+ report = {
345
+ "version": "V6.5-attn-v3",
346
+ "upload_timestamp": time.strftime("%Y-%m-%dT%H:%M:%S"),
347
+ "repo_id": repo_id,
348
+ "upload_commit_oid": commit_info.oid,
349
+ "upload_commit_url": f"https://huggingface.co/PowerMachine/BiGRU_T_version/commit/{commit_info.oid}",
350
+ "upload_duration_s": float(t1 - t0),
351
+ "delete_commit_oid": commit_del,
352
+ "delete_count": len(old_files_in_repo) if old_files_in_repo else 0,
353
+ "delete_errors": delete_errors,
354
+ "n_files_before": len(files_before),
355
+ "n_files_after_upload": len(files_after_upload),
356
+ "n_files_after_cleanup": len(files_in_repo) if files_in_repo else None,
357
+ "old_files_removed": old_files_in_repo,
358
+ "old_files_not_in_repo_skipped": old_files_not_in_repo,
359
+ "critical_files": CRITICAL_FILES_V65_ATTN,
360
+ "all_critical_files_present": all_present,
361
+ "token_scrubbed_count": scrubbed_count,
362
+ }
363
+ report_path = BIGRU_ROOT / "v6_5_7ds_attn_v3_upload_report.json"
364
+ report_path.write_text(json.dumps(report, indent=2, ensure_ascii=False))
365
+ print(f"\n Upload report: {report_path}")
366
+ print("\n" + "=" * 72)
367
+ print("V6.5-attn-v3 — UPLOAD COMPLETED (with old file removal)")
368
+ print("=" * 72)
369
+ return 0
370
+
371
+
372
+ if __name__ == "__main__":
373
+ sys.exit(main())