Skip to content

Instantly share code, notes, and snippets.

@crowsonkb
crowsonkb / maxrl.py
Last active November 6, 2026 09:31
MaxRL for binary and Bernoulli rewards.
"""MaxRL for binary and Bernoulli rewards.
MaxRL (https://arxiv.org/abs/2602.02710) minimizes a Taylor series approximation of -log(p(pass))
per task. The Taylor series approximation is of order `group_size - 1`, where `group_size` is the
number of rollouts per RLOO/GRPO-style group. With the minimum group size of two, MaxRL is
equivalent to RLOO. MaxRL is defined for binary rewards, but this module contains an extension to
Bernoulli rewards in the range [0, 1], where rewards represent the probability of success for each
rollout.
The two functions in this module that are intended to be used in training are `maxrl()` for binary
@crowsonkb
crowsonkb / lfq.py
Last active July 27, 2026 14:16
Lookup Free Quantization (LFQ) for PyTorch.
"""Lookup Free Quantization (LFQ) for PyTorch."""
from dataclasses import dataclass
from itertools import product
import math
from typing import Optional
import torch
from torch import distributed as dist, nn
from torch.distributed import nn as dnn
@crowsonkb
crowsonkb / sinkhorn_distributed.py
Last active July 13, 2026 22:33
Sinkhorn balancing and a Sinkhorn balance loss function.
"""Sinkhorn balancing and a Sinkhorn balance loss function."""
import math
from typing import Optional
import torch
from torch import distributed as dist
from torch.distributed import nn as dnn
from torch.nn import functional as F
@crowsonkb
crowsonkb / resample.py
Last active October 22, 2025 03:35
Good differentiable image resampling for PyTorch.
"""Good differentiable image resampling for PyTorch."""
from functools import update_wrapper
import math
import torch
from torch.nn import functional as F
def sinc(x):
@crowsonkb
crowsonkb / pseudo_huber.py
Created February 7, 2021 12:33
The Pseudo-Huber loss
import torch
from torch import nn
class PseudoHuberLoss(nn.Module):
"""The Pseudo-Huber loss."""
reductions = {'mean': torch.mean, 'sum': torch.sum, 'none': lambda x: x}
def __init__(self, beta=1, reduction='mean'):
@crowsonkb
crowsonkb / syre.py
Last active August 3, 2025 21:05
Implementation of the syre weight decay algorithm from "Remove Symmetries to Control Model Expressivity and Improve Optimization" (https://arxiv.org/abs/2408.15495).
"""Implementation of the syre weight decay algorithm from "Remove Symmetries to Control Model
Expressivity and Improve Optimization" (https://arxiv.org/abs/2408.15495)."""
import math
import torch
from torch import optim
import triton
import triton.language as tl
@crowsonkb
crowsonkb / energy_matching.py
Last active July 8, 2025 10:28
The energy matching loss
"""The energy matching loss.
Energy matching regresses an energy function to match a target energy function at the points in the
dataset. ("Energy" refers to an unnormalized negative log probability: for a sequence model it is
the sum of the cross-entropy losses of a sequence's completion tokens plus some constant. Two energy
functions are considered "the same" by the energy matching loss if they differ by an arbitrary
constant.) This is useful for on-policy reinforcement learning or off-policy preference tuning of
sequence models, where the target energies are:
[the sequences' energies under the reference model] - [the sequences' rewards] / beta,
#!/usr/bin/env python3
"""Pads an image for Bluesky."""
import argparse
import math
from pathlib import Path
from PIL import Image
@crowsonkb
crowsonkb / denoise.py
Last active March 14, 2025 23:29
Total variation denoising
#!/usr/bin/env python3
"""Total variation denoising."""
import argparse
import time
import numpy as np
from PIL import Image
from scipy.linalg import blas
@crowsonkb
crowsonkb / plackett_luce.py
Last active November 5, 2024 21:33
Plackett-Luce loss function for ranking tasks. The rankings may be partial and include ties.
"""Plackett-Luce loss function for ranking tasks. The rankings may be partial and include ties."""
from itertools import chain
from typing import List, Optional, Tuple
import torch
def plackett_luce_loss(
scores: torch.Tensor,