Latent Encode-Decode Round Trip
Problem Statement
Verify the scaling is applied consistently: encoding to a latent multiplies by s, decoding divides by s. Run the round trip and report the max absolute reconstruction error against the original.
Background
Stable Diffusion scales latents on encode (z = s * z_raw) and unscales on decode (z_raw = z / s). If both use the same s, the round trip is exact up to floating point. This problem models the scale-only part of the round trip (ignoring the VAE), so the reconstruction should match the input to numerical precision.
Your Task
Implement:
def roundtrip_error(z_raw, encode_scale, decode_scale):
- Encode: z = encode_scale * z_raw. Decode: recon = z / decode_scale.
Return the maximum absolute error max|recon - z_raw| rounded to 6 decimals.
Input Format
- z_raw: list of floats.
- encode_scale, decode_scale (float, nonzero).
Output Format
- A float rounded to 6 decimals.
Sample
print(roundtrip_error([1.0, 2.0, 3.0], 0.18215, 0.18215))
Output:
0.0
Example:
print(roundtrip_error([1.0, 2.0, 3.0], 0.18215, 0.18215))
0.0
- Encoding Step: Multiply each element of the input vector zraw=[1.0,2.0,3.0] by the
encode_scale(0.18215) to produce the latent representation z. This yields z=[0.18215,0.36430,0.54645]. - Decoding Step: Divide each element of the latent vector z by the
decode_scale(0.18215) to reconstruct the original values. Since the scales are identical, the division cancels the multiplication: recon=[0.18215/0.18215,0.36430/0.18215,0.54645/0.18215]=[1.0,2.0,3.0]. - Error Calculation: Compute the absolute difference between the reconstructed vector and the original input: ∣recon−zraw∣=∣[1.0−1.0,2.0−2.0,3.0−3.0]∣=[0.0,0.0,0.0].
- Max Error Extraction: Identify the maximum value in the error vector, which is max([0.0,0.0,0.0])=0.0.
- The final output is 0.0
Constraints:
1 <= len(z_raw) <= 100000, scales nonzero.recon = (encode_scale * z_raw) / decode_scale.- Return
max|recon - z_raw|rounded to 6 decimals.
1. Background Knowledge
In latent diffusion models like Stable Diffusion, the Variational Autoencoder (VAE) compresses images into a lower-dimensional latent space. To stabilize training and inference, the latent tensor is scaled by a constant factor s (commonly ≈0.18215) during encoding: z=s⋅zraw. During decoding, the inverse operation is applied: zraw=z/s. This symmetric scaling ensures that the information content is preserved while keeping numerical values in a well-behaved range for the diffusion process.
The round-trip property states that if the same scale factor is used for both encoding and decoding, the reconstruction should be identical to the original input, up to floating-point precision. Mathematically, (s⋅x)/s=x holds exactly in real arithmetic. In floating-point arithmetic, small rounding errors may accumulate, but for typical scale factors and input magnitudes, the error is negligible (often exactly 0.0 when rounded to 6 decimal places).
This problem isolates the scaling component of the VAE round trip, ignoring the actual neural network operations. It tests whether you understand that consistent scaling is critical for lossless reconstruction and can verify this numerically.
2. Algorithm Approach
The approach is a direct element-wise arithmetic verification:
- Apply the encode scale to each element of the input vector.
- Apply the decode scale (division) to the scaled vector.
- Compute the element-wise absolute difference between the reconstructed vector and the original.
- Return the maximum of these differences, rounded to 6 decimal places.
This is a linear pass over the data with no branching or iterative refinement. The core insight is that you are verifying an algebraic identity numerically.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.