Exemple #1
0
def find_optimal_kernel(mnk, algo, tree, tree_features, gpu_properties,
                        autotuning_properties):
    """
    Find the optimal kernel parameter set for a given (m, n, k) and a given algorithm
    :return: optimal_kernels: dictionary, keys: (m, n, k), values: Kernel object describing best parameters
    """

    # Get parameter space for this (m, n, k) and this algorithm
    m, n, k = mnk
    parameter_space_ = kernel_algorithm[algo].promising_parameters(
        m, n, k, gpu_properties, autotuning_properties)
    parameter_space = pd.DataFrame(parameter_space_)
    del parameter_space_
    parameter_space["algorithm"] = [algo] * len(
        parameter_space.index)  # Add "algorithm" column
    if len(parameter_space.index) == 0:
        optimal_kernels = dict()

    else:

        # Get predictor features from raw parameters
        parameter_sets = PredictiveParameters(parameter_space, gpu_properties,
                                              autotuning_properties, None)
        predictors = np.array(parameter_sets.get_features(tree_features))

        # Predict performances
        performances_scaled = tree.predict(predictors)
        del predictors
        parameter_performances = parameter_sets.params
        del parameter_sets
        parameter_performances["perf"] = performances_scaled
        del performances_scaled

        # Pick optimal kernel
        optimal_kernel = max(parameter_performances.to_dict("records"),
                             key=lambda x: x["perf"])
        del parameter_performances
        optimal_kernels = dict()
        optimal_kernels[(m, n, k)] = params_dict_to_kernel(**optimal_kernel,
                                                           source="predicted")

    return optimal_kernels
def get_derived_pars(
    data_path,
    i,
    data_chunk,
    algorithm,
    gpu_properties,
    autotuning_properties,
    max_performances,
):
    # Compute derived parameters
    data_chunk["algorithm"] = [algorithm] * len(
        data_chunk.index)  # add 'algorithm' column manually
    parameter_sets = PredictiveParameters(data_chunk, gpu_properties,
                                          autotuning_properties,
                                          max_performances)
    pars_to_get = derived_parameters["common"] + derived_parameters[algorithm]
    new_data = parameter_sets.get_features(pars_to_get)

    # Write to CSV
    filename = os.path.join(data_path,
                            "training_data_{}-{}.csv".format(algorithm, i))
    new_data.to_csv(filename, index=False)

    return filename
Exemple #3
0
def main(tunedir, arch):
    """
    This script is part of the workflow for predictive modelling of optimal libcusmm parameters.
    For more details, see predict.md.

    After downloading raw data from the dedicated repository, use this script to
    - Compute derived training data and write it to a CSV file
    - Record maximum and baseline performances of (m,n,k)-triplets in JSON files
    """
    # ===============================================================================
    # Read GPU properties and autotuning properties
    with open("kernels/gpu_properties.json") as f:
        gpu_properties = json.load(f)["sm_" + str(arch)]
    with open("kernels/autotuning_properties.json") as f:
        autotuning_properties = json.load(f)

    # ===============================================================================
    # Loop over algorithms
    max_performances_per_mnk = dict()
    baseline_performances_per_algo_per_mnk = {
        "tiny": dict(),
        "small": dict(),
        "medium": dict(),
        "largeDB1": dict(),
        "largeDB2": dict(),
    }
    for name_algo, kernel_algo in kernel_algorithm.items():

        raw_training_data_filename = os.path.join(
            tunedir, "raw_training_data_{}.csv".format(name_algo))
        print("\nReading from {}".format(raw_training_data_filename))

        # Read CSV and loop over chunks
        chunk_size = 10000  # Number of rows of CSV file to process at a time
        chunk_count = 0

        for data_chunk in pd.read_csv(raw_training_data_filename,
                                      chunksize=chunk_size):

            # Print progress
            chunk_count += 1
            print("Read chunk {:5>}".format(chunk_count))

            # Get max_performance_per_mnk
            max_performances = get_max_performances_per_mnk(data_chunk)
            max_performances_per_mnk.update(
                dict(
                    zip(to_string(*max_performances.keys()),
                        max_performances.values())))

            # Get baseline_per_mnk
            baseline_performances_algo = get_baseline_performances_per_mnk(
                data_chunk, name_algo, gpu_properties, autotuning_properties)
            baseline_performances_per_algo_per_mnk[name_algo].update(
                dict(
                    zip(
                        to_string(*baseline_performances_algo.keys()),
                        baseline_performances_algo.values(),
                    )))

            # Compute derived parameters
            data_chunk["algorithm"] = [name_algo] * len(
                data_chunk.index)  # add 'algorithm' column manually
            parameter_sets = PredictiveParameters(data_chunk, gpu_properties,
                                                  autotuning_properties,
                                                  max_performances)
            pars_to_get = derived_parameters["common"] + derived_parameters[
                name_algo]
            new_data = parameter_sets.get_features(pars_to_get)

            # Write derived parameters
            derived_training_data_filename = os.path.join(
                tunedir,
                "training_data_{}_{}.csv".format(name_algo, chunk_count - 1))
            new_data[pars_to_get].to_csv(derived_training_data_filename,
                                         index=False)
            print("\tWrote", derived_training_data_filename)

    # ===============================================================================
    print("\nRead all raw and computed all derived data")

    # Print header lines & merge instructions
    print("\n$ # Merge instructions:")
    print("$ cd {}".format(tunedir))
    for name_algo, kernel_algo in kernel_algorithm.items():

        # Print header line
        derived_training_data_filename_base = "training_data_{}_{}.csv"
        derived_training_data_filename_chunk = derived_training_data_filename_base.format(
            name_algo, 0)
        with open(derived_training_data_filename_chunk, "r") as f:
            header_line = f.readline()
        derived_training_data_filename = "training_data_{}.csv".format(
            name_algo)
        with open(derived_training_data_filename, "w") as f:
            f.write(header_line)
        print("$ # Wrote header line to {}".format(
            derived_training_data_filename))

        # Print merge instructions
        print("$ # Wrote header line to {}".format(
            derived_training_data_filename))
        print("$ # Append training data chunks to {} by running:".format(
            derived_training_data_filename))
        derived_training_data_filename_wildcard = derived_training_data_filename_base.format(
            name_algo, "*")
        print("$ tail -n +2 -q {to_merge} >> {training_data_file}".format(
            to_merge=derived_training_data_filename_wildcard,
            training_data_file=derived_training_data_filename,
        ))

    # Print max performances
    max_performances_per_mnk_file = os.path.join(tunedir,
                                                 "max_performances.json")
    with open(max_performances_per_mnk_file, "w") as f:
        json.dump(max_performances_per_mnk, f)
    print("\nWrote maximum performances to:\n", max_performances_per_mnk_file)

    # Print baseline
    baseline_performances_per_algo_per_mnk_file = os.path.join(
        tunedir, "baseline_performances_by_algo.json")
    with open(baseline_performances_per_algo_per_mnk_file, "w") as f:
        json.dump(baseline_performances_per_algo_per_mnk, f)
    print(
        "\nWrote baseline performances to:\n",
        baseline_performances_per_algo_per_mnk_file,
    )
Exemple #4
0
def collect_training_data(
    kernel_folders,
    kernel_folder_pattern,
    gpu_properties,
    autotuning_properties,
    max_performances_per_mnk,
    baseline_performances_per_algo_per_mnk,
):
    """
    Collect training data from log files resulting of autotuning
    """

    n_kernels = len(kernel_folders)

    # For each folder:
    for i, kernel_folder in enumerate(kernel_folders):

        print("\nProcess folder {} ({}/{:,})".format(kernel_folder, i + 1,
                                                     n_kernels))

        # Find (m, n, k)
        match = kernel_folder_pattern.search(kernel_folder).groups()
        m = int(match[0])
        n = int(match[1])
        k = int(match[2])

        # ===============================================================================
        # Collect info from log files
        data = read_log_file(kernel_folder, m, n, k)

        # Collect max performances per (m, n, k)
        max_performances = get_max_performances_per_mnk(data)
        max_performances_per_mnk.update(
            dict(
                zip(to_string(*max_performances.keys()),
                    max_performances.values())))

        # ===============================================================================
        # Write parameters to CSV
        for name_algo, kernel_algo in kernel_algorithm.items():

            # if applicable to this mnk
            if name_algo in data["algorithm"].values:

                # Get the data corresponding to this algorithm
                data_algo = data[data["algorithm"] == name_algo]

                # Collect baseline performances per algo, per (m, n, k)
                baseline_performances_algo = get_baseline_performances_per_mnk(
                    data_algo, name_algo, gpu_properties,
                    autotuning_properties)
                baseline_performances_per_algo_per_mnk[name_algo].update(
                    dict(
                        zip(
                            to_string(*baseline_performances_algo.keys()),
                            baseline_performances_algo.values(),
                        )))

                # Does collected csv file exist already?
                raw_parameters_file_name = os.path.join(
                    kernel_folder,
                    "raw_training_data_" + to_string(m, n, k) + "_" +
                    name_algo + ".csv",
                )
                derived_parameters_file_name = os.path.join(
                    kernel_folder,
                    "training_data_" + to_string(m, n, k) + "_" + name_algo +
                    ".csv",
                )

                if os.path.exists(raw_parameters_file_name):
                    print("\tFound csv file:", raw_parameters_file_name,
                          ", skipping ...")

                else:

                    # Write raw parameters
                    pars_to_get = kernel_algo.launch_parameters + [
                        "perf (Gflop/s)"
                    ]
                    data_algo[pars_to_get].to_csv(raw_parameters_file_name,
                                                  index=False)
                    print("\tWrote", raw_parameters_file_name)

                if os.path.exists(derived_parameters_file_name):
                    print(
                        "\tFound csv file:",
                        derived_parameters_file_name,
                        ", skipping ...",
                    )

                else:
                    # Compute derived parameters
                    parameter_sets = PredictiveParameters(
                        data_algo,
                        gpu_properties,
                        autotuning_properties,
                        max_performances,
                    )
                    pars_to_get = (derived_parameters["common"] +
                                   derived_parameters[name_algo])
                    new_df = parameter_sets.get_features(pars_to_get)
                    data_algo.merge(new_df)

                    # Write derived parameters
                    data_algo[pars_to_get].to_csv(derived_parameters_file_name,
                                                  index=False)
                    print("\tWrote", derived_parameters_file_name)