|
| 1 | +import scipy.sparse |
| 2 | +import scipy.io |
| 3 | +import sparse |
| 4 | +import os |
| 5 | +import glob |
| 6 | +import numpy |
| 7 | +import cv2 |
| 8 | + |
| 9 | +# NEEDS TO BE COMMENTED OUT FOR LANKA |
| 10 | +# import matplotlib.pyplot as plt |
| 11 | + |
| 12 | +# Get the path to the directory holding random tensors. Error out |
| 13 | +# if this isn't set. |
| 14 | +TENSOR_PATH = os.environ['TACO_TENSOR_PATH'] |
| 15 | + |
| 16 | +# TnsFileLoader loads a tensor stored in .tns format. |
| 17 | +class TnsFileLoader: |
| 18 | + def __init__(self): |
| 19 | + pass |
| 20 | + |
| 21 | + def load(self, path): |
| 22 | + coordinates = [] |
| 23 | + values = [] |
| 24 | + dims = [] |
| 25 | + first = True |
| 26 | + with open(path, 'r') as f: |
| 27 | + for line in f: |
| 28 | + data = line.split(' ') |
| 29 | + if first: |
| 30 | + first = False |
| 31 | + dims = [0] * (len(data) - 1) |
| 32 | + for i in range(len(data) - 1): |
| 33 | + coordinates.append([]) |
| 34 | + |
| 35 | + for i in range(len(data) - 1): |
| 36 | + coordinates[i].append(int(data[i]) - 1) |
| 37 | + dims[i] = max(dims[i], coordinates[i][-1] + 1) |
| 38 | + # TODO (rohany): What if we want this to be an integer? |
| 39 | + values.append(float(data[-1])) |
| 40 | + return dims, coordinates, values |
| 41 | + |
| 42 | +# TnsFileDumper dumps a dictionary of coordinates to values |
| 43 | +# into a coordinate list tensor file. |
| 44 | +class TnsFileDumper: |
| 45 | + def __init__(self): |
| 46 | + pass |
| 47 | + |
| 48 | + def dump_dict_to_file(self, shape, data, path, write_shape = False): |
| 49 | + # Sort the data so that the output is deterministic. |
| 50 | + sorted_data = sorted([list(coords) + [value] for coords, value in data.items()]) |
| 51 | + with open(path, 'w+') as f: |
| 52 | + for line in sorted_data: |
| 53 | + coords = [str(elem + 1) for elem in line[:len(line) - 1]] |
| 54 | + strings = coords + [str(line[-1])] |
| 55 | + f.write(" ".join(strings)) |
| 56 | + f.write("\n") |
| 57 | + if write_shape: |
| 58 | + shape_strings = [str(elem) for elem in shape] + ['0'] |
| 59 | + f.write(" ".join(shape_strings)) |
| 60 | + f.write("\n") |
| 61 | + |
| 62 | +# PydataSparseTensorLoader loads a sparse tensor from a file into |
| 63 | +# a pydata.sparse tensor. |
| 64 | +class PydataSparseTensorLoader: |
| 65 | + def __init__(self): |
| 66 | + self.loader = TnsFileLoader() |
| 67 | + |
| 68 | + def load(self, path): |
| 69 | + dims, coords, values = self.loader.load(path) |
| 70 | + return sparse.COO(coords, values, tuple(dims)) |
| 71 | + |
| 72 | +# construct_minmax_tensor_key constructs a unique key that represents |
| 73 | +# an image tensor parameterized by the tensor order |
| 74 | +# The key itself is formatted by the string 'minmax', followed by the |
| 75 | +# tensor order. For example, a parameter of 3 |
| 76 | +# would have a key of minmax-3.tns. |
| 77 | +def construct_minmax_tensor_key(dims, variant=None): |
| 78 | + path = TENSOR_PATH |
| 79 | + name = "minmax" |
| 80 | + if variant is None: |
| 81 | + key = "{}-{}.tns".format(name, len(dims)) |
| 82 | + else: |
| 83 | + key = "{}-{}-{}.tns".format(name,len(dims), variant) |
| 84 | + return os.path.join(path, name, key) |
| 85 | + |
| 86 | +def generate_crds_helper(shape, level, crds): |
| 87 | + sampling = 0.1 |
| 88 | + num = 3 |
| 89 | + std = 2 |
| 90 | + last_layer_sampling = 0.4 |
| 91 | + |
| 92 | + if level == len(shape) - 1: |
| 93 | + return crds |
| 94 | + else: |
| 95 | + result = [] |
| 96 | + d = shape[level] |
| 97 | + for c in crds: |
| 98 | + # Get number of locations |
| 99 | + num_locs = int(sampling*d) |
| 100 | + # Get location uniformly of where to sample around |
| 101 | + locs = numpy.random.rand(num_locs)*d |
| 102 | + |
| 103 | + # sample around each location using a normal distribution around that value with a std of 2 |
| 104 | + for loc in locs: |
| 105 | + points = std * numpy.random.randn(num) + loc |
| 106 | + points = points.astype('int') |
| 107 | + points = numpy.clip(points, 0, d - 1) |
| 108 | + for p in points: |
| 109 | + result.append(c+[p]) |
| 110 | + |
| 111 | + return generate_crds_helper(shape, level + 1, result) |
| 112 | + |
| 113 | +# RandomPydataSparseTensorLoader should be used to generate |
| 114 | +# random pydata.sparse tensors. It caches the loaded tensors |
| 115 | +# in the file system so that TACO benchmarks using tensors |
| 116 | +# with the same parameters can use the exact same tensors. |
| 117 | +class MinMaxPydataSparseTensorLoader: |
| 118 | + def __init__(self): |
| 119 | + self.loader = PydataSparseTensorLoader() |
| 120 | + |
| 121 | + def tensor(self, shape, variant=None): |
| 122 | + key = construct_minmax_tensor_key(shape) |
| 123 | + # If a tensor with these properties exists already, then load it. |
| 124 | + if os.path.exists(key): |
| 125 | + return self.loader.load(key) |
| 126 | + else: |
| 127 | + # Otherwise, we must create a random tensor with the desired properties, |
| 128 | + # dump it to the output file, then return it. |
| 129 | + crds = self.generate_crds(shape) |
| 130 | + values = dict() |
| 131 | + for c in crds: |
| 132 | + ind_list = numpy.random.rand(2)*shape[-1] |
| 133 | + ind_list = ind_list.astype('int') |
| 134 | + start = numpy.min(ind_list) |
| 135 | + stop = numpy.max(ind_list) |
| 136 | + for i in range(start, stop): |
| 137 | + temp = tuple(c[1:] + [i]) |
| 138 | + values[temp] = int(20*numpy.random.rand()) |
| 139 | + |
| 140 | + dok = sparse.DOK(shape, values) |
| 141 | + TnsFileDumper().dump_dict_to_file(shape, dok.data, key) |
| 142 | + result = dok.asformat('coo') |
| 143 | + return result |
| 144 | + |
| 145 | + |
| 146 | + def generate_crds(self, shape): |
| 147 | + return generate_crds_helper(shape, 0, [[0]]) |
0 commit comments