Skip to content

Instantly share code, notes, and snippets.

View ekzhang's full-sized avatar

Eric Zhang ekzhang

View GitHub Profile
@ekzhang
ekzhang / lspclass.py
Last active May 7, 2025 21:50
Simplified NotebookLsp class for Python — see usage example at the end
from __future__ import annotations
import asyncio
import json
import logging
import sys
from collections import deque
from dataclasses import dataclass
from pathlib import PurePosixPath
from typing import Any, Dict, Optional
@ekzhang
ekzhang / webgpu_sync_reader.ts
Created April 12, 2025 20:11
Synchronously read data from a WebGPU buffer
/**
* Graphics state used to synchronously read data from WebGPU buffers.
*
* This trick is borrowed from TensorFlow.js. Basically, the idea is to create
* an offscreen canvas with one pixel for every 4 bytes ("device storage"), then
* configure it with a WebGPU context. Copy the buffer to a texture, then draw
* the canvas onto another offscreen canvas with '2d' context ("host storage").
*
* Once it's on host storage, we can use `getImageData()` to read the pixels
* from the image directly.
@ekzhang
ekzhang / hello_matmul.cu
Last active February 2, 2025 19:24
Get started with matmul on CUDA — following https://siboehm.com/articles/22/CUDA-MMM
// nvcc hello_matmul.cu -o hello_matmul
// ./hello_matmul
// Quick setup for cloud GPU with VS Code (Modal):
//
// $ pip install modal && modal setup
// $ modal volume create learn-cuda
// $ modal launch vscode --gpu t4 --volume learn-cuda --image nvidia/cuda:12.4.0-devel-ubuntu22.04
#include <cstdio>
@ekzhang
ekzhang / what-is-modal.md
Last active September 12, 2024 12:51
What is Modal? (2 min)

What is [[Modal]] (to an audience of my people at Harvard/MIT):

(Preface: I really like computers! Just saying.)

Modal does two things: rent out flexible increments of compute, and build the systems for people to interact with remote computers.

Why? Computers are all around us; you checked the weather this morning, but a wind turbine operator might check the state of the electrical grid, restaurants manage inventory, even the ISS navigates in orbit with computers.

Computers are how we process huge amounts of data (in energy, elections, housing, biology…), which would never be possible by hand, and in that sense computation augments how we understand the world.

@ekzhang
ekzhang / Moonray.md
Created September 10, 2024 17:27
Notes on Moonray
  • MoonRay — [[September 8th, 2024]]
    • AOV system is interesting. Light path expressions primarily.
      • "Material AOV" provides different kinds of syntax for debugging materials.
    • Use of different JIT compilation through LLVM to implement their ISPC framework. SPMD computation on an Arras cluster.
  • This is also why the BVH data structure is important? Intel Embree vs Nvidia RTX
@ekzhang
ekzhang / hashlife.rs
Created July 1, 2024 20:30
An incomplete interview question for performance-optimizing Hashlife in Rust
use std::{collections::HashMap, sync::Arc};
use md5::{Digest, Md5};
#[derive(Clone)]
pub enum Node {
Subtree(Arc<Subtree>),
Leaf(Leaf),
}
@ekzhang
ekzhang / posix_psutil_net.py
Created February 14, 2024 23:34
Standalone net_connections() function from psutil for POSIX
"""This is a vendored copy of parts of the 'psutil' Python package.
It only contains what's needed to run the `net_connections()` function, which
lists active network connections from all processes on the system.
"""
import base64
from collections import defaultdict, namedtuple
import errno
@ekzhang
ekzhang / plasmid_data.py
Created November 19, 2023 17:34
Fast API for plasmid data from PLSDB
import json
import subprocess
import numpy as np
import pandas as pd
from fastapi import HTTPException
from modal import Image, Stub, web_endpoint
stub = Stub("plasmid-data")
@ekzhang
ekzhang / block_event_loop.py
Created August 16, 2023 17:24
Demo of yield-dependent behavior when a coroutine blocks the event loop.
# Demo of yield-dependent behavior when a coroutine blocks the event loop.
#
# Vary the number of calls to asyncio.sleep(0.0) in either block, and behavior will change greatly.
#
# (4 in top block, 9 in bottom block)
# 0 3.0027458667755127
# 1 2.0022785663604736
# 2 8.320808410644531e-05
# 3 4.38690185546875e-05
# 4 3.838539123535156e-05