Skip to content

Commit

Permalink
Removed usage of OrderedDict as per issue #209, minor changes to test…
Browse files Browse the repository at this point in the history
…s for this, reogranized imports and made sure some appearantly unused imports are not automatically removed
  • Loading branch information
fjwillemsen committed Sep 14, 2023
1 parent 66180ab commit 72744e7
Show file tree
Hide file tree
Showing 36 changed files with 233 additions and 289 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnType": true,
"editor.formatOnSave": true,
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.organizeImports": true,
Expand Down
28 changes: 11 additions & 17 deletions kernel_tuner/energy/energy.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
"""
This module contains a set of helper functions specifically for auto-tuning codes
for energy efficiency.
"""
from collections import OrderedDict

"""This module contains a set of helper functions specifically for auto-tuning codes for energy efficiency."""
import numpy as np
from scipy import optimize

from kernel_tuner import tune_kernel, util
from kernel_tuner.observers.nvml import NVMLObserver, get_nvml_gr_clocks
from scipy import optimize

try:
import pycuda.driver as drv
Expand Down Expand Up @@ -42,8 +38,7 @@
"""

def get_frequency_power_relation_fp32(device, n_samples=10, nvidia_smi_fallback=None, use_locked_clocks=False, cache=None, simulation_mode=None):
""" Use NVML and PyCUDA with a synthetic kernel to obtain samples of frequency-power pairs """

"""Use NVML and PyCUDA with a synthetic kernel to obtain samples of frequency-power pairs."""
# get some numbers about the device
if not cache:
if drv is None:
Expand All @@ -70,14 +65,14 @@ def get_frequency_power_relation_fp32(device, n_samples=10, nvidia_smi_fallback=
arguments = [data]

# setup tunable parameters
tune_params = OrderedDict()
tune_params = dict()
tune_params["block_size_x"] = [max_block_dim_x]
tune_params["nr_outer"] = [64]
tune_params["nr_inner"] = [1024]
tune_params.update(nvml_gr_clocks)

# metrics
metrics = OrderedDict()
metrics = dict()
metrics["f"] = lambda p: p["core_freq"]

nvmlobserver = NVMLObserver(
Expand All @@ -95,12 +90,12 @@ def get_frequency_power_relation_fp32(device, n_samples=10, nvidia_smi_fallback=


def estimated_voltage(clocks, clock_threshold, voltage_scale):
""" estimate voltage based on clock_threshold and voltage_scale """
"""Estimate voltage based on clock_threshold and voltage_scale."""
return [1 + ((clock > clock_threshold) * (1e-3 * voltage_scale * (clock-clock_threshold))) for clock in clocks]


def estimated_power(clocks, clock_threshold, voltage_scale, clock_scale, power_max):
""" estimate power consumption based on clock threshold, clock_scale and max power """
"""Estimate power consumption based on clock threshold, clock_scale and max power."""
n = len(clocks)
powers = np.zeros(n)

Expand All @@ -116,7 +111,7 @@ def estimated_power(clocks, clock_threshold, voltage_scale, clock_scale, power_m


def fit_power_frequency_model(freqs, nvml_power):
""" Fit the power-frequency model based on frequency and power measurements """
"""Fit the power-frequency model based on frequency and power measurements."""
nvml_gr_clocks = np.array(freqs)
nvml_power = np.array(nvml_power)

Expand Down Expand Up @@ -148,7 +143,7 @@ def fit_power_frequency_model(freqs, nvml_power):


def create_power_frequency_model(device=0, n_samples=10, verbose=False, nvidia_smi_fallback=None, use_locked_clocks=False, cache=None, simulation_mode=None):
""" Calculate the most energy-efficient clock frequency of device
"""Calculate the most energy-efficient clock frequency of device.
This function uses a performance model to fit the power-frequency curve
using a synthethic benchmarking kernel. The method has been described in:
Expand Down Expand Up @@ -202,8 +197,7 @@ def create_power_frequency_model(device=0, n_samples=10, verbose=False, nvidia_s


def get_frequency_range_around_ridge(ridge_frequency, all_frequencies, freq_range, number_of_freqs, verbose=False):
""" Return number_of_freqs frequencies in a freq_range percentage around the ridge_frequency from among all_frequencies """

"""Return number_of_freqs frequencies in a freq_range percentage around the ridge_frequency from among all_frequencies."""
min_freq = 1e-2 * (100 - int(freq_range)) * ridge_frequency
max_freq = 1e-2 * (100 + int(freq_range)) * ridge_frequency
frequency_selection = np.unique([all_frequencies[np.argmin(abs(
Expand Down
28 changes: 14 additions & 14 deletions kernel_tuner/file_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
""" This module contains utility functions for operations on files, mostly JSON cache files """
"""This module contains utility functions for operations on files, mostly JSON cache files."""

import os
import json
import os
import subprocess
import xmltodict
from sys import platform
from importlib.metadata import PackageNotFoundError, requires, version
from pathlib import Path
from sys import platform

from importlib.metadata import requires, version, PackageNotFoundError
import xmltodict
from packaging.requirements import Requirement

from kernel_tuner import util
Expand All @@ -16,7 +16,7 @@


def output_file_schema(target):
"""Get the requested JSON schema and the version number
"""Get the requested JSON schema and the version number.
:param target: Name of the T4 schema to return, should be any of ['output', 'metadata']
:type target: string
Expand All @@ -33,7 +33,7 @@ def output_file_schema(target):


def get_configuration_validity(objective) -> str:
"""Convert internal Kernel Tuner error to string"""
"""Convert internal Kernel Tuner error to string."""
errorstring: str
if not isinstance(objective, util.ErrorConfig):
errorstring = "correct"
Expand All @@ -50,21 +50,21 @@ def get_configuration_validity(objective) -> str:


def filename_ensure_json_extension(filename: str) -> str:
"""Check if the filename has a .json extension, if not, add it"""
"""Check if the filename has a .json extension, if not, add it."""
if filename[-5:] != ".json":
filename += ".json"
return filename


def make_filenamepath(filenamepath: Path):
"""Create the given path to a filename if the path does not yet exist"""
"""Create the given path to a filename if the path does not yet exist."""
filepath = filenamepath.parents[0]
if not filepath.exists():
filepath.mkdir()


def store_output_file(output_filename: str, results, tune_params, objective="time"):
"""Store the obtained auto-tuning results in a JSON output file
"""Store the obtained auto-tuning results in a JSON output file.
This function produces a JSON file that adheres to the T4 auto-tuning output JSON schema.
Expand All @@ -75,7 +75,7 @@ def store_output_file(output_filename: str, results, tune_params, objective="tim
:type results: list of dicts
:param tune_params: Tunable parameters as passed to tune_kernel
:type tune_params: OrderedDict
:type tune_params: dict
:param objective: The objective used during auto-tuning, default is 'time'.
:type objective: string
Expand Down Expand Up @@ -140,7 +140,7 @@ def store_output_file(output_filename: str, results, tune_params, objective="tim


def get_dependencies(package="kernel_tuner"):
"""Get the Python dependencies of Kernel Tuner currently installed and their version numbers"""
"""Get the Python dependencies of Kernel Tuner currently installed and their version numbers."""
requirements = requires(package)
deps = [Requirement(req).name for req in requirements]
depends = []
Expand All @@ -155,7 +155,7 @@ def get_dependencies(package="kernel_tuner"):


def get_device_query(target):
"""Get the information about GPUs in the current system, target is any of ['nvidia', 'amd']"""
"""Get the information about GPUs in the current system, target is any of ['nvidia', 'amd']."""
if target == "nvidia":
nvidia_smi_out = subprocess.run(["nvidia-smi", "--query", "-x"], capture_output=True)
nvidia_smi = xmltodict.parse(nvidia_smi_out.stdout)
Expand All @@ -176,7 +176,7 @@ def get_device_query(target):


def store_metadata_file(metadata_filename: str):
"""Store the metadata about the current hardware and software environment in a JSON output file
"""Store the metadata about the current hardware and software environment in a JSON output file.
This function produces a JSON file that adheres to the T4 auto-tuning metadata JSON schema.
Expand Down
5 changes: 2 additions & 3 deletions kernel_tuner/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
limitations under the License.
"""
import logging
from collections import OrderedDict
from datetime import datetime
from time import perf_counter

Expand Down Expand Up @@ -79,7 +78,7 @@
}


class Options(OrderedDict):
class Options(dict):
"""read-only class for passing options around."""

def __getattr__(self, name):
Expand Down Expand Up @@ -462,7 +461,7 @@ def __deepcopy__(self, _):
"string",
),
),
("metrics", ("specifies user-defined metrics, please see :ref:`metrics`.", "OrderedDict")),
("metrics", ("specifies user-defined metrics, please see :ref:`metrics`.", "dict")),
("simulation_mode", ("Simulate an auto-tuning search from an existing cachefile", "bool")),
("observers", ("""A list of Observers to use during tuning, please see :ref:`observers`.""", "list")),
]
Expand Down
Loading

0 comments on commit 72744e7

Please sign in to comment.