Skip to content

Instantly share code, notes, and snippets.

View shreyansh26's full-sized avatar
👨‍🎓
Always Learning

Shreyansh Singh shreyansh26

👨‍🎓
Always Learning
View GitHub Profile
@Chillee
Chillee / prefixlm.py
Created August 14, 2024 17:38
FlexAttention examples
import torch.nn as nn
import copy
import torch
from torch.nn.attention.flex_attention import flex_attention, create_block_mask, or_masks, create_mask
from triton.testing import do_bench
from functools import partial
torch.set_default_device('cuda')
B = 4
@Raibows
Raibows / split_qkv4vllm.py
Created May 13, 2024 02:05
split merged qkv_proj of lora for phi-3, useful for enabling lora in VLLM.
import torch
import os
import json
from safetensors.torch import load_file, save_file
def replicate_lora_a(name: str, weight: "torch.Tensor") -> dict[str, "torch.Tensor"]:
prefix, suffix = name.split('qkv_proj')
res = {}
for t in ['q_proj', 'k_proj', 'v_proj']:
name = f"{prefix}{t}{suffix}"
@Chillee
Chillee / mm_weird.py
Last active July 31, 2024 06:20
Strangely, Matrix Multiplications Run Faster When Given "Predictable" Data! https://www.thonking.ai/p/strangely-matrix-multiplications
import torch
torch.set_default_device('cuda')
from triton.testing import do_bench
from collections import defaultdict
from functools import partial
import random
random.seed(0)
def get_flops(A, B):
ms = do_bench(lambda: torch.mm(A, B))
@zphang
zphang / hf_arxiv.md
Created August 13, 2023 08:07
Redirect HF papers to arXiv

Reinforcement Learning for Language Models

Yoav Goldberg, April 2023.

Why RL?

With the release of the ChatGPT model and followup large language models (LLMs), there was a lot of discussion of the importance of "RLHF training", that is, "reinforcement learning from human feedback". I was puzzled for a while as to why RL (Reinforcement Learning) is better than learning from demonstrations (a.k.a supervised learning) for training language models. Shouldn't learning from demonstrations (or, in language model terminology "instruction fine tuning", learning to immitate human written answers) be sufficient? I came up with a theoretical argument that was somewhat convincing. But I came to realize there is an additional argumment which not only supports the case of RL training, but also requires it, in particular for models like ChatGPT. This additional argument is spelled out in (the first half of) a talk by John Schulman from OpenAI. This post pretty much

@moyix
moyix / codegen_gptj_convert.py
Created July 22, 2022 19:33
Convert a SalesForce CodeGen model's weights to plain GPT-J
#!/usr/bin/env python
import argparse
import torch
from transformers import GPTJForCausalLM, GPTJConfig
# Note: these need the git version of Transformers as of 7/22/2022
from transformers import CodeGenTokenizer, CodeGenForCausalLM
from transformers import CODEGEN_PRETRAINED_MODEL_ARCHIVE_LIST
parser = argparse.ArgumentParser('Convert SalesForce CodeGen model to GPT-J')
@tdhopper
tdhopper / .editorconfig
Last active July 21, 2022 12:52
Configuration files to enable validating python code with flake8, mypy, isort, and black using pre-commit hooks (via https://pre-commit.com/). Also an .editorconfig file for standardization across editors.
# http://editorconfig.org/#file-format-details
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
@muellerzr
muellerzr / lr-suggestors.ipynb
Last active April 19, 2023 04:06
LR Suggestors.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mcarilli
mcarilli / nsight.sh
Last active September 10, 2024 11:50
Favorite nsight systems profiling commands for Pytorch scripts
# This isn't supposed to run as a bash script, i named it with ".sh" for syntax highlighting.
# https://developer.nvidia.com/nsight-systems
# https://docs.nvidia.com/nsight-systems/profiling/index.html
# My preferred nsys (command line executable used to create profiles) commands
#
# In your script, write
# torch.cuda.nvtx.range_push("region name")
# ...
@huchenxucs
huchenxucs / pos_embed.py
Created July 23, 2020 06:09
T5 relative positional embedding
import math
import torch
import torch.nn as nn
from torch.nn import functional as F
class RelativePositionBias(nn.Module):
def __init__(self, bidirectional=True, num_buckets=32, max_distance=128, n_heads=2):
super(RelativePositionBias, self).__init__()
self.bidirectional = bidirectional