Skip to content

Instantly share code, notes, and snippets.

View t-vi's full-sized avatar
🌍
Creating fun and friendly AIs

Thomas Viehmann t-vi

🌍
Creating fun and friendly AIs
View GitHub Profile
@t-vi
t-vi / speech_command_recognition_quantization_full.ipynb
Created July 19, 2021 10:51
Code for Training an Edge Optimized Speech Recognition Model with PyTorch Lightning
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@t-vi
t-vi / benchmarking.py
Last active October 18, 2018 12:09
native batch norm
import torch
import pickle
import math
print ("git revision:", torch.__version__)
with open('/tmp/1.0.0a0-058c128_timings.pkl', "rb") as f:
reference_timings_dict = pickle.load(f)
print ("""
input shape = (bs, channels) + features
mode = (training/eval)-(forward+backward)/forward
@t-vi
t-vi / batch_norm_backward.py
Last active October 14, 2018 14:48
Highly accurate batchnorm backward reductions.
csrc = """
#include <torch/extension.h>
#include <THC/THCDeviceUtils.cuh>
#include <THC/THCGeneral.h>
#include "ATen/ATen.h"
#include "ATen/AccumulateType.h"
#include "ATen/cuda/CUDAContext.h"
using namespace at;
@t-vi
t-vi / __init__.pyi
Last active July 13, 2023 09:11
PyTorch Type Hints work in progress (put into python3.x/dist-packages/torch/ directory to try)
from typing import List, Tuple, Optional, Union, Any, ContextManager, Callable, overload
import builtins
import math
import pickle
class dtype: ...
_dtype = dtype
import torch
from PIL import Image
from torch.utils.data import DataLoader
import torchvision
from torchvision import transforms, datasets
from torch.autograd import Variable
import torch.nn as nn
import torch.optim
import torch.backends.cudnn as cudnn; cudnn.benchmark = True
@t-vi
t-vi / example.py
Created November 21, 2017 19:05
add.at-like addition using sparse matrices
import torch
indices = torch.LongTensor([[1,1,1],
[2,1,1]]) # must be two dimensional with one row per dimension
values = torch.arange(1,4)
size = torch.Size((3,3))
a = torch.sparse.FloatTensor(indices, values, size)
b = torch.eye(3)
b += a
print (b)
@t-vi
t-vi / sine_wave_prediction_as_in_example.ipynb
Last active November 9, 2017 05:33
Pytorch Time Sequence Example
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@t-vi
t-vi / variance_of_grad.py
Created October 13, 2017 07:27
Computing the Variance of Gradients for Linear Layers
import torch
from torch.autograd import Variable
def linear_with_sumsq(inp, weight, bias=None):
def provide_sumsq(inp,w,b):
def _h(i):
if not hasattr(w, 'grad_sumsq'):
w.grad_sumsq = 0
w.grad_sumsq += ((i**2).t().matmul(inp**2))*i.size(0)
if b is not None:
from timeit import default_timer as time
import numpy as np
from numba import cuda
import os
os.environ['NUMBAPRO_LIBDEVICE']='/usr/lib/nvidia-cuda-toolkit/libdevice/'
os.environ['NUMBAPRO_NVVM']='/usr/lib/x86_64-linux-gnu/libnvvm.so.3.1.0'
import numpy
import torch
import ctypes