r""" The [`UNet1DModel`] forward method. Args: sample (`torch.Tensor`): The noisy input tensor with the following shape `(batch_size, num_channels, sample_size)`. timestep (`torch.Tensor` or `float` or `int`): The number of timesteps to denois
(
self,
sample: torch.Tensor,
timestep: Union[torch.Tensor, float, int],
return_dict: bool = True,
)
| 193 | ) |
| 194 | |
| 195 | def forward( |
| 196 | self, |
| 197 | sample: torch.Tensor, |
| 198 | timestep: Union[torch.Tensor, float, int], |
| 199 | return_dict: bool = True, |
| 200 | ) -> Union[UNet1DOutput, Tuple]: |
| 201 | r""" |
| 202 | The [`UNet1DModel`] forward method. |
| 203 | |
| 204 | Args: |
| 205 | sample (`torch.Tensor`): |
| 206 | The noisy input tensor with the following shape `(batch_size, num_channels, sample_size)`. |
| 207 | timestep (`torch.Tensor` or `float` or `int`): The number of timesteps to denoise an input. |
| 208 | return_dict (`bool`, *optional*, defaults to `True`): |
| 209 | Whether or not to return a [`~models.unets.unet_1d.UNet1DOutput`] instead of a plain tuple. |
| 210 | |
| 211 | Returns: |
| 212 | [`~models.unets.unet_1d.UNet1DOutput`] or `tuple`: |
| 213 | If `return_dict` is True, an [`~models.unets.unet_1d.UNet1DOutput`] is returned, otherwise a `tuple` is |
| 214 | returned where the first element is the sample tensor. |
| 215 | """ |
| 216 | |
| 217 | # 1. time |
| 218 | timesteps = timestep |
| 219 | if not torch.is_tensor(timesteps): |
| 220 | timesteps = torch.tensor([timesteps], dtype=torch.long, device=sample.device) |
| 221 | elif torch.is_tensor(timesteps) and len(timesteps.shape) == 0: |
| 222 | timesteps = timesteps[None].to(sample.device) |
| 223 | |
| 224 | timestep_embed = self.time_proj(timesteps) |
| 225 | if self.config.use_timestep_embedding: |
| 226 | timestep_embed = self.time_mlp(timestep_embed) |
| 227 | else: |
| 228 | timestep_embed = timestep_embed[..., None] |
| 229 | timestep_embed = timestep_embed.repeat([1, 1, sample.shape[2]]).to(sample.dtype) |
| 230 | timestep_embed = timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:])) |
| 231 | |
| 232 | # 2. down |
| 233 | down_block_res_samples = () |
| 234 | for downsample_block in self.down_blocks: |
| 235 | sample, res_samples = downsample_block(hidden_states=sample, temb=timestep_embed) |
| 236 | down_block_res_samples += res_samples |
| 237 | |
| 238 | # 3. mid |
| 239 | if self.mid_block: |
| 240 | sample = self.mid_block(sample, timestep_embed) |
| 241 | |
| 242 | # 4. up |
| 243 | for i, upsample_block in enumerate(self.up_blocks): |
| 244 | res_samples = down_block_res_samples[-1:] |
| 245 | down_block_res_samples = down_block_res_samples[:-1] |
| 246 | sample = upsample_block(sample, res_hidden_states_tuple=res_samples, temb=timestep_embed) |
| 247 | |
| 248 | # 5. post-process |
| 249 | if self.out_block: |
| 250 | sample = self.out_block(sample, timestep_embed) |
| 251 | |
| 252 | if not return_dict: |
nothing calls this directly
no test coverage detected