def test_equal(self): """Test equality comparison""" tensor1 = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") tensor2 = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") self.assertTrue(tensor1 == tensor2)
def test_fromYAML(self): """Test construction from a YAML file""" tensor_ref = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") tensor = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") self.assertTrue(tensor == tensor_ref)
def test_dump(self): """Test dumping a tensor""" tensor = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") tensor.dump("/tmp/test_tensor-1.yaml") tensor_tmp = Tensor.fromYAMLfile("/tmp/test_tensor-1.yaml") self.assertTrue(tensor == tensor_tmp)
def test_floordiv(self): """ Test /, the __floordiv__ operator """ a = Tensor.fromYAMLfile("./data/tensor_transform-b.yaml") a_verify = Tensor.fromYAMLfile("./data/tensor_transform-b-floordiv.yaml") a_out = a // 4 self.assertEqual(a_out, a_verify) self.assertEqual(a_out.getRankIds(), ["M.1", "M.0", "N"]) self.assertEqual(a_out.getShape(), [17, 20, 10])
def test_swapRanks_1(self): """ Test swapRanks - depth=1 """ a = Tensor.fromYAMLfile("./data/tensor_transform-a.yaml") a_verify = Tensor.fromYAMLfile("./data/tensor_transform-a-swapRanks_1.yaml") a_out = a.swapRanks(depth=1) self.assertEqual(a_out, a_verify) self.assertEqual(a_out.getRankIds(), ["M", "K", "N"]) self.assertEqual(a_out.getShape(), [41, 10, 42])
def test_splitUniform_2(self): """ Test splitUniform - depth=2 """ a = Tensor.fromYAMLfile("./data/tensor_transform-a.yaml") a_verify = Tensor.fromYAMLfile("./data/tensor_transform-a-splitUniform_2.yaml") tests = { "by-depth": {"depth": 2}, "by-name": {"rankid": "K"}} for test, kwargs in tests.items(): with self.subTest(test=test): a_out = a.splitUniform(4, **kwargs) self.assertEqual(a_out, a_verify) self.assertEqual(a_out.getRankIds(), ["M", "N", "K.1", "K.0"]) self.assertEqual(a_out.getShape(), [41, 42, 9, 10])
def test_shape_fromFiber_authoritative(self): """Test shape of a tensor from a fiber with authoritative shape""" y1 = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") f1 = y1.getRoot() t1 = Tensor.fromFiber(["M", "K"], f1, [100, 200]) with self.subTest(test="All ranks"): self.assertEqual(t1.getRankIds(), ["M", "K"]) self.assertEqual(t1.getShape(), [100, 200]) with self.subTest(test="All ranks specified"): self.assertEqual(t1.getShape(["M", "K"]), [100, 200]) with self.subTest(test="Just rank 'M'"): self.assertEqual(t1.getShape(["M"]), [100]) with self.subTest(test="Just rank 'K'"): self.assertEqual(t1.getShape(["K"]), [200]) with self.subTest(test="Just rank 'M'"): self.assertEqual(t1.getShape("M"), 100) with self.subTest(test="Just rank 'K'"): self.assertEqual(t1.getShape("K"), 200) with self.subTest(test="Check authoritative"): self.assertEqual(t1.getShape(authoritative=True), [100, 200]) self.assertEqual(t1.getShape(["M", "K"], authoritative=True), [100, 200]) self.assertEqual(t1.getShape(["M"], authoritative=True), [100]) self.assertEqual(t1.getShape(["K"], authoritative=True), [200]) self.assertEqual(t1.getShape("M", authoritative=True), 100) self.assertEqual(t1.getShape("K", authoritative=True), 200)
def test_shape_fromFiber(self): """Test shape of a tensor from a fiber without authoritative shape""" y1 = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") f1 = y1.getRoot() t1 = Tensor.fromFiber(["M", "K"], f1) with self.subTest(test="All ranks"): self.assertEqual(t1.getRankIds(), ["M", "K"]) self.assertEqual(t1.getShape(), [7, 4]) with self.subTest(test="All ranks specified"): self.assertEqual(t1.getShape(["M", "K"]), [7, 4]) with self.subTest(test="Just rank 'M' as list"): self.assertEqual(t1.getShape(["M"]), [7]) with self.subTest(test="Just rank 'K' as list"): self.assertEqual(t1.getShape(["K"]), [4]) with self.subTest(test="Just rank 'M'"): self.assertEqual(t1.getShape("M"), 7) with self.subTest(test="Just rank 'K'"): self.assertEqual(t1.getShape("K"), 4) with self.subTest(test="Check authoritative"): self.assertIsNone(t1.getShape(authoritative=True)) self.assertIsNone(t1.getShape(["M", "K"], authoritative=True)) self.assertIsNone(t1.getShape(["M"], authoritative=True)) self.assertIsNone(t1.getShape(["K"], authoritative=True)) self.assertIsNone(t1.getShape("M", authoritative=True)) self.assertIsNone(t1.getShape("K", authoritative=True))
def get_A_HFA(a_file): # read in inputs # jhu_len = 5157 shape = 500 # TODO: take this as input # generate input frontier and tile it A_data = [0] * shape # read in frontier count = 1 A_HFA = None if not a_file.endswith('.yaml'): with open(a_file, 'r') as f: for line in f: elt = int(line) A_data[elt] = count count += 1 A_untiled = Tensor.fromUncompressed(["S"], A_data, name="A") A_HFA = A_untiled.splitUniform(32, relativeCoords=True) # split S print("A untiled shape {}, tiled shape {}".format( A_untiled.getShape(), A_HFA.getShape())) else: # already in pretiled yaml A_HFA = Tensor.fromYAMLfile(a_file) A_HFA.setName("A") return A_HFA
def test_fromUncompressed_2D_wo_ids(self): """Test construction of a tensor from nested lists without ids""" tensor_in = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") root = tensor_in.getRoot() tensor_ref = Tensor.fromFiber(["R1", "R0"], root) # Manual copy of test_tensor-1.yaml # 0 1 2 3 # t = [ [0, 0, 0, 0], # 0 [100, 101, 102, 0], # 1 [0, 201, 0, 203], # 2 [0, 0, 0, 0], # 3 [400, 0, 402, 0], # 4 [0, 0, 0, 0], # 5 [0, 601, 0, 603] ] # 6 tensor = Tensor.fromUncompressed(["R1", "R0"], t) self.assertEqual(tensor, tensor_ref)
def test_splitNonUniform_1(self): """ Test splitNonUniform - depth=1 """ a = Tensor.fromYAMLfile("./data/tensor_transform-a.yaml") a_verify = Tensor.fromYAMLfile("./data/tensor_transform-a-splitNonUniform_1.yaml") tests = { "by-depth": {"depth": 1}, "by-name": {"rankid": "N"}} for test, kwargs in tests.items(): with self.subTest(test=test): a_out = a.splitNonUniform([0, 15, 25], **kwargs) self.assertEqual(a_out, a_verify) self.assertEqual(a_out.getRankIds(), ["M", "N.1", "N.0", "K"]) self.assertEqual(a_out.getShape(), [41, 26, 42, 10])
def test_shape_new(self): """Test shape of a tensor from a file""" t1 = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") self.assertEqual(t1.getRankIds(), ["M", "K"]) self.assertEqual(t1.getShape(), [7, 4])
def test_flattenRanks_f12(self): """ Test flattenRanks - f12 """ t0 = Tensor.fromYAMLfile("./data/tensor_3d-0.yaml") f12 = t0.flattenRanks(depth=1, levels=1) u12 = f12.unflattenRanks(depth=1, levels=1) self.assertEqual(u12, t0)
def get_B_HFA(b_file): print("reading tiled mtx from yaml") t0 = time.time() B_HFA = Tensor.fromYAMLfile(b_file) t1 = time.time() - t0 print("read B from yaml in {} s".format(t1)) # B_HFA.print() return B_HFA
def test_mutable_after_flatten(self): t = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") t2 = t.flattenRanks() self.assertFalse(t2.isMutable()) t3 = Tensor(rank_ids=["X", "Y", "Z"]) t4 = t3.flattenRanks() self.assertTrue(t4.isMutable())
def test_values(self): """Test counting values in a tensor""" tensor = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") count = tensor.countValues() self.assertEqual(count, 9)
def test_mutable_after_split(self): t = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") t2 = t.splitUniform(10) self.assertFalse(t2.isMutable()) t3 = Tensor(rank_ids=["X", "Y", "Z"]) t4 = t3.splitUniform(10) self.assertTrue(t4.isMutable())
def test_flattenRanks_1(self): """ Test flattenRanks - depth=1 """ a = Tensor.fromYAMLfile("./data/tensor_transform-a.yaml") a_out = a.flattenRanks(depth=1) a_again = a_out.unflattenRanks(depth=1) self.assertEqual(a_again, a) self.assertEqual(a_out.getRankIds(), ["M", ["N", "K"]])
def test_flattenRanks_f01(self): """ Test flattenRanks - f01 """ t0 = Tensor.fromYAMLfile("./data/tensor_3d-0.yaml") f01 = t0.flattenRanks(depth=0, levels=1) u01 = f01.unflattenRanks(depth=0, levels=1) self.assertEqual(u01, t0)
def test_dot(self): "Test dot product" a = Tensor.fromYAMLfile("./data/tensor_sum_a.yaml") b = Tensor.fromYAMLfile("./data/tensor_sum_b.yaml") z = Tensor(rank_ids=["M"]) a_m = a.getRoot() b_m = b.getRoot() z_m = z.getRoot() for m_coord, (z_ref, (a_k, b_k)) in z_m << (a_m & b_m): for k_coord, (a_val, b_val) in a_k & b_k: z_ref += a_val * b_val z_correct = Tensor.fromYAMLfile("./data/tensor_dot_z.yaml") self.assertEqual(z, z_correct)
def test_fromYAMLfile_0D(self): """Test construction of 0-D tensor from a YAML file""" tensor_ref = Tensor(rank_ids=[]) root = tensor_ref.getRoot() root += 2 tensor = Tensor.fromYAMLfile("./data/tensor_0d.yaml") self.assertTrue(tensor == tensor_ref)
def test_new(self): """Test construction of a tensor from a file""" t = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") # Filename no longer becomes tensor name # self.assertEqual(t.getName(), "test_tensor-1") self.assertEqual(t.getRankIds(), ["M", "K"]) self.assertEqual(t.getShape(), [7, 4])
def test_flattenRanks_f02(self): """ Test flattenRanks - f02 """ t0 = Tensor.fromYAMLfile("./data/tensor_3d-0.yaml") t1 = Tensor.fromYAMLfile("./data/tensor_3d-1.yaml") t2 = Tensor.fromFiber(["A", "B", "C", "D"], Fiber([1, 4], [t0.getRoot(), t1.getRoot()]), name="t2") f13 = t2.flattenRanks(depth=1, levels=2) u13 = f13.unflattenRanks(depth=1, levels=2) self.assertEqual(u13, t2) f04 = t2.flattenRanks(depth=0, levels=3) u04 = f04.unflattenRanks(depth=0, levels=3) self.assertEqual(u04, t2)
def test_fromFiber(self): """Test construction of a tensor from a fiber""" tensor_ref = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") root = tensor_ref.getRoot() tensor = Tensor.fromFiber(["M", "K"], root) self.assertEqual(tensor, tensor_ref)
def test_setRoot(self): """Test adding a new root""" tensor_ref = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") root = tensor_ref.getRoot() tensor = Tensor(rank_ids=["M", "K"]) tensor.setRoot(root) self.assertEqual(tensor, tensor_ref)
def test_fromFiber_wo_ids(self): """Test construction of a tensor from a fiber without rank ids""" tensor_in = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") root = tensor_in.getRoot() tensor_ref = Tensor.fromFiber(["R1", "R0"], root) tensor = Tensor.fromFiber(fiber=root) self.assertEqual(tensor, tensor_ref)
def test_fomYAMLfile_3D(self): """Test construction of 0-D tensor from a YAML file""" t = Tensor.fromYAMLfile("./data/tensor_3d-0.yaml") # TBD: Check that data is good rankids_ref = ["M", "N", "K"] shape_ref = [21, 51, 11] self.assertEqual(t.getRankIds(), rankids_ref) self.assertEqual(t.getShape(), shape_ref)
def test_getPayloadRef_2d(self): """Test getPayloadRef of a 2-D tensor""" t = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") with self.subTest(test="Existing element"): p23_ref = 203 p23 = t.getPayloadRef(2, 3) self.assertEqual(p23_ref, p23) # Make sure change is seen p23_new_ref = 310 p23 <<= p23_new_ref p23_new = t.getPayload(2, 3) self.assertEqual(p23_new_ref, p23_new) with self.subTest(test="Non-existing element"): p31_ref = 0 p31 = t.getPayloadRef(3, 1) self.assertEqual(p31_ref, p31) # Make sure change is seen p31_new_ref = 100 p31 <<= p31_new_ref p31_new = t.getPayload(3, 1) self.assertEqual(p31_new_ref, p31_new) with self.subTest(test="Element of non-existing fiber"): p51_ref = 0 p51 = t.getPayloadRef(5, 1) self.assertEqual(p51_ref, p51) # Make sure change is NOT seen p51_new_ref = 100 p51 <<= p51_new_ref p51_new = t.getPayload(5, 1) self.assertEqual(p51_new_ref, p51_new) with self.subTest(test="Existing fiber"): p4_ref = Fiber([0, 2], [400, 402]) p4 = t.getPayloadRef(4) self.assertEqual(p4_ref, p4)
def test_0D(self): "Test sum to rank 0 tensor" a = Tensor.fromYAMLfile("./data/conv-activations-a.yaml") z = Tensor(rank_ids=[]) a_m = a.getRoot() z_ref = z.getRoot() for m_coord, (a_val) in a_m: z_ref += a_val self.assertEqual(z_ref, 12)
def test_conv1d_ws(self): """Convolution 1d ws""" w = Tensor.fromYAMLfile("./data/conv-weights-a.yaml") i = Tensor.fromYAMLfile("./data/conv-activations-a.yaml") o = Tensor(rank_ids=["Q"]) w_r = w.getRoot() i_h = i.getRoot() o_q = o.getRoot() W = w_r.maxCoord() + 1 I = i_h.maxCoord() + 1 Q = I - W + 1 for r, (w_val) in w_r: for q, (o_q_ref, i_val) in o_q << i_h.project(lambda h: h - r, (0, Q)): o_q_ref += w_val * i_val o_ref = Tensor.fromYAMLfile("./data/conv-output-a.yaml") self.assertEqual(o, o_ref)