Skip to content

Instantly share code, notes, and snippets.

@kklemon
kklemon / mri_tensordicts.py
Last active February 1, 2024 08:50
Exemplary showcase implementation of MRI sequence handling with TensorDicts and memory mapped tensors
import torch
import nibabel as nib
import numpy as np
from tensordidct import TensorDict, MemmapTensor
files_by_modality = {
'flair': [...]
}
num_files = ...
@kklemon
kklemon / flash-attention-2-transformer.py
Last active March 22, 2024 15:01
PyTorch Transformer API compatible wrapper around FlashAttention-2
import torch.nn as nn
import torch.nn.functional as F
class FlashAttentionTransformerEncoder(nn.Module):
def __init__(
self,
dim_model,
num_layers,
num_heads=None,
@kklemon
kklemon / pytorch_transformer_benchmark.py
Last active July 4, 2023 07:09
PyTorch Transformer Benchmark
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
bz = 128
seq_len = 512
d_model = 64
n_heads = 8
@kklemon
kklemon / dash_bio_residue_coloring.py
Created January 26, 2023 16:00
Sample code for residue coloring in a dash-bio 3d molecule view
"""
Sample code for residue coloring in a dash-bio 3d molecule view
Requirements:
- dash
- dash-bio
- numpy
- biopython
pip install dash dash-bio numpy biopython
@kklemon
kklemon / iterable_dataset_dist.py
Last active May 14, 2024 02:48
PyTorch IterableDataset implementation with multiprocessing and distributed training support
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
from torch.utils.data import IterableDataset, DataLoader
class DistributedIterableDataset(IterableDataset):
"""
Example implementation of an IterableDataset that handles both multiprocessing (num_workers > 0)
@kklemon
kklemon / imagenet_class_labels.txt
Last active February 19, 2021 08:48
ImageNet ILSVRC2011 class label meta data
1 n02119789 kit fox, Vulpes macrotis
2 n02100735 English setter
3 n02096294 Australian terrier
4 n02066245 grey whale, gray whale, devilfish, Eschrichtius gibbosus, Eschrichtius robustus
5 n02509815 lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens
6 n02124075 Egyptian cat
7 n02417914 ibex, Capra ibex
8 n02123394 Persian cat
9 n02125311 cougar, puma, catamount, mountain lion, painter, panther, Felis concolor
10 n02423022 gazelle
@kklemon
kklemon / midi-program-change-events.json
Last active June 27, 2018 14:50
JSON file containing all MIDI program change events with the event number and instrument name hierarchically categorized. Parsed from https://en.wikipedia.org/wiki/General_MIDI#Program_change_events
[
{
"instruments": [
{
"name": "Acoustic Grand Piano",
"program": 1
},
{
"name": "Bright Acoustic Piano",
"program": 2