Skip to content

Instantly share code, notes, and snippets.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@madebyollin
madebyollin / automatic_profiling_markers.py
Created February 27, 2024 02:57
Add human-readable profiling markers to a pytorch module
def add_profiling_markers(model):
"""Monkey-patch profiling markers into an nn.Module.
Args:
model: an nn.Module
Effect:
all model.named_module() forward calls get wrapped in their
own profiling scope, making traces easier to understand.
"""
@madebyollin
madebyollin / Mamba_Diffusion_IADB_Colab.ipynb
Created December 6, 2023 04:47
Mamba Diffusion (IADB)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@madebyollin
madebyollin / notes_on_sd_vae.md
Last active April 23, 2024 06:21
notes_on_sd_vae

Notes / Links about Stable Diffusion VAE

Stable Diffusion's VAE is a neural network that encodes and decodes images into a compressed "latent" format. The encoder performs 48x lossy compression, and the decoder generates new detail to fill in the gaps.

(Calling this model a "VAE" is sort of a misnomer - it's an encoder with some very slight KL regularization, and a conditional GAN decoder)

This document is a big pile of various links with more info.

VAE Versions & Lineage

  • CompVis
@madebyollin
madebyollin / README.md
Last active April 2, 2024 13:50 — forked from mrsteyk/README.md
dalle_runner_api.model_infra.modules.public_diff_vae
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@madebyollin
madebyollin / model_activation_printer.py
Created July 30, 2023 17:47
Helper for logging output activation-map statistics for a PyTorch module, using forward hooks
def summarize_tensor(x):
return f"\033[34m{str(tuple(x.shape)).ljust(24)}\033[0m (\033[31mmin {x.min().item():+.4f}\033[0m / \033[32mmean {x.mean().item():+.4f}\033[0m / \033[33mmax {x.max().item():+.4f}\033[0m)"
class ModelActivationPrinter:
def __init__(self, module, submodules_to_log):
self.id_to_name = {
id(module): str(name) for name, module in module.named_modules()
}
self.submodules = submodules_to_log
@madebyollin
madebyollin / compare_safetensors.py
Created July 29, 2023 02:36
script for comparing the contents of safetensors files
#!/usr/bin/env python3
from pathlib import Path
from safetensors.torch import load_file
def summarize_tensor(x):
if x is None:
return "None"
x = x.float()
return f"({x.min().item():.3f}, {x.mean().item():.3f}, {x.max().item():.3f})"
@madebyollin
madebyollin / nearest_neighbor_upsample_bf16.py
Created July 16, 2023 15:00
bfloat16 nearest neighbor upsample code, for when F.interpolate / nn.Upsample don't work
# PyTorch <=2.0 doesn't support bfloat16 F.interpolate natively.
# so, we have to do things the old fashioned way.
import torch
import torch.nn as nn
# functional implementation
def nearest_neighbor_upsample(x: torch.Tensor, scale_factor: int):
"""Upsample {x} (NCHW) by scale factor {scale_factor} using nearest neighbor interpolation."""
s = scale_factor

Variational Autoencoders Will Never Work

So you want to generate images with neural networks. You're in luck! VAEs are here to save the day. They're simple to implement, they generate images in one inference step (unlike those awful slow autoregressive models) and (most importantly) VAEs are 🚀🎉🎂🥳 theoretically grounded 🚀🎉🎂🥳 (unlike those scary GANs - don't look at the GANs)!

The idea

The idea of VAE is so simple, even an AI chatbot could explain it:

  1. Your goal is to train a "decoder" neural network that consumes blobs of random noise from a fixed distribution (like torch.randn(1024)), interprets that noise as decisions about what to generate, and produces corresponding real-looking images. You want to train this network with nice simple image-space MSE loss against your dataset of real images.