Skip to content

Instantly share code, notes, and snippets.

Avatar

Daniel Havir danielhavir

View GitHub Profile
View scroll.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINhqM2fhXk0Qgl+6R9kPjJ/48ZrNcXjc/7zTn8oi88nH daniel@Daniels-MacBook-Pro.local
View lora.py
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 Jul 8, 2022
ε-greedy action selection, sample-average action-value estimates
View k_armed_bandit.py
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
View scaleai2.py
# 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]:
View .vimrc
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 Feb 23, 2022
Monitor GPU usage, memory and temperature in Visdom during training
View cpu_monitor.py
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
View style_loss.py
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
View content_loss.py
def content_loss(content_features, generated_features, j):
loss_content = F.mse_loss(generated_features[j], content_features[j])
return loss_content
View gram_matrix.py
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
@danielhavir
danielhavir / query.go
Last active Oct 1, 2019
Simple utility CLI interface for encryption, IP querying and some other functions. Build and move the binary to "~/.local/bin"
View query.go
package main
import (
"crypto/rand"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os/exec"