Encode to Latent with the Scaling Factor
Problem Statement
Stable Diffusion multiplies the VAE encoder's output by a fixed scaling factor so the latents have roughly unit variance before diffusion. Apply it.
Background
The VAE encodes an image to a latent z_raw; Stable Diffusion then scales it:
z=sβ zrawβ
with s = 0.18215 for SD 1.x/2.x. This puts the latent distribution near unit standard deviation, matching the noise schedule's assumptions.
Your Task
Implement:
def encode_latent(z_raw, scale=0.18215):
Return the scaled latent as a list rounded to 6 decimals.
Input Format
- z_raw: list of floats.
- scale (float): the scaling factor.
Output Format
- A list of floats rounded to 6 decimals.
Sample
print(encode_latent([1.0, 2.0, -4.0]))
Output:
[0.18215, 0.3643, -0.7286]
Example:
print(encode_latent([1.0, 2.0, -4.0]))
[0.18215, 0.3643, -0.7286]
- The input list [1.0,2.0,β4.0] is multiplied element-wise by the default scaling factor s=0.18215 to normalize the latent variance.
- The first element becomes 1.0Γ0.18215=0.18215.
- The second element becomes 2.0Γ0.18215=0.3643.
- The third element becomes β4.0Γ0.18215=β0.7286.
- Each result is rounded to 6 decimal places; since the products have at most 5 significant decimal digits, the values remain unchanged.
- The final output is
[0.18215, 0.3643, -0.7286]
Constraints:
1 <= len(z_raw) <= 100000.- Multiply every element by
scale. - Round to 6 decimals; avoid
-0.0.
1. Background Knowledge
In Latent Diffusion Models (such as Stable Diffusion), images are not denoised directly in pixel space. Instead, a Variational Autoencoder (VAE) compresses the image into a lower-dimensional latent space. This reduces computational cost while preserving semantic structure. The VAE encoder produces a raw latent vector, zrawβ, which typically has a distribution that does not match the assumptions of the diffusion noise schedule.
The diffusion process assumes that the data being denoised has approximately unit variance (standard deviation of 1). If the raw latents have a different scale, the noise schedule (which adds Gaussian noise with a fixed variance) will be mismatched, leading to poor generation quality. To fix this, Stable Diffusion applies a fixed scaling factor s to the raw latents:
z=sβ zrawβ
For Stable Diffusion 1.x and 2.x, this factor is empirically set to s=0.18215. This scaling ensures the latent distribution is centered near zero with unit standard deviation, aligning with the diffusion modelβs training assumptions.
2. Algorithm Approach
This problem is a straightforward element-wise scalar multiplication followed by rounding. There is no complex algorithmic pattern here; the core operation is:
- Multiply each element of the input list by the scalar scale.
- Round each result to 6 decimal places.
- Return the resulting list.
This is an O(n) operation where n is the length of the latent vector.
3. Step-by-Step Strategy
-
Step 1: Iterate over the input list. Loop through each value in z_raw.
-
Step 2: Apply the scaling factor. For each value ziβ, compute ziββ scale.
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.