### Install RIN Pytorch Source: https://github.com/lucidrains/recurrent-interface-network-pytorch/blob/main/README.md Install the package via pip. ```bash $ pip install rin-pytorch ``` -------------------------------- ### Initialize and Train RIN Model Source: https://context7.com/lucidrains/recurrent-interface-network-pytorch/llms.txt Initializes a RIN model and Gaussian Diffusion process, then configures and starts the training pipeline using the Trainer class. Resuming training from a checkpoint is also demonstrated. ```python from rin_pytorch import GaussianDiffusion, RIN, Trainer # Initialize model and diffusion model = RIN( dim=256, image_size=128, patch_size=8, depth=6, num_latents=128, dim_latent=512, latent_self_attn_depth=4, ).cuda() diffusion = GaussianDiffusion( model, timesteps=400, train_prob_self_cond=0.9, scale=1.0, # Use scale < 1.0 for 512x512 or larger ).cuda() # Create trainer with all training configurations trainer = Trainer( diffusion, folder='/path/to/your/images', # Directory containing training images train_batch_size=4, # Batch size per GPU gradient_accumulate_every=4, # Effective batch = 4 * 4 = 16 train_lr=1e-4, # Learning rate train_num_steps=700000, # Total training steps ema_decay=0.995, # EMA decay for model averaging ema_update_every=10, # Update EMA every N steps save_and_sample_every=1000, # Checkpoint and sample frequency num_samples=16, # Number of samples to generate results_folder='./results', # Output directory for checkpoints amp=False, # Enable automatic mixed precision mixed_precision_type='fp16', # 'fp16' or 'bf16' when amp=True augment_horizontal_flip=True, # Data augmentation max_grad_norm=1.0, # Gradient clipping ) # Start training - results saved to ./results/ trainer.train() # Resume training from checkpoint trainer.load(milestone=50) # Load model-50.pt trainer.train() # Continue training ``` -------------------------------- ### RIN PyTorch Attention with Flash Attention Source: https://context7.com/lucidrains/recurrent-interface-network-pytorch/llms.txt Demonstrates initializing and using the Attend class for attention mechanisms, with support for standard PyTorch and optimized Flash Attention. Includes examples with and without attention masks. ```python import torch from rin_pytorch.attend import Attend # Standard attention (compatible with all PyTorch versions) attend_standard = Attend(dropout=0.1, flash=False) # Flash attention (requires PyTorch 2.0+, auto-detects GPU capabilities) attend_flash = Attend(dropout=0.1, flash=True) # Prepare query, key, value tensors batch_size, heads, seq_len, dim_head = 4, 8, 256, 64 q = torch.randn(batch_size, heads, seq_len, dim_head).cuda() k = torch.randn(batch_size, heads, seq_len, dim_head).cuda() v = torch.randn(batch_size, heads, seq_len, dim_head).cuda() # Forward pass - returns attended values out = attend_flash(q, k, v) print(f"Attention output shape: {out.shape}") # torch.Size([4, 8, 256, 64]) # With attention mask (for padding or causal attention) mask = torch.ones(batch_size, seq_len, dtype=torch.bool).cuda() mask[:, 200:] = False # Mask out last 56 positions out_masked = attend_flash(q, k, v, mask=mask) print(f"Masked attention output shape: {out_masked.shape}") ``` -------------------------------- ### Train RIN with Trainer Source: https://github.com/lucidrains/recurrent-interface-network-pytorch/blob/main/README.md Initialize the RIN model, GaussianDiffusion, and Trainer to begin the training process. ```python from rin_pytorch import GaussianDiffusion, RIN, Trainer model = RIN( dim = 256, # model dimensions image_size = 128, # image size patch_size = 8, # patch size depth = 6, # depth num_latents = 128, # number of latents. they used 256 in the paper dim_latent = 512, # can be greater than the image dimension (dim) for greater capacity latent_self_attn_depth = 4, # number of latent self attention blocks per recurrent step, K in the paper ).cuda() diffusion = GaussianDiffusion( model, timesteps = 400, train_prob_self_cond = 0.9, # how often to self condition on latents scale = 1. # this will be set to < 1. for more noising and leads to better convergence when training on higher resolution images (512, 1024) - input noised images will be auto variance normalized ).cuda() trainer = Trainer( diffusion, '/path/to/your/images', num_samples = 16, train_batch_size = 4, gradient_accumulate_every = 4, train_lr = 1e-4, save_and_sample_every = 1000, train_num_steps = 700000, # total training steps ema_decay = 0.995, # exponential moving average decay ) trainer.train() ``` -------------------------------- ### Initialize and Run RIN Model Source: https://context7.com/lucidrains/recurrent-interface-network-pytorch/llms.txt Configure the RIN model and perform forward passes with or without latent self-conditioning. ```python import torch from rin_pytorch import RIN # Initialize the RIN model with custom configuration model = RIN( dim=256, # Model dimension for patch embeddings image_size=128, # Input image size (must be divisible by patch_size) patch_size=8, # Size of image patches (smaller = more patches) channels=3, # Number of input channels (3 for RGB) depth=6, # Number of RIN blocks (recurrent iterations) num_latents=128, # Number of latent tokens (paper used 256) dim_latent=512, # Latent dimension (can exceed dim for more capacity) latent_self_attn_depth=4, # Self-attention layers per recurrent step (K in paper) learned_sinusoidal_dim=16, # Dimension for learned sinusoidal time embeddings latent_token_time_cond=False, # Use latent token vs adaptive layernorm for time dual_patchnorm=True, # Apply normalization before and after patch projection patches_self_attn=True, # Enable linear self-attention on patches ).cuda() # Forward pass with image and time conditioning batch_size = 4 images = torch.randn(batch_size, 3, 128, 128).cuda() # Noised images time = torch.rand(batch_size).cuda() # Diffusion timesteps [0, 1] # Basic forward pass - returns denoised output output = model(images, time) print(f"Output shape: {output.shape}") # torch.Size([4, 3, 128, 128]) # Forward pass with self-conditioning (latent warm-starting) x_self_cond = torch.randn(batch_size, 3, 128, 128).cuda() # Previous prediction latent_self_cond = torch.randn(batch_size, 128, 512).cuda() # Previous latents output, latents = model( images, time, x_self_cond=x_self_cond, latent_self_cond=latent_self_cond, return_latents=True ) print(f"Output: {output.shape}, Latents: {latents.shape}") # Output: torch.Size([4, 3, 128, 128]), Latents: torch.Size([4, 128, 512]) ``` -------------------------------- ### Configure and Train RIN for High-Resolution Images Source: https://context7.com/lucidrains/recurrent-interface-network-pytorch/llms.txt Initializes a RIN model for 512x512 images, configures GaussianDiffusion with specific noise schedules, and sets up a Trainer for the full pipeline. ```python model = RIN( dim=384, # Larger dimension for high-res image_size=512, patch_size=16, # Larger patches to manage memory depth=8, num_latents=256, # More latents for detail dim_latent=768, latent_self_attn_depth=4, ).cuda() # Use scale < 1.0 for high resolution - adds more noise during training # This helps convergence as shown in "simple diffusion" paper diffusion = GaussianDiffusion( model, timesteps=1000, noise_schedule='sigmoid', # Sigmoid schedule works best for large images train_prob_self_cond=0.9, scale=0.5, # Key parameter: more noise for high-res min_snr_loss_weight=True, ).cuda() # Training loop with scale normalization training_images = torch.randn(2, 3, 512, 512).cuda() loss = diffusion(training_images) print(f"High-res training loss: {loss.item():.4f}") # Trainer for full pipeline trainer = Trainer( diffusion, folder='/path/to/512x512/images', train_batch_size=2, # Smaller batch for memory gradient_accumulate_every=8, # Effective batch = 16 train_lr=1e-4, train_num_steps=1000000, # More steps for high-res save_and_sample_every=2000, num_samples=4, amp=True, # Use mixed precision to save memory ) trainer.train() ``` -------------------------------- ### High Resolution Image Training Configuration Source: https://context7.com/lucidrains/recurrent-interface-network-pytorch/llms.txt Configuration snippet for training RIN models on high-resolution images (512x512 and above). It highlights the use of the 'scale' parameter in GaussianDiffusion for improved convergence. ```python import torch from rin_pytorch import RIN, GaussianDiffusion, Trainer ``` -------------------------------- ### RIN PyTorch Noise Schedules Source: https://context7.com/lucidrains/recurrent-interface-network-pytorch/llms.txt Demonstrates the usage of different noise schedule functions (linear, cosine, sigmoid) for diffusion models. The sigmoid schedule is recommended for larger image resolutions. ```python import torch from rin_pytorch.rin_pytorch import ( simple_linear_schedule, cosine_schedule, sigmoid_schedule, gamma_to_alpha_sigma, gamma_to_log_snr ) # Create timesteps from 0 to 1 t = torch.linspace(0, 1, 100) # Linear schedule - simplest, uniform noise addition gamma_linear = simple_linear_schedule(t, clip_min=1e-9) print(f"Linear gamma range: [{gamma_linear.min():.4f}, {gamma_linear.max():.4f}]") # Cosine schedule - smoother transitions, good for small images gamma_cosine = cosine_schedule(t, start=0, end=1, tau=1, clip_min=1e-9) print(f"Cosine gamma range: [{gamma_cosine.min():.4f}, {gamma_cosine.max():.4f}]") # Sigmoid schedule - recommended for larger images (512+) gamma_sigmoid = sigmoid_schedule(t, start=-3, end=3, tau=1, clamp_min=1e-9) print(f"Sigmoid gamma range: [{gamma_sigmoid.min():.4f}, {gamma_sigmoid.max():.4f}]") # Convert gamma to alpha (signal) and sigma (noise) coefficients # For noised_img = alpha * img + sigma * noise alpha, sigma = gamma_to_alpha_sigma(gamma_sigmoid, scale=1.0) print(f"Alpha range: [{alpha.min():.4f}, {alpha.max():.4f}]") print(f"Sigma range: [{sigma.min():.4f}, {sigma.max():.4f}]") # Compute log signal-to-noise ratio for loss weighting log_snr = gamma_to_log_snr(gamma_sigmoid, scale=1.0) print(f"Log SNR range: [{log_snr.min():.4f}, {log_snr.max():.4f}]") ``` -------------------------------- ### Train and Sample with GaussianDiffusion Source: https://context7.com/lucidrains/recurrent-interface-network-pytorch/llms.txt Wrap the RIN model with GaussianDiffusion to handle training loss calculation and image sampling. ```python import torch from rin_pytorch import RIN, GaussianDiffusion # Create the base RIN model model = RIN( dim=256, image_size=128, patch_size=8, depth=6, num_latents=128, latent_self_attn_depth=4, ).cuda() # Wrap with GaussianDiffusion for training and sampling diffusion = GaussianDiffusion( model, timesteps=1000, # Number of diffusion timesteps use_ddim=True, # Use DDIM sampling (faster) vs DDPM noise_schedule='sigmoid', # 'sigmoid', 'cosine', or 'linear' objective='v', # 'v' (velocity), 'x0', or 'eps' prediction train_prob_self_cond=0.9, # Probability of self-conditioning during training min_snr_loss_weight=True, # Use Min-SNR weighting for stable training min_snr_gamma=5, # Gamma value for Min-SNR weighting scale=1.0, # Set < 1.0 for higher resolution (512, 1024) time_difference=0., # Time offset for sampling (helps with few timesteps) ).cuda() # Training: forward pass returns loss training_images = torch.randn(8, 3, 128, 128).cuda() # Normalized images [0, 1] loss = diffusion(training_images) loss.backward() print(f"Training loss: {loss.item():.4f}") # Sampling: generate new images from noise with torch.no_grad(): sampled_images = diffusion.sample(batch_size=4) print(f"Sampled images shape: {sampled_images.shape}") # torch.Size([4, 3, 128, 128]) # Images are in range [0, 1], ready for visualization ``` -------------------------------- ### Experiment with RIN and GaussianDiffusion Source: https://github.com/lucidrains/recurrent-interface-network-pytorch/blob/main/README.md Use the RIN and GaussianDiffusion classes directly for custom training loops or inference. ```python import torch from rin_pytorch import RIN, GaussianDiffusion model = RIN( dim = 256, # model dimensions image_size = 128, # image size patch_size = 8, # patch size depth = 6, # depth num_latents = 128, # number of latents. they used 256 in the paper latent_self_attn_depth = 4, # number of latent self attention blocks per recurrent step, K in the paper ).cuda() diffusion = GaussianDiffusion( model, timesteps = 1000, train_prob_self_cond = 0.9, scale = 1. ) training_images = torch.randn(8, 3, 128, 128).cuda() # images are normalized from 0 to 1 loss = diffusion(training_images) loss.backward() # after a lot of training sampled_images = diffusion.sample(batch_size = 4) sampled_images.shape # (4, 3, 128, 128) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.