Skip to content

Instantly share code, notes, and snippets.

@JonathanRaiman
JonathanRaiman / save_submission.py
Last active October 6, 2021 02:53
Code to submit predictions to cardinality estimation leaderboard @ ML for Systems NeurIPS 2021 workshop
import numpy as np
def save_submission(predictions, filename):
"Take model output & save for cardinality estimation benchmark upload."""
np.save(filename, np.array([value for item in predictions for key, value in sorted(item.items())]))
# preds = rf.test(testqs) # run your prediction code on the test data
save_submission(preds, "mysubmission.npy")
# Then Upload "mysubmission.npy" to the leaderboard https://mlforsystems.wl.r.appspot.com :)
@JonathanRaiman
JonathanRaiman / google_tsp_solver.py
Created February 6, 2019 19:46
approximate tsp solver
from ortools.constraint_solver import pywrapcp
import numpy as np
def _create_distance_callback(dist_matrix):
# Create a callback to calculate distances between cities.
def distance_callback(from_node, to_node):
return int(dist_matrix[from_node][to_node])
return distance_callback
@JonathanRaiman
JonathanRaiman / graph_optimization.py
Created July 29, 2018 22:00
Memoized Graph Optimization
from contextlib import contextmanager
import time
CURRENT_SCOPE = []
@contextmanager
def printing_scope(message):
CURRENT_SCOPE.append(message)
yield
last = CURRENT_SCOPE.pop()
@JonathanRaiman
JonathanRaiman / clear_subl.sh
Created March 30, 2018 21:19
Clears out window cache of sublime which can cause issues on boot up (MacOSX)
function clear_subl {
python3 -c "path = '~/Library/Application Support/Sublime Text 3/Local/Auto Save Session.sublime_session'; import os, json; data = json.load(open(os.path.expanduser(path), 'rt')); data['windows'] = []; json.dump(data, open(os.path.expanduser(path), 'wt'))"
}
@JonathanRaiman
JonathanRaiman / access_pattern.py
Last active April 29, 2018 07:27
Access Pattern inference
"""
Access Pattern Search
---------------------
Code for simulating the effect of searching for the right access pattern in
a CUDA Kernel computation directed acyclic graph.
The key idea is to have every node in the computation graph return an object
representing "for loops" that can be optionally parallelized using blocks
or threads (followed by syncs).
@JonathanRaiman
JonathanRaiman / human.cpp
Last active November 27, 2018 02:25
Code auto generated by Dali
auto a = op::uniform(-20.0, 20.0, {2, 5}).astype(dtype);
a.eval();
auto exped = op::exp(a - op::max(a, {-1}, true));
auto fused_softmax = exped / op::sum(exped, {-1}, true);
@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)
import random
from deap import algorithms, base, creator, tools
import numpy as np
domains = 100
num_entities = 10000
entity_num_domains = 5
num_mentions = 200
classifications = np.random.binomial(
1, np.ones(domains) * entity_num_domains / domains, size=(num_entities, domains)
@JonathanRaiman
JonathanRaiman / faux_cudnn.py
Last active March 22, 2019 11:49
Convert CUDNN LSTM to Dynamic RNN
"""
Little script demonstration how to run cudnn rnns
without cudnn using dynamic rnn with the same weights
(e.g. train on cudnn, use with dynamic rnn on cpu).
Note: this will run slower than cudnn on a gpu (see below).
Tested on Titan X Pascal:
With cudnn 3.5s vs. with dynamic_rnn 8s to run through 79 batches
with batch size 128.
Network: input size: 127, 2 layer bidirectional LSTM with num_units 200.
@JonathanRaiman
JonathanRaiman / viterbi.py
Last active February 4, 2020 11:30
tensorflow_viterbi_decode
import tensorflow as tf
def batch_gather_3d(values, indices):
return tf.gather(tf.reshape(values, [-1, tf.shape(values)[2]]),
tf.range(0, tf.shape(values)[0]) * tf.shape(values)[1] +
indices)
def batch_gather_2d(values, indices):