liveBook · Manning

Code compilation

As explained in Deep Learning with R, Third Edition

PyTorch compilation is the transformation of PyTorch computations into an optimized executable representation.

PyTorch normally runs computations eagerly, which makes them easy to debug. Compilation is an optional attempt to make repeated computations faster: `torch$compile()` accepts a function or a `Module` and returns an object intended to behave the same way, with faster forward and backward passes. It uses PyTorch’s Dynamo compiler. section 3.4.2 section 3.4.3

Compile a PyTorch function

After `library(keras3)`, `use_backend("torch")`, and `torch <- import("torch")`, define a dense computation as `dense <- torch$compile(function(inputs, W, b) { torch$nn$relu(torch$matmul(inputs, W) + b) })`. The multiplication, bias addition, and activation stay together inside the function passed to the compiler. If the computation is already packaged as a `Module` called `model`, use `compiled_model <- torch$compile(model)` instead. section 3.4.1 section 3.4.2

Treat compilation as a performance option, not a required training step. Most PyTorch code remains eager: compilation may fail for some models or produce no speedup. Keeping an eager version is useful when debugging. TensorFlow offers a comparable function-level option through `tf_function()` and `jit_compile = TRUE`. section 3.4.2 section 3.3.1

How Deep Learning with R, Third Edition covers it

How deep it goes
Shows the compilation calls and explains expected behavior and limitations, but does not detail compiler internals.
Examples
Compiles a linear-classifier Module and a dense function using `torch$compile()`.
What it assumes
Builds on PyTorch tensors, functions, and Modules introduced alongside the compilation example.
What you can do after
Apply it via `torch$compile()` to a function or Module, then check whether it works and improves performance.
How it connects
Connects compilation to PyTorch Modules, forward and backward passes, eager execution, debugging, and TensorFlow function compilation.

Also explained in

Compare how 7 books explain code compilation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.