Skip to content

Instantly share code, notes, and snippets.

View scturtle's full-sized avatar
🐢

scturtle

🐢
View GitHub Profile
@scturtle
scturtle / llama3.py
Created May 20, 2024 09:43
llama3 in numpy
import numpy as np
class ModelArgs:
dim = 288
n_layers = 6
n_heads = 6
norm_eps = 1e-6
def build_cos_sin_cache(head_dim, seq_len, base=10000):
theta = 1. / (base ** (np.arange(0, head_dim, 2, dtype=np.float32) / head_dim))
@scturtle
scturtle / flash_attention.py
Last active April 11, 2024 16:38
flash attention v1 v2 in numpy
import numpy as np
N_inp = 64
N_out = 64
d = 128
Q = np.random.randn(N_out, d)
K = np.random.randn(N_inp, d)
V = np.random.randn(N_inp, d)
O = np.random.randn(N_out, d)
@scturtle
scturtle / matmul.py
Created April 8, 2024 11:44
how matmul is tiled in cuda
import numpy as np
from threading import Barrier, Thread
from collections import namedtuple
dim3 = namedtuple("dim3", ["x", "y", "z"], defaults=(1, 1))
TILE = 16
def cdiv(a, b):
@scturtle
scturtle / script.js
Created December 1, 2023 06:39
adventofcode daylight mode userscript
// ==UserScript==
// @name adventofcode daylight mode
// @namespace Violentmonkey Scripts
// @match https://adventofcode.com/*
// @grant none
// @version 1.0
// @author scturtle
// @description 2023/11/27 16:52:13
// ==/UserScript==
#!/usr/bin/env python3
import re
from pathlib import Path
from typing import List
MARK = "// -----// IR Dump"
PAT = re.compile(r'IR Dump \w+ (\w+)')
def main(infile: Path, outdir: Path):
assert infile.exists(), f'{infile} not exists'
@scturtle
scturtle / emutool.py
Created May 16, 2023 16:22
emuiibo data generator
import json
import time
import random
import struct
import urllib.request
from pathlib import Path
# https://www.amiiboapi.com/api/amiibo/
database = json.load(open("amiibos.json"))
@scturtle
scturtle / dig_torch_mlir.py
Last active May 12, 2023 09:42
dig into torch-mlir
import torch
from torch import nn
# import torch_mlir
from torch_mlir.passmanager import PassManager
from torch_mlir.dialects.torch.importer.jit_ir import ClassAnnotator, ModuleBuilder
torch.manual_seed(42)
@scturtle
scturtle / im2col.py
Last active May 6, 2023 07:28
conv2d forward and backward implementation
import torch
from torch import nn
import numpy as np
# https://github.com/arasdar/DL/blob/master/uri-dl/uri-dl-hw-2/assignment2/cs231n/layers.py
# https://github.com/brandontrabucco/conv-python/blob/master/main.py
def get_output_shape(x, kernel, stride):
(_, _, ih, iw), (kh, kw) = x.shape, kernel
oh = (ih - kh) // stride + 1
#include <algorithm>
#include <cassert>
#include <random>
#include <vector>
constexpr unsigned int bit_floor(unsigned int num) {
return num == 0 ? 0 : 1 << (31 - __builtin_clz(num));
}
template <typename It, typename T, typename Cmp>
@scturtle
scturtle / arguments.cc
Created October 30, 2022 14:15
c++ argparser (adapted from Corrade)
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <optional>
#include <iomanip>
enum class Type {
PositionalArgument,