Example #1
0
    def test_shape_empty(self):
        """Test shape of empty tensor"""

        t1 = Tensor(rank_ids=["M", "K"])

        self.assertEqual(t1.getRankIds(), ["M", "K"])
        self.assertEqual(t1.getShape(), [0, 0])

        t2 = Tensor(rank_ids=["M", "K"], shape=[10, 20])

        self.assertEqual(t2.getRankIds(), ["M", "K"])
        self.assertEqual(t2.getShape(), [10, 20])
    def test_constructor_shape(self):
        """Test construction of shape of tensor"""

        ranks = ["M", "K"]
        name = "ME"

        t1 = Tensor(rank_ids=ranks, name=name)

        self.assertEqual(t1.getName(), name)

        t2 = Tensor(rank_ids=ranks)
        t2.setName(name)

        self.assertEqual(t2.getName(), name)
Example #3
0
    def __init__(self, *tensors):
        """__init__"""

        #
        # Set up logging
        #
        self.logger = logging.getLogger('fibertree.graphics.spacetime_canvas')

        #
        # Structures to hold infomation about each tracked tensor
        #
        self.tensors = []
        self.spacetime = []
        self.highlights = []

        for tensor in tensors:
            #
            # Append each tensor being tracked, conditionally
            # unwraping it if it is a Payload object
            #
            self.tensors.append(Payload.get(tensor))

            #
            # Create a "spacetime" tensor to hold the spacetime
            # information for this tracked tensor
            #
            if isinstance(tensor, Tensor):
                assert tensor.getShape() != [], "No support for 0-D tensors"

                spacetime = Tensor(rank_ids=["T"] + tensor.getRankIds())
                spacetime.setName(tensor.getName())
                spacetime.setColor(tensor.getColor())
            else:
                assert tensor.getDepth() == 1, "Only 1-D fibers are supported"

                spacetime = Tensor(rank_ids=["T", "S"])

            #
            # Append the "spacetime" tensor to hold this tracked
            # tensor's spacetime information
            #
            self.spacetime.append(spacetime)
            #
            # Append an empty highlight object to hold the highlighting
            # information for this tracked tensor
            #
            self.highlights.append({})

        self.frame_num = 0
    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 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_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_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)
Example #10
0
    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_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_constructor_shape(self):
        """Test construction of shape of tensor"""

        ranks = ["M", "K"]
        shape = [4, 8]

        t = Tensor(rank_ids=ranks, shape=shape)

        self.assertEqual(t.getRankIds(), ranks)
        self.assertEqual(t.getRoot().getRankIds(), ranks)

        self.assertEqual(t.getShape(), shape)
        self.assertEqual(t.getRoot().getShape(), shape)
Example #14
0
    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)
Example #15
0
    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_getPayloadRef_0d(self):
        """Test getPayloadRef of a 0-D tensor"""

        p_ref = 10

        t = Tensor(rank_ids=[])
        r = t.getRoot()
        r <<= p_ref

        p = t.getPayloadRef()
        self.assertEqual(p_ref, p)

        p = t.getPayloadRef(0)
        self.assertEqual(p_ref, p)

        p = t.getPayloadRef(1)
        self.assertEqual(p_ref, p)
    def test_fromUncompressed_20(self):
        """Test construction of a tensor a scalar"""

        t_ref = Tensor(rank_ids=[])
        p = t_ref.getRoot()
        p += 2

        t0 = Tensor.fromUncompressed([], 2)

        self.assertEqual(t0, t_ref)

        t1 = Tensor.fromUncompressed(rank_ids=[], root=2)

        self.assertEqual(t1, t_ref)

        t2 = Tensor.fromUncompressed(root=2)

        self.assertEqual(t2, t_ref)
Example #18
0
    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_print_0D(self):
        """Test printing a 0-D tensor"""

        a = Tensor(rank_ids=[])
        p = a.getRoot()
        p += 2

        a_s_ref = "<2>"

        a_s = f"{a}"

        self.assertEqual(a_s, a_s_ref)

        a_r_ref = "T()/[Payload(2)]"

        a_r = f"{a!r}"

        self.assertEqual(a_r, a_r_ref)
    def test_default_nonscalar(self):
        """Test set/get of nonzero default"""

        t = Tensor(rank_ids=["X", "Y", "Z"])

        v = (10, 10)
        e = Fiber([], [])

        t.setDefault(v)
        self.assertEqual(t.getDefault(), v)

        t_root = t.getRoot()

        x = t_root.getPayload(1)
        self.assertEqual(x, e)

        y = t_root.getPayload(1, 2)
        self.assertEqual(y, e)

        z = t_root.getPayload(1, 2, 3)
        self.assertEqual(z, v)
    def test_default(self):
        """Test of default default"""

        t = Tensor(rank_ids=["X", "Y", "Z"])

        e = Fiber([], [])

        self.assertEqual(t.getDefault(), 0)

        t_root = t.getRoot()

        x = t_root.getPayload(1)
        self.assertEqual(x, e)
        self.assertEqual(x.getDefault(), Fiber)

        y = t_root.getPayload(1, 2)
        self.assertEqual(y, e)
        self.assertEqual(y.getDefault(), 0)

        z = t_root.getPayload(1, 2, 3)
        self.assertEqual(z, 0)
Example #22
0
    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)
Example #23
0
    def test_conv1d_os(self):
        """Convolution 1d os"""

        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

        output_shape = Fiber(coords=range(Q), initial=1)

        for q, (o_q_ref, _) in o_q << output_shape:
            for r, (w_val, i_val) in w_r.project(lambda r: q + r) & i_h:
                o_q_ref += w_val * i_val

        o_ref = Tensor.fromYAMLfile("./data/conv-output-os-a.yaml")

        self.assertEqual(o, o_ref)
Example #24
0
#
# C-stationary matrix-vector multiply
# i.e., output-stationary or inner-product style
# i.e., or A-stationary row major

print("--------------------------------------")
print("      C-stationary spMspV")
print("--------------------------------------")
print("")

data_dir = "../../data"

a = Tensor.fromYAMLfile(os.path.join(data_dir, "spMspV-a.yaml"))
b = Tensor.fromYAMLfile(os.path.join(data_dir, "spMspV-b.yaml"))

z = Tensor(rank_ids=["M"])

a.print("A Tensor")
b.print("B Tensor")
z.print("Z Tensor")

a_m = a.getRoot()
b_k = b.getRoot()
z_m = z.getRoot()

a_m.print("A Tensor - Rank M")
b_k.print("B Tensor - Rank K")
z_m.print("Z Tensor - Rank M")

for m_coord, (z_ref, a_k) in (z_m << a_m):
    for k_coord, (a_val, b_val) in (a_k & b_k):
Example #25
0
print("")

data_dir = "../../data"

#a = Tensor.fromYAMLfile(os.path.join(data_dir, "graph-a.yaml"))

# Adjacency matrix
a = Tensor.fromUncompressed(
    ["S", "D"], [[0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 1, 0],
                 [0, 0, 0, 0, 1, 1], [1, 0, 0, 0, 0, 1], [1, 1, 0, 0, 0, 0]])

# Fringe (current and next)
f0 = Tensor.fromUncompressed(["D"], [1, 0, 0, 0, 0, 0])

# Distance
d = Tensor(rank_ids=["S"])

# Get root fibers
a_s = a.getRoot()
f0_d = f0.getRoot()
d_d = d.getRoot()

print("BFS")

level = 1

while (f0_d.countValues() > 0):
    f0_d.print("\nFringe")

    f1 = Tensor(rank_ids=["D"])
    f1_d = f1.getRoot()
Example #26
0
import os

from fibertree import Tensor

print("----------------------------------------")
print("    Convolution 1-D Input Stationary")
print("----------------------------------------")
print("")

data_dir = "../../data"

w = Tensor.fromYAMLfile(os.path.join(data_dir, "conv-weights-a.yaml"))
i = Tensor.fromYAMLfile(os.path.join(data_dir, "conv-activations-a.yaml"))
o = Tensor(rank_ids=["Q"])

w.print("W Tensor")
i.print("I Tensor")
o.print("O Tensor")

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

w_r.print("W Tensor - R rank - size=%s" % W)
i_h.print("I Tensor - H rank - size=%s" % I)
o_q.print("O Tensor - Q rank - size=%s" % I)
import os

from fibertree import Tensor

print("--------------------------------------")
print("         Matrix copy")
print("--------------------------------------")
print("")

data_dir = "../../data"

a = Tensor.fromYAMLfile(os.path.join(data_dir, "matrix-a.yaml"))
z = Tensor(rank_ids=["M", "K"])

a.print("A Tensor")
z.print("Z Tensor")

a_m = a.getRoot()
z_m = z.getRoot()

a_m.print("A Tensor - M rank")
z_m.print("Z Tensor - M rank")

for m, (z_k, a_k) in z_m << a_m:
    print(f"Processing: Coord: {m}")
    print(f"   z_k: {z_k}")
    print(f"   a_k: {a_k}")

    for k, (z_ref, a_val) in z_k << a_k:
        z_ref += a_val
    def __init__(self,
                 *tensors,
                 animation='movie',
                 style='tree',
                 enable_wait=False):
        """__init__

        """
        #
        # Set up logging
        #
        self.logger = logging.getLogger('fibertree.graphics.tensor_canvas')

        #
        # Places to collect information about the frames
        #
        num_tensors = len(tensors)

        self.num_tensors = num_tensors
        self.orig_tensors = []
        self.shadow_tensors = []

        self.using_spacetimestamp = None

        self.update_times = [] if enable_wait else None
        self.waitname_map = {}

        for t in tensors:
            #
            # Build list of orignal tensors,
            # but convert Fibers into a Tensor with the Fiber as its root
            #
            if isinstance(t, Fiber):
                # TBD: We do not really know if the fiber is mutable...
                t = Tensor.fromFiber(fiber=t)

            self.orig_tensors.append(t)
            #
            # Create a tensor to hold a shadow tensor that tracks
            # updates to the tracked tensors at the right time
            #
            if t.isMutable():
                self.shadow_tensors.append(copy.deepcopy(t))
            else:
                self.shadow_tensors.append(t)

            #
            # Create a tensor to hold last update time
            # of each element of a tensor
            #
            if enable_wait:
                self.update_times.append(Tensor(rank_ids=t.getRankIds()))

        #
        # Create a list to hold a record of activity at each timestamp
        #
        self.log = []

        #
        # Flag to help addFrame() know if it is adding activity
        # to an existing frame
        #
        self.inframe = False

        #
        # Global cycle tracker
        #
        self.cycle = 0

        #
        # Reset image highlighting
        #
        ImageUtils.resetColors()

        #
        # Create desired canvas
        #
        # Note: We create the canvas with the shadow tensors, so that
        # the visualized activity happens in the desired order
        #
        if animation == 'movie':
            self.canvas = MovieCanvas(*self.shadow_tensors, style=style)
        elif animation == 'spacetime':
            self.canvas = SpacetimeCanvas(*self.shadow_tensors)
        elif animation == 'none':
            self.canvas = NoneCanvas(*self.shadow_tensors, style=style)
        else:
            self.logger.warning("TensorCanvas: No animation type: %s",
                                animation)
    def test_init_mutable(self):
        t = Tensor.fromYAMLfile("./data/test_tensor-1.yaml")
        self.assertFalse(t.isMutable())

        t2 = Tensor(rank_ids=["X", "Y", "Z"])
        self.assertTrue(t2.isMutable())
Example #30
0
#!/usr/bin/python3

import os
import argparse

from fibertree import Tensor, TensorImage

data_dir = "../examples/data"
example_file = os.path.join(data_dir, "draw-a.yaml")

parser = argparse.ArgumentParser(description='Display a tensor')
parser.add_argument("tensorfile", nargs="?", default=example_file)
args = parser.parse_args()

filename = args.tensorfile

a = Tensor(filename)
a.print(filename)

i = TensorImage(a)
i.show()