Skip to content

Instantly share code, notes, and snippets.

import torch
import numpy as np
import time
def benchmark():
SIZE_GB = 1
num_iters = 100
def compute():
a = torch.ones(int(SIZE_GB*1000*250000)).cuda()
@yaroslavvb
yaroslavvb / resnet_test.py
Created October 28, 2017 22:48
Resnet minimize in Eager mode
import tensorflow as tf
from tensorflow.contrib.eager.python import tfe
tfe.enable_eager_execution()
context = tf.device('/gpu:0')
context.__enter__()
# download resnet_model
import sys, os, urllib.request
resnet_model_url="https://raw.githubusercontent.com/tensorflow/models/master/official/resnet/resnet_model.py"
response = urllib.request.urlopen(resnet_model_url)
@yaroslavvb
yaroslavvb / train2.py
Last active April 24, 2019 02:29
example of using TMUX
import argparse
import os
import sys
from six.moves import shlex_quote
parser = argparse.ArgumentParser(description="Run commands")
parser.add_argument('-w', '--num-workers', default=1, type=int,
help="Number of workers")
parser.add_argument('-r', '--remotes', default=None,
help='The address of pre-existing VNC servers and '
@yaroslavvb
yaroslavvb / kfac_nano_eager_test.py
Last active May 31, 2018 22:11
Small example of KFAC in Eager mode
import numpy as np
import tensorflow as tf
import scipy
from tensorflow.contrib.eager.python import tfe
tfe.enable_eager_execution()
# manual numpy example
# X = np.array(([[0., 1], [2, 3]]))
# W0 = X
# W1 = np.array(([[0., 1], [2, 3]]))/10
@yaroslavvb
yaroslavvb / kfac_small_pytorch_test.py
Created October 19, 2017 15:23
toy example of KFAC in pytorch
# Times: min: 440.60, median: 452.39, mean: 453.87
import util as u
u.check_mkl()
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import tensorflow as tf
from tensorflow.python.ops import control_flow_ops
from tensorflow.contrib import graph_editor as ge
def make_conditional_initializer(v):
"""Makes initializer of variable var lazy, returns new conditional init
op."""
cond = tf.is_variable_initialized(v)
@yaroslavvb
yaroslavvb / copy_initializer
Created March 7, 2017 20:02
utility to copy initializer for one variable to another
def copy_initializer(from_var, to_var):
from tensorflow.contrib import graph_editor as ge
assert ge.reroute_a2b_ts(from_var.initial_value, to_var.initial_value), "No copy took place"
@yaroslavvb
yaroslavvb / github-recipes.txt
Last active April 6, 2017 13:44
TensorFlow github recipes
1. Making Pull Request
First, click "Fork" on github tensorflow page
Then in Terminal:
export user=yaroslavvb
export branch_name
git clone https://github.com/$user/tensorflow.git
cd tensorflow
@yaroslavvb
yaroslavvb / session-run-benchmark.py
Last active February 6, 2018 09:22
Example of benchmarking session.run call
# Example of profiling session.run overhead
# for python profiling
# python -m cProfile -o session-run-benchmark-feed.prof session-run-benchmark.py feed_dict
# python -m cProfile -o session-run-benchmark-variable.prof session-run-benchmark.py variable
# pip install snakeviz
# snakeviz session-run-benchmark-feed.prof
# snakeviz session-run-benchmark.prof
#
#
# Feed_dict: 147 usec, no feed dict, 71 usec
# GPU picking
# http://stackoverflow.com/a/41638727/419116
# Nvidia-smi GPU memory parsing.
# must set
# CUDA_DEVICE_ORDER=PCI_BUS_ID
# see https://github.com/tensorflow/tensorflow/issues/152#issuecomment-273663277
# Tested on nvidia-smi 370.23
def run_command(cmd):