ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Advanced Compiler and Runtime Optimizations for ML Workloads
Chapter 1: Foundation: ML Execution Stack and Challenges
The ML Model Deployment Gap
Overview of ML Compiler and Runtime Stacks
Performance Bottlenecks in ML Inference
Hardware for ML Acceleration
The Need for Specialized Optimizations
Chapter 2: Advanced Intermediate Representations for ML
Limitations of Traditional Compiler IRs
Principles of Multi-Level IRs
MLIR: Dialects and Operations
Representing High-Level ML Graphs (e.g., TF, TOSA)
Lowering Paths within MLIR
Extensibility and Custom Dialects
Hands-on Practical: Analyzing MLIR Representations
Chapter 3: Advanced Graph-Level Optimizations
Graph Rewriting Systems
Aggressive Operator Fusion Techniques
Memory-Aware Layout Transformations
Advanced Algebraic Simplification
Static Memory Planning and Allocation
Handling Control Flow in Graphs
Hands-on Practical: Implementing a Fusion Pass
Chapter 4: Tensor-Level Optimizations and Polyhedral Modeling
Representing Tensor Computations as Loop Nests
Introduction to Polyhedral Modeling
Iteration Domains, Access Functions, and Dependencies
Scheduling Transformations (Skewing, Tiling)
Code Generation from Polyhedral Schedules
Auto-Vectorization Techniques (SIMD)
Memory Hierarchy Optimization: Tiling and Prefetching
Hands-on Practical: Optimizing Loops with Polyhedral Tools
Chapter 5: Code Generation for Heterogeneous Hardware
Target-Specific Instruction Selection
Register Allocation for Vector/Matrix Units
GPU Code Generation: CUDA and ROCm Backends
Generating Code for Tensor Cores and Matrix Units
Targeting AI Accelerators (TPUs, NPUs)
Intermediate Formats for Heterogeneous Execution (SPIR-V)
Vendor-Specific Compiler Toolchains and Libraries (cuDNN, MIOpen)
Hands-on Practical: Analyzing Generated GPU Kernels
Chapter 6: Advanced Runtime Systems for ML
Runtime Architecture Overview
Handling Dynamic Shapes and Sizes
Efficient Memory Management Strategies
Asynchronous Execution and Scheduling
Scheduling for Heterogeneous Systems
Integrating Custom Operators and Kernels
Interoperability with ML Frameworks
Hands-on Practical: Implementing a Simple Allocator
Chapter 7: Just-In-Time (JIT) Compilation Techniques for ML
Motivation for JIT Compilation in ML
Tracing vs. Scripting Approaches
Intermediate Representation in JIT Systems
Runtime Specialization and Polymorphism
Profile-Guided Optimization (PGO) in JITs
Adaptive and Multi-Tier Compilation
Case Study: TensorFlow XLA
Case Study: PyTorch JIT (TorchScript)
Hands-on Practical: Analyzing JIT Compiled Code
Chapter 8: Quantization and Low-Precision Optimizations
Fundamentals of Model Quantization (INT8, FP8)
Representing Quantized Operations in IR
Compiler Passes for Quantization-Aware Training (QAT)
Post-Training Quantization (PTQ) Compilation Flows
Generating Low-Precision Kernels
Mixed-Precision Computation Optimization
Handling Quantization Scales and Zero Points
Hands-on Practical: Lowering Quantized Operations
Chapter 9: Profiling and Performance Analysis Tools
Challenges in Profiling Compiled ML Code
System-Level Profiling (CPU, GPU, Interconnect)
CPU Performance Analysis (VTune, perf)
GPU Kernel Profiling (Nsight Compute, ROCprof)
Correlating Framework Operations to Compiled Kernels
Memory Access Pattern Analysis
Interpreting Compiler Optimization Reports
Hands-on Practical: Profiling an Optimized Model

Tracing vs. Scripting Approaches

When employing Just-In-Time (JIT) compilation, a critical step involves capturing the model's computational graph directly from high-level Python code. This captured graph serves as the input for the JIT compiler's optimization and code generation passes. Two primary strategies dominate this graph acquisition phase: tracing and scripting. Each approach presents distinct advantages and limitations, influencing the types of models they handle well and the optimizations that can be subsequently applied.

Tracing: Recording Execution Paths

Tracing operates by executing the model function with example inputs and recording the sequence of operations performed on tensor objects during that specific execution. Think of it like running a profiler that specifically logs the ML operations and their data dependencies.

How it works:

  1. You provide the JIT system with your model function (e.g., a torch.nn.Module's forward method or a TensorFlow function decorated with @tf.function).
  2. You invoke the function with sample tensor inputs (e.g., model(example_input)).
  3. The JIT mechanism intercepts calls to framework operations (like convolutions, matrix multiplications, activations) involving tensors.
  4. It builds a graph representation (often in an intermediate representation like TorchScript IR, TensorFlow GraphDef, or HLO) that mirrors the exact sequence of operations executed for those specific inputs.

Example:

For example, a simple Python function:

def simple_op(a, b):
  c = a + b
  d = c * 2
  return d

If traced with a = tensor([1]) and b = tensor([2]), the tracer records:

  • Operation: add (Inputs: a, b, Output: c)
  • Operation: mul (Inputs: c, constant 2, Output: d)
  • Return: d

The resulting graph captures this linear sequence.

Advantages:

  • Simplicity: It's often straightforward to apply tracing to existing Python code without significant modification. If the code runs eagerly, it can likely be traced.
  • Python Compatibility: Tracing naturally handles arbitrary Python code executing between the framework operations, as long as that code doesn't affect the graph structure itself (e.g., using standard Python data structures, printing values).

Disadvantages:

  • Static Control Flow: Tracing fundamentally struggles with data-dependent control flow. If your model contains Python if, for, or while statements where the condition or loop bounds depend on tensor values, the trace captures only the path taken for the specific example inputs used during tracing. The resulting graph won't include the alternative branches or represent the loop structure generically.

    def conditional_op(x, threshold):
      if x.sum() > threshold: # Data-dependent condition
        return x * 2
      else:
        return x + 1
    

    Tracing conditional_op with an x that satisfies the condition will yield a graph containing only the x * 2 path. The x + 1 path is entirely absent.

  • Input Dependence: The traced graph is inherently tied to the properties (like shape, dtype) of the inputs used during tracing. While some JIT systems can handle limited dynamism later, the initial trace might be overly specialized.

  • Side Effects: Tracing might not capture Python side effects correctly or might bake them into the graph in unexpected ways.

Scripting: Parsing the Source

Scripting takes a different approach. Instead of executing the code, it directly parses the Python source code of the model function (or a subset of it) and translates it into a graph representation, including control flow structures.

How it works:

  1. You typically need to explicitly mark the function for scripting (e.g., using @torch.jit.script in PyTorch) or write the function using a restricted subset of Python that the scripting compiler understands.
  2. The scripting compiler parses the function's source code, building an Abstract Syntax Tree (AST).
  3. It analyzes the AST, converting Python constructs (assignments, operators, control flow) into corresponding graph nodes and structures within its IR.

Example:

Using the same conditional_op function:

@torch.jit.script # Example decorator
def conditional_op(x, threshold):
  # Scripting compiler parses this structure
  if x.sum() > threshold:
    result = x * 2
  else:
    result = x + 1
  return result

The scripting compiler analyzes the if/else structure and generates a graph containing nodes representing the condition (x.sum() > threshold), both the true branch (x * 2) and the false branch (x + 1), and a control flow mechanism to select the appropriate path at runtime.

Graph representation resulting from scripting the conditional_op function, explicitly showing the conditional branch.

Advantages:

  • Control Flow Handling: Scripting excels at capturing Python control flow (if, for, while) that depends on tensor values, representing them directly in the graph.
  • Input Independence: The generated graph represents the actual logic of the function, independent of any specific input values used during the "compilation" step.
  • Explicit Graph: Provides a more complete representation of the intended computation, potentially enabling more sophisticated graph-level optimizations across control flow paths.

Disadvantages:

  • Language Subset: Scripting compilers typically only support a subset of the full Python language. You might need to refactor complex Python logic, list comprehensions, or calls to unsupported libraries to conform to the scriptable subset.
  • Code Modification: Often requires explicit annotation (decorators) or adherence to specific coding patterns. It cannot usually consume arbitrary, unmodified Python code transparently.
  • Steeper Learning Curve: Developers need to understand the limitations and requirements of the scripting language subset. Debugging scripting errors can sometimes be less intuitive than debugging standard Python.

Choosing Between Tracing and Scripting

The choice between tracing and scripting often depends on the nature of the model and the development workflow:

Feature Tracing Scripting
Ease of Use Generally easier for existing Python code Requires code adaptation/annotation
Control Flow Poor (captures only one path) Good (explicitly captures branches/loops)
Python Features Handles most Python code between ops Restricted to a language subset
Input Dependence High (graph tied to trace inputs) Low (graph represents code logic)
Robustness Can be fragile if control flow changes More reliable representation
Use Case Simple models, quick prototyping, static graphs Models with data-dependent control flow, deployment

Modern JIT systems often provide both options. For example, PyTorch's TorchScript allows users to choose @torch.jit.trace or @torch.jit.script, and even combine traced and scripted modules. TensorFlow's @tf.function primarily uses a tracing mechanism ("autograph" implicitly converts some Python control flow, blurring the lines slightly, but the fundamental capture is trace-based).

Understanding the fundamental difference between observing an execution path (tracing) and parsing the code logic (scripting) is essential for effectively using JIT compilers. Tracing offers convenience but limits expressiveness, especially concerning dynamic control flow. Scripting demands more developer effort to conform to its constraints but yields a more complete representation capable of handling complex program structures, creating the way for more comprehensive optimizations within the JIT compiler.

  • Introduction to TorchScript, PyTorch Documentation, 2024 - Official documentation explaining both tracing (torch.jit.trace) and scripting (torch.jit.script) approaches in PyTorch, with examples and use cases.
  • Better performance with tf.function, TensorFlow Documentation, 2024 (TensorFlow) - Official guide detailing TensorFlow's tracing-based graph capture mechanism (tf.function) and its implicit handling of Python control flow with AutoGraph.
  • TVM: An Automatic End-to-End Optimizing Compiler for Deep Learning, Tianqi Chen, Thierry Moreau, Ziheng Jiang, Lianmin Zheng, Eddie Yan, Haichen Shen, Meghan Cowan, Leyuan Wang, Yuwei Hu, Luis Ceze, Carlos Guestrin, Arvind Krishnamurthy, 2018 13th USENIX Symposium on Operating Systems Design and Implementation (OSDI 18) (USENIX Association) DOI: 10.5555/3342354.3342416 - Provides context for the need for deep learning compilers and graph intermediate representations (IRs), which are the output of tracing/scripting, and discusses challenges in optimizing them.

© 2026 ApX Machine Learning