Skip to content

Instantly share code, notes, and snippets.

View tjyuyao's full-sized avatar
🎯
Focusing

Yuyao Huang tjyuyao

🎯
Focusing
View GitHub Profile
@tjyuyao
tjyuyao / left_aligned_gather.py
Created November 19, 2023 03:02
torch left aligned gather
def left_aligned_gather(input, index):
""" take along the last dim in index, while index shape is left-aligned with input and can be broadcasted.
"""
si = list(index.shape)
sx = list(input.shape)[len(si):]
so = si + sx
ex = si + [1 for _ in sx]
index = index.view(ex).expand(so)
return input.gather(len(si)-1, index)
@tjyuyao
tjyuyao / torch_debug_nan.py
Last active November 22, 2023 04:11
utils to debug pytorch nan values
import inspect
import re
import torch
from torch import Tensor
def allfinite(x: Tensor) -> bool:
return torch.isfinite(x).all().item()
@tjyuyao
tjyuyao / torch_pqdm.py
Created July 19, 2023 14:05
pytorch based parallel tqdm loader
def pqdm(func, data, n_jobs=2):
# pytorch based dataloader does not block for the whole results,
# it also accepts locally defined functions.
# These features make it favorable compared to the pqdm library or the standard multiprocessing library.
from torch.utils.data import DataLoader
from tqdm import tqdm
datalen = len(data)
@tjyuyao
tjyuyao / model_convert.py
Last active October 26, 2022 01:32
convert pytorch model weight by shape matching
import torch
import torch.nn as nn
def convert_pretrained(net: nn.Module, load_path: str, save_path:str):
"""convert 3rd party pretrained weights into target module assuming the state order is same.
Args:
net_type (Type): ice-learn module type.
path (str): saved module state_dict file.
"""
@tjyuyao
tjyuyao / remote_cmder.py
Last active October 26, 2022 02:03
This is for running commands on multiple remote hosts from local python notebooks.
from __future__ import annotations
from typing import List
import paramiko
from scp import SCPClient
import time
from dataclasses import dataclass
@dataclass
@tjyuyao
tjyuyao / test_grid_sample_indexing.py
Last active May 7, 2022 04:35
test storage indexing order of grid required grid_sample in pytorch
import torch
import torch.nn.functional as F
size = (3, 4)
H, W = size
def xy():
x = torch.linspace(-1, 1, W)
y = torch.linspace(-1, 1, H)
@tjyuyao
tjyuyao / pycuda_torch_accessor.py
Last active April 6, 2022 08:37
pycuda and pytorch direct inter operation on tensors with multidimensional element accessor.
import torch
import cutex
M, N, K = 4, 4, 1
a = torch.rand((M, K), dtype=torch.float32).cuda()
b = torch.rand((K, N), dtype=torch.float32).cuda()
c = torch.empty((M, N), dtype=torch.float32).cuda()
kernels = cutex.SourceModule(r"""
__global__ void matmul(Tensor<float, 2> *a, Tensor<float, 2> *b, Tensor<float, 2> *c, int M, int N, int K) {
@tjyuyao
tjyuyao / env.sh
Last active November 10, 2020 08:36
Project wise environment utils.
export PRJNAME="STViT"
export PRJ_ROOT="/home/hyuyao/prj/$PRJNAME"
export SRC_ROOT="$PRJ_ROOT/src/$PRJNAME"
export DATA_ROOT="$PRJ_ROOT/data"
export UTILS_ROOT="$PRJ_ROOT/utils" # put current env.sh to $PRJ_ROOT/utils
export PATH=$PATH:$UTILS_ROOT
alias cddata="cd $DATA_ROOT"
alias cdsrc="cd $SRC_ROOT && git config fetch.prune true"
@tjyuyao
tjyuyao / steady_dropout.py
Created July 27, 2020 07:24
This dropout always select const number of units to dropout.
import torch
def steady_dropout(x, prob=0.2):
assert len(x.shape) == 2, "expected data shape of (batch, feature), while getting " + x.shape
n_batch = x.shape[0]
n_feat = x.shape[1]
n_select = int(round(n_feat * prob))
prob = float(n_select) / float(n_feat)
r = torch.randn(n_batch, n_feat)
_, i = r.sort(dim=1)
set -g mouse on
# to enable mouse scroll, see https://github.com/tmux/tmux/issues/145#issuecomment-150736967
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'copy-mode -e'"