Skip to content

Instantly share code, notes, and snippets.

@JosephCatrambone
JosephCatrambone / basic_backprop.py
Created February 23, 2021 18:34
Reverse Mode Automatic Differentiation
import numpy
import random
class Graph:
def __init__(self):
self.nodes = list()
self.last_tape = None
def forward(self, variables, output_node):
for n in self.nodes:
n.forward(variables)
@JosephCatrambone
JosephCatrambone / decision_tree.rs
Created April 7, 2021 02:15
A Rust-language decision tree implementation with no external dependencies.
//use serde::{Serialize, Deserialize};
use std::collections::HashMap;
// If you have serde, serialization is available with this derive chain:
//#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[derive(Clone, Debug, Default)]
pub struct DecisionTree {
feature: usize,
threshold: f32,
@JosephCatrambone
JosephCatrambone / DecisionTree.gd
Created February 25, 2022 04:32
Basic Decision Tree in GDScript
# Sample Usage:
# var day_features = [
# # freezing, raining, foggy, sunny
# [0, 1, 0, 0],
# [0, 0, 0, 1],
# [0, 0, 0, 0],
# [0, 0, 1, 0],
# [1, 0, 0, 1],
# [1, 1, 0, 0],
# [1, 0, 1, 0],
@JosephCatrambone
JosephCatrambone / tinypico_wireless_morse_keyboard.ino
Created May 15, 2022 21:03
TinyPICO BlueTooth BLE Morse Keyboard
#include <BleKeyboard.h>
#include <TinyPICO.h>
TinyPICO tinypico = TinyPICO();
BleKeyboard keyboard; // keyboard("Name", "manufacturer", battery_level_100)
// a .-
// b -...
// c -.-.
// d -..
@JosephCatrambone
JosephCatrambone / union_find.py
Created July 11, 2022 23:51
A Union-Find / Disjoint-Set Python class
class UnionFind:
def __init__(self):
self.parents = dict()
def add(self, node_id:int):
self.parents[node_id] = node_id
def union(self, node_a:int, node_b:int):
min_parent = min(self.parents[node_a], self.parents[node_b])
self.parents[node_a] = min_parent
@JosephCatrambone
JosephCatrambone / lib.rs
Last active March 8, 2024 01:32
Embedding GPT-2 in Godot via Rust
mod ml_thread;
use gdnative::prelude::{godot_print, methods, Method, NativeClass, Node as GDNode, InitHandle, godot_init};
use ml_thread::start_language_model_thread;
use std::sync::mpsc::{channel, Receiver, RecvError, Sender, SendError};
const MAX_INPUT_LENGTH: usize = 512;
const BATCH_SIZE: usize = 1;
@JosephCatrambone
JosephCatrambone / cluster_lines.py
Created November 17, 2023 18:28
Cluster Suggestions
import itertools
import sys
import numpy
import torch
from scipy.cluster.vq import kmeans, kmeans2
from transformers import AutoTokenizer, GPT2Model
def magnitude(x):
return numpy.dot(x, x)**0.5
@JosephCatrambone
JosephCatrambone / spiral_iterator.py
Last active March 4, 2024 06:23
Discrete Grid Spiral Iterator
def square_iterator(x: int, y: int):
"""Generate all the points on a square 2d lattice, spiraling out from the center. Modified from a solution by Nikita Rybak. https://stackoverflow.com/a/3706260"""
dx = 1
dy = 0
segment_length = 1
leg_iterations = 0
while True:
x += dx
y += dy
@JosephCatrambone
JosephCatrambone / notes_fast_model_editing_at_scale.md
Created May 7, 2024 05:11
Notes on Fast Model Editing at Scale by Mitchell, Lin, Bosselut, Finn, and Manning (2022)

Overview:

  • MEND is a way to edit models to change output in a way that is local, reliable, and general.
  • "Local" means unrelated output is not changed. "Reliable" means the model takes the desired corrections. "General" meaning variations on similar questions which would need correction also are corrected.
  • Works even on very large models.

Differences to Prior Art:

Open Questions:

  • They say it's a method for transforming raw fine-tuning gradients into a targeted parameter update. That would seem to suggest a new model results. At the same time, they say that the edits are applied to the model's weights at test time, which says the base MEND models need to be kept. Which is it?
  • If they're producing a layer's parameter edit as an output, why do they care about producing the low-rank embedding of the deltas? Accumulate and apply them.