Skip to content

Instantly share code, notes, and snippets.

View epwalsh's full-sized avatar

Pete epwalsh

  • Central Oregon
  • 16:20 (UTC -07:00)
  • X @epwalsh
View GitHub Profile
@epwalsh
epwalsh / shuffled_index.py
Last active February 16, 2023 02:44
Quick and easy way convert an array index into an index into a permutation of the array
import numpy as np
def num_bits(max_i: int) -> int:
return len(bin(max_i)[2:])
def int_to_bits(i: int, n: int, dtype=int) -> list[int]:
return [dtype(int(b)) for b in bin(i)[2:].zfill(n)]
@epwalsh
epwalsh / run.sh
Created February 1, 2022 21:41
Training GPT-J 6B with tango
# Step 1: Create and activate a new virtual environment (need Python 3.7 or newer)
virtualenv .venv
. .venv/bin/activate
# Step 2: Install latest PyTorch
# This assumes your drivers are compatable with CUDA 11.*. If not, see https://pytorch.org/
# for alternate install instructions.
pip install torch==1.10.2+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
# Step 3: Clone and install the "tango" repo which has the GPT-J example.
@epwalsh
epwalsh / load.py
Last active August 26, 2021 16:08
MultiProcessDataLoader zombie workers
import signal
import logging
import time
from transformers import AutoTokenizer
from allennlp.data.instance import Instance
from allennlp.data.dataset_readers import DatasetReader
from allennlp.data.data_loaders import MultiProcessDataLoader
from allennlp.data.fields import TransformerTextField
@epwalsh
epwalsh / t5_11b_allennlp.py
Last active May 5, 2021 18:19
Running T5-11B predictions in AllenNLP
# Requires allennlp>=2.4.0, allennlp_models>=2.4.0
from allennlp_models.generation.predictors import Seq2SeqPredictor
ARTICLE_TO_SUMMARIZE = '''
summarize: The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building,
and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side.
During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest
man-made structure in the world, a title it held for 41 years until the Chrysler Building in
New York City was finished in 1930. It was the first structure to reach a height of 300 metres.
@epwalsh
epwalsh / config.jsonnet
Created March 26, 2021 21:25
AllenNLP and W&B
local target_namespace = "target_tokens";
{
"dataset_reader": {
"target_namespace": target_namespace,
"type": "copynet_seq2seq",
"source_token_indexers": {
"tokens": {
"type": "single_id",
"namespace": "source_tokens"
from typing import List, Tuple, Dict, Any
import torch
from allennlp.common.lazy import Lazy
from allennlp.common.params import Params
from allennlp.training.optimizers import Optimizer
ParameterGroupType = List[Tuple[List[str], Dict[str, Any]]]
@epwalsh
epwalsh / ci.yml
Last active January 5, 2021 15:06
Python GitHub Actions with venv cache
- uses: actions/cache@v2
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('setup.py') }}-${{ hashFiles('dev-requirements.txt') }}
@epwalsh
epwalsh / ci.yml
Last active November 3, 2020 10:39
Python GitHub Actions with pip cache
steps:
- uses: actions/checkout@v2
- name: Setup Python
uses: actions/setup-python@v1
with:
python-version: 3.7
- uses: actions/cache@v2
with:
@epwalsh
epwalsh / ci.yml
Last active September 24, 2020 18:43
Python GitHub Actions
name: PR
on:
pull_request:
branches:
- master
jobs:
checks:
name: Checks
runs-on: ubuntu-latest
steps:
@epwalsh
epwalsh / transformer_qa_multi_gpu.jsonnet
Last active August 19, 2020 21:46
Transformer QA multi GPU
local transformer_model = 'bert-base-cased';
local epochs = 3;
local batch_size = 8;
local train_path = "https://allennlp.s3.amazonaws.com/datasets/squad/squad-train-v1.1.json";
local dev_path = "https://allennlp.s3.amazonaws.com/datasets/squad/squad-dev-v1.1.json";
{
"dataset_reader": {
"type": "transformer_squad",
"transformer_model_name": transformer_model,