Skip to content

Instantly share code, notes, and snippets.

View gautierdag's full-sized avatar
🇺🇦

Gautier Dagan gautierdag

🇺🇦
View GitHub Profile
@gautierdag
gautierdag / cache.py
Created April 27, 2023 10:57
Simple caching of openai Embeddings
import openai
text_to_embeddings_cache = {}
def get_embeddings_for_instructions(instructions: list[str]):
response = openai.Embedding.create(
input=instructions,
model="text-embedding-ada-002"
)
return response['data']
@gautierdag
gautierdag / pretrain.py
Created May 23, 2022 20:42
Pretrain transformer using MLM objective from Pandas dataframe
import pandas as pd
from datasets import Dataset
from transformers import (
AutoModelForMaskedLM,
AutoTokenizer,
DataCollatorForLanguageModeling,
Trainer,
TrainingArguments,
)
@gautierdag
gautierdag / deepspeedbug.py
Created July 21, 2021 14:57
RuntimeError: output with shape [1] doesn't match the broadcast shape [1024, 1024]
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from transformers import AutoModel, AutoTokenizer
from transformers.optimization import get_cosine_schedule_with_warmup
import pytorch_lightning as pl
# from deepspeed.ops.adam import FusedAdam - get different error (expected tensor on cuda but got cpu) with FusedAdam
class BoringDataset(torch.utils.data.Dataset):
@gautierdag
gautierdag / layerwise_lr.py
Created July 5, 2021 10:24
Pytorch Bert Layer-wise Learning Rate Decay
import torch
from torch.optim import AdamW
from transformers import AutoModel
def get_bert_layerwise_lr_groups(bert_model, learning_rate=1e-5, layer_decay=0.9):
"""
Gets parameter groups with decayed learning rate based on depth in network
Layers closer to output will have higher learning rate
Args:
@gautierdag
gautierdag / update_tables.sh
Created June 11, 2021 19:54
Make all DynamoDB Tables On-Demand - Use with caution ⚠️
for name in $(aws dynamodb list-tables | jq .TableNames)
do
l=${#name}
if [ ${#name} -le 3 ]; then echo "Skipping name!"
else
short=${name:1:l-2} # clean json string representation of dataset
if [ "${short: -1}" == '"' ]
then short=${name:1:l-3}
fi
echo "Updating Table: ${short}"
@gautierdag
gautierdag / multiple_optims_pl.py
Created April 14, 2021 12:04
Example of how to use different optims for different layers or modules using pytorch lightning
import torch
import torch.nn as nn
import pytorch_lightning as pl
class BoringModel(pl.LightningModule):
def __init__(
self
):
super(BoringModel, self).__init__()
self.automatic_optimization = False
@gautierdag
gautierdag / NCE.py
Last active April 12, 2021 21:50
Pytorch NCE Loss
import torch
import torch.nn as nn
import pytorch_lightning as pl
class NCE(pl.LightningModule):
"""
This implementation is taken from https://github.com/Spijkervet/SimCLR/blob/master/simclr/modules/nt_xent.py
The mask_correlated_samples funtion has been modified to be much faster to compute
and therefore be able to be called at train time without a predifined batch size.
"""
@gautierdag
gautierdag / model_with_noam.py
Last active September 25, 2023 02:27
pytorch-lightning Transformer (noam) lr policy
import torch
import pytorch_lightning as pl
class MyTransformer(pl.LightningModule):
def __init__(
self,
learning_rate=0.001,
warmup=4000,
):
self.learning_rate = learning_rate
@gautierdag
gautierdag / print_macro.workflow
Last active September 9, 2020 09:22
MacOS macro for wrapping line in text with print("....")
# https://apple.stackexchange.com/questions/175215/how-do-i-assign-a-keyboard-shortcut-to-an-applescript-i-wrote
on run {input, parameters}
tell application "System Events"
key code 123 using command down
keystroke "print("
key code 124 using command down
keystroke ")"
end tell
@gautierdag
gautierdag / numpy_compress.py
Last active July 15, 2020 10:47
Numpy Optimized Compress
import numpy as np
#cast as object to be able to set np.nan and handle different types
arr = df[cols].values.astype(object)
# build shifted array
shifted = np.roll(arr, 1)
shifted[0] = np.nan
# choose indexes based on shift comparaison