PIXELBANKv9.1.0
Menu

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β‹…zrawz = s \cdot z_{\text{raw}}

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:

Input:
print(encode_latent([1.0, 2.0, -4.0]))
Output:
[0.18215, 0.3643, -0.7286]
Reasoning:
  • The input list [1.0,2.0,βˆ’4.0][1.0, 2.0, -4.0] is multiplied element-wise by the default scaling factor s=0.18215s = 0.18215 to normalize the latent variance.
  • The first element becomes 1.0Γ—0.18215=0.182151.0 \times 0.18215 = 0.18215.
  • The second element becomes 2.0Γ—0.18215=0.36432.0 \times 0.18215 = 0.3643.
  • The third element becomes βˆ’4.0Γ—0.18215=βˆ’0.7286-4.0 \times 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.
πŸ”’

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.

solution.py

Test Results

0/0
Run code to see test results.