% =============================================================================
% MLSys·im Tutorial — Module 3: Scale, Dollars, and Carbon
% =============================================================================
\documentclass[aspectratio=169, 12pt]{beamer}
\usepackage{../../../slides/assets/beamerthememlsys}

\mlsyssetup{
  volume       = {Tutorial},
  chapter      = {Module 3},
  logo         = {../../../slides/assets/img/logo-mlsysbook.png},
  instlogo     = {../../../slides/assets/img/logo-harvard.png},
  chaptertitle = {MLSys·im: Scale, Dollars, and Carbon},
}

% --- Fonts & Packages ---
\usepackage[T1]{fontenc}
\usepackage[scaled=0.9]{helvet}
\usepackage{courier}
\renewcommand{\familydefault}{\sfdefault}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{listings}
\usepackage{tikz}

% --- Code listings ---
\lstset{
  language=Python,
  basicstyle=\ttfamily\footnotesize,
  keywordstyle=\color{crimson}\bfseries,
  stringstyle=\color{datastroke},
  commentstyle=\color{midgray}\itshape,
  backgroundcolor=\color{computeblue!20},
  frame=single,
  rulecolor=\color{computestroke},
  numbers=none,
  breaklines=true,
  columns=fullflexible,
  keepspaces=true,
  showstringspaces=false,
  xleftmargin=4pt,
  xrightmargin=4pt,
  aboveskip=6pt,
  belowskip=4pt,
}

\newcommand{\mlsysim}{\texttt{mlsysim}}
\graphicspath{{images/}}

\title{MLSys·im Tutorial --- Module 3}
\subtitle{Scale, Dollars, and Carbon}
\author{Vijay Janapa Reddi}
\institute{Harvard University}
\date{Conference Tutorial}

\begin{document}

\begin{frame}[plain]
  \titlepage
\end{frame}

\begin{frame}{Roadmap: Conference Tutorial}
\centering\small
\begin{tabular}{rll}
\toprule
\textbf{Module} & \textbf{Topic} & \textbf{Status} \\
\midrule
Module 1 & Foundations \& Architecture & \checkmark Done \\
Module 2 & Advanced Single-Node Analysis & \checkmark Done \\
\rowcolor{crimson!12}
\textbf{Module 3} & \textbf{Scale, Dollars, and Carbon} & \textbf{$\leftarrow$ You are here} \\
Module 4 & Design Space Exploration \& Synthesis & \\
\bottomrule
\end{tabular}
\end{frame}

\section{Going Distributed}

\begin{frame}{The Distributed Computing Taxonomy}
When models outgrow a single GPU, we must partition them:
\begin{itemize}
  \item \textbf{Data Parallelism (DP):} Replicate model, shard data.
  \item \textbf{Tensor Parallelism (TP):} Shard matrix multiplications. High communication bandwidth required (NVLink).
  \item \textbf{Pipeline Parallelism (PP):} Shard layers across GPUs. Leads to pipeline bubbles.
\end{itemize}
\end{frame}

\begin{frame}{The Amdahl Trap}
Scaling efficiency is never 100\%. It degrades due to:
\begin{enumerate}
  \item \textbf{Communication overhead:} Time spent passing tensors over InfiniBand (AllReduce, AllGather).
  \item \textbf{Pipeline bubbles:} GPUs sitting idle waiting for previous pipeline stages to finish.
  \item \textbf{Stragglers:} The cluster is only as fast as its slowest node.
\end{enumerate}
\vfill
\begin{center}
The \texttt{DistributedModel} solver automatically calculates network transmission times and pipeline bubbles.
\end{center}
\end{frame}

\section{Economics \& TCO}

\begin{frame}{The Total Cost of Ownership (TCO)}
\begin{center}
\Large
\[
\text{TCO} = \underbrace{\text{CapEx}}_{\text{hw + facility}}
          + \underbrace{\text{OpEx}_{\text{energy}}}_{\text{electricity}}
          + \underbrace{\text{OpEx}_{\text{maint}}}_{\text{staff}}
\]
\end{center}
\vspace{1em}
\textbf{The Infrastructure Multiplier:}
GPUs are only $\sim$40\% of total CapEx. Networking (InfiniBand), power delivery, cooling, facility, and operations add 50--150\%.
\end{frame}

\section{Sustainability \& Composition}

\begin{frame}[fragile]{Cross-Domain Carbon Accounting (Scenario B)}
Training time depends on hardware efficiency. Carbon footprint depends on geographic grid intensity. \mlsysim{} composes solvers cleanly.
\begin{lstlisting}[language=Python]
from mlsysim.solvers import DistributedModel, SustainabilityModel
from mlsysim.systems.registry import Systems
from mlsysim.infrastructure.registry import Infrastructure
from mlsysim.models.registry import Models

fleet = Systems.Clusters.Research_256  # 32x DGX H100 nodes

# 1. Performance Domain: How long will it take to train?
perf = DistributedModel().solve(
    model=Models.Language.Llama3_70B, fleet=fleet,
    tp_size=8, pp_size=1, dp_size=32)
training_days = perf.latency.to("days").magnitude

# 2. Sustainability Domain: What is the carbon impact?
sust = SustainabilityModel().solve(
    fleet=fleet, duration_days=training_days,
    datacenter=Infrastructure.Datacenters.GCP_Iowa,  # High-carbon grid
    mfu=perf.mfu)
print(f"Total Carbon: {sust.carbon_footprint:.1f} tonnes CO2e")
\end{lstlisting}
\end{frame}

\begin{frame}{Geography is the Biggest Lever}
\centering
\begin{tabular}{lcr}
\toprule
\textbf{Region} & \textbf{Mix} & \textbf{Carbon Intensity (gCO$_2$/kWh)} \\
\midrule
Quebec    & Hydro       & 20 \\
Sweden    & Hydro+Nuc   & 25 \\
US Avg    & Mixed       & 390 \\
Poland    & Coal        & 820 \\
\bottomrule
\end{tabular}
\vfill
\begin{alertblock}{The 41$\times$ Gap}
Training the exact same model on the exact same hardware in Poland emits \textbf{41$\times$ more carbon} than training it in Quebec.
\end{alertblock}
\end{frame}

\begin{frame}{Summary: Module 3}
\begin{enumerate}
  \item \textbf{Distributed Computing:} Tensor Parallelism requires high bandwidth (NVLink); Pipeline Parallelism incurs bubbles.
  \item \textbf{Economics:} Hardware is only 40\% of TCO. Do not ignore the Infrastructure Multiplier.
  \item \textbf{Composition:} Output from Performance solvers (latency) pipes directly into Operations solvers (carbon).
  \item \textbf{Sustainability:} Geography (Carbon Intensity) is the single biggest lever in AI sustainability.
\end{enumerate}
\vspace{1em}
\begin{center}
\textit{Next up: Module 4 - Design Space Exploration!}
\end{center}
\end{frame}

\end{document}