We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Production ML — Training Stack step 11 of 12
JIT Compile a Function
Compile a function that applies sin(x)**2 + cos(x)**2 to every tensor
value. This exercise practices compilation, so keep the trigonometric
operations instead of replacing them with a tensor of ones.
Your task
Implement jit_trig_identity(x) using @jax.jit in JAX or @torch.compile
in PyTorch. The input is a floating-point tensor of finite angles in radians.
Return a tensor with the same shape, dtype and device, leaving x unchanged.
The identity sin(t)**2 + cos(t)**2 = 1 holds for real numbers in exact
arithmetic. Computed values should be close to one, with small differences
from floating-point rounding. For input [0, 1, 2], the expected result is
approximately [1, 1, 1]. Do not return a single scalar or reduce over an axis.
Define the function with framework operations, then decorate it or create a
compiled wrapper once and reuse it. Return the computed tensor, not the
wrapper. Python uses ** for powers; ^ is not exponentiation.
What compilation changes
The first call may include tracing and compilation work. Later calls can reuse compiled work when their inputs satisfy the compiler's requirements. Compilation does not guarantee a speedup for this tiny function, especially if the timing includes the first call.
The diagram is a simplified JAX cache model for one function. The first float32 vector of length 32 creates an entry; another call of the same shape and dtype reuses it; a vector of length 64 creates another entry. The labels refer to vector lengths, not float32 versus float64 precision.
Real cache behavior also depends on the function and compilation settings. PyTorch uses guards and can support dynamic shapes, so this diagram does not predict its exact recompilation behavior. It simulates cache decisions; it does not compile or time code in your browser.
Loading visualization…
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Compile a calculation so later calls can reuse it
Just-in-time compilation turns a supported calculation into code that can be
reused. In JAX, jit traces array operations using information such as input
shapes and dtypes, then compiles the resulting computation.
A compatible later call can reuse the compiled version. Changes to shapes, dtypes, or arguments marked static may require another version. A static argument's value becomes part of the specialization instead of remaining an ordinary runtime input.
Keep traced calculations separate from Python side effects
An ordinary Python side effect, such as appending to a list, can happen during tracing rather than once per execution of the compiled calculation. Do not use it as a per-call numerical operation.
Branching on an array value being traced also needs appropriate array or control-flow operations. The Python interpreter cannot choose a branch using a value that will only be available when the compiled computation runs.
For an exercise computing sin(x)^2 + cos(x)^2, evaluate the specified expression.
Its exact mathematical value is one, but its purpose is to demonstrate compilation,
and floating-point results can differ slightly from one.
Measure compilation and execution separately
The first call can include compilation overhead. Time repeated execution separately to see whether reuse helps. Accelerator dispatch can be asynchronous, so synchronize the result before treating execution as complete. Small workloads may not save time once compilation is included.
PyTorch's torch.compile uses a different capture and compilation system.
Its supported Python behavior and reasons for splitting a computation into graphs
are not identical to JAX's. See
JAX's compilation guide.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
PyTorch reference solution
Sign in to track your attempts toward unlocking the reference solution.