Skip to content

Instantly share code, notes, and snippets.

View vwxyzjn's full-sized avatar
😃

Costa Huang vwxyzjn

😃
View GitHub Profile
@ArthurZucker
ArthurZucker / static_kv_cache.py
Last active May 24, 2024 14:12
simple static kv cache script
from transformers import AutoModelForCausalLM, AutoTokenizer, StaticCache
import torch
from typing import Optional
device = "cuda"
# Copied from the gpt-fast repo
def multinomial_sample_one_no_sync(probs_sort): # Does multinomial sampling without a cuda synchronization
q = torch.empty_like(probs_sort).exponential_(1)
return torch.argmax(probs_sort / q, dim=-1, keepdim=True).to(dtype=torch.int)
@Miffyli
Miffyli / assign-credits.py
Last active September 16, 2022 17:28
A very useful script for making sure everyone is credited accordingly
# A very useful script for giving credit where it is due: by replacing (most) variable names by the authors who wrote them.
# Requirements: pip install gitpython
# Usage: python3 assign-credits.py <input_file> <output_file>
# Example: python3 assign-credits.py ./src/main.py ./src/main-credited.py
# NOTE that this is an awful idea with an awful implementation. The "generated" code likely does not work
# (e.g. typing stuff is skipped, class attribute names are replaced). The "author" is decided by the current HEAD of git repo, and whoever
# defines the variable first will get the credits (hahaa dunno if even this is right).
# It is getting late and I am tired of typing so I will let Github Copilot write something for me.
@skye
skye / tpu_topology_env_vars.py
Last active March 28, 2024 05:52
You can use these environment variables to run a Python process on a subset of the TPU cores on a Cloud TPU VM. This allows running multiple TPU processes at the same time, since only one process can access a given TPU chip at a time. Note that on TPU v2 and v3, 1 TPU chip = 2 TpuDevice as reported by `jax.devices()` (8 devices total). On v4, 1 …
# ==== Non-communicating processes
# 4x 1 chip per process:
os.environ["TPU_CHIPS_PER_PROCESS_BOUNDS"] = "1,1,1"
os.environ["TPU_PROCESS_BOUNDS"] = "1,1,1"
# Different per process:
os.environ["TPU_VISIBLE_DEVICES"] = "0" # "1", "2", "3"
# Pick a unique port per process
os.environ["TPU_MESH_CONTROLLER_ADDRESS"] = "localhost:8476"
os.environ["TPU_MESH_CONTROLLER_PORT"] = "8476"
@thomwolf
thomwolf / gradient_accumulation.py
Last active January 16, 2024 02:38
PyTorch gradient accumulation training loop
model.zero_grad() # Reset gradients tensors
for i, (inputs, labels) in enumerate(training_set):
predictions = model(inputs) # Forward pass
loss = loss_function(predictions, labels) # Compute loss function
loss = loss / accumulation_steps # Normalize our loss (if averaged)
loss.backward() # Backward pass
if (i+1) % accumulation_steps == 0: # Wait for several backward steps
optimizer.step() # Now we can do an optimizer step
model.zero_grad() # Reset gradients tensors
if (i+1) % evaluation_steps == 0: # Evaluate the model when we...
@jagrosh
jagrosh / Github Webhook Tutorial.md
Last active May 21, 2024 15:55
Simple Github -> Discord webhook

Step 1 - Make a Discord Webhook

  1. Find the Discord channel in which you would like to send commits and other updates

  2. In the settings for that channel, find the Webhooks option and create a new webhook. Note: Do NOT give this URL out to the public. Anyone or service can post messages to this channel, without even needing to be in the server. Keep it safe! WebhookDiscord

Step 2 - Set up the webhook on Github

  1. Navigate to your repository on Github, and open the Settings Settings
@TheGU
TheGU / uploader.go
Created September 11, 2015 10:07
Golang to upload file to google drive with progress bar using Google API
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
@aolde
aolde / static_server.js
Last active February 27, 2024 23:00 — forked from ryanflorence/static_server.js
Simple web server in Node.js. This fork added mime types for common file types.
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs")
port = process.argv[2] || 8888,
mimeTypes = {
"html": "text/html",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"png": "image/png",