Skip to content

Instantly share code, notes, and snippets.

View khaotik's full-sized avatar
💫
gradient descending

khaotik

💫
gradient descending
View GitHub Profile
@khaotik
khaotik / vastai-comfyui-provision-flux2.sh
Created November 30, 2025 19:22
VastAI flux 2 provision script
#!/bin/bash
source /venv/main/bin/activate
COMFYUI_DIR=${WORKSPACE}/ComfyUI
# Packages are installed after nodes so we can fix them...
APT_PACKAGES=(
#"package-1"
#"package-2"
@khaotik
khaotik / elliptic.c
Created January 19, 2025 16:43
elltaniyama code snippet
GEN
elltaniyama(GEN e, long prec)
{
GEN x, w, c, d, X, C, b2, b4;
long n, m;
pari_sp av = avma;
checkell_Q(e);
if (prec < 0) pari_err_DOMAIN("elltaniyama","precision","<",gen_0,stoi(prec));
if (!prec) retmkvec2(triv_ser(gen_1,-2), triv_ser(gen_m1,-3));
@khaotik
khaotik / minsecp256k1.py
Created June 11, 2021 14:49
minimal python implementation of secp256k1 curve
#!/usr/bin/env python
'''
minimal reference implementation of bitcoin's secp256k1 with general prime field
requires pari && cypari2 for certain operations.
'''
class EcPoint:
__slots__ = ('x', 'y')
def __init__(self, x, y):
@khaotik
khaotik / memfd_create.py
Created May 2, 2021 14:38
Simple memfd_create wrapper in python
class memfd_create:
'''create in memory file with actual fileno using memfd_create
usage:
with memfd_create('file_name') as f:
pass # treat f as file-like in binary mode
'''
import ctypes, os
_libc = ctypes.CDLL(None)
_syscall = _libc.syscall
def __init__(self, name:str):
@khaotik
khaotik / tarfile-min-example.py
Created May 2, 2021 11:53
Minimal python tarfile module usage
#!/usr/bin/env python
import io
import tarfile
a_content = '''
content:aaa
content:111
'''
b_content = '''
content:bbb
content:222
@khaotik
khaotik / accum.py
Created January 26, 2019 11:14
binary carry accumulator
# binary carry accumulator with O(log(N)) space requirement
# better numerical stability when adding lots of small numbers
class Accumulator:
__slots__ = ('fn', 'data', 'index', 'init')
def __init__(self, size=31, init=0., fn='add'):
if fn == 'mean':
fn=lambda x,wx,y,wy:(x*wx+y*wy)/(wx+wy)
elif fn == 'add':
fn=lambda x,wx,y,wy:x+y
self.fn = fn
@khaotik
khaotik / ezumount.py
Created January 8, 2019 05:01
ezumount - easily unmount and power off plugged USB disks from linux CLI
#!/usr/bin/env python3
import os
import sys
import subprocess as subp
if len(sys.argv) != 2:
appname = sys.argv[0]
print('%s - unmount USB directory and power it off' % appname, file=sys.stderr)
print('Usage: %s <directory>' % appname, file=sys.stderr)
sys.exit(-1)
@khaotik
khaotik / method_1.cpp
Last active September 5, 2018 07:19
Receive packed array via WSTP
#include <stdlib.h>
#include <vector>
#include <string>
#include "wstp.h"
using namespace std;
int main() {
int err;
auto env_p = WSInitialize((void*)0);
@khaotik
khaotik / test_roll.py
Created March 14, 2017 10:49
roll using generalized elemwise op
from random import randint
from time import time
import numpy as np
import theano as th
import theano.tensor as T
from theano.tensor.padding import idx, at_idx
N = 256
x = T.matrix()
@khaotik
khaotik / test_circconv.py
Created March 14, 2017 10:19
circular convolution with generalized elemwise op
from itertools import product
from random import randint
from time import time
import numpy as np
import theano as th
T = th.tensor
from theano.tensor.padding import idx, at_idx
from theano.tensor.signal import conv
N = 256