Exemplo n.º 1
0
    def move(self):
        while True:
            fpga_to_shift = np.random.randint(self.state.num_fpga_used)
            adjacent = (fpga_to_shift -
                        1 if np.random.randint(2) == 0 else fpga_to_shift + 1)
            fpga_alloc_table = make_fpga_alloc_table(self.state)

            if (adjacent < 0 or adjacent >= self.state.num_fpga_used
                    or fpga_alloc_table[adjacent] == 1):
                continue
            fpga_alloc_table[adjacent] -= 1
            fpga_alloc_table[fpga_to_shift] += 1

            new_state = parameters_pb2.Network()
            new_state.CopyFrom(self.state)

            fpga_index = 0
            for layer in new_state.layer:
                layer.fpga_id = fpga_index
                fpga_alloc_table[fpga_index] -= 1
                if fpga_alloc_table[fpga_index] == 0:
                    fpga_index += 1

            self.state = new_state
            break
Exemplo n.º 2
0
def main():
    filename = sys.argv[1]
    logging.getLogger().setLevel(logging.DEBUG)
    with open(filename, "r") as f:
        network = text_format.Parse(f.read(), parameters_pb2.Network())
    print network
    print project(network)
Exemplo n.º 3
0
def main():
    with open(FLAGS.descriptor, "r") as f:
        network = text_format.Parse(f.read(), parameters_pb2.Network())
    arr = []
    for i, layer in enumerate(network.layer):
        if layer.HasField("bitstream_id"):
            bitstream_id = layer.bitstream_id
        else:
            bitstream_id = 0
        fpga_id = layer.fpga_id

        if (bitstream_id, fpga_id) not in arr:
            arr.append((bitstream_id, fpga_id))

    with open(FLAGS.output, "w") as f:
        f.write("TARGET_NAMES= " + " ".join(["target_%d_%d" % (i, j) for (i, j) in arr]))
def main(argv):
    results = collections.OrderedDict([("resources",
                                        collections.OrderedDict([
                                            ("logic_utilization", 262400),
                                            ("primary_ff", 524800),
                                            ("secondary_ff", 524800),
                                            ("multiplier", 3926),
                                            ("dsp_block", 1963),
                                            ("block_memory", 2567),
                                        ])), ("data", [])])

    if os.path.exists(FLAGS.output):
        with open(FLAGS.output, "r") as f:
            results["data"] = yaml.load(f.read())["data"]

    for filename in FLAGS.logs:
        new_result = parse_file(filename)
        new_result["commit_hash"] = FLAGS.commit_hash

        if new_result["usage"] and not is_duplicate(results["data"],
                                                    new_result):
            with open("../descriptors/resource_bench/%s.prototxt" %
                      new_result["name"].replace("_MAIA_DFE", "")) as f:
                network = text_format.Parse(f.read(), parameters_pb2.Network())
                assert len(network.layer) == 1
                layer = network.layer[0]
                assert layer.HasField("conv")

            new_result["layer"] = {
                "input_height": layer.input_height,
                "input_width": layer.input_width,
                "num_inputs": layer.num_inputs,
                "num_outputs": layer.num_outputs,
                "kernel_size": layer.conv.kernel_size
            }
            new_result["params"] = {
                "conv_folding_factor": layer.conv.conv_folding_factor,
                "worker_factor": layer.conv.worker_factor,
                "kernel_folding_factor": layer.conv.kernel_folding_factor
            }
            results["data"].append(new_result)

    with open(FLAGS.output, "w") as f:
        f.write(yaml.dump(results, default_flow_style=False))
Exemplo n.º 5
0
def run_optimizer(network):
    minimized_states = []
    original_network = network

    for i in range(network.num_fpga_available, network.num_fpga_available + 1):
        network = parameters_pb2.Network()
        network.CopyFrom(original_network)
        network.num_fpga_used = i
        minimized_states.append(optimize_with_fixed_fpga_count(network, i))

    # Choose the best state, that uses the lowest number of FPGAs.
    best = None
    best_index = None
    for i, network in enumerate(minimized_states):
        if (network is not None
                and (best is None or estimate_gops(network) > best + 1.0)):
            best = estimate_gops(network)
            best_index = i
    return minimized_states[best_index]
Exemplo n.º 6
0
    def __init__(self, network):
        initial_state = parameters_pb2.Network()
        initial_state.CopyFrom(network)
        super(FoldingFactorOptimizationProblem, self).__init__(initial_state)
        self.valid_values = []

        for layer in network.layer:
            if layer.HasField("conv"):
                self.valid_values.append((
                    "conv",
                    {
                        "worker_factor":
                        compute_valid_values(layer.num_inputs),
                        "conv_folding_factor":
                        compute_valid_values(layer.num_outputs),
                        "kernel_folding_factor":
                        compute_valid_values(layer.conv.kernel_size *
                                             layer.conv.kernel_size),
                        # "look_ahead": compute_factors(layer.output_height * layer.output_width),

                        # Special cases... argh.
                        "bram_factor":
                        None,
                    }))

            elif layer.HasField("pool"):
                self.valid_values.append(("pool", {
                    "channel_folding_factor":
                    compute_valid_values(layer.num_inputs),
                }))

            elif layer.HasField("lrn"):
                self.valid_values.append(("lrn", {
                    "channel_folding_factor":
                    compute_valid_values(layer.num_inputs),
                }))

            else:
                raise RuntimeError("Unknown layer type.")
Exemplo n.º 7
0
def main():
    with open(FLAGS.design, "r") as f:
        network = text_format.Parse(f.read(), parameters_pb2.Network())

    for i, layer in enumerate(network.layer):
        if i != 0:
            layer.input_height = network.layer[i - 1].output_height
            layer.input_width = network.layer[i - 1].output_width
            layer.num_inputs = network.layer[i - 1].num_outputs

        layer.layer_id = i

        if layer.HasField("conv"):
            layer.output_height = (
                (layer.input_height + 2 * layer.conv.pad -
                 layer.conv.kernel_size) / layer.conv.stride + 1)
            layer.output_width = (
                (layer.input_width + 2 * layer.conv.pad -
                 layer.conv.kernel_size) / layer.conv.stride + 1)

            # Default parameters
            # Roughly half the worker factor, prevents heavy multiplexing at the
            # conv scheduler.
            layer.conv.worker_factor = int(math.ceil(layer.num_inputs / 3.))
            layer.conv.kernel_folding_factor = 1
            layer.conv.conv_folding_factor = 1
            layer.conv.look_ahead = 1
            layer.conv.bram_factor = layer.num_inputs * layer.num_outputs
            layer_bram_required = (layer.num_inputs * layer.num_outputs *
                                   (layer.conv.kernel_size**2) *
                                   resource_model.NUM_BITS)
            layer.conv.should_fit_on_chip = True

        elif layer.HasField("pool"):
            layer.num_outputs = layer.num_inputs
            stride = layer.pool.stride or layer.pool.dim
            layer.pool.stride = stride
            layer.output_height = div_ceil(layer.input_height, stride)
            layer.output_width = div_ceil(layer.input_width, stride)

            # Default parameters
            layer.pool.channel_folding_factor = 1

        elif layer.HasField("lrn"):
            layer.num_outputs = layer.num_inputs
            layer.output_height = layer.input_height
            layer.output_width = layer.input_width

            # Default parameters
            layer.lrn.channel_folding_factor = 1

        else:
            raise RuntimeError("Unknown layer!")

    network.layer[0].is_first_layer = True
    network.layer[-1].is_last_layer = True
    print network
    # logging.getLogger().setLevel(logging.DEBUG)
    optimized_network = run_optimizer(network)
    populate_weight_address(optimized_network)

    resource_model.project(optimized_network)

    with open(FLAGS.output, "w") as f:
        f.write(text_format.MessageToString(optimized_network))
Exemplo n.º 8
0
 def copy_state(self, state):
     new_network = parameters_pb2.Network()
     new_network.CopyFrom(state)
     return new_network
Exemplo n.º 9
0
    def _pertubate(self, state):
        while True:
            layer_id = random.randint(0, len(state.layer) - 1)
            layer_type = self.valid_values[layer_id][0]
            field_name = sample_array(self.valid_values[layer_id][1].keys())
            new_network = parameters_pb2.Network()
            new_network.CopyFrom(state)

            if field_name == "bram_factor":
                if new_network.layer[layer_id].conv.should_fit_on_chip:
                    continue
                layer = new_network.layer[layer_id]
                scheduler_iters = div_ceil(layer.num_inputs,
                                           layer.conv.worker_factor)
                conv_iters = div_ceil(layer.num_outputs,
                                      layer.conv.conv_folding_factor)
                new_value = (
                    layer.conv.conv_folding_factor * layer.conv.worker_factor *
                    sample_array(
                        compute_factors(conv_iters) +
                        compute_multiples(conv_iters, scheduler_iters *
                                          conv_iters)))
            elif (new_network.layer[layer_id].conv.should_fit_on_chip
                  and field_name == "look_ahead"):
                continue
            else:
                new_value = sample_array(
                    self.valid_values[layer_id][1][field_name])

            setattr(getattr(new_network.layer[layer_id], layer_type),
                    field_name, new_value)

            # Special case: synchronize changes to bram_factor
            if field_name == "worker_factor" or field_name == "conv_folding_factor":
                layer = new_network.layer[layer_id]

                layer.conv.bram_factor = (
                    layer.conv.conv_folding_factor * layer.conv.worker_factor *
                    div_ceil(layer.num_outputs, layer.conv.conv_folding_factor)
                    * div_ceil(layer.num_inputs, layer.conv.worker_factor))

                # Select the largest layer.layer_id that satisfies the new
                # constraints imposed.
                # while True:
                #     total_convolvers = (layer.conv.conv_folding_factor
                #                         * layer.conv.worker_factor)

                #     if layer.conv.bram_factor % total_convolvers == 0:
                #         size = layer.conv.bram_factor / total
                #         conv_iters = div_ceil(layer.num_outputs, layer.conv.conv_folding_factor)
                #         if (size < conv_iters and conv_iters % size == 0
                #                 or size >= conv_iters and size % conv_iters):
                #             break
                #         layer.conv.bram_factor -= 1

            resources = resource_model.project(new_network)

            if satisfies_resource_constraints(resources):
                return new_network

        return None