Skip to content

Instantly share code, notes, and snippets.

View numb3r3's full-sized avatar

felix-wang numb3r3

View GitHub Profile
@afspies
afspies / gpu_allocation.py
Last active June 18, 2024 23:19
Automatic GPU Allocation
# EDIT 10/04/2022 - This version was provided by @jayelm who fixed some bugs and made the function much more robust
import os
import subprocess
import time
def assign_free_gpus(threshold_vram_usage=1500, max_gpus=2, wait=False, sleep_time=10):
"""
Assigns free gpus to the current process via the CUDA_AVAILABLE_DEVICES env variable
This function should be called after all imports,
@kklemon
kklemon / iterable_dataset_dist.py
Last active June 6, 2024 08:05
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)
import torch
import torch.nn as nn
import torch.nn.functional as F
from dataclasses import dataclass
import transformers.modeling_bert as modeling_bert
import nlpr.shared.torch_utils as torch_utils
import nlpr.shared.model_resolution as model_resolution
@SuperShinyEyes
SuperShinyEyes / f1_score.py
Created October 15, 2019 10:16
F1 score in PyTorch
def f1_loss(y_true:torch.Tensor, y_pred:torch.Tensor, is_training=False) -> torch.Tensor:
'''Calculate F1 score. Can work with gpu tensors
The original implmentation is written by Michal Haltuf on Kaggle.
Returns
-------
torch.Tensor
`ndim` == 1. 0 <= val <= 1
@lopuhin
lopuhin / pytorch_high_mem.py
Created October 15, 2019 10:06
pytorch high memory usage on CPU inference with variable input shapes
import os
os.environ['OMP_NUM_THREADS'] = '1'
import argparse
import resource
import sys
import time
import numpy as np
import torch
from torchvision.models import resnet34
@vi
vi / split_by_silence_kf.sh
Last active December 5, 2022 14:40
Video-enhanced split_by_silence.sh script.
#!/bin/bash
IN=$1
OUT=$2
true ${SD_PARAMS:="-55dB:d=0.3"};
true ${MIN_FRAGMENT_DURATION:="20"};
export MIN_FRAGMENT_DURATION
if [ -z "$OUT" ]; then
@alsrgv
alsrgv / pytorch_synthetic_benchmark_apex.py
Last active January 12, 2022 12:04
Horovod-PyTorch with Apex (look for "# Apex")
from __future__ import print_function
import argparse
import torch.backends.cudnn as cudnn
import torch.nn.functional as F
import torch.optim as optim
import torch.utils.data.distributed
from torchvision import models
import horovod.torch as hvd
import timeit
@thomwolf
thomwolf / gradient_accumulation.py
Last active January 16, 2024 02:38
PyTorch gradient accumulation training loop
model.zero_grad() # Reset gradients tensors
for i, (inputs, labels) in enumerate(training_set):
predictions = model(inputs) # Forward pass
loss = loss_function(predictions, labels) # Compute loss function
loss = loss / accumulation_steps # Normalize our loss (if averaged)
loss.backward() # Backward pass
if (i+1) % accumulation_steps == 0: # Wait for several backward steps
optimizer.step() # Now we can do an optimizer step
model.zero_grad() # Reset gradients tensors
if (i+1) % evaluation_steps == 0: # Evaluate the model when we...
@okomarov
okomarov / check_gpu_ram_colab.py
Created July 18, 2018 15:15
Check allocated GPU by Colab
# memory footprint support libraries/code
!ln -sf /opt/bin/nvidia-smi /usr/bin/nvidia-smi
!pip install gputil
!pip install psutil
!pip install humanize
import psutil
import humanize
import os
import GPUtil as GPU
"""Automatically build a vanilla or Cudnn RNN."""
import numpy as np
import tensorflow as tf
def gru(inputs,
num_layers,
num_units,
direction='unidirectional',