liveBook · Manning

Timestep

As explained in Learn Generative AI with PyTorch

A discrete index into the diffusion/denoising schedule that determines how much noise is added or removed at a given stage.

A timestep is a discrete stage in a diffusion schedule. During forward diffusion, each stage adds noise; during reverse diffusion, stages remove it until the data becomes clean. As t increases from 0 toward T, the clean-image contribution decreases and the noise contribution increases. section 15.1.1

The timestep tells a denoising U-Net how noisy its input is. The model encodes t with sine and cosine functions into a 128-value vector, then expands and broadcasts that embedding to match image-feature shapes such as (128, 64, 64). section 15.3.2

A timestep participates in diffusion in three practical ways:

  • Training randomly selects a timestep, adds the corresponding noise to a clean image, and trains the U-Net to predict that noise. section 15.4
  • Inference traverses selected timesteps in reverse, repeatedly using the model prediction to transform noise into a cleaner image. section 15.4.2
  • The number of inference steps need not equal the full training schedule: 50 steps can use 980, 960, through 0 from a 1,000-step schedule. section 15.4.2

Generating flowers across timesteps

Start with pure random noise at t = 1000. Apply the trained denoising U-Net at t = 980, then 960, continuing through t = 0. Intermediate images show the gradual transition from noisy structure to a clean flower; selected checkpoints include t = 800, 600, 400, 200, and 0. section 15.4.2

Listing 15.7 Defining a generate() method in the DDIMScheduler() class
    @torch.no_grad()
    def generate(self,model,device,batch_size=1,generator=None,
         eta=1.0,use_clipped_model_output=True,num_inference_steps=50):
        imgs=[]
        image=torch.randn((batch_size,model.in_channels,model.sample_size,
            
model.sample_size),
            generator=generator).to(device)              #1
  
        self.set_timesteps(num_inference_steps)
        for t in tqdm(self.timesteps):                   #2
            model_output = model(image, t)["sample"]     #3
            image = self.step(model_output,t,image,eta,
                  use_clipped_model_output=\
                  use_clipped_model_output)              #4
            img = unnormalize_to_zero_to_one(image)
            img = img.cpu().permute(0, 2, 3, 1).numpy()
            imgs.append(img)                             #5
        image = unnormalize_to_zero_to_one(image)
        image = image.cpu().permute(0, 2, 3, 1).numpy()
        return {"sample": image}, imgs 

Inspect how a DDIM scheduler initializes random noise, sets inference timesteps, passes each t to the model, applies the scheduler step, and stores intermediate images. section 15.4.2

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains forward and reverse schedules, timestep embeddings, tensor broadcasting, and inference loops, but does not derive the full equations.
Examples
Uses 1,000-step flower diffusion, 50-step generation, and visual checkpoints at t = 800, 600, 400, 200, and 0.
What it assumes
Assumes basic images, noise, tensors, and neural networks while teaching the forward and reverse diffusion roles from the schedule.
What you can do after
Apply it via a denoising U-Net and DDIM scheduler to train noise prediction and generate images through selected timesteps.
How it connects
Connects timesteps to diffusion models, denoising U-Nets, positional encoding, and DDIM schedulers.

Also explained in

Compare how 5 books explain timestep →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.