MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / to_latent

Function to_latent

diffusers/examples/research_projects/vae/vae_roundtrip.py:119–138  ·  view source on GitHub ↗
(
    *,
    rgb_nchw: torch.Tensor,
    vae: SupportedAutoencoder,
)

Source from the content-addressed store, hash-verified

117
118
119def to_latent(
120 *,
121 rgb_nchw: torch.Tensor,
122 vae: SupportedAutoencoder,
123) -> torch.Tensor:
124 rgb_nchw = VaeImageProcessor.normalize(rgb_nchw) # type: ignore
125 encoding_nchw = vae.encode(typing.cast(torch.FloatTensor, rgb_nchw))
126 if isinstance(encoding_nchw, AutoencoderKLOutput):
127 latent = encoding_nchw.latent_dist.sample() # type: ignore
128 assert isinstance(latent, torch.Tensor)
129 elif isinstance(encoding_nchw, AutoencoderTinyOutput):
130 latent = encoding_nchw.latents
131 do_internal_vae_scaling = False # Is this needed?
132 if do_internal_vae_scaling:
133 latent = vae.scale_latents(latent).mul(255).round().byte() # type: ignore
134 latent = vae.unscale_latents(latent / 255.0) # type: ignore
135 assert isinstance(latent, torch.Tensor)
136 else:
137 assert False, f"Unknown encoding type: {type(encoding_nchw)}"
138 return latent
139
140
141def from_latent(

Callers 1

main_kwargsFunction · 0.85

Calls 5

scale_latentsMethod · 0.80
unscale_latentsMethod · 0.80
normalizeMethod · 0.45
encodeMethod · 0.45
sampleMethod · 0.45

Tested by

no test coverage detected