Skip to content

Instantly share code, notes, and snippets.

@qpwo
qpwo / gpt.sh
Created January 3, 2024 19:32
chatgpt cli
#!/usr/bin/bash
# copy into /usr/bin or whatever and chmod+x
# example:
# $ gpt 'untar and ensure subdir (but no extraneous nesting)'
# ```bash mkdir subdir && tar -xvf archive.tar -C subdir --strip-components=1 ```
# https://platform.openai.com/api-keys
@qpwo
qpwo / pytorch_mnist_no_autograd.py
Last active December 22, 2023 17:17
pytorch simple feed forward net from scratch (no autograd)
# Neural net implementation in PyTorch without autograd
# https://gist.github.com/qpwo/86c41eb6869b36dc53d5df798e66a040
# based on https://gist.github.com/jessicabuzzelli/8317c2350ce63b899e0682ce256c44cf#file-image_classification_nn-py
# wget https://github.com/mnielsen/neural-networks-and-deep-learning/raw/master/data/mnist.pkl.gz
import numpy as np
import torch as torch
import gzip
import pickle
# from tqdm import tqdm_notebook as tqdm
@qpwo
qpwo / ventura-shutdown-daily.sh
Created October 23, 2023 18:58
macos ventura force shutdown daily (and wake up from sleep)
# make a cron job
sudo crontab -e
# add this line to shutdown every day at 2:00AM:
# 0 2 * * * /sbin/shutdown -h now
# check your cron job is there:
sudo crontab -l
# the job won't run if computer is asleep. Wake up first to run it.
sudo pmset repeat wake MTWRFSU 01:59:00
@qpwo
qpwo / compose.yaml
Created October 18, 2023 23:06
docker compose ip route transparent proxy
# Do `docker compose up`, then on host run:
# iptables -I DOCKER-USER -s 10.0.1.0/24 -o br-$INTERNAL_NETWORK_ID -j LOG_ACCEPT
# iptables -I DOCKER-USER -d 10.0.1.0/24 -i br-$INTERNAL_NETWORK_ID -j LOG_ACCEPT
# You will see the pings succeeding.
version: "3"
services:
proxy_cntnr:
container_name: proxy_cntnr
@qpwo
qpwo / cd-git-root.sh
Created September 7, 2023 18:34
zsh/bash alias to cd into git repo root
# append to .zshrc or .bashrc
# cd into git root
function gr() {
# check in git repo:
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
echo "not in git repo"
return 1
fi
@qpwo
qpwo / context-test.ts
Created May 11, 2023 23:05
node js async context trace
import { AsyncLocalStorage } from 'node:async_hooks'
const asyncLocalStorage = new AsyncLocalStorage<number>()
async function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}
async function foo() {
console.log('started')
await sleep(1000)
@qpwo
qpwo / async_context.py
Created May 11, 2023 22:55
python async context trace
import asyncio
from contextvars import ContextVar
ctx = ContextVar[int]("myctx")
async def bar():
await asyncio.sleep(1)
print(ctx.get(), end=" ")
@qpwo
qpwo / local-stdin-remote-docker-file.sh
Created April 17, 2023 23:09
pipe local stdin (or file) directly to remote docker container (spoof filename)
# If you have binary data or a large file that you need to copy directly
# from local to inside a remote docker container then you can do it like this:
echo fromlocal | ssh dockerhost 'docker exec -i mycontainer sh -c "cat - > /home/filename.txt"'
@qpwo
qpwo / BigMap.ts
Created October 31, 2022 10:46
typescript big map class (exceeds 2^24 limit)
// https://gist.github.com/qpwo/5b7f068089c8a826b8a709f2fd937407
const JS_MAP_MAX = Math.pow(2, 24)
export default class BigMap<K = unknown, V = unknown> {
maps: Map<K, V>[]
constructor(pairs: null | readonly (readonly [K, V])[]) {
this.maps = [new Map<K, V>(pairs)]
}
@qpwo
qpwo / as_strided_matmul.py
Created August 15, 2022 02:04
as_strided matrix multiplication
import torch as t
l = 2
n = 3
m = 5
a = t.randint(0,100,(l,n))
b = t.randint(0,100,(n,m))
print("matmul:", a.matmul(b))
print("with strides:", (a.as_strided((l,n,m), (n,1,0)) * b.as_strided((l, n, m), (0, m, 1))).sum(1))