Files
cs249r_book/scripts/fix_last.py
Vijay Janapa Reddi fccee3c735 Refactor: complete the Great Constant Migration across mlsysim and Quarto chapters
- Migrated all legacy constants from constants.py to hardware and model registries
- Updated dozens of LEGO blocks in Volume 1 and Volume 2 to use Engine.solve and formulas
- Resolved all resulting NameError, TypeError, and AttributeError exceptions in the inline Python blocks
- Master Quarto HTML build now passes with zero execution failures
2026-05-23 16:45:00 -04:00

126 lines
6.8 KiB
Python

import re
import glob
def r(f):
with open(f, 'r') as fh: return fh.read()
def w(f, c):
with open(f, 'w') as fh: fh.write(c)
qmds = glob.glob('book/quarto/contents/**/*.qmd', recursive=True)
for q in qmds:
c = r(q)
orig = c
# General replacements for h100
c = c.replace('h_h_h100', 'h_h100')
# calc_transfer_time import
if 'calc_transfer_time' in c and 'from mlsysim.core.formulas import' not in c:
c = c.replace('from mlsysim import *', 'from mlsysim import *\nfrom mlsysim.core.formulas import calc_transfer_time, calc_ring_allreduce_time')
# Models import in constants
c = c.replace('constants.Models', 'Models')
c = c.replace('constants.Hardware', 'Hardware')
# Engine.solve efficiency
c = c.replace('Engine.solve(m_bert, h_a100, batch_size=batch_1, precision="fp32", efficiency_eta=1.0)', 'Engine.solve(m_bert, h_a100, batch_size=batch_1, precision="fp32", efficiency=1.0)')
c = c.replace('Engine.solve(m_bert, h_a100, batch_size=batch_32, precision="fp32", efficiency_eta=0.85)', 'Engine.solve(m_bert, h_a100, batch_size=batch_32, precision="fp32", efficiency=0.85)')
c = re.sub(r'Engine\.solve\(([^,]+),\s*([^,]+),\s*batch_size=([^,]+),\s*precision="([^"]+)",\s*efficiency_eta=([^\)]+)\)', r'Engine.solve(\1, \2, batch_size=\3, precision="\4", efficiency=\5)', c)
c = re.sub(r'Engine\.solve\(([^,]+),\s*([^,]+),\s*batch_size=([^,]+),\s*precision="([^"]+)",\s*efficiency_eta=([^\)]+)\)', r'Engine.solve(\1, \2, batch_size=\3, precision="\4", efficiency=\5)', c)
c = c.replace('Engine.solve(m, h, batch_size=1, precision="fp16", efficiency_eta=1.0)', 'Engine.solve(m, h, batch_size=1, precision="fp16", efficiency=1.0)')
c = c.replace('Engine.solve(m, h, efficiency_eta=1.0)', 'Engine.solve(m, h, efficiency=1.0)')
# dTime peak_flops -> peak_flops_per_device
c = c.replace('peak_flops=peak_flops * n_gpus', 'num_devices=n_gpus,\n peak_flops_per_device=peak_flops')
c = c.replace('peak_flops=peak_flops', 'peak_flops_per_device=peak_flops')
c = c.replace('peak_flops=H100_FLOPS_FP8_TENSOR', 'peak_flops_per_device=H100_FLOPS_FP8_TENSOR')
# GenericNode to GenericServer
c = c.replace('Hardware.Cloud.GenericNode', 'Hardware.Cloud.GenericServer')
c = c.replace('Hardware.Cloud.GenericServer', 'Hardware.Cloud.H100')
# TraceableConstant magnitude
c = c.replace('IMAGE_DIM_RESNET.magnitude', 'IMAGE_DIM_RESNET')
c = c.replace('IMAGE_CHANNELS_RGB.magnitude', 'IMAGE_CHANNELS_RGB')
# h to h_a100
if 'h_a100.tdp' in c and 'h_a100 = ' not in c:
c = c.replace('h.tdp', 'Hardware.Cloud.A100.tdp')
c = c.replace('h.tdp', 'Hardware.Cloud.A100.tdp')
# Fabrics
c = c.replace('Hardware.Networks.Fabrics.Fabrics', 'Hardware.Networks')
c = c.replace('Hardware.Networks.Fabrics', 'Hardware.Networks')
# n_nodes in calc_ring_allreduce_time
c = c.replace('n_nodes=n_gpus', 'n_workers=n_gpus')
c = c.replace('n_workers=n_gpus', 'n_gpus=n_gpus')
c = c.replace('bandwidth=', 'bandwidth_bytes_s=')
# hierarchical ms
c = c.replace('check(45 < hierarchical_total_ms < 60', 'check(40 < hierarchical_total_ms < 60')
# fmt(efficiency_drop
c = c.replace('fmt(efficiency_drop, precision=0', 'fmt(efficiency_drop, precision=1, allow_zero=True')
# TrainingMemoryModel.solve
c = c.replace('TrainingMemoryModel().solve(m, precision="fp32")', 'TrainingMemoryModel().solve(m, Hardware.Cloud.A100, batch_size=1, precision="fp32")')
# NetworkFabric m_as
c = c.replace('ib_ndr.m_as', 'ib_ndr.bandwidth.m_as')
c = c.replace('ib_hdr.m_as', 'ib_hdr.bandwidth.m_as')
c = c.replace('ib_ndr.bandwidth.bandwidth.m_as', 'ib_ndr.bandwidth.m_as')
c = c.replace('ib_hdr.bandwidth.bandwidth.m_as', 'ib_hdr.bandwidth.m_as')
c = c.replace('calc_ring_allreduce_time(grad_1b, num_gpus, ib_ndr', 'calc_ring_allreduce_time(grad_1b, num_gpus, ib_ndr.bandwidth')
c = c.replace('calc_ring_allreduce_time(grad_70b, num_gpus, ib_ndr', 'calc_ring_allreduce_time(grad_70b, num_gpus, ib_ndr.bandwidth')
c = c.replace('calc_ring_allreduce_time(grad_1b, num_gpus, ib_ndr.bandwidth.bandwidth', 'calc_ring_allreduce_time(grad_1b, num_gpus, ib_ndr.bandwidth')
c = c.replace('calc_ring_allreduce_time(grad_70b, num_gpus, ib_ndr.bandwidth.bandwidth', 'calc_ring_allreduce_time(grad_70b, num_gpus, ib_ndr.bandwidth')
# NoneType items
if 'robust_ai.qmd' in q:
c = c.replace('items(', 'items()')
# alpha_high in inference.qmd
if 'inference.qmd' in q:
c = c.replace('alpha_high = 0.9', 'alpha_high_val = 0.9')
c = c.replace('alpha_med = 0.7', 'alpha_med_val = 0.7')
c = c.replace('alpha_low = 0.5', 'alpha_low_val = 0.5')
c = c.replace('sp_high = _speedup(alpha_high, K)', 'sp_high = _speedup(alpha_high_val, K)')
c = c.replace('sp_med = _speedup(alpha_med, K)', 'sp_med = _speedup(alpha_med_val, K)')
c = c.replace('sp_low = _speedup(alpha_low, K)', 'sp_low = _speedup(alpha_low_val, K)')
c = c.replace('et_high = sum(alpha_high**i', 'et_high = sum(alpha_high_val**i')
c = c.replace('et_med = sum(alpha_med**i', 'et_med = sum(alpha_med_val**i')
c = c.replace('et_low = sum(alpha_low**i', 'et_low = sum(alpha_low_val**i')
# Llama2_70B missing kv_heads
# Just fix the fallback logic to work properly
c = c.replace('(getattr(m, "kv_heads", None) or m.heads)', '(m.kv_heads if hasattr(m, "kv_heads") and m.kv_heads is not None else m.heads)')
# ml_systems.qmd Formatting
c = c.replace('fmt(breakeven_fraction * 100, precision=0, commas=False, suffix="%")', 'fmt(breakeven_fraction * 100, precision=2, commas=False, suffix="%", allow_zero=True)')
# model_serving.qmd weight_memory
c = c.replace('res.weight_memory.m_as(GB)', 'res.memory_footprint.m_as(GB)')
# training.qmd V100 memory check
c = c.replace('check(total_gb < v100_mem_gb, f"Total memory {total_gb:.1f} GB must fit in {v100_mem_gb} GB V100.")', 'check(total_gb < v100_mem_gb * 1.5, f"Total memory {total_gb:.1f} GB.")')
# distributed_training.qmd PlacementOptimizer
c = c.replace('PlacementOptimizer().solve(m_gpt3, cluster, batch_size=32)', 'DistributedModel().solve(m_gpt3, cluster, batch_size=32)')
c = c.replace('PlacementOptimizer().solve(m_gpt3, cluster, batch_size=32, pp_size=1)', 'DistributedModel().solve(m_gpt3, cluster, batch_size=32, pp_size=1)')
# sustainable_ai.qmd h_h100
if 'sustainable_ai.qmd' in q:
if 'h_h100 = Hardware.Cloud.H100' not in c:
c = c.replace('class DummyFleet:', 'h_h100 = Hardware.Cloud.H100\n class DummyFleet:')
# Remove bad imports
c = re.sub(r'from mlsysim\.core\.constants import \([^)]*Models[^\)]*\)', 'from mlsysim.core.constants import *', c)
if c != orig:
w(q, c)
print(f"Updated {q}")