Skip to content

Instantly share code, notes, and snippets.

View spezold's full-sized avatar

Simon Pezold spezold

  • Basel (CH)
View GitHub Profile
@spezold
spezold / coupled_weight_decay.py
Last active July 20, 2026 13:03
Coupled weight decay mixin (AdamC, MuonC) for PyTorch
"""
Provide a mixin for coupling weight decay, e.g. AdamC, as proposed in A. Defazio, “Why Gradients Rapidly Increase
Near the End of Training,” (arXiv:2506.02285).
Use e.g. as `class AdamC(CoupledWeightDecayMixin, optim.AdamW): pass` (see below); then provide `{"normalized": True}`
for parameter groups where the coupling/correction factor should be applied (norm layers usually).
"""
from typing import Any, Mapping
from torch import no_grad, optim
@spezold
spezold / attempt.py
Created February 22, 2026 12:57
Simulating ternary (one-liner) try-except in Python
from functools import wraps
from typing import Callable as C
def attempt[**A, R, F](*, unless: type[BaseException], fallback: F, call: C[A, R]) -> C[A, R | F]:
"""
Attempt a function call; return its value if it succeeds, return the ``fallback`` if an exception of type
``unless`` is raised (so, basically, simulate a one-liner ``try-except`` statement).
Usage example::
@spezold
spezold / parallel_scan.py
Last active October 23, 2025 09:13
A demonstration of applying the parallel scan algorithm (Blelloch, 1990) to a first-order recursive problem
"""
Demonstrate the application of the parallel scan algorithm with a first-order recurrence problem, as proposed by
Blelloch (1990). Harris et al. (2007) provide helpful illustrations and discuss a CUDA implementation; online version:
https://developer.nvidia.com/gpugems/gpugems3/part-vi-gpu-computing/chapter-39-parallel-prefix-sum-scan-cuda
- G. E. Blelloch, “Prefix Sums and Their Applications,” School of Computer Science, Carnegie Mellon University,
CMU-CS-90-190, Nov. 1990.
- M. Harris, S. Sengupta, and J. D. Owens, “Parallel prefix sum (scan) with CUDA,” GPU gems, vol. 3, no. 39, pp.
851–876, 2007.
"""
@spezold
spezold / gradient_legend.py
Created September 15, 2025 11:16
Matplotlib legend with a patch that has a horizontal gradient
from typing import NamedTuple, Literal
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.image import BboxImage
import matplotlib.pyplot as plt
from matplotlib.transforms import TransformedBbox, Bbox
import numpy as np
class GradientHandle(NamedTuple):
rgb_src: tuple[float, float, float] | tuple[float, float, float, float]
@spezold
spezold / json_dumps_compact.py
Created August 9, 2024 14:52
Dump given JSON document to a string, flattening lists (recursively) but respecting the indent for other objects.
import json
def json_dumps_compact(data, indent: int | str, **kwargs) -> str:
"""
Dump given JSON document to a string, flattening lists (recursively) but respecting the indent for other objects.
:param data: JSON object to be dumped
:param indent: indent level for JSON object members (None is not supported)
:param kwargs: other arguments passed to :func:`json.dumps` ("separators" is not supported)
@spezold
spezold / mean_std_torch.py
Created May 17, 2024 14:38
Calculate stable mean and standard deviation over a potentially large sample of values, using Chan et al.'s version of Welford's online algorithm (same as "mean_std.py", but in PyTorch)
"""
Calculate sample mean and std. over all desired axes of given samples, using Chan et al.'s version of Welford's online
algorithm; cf. https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm (20240515) and
equations (2.1a), (2.1b) in the referenced paper [1]_; or likewise, equations (1.5a), (1.5b) in [2]_.
Mind the typo in [1]_, (2.1a): T_{1, m+n} on the right side of the equation should be T_{m+1, m+n} (cf. [2]_, (1.5a)).
References
----------
.. [1] T. F. Chan, G. H. Golub, and R. J. LeVeque, “Updating Formulae and a Pairwise Algorithm for Computing Sample
Variances,” in COMPSTAT 1982 5th Symposium held at Toulouse 1982, Heidelberg, 1982, pp. 30–41,
doi: 10.1007/978-3-642-51461-6_3.
@spezold
spezold / mean_std.py
Last active May 17, 2024 14:36
Calculate stable mean and standard deviation over a potentially large sample of values, using Chan et al.'s version of Welford's online algorithm
"""
Calculate sample mean and std. over all desired axes of given samples, using Chan et al.'s version of Welford's online
algorithm; cf. https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm (20240515) and
equations (2.1a), (2.1b) in the referenced paper [1]_; or likewise, equations (1.5a), (1.5b) in [2]_.
Mind the typo in [1]_, (2.1a): T_{1, m+n} on the right side of the equation should be T_{m+1, m+n} (cf. [2]_, (1.5a)).
References
----------
.. [1] T. F. Chan, G. H. Golub, and R. J. LeVeque, “Updating Formulae and a Pairwise Algorithm for Computing Sample
@spezold
spezold / my_modulefinder.py
Last active April 14, 2022 09:00
Find all modules that are imported by the given project, list the code files (*.py, *.ipynb) that use them, and try to distinguish between STL and non-STL modules.
"""
CAUTION: Make sure that
1. this file is placed in the root directory of the project of interest
(or otherwise, adjust `BASE_DIR` accordingly);
2. the file is run in the same Python environment (conda environment, poetry environment, ...)
as the project of interest (so activate the corresponding environment first, if necessary).
"""
from collections import defaultdict
from importlib.util import find_spec
@spezold
spezold / rolling_window_inside_and_outside.py
Last active August 19, 2021 19:20
Return both the values inside and outside of a rolling window over a 1D PyTorch tensor as a 2D tensor
from typing import Tuple
import torch
from torch import Tensor
# The straightforward solution
def rolling_window_inside_and_outside(t: Tensor, size: int, stride: int=1) -> Tuple[Tensor, Tensor]:
"""
Given a 1D tensor, provide both the values inside the rolling window and outside the rolling window for each window
position with the given window size and stride.
@spezold
spezold / save_and_load_model.py
Last active June 16, 2021 11:42
**Update: have a look at torch.package instead** (https://pytorch.org/docs/1.9.0/package.html) -- Original description: Save and load a PyTorch model (both code and weights): minimum working example, based on the inner workings of TorchServe.
import importlib.util
import inspect
import json
from pathlib import Path
from typing import Optional, Union
import zipfile
import torch
from torch import nn