Example #1
0
def collect_training_data(kernel_folders, kernel_folder_pattern):
    """
    Collect training data from log files resulting of autotuning
    """

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

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

        # Find (m, n, k)
        # Each folder contains data for just one (m, n, k) but potentially mutliple algorithms
        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)

        # ===============================================================================
        # 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:

                # 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",
                )

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

                else:

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

                    # 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)
Example #2
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,
    )
Example #3
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)