Skip to content

Instantly share code, notes, and snippets.

View danesherbs's full-sized avatar
🍹
Poolside

Dane danesherbs

🍹
Poolside
View GitHub Profile
@danesherbs
danesherbs / marathon.py
Created August 22, 2023 02:38
Generates a marathon training plan
import csv
from datetime import datetime, timedelta
START_DATE = datetime.today()
WEEKS = 15
INITIAL_DISTANCE = 35
MAX_INCREASE_RATE = 0.10
PARK_RUN_DISTANCE = 8
INCREASE_RATE = 0.05
@danesherbs
danesherbs / app.py
Last active January 30, 2024 03:10
A tiny web app to render OpenAI eval log files nicely :)
from flask import Flask, render_template, request, redirect
import jsonlines
import os
app = Flask(__name__)
@app.route("/")
def home():
default_fname = "tmp/evallogs/230807123401PG6IQKIY_gpt-3.5-turbo-16k,gpt-3.5-turbo-16k_make-me-say.jsonl"
@danesherbs
danesherbs / compiler.py
Created March 25, 2023 05:25
A tiny compiler for a Ruby-like language
"""A teeny tiny compiler!"""
import re
from typing import List, Tuple
from dataclasses import dataclass
TOKEN_TYPES = [
("def", r"\bdef\b"),
("end", r"\bend\b"),
("identifier", r"\b[a-zA-Z]+\b"),
@danesherbs
danesherbs / sample.py
Last active October 14, 2022 14:58
Function to sample without replacement and optionally include or exclude items
def sample(seq: Sequence, k: int, include: Union[None, set] = None, exclude: Union[None, set] = None) -> Sequence:
"""Samples `k` items from `seq` without replacement, optionally including or excluding items."""
# pre-condition
assert k >= 0, "Number of items to sample must be non-negative"
assert k <= len(seq), f"Cannot sample {k} items without replacement from a sequence with {len(seq)} items"
if include is None:
include = set()
@danesherbs
danesherbs / dict_dataset.py
Last active September 7, 2022 02:15
A class which makes a PyTorch dataset from a dictionary of tensors
class DictDataset(torch.utils.data.Dataset):
"""Makes a dataset from a dictionary of tensors"""
def __init__(self, inputs: Dict[str, Tensor]):
assert len(inputs) > 0, "inputs must be non-empty"
keys = list(inputs.keys())
key = keys[0]
self._length = inputs[key].shape[0]
@danesherbs
danesherbs / context_hook.py
Last active February 11, 2024 01:04
A PyTorch hook that's registered in a `with` statement
# This hook is particularly useful when ablating layers
class ContextHook:
def __init__(self, layer):
self.layer = layer
def __enter__(self):
self.handle = self.layer.register_forward_hook(self.hook)
return self
@danesherbs
danesherbs / stateful_hook.py
Created March 14, 2022 08:49
A PyTorch hook that stores the state of a forward pass
class StatefulHook:
def __init__(self):
self.module = None
self.input = None
self.output = None
def __call__(self, module, input, output):
self.module = module
self.input = input
self.output = output