def testSameBuckets(self): d = 100 c = 20 r = 5 h = 0 a = CSVec(d, c, r, **self.csvecArgs) vec = torch.randn(d) a += vec b = TopHCS(h=h, d=d, c=c, r=r, **self.csvecArgs) b.store(vec) self.assertTrue(torch.allclose(a.table, b.csvec.table))
def testStoreVec3(self): # store randn tensor, recover all d = 100 c = 20 r = 5 h = d a = TopHCS(h, d, c, r, **self.csvecArgs) vec = torch.randn(d) a.store(vec) self.assertTrue(torch.allclose(a.topH, vec))
def testStoreVec2(self): # store vec [1, 100], recover all d = 100 c = 20 r = 5 h = d a = TopHCS(h, d, c, r, **self.csvecArgs) vec = torch.arange(1, d+1, dtype=torch.float, device=self.device) a.store(vec) self.assertTrue(torch.allclose(a.topH, vec))
def testStoreVec1(self): # store vec [1, 100], recover 1 d = 100 c = 20 r = 5 h = 1 a = TopHCS(h, d, c, r, **self.csvecArgs) vec = torch.arange(1, d+1, dtype=torch.float, device=self.device) a.store(vec) expected = torch.zeros(d, dtype=torch.float, device=self.device) expected[d-1] = d self.assertTrue(torch.allclose(a.topH, expected)) self.assertTrue(torch.allclose(a.bottomH, vec - expected))
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 testInit(self): d = 100 c = 20 r = 5 h = 36 a = TopHCS(h, d, c, r, **self.csvecArgs) zerosTable = torch.zeros(r, c).to(self.device) self.assertTrue(torch.allclose(a.csvec.table, zerosTable)) zerosH = torch.zeros(h).to(self.device) self.assertTrue(torch.allclose(a.topH, zerosH)) self.assertTrue(a.h == h)
csvec_accuracy[k_i] = ( expected[expectedIndices] * result[expectedIndices]).nonzero().numel() / k print("k = ", k) #print("num of non 0 elements in expected : ", expected[expectedIndices].nonzero().numel()) #print("num of non 0 elements in result : ", result[expectedIndices].nonzero().numel()) print('csvec accuracy :', csvec_accuracy) print('expected :', expected) print('result :', result) for h_i, h in enumerate(hVals): result = torch.zeros(len(summed), device=device) h_curr = int(h * k) w_0 = TopHCS(d=d, c=c, r=r, h=h_curr, numBlocks=numBlocks, device=device) w_0.store(vecs[0]) print(w_0.topH) print("") w_1 = TopHCS(d=d, c=c, r=r, h=h_curr, numBlocks=numBlocks, device=device) w_1.store(vecs[1]) print(w_0.topH)
workers[0] += w recovered_1 = workers[0].unSketch(k) indexAcc_1[k_i] = (expected[expectedIndices] * recovered_1[expectedIndices]).nonzero().numel() / k diff = recovered_1[recovered_1 != 0] - expected[recovered_1 != 0] L1_1[k_i] = torch.median(torch.abs(diff)) L2_1[k_i] = torch.median(diff**2) 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))
assert (summed.size() == vecs[0].size()) w_0 = CSVec(d=d, c=c, r=r, numBlocks=numBlocks, device=device) w_0 += vecs[0] print("worker added") result = w_0.unSketch(k) csvecAcc[k_i] = (expected[expectedIndices] * result[expectedIndices]).nonzero().numel() / k print("k = {}".format(k)) csvecL2[k_i] = (torch.sum((result - expected)**2))**0.5 for h_i, h in enumerate(hVals): result = torch.zeros(len(summed), device=device) w_0 = TopHCS(d=d, c=c, r=r, h=h, numBlocks=numBlocks, device=device) w_0.store(vecs[0]) print("worker added") workers = [w_0] result = TopHCS.topKSum(workers, k) topHCSAcc[ h_i, k_i] = (expected[expectedIndices] * result[expectedIndices]).nonzero().numel() / k print("h_curr = {}".format(h_curr)) topHCSL2[h_i, k_i] = (torch.sum((result - expected)**2))**0.5 print('topHCS accuracy :', '\n', topHCSAcc) print('CSVec accuracy :', '\n', csvecAcc) numColors = len(hVals) + 1
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))