Skip to content

Instantly share code, notes, and snippets.

View MilesCranmer's full-sized avatar

Miles Cranmer MilesCranmer

View GitHub Profile
@MilesCranmer
MilesCranmer / num_tokens.sh
Created September 29, 2023 06:25
Easily count the number of tokens in text from the command line
function num_tokens {
prun python -c 'import sys; import tiktoken; s = "\n".join([line for line in sys.stdin]); encoding = tiktoken.get_encoding("cl100k_base"); print(len(encoding.encode(s)))'
}
# e.g., `echo "Hello World!" | num_tokens`
# this should give 3 tokens.
from functools import lru_cache
@lru_cache # Stores previously computed factorials
def factorial(num):
if num < 2:
return 1
return num * factorial(num - 1)
def N(C, m_2, m_1, m_0):
count = 0
@MilesCranmer
MilesCranmer / slurm_macros.sh
Created June 24, 2023 16:44
Some useful slurm macros
# Apply a function to any slurm job matching a regexp.
# For example, `son 'my_job_32.*' scancel {}` would run `scancel <job>` on any
# job matching the regexp.
function son {
squeue -u $USER --format="%i %j" | awk "/${1}/"' {print $1}' | xargs -I {} ${@:2}
}
# Watch a detailed `squeue` output (from Siavash Golkar)
function sqw {
This file has been truncated, but you can view the full file.
┌ Warning: Recursive type
│ T = Node{Float64}
└ @ Enzyme /dev/shm/.julia/packages/GPUCompiler/BxfIW/src/utils.jl:56
┌ Warning: Recursive type
│ T = Node{Float64}
└ @ Enzyme /dev/shm/.julia/packages/GPUCompiler/BxfIW/src/utils.jl:56
┌ Warning: Recursive type
│ T = Node{Float64}
└ @ Enzyme /dev/shm/.julia/packages/GPUCompiler/BxfIW/src/utils.jl:56
┌ Warning: Recursive type
@MilesCranmer
MilesCranmer / multihead.jl
Last active April 18, 2023 21:48
Minimal multi-headed self-attention
using Flux
using Fluxperimental: @compact
nf = 10
nb = 32
nt = 100
d_attn = 64
d_value = 128
d_head = 16
d_out = 256
@MilesCranmer
MilesCranmer / benchmark.jl
Last active April 6, 2023 01:05
Compare forward-mode and reverse-mode differentiation over parameter #
using BenchmarkTools
using ForwardDiff
using ReverseDiff
using Random
using Plots
using Statistics: mean, quantile, std
using Measurements
using Printf: @sprintf
using Colors
from argparse import ArgumentParser
import time
import numpy as np
import torch
from torch import nn
from torch.utils.data import DataLoader, TensorDataset
from torch.optim import Adam
from torch.nn import functional as F
@MilesCranmer
MilesCranmer / create_captcha_text.php
Created December 18, 2022 21:36
Generate captcha text to hide text from bots
<?php
require_once 'vendor/autoload.php';
use Gregwar\Captcha\CaptchaBuilder;
// Create a captcha with the text "MilesCranmer@mastodon.social"
$builder = new CaptchaBuilder('MilesCranmer@mastodon.social');
$builder->setMaxBehindLines(10);
$builder->setMaxFrontLines(10);
@MilesCranmer
MilesCranmer / userChrome.css
Created December 5, 2022 21:32
Sidebery customization with proper hiding
#main-window[titlepreface*="[Sidebery]"] #TabsToolbar {
visibility: collapse !important;
}
#sidebar-header {
display: none;
}
#main-window #TabsToolbar {
overflow: hidden;
transition: height .3s .3s !important;
@MilesCranmer
MilesCranmer / reduce_precision.py
Created July 1, 2022 20:35
Reduce precision of constants in a string
import re
def reduce_precision_of_constants_in_string(s, precision=3):
# Find all constants in the string:
constants = re.findall(r"\b[-+]?\d*\.\d+|\b[-+]?\d+\.?\d*", s)
for c in constants:
reduced_c = "{:.{precision}g}".format(float(c), precision=precision)
s = s.replace(c, reduced_c)
return s