def testUnsketch(self): # make sure heavy hitter recovery works correctly # use a gigantic sketch so there's no chance of collision d = 5 c = 10000 r = 20 a = CSVec(d, c, r, **self.csvecArgs) vec = torch.rand(d).to(self.device) a += vec with self.subTest(method="topk"): recovered = a.unSketch(k=d) self.assertTrue(torch.allclose(recovered, vec)) with self.subTest(method="epsilon"): thr = vec.abs().min() * 0.9 recovered = a.unSketch(epsilon=thr / vec.norm()) self.assertTrue(torch.allclose(recovered, vec))
def testSketchSum(self): d = 5 c = 10000 r = 20 summed = CSVec(d, c, r, **self.csvecArgs) for i in range(d): vec = torch.zeros(d).to(self.device) vec[i] = 1 sketch = CSVec(d, c, r, **self.csvecArgs) sketch += vec summed += sketch recovered = summed.unSketch(k=d) trueSum = torch.ones(d).to(self.device) self.assertTrue(torch.allclose(recovered, trueSum))
y = comm.recv(source=n, tag=11) table_rx = np.array(table_rx) + np.array(y) # Ramp up alpha if epoch <= 5: alpha_t = alpha + epoch * (0.3 - alpha) / 5 elif epoch > 5 and epoch <= 10: alpha_t = 0.3 - (epoch - 5) * (0.3 - alpha) / 5 else: alpha_t = alpha table_rx = alpha * (1.0 / N) * table_rx S.table = torch.tensor(table_rx) S_e = S + S_e unsketched = S_e.unSketch(k=k) np_unsketched = unsketched.numpy() S.zero() S.accumulateVec(unsketched) S_e.table = S_e.table - S.table # Rehape unsketched print(unsketched.shape) shapes = [ model.trainable_weights[i].shape for i in range(len(model.trainable_weights)) ] grad_tx = []
topHCSAcc = np.zeros((len(hVals), len(kVals))) csvecL2 = np.zeros(len(kVals)) topHCSL2 = np.zeros((len(hVals), len(kVals))) for k_i, k in enumerate(kVals): d, c, r, numBlocks = len(summed), cols, 15, 30 #ipdb.set_trace() expected = torch.zeros(len(summed), device=device) expected[expectedIndices[-k:].to(device)] = summed[ expectedIndices[-k:].to(device)] 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]