Skip to content

Instantly share code, notes, and snippets.

View moyix's full-sized avatar

Brendan Dolan-Gavitt moyix

View GitHub Profile
#!/usr/bin/env python
import os
import sys
import subprocess as sp
import tempfile
import hashlib
script_dir = os.path.dirname(os.path.realpath(__file__))
sys.path.append(script_dir)
from fast_check_for_ffast_math import check_file
@moyix
moyix / fast_check_for_ffast_math.py
Created September 2, 2022 16:12
A faster check to see if a binary has a constructor that enables FTZ/DAZ that just does byte matching
import sys
import mmap
from elftools.elf.elffile import ELFFile, ELFError
import struct
set_fast_math_code = bytes.fromhex('0fae5c24fc814c24fc408000000fae5424fcc3')
def load_bytes_from_elf(bindata, elf, vaddr, size):
try:
paddr = next(iter(elf.address_offsets(vaddr)))
$ objdump -s -j .init_array ./jaxlib/xla_extension.so | sed -e '1,/Contents/ d' | cut -c 10-44 | xxd -r -p | od -A none -w8 -t x8 --endian=little | addr2line -a -f -e ./jaxlib/xla_extension.so | paste -sd ' \n' | c++filt
0x000000000084c5e0 __cpu_indicator_init /dt9-src/libgcc/config/i386/cpuinfo.c:434
0x000000000084ca20 frame_dummy crtstuff.c:?
0x000000000079c440 _GLOBAL__sub_I_xla.cc xla.cc:?
0x000000000079c540 _GLOBAL__sub_I_dlpack.cc dlpack.cc:?
0x000000000079c5f0 _GLOBAL__sub_I_mlir.cc mlir.cc:?
0x000000000079c620 _GLOBAL__sub_I_ops.cc ops.cc:?
0x000000000079c650 _GLOBAL__sub_I_approx_topk.cc approx_topk.cc:?
0x000000000079c680 _GLOBAL__sub_I_approx_topk_shape.cc approx_topk_shape.cc:?
0x000000000079c6b0 _GLOBAL__sub_I_lu_decomposition.cc lu_decomposition.cc:?
@moyix
moyix / check_for_ffast_math.py
Last active October 29, 2022 11:42
Hacky script to check for the set_fast_math constructor in an executable/shared library using objdump
#!/usr/bin/env python
import subprocess
import re
import sys
def get_init_array(filename):
# Call objdump -s -j .init_array <filename> to get the contents of the .init_array section
try:
objdump_output = subprocess.check_output(['objdump', '-s', '-j', '.init_array', filename], stderr=subprocess.STDOUT)
@moyix
moyix / 00_output.txt
Created August 30, 2022 00:54
Demo of extending a rotary position embedding model to a longer context than it was trained on
(sfcodegen) moyix@isabella:~$ python load_codegen_with_longer_context.py
vocab_file vocab.json
merges_file merges.txt
tokenizer_file tokenizer.json
added_tokens_file added_tokens.json
special_tokens_map_file special_tokens_map.json
tokenizer_config_file tokenizer_config.json
Partial prompt from /usr/include/stdlib.h:
[...] restrict __nptr,
#!/usr/bin/env python
import torch
from transformers import CodeGenConfig, CodeGenForCausalLM, CodeGenTokenizer
from transformers.utils.hub import cached_file
NEW_SIZE = 4096
cg_config = CodeGenConfig.from_pretrained('Salesforce/codegen-350M-mono')
cg_config.n_ctx = NEW_SIZE
@moyix
moyix / copilot_proxy.py
Created July 29, 2022 03:58
Proxy between Copilot and a Triton inference server running FasterTransformer
#!/usr/bin/env python
import time
import random
import string
from flask import Flask, request
import numpy as np
import tritonclient.grpc as client_util
import json
from tritonclient.utils import np_to_triton_dtype
@moyix
moyix / huggingface_gptj_convert.py
Created July 25, 2022 16:11
Convert HuggingFace GPT-J model to FasterTransformers
# Copyright (c) 2021-2022, NVIDIA CORPORATION. All rights reserved.
# Modified by Brendan Dolan-Gavitt, 2022
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
@moyix
moyix / CodeGen_GPTJ_Conversion.md
Last active May 5, 2025 14:22
How to convert the SalesForce CodeGen models to GPT-J

Using Linear Algebra to Convert a Large Code Model

Background

The SalesForce CodeGen models are a family of large language models trained on a large amount of natural language data and then fine-tuned on specialized datasets of code. Models of size 350M, 2B, 6B, and 16B parameters are provided in three flavors:

  • nl, the base model trained on The Pile, a large natural language dataset compiled by EleutherAI
  • multi, which is fine-tuned from the nl model on a dataset of code in multiple languages, scraped from GitHub, and
  • mono, which is fine-tuned from the multi model on Python code only.
@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')