Skip to content

Instantly share code, notes, and snippets.

View qxcv's full-sized avatar

Sam Toyer qxcv

View GitHub Profile
@qxcv
qxcv / load_cuda.py
Last active June 27, 2024 22:39
CUDA library loading logic copied out of Torch's __init__.py
"""Hack to load CUDA variables shipped via PyPI. Addresses this Torch bug:
https://github.com/pytorch/pytorch/issues/101314
Copied from PyTorch's __init__.py file, with modifications:
https://github.com/pytorch/pytorch/blob/main/torch/__init__.py
Copyright notice below is from Torch.
"""
@qxcv
qxcv / from_gymnasium.py
Last active August 21, 2024 11:54
Gymnasium envs with Dreamer v3, along with a Minigrid example
# from_gym.py adapted to work with Gymnasium. Differences:
#
# - gym.* -> gymnasium.*
# - Deals with .step() returning a tuple of (obs, reward, terminated, truncated,
# info) rather than (obs, reward, done, info).
# - Also deals with .reset() returning a tuple of (obs, info) rather than just
# obs.
# - Passes render_mode='rgb_array' to gymnasium.make() rather than .render().
# - A bunch of minor/irrelevant type checking changes that stopped pyright from
# complaining (these have no functional purpose, I'm just a completionist who
@qxcv
qxcv / delete_numbered_files.py
Created October 1, 2022 02:45
Find and delete large groups of files that are identical except for a single number (preserving the largest-numbered entry)
import collections
import re
import os
number_re = re.compile(r"([0-9]+)")
def number_split_permutations(filename):
for match in number_re.finditer(filename):
start, end = match.span()
prefix = filename[:start]
@qxcv
qxcv / sort_pytest_durations.py
Created July 16, 2020 23:42
Sort the times produced by pytest --durations=0
# coding: utf-8
"""Some helpful functions for neatly displaying the output of `pytest
--durations=0`."""
import itertools
import re
import sys
def parse_lines(text):
results = []
for line in text.splitlines():
@qxcv
qxcv / testing_mp_bug.ipynb
Created February 8, 2020 01:09
Testing multiprocessing-transparent numpy array
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@qxcv
qxcv / peakmem.sh
Created January 16, 2020 19:03
Bash script to track peak system memory usage (mem + swap)
#!/bin/bash
max_mem=0
while [ true ]; do
# awk trick from https://stackoverflow.com/a/450821
mem="$(free --giga | sed 's/ \+/ /g' | cut -d ' ' -f 3 | tail -n +2 | awk '{s+=$1} END {print s}')"
if [ "$mem" -gt "$max_mem" ]; then
max_mem="$mem"
echo "new peak ${max_mem}GB at $(date)"
fi
sleep 0.1
@qxcv
qxcv / exit_with_parent.py
Created July 11, 2019 02:36
Forcing Python process to exit with parent
import ctypes
import sys
def parent_death_pact(signal=signal.SIGINT):
"""Commit to kill current process when parent process dies.
Each time you spawn a new process, run this to set signal
handler appropriately (e.g put it at the beginning of each
script, and in multiprocessing startup code)."""
assert sys.platform == 'linux', \
"this fn only works on Linux right now"
@qxcv
qxcv / mlp_compare_cpu_gpu.py
Created November 19, 2018 18:09
Script for comparing performance of CPU & GPU on a simple MLP
#!/usr/bin/env python3
# (this script also requires tensorflow-gpu to be installed & working)
# increase hidden size/batch size to close gap between CPU and GPU
DISABLE_GPU = True
HIDDEN_UNITS = 64
BATCH_SIZE = 64
if DISABLE_GPU:
import os
@qxcv
qxcv / freeglut_demo.py
Created October 25, 2018 03:43
FreeGLUT non-blocking drawing demo
#!/usr/bin/env python3
# Shows us to use non-blocking event loop from FreeGLUT. Original code from
# https://code.activestate.com/recipes/325391-open-a-glut-window-and-draw-a-sphere-using-pythono/
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
import sys
import threading
@qxcv
qxcv / spacemacs-cheatsheet.md
Last active January 18, 2017 18:22 — forked from 526avijitgupta/spacemacs-cheatsheet.md
Spacemacs cheatsheet

emacs --daemon to run in the background. emacsclient.emacs24 <filename/dirname> to open in terminal

M-m and SPC can be used interchangeably. I've used Ldr (for "leader") as a compromise.

  • Undo - C-/
  • Redo - C-?
  • Change case: 1. Camel Case : M-c 2. Upper Case : M-u
  1. Lower Case : M-l