Skip to content

Instantly share code, notes, and snippets.

View MilesCranmer's full-sized avatar

Miles Cranmer MilesCranmer

View GitHub Profile
@MilesCranmer
MilesCranmer / python_syntax_for_gin.py
Last active May 7, 2022 01:33
Enable valid Python to be a config.gin file, so code analysis and syntax highlighting works
def preprocess_config(s: str):
"""Remove imports from a string representation of a python file"""
# We assume that imports are not multi-line.
lines = s.splitlines()
out_lines = []
for line in lines:
# Skip lines with import in them:
if 'import' in line:
continue
@MilesCranmer
MilesCranmer / unique.c
Created June 26, 2022 01:58
Get unique elements of an array using a lookup table
// Count elements of an array by using a lookup table.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
int main(int argc, char *argv[])
{
// Generate random array of integers, with
// size given by args.
@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
@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 / 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);
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 / 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
@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
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 / 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 {