Skip to content

Instantly share code, notes, and snippets.

View yzhangcs's full-sized avatar
:octocat:
Focusing

Yu Zhang yzhangcs

:octocat:
Focusing
View GitHub Profile
@chu-tianxiang
chu-tianxiang / rerope.py
Last active March 8, 2024 02:00
triton implementation of ReRope
# Adapted from the triton implementation of flash-attention v2
# https://github.com/openai/triton/blob/main/python/tutorials/06-fused-attention.py
import time
import torch
import torch.utils.benchmark as benchmark
import triton
import triton.language as tl
@triton.jit
@hankcs
hankcs / ontonotes_to_conll.sh
Last active August 17, 2022 10:41
This script downloads and compiles the Ontonotes 2012 data into conll format. Modified from https://github.com/allenai/allennlp/blob/c4c532d25e012dbe6ab1ac14bca75e53e0acc621/scripts/compile_coref_data.sh
#!/bin/bash
# This script downloads and compiles the Ontonotes 2012 data in a helpful format
# for co-reference resolution. It generates 3 files: {train, dev, test}.english.v4_gold_conll,
# as well as a directory 'conll-2012' which contains the raw extracted data.
# The script downloads and runs some python scripts which require python 2.X.
ONTONOTES_PATH=$1
LANGUAGE=$2
# Author: bbrighttaer
# Date: 5/24/19
# Time: 12:27 AM
# File: math.py
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
@ferrine
ferrine / network_builder.py
Last active September 28, 2024 18:48
Pytorch models from yaml files
import torch.nn
import collections
class Builder(object):
def __init__(self, *namespaces):
self._namespace = collections.ChainMap(*namespaces)
def __call__(self, name, *args, **kwargs):
try:
return self._namespace[name](*args, **kwargs)
@thomwolf
thomwolf / parallel.py
Last active August 8, 2023 15:50
Data Parallelism in PyTorch for modules and losses
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
## Created by: Hang Zhang, Rutgers University, Email: [email protected]
## Modified by Thomas Wolf, HuggingFace Inc., Email: [email protected]
## Copyright (c) 2017-2018
##
## This source code is licensed under the MIT-style license found in the
## LICENSE file in the root directory of this source tree
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""Encoding Data Parallel"""
@thomwolf
thomwolf / gradient_accumulation.py
Last active October 21, 2024 08:08
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...
@jeasinema
jeasinema / weight_init.py
Last active July 30, 2024 16:35
A simple script for parameter initialization for PyTorch
#!/usr/bin/env python
# -*- coding:UTF-8 -*-
import torch
import torch.nn as nn
import torch.nn.init as init
def weight_init(m):
'''
@goldsborough
goldsborough / install-gcc.sh
Last active July 29, 2024 16:48
Instructions for installing GCC >= 4.9 for PyTorch Extensions
# Instructions for installing GCC 4.9 on various platforms.
# The commands show instructions for GCC 4.9, but any higher version will also work!
# Ubuntu (https://askubuntu.com/questions/466651/how-do-i-use-the-latest-gcc-on-ubuntu/581497#581497)
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-4.9 g++-4.9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.9
@alexdevero
alexdevero / markdown.reg
Last active January 3, 2024 08:48
Add markdown as an item to “New” context menu in Windows 10 (https://superuser.com/questions/638755/add-item-to-new-context-menu-in-windows-8#640486).
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.md]
@="markdown"
[HKEY_CLASSES_ROOT\.md\ShellNew]
"NullFile"=""
[HKEY_CLASSES_ROOT\markdown]
@="Blank Markdown file"
@mblondel
mblondel / projection_simplex_vectorized.py
Last active July 2, 2024 08:55
Vectorized projection onto the simplex
# Author: Mathieu Blondel
# License: BSD 3 clause
import numpy as np
def projection_simplex(V, z=1, axis=None):
"""
Projection of x onto the simplex, scaled by z:
P(x; z) = argmin_{y >= 0, sum(y) = z} ||y - x||^2