Skip to content

Instantly share code, notes, and snippets.

View BarclayII's full-sized avatar

Quan (Andy) Gan BarclayII

  • AWS Shanghai
  • Shanghai
View GitHub Profile
@BarclayII
BarclayII / bpm.py
Last active May 14, 2021 08:27
O(n^3) minimum cost bipartite matching algorithm
# I found all over the place for a O(n^3) minimum cost bipartite matching
# or equivalently maximum weight bipartite matching algorithm but I
# cannot find a clean but precise explanation; the Wikipedia ones,
# Stack Overflow, and course materials are either too high-level
# or only O(n^4).
# So I decided to read the original paper of Edmunds and Karp. The
# idea is surprisingly simple: just always find a shortest path (that
# has minimum cost) to augment and you will be good. In fact they
# talked about minimum cost maximum flow problem, but minimum weight
# bipartite matching is just a special case of that.
@BarclayII
BarclayII / DNC.py
Last active December 1, 2020 18:00
Differentiable Neural Computer
#!/usr/bin/env python
# coding: utf-8
import torch
import torch.nn as nn
import torch.nn.functional as F
import tqdm
def reverse_permute(x):
return torch.zeros_like(x).scatter_(
@BarclayII
BarclayII / ppr.py
Last active May 14, 2019 03:50
Compute significant PPR for all nodes with Reverse Push
import scipy.sparse as ssp
import numpy as np
from sklearn.preprocessing import normalize
from numba.typed import Dict
from numba import jit, types, njit, prange
@njit(parallel=True, nogil=True)
def reverse_push_bipartite(A_indptr, A_indices, A_data, nL, nR, r_max, alpha):
'''
@BarclayII
BarclayII / prof.py
Last active March 8, 2019 03:10
Batched GEMM profiling for transformers in PyTorch
# coding: utf-8
import torch
import time
import pandas as pd
import tqdm
B, L, N, H, W = 64, 50, 10, 256, 3
print('warming up')
for _ in tqdm.trange(10):
@BarclayII
BarclayII / movielens.py
Last active December 9, 2023 05:47
PinSage example implementation
import pandas as pd
import dgl
import os
import torch
class MovieLens(object):
def __init__(self, directory):
'''
directory: path to movielens directory which should have the three
files:
@BarclayII
BarclayII / rbtree.py
Last active September 30, 2022 06:20
RBTree
class Node(object):
def __init__(self, key, red):
self.key = key
self.left = self.right = self.parent = None
self.red = red
@property
def grandparent(self):
if self.parent is None:
@BarclayII
BarclayII / spmvtest.py
Last active November 5, 2018 21:36
PyTorch gather-scatter/SPMV benchmarks
import torch
import time
N = 10000
D = 50
E = 500000
T = 10
t_gather = 0
t_scatter = 0
@BarclayII
BarclayII / hashmaps.cpp
Last active March 24, 2024 10:51
C++ unordered_map, ordered_map, and custom hashmap comparison (compile with -O2)
#include <unordered_map>
#include <map>
#include <vector>
#include <iostream>
#include <utility>
#include <cstdint>
#include <ctime>
#include <cstdlib>
using std::uint64_t;
@BarclayII
BarclayII / mips.py
Created June 21, 2018 08:44
Maximum Inner Product Search with Asymmetric LSH + Random Projection
# References:
# https://arxiv.org/pdf/1405.5869.pdf
# https://arxiv.org/abs/1507.05910
import numpy as np
from scipy.spatial.distance import cosine
# Rescaling and appending new components to "normalize" data vectors
X = np.random.randn(10000, 100)
Xn = np.sqrt((X ** 2).sum(1))
@BarclayII
BarclayII / reinforce.py
Created November 1, 2017 03:59
PyTorch `reinforce()` function sucks so I keep the alternative solution here
import torch as T
import numpy as np
x = T.autograd.Variable(T.randn(5, 8), requires_grad=True)
p = T.nn.functional.softmax(x)
y = p.multinomial()
y.reinforce(T.ones(y.size()))
y.backward()
d = x.grad.data.clone().numpy()
x.grad.data.zero_()
logp = T.nn.functional.log_softmax(x)