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:
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 is multiplied element-wise by the default scaling factor to normalize the latent variance.
- The first element becomes .
- The second element becomes .
- The third element becomes .
- 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.