Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve memory management in clustering_qr.kmeans_plusplus #775

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
20 changes: 12 additions & 8 deletions kilosort/clustering_qr.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,18 @@ def cluster(Xd, iclust = None, kn = None, nskip = 20, n_neigh = 10, nclust = 200

m, ki, kj = Mstats(M, device=device)

#Xg = torch.from_numpy(Xd).to(dev)
Xg = Xd.to(device)
kn = torch.from_numpy(kn).to(device)

n_neigh = kn.shape[1]
NN, nfeat = Xg.shape
NN, nfeat = Xd.shape
nsub = (NN-1)//nskip + 1

rows_neigh = torch.arange(NN, device = device).unsqueeze(-1).tile((1,n_neigh))

tones2 = torch.ones((NN, n_neigh), device = device)

if iclust is None:
iclust_init = kmeans_plusplus(Xg, niter = nclust, seed = seed, device=device)
iclust_init = kmeans_plusplus(Xd, niter = nclust, seed = seed, device=device)
iclust = iclust_init.clone()
else:
iclust_init = iclust.clone()
Expand All @@ -162,9 +160,12 @@ def cluster(Xd, iclust = None, kn = None, nskip = 20, n_neigh = 10, nclust = 200
return iclust, isub, M, iclust_init


def kmeans_plusplus(Xg, niter = 200, seed = 1, device=torch.device('cuda')):
#Xg = torch.from_numpy(Xd).to(dev)
vtot = (Xg**2).sum(1)
def kmeans_plusplus(Xd, niter = 200, seed = 1, device=torch.device('cuda')):

Xg = Xd.to(device)

Xd_squared = (Xd ** 2)
vtot = Xd_squared.sum(1).to(device)

n1 = vtot.shape[0]
if n1 > 2**24:
Expand Down Expand Up @@ -199,7 +200,10 @@ def kmeans_plusplus(Xg, niter = 200, seed = 1, device=torch.device('cuda')):
isamp = torch.multinomial(v2, ntry)

Xc = Xg[isamp]
vexp = 2 * Xg @ Xc.T - (Xc**2).sum(1)
Xc_squared_sum = (Xc ** 2).sum(1)
vexp = Xg @ Xc.T
vexp.mul_(2)
vexp = vexp - Xc_squared_sum

dexp = vexp - vexp0.unsqueeze(1)
dexp = torch.relu(dexp)
Expand Down