Skip to content

Instantly share code, notes, and snippets.

View danielhavir's full-sized avatar

Daniel Havir danielhavir

View GitHub Profile
@danielhavir
danielhavir / mathpix2gpt.py
Created August 1, 2023 00:17 — forked from danielgross/mathpix2gpt.py
mathpix2gpt.py
import requests
import time
import os
import sys
import openai
import tiktoken
from termcolor import colored
openai.api_key = open(os.path.expanduser('~/.openai')).read().strip()
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINhqM2fhXk0Qgl+6R9kPjJ/48ZrNcXjc/7zTn8oi88nH daniel@Daniels-MacBook-Pro.local
class LoRAConv1DWrapper(nn.Module):
"""SimpleWrapper class that implements LoRA: Low-Rank Adaptation of Large Language Models.
Arxiv link: https://arxiv.org/abs/2106.09685"""
def __init__(self, conv1dmodule: transformers.pytorch_utils.Conv1D, lora_rank: int):
super().__init__()
self.base_module = conv1dmodule
d1, d2 = self.base_module.weight.size()
self.A = nn.Parameter(
@danielhavir
danielhavir / k_armed_bandit.py
Last active July 8, 2022 18:25
ε-greedy action selection, sample-average action-value estimates
import argparse
import multiprocessing as mp
from functools import partial
import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt
STEPS = 10000
# Full Example: https://gist.github.com/danielhavir/407a6cfd592dfc2ad1e23a1ed3539e07
import os
from typing import Callable, List, Tuple, Generator, Dict
import torch
import torch.utils.data
from PIL.Image import Image as ImageType
def list_items_local(path: str) -> List[str]:
au BufNewFile,BufRead *.py set tabstop=4
au BufNewFile,BufRead *.py set softtabstop=4
au BufNewFile,BufRead *.py set shiftwidth=4
au BufNewFile,BufRead *.py set textwidth=79
au BufNewFile,BufRead *.py set expandtab
au BufNewFile,BufRead *.py set autoindent
au BufNewFile,BufRead *.py set fileformat=unix
highlight BadWhitespace ctermbg=red guibg=red
au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match BadWhitespace /\s\+$/
@danielhavir
danielhavir / cpu_monitor.py
Last active February 23, 2022 11:08
Monitor GPU usage, memory and temperature in Visdom during training
from visdom import Visdom
import numpy as np
from time import sleep
import logging
from threading import Event
try:
import psutil
except ImportError:
logging.error("You must \"pip install psutil\"")
raise
def style_loss(style_features, generated_features, J):
loss_style = 0.
for j in J:
gram_style = gram_matrix(style_features[j])
gram_gen = gram_matrix(generated_features[j])
loss_style += F.mse_loss(gram_gen, gram_style)
return loss_style
def content_loss(content_features, generated_features, j):
loss_content = F.mse_loss(generated_features[j], content_features[j])
return loss_content
def gram_matrix(feature_map):
C, H, W = feature_map.size()
reshaped_map = feature_map.view(C, H*W)
G = reshaped_map.mm(reshaped_map.t()) / (C * H * W)
return G