def testTopKSum(self): d = 10 c = 10000 r = 20 h = d a = TopHCS(h, d, c, r, **self.csvecArgs) b = TopHCS(h, d, c, r, **self.csvecArgs) zerosHalf = torch.zeros(d//2, dtype=torch.float, device=self.device) vec = torch.cat((torch.randn(d//2, device=self.device), zerosHalf), 0) vec2 = torch.cat((zerosHalf, torch.randn(d//2, device=self.device)), 0) a.store(vec) b.store(vec2) result = TopHCS.topKSum([a, b], d) expected = vec + vec2 self.assertTrue(torch.equal(expected, result))
def testTopKSum2(self): d = 10 c = 10000 r = 20 h = d a = TopHCS(h, d, c, r, **self.csvecArgs) b = TopHCS(h, d, c, r, **self.csvecArgs) c = TopHCS(h, d, c, r, **self.csvecArgs) vec = torch.randn(d, device=self.device) vec2 = torch.randn(d, device=self.device) vec3 = torch.randn(d, device=self.device) a.store(vec) b.store(vec2) c.store(vec3) result = TopHCS.topKSum([a, b, c], d) expected = vec + vec2 + vec3 self.assertTrue(torch.equal(expected, result))
w_2 = TopHCS(d=d, c=c, r=r, h=h_curr, numBlocks=numBlocks, device=device) w_2.store(vecs[2]) print(w_0.topH) print("") w_3 = TopHCS(d=d, c=c, r=r, h=h_curr, numBlocks=numBlocks, device=device) w_3.store(vecs[3]) print(w_0.topH) print("") workers = [w_0, w_1, w_2, w_3] result = TopHCS.topKSum(workers, k) #topHCS_accuracy = (expected == result).nonzero().numel() / k #print("num of non 0 elements in topH result : ", result[expectedIndices].nonzero().numel()) topHCS_accuracy[ h_i, k_i] = (expected[expectedIndices] * result[expectedIndices]).nonzero().numel() / k print('result for h =', h_curr, " :", result) print('topHCS accuracy :', '\n', topHCS_accuracy)
for h_i, hVal in enumerate(hVals): h = int(k * hVal) workers = [] #reusing for vec in vecs: w = TopHCS(h=h, d=d, c=c, r=r, numBlocks=numBlocks, device=device) w.store(vec) workers.append(w) assert (len(workers) == len(vecs)) recovered_2 = TopHCS.topKSum(workers, k) indexAcc_2[ h_i, k_i] = (expected[expectedIndices] * recovered_2[expectedIndices]).nonzero().numel() / k diff = recovered_2[recovered_2 != 0] - expected[recovered_2 != 0] L1_2[h_i, k_i] = torch.median(torch.abs(diff)) L2_2[h_i, k_i] = torch.median(diff**2) #indexAcc = (expected == recovered).nonzero().numel() / k # Use above only when k == d #print("\n", "k = %r; index accuracy = %r" % (k, indexAcc)) colors = ["#A30CE8", "#FF0000", "#E8710C", "#FFD20D"]