Skip to content

Instantly share code, notes, and snippets.

View Ryu1845's full-sized avatar
🎯
Focusing

Sofian Mejjoute Ryu1845

🎯
Focusing
View GitHub Profile
from typing import Callable
import numpy as np
from tqdm import tqdm
def wsola_chunked_processing(audio: np.ndarray, sr: int, chunk_size: int, hop_size: int, mod_func: Callable[[np.ndarray], np.ndarray]):
# Check if chunk_size is larger than the audio length
if chunk_size >= len(audio):
# Process the entire audio in one go
output = mod_func(audio).squeeze()
@lucidrains
lucidrains / tree_attn_decode.py
Created August 12, 2024 17:48
Tree Attention Decoding
import torch
from torch import einsum
import torch.distributed as dist
def tree_attn_decode(q, k, v):
"""
Algorithm 3 proposed in Tree Attention
https://arxiv.org/abs/2408.04093
"""
""" Computing zeroth matrix powers via Lakic 1998.
paper: "On the Computation of the Matrix k-th Root"
Suppose we have a matrix G = USV^T and we want to compute
G^0 defined via G^0 = UV^T. We might want to do this to run
"stochastic spectral descent" of Carlson et al 2015. The
naive way to do this is via the SVD. But we can also just do
(GG^T)^(-1/2) G or alternatively G (G^TG)^(-1/2) and apply
the iterative method from Lakic 1998.
"""Generates a document causal attention mask based on a document ID tensor"""
from typing import List, Union
import torch
from torch import Tensor
from torch.nn.attention.flex_attention import _mask_mod_signature, or_masks
from attn_gym.masks import causal_mask
@secemp9
secemp9 / reset_gpu.sh
Created June 29, 2024 10:30
Resetting gpu
#!/bin/bash
unbind_gpu() {
echo "Unbinding NVIDIA driver..."
GPU_PCI=$(lspci | grep -i nvidia | cut -d ' ' -f 1)
for gpu in $GPU_PCI; do
echo -n "0000:$gpu" > /sys/bus/pci/drivers/nvidia/unbind
done
}
import numpy as np
from openai import OpenAI
import plotly
import plotly.graph_objs as go
import umap
url = "http://localhost:80"
client = OpenAI(
@crowsonkb
crowsonkb / mos.py
Last active April 11, 2024 21:23
Mixture of Softmaxes
"""Mixture of Softmaxes"""
import torch
from torch.nn import functional as F
class MixtureOfSoftmaxes(torch.autograd.Function):
@staticmethod
def forward(ctx, x, p):
with torch.cuda.amp.autocast(enabled=False):
@aredden
aredden / cuda_python_cudart_types.py
Created March 12, 2024 22:07
NVIDIA cuda-python's cudart typings for their untyped cpp bound library.
from typing import List, Any
import enum
from cuda import cudart
CUDART_VERSION = 12020
CUDA_EGL_MAX_PLANES = 3
CUDA_IPC_HANDLE_SIZE = 64
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
# This layer is dropped into your pre-trained PyTorch model where nn.Linear is used
class DoRALayer(nn.Module):
def __init__(self, d_in, d_out, rank=4):
super().__init__()
@euclaise
euclaise / scan.py
Created February 9, 2024 00:08
Prefix-sum scan in PyTorch
import torch
from torch.nn import functional as F
import math
from typing import Callable
def split(xs):
xs = [x.view(x.shape[0], x.shape[-1]//2, 2) for x in xs]
return [x[: , :, 0] for x in xs], [x[:, :, 1] for x in xs]
def merge1(l, r):