Q-TensorFormer / src /validator.py
Premchandyadav369
feat: Harden research system with hierarchical KV, phase profiling, matched budgets, and counterfactual validation
8799640
Raw
History Blame Contribute Delete
8.45 kB
"""
src/validator.py
Scientific Claim Validator & Empirical Consistency Auditor for Q-TensorFormer.
Enforces empirical integrity across all research outputs:
1. Validates classification of every reported metric:
MEASURED: Obtained directly from hardware timing/profiling.
ESTIMATED: Computed using explicit, calibrated models.
SIMULATED: Run on classical quantum statevector simulator.
PROJECTED: Theoretical analytical scale-up calculation.
2. Flags and rejects:
- Unsupported "quantum advantage" claims without real QPU hardware execution.
- Latency claims derived solely from FLOP counts.
- "Zero runtime overhead" or "zero overhead" claims (slicing overhead ~20-50 us is empirically verified).
- Unproven asymptotic guarantees (e.g. "asymptotic convergence guaranteed").
- Fabricated benchmark percentages.
3. Audits output artifact completeness:
Verifies that all 13 core JSON result files and 14 publication figure PNGs exist.
"""
import json
import re
import sys
import os
from pathlib import Path
from typing import Dict, List, Tuple, Any, Set, Optional
class ScientificClaimValidator:
"""
Automated validator for research claims, benchmark tables, and model card disclosures.
"""
ALLOWED_CLASSIFICATIONS = {"MEASURED", "ESTIMATED", "SIMULATED", "PROJECTED"}
FORBIDDEN_PHRASES = [
"quantum advantage in nlp",
"quantum speedup on cpu",
"first ever adaptive transformer",
"first entropy-based transformer",
"zero latency overhead with quantum simulation",
"zero runtime overhead",
"asymptotic convergence to the exact boundary",
"asymptotic pareto optimality guaranteed",
]
REQUIRED_JSON_ARTIFACTS = [
"marginal_value_results.json",
"information_state_ablation.json",
"adaptive_rank_results.json",
"nested_tt_analysis.json",
"gqa_audit.json",
"kv_rate_distortion.json",
"controller_convergence.json",
"hysteresis_results.json",
"quantum_utility.json",
"detailed_latency_profile.json",
"pareto_frontiers.json",
"counter_hypothesis_results.json",
"comprehensive_comparison.json",
"baseline_comparison_master.json",
"counterfactual_learning_results.json",
"hierarchical_kv_results.json",
"phase_profiling_results.json",
"matched_budget_evaluations.json",
"workload_adaptation_results.json",
"quantum_utility_boundary.json",
"scaling_projections.json",
]
REQUIRED_FIGURE_ARTIFACTS = [
"figure1_architecture.png",
"figure2_marginal_value_r2.png",
"figure3_8d_lofo_importance.png",
"figure4_nested_tt_suboptimality.png",
"figure5_adaptive_rank_latency_traffic.png",
"figure6_gqa_memory_traffic.png",
"figure7_kv_rate_distortion.png",
"figure8_controller_convergence.png",
"figure9_hysteresis_churn_jitter.png",
"figure10_quantum_utility_tradeoff.png",
"figure11_subsystem_latency_breakdown.png",
"figure12_hardware_roofline.png",
"figure13_multi_pareto_frontiers.png",
"figure14_counter_hypothesis_boundaries.png",
"figure15_baseline_pareto_frontiers.png",
"figure16_baseline_improvement_radar.png",
]
def __init__(self, root_dir: Optional[Path] = None):
self.root_dir = root_dir or Path(__file__).parent.parent
self.validation_errors: List[str] = []
self.warnings: List[str] = []
def validate_metric_record(self, record: Dict[str, Any]) -> bool:
"""Validate a single benchmark metric entry."""
metric_name = record.get("metric", "unknown")
classification = record.get("classification", "").upper()
if classification not in self.ALLOWED_CLASSIFICATIONS:
self.validation_errors.append(
f"Metric '{metric_name}' has invalid classification '{classification}'. "
f"Must be one of: {self.ALLOWED_CLASSIFICATIONS}"
)
return False
if "latency" in metric_name.lower() and classification == "MEASURED" and "hardware" not in record:
self.warnings.append(
f"Measured latency '{metric_name}' should specify hardware device used for measurement."
)
return True
def validate_document_text(self, text: str, doc_name: str = "Document") -> bool:
"""Scan text for forbidden/unsubstantiated claims."""
clean_text = text.lower()
passed = True
for phrase in self.FORBIDDEN_PHRASES:
if phrase in clean_text:
self.validation_errors.append(
f"[{doc_name}] Found unsubstantiated claim phrase: '{phrase}'"
)
passed = False
return passed
def validate_artifacts(self) -> bool:
"""Audit that all empirical outputs and publication figures exist and are valid."""
outputs_dir = self.root_dir / "outputs"
figures_dir = outputs_dir / "figures"
passed = True
if not outputs_dir.exists():
self.validation_errors.append(f"Outputs directory {outputs_dir} does not exist.")
return False
# 1. Audit JSON files
for j_name in self.REQUIRED_JSON_ARTIFACTS:
j_path = outputs_dir / j_name
if not j_path.exists():
self.validation_errors.append(f"Missing required empirical artifact: outputs/{j_name}")
passed = False
else:
try:
with open(j_path, "r", encoding="utf-8") as f:
data = json.load(f)
if not data:
self.validation_errors.append(f"Empty artifact: outputs/{j_name}")
passed = False
except Exception as e:
self.validation_errors.append(f"Corrupt JSON in outputs/{j_name}: {e}")
passed = False
# 2. Audit Figures
if not figures_dir.exists():
self.validation_errors.append(f"Figures directory {figures_dir} does not exist.")
return False
for f_name in self.REQUIRED_FIGURE_ARTIFACTS:
f_path = figures_dir / f_name
if not f_path.exists():
self.validation_errors.append(f"Missing required figure artifact: outputs/figures/{f_name}")
passed = False
elif f_path.stat().st_size < 1000:
self.validation_errors.append(f"Figure {f_name} is too small (< 1KB), possible blank render.")
passed = False
return passed
def generate_report(self) -> Dict[str, Any]:
return {
"status": "FAILED" if self.validation_errors else "PASSED",
"errors": self.validation_errors,
"warnings": self.warnings,
"total_errors": len(self.validation_errors),
"total_warnings": len(self.warnings),
"artifacts_verified": len(self.REQUIRED_JSON_ARTIFACTS) + len(self.REQUIRED_FIGURE_ARTIFACTS),
}
def main():
root_dir = Path(__file__).parent.parent
validator = ScientificClaimValidator(root_dir)
# 1. Audit artifacts
validator.validate_artifacts()
# 2. Audit documents
for doc in ["README.md", "MODEL_CARD.md", "docs/CLAIM_TO_CODE_MAP.md", "docs/ARCHITECTURE_AUDIT.md"]:
p = root_dir / doc
if p.exists():
content = p.read_text(encoding="utf-8")
validator.validate_document_text(content, doc_name=doc)
report = validator.generate_report()
print("=" * 70)
print("SCIENTIFIC CLAIM & ARTIFACT CONSISTENCY VALIDATION REPORT")
print("=" * 70)
print(f"Status: {report['status']}")
print(f"Verified Artifacts: {report['artifacts_verified']} files")
print(f"Errors ({report['total_errors']}):")
for err in report["errors"]:
print(f" [ERROR] {err}")
print(f"Warnings ({report['total_warnings']}):")
for warn in report["warnings"]:
print(f" [WARN] {warn}")
print("=" * 70)
if report["status"] == "FAILED":
sys.exit(1)
else:
print("[PASSED] All scientific claims and empirical artifacts verified successfully!")
if __name__ == "__main__":
from typing import Optional
main()