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 setUp(self): self.input = {} self.input["a1_M"] = Tensor.fromUncompressed(["M"], [1, 0, 3, 0, 5, 0, 7]) self.input["a1_m"] = self.input["a1_M"].getRoot() self.input["b1_M"] = Tensor.fromUncompressed(["M"], [2, 0, 4, 5]) self.input["b1_m"] = self.input["b1_M"].getRoot() self.input['c1_M'] = Tensor.fromUncompressed(["M"], [1, 2, 3]) self.input["c1_m"] = self.input["c1_M"].getRoot() self.input["a2_MK"] = Tensor.fromUncompressed( ["M", "K"], [[1, 0, 3, 0, 5, 0, 7], [2, 2, 0, 3, 0, 0, 8], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [4, 0, 5, 0, 8, 0, 9]]) self.input["a2_m"] = self.input["a2_MK"].getRoot() self.input["b2_MK"] = Tensor.fromUncompressed( ["M", "K"], [[2, 0, 4, 5], [0, 0, 0, 0], [3, 4, 6, 0], [0, 0, 0, 0], [1, 2, 3, 4]]) self.input["b2_m"] = self.input["b2_MK"].getRoot()
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_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_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 test_unflattenRanks_empty(self): t = Tensor(rank_ids=["X", "Y", "Z"]) t2 = t.flattenRanks() t3 = t2.unflattenRanks() t3.setRankIds(["X", "Y", "Z"]) self.assertEqual(t, t3)
def test_swizzleRanks_empty(self): """ Test swizzleRanks() on an empty tensor """ Z_MNOP = Tensor(rank_ids=["M", "N", "O", "P"]) Z_PNMO = Z_MNOP.swizzleRanks(rank_ids=["P", "N", "M", "O"]) self.assertEqual(Z_MNOP.getRankIds(), ["M", "N", "O", "P"]) self.assertEqual(Z_PNMO.getRankIds(), ["P", "N", "M", "O"])
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_constructor_rank_0D(self): """Test construction of 0-D tensor""" t = Tensor(rank_ids=[]) p = t.getRoot() p += 1 self.assertEqual(p, 1)
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_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_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_constructor_empty(self): """Test construction of empty tensor""" ranks = ["M", "K"] t = Tensor(rank_ids=ranks) self.assertEqual(t.getRankIds(), ranks) self.assertEqual(t.getRoot().getRankIds(), ranks)
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_shape_0D(self): """Test shpe of 0-D tensor""" t = Tensor(rank_ids=[]) p = t.getRoot() p += 1 self.assertEqual(t.getRankIds(), []) self.assertEqual(t.getShape(), [])
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_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_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_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_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_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_copy(self): """Copy a tensor""" a = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") z = Tensor(rank_ids=["M", "K"]) a_m = a.getRoot() z_m = z.getRoot() for m, (z_k, a_k) in z_m << a_m: for k, (z_ref, a_val) in z_k << a_k: z_ref += a_val self.assertEqual(a, z)
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_fromUncompressed_1D(self): """Test construction of a tensor from nested lists""" tensor_ref = Tensor.fromYAMLfile("./data/test_tensor-1.yaml") # Manual copy of test_tensor-1.yaml # 0 1 2 3 # t = [100, 101, 0, 102] fiber = Fiber([0, 1, 3], [100, 101, 102]) tensor = Tensor.fromUncompressed(["M"], t) self.assertEqual(tensor.getRoot(), fiber)
def get_Z_HFA(B_shape): Z_data = [[0], [0]] Z_HFA = Tensor.fromUncompressed(["D1", "D0"], Z_data, shape=[B_shape[0], B_shape[2]], name="Z") return Z_HFA
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_rankid_2D(self): """Test setting rank ids of 2D tensor""" # 0 1 2 3 # l1 = [ [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 rank_ids = ["M", "K"] t1 = Tensor.fromUncompressed(rank_ids, l1) rank_ids2 = t1.getRankIds() self.assertEqual(rank_ids2, rank_ids) rank_ids_new = ["M2", "M1"] t1.setRankIds(rank_ids_new) rank_ids3 = t1.getRankIds() self.assertEqual(rank_ids3, rank_ids_new)
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_shape_fromUncompressed_1D(self): """Test shape of a tensor from 1D nested lists""" l1 = [100, 101, 0, 102] t1 = Tensor.fromUncompressed(["M"], l1) self.assertEqual(t1.getRankIds(), ["M"]) self.assertEqual(t1.getShape(), [4]) l2 = [100, 101, 0, 0] t2 = Tensor.fromUncompressed(["M"], l2) self.assertEqual(t2.getRankIds(), ["M"]) self.assertEqual(t2.getShape(), [4])