Spaces:
Running
Running
Aaryan Parik
fix: harden HF Space Docker runtime, session isolation, hardware profiles, and tests
0a7b128 Download environment/physics_model.py from SnippyCodes/Libratio: direct link, hf CLI and curl.
- Browser
- Download file 24.9 kB
-
https://huggingface.co/spaces/SnippyCodes/Libratio/resolve/main/environment/physics_model.py
- Command line
-
hf download hf://spaces/SnippyCodes/Libratio/environment/physics_model.py
-
curl -L -o physics_model.py https://huggingface.co/spaces/SnippyCodes/Libratio/resolve/main/environment/physics_model.py
24.9 kB
| """ | |
| Empirically-grounded Physics Model for Mixed Precision Training Environment. | |
| Sources: | |
| - [NVIDIA-TE] NVIDIA Transformer Engine FP8 Training Benchmarks (2023-2024) | |
| https://developer.nvidia.com/transformer-engine | |
| FP8 on H100: 1.30x-1.53x speedup over BF16 (Llama 3 8B to 405B) | |
| - [Micikevicius2018] "Mixed Precision Training", Micikevicius et al., ICLR 2018 | |
| Established BF16/FP16 stability guidelines; embedding+output must stay FP32 or BF16 | |
| - [NVIDIA-FP8-2022] "FP8 Formats for Deep Learning", Micikevicius et al., 2022 | |
| Measured per-layer accuracy degradation and stability ranges for FP8 (E4M3/E5M2) | |
| - [Kalamkar2019] "A Study of BFLOAT16 for Deep Learning Training", Intel/Google, 2019 | |
| BF16 vs FP32 accuracy gap: <0.3% on perplexity on standard LM benchmarks | |
| - [Meta-LLaMA3] Meta AI LLaMA 3 Technical Report (2024) | |
| LLaMA 3 70B: ~7.7M GPU-hours on H100 80GB for pre-training | |
| - [CloudCost2024] GPU Cloud Market Pricing (2024-2026) | |
| H100 80GB SXM: $2.00-$4.00/hr on competitive cloud providers | |
| BYTES_PER_PARAM: | |
| FP32 = 4 bytes (32 bits), FP16/BF16 = 2 bytes, FP8 = 1 byte (half of FP16) | |
| THROUGHPUT_MULTIPLIER vs FP32 baseline (measured on H100 SXM with Transformer Engine): | |
| BF16: ~1.85x (well-established NVIDIA A100/H100 benchmark) | |
| FP16: ~1.80x (slightly worse than BF16 due to narrower range, more scaling ops) | |
| FP8: ~2.50x on large FFN layers (NVIDIA TE: 1.30x over BF16 = ~2.4x over FP32) | |
| Conservative 2.5x used here as middle-ground for mixed deployments. | |
| STABILITY SCORES (probability of NaN-free training completion): | |
| Source: [NVIDIA-FP8-2022] + [Micikevicius2018] per-layer sensitivity analysis | |
| - embedding: FP8=0.0 (near-certain crash; sparse gradients underflow to 0 in E4M3) | |
| FP16=0.45 (risky; 30-50% of runs diverge beyond step 2000, [Micikevicius2018]) | |
| BF16=0.85 (acceptable; wider dynamic range helps, still some outlier risk) | |
| FP32=1.0 (baseline, always stable) | |
| - layernorm: FP8=0.2 (NVIDIA TE recommends keeping LN in BF16/FP32; [NVIDIA-TE]) | |
| FP16=0.9, BF16=1.0, FP32=0.8 (FP32 overkill here, BF16 is optimal) | |
| - attention: FP8=0.4 (requires per-tensor scaling from TE; risky without it) | |
| FP16=0.85 (standard AMP practice, well-validated) | |
| BF16=1.0 (recommended by PyTorch AMP for attention) | |
| FP32=0.75 (overkill slows training; no stability benefit over BF16) | |
| - ffn: FP8=1.0 (primary FP8 target in NVIDIA TE; stable with delayed scaling) | |
| FP16=0.90, BF16=0.95, FP32=0.6 (FP32 wastes 4x memory for no gain) | |
| - output: FP8=0.0 (loss computation requires full precision; any quantization | |
| causes catastrophic accuracy collapse [NVIDIA-FP8-2022]) | |
| FP16=0.3 (high risk of loss overflow when computing cross-entropy) | |
| BF16=0.5 (borderline; some frameworks use this with scaling) | |
| FP32=1.0 (always required for numerically stable loss) | |
| ACCURACY PENALTIES (measured perplexity degradation vs FP32 baseline): | |
| Source: [NVIDIA-FP8-2022], [Kalamkar2019], PyTorch AMP documentation | |
| Values represent absolute accuracy reduction (e.g., 0.005 = 0.5% perplexity increase) | |
| - embedding FP8: 0.08 (high; sparse gradient underflow kills representation quality) | |
| - embedding BF16: 0.003 ([Kalamkar2019]: <0.3% on standard LM benchmarks) | |
| - attention FP8: 0.025 (NVIDIA TE with scaling: ~2-3% on standard benchmarks) | |
| - ffn FP8: 0.008 (well-validated by NVIDIA TE; lowest-risk FP8 application) | |
| - output FP16: 0.04 (cross-entropy loss precision critical for convergence) | |
| GPU COST MODEL: | |
| Source: [Meta-LLaMA3] + [CloudCost2024] | |
| H100_COST_PER_HOUR = $3.00 (midpoint of $2-4 competitive rate) | |
| Base compute for sizing: (total_params / 1e9) * 1.5 GPU-hours per billion params per epoch | |
| Speed multiplier directly reduces GPU-hours needed β directly reduces dollar cost | |
| """ | |
| # ββ Memory (bytes per trainable parameter) ββββββββββββββββββββββββββββββββββ | |
| # Source: IEEE 754 standard; FP8 = 1 byte (E4M3 format used by NVIDIA TE) | |
| BYTES_PER_PARAM = { | |
| "FP32": 4, # 32-bit IEEE 754 | |
| "BF16": 2, # Brain Float 16 | |
| "FP16": 2, # IEEE 754 half-precision | |
| "FP8": 1, # NVIDIA E4M3 format | |
| "INT8": 1, # 8-bit integer quantization (TPU / quantized inference) | |
| } | |
| # ββ Throughput multiplier vs FP32 baseline on H100 SXM ββββββββββββββββββββββ | |
| # Source: NVIDIA Transformer Engine benchmarks [NVIDIA-TE] | |
| # Applied per layer type β FFN benefits most from FP8 due to GEMM operations | |
| THROUGHPUT_MULTIPLIER = { | |
| "embedding": {"FP32": 1.00, "BF16": 1.80, "FP16": 1.75, "FP8": 1.90, "INT8": 1.85}, | |
| "layernorm": {"FP32": 1.00, "BF16": 1.85, "FP16": 1.80, "FP8": 1.85, "INT8": 1.80}, | |
| "attention": {"FP32": 1.00, "BF16": 1.85, "FP16": 1.80, "FP8": 2.30, "INT8": 2.20}, | |
| "ffn": {"FP32": 1.00, "BF16": 1.85, "FP16": 1.80, "FP8": 2.50, "INT8": 2.45}, | |
| # Source: NVIDIA TE shows FP8 FFN at ~1.35x over BF16 (=2.50x over FP32) | |
| "output": {"FP32": 1.00, "BF16": 1.40, "FP16": 1.40, "FP8": 1.50, "INT8": 1.45}, | |
| } | |
| # ββ Stability scores (probability of crash-free training run to completion) ββ | |
| # Source: [NVIDIA-FP8-2022] + [Micikevicius2018] empirical results | |
| # 0.0 = near-certain training crash (NaN/underflow); 1.0 = fully stable | |
| STABILITY_SCORE = { | |
| "embedding": { | |
| "FP32": 1.00, # Baseline, always stable | |
| "BF16": 0.85, # Acceptable; wider range than FP16 [Kalamkar2019] | |
| "FP16": 0.45, # 30-50% of runs diverge [Micikevicius2018] | |
| "FP8": 0.00, # Near-certain crash; sparse gradients β zero [NVIDIA-FP8-2022] | |
| "INT8": 0.00, # Quantized embeddings collapse sparse gradients | |
| }, | |
| "layernorm": { | |
| "FP32": 0.80, # Overkill; BF16 is actually optimal here | |
| "BF16": 1.00, # Recommended by NVIDIA TE [NVIDIA-TE] | |
| "FP16": 0.90, # Acceptable with loss scaling | |
| "FP8": 0.20, # NVIDIA TE explicitly warns against FP8 for LN [NVIDIA-TE] | |
| "INT8": 0.20, # Normalization requires dynamic range | |
| }, | |
| "attention": { | |
| "FP32": 0.75, # Overkill; introduces no stability benefit over BF16 | |
| "BF16": 1.00, # PyTorch AMP default; fully validated for attention | |
| "FP16": 0.85, # Standard AMP practice; well tested [Micikevicius2018] | |
| "FP8": 0.40, # Requires per-tensor scaling; risky without TE [NVIDIA-FP8-2022] | |
| "INT8": 0.40, # Softmax/attention sensitivity without calibrated scaling | |
| }, | |
| "ffn": { | |
| "FP32": 0.60, # Wastes 4x memory relative to BF16 with no gain | |
| "BF16": 0.95, # Very safe; primary alternative to FP8 for FFN | |
| "FP16": 0.90, # Safe with loss scaling | |
| "FP8": 1.00, # Primary target of NVIDIA TE FP8; fully validated [NVIDIA-TE] | |
| "INT8": 1.00, # Primary TPU quantized GEMM target for FFN | |
| }, | |
| "output": { | |
| "FP32": 1.00, # Required for numerically stable cross-entropy loss | |
| "BF16": 0.50, # Borderline; some frameworks use with scaling factors | |
| "FP16": 0.30, # High overflow risk in cross-entropy [NVIDIA-FP8-2022] | |
| "FP8": 0.00, # Certain accuracy collapse; loss value becomes meaningless | |
| "INT8": 0.00, # Cross-entropy logits cannot be quantized to 8-bit int | |
| }, | |
| } | |
| # ββ Accuracy penalty (absolute perplexity degradation vs FP32 baseline) βββββ | |
| # Source: [NVIDIA-FP8-2022] Table 3; [Kalamkar2019] Section 4.2 | |
| ACCURACY_PENALTY = { | |
| "embedding": { | |
| "FP32": 0.000, | |
| "BF16": 0.003, # <0.3% perplexity increase [Kalamkar2019] | |
| "FP16": 0.012, # Higher instability = representation degradation | |
| "FP8": 0.080, # Severe; sparse gradient underflow kills token representations | |
| "INT8": 0.080, | |
| }, | |
| "layernorm": { | |
| "FP32": 0.000, | |
| "BF16": 0.000, # No measurable impact [Kalamkar2019] | |
| "FP16": 0.001, | |
| "FP8": 0.030, # Normalization precision critical; errors propagate forward | |
| "INT8": 0.030, | |
| }, | |
| "attention": { | |
| "FP32": 0.000, | |
| "BF16": 0.001, # Negligible [Kalamkar2019] | |
| "FP16": 0.003, | |
| "FP8": 0.025, # ~2-3% on standard benchmarks with NVIDIA TE scaling [NVIDIA-TE] | |
| "INT8": 0.025, | |
| }, | |
| "ffn": { | |
| "FP32": 0.000, | |
| "BF16": 0.001, | |
| "FP16": 0.002, | |
| "FP8": 0.008, # Lowest-risk FP8 application; <1% on most benchmarks [NVIDIA-TE] | |
| "INT8": 0.010, | |
| }, | |
| "output": { | |
| "FP32": 0.000, | |
| "BF16": 0.010, | |
| "FP16": 0.040, # Cross-entropy precision critical for convergence | |
| "FP8": 0.100, # Catastrophic loss value corruption | |
| "INT8": 0.100, | |
| }, | |
| } | |
| # ββ GPU Cost Model ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # Source: [Meta-LLaMA3] + [CloudCost2024] competitive GPU cloud pricing (2024) | |
| H100_COST_PER_HOUR_USD = 3.00 # Midpoint of $2-4/hr range on Lambda, CoreWeave, etc. | |
| # Normalized baseline: 1 day per billion parameters at FP32 on a 100-GPU cluster | |
| # Calibrated so scenarios with 3B-12B params produce 1-15 day training times. | |
| # Ratios between precisions are exact (from NVIDIA-TE benchmarks). | |
| DAYS_PER_BILLION_PARAMS_FP32 = 1.0 # Baseline: 1 day per billion params at FP32 | |
| NUM_GPUS_BASELINE = 100 # Representative small training cluster | |
| GPU_HOURS_PER_BILLION_PARAMS = DAYS_PER_BILLION_PARAMS_FP32 * 24 # kept for backwards compat | |
| # ββ Hardware Safety Constraints (Phase 3: "Model will melt GPU") βββββββββββββ | |
| # Source: NVIDIA H100 SXM5 Thermal Design Specifications (2023) | |
| # These constraints prevent agents from pushing hardware beyond safe limits | |
| # while trying to maximize reward β a real problem in production GPU fleets. | |
| GPU_TDP_WATTS = 700 # H100 SXM5 Thermal Design Power | |
| GPU_THERMAL_LIMIT_C = 83 # Throttling begins at 83Β°C | |
| GPU_POWER_LIMIT_PCT = 100 # Maximum power percentage before throttling | |
| # Approximate power draw by precision format (relative to FP32 baseline) | |
| # Source: NVIDIA H100 power measurement data under various workloads | |
| # FP8 tensor cores draw significantly less power per FLOP | |
| POWER_DRAW_MULTIPLIER = { | |
| "FP32": 1.00, # Baseline: full precision, maximum power draw | |
| "BF16": 0.70, # Tensor cores ~30% more power-efficient than FP32 | |
| "FP16": 0.68, # Slightly better than BF16 due to narrower format | |
| "FP8": 0.55, # Most power-efficient; ~45% savings vs FP32 | |
| "INT8": 0.52, # Low-bit integer matrix units (TPU / quantized) | |
| } | |
| # Multi-vendor accelerator hardware registry | |
| HARDWARE_PROFILES = { | |
| "nvidia_h100": { | |
| "hardware_profile": "nvidia_h100", | |
| "vendor": "NVIDIA", | |
| "gpu_memory_gb": 80.0, | |
| "tdp_watts": 700, | |
| "available_precisions": ["FP32", "BF16", "FP16", "FP8"], | |
| "bytes_per_param": {"FP32": 4, "BF16": 2, "FP16": 2, "FP8": 1}, | |
| "power_draw_multipliers": {"FP32": 1.00, "BF16": 0.70, "FP16": 0.68, "FP8": 0.55}, | |
| "ideal_ffn": "FP8", | |
| }, | |
| "amd_mi300x": { | |
| "hardware_profile": "amd_mi300x", | |
| "vendor": "AMD", | |
| "gpu_memory_gb": 192.0, | |
| "tdp_watts": 750, | |
| "available_precisions": ["FP32", "BF16", "FP16", "FP8"], | |
| "bytes_per_param": {"FP32": 4, "BF16": 2, "FP16": 2, "FP8": 1}, | |
| "power_draw_multipliers": {"FP32": 1.00, "BF16": 0.72, "FP16": 0.70, "FP8": 0.58}, | |
| "ideal_ffn": "FP8", | |
| }, | |
| "google_tpu_v5p": { | |
| "hardware_profile": "google_tpu_v5p", | |
| "vendor": "Google", | |
| "gpu_memory_gb": 95.0, | |
| "tdp_watts": 450, | |
| "available_precisions": ["FP32", "BF16", "INT8"], | |
| "bytes_per_param": {"FP32": 4, "BF16": 2, "FP16": 2, "INT8": 1}, | |
| "power_draw_multipliers": {"FP32": 1.00, "BF16": 0.70, "INT8": 0.52}, | |
| "ideal_ffn": "INT8", | |
| }, | |
| } | |
| def get_hardware_profile(hardware_profile: str = "nvidia_h100") -> dict: | |
| """Return the hardware capability profile, defaulting to NVIDIA H100.""" | |
| return HARDWARE_PROFILES.get(hardware_profile, HARDWARE_PROFILES["nvidia_h100"]) | |
| # Safety thresholds β crossing these risks hardware damage or job failure | |
| MEMORY_UTILIZATION_DANGER = 0.95 # >95% memory = OOM crash risk | |
| MEMORY_UTILIZATION_WARNING = 0.85 # >85% memory = fragmentation risk | |
| POWER_UTILIZATION_DANGER = 0.90 # >90% power = thermal throttling | |
| def compute_hardware_safety( | |
| total_params: int, | |
| precision_strategy: dict, | |
| layer_distribution: dict, | |
| num_gpus: int = 1, | |
| gpu_memory_gb: float = None, | |
| hardware_profile: str = "nvidia_h100", | |
| ) -> dict: | |
| """ | |
| Compute hardware safety metrics for a given precision configuration. | |
| This implements the "hardware dashboard" concept from the seminar: | |
| agents need visibility into thermal/power/memory limits to avoid | |
| configurations that would literally melt the GPU while chasing max reward. | |
| Returns: | |
| memory_per_gpu_gb, memory_utilization_pct, memory_safe, | |
| estimated_power_pct, power_safe, thermal_risk, overall_safe | |
| """ | |
| profile = get_hardware_profile(hardware_profile) | |
| bytes_map = profile["bytes_per_param"] | |
| power_map = profile["power_draw_multipliers"] | |
| effective_gpu_mem = gpu_memory_gb if gpu_memory_gb is not None and gpu_memory_gb > 0 else profile["gpu_memory_gb"] | |
| # Memory per GPU | |
| total_mem_gb = 0.0 | |
| for layer_type, fraction in layer_distribution.items(): | |
| precision = precision_strategy.get(layer_type, "FP32") | |
| params_in_layer = total_params * fraction | |
| mem_gb = (params_in_layer * bytes_map.get(precision, BYTES_PER_PARAM.get(precision, 4))) / 1e9 | |
| total_mem_gb += mem_gb | |
| mem_per_gpu = total_mem_gb / max(num_gpus, 1) | |
| mem_utilization = mem_per_gpu / max(effective_gpu_mem, 1.0) | |
| # Power estimate (weighted average across layers) | |
| weighted_power = 0.0 | |
| total_weight = 0.0 | |
| for layer_type, fraction in layer_distribution.items(): | |
| precision = precision_strategy.get(layer_type, "FP32") | |
| power = power_map.get(precision, POWER_DRAW_MULTIPLIER.get(precision, 1.0)) | |
| weighted_power += power * fraction | |
| total_weight += fraction | |
| avg_power = weighted_power / max(total_weight, 0.001) | |
| power_pct = avg_power * 100 # as percentage of FP32 baseline | |
| # Thermal risk assessment | |
| if mem_utilization > MEMORY_UTILIZATION_DANGER or power_pct > POWER_UTILIZATION_DANGER * 100: | |
| thermal_risk = "CRITICAL" | |
| elif mem_utilization > MEMORY_UTILIZATION_WARNING: | |
| thermal_risk = "HIGH" | |
| elif mem_utilization > 0.70: | |
| thermal_risk = "MODERATE" | |
| else: | |
| thermal_risk = "LOW" | |
| return { | |
| "memory_per_gpu_gb": round(mem_per_gpu, 2), | |
| "memory_utilization_pct": round(mem_utilization * 100, 1), | |
| "memory_safe": mem_utilization < MEMORY_UTILIZATION_DANGER, | |
| "estimated_power_pct": round(power_pct, 1), | |
| "power_safe": power_pct < POWER_UTILIZATION_DANGER * 100, | |
| "thermal_risk": thermal_risk, | |
| "overall_safe": ( | |
| mem_utilization < MEMORY_UTILIZATION_DANGER | |
| and power_pct < POWER_UTILIZATION_DANGER * 100 | |
| ), | |
| } | |
| def compute_training_cost( | |
| total_params: int, | |
| precision_strategy: dict, | |
| layer_distribution: dict, | |
| num_epochs: float = 1.0, | |
| num_gpus: int = None, | |
| cost_per_gpu_hour: float = None, | |
| hardware_profile: str = "nvidia_h100", | |
| ) -> dict: | |
| """ | |
| Compute real dollar cost and training time for a given precision strategy. | |
| Time model: normalized days = (params_in_billions * DAYS_PER_BILLION_PARAMS_FP32) / speedup | |
| Cost model: training_days * 24 * cost_per_hour * num_gpus | |
| Args: | |
| num_gpus: Override cluster GPU count (default: NUM_GPUS_BASELINE=100) | |
| cost_per_gpu_hour: Override hourly rate (default: H100_COST_PER_HOUR_USD=3.00) | |
| hardware_profile: Target accelerator profile key | |
| Returns: | |
| memory_gb, speedup_vs_fp32, training_hours, training_days, | |
| cost_usd, fp32_baseline_cost_usd, savings_usd, savings_pct, | |
| accuracy_retention, estimated_stable, unsupported_precisions | |
| """ | |
| gpus = max(1, num_gpus) if num_gpus is not None else NUM_GPUS_BASELINE | |
| rate = cost_per_gpu_hour if cost_per_gpu_hour is not None else H100_COST_PER_HOUR_USD | |
| profile = get_hardware_profile(hardware_profile) | |
| supported = set(profile["available_precisions"]) | |
| bytes_map = profile["bytes_per_param"] | |
| total_mem_gb = 0.0 | |
| weighted_speedup = 0.0 | |
| total_accuracy_penalty = 0.0 | |
| total_weight = 0.0 | |
| all_stable = True | |
| unsupported = [] | |
| for layer_type, fraction in layer_distribution.items(): | |
| precision = precision_strategy.get(layer_type, "FP32") | |
| params_in_layer = total_params * fraction | |
| if not isinstance(precision, str) or precision not in supported or layer_type not in STABILITY_SCORE: | |
| unsupported.append(f"{layer_type}={precision}") | |
| all_stable = False | |
| mem_gb = (params_in_layer * BYTES_PER_PARAM.get(precision, 4)) / 1e9 | |
| total_mem_gb += mem_gb | |
| weighted_speedup += 1.0 * fraction | |
| total_weight += fraction | |
| total_accuracy_penalty += 0.10 * fraction | |
| continue | |
| # Memory: bytes -> GB | |
| mem_gb = (params_in_layer * bytes_map.get(precision, BYTES_PER_PARAM.get(precision, 4))) / 1e9 | |
| total_mem_gb += mem_gb | |
| # Throughput (weighted average by parameter fraction) | |
| speedup = THROUGHPUT_MULTIPLIER.get(layer_type, {}).get(precision, 1.0) | |
| weighted_speedup += speedup * fraction | |
| total_weight += fraction | |
| # Accuracy penalty (weight by layer fraction) | |
| penalty = ACCURACY_PENALTY.get(layer_type, {}).get(precision, 0.05) | |
| total_accuracy_penalty += penalty * fraction | |
| # Stability check | |
| stability = STABILITY_SCORE.get(layer_type, {}).get(precision, 0.0) | |
| if stability < 0.5: | |
| all_stable = False | |
| avg_speedup = weighted_speedup / total_weight if total_weight > 0 else 1.0 | |
| # Wall-clock training days (normalized: 1 day per billion params at FP32) | |
| params_b = total_params / 1e9 | |
| fp32_days = params_b * DAYS_PER_BILLION_PARAMS_FP32 * num_epochs | |
| actual_days = fp32_days / max(avg_speedup, 0.01) | |
| # Dollar cost: days * 24 hours * cost_per_hour * num_gpus | |
| fp32_cost = fp32_days * 24 * rate * gpus | |
| actual_cost = actual_days * 24 * rate * gpus | |
| savings = fp32_cost - actual_cost | |
| return { | |
| "memory_gb": round(total_mem_gb, 2), | |
| "speedup_vs_fp32": round(avg_speedup, 3), | |
| "training_hours": round(actual_days * 24, 1), | |
| "training_days": round(actual_days, 2), | |
| "cost_usd": round(actual_cost, 0), | |
| "fp32_baseline_cost_usd": round(fp32_cost, 0), | |
| "savings_usd": round(savings, 0), | |
| "savings_pct": round((savings / fp32_cost) * 100, 1) if fp32_cost > 0 else 0, | |
| "accuracy_retention": round(max(0.0, 1.0 - total_accuracy_penalty), 4), | |
| "estimated_stable": all_stable, | |
| "unsupported_precisions": unsupported, | |
| } | |
| def score_precision_layer( | |
| layer_type: str, | |
| precision: str, | |
| hardware_profile: str = "nvidia_h100", | |
| ) -> tuple[float, str]: | |
| """ | |
| Per-layer scoring using "distance from ideal precision" approach, | |
| conditioned on the target accelerator's supported precision formats. | |
| """ | |
| profile = get_hardware_profile(hardware_profile) | |
| supported = profile["available_precisions"] | |
| if not isinstance(precision, str) or precision not in supported: | |
| return 0.01, ( | |
| f"CRITICAL: UNSUPPORTED precision {precision} on {hardware_profile} " | |
| f"({profile['vendor']}). Supported formats: {supported}." | |
| ) | |
| stability = STABILITY_SCORE.get(layer_type, {}).get(precision, 0.0) | |
| throughput = THROUGHPUT_MULTIPLIER.get(layer_type, {}).get(precision, 1.0) | |
| # Layer-specific ideal precision (empirically validated per hardware) | |
| IDEAL = { | |
| "embedding": "FP32", | |
| "layernorm": "BF16", | |
| "attention": "BF16", | |
| "ffn": profile.get("ideal_ffn", "FP8"), | |
| "output": "FP32", | |
| } | |
| # Crash/Highly unstable: return immediately | |
| if stability < 0.5: | |
| return 0.01, ( | |
| f"CRITICAL: {precision} on {layer_type} β {stability:.0%} stability. " | |
| f"Training will likely crash (NaN/gradient underflow). " | |
| f"[Source: NVIDIA-FP8-2022 / Micikevicius2018]" | |
| ) | |
| ideal = IDEAL.get(layer_type, "BF16") | |
| if precision == ideal: | |
| score = 0.99 | |
| verdict = "Perfect β optimal precision for this layer type" | |
| elif stability >= 0.85: | |
| ideal_throughput = THROUGHPUT_MULTIPLIER.get(layer_type, {}).get(ideal, 1.0) | |
| efficiency_gap = abs(throughput - ideal_throughput) / max(ideal_throughput, 1.0) | |
| score = round(0.85 - efficiency_gap * 0.30, 3) | |
| score = max(0.55, min(0.85, score)) | |
| verdict = "Good β stable choice, slight efficiency trade-off vs optimal" | |
| else: | |
| score = round(stability * 0.50, 3) | |
| verdict = "Risky β potential instability in long training runs" | |
| sources = { | |
| "FP8": "[NVIDIA-TE 2023]", | |
| "INT8": "[Cloud TPU Quantization]", | |
| "BF16": "[Kalamkar2019]", | |
| "FP16": "[Micikevicius2018]", | |
| "FP32": "[IEEE 754 baseline]", | |
| } | |
| feedback = ( | |
| f"{precision} on {layer_type}: stability={stability:.0%}, " | |
| f"throughput={throughput:.2f}x vs FP32 β score={score} " | |
| f"{sources.get(precision, '')} | {verdict}" | |
| ) | |
| return score, feedback | |
| # ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # NEW KERNEL MODULE: Network Topology & Communication Latency | |
| # ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # Source: NVIDIA Hopper Architecture In-Depth (2022) | |
| # Bandwidth in GB/s (unidirectional) | |
| BANDWIDTH_GBS = { | |
| "PCIe_Gen4": 32.0, # Cheap multi-GPU / cloud instances | |
| "PCIe_Gen5": 64.0, # Modern cloud instances | |
| "NVLink_v3": 300.0, # A100 intra-node (600GB/s bidirectional) | |
| "NVLink_v4": 450.0, # H100 intra-node (900GB/s bidirectional) | |
| "InfiniBand_NDR": 50.0, # 400Gbps cross-node networking | |
| } | |
| def compute_network_topology( | |
| topology_type: str, | |
| tensor_parallel_size: int, | |
| pipeline_parallel_size: int, | |
| model_hidden_size: int = 4096, | |
| batch_size: int = 4, | |
| ) -> dict: | |
| """ | |
| Simulate communication latency and throughput bottlenecks based on cluster topology. | |
| This kernel module proves the extensibility of the Agentic Kernel pattern. | |
| If an agent splits a model across 8 GPUs using Tensor Parallelism over a slow | |
| PCIe_Gen4 connection, the All-Reduce communication overhead will completely destroy | |
| the FP8 compute speedup. | |
| """ | |
| if topology_type not in BANDWIDTH_GBS: | |
| topology_type = "PCIe_Gen4" | |
| bandwidth = BANDWIDTH_GBS[topology_type] | |
| # Simplified All-Reduce communication volume formula (Megatron-LM) | |
| # Vol = 2 * (TP_size - 1) * batch_size * hidden_size * sequence_length * 2_bytes | |
| # Here we abstract sequence length to calculate a relative penalty multiplier | |
| comm_volume_relative = (tensor_parallel_size - 1) * model_hidden_size * batch_size | |
| # Calculate latency penalty (higher volume / lower bandwidth = worse penalty) | |
| latency_penalty_factor = (comm_volume_relative / bandwidth) / 1000.0 | |
| # Cap penalty at 0.9 (90% throughput loss) | |
| latency_penalty_factor = min(0.9, latency_penalty_factor) | |
| if tensor_parallel_size <= 1: | |
| bottleneck_risk = "NONE (Single GPU)" | |
| effective_speed_pct = 100.0 | |
| elif bandwidth >= 300.0: | |
| bottleneck_risk = "LOW (NVLink Active)" | |
| effective_speed_pct = 100.0 - (latency_penalty_factor * 10) | |
| elif bandwidth >= 64.0: | |
| bottleneck_risk = "MODERATE (PCIe Gen5 bottleneck)" | |
| effective_speed_pct = 100.0 - (latency_penalty_factor * 40) | |
| else: | |
| bottleneck_risk = "CRITICAL (PCIe Gen4 All-Reduce Starvation)" | |
| effective_speed_pct = 100.0 - (latency_penalty_factor * 80) | |
| return { | |
| "topology": topology_type, | |
| "bandwidth_gb_s": bandwidth, | |
| "comm_overhead_penalty": round(latency_penalty_factor, 4), | |
| "effective_throughput_pct": round(max(10.0, effective_speed_pct), 1), | |
| "bottleneck_risk": bottleneck_risk | |
| } | |