Skip to content

Instantly share code, notes, and snippets.

View jekbradbury's full-sized avatar

James Bradbury jekbradbury

View GitHub Profile
@MInner
MInner / gpu_profile.py
Created September 12, 2017 16:11
A script to generate per-line GPU memory usage trace. For more meaningful results set `CUDA_LAUNCH_BLOCKING=1`.
import datetime
import linecache
import os
import pynvml3
import torch
print_tensor_sizes = True
last_tensor_sizes = set()
gpu_profile_fn = f'{datetime.datetime.now():%d-%b-%y-%H:%M:%S}-gpu_mem_prof.txt'
@JonathanRaiman
JonathanRaiman / plan.py
Last active November 27, 2018 02:25
Dali graph transformation Plan
"""
Micro-dali JIT Plan:
- contains gemm, operator fusion, elementwise/reduction ops.
- supports tensordot
- supports 'jit'
- supports conversion from gemm + im2col to conv2d (NHWC)
- supports 'optimization' passes
- supports 'implementation' registries for specialization
(e.g. int vs float)
@jblumenau
jblumenau / party_constituency_vote_shares_may_19.csv
Created June 12, 2017 16:47
party_constituency_vote_shares_may_19
We can make this file beautiful and searchable if this error is corrected: Unclosed quoted field in line 6.
"","code","constituency","Con","Lab","LD","UKIP","Green","SNP","PC","Other","Con_hi","Lab_hi","LD_hi","UKIP_hi","Green_hi","SNP_hi","PC_hi","Other_hi","Con_lo","Lab_lo","LD_lo","UKIP_lo","Green_lo","SNP_lo","PC_lo","Other_lo"
"1","E14000530","Aldershot","55.0","25.5","12.3","4.7","2.5","0.0","0.0","0.0","61.3","32.8","17.1","7.1","4.3","0.0","0.0","0.0","48.7","19.4","8.8","2.7","1.4","0.0","0.0","0.0"
"2","E14000531","Aldridge-Brownhills","62.8","26.8","5.4","0.0","0.0","0.0","0.0","5.0","68.2","31.9","8.3","0.0","0.0","0.0","0.0","13.9","56.9","21.2","3.4","0.0","0.0","0.0","0.0","1.0"
"3","E14000532","Altrincham and Sale West","52.9","33.7","10.0","0.0","2.2","0.0","0.0","1.1","59.2","41.4","14.8","0.0","3.9","0.0","0.0","3.3","47.5","26.7","6.2","0.0","1.2","0.0","0.0","0.3"
"4","E14000533","Amber Valley","55.3","34.5","4.7","0.0","1.8","0.0","0.0","3.7","61.5","42.1","8.1","0.0","3.5","0.0","0.0","9.9","48.6","28.2","2.4","0.0","0.7","0.0","0.0","0.8"
"5","E14000534","Arundel and South Downs","60.9","17.
@jblumenau
jblumenau / party_constituency_vote_shares.csv
Created June 9, 2017 02:40
Constituency-level vote share predictions from YouGov MRP GE2017 model
We can make this file beautiful and searchable if this error is corrected: Unclosed quoted field in line 6.
"","code","constituency","Con","Lab","LD","UKIP","Green","SNP","PC","Other","Con_lo","Lab_lo","LD_lo","UKIP_lo","Green_lo","SNP_lo","PC_lo","Other_lo","Con_hi","Lab_hi","LD_hi","UKIP_hi","Green_hi","SNP_hi","PC_hi","Other_hi"
"1","E14000530","Aldershot","51.8","29.1","11.0","5.5","2.6","0.0","0.0","0.0","44.4","22.2","6.7","3.0","1.3","0.0","0.0","0.0","58.4","36.4","16.6","8.5","4.8","0.0","0.0","0.0"
"2","E14000531","Aldridge-Brownhills","62.3","29.3","4.8","0.0","0.0","0.0","0.0","3.5","53.9","21.4","2.4","0.0","0.0","0.0","0.0","0.6","69.3","36.5","8.4","0.0","0.0","0.0","0.0","10.8"
"3","E14000532","Altrincham and Sale West","48.6","35.2","11.1","0.0","1.9","0.0","0.0","3.2","41.3","28.5","6.7","0.0","0.8","0.0","0.0","0.6","56.0","42.0","17.0","0.0","3.8","0.0","0.0","8.3"
"4","E14000533","Amber Valley","51.3","40.7","3.2","0.0","2.2","0.0","0.0","2.7","43.9","33.7","1.5","0.0","0.8","0.0","0.0","0.3","58.0","47.4","5.7","0.0","4.7","0.0","0.0","8.8"
"5","E14000534","Arundel and South Downs","58.7","21.
@hyqneuron
hyqneuron / pytorch_visualize.py
Created June 7, 2017 07:06
PyTorch graph visualization
import torch
import torch.nn as nn
from torch.nn import Parameter
from torch.autograd import Variable, Function
from collections import defaultdict
import graphviz
"""
This is a rather distorted implementation of graph visualization in PyTorch.
from graphviz import Digraph
import torch
from torch.autograd import Variable, Function
def iter_graph(root, callback):
queue = [root]
seen = set()
while queue:
fn = queue.pop()
if fn in seen:
import autograd.numpy as np
import autograd.numpy.random as npr
from autograd import grad, jacobian, hessian, make_vjp
from autograd.core import backward_pass
# explicit dense version, for testing
def make_dense_ggnvp(f, g=lambda x: 1./2*np.dot(x, x)):
def ggnvp_maker(x):
J = jacobian(f)(x)
H = hessian(g)(f(x))
import torch
from torch import nn
from torch.autograd import Variable
import torch.nn.functional as F
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size, n_layers=1):
super(RNN, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size

A Tour of PyTorch Internals (Part I)

The fundamental unit in PyTorch is the Tensor. This post will serve as an overview for how we implement Tensors in PyTorch, such that the user can interact with it from the Python shell. In particular, we want to answer four main questions:

  1. How does PyTorch extend the Python interpreter to define a Tensor type that can be manipulated from Python code?
  2. How does PyTorch wrap the C libraries that actually define the Tensor's properties and methods?
  3. How does PyTorch cwrap work to generate code for Tensor methods?
  4. How does PyTorch's build system take all of these components to compile and generate a workable application?

Extending the Python Interpreter

PyTorch defines a new package torch. In this post we will consider the ._C module. This module is known as an "extension module" - a Python module written in C. Such modules allow us to define new built-in object types (e.g. the Tensor) and to call C/C++ functions.

@rtqichen
rtqichen / pytorch_weight_norm.py
Last active May 11, 2023 06:58
Pytorch weight normalization - works for all nn.Module (probably)
## Weight norm is now added to pytorch as a pre-hook, so use that instead :)
import torch
import torch.nn as nn
from torch.nn import Parameter
from functools import wraps
class WeightNorm(nn.Module):
append_g = '_g'
append_v = '_v'