diff --git a/src/autotuning_methodology/formats_interface.py b/src/autotuning_methodology/formats_interface.py index 4d6fa58..f2b2538 100644 --- a/src/autotuning_methodology/formats_interface.py +++ b/src/autotuning_methodology/formats_interface.py @@ -5,6 +5,24 @@ from autotuning_methodology.validators import validate_T4 +def get_T4_timeunit(metadata: dict) -> str: + """Get the time unit the runtime measurements of a T4 file are expressed in. + + Reads ``metadata.session.timeunit``, falling back to the flat ``metadata.timeunit`` key used + before the metadata reorganization, and to seconds when the file names no unit at all. + + Args: + metadata: the ``metadata`` object of a T4 document. + + Returns: + the name of the time unit. + """ + session_timeunit = metadata.get("session", {}).get("timeunit") + if session_timeunit is not None: + return session_timeunit + return metadata.get("timeunit", "seconds") + + def load_T4_format(filepath: Path, validate: True) -> dict: """Load and optionally validate a T4 format file.""" with open(filepath, "r", encoding="utf-8") as fh: diff --git a/src/autotuning_methodology/runner.py b/src/autotuning_methodology/runner.py index 6035a97..2ce27e8 100755 --- a/src/autotuning_methodology/runner.py +++ b/src/autotuning_methodology/runner.py @@ -18,6 +18,7 @@ import yappi from autotuning_methodology.caching import ResultsDescription +from autotuning_methodology.formats_interface import get_T4_timeunit from autotuning_methodology.searchspace_statistics import SearchspaceStatistics, convert_from_time_unit from autotuning_methodology.validators import ( is_invalid_objective_performance, @@ -200,7 +201,7 @@ def tune_with_KTT(): raise ValueError(f"Invalid autotuning framework '{group['autotuner']}'") # convert time units - timeunit: str = results.get("metadata", {}).get("timeunit", "seconds") + timeunit: str = get_T4_timeunit(results.get("metadata", {})) for result in results["results"]: for k, v in result["times"].items(): result["times"][k] = convert_from_time_unit(v, timeunit) diff --git a/src/autotuning_methodology/schemas/README.md b/src/autotuning_methodology/schemas/README.md new file mode 100644 index 0000000..210112a --- /dev/null +++ b/src/autotuning_methodology/schemas/README.md @@ -0,0 +1,178 @@ +# T4: Open Autotuning Results Schema + +`T4.json` is a [JSON Schema](https://json-schema.org/draft/2020-12/schema) (Draft 2020-12) +describing the output format autotuning frameworks write their results in. A T4 document has +three top-level members: + +| Member | Purpose | +| --- | --- | +| `schema_version` | Version of this schema the document follows, in `major.minor.patch` form. | +| `metadata` | Everything about the environment the tuning session ran in. | +| `results` | One entry per tested configuration: its parameters, timings and validity. | + +## Metadata structure + +The `metadata` object captures comprehensive reproducibility information organized into six +categories. **Every category, and every field within a category, is optional** — collect what +the machine can tell you and leave the rest out. + +### Compute + +What the kernels ran on, as the compute API reports it. + +- `api` — compute API (`CUDA`, `OpenCL`, `HIP`, `SYCL`, `Vulkan`) +- `platform` — SDK/platform version, e.g. `NVIDIA CUDA 12.1` +- `device` — device name as reported by the API + +### Framework + +Which autotuner orchestrated the session. + +- `autotuner` — framework name (`KernelTuner`, `KTT`, `CLTune`, `ATF`) +- `version` — framework version +- `backend` — backend implementation (`PyCUDA`, `CuPy`, `HIP`), if the framework has one + +### Session + +When the session ran and how its numbers should be read. + +- `timestamp` — ISO 8601 UTC start time, e.g. `2024-03-15T14:30:00Z` +- `timeunit` — unit for the runtime measurements in `results`, one of `nanoseconds`, + `microseconds`, `milliseconds`, `seconds` + +### Hardware + +Hardware inventory, stored as the raw JSON the introspection tools produce. The schema fixes +only each field's container type; the keys inside are whatever the tool printed, so a newer +tool version does not need a schema change. + +- `accelerators` — array of GPU/accelerator entries, vendor-neutral so heterogeneous and + multi-GPU hosts both fit. Each entry needs a `vendor` (`nvidia`, `amd`, `intel`, `apple`) + and its `data`; `tool` names the query tool (`nvidia-smi`, `rocm-smi`, `clinfo`, + `system_profiler`) that produced it. +- `cpu` — `lscpu --json` output (architecture, cores, cache) +- `memory` — `/proc/meminfo` parsed to JSON (total, free, swap) +- `storage` — `lsblk` array (devices, sizes, mount points) +- `system` — `lshw` hardware tree (motherboard, buses, peripherals) + +### Environment + +The software side of the same question. + +- `os_info` — `lsb_release -a` output parsed to JSON (distribution, version, codename) +- `hostname` — system identifier, useful for multi-node runs +- `python_requirements` — array of requirement specifier strings, as `requirements.txt` holds + them +- `software` — map of tool name to version (`gcc`, `nvcc`, `cuda-toolkit`, `cmake`, …). A value + is normally a version string, but may be an object when a path or build flags are worth + recording. + +### DOI + +- `doi` — Digital Object Identifier of the archived dataset, validated against the standard + `10.{registrant}/{suffix}` form. Added after publishing to Zenodo, Figshare, OSF or an + institutional archive, so whoever holds the file can find the archive it came from. + +## Metadata collection workflow + +### System metadata collection + +A collector script (the reference implementation is `metadata.py` from +[odgaard/TuningSchema](https://github.com/odgaard/TuningSchema)) gathers the hardware and +environment data and emits JSON shaped like the two matching metadata categories: + +```json +{ + "hardware": { + "accelerators": [], + "cpu": {}, + "memory": {}, + "storage": [], + "system": {} + }, + "environment": { + "os_info": {}, + "hostname": "gpu-node-01", + "python_requirements": [], + "software": {} + } +} +``` + +The collection commands behind those fields: + +| Field | Command | +| --- | --- | +| `hardware.accelerators` | `nvidia-smi --query -x`, `rocm-smi`, `clinfo` → JSON | +| `hardware.cpu` | `lscpu --json` | +| `hardware.memory` | `cat /proc/meminfo` → JSON | +| `hardware.storage` | `lsblk -a` → JSON | +| `hardware.system` | `lshw -json -sanitize` | +| `environment.os_info` | `lsb_release -a` → JSON | +| `environment.hostname` | `hostname` | +| `environment.python_requirements` | `requirements.txt` | +| `environment.software` | `gcc --version`, `nvcc --version`, … | + +**Privacy.** Strip the `` section from `nvidia-smi` output and always pass +`-sanitize` to `lshw` so serial numbers stay out of the file. Note that `hostname` can reveal +internal network topology, and absolute paths can reveal directory structure; document +whatever measures you took. + +### Merging into T4 results + +The autotuning framework merges the collected system metadata with the metadata it knows +itself (compute, framework, session): + +**Python** + +```python +import json + +with open("system-metadata.json") as system_metadata_file: + system_metadata = json.load(system_metadata_file) + +output = { + "schema_version": "1.1.0", + "metadata": { + "compute": {"api": "CUDA", "platform": "NVIDIA CUDA 12.1", "device": "NVIDIA A100-SXM4-40GB"}, + "framework": {"autotuner": "KernelTuner", "version": "1.0.0", "backend": "PyCUDA"}, + "session": {"timestamp": "2024-03-15T14:30:00Z", "timeunit": "milliseconds"}, + **system_metadata, # adds hardware and environment + }, + "results": [], +} +``` + +**C++ (nlohmann/json)** + +```cpp +#include +using json = nlohmann::json; + +json system_metadata = /* load from file */; +json output = { + {"schema_version", "1.1.0"}, + {"metadata", { + {"compute", {{"api", "CUDA"}, {"platform", "NVIDIA CUDA 12.1"}, {"device", "NVIDIA A100-SXM4-40GB"}}}, + {"framework", {{"autotuner", "KTT"}, {"version", "1.0.0"}}}, + {"session", {{"timestamp", "2024-03-15T14:30:00Z"}, {"timeunit", "milliseconds"}}}, + {"hardware", system_metadata["hardware"]}, + {"environment", system_metadata["environment"]} + }}, + {"results", json::array()} +}; +``` + +## Validating a results file + +```python +import json +from jsonschema import validate + +with open("T4.json") as schema_file: + schema = json.load(schema_file) +with open("my-results.json") as results_file: + validate(instance=json.load(results_file), schema=schema) +``` + +`ajv` (JavaScript) and [jsonschemavalidator.net](https://www.jsonschemavalidator.net) work too. diff --git a/src/autotuning_methodology/schemas/T4.json b/src/autotuning_methodology/schemas/T4.json index 82db64e..3af754d 100644 --- a/src/autotuning_methodology/schemas/T4.json +++ b/src/autotuning_methodology/schemas/T4.json @@ -1,50 +1,249 @@ { "$schema": "https://json-schema.org/draft/2020-12/schema", - "$id": "https://github.com/odgaard/TuningSchema/blob/T4/results-schema.json", - "description": "Open Autotuning Results Schema", + "$id": "https://github.com/AutoTuningAssociation/autotuning_methodology/blob/main/src/autotuning_methodology/schemas/T4.json", + "description": "Open Autotuning Results Schema with comprehensive hardware and environment metadata", "type": "object", "properties": { "schema_version": { "description": "The version number of the schema in major.minor.patch format.", "type": "string", - "pattern": "^[0-9]{1,}.[0-9]{1,}.[0-9]{1,}$", - "example": "1.0.0" + "pattern": "^[0-9]{1,}\\.[0-9]{1,}\\.[0-9]{1,}$", + "examples": ["1.1.0"] + }, + "metadata": { + "description": "Metadata about the tuning environment and execution context", + "type": "object", + "properties": { + "compute": { + "description": "Compute API and device configuration used for kernel execution", + "type": "object", + "properties": { + "api": { + "description": "The compute API used for kernel execution", + "type": "string", + "examples": ["OpenCL", "CUDA", "Vulkan", "HIP", "SYCL"] + }, + "platform": { + "description": "The compute platform name (e.g., vendor SDK version)", + "type": "string", + "examples": ["NVIDIA CUDA 12.1", "AMD ROCm 5.6.0"] + }, + "device": { + "description": "The compute device name as reported by the API", + "type": "string", + "examples": ["NVIDIA A100-SXM4-40GB", "AMD Instinct MI250X"] + } + } + }, + "framework": { + "description": "Autotuning framework identification and configuration", + "type": "object", + "properties": { + "autotuner": { + "description": "The name of the autotuning framework", + "type": "string", + "examples": ["KernelTuner", "KTT", "CLTune", "ATF"] + }, + "version": { + "description": "The version of the autotuning framework", + "type": "string", + "examples": ["1.0.0", "2.3.1"] + }, + "backend": { + "description": "The backend used by the autotuning framework (when applicable)", + "type": "string", + "examples": ["PyCUDA", "CuPy", "HIP", "cupy"] + } + } + }, + "session": { + "description": "Tuning session timing and measurement configuration", + "type": "object", + "properties": { + "timestamp": { + "description": "ISO 8601 UTC timestamp when the tuning session started", + "type": "string", + "examples": ["2024-03-15T14:30:00Z"] + }, + "timeunit": { + "description": "The default time unit used for runtime measurements in the results", + "type": "string", + "enum": ["nanoseconds", "microseconds", "milliseconds", "seconds"] + } + } + }, + "hardware": { + "description": "Hardware inventory collected via system introspection tools", + "type": "object", + "properties": { + "accelerators": { + "description": "Array of GPU/accelerator devices. Each entry represents one device or device query tool output. Supports heterogeneous systems with multiple vendor accelerators.", + "type": "array", + "items": { + "type": "object", + "properties": { + "vendor": { + "description": "Accelerator vendor identifier. Use consistent naming: 'nvidia', 'amd', 'intel' for common vendors.", + "type": "string", + "examples": ["nvidia", "amd", "intel", "apple"] + }, + "tool": { + "description": "The tool used to query device information", + "type": "string", + "examples": ["nvidia-smi", "rocm-smi", "clinfo", "system_profiler"] + }, + "data": { + "description": "Raw tool output converted to JSON. Structure varies by tool and version. For nvidia-smi: XML output converted to JSON with processes removed for privacy.", + "type": "object" + } + }, + "required": ["vendor", "data"] + } + }, + "cpu": { + "description": "CPU information from lscpu --json output. Contains processor architecture, model, cores, threads, cache sizes, and capabilities.", + "type": "object" + }, + "memory": { + "description": "Memory information from /proc/meminfo parsed to JSON. Includes total/free/available memory, swap, buffers, and cache statistics.", + "type": "object" + }, + "storage": { + "description": "Block device information from lsblk output parsed to JSON. Array of storage devices with name, size, type, and mount points.", + "type": "array", + "items": { + "type": "object" + } + }, + "system": { + "description": "General hardware information from lshw -json -sanitize. Complete system hardware tree including motherboard, buses, and peripherals. The -sanitize flag removes serial numbers for privacy.", + "type": "object" + } + } + }, + "environment": { + "description": "Software environment metadata including OS, hostname, dependencies, and toolchain", + "type": "object", + "properties": { + "os_info": { + "description": "Operating system information from lsb_release -a output parsed to JSON. Contains distribution name, version, codename, and description.", + "type": "object", + "examples": [ + { + "Distributor ID": "Ubuntu", + "Description": "Ubuntu 22.04.3 LTS", + "Release": "22.04", + "Codename": "jammy" + } + ] + }, + "hostname": { + "description": "System hostname from the hostname command. Useful for distinguishing results from different machines or cluster nodes.", + "type": "string", + "examples": ["gpu-node-01", "workstation.example.com"] + }, + "python_requirements": { + "description": "Python package dependencies from requirements.txt. Array of requirement specifier strings.", + "type": "array", + "items": { + "type": "string" + }, + "examples": [["numpy>=1.24.0", "kernel_tuner==1.0.0", "cuda-python==12.1.0"]] + }, + "software": { + "description": "Compilers, libraries, and toolkits used for kernel compilation and execution. Keys are tool/library names, values are typically version strings but can be objects with additional metadata.", + "type": "object", + "additionalProperties": true, + "examples": [ + { + "gcc": "11.4.0", + "nvcc": "12.1.105", + "cuda-toolkit": "12.1", + "rocm": "5.6.0", + "openblas": "0.3.21", + "cmake": "3.27.0" + } + ] + } + } + }, + "doi": { + "description": "Digital Object Identifier (DOI) for archived results. Added after publishing to a repository like Zenodo, Figshare, or institutional archives. The DOI provides a permanent, citable link to the complete dataset.", + "type": "string", + "pattern": "^10\\.\\d{4,9}/[-._;()/:A-Za-z0-9]+$", + "examples": ["10.5281/zenodo.1234567", "10.6084/m9.figshare.9876543", "10.17605/OSF.IO/ABC123"] + } + } }, "results": { + "description": "Array of tuning results, one per tested configuration", "type": "array", "items": { "type": "object", "properties": { "timestamp": { + "description": "ISO 8601 timestamp when this configuration was evaluated", "type": "string" }, "configuration": { + "description": "The parameter configuration tested. Keys are parameter names, values are parameter values.", "type": "object" }, "objectives": { - "type": "array" + "description": "List of optimization objectives for this result", + "type": "array", + "items": { + "type": "string" + }, + "examples": ["time"] }, "times": { + "description": "Breakdown of time measurements for this configuration", "type": "object", "properties": { "compilation_time": { + "description": "Time spent compiling the kernel function", "type": "number" }, "runtimes": { - "type": "array" + "description": "Array of kernel runtime measurements", + "type": "array", + "items": { + "type": "number" + } }, "framework": { + "description": "Total framework overhead (sum of data, profiling, and kernel overhead)", "type": "number" }, "search_algorithm": { + "description": "Time spent by the search algorithm selecting this configuration", "type": "number" }, "validation": { + "description": "Time spent validating correctness of results", + "type": "number" + }, + "data": { + "description": "Time spent on data movement (host-device transfers)", + "type": "number" + }, + "kernel_overhead": { + "description": "Kernel launch overhead", + "type": "number" + }, + "profiling_runs": { + "description": "Time spent on profiling runs", + "type": "number" + }, + "profiling_overhead": { + "description": "Overhead from profiling instrumentation", "type": "number" } } }, "invalidity": { + "description": "Status indicating whether the configuration is valid or the reason for invalidity", "enum": [ "timeout", "compile", @@ -55,28 +254,120 @@ ] }, "correctness": { - "type": "number" + "description": "Binary indicator of correctness (1 = correct, 0 = incorrect)", + "type": "number", + "enum": [0, 1] }, "measurements": { + "description": "Array of measurements including runtime and optional profiling counters", "type": "array", "items": { "type": "object", "properties": { "name": { - "type": "string" + "description": "Name of the measurement or profiling counter", + "type": "string", + "example": "time" }, "value": { - "type": [ - "number", - "string", - "array" - ] + "description": "The measured value", + "type": ["number", "string", "array"] }, "unit": { + "description": "Unit of measurement (empty string if dimensionless or in metadata timeunit)", "type": "string" + }, + "type": { + "description": "Data type of the profiling counter (used in KTT autotuner)", + "type": "string", + "enum": ["Int", "UnsignedInt", "Double", "Percent", "Throughput", "UtilizationLevel"] } } } + }, + "compilation_data": { + "description": "Kernel compilation metadata and thread dimensions (optional, used by KTT's profile-based searcher)", + "type": "object", + "properties": { + "max_work_group_size": { + "description": "Maximum work-group size (thread block size) for this kernel on this device", + "type": "integer", + "minimum": 0 + }, + "local_memory_size": { + "description": "Statically-allocated local memory (shared memory in CUDA) in bytes", + "type": "integer", + "minimum": 0 + }, + "private_memory_size": { + "description": "Private memory (local memory in CUDA) per work-item in bytes", + "type": "integer", + "minimum": 0 + }, + "constant_memory_size": { + "description": "User-allocated constant memory in bytes (CUDA only, 0 for OpenCL)", + "type": "integer", + "minimum": 0 + }, + "registers": { + "description": "Number of registers per work-item (CUDA only, 0 for OpenCL)", + "type": "integer", + "minimum": 0 + }, + "global_size": { + "description": "Global thread dimensions (NDRange in OpenCL, grid dimensions in CUDA)", + "type": "object", + "properties": { + "x": { + "description": "Size in X dimension", + "type": "integer", + "minimum": 1 + }, + "y": { + "description": "Size in Y dimension", + "type": "integer", + "minimum": 1 + }, + "z": { + "description": "Size in Z dimension", + "type": "integer", + "minimum": 1 + } + }, + "required": ["x", "y", "z"] + }, + "local_size": { + "description": "Local work-group dimensions (work-group size in OpenCL, thread block size in CUDA)", + "type": "object", + "properties": { + "x": { + "description": "Size in X dimension", + "type": "integer", + "minimum": 1 + }, + "y": { + "description": "Size in Y dimension", + "type": "integer", + "minimum": 1 + }, + "z": { + "description": "Size in Z dimension", + "type": "integer", + "minimum": 1 + } + }, + "required": ["x", "y", "z"] + } + }, + "required": [ + "max_work_group_size", + "local_memory_size", + "private_memory_size", + "constant_memory_size", + "registers", + "global_size", + "local_size" + ] } }, "required": [ @@ -88,4 +379,4 @@ } } } -} \ No newline at end of file +} diff --git a/src/autotuning_methodology/searchspace_statistics.py b/src/autotuning_methodology/searchspace_statistics.py index 43d46c7..f5ca53f 100644 --- a/src/autotuning_methodology/searchspace_statistics.py +++ b/src/autotuning_methodology/searchspace_statistics.py @@ -9,7 +9,7 @@ import matplotlib.pyplot as plt import numpy as np -from autotuning_methodology.formats_interface import load_T4_format +from autotuning_methodology.formats_interface import get_T4_timeunit, load_T4_format from autotuning_methodology.validators import is_invalid_objective_performance, is_invalid_objective_time @@ -428,7 +428,7 @@ def _load(self, validate=True) -> bool: filepath = self.get_valid_filepath() data = load_T4_format(filepath, validate=validate) metadata: dict = data.get("metadata", {}) - timeunit = metadata.get("timeunit", "seconds") + timeunit = get_T4_timeunit(metadata) results: dict = data["results"] self.results = results