Skip to content

Instantly share code, notes, and snippets.

@eddiebergman
eddiebergman / main.py
Created February 9, 2022 23:25 — forked from mypy-play/main.py
Shared via mypy Playground
from abc import abstractmethod
from typing import TypeVar, Generic, Protocol
class P1(Protocol):
def predict(self): ...
class P2(Protocol, P1):
def predict_proba(self): ...
T1 = TypeVar("T1", bound=P1)
@eddiebergman
eddiebergman / sq.sh
Last active January 30, 2023 09:40
A more informative `sq` command
fudge() {
# Does some simple formatting for general things
# * Highlights based on status code
# * Highlight your name
# * Dim some things
# https://stackoverflow.com/a/33206814
# https://www.linuxquestions.org/questions/linux-newbie-8/tput-for-bold-dim-italic-underline-blinking-reverse-invisible-4175704737/
green=$(tput setaf 2)
orange=$(tput setaf 3)
@eddiebergman
eddiebergman / seed.py
Created February 6, 2023 07:26
Case for not using `np.random.seed`
import numpy as np
np.random.seed(1)
print("My code", np.random.choice([1,2,3,4,5]))
# My code 4
# Update library
np.random.seed(1)
from dask_jobqueue import SLURMCluster
import time
from pathlib import Path
from concurrent.futures import wait, ALL_COMPLETED
def f(x: int) -> int:
time.sleep(20)
print(f"Done {x}")
@eddiebergman
eddiebergman / eigen_spectra_via_lanczos.py
Last active October 11, 2024 10:09
An optimized implementation of `PyHessian`'s density function for computing the eigen-spectra of a hessian in torch
# An optimized implementation of the Lanczos algorithm to compute an
# estimate of the eigen-spectra of the Hessian of a neural network.
#
# The original implementation by `pyhessian` is used as a benchmark.
# https://github.com/amirgholami/PyHessian/tree/master
#
# To see how to actually use and plot the eigen-spectra based on this function,
# please see the original repository.
#
# # Requires:
@eddiebergman
eddiebergman / sparseset.rs
Last active December 26, 2024 01:46
SparseSet: O(1) insert/delete/lookup with contiguous memory for efficient GPU buffer transfer
/*
* SparseSet is a data structure that most importantly keeps all its stored
* items in contiguous memory, while still allowing for O(1) insertions,
* deletions, and lookups. These are usually objects you'd like to keep
* a reference for later O(1) lookup and mutation. You may also think
* of it as a `HashMap<usize, T>` but contiguous memory holding Vec<T>
*
* The contiguous part is particularly useful to be able to map them
* into a GPU buffer, as the data transfer can only specify full blocks
* of memory. This is why this structure **must** own the data it stores.
@eddiebergman
eddiebergman / date_iter.py
Created March 10, 2025 09:14
A Python `datetime` iterator
from datetime import datetime, timedelta
from typing import Iterator
def date_range(
start: datetime,
end: datetime,
step: timedelta,
*,
clamp: bool = True,