From a38bf890f087932922d64458fb6dc605b6e232c6 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk <21087696+oleksandr-pavlyk@users.noreply.github.com> Date: Tue, 12 May 2026 15:46:26 -0500 Subject: [PATCH] Fix license header, add validation --- python/cuda/bench/results/__init__.py | 17 +----- python/cuda/bench/results/__init__.pyi | 17 +----- .../cuda/bench/results/_benchmark_result.py | 59 +++++++++++-------- python/examples/benchmark_result_autotune.py | 17 +----- python/scripts/nvbench_json_summary.py | 18 +----- python/test/test_benchmark_result.py | 55 ++++++++++++----- python/test/test_nvbench_json_summary.py | 17 +----- 7 files changed, 86 insertions(+), 114 deletions(-) diff --git a/python/cuda/bench/results/__init__.py b/python/cuda/bench/results/__init__.py index 955d975..0c5498c 100644 --- a/python/cuda/bench/results/__init__.py +++ b/python/cuda/bench/results/__init__.py @@ -1,18 +1,5 @@ -# Copyright 2026 NVIDIA Corporation -# -# Licensed under the Apache License, Version 2.0 with the LLVM exception -# (the "License"); you may not use this file except in compliance with -# the License. -# -# You may obtain a copy of the License at -# -# http://llvm.org/foundation/relicensing/LICENSE.txt -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. +# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception """Utilities for reading NVBench JSON benchmark result files.""" diff --git a/python/cuda/bench/results/__init__.pyi b/python/cuda/bench/results/__init__.pyi index 3435895..2928441 100644 --- a/python/cuda/bench/results/__init__.pyi +++ b/python/cuda/bench/results/__init__.pyi @@ -1,18 +1,5 @@ -# Copyright 2026 NVIDIA Corporation -# -# Licensed under the Apache License, Version 2.0 with the LLVM exception -# (the "License"); you may not use this file except in compliance with -# the License. -# -# You may obtain a copy of the License at -# -# http://llvm.org/foundation/relicensing/LICENSE.txt -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. +# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception from array import array from collections.abc import Callable, ItemsView, Iterator, KeysView, ValuesView diff --git a/python/cuda/bench/results/_benchmark_result.py b/python/cuda/bench/results/_benchmark_result.py index 0bb8e04..6695aba 100644 --- a/python/cuda/bench/results/_benchmark_result.py +++ b/python/cuda/bench/results/_benchmark_result.py @@ -1,18 +1,5 @@ -# Copyright 2026 NVIDIA Corporation -# -# Licensed under the Apache License, Version 2.0 with the LLVM exception -# (the "License"); you may not use this file except in compliance with -# the License. -# -# You may obtain a copy of the License at -# -# http://llvm.org/foundation/relicensing/LICENSE.txt -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. +# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception from __future__ import annotations @@ -52,18 +39,44 @@ def read_json(filename: str | os.PathLike[str]) -> dict: return file_root +def extract_summary_data_value(summary: dict, name: str, expected_type: str) -> Any: + summary_tag = summary.get("tag", "") + for value_data in summary.get("data", []): + if value_data.get("name") != name: + continue + + value_type = value_data.get("type") + if value_type != expected_type: + raise ValueError( + f"summary {summary_tag!r} field {name!r} has type " + f"{value_type!r}; expected {expected_type!r}" + ) + if "value" not in value_data: + raise ValueError(f"summary {summary_tag!r} field {name!r} is missing value") + return value_data["value"] + + raise ValueError(f"summary {summary_tag!r} is missing field {name!r}") + + def extract_filename(summary: dict) -> str: - summary_data = summary["data"] - value_data = next(filter(lambda v: v["name"] == "filename", summary_data)) - assert value_data["type"] == "string" - return value_data["value"] + value = extract_summary_data_value(summary, "filename", "string") + if not isinstance(value, str): + raise ValueError( + f"summary {summary.get('tag', '')!r} field 'filename' " + "value must be a string" + ) + return value def extract_size(summary: dict) -> int: - summary_data = summary["data"] - value_data = next(filter(lambda v: v["name"] == "size", summary_data)) - assert value_data["type"] == "int64" - return int(value_data["value"]) + value = extract_summary_data_value(summary, "size", "int64") + try: + return int(value) + except (TypeError, ValueError) as e: + raise ValueError( + f"summary {summary.get('tag', '')!r} field 'size' " + f"value {value!r} is not an int64" + ) from e def parse_summary_value(value_data: dict) -> _SummaryValue: diff --git a/python/examples/benchmark_result_autotune.py b/python/examples/benchmark_result_autotune.py index 14d85d6..d5b5c1c 100644 --- a/python/examples/benchmark_result_autotune.py +++ b/python/examples/benchmark_result_autotune.py @@ -1,18 +1,5 @@ -# Copyright 2026 NVIDIA Corporation -# -# Licensed under the Apache License, Version 2.0 with the LLVM exception -# (the "License"); you may not use this file except in compliance with -# the License. -# -# You may obtain a copy of the License at -# -# http://llvm.org/foundation/relicensing/LICENSE.txt -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. +# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception from __future__ import annotations diff --git a/python/scripts/nvbench_json_summary.py b/python/scripts/nvbench_json_summary.py index d582a55..1478bff 100644 --- a/python/scripts/nvbench_json_summary.py +++ b/python/scripts/nvbench_json_summary.py @@ -1,20 +1,6 @@ #!/usr/bin/env python -# -# Copyright 2026 NVIDIA Corporation -# -# Licensed under the Apache License, Version 2.0 with the LLVM exception -# (the "License"); you may not use this file except in compliance with -# the License. -# -# You may obtain a copy of the License at -# -# http://llvm.org/foundation/relicensing/LICENSE.txt -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. +# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception from __future__ import annotations diff --git a/python/test/test_benchmark_result.py b/python/test/test_benchmark_result.py index cbdfefb..48c361a 100644 --- a/python/test/test_benchmark_result.py +++ b/python/test/test_benchmark_result.py @@ -1,18 +1,5 @@ -# Copyright 2026 NVIDIA Corporation -# -# Licensed under the Apache License, Version 2.0 with the LLVM exception -# (the "License"); you may not use this file except in compliance with -# the License. -# -# You may obtain a copy of the License at -# -# http://llvm.org/foundation/relicensing/LICENSE.txt -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. +# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception import json import struct @@ -514,6 +501,44 @@ def test_benchmark_result_uses_empty_summaries_when_field_is_missing(): assert state.bw is None +@pytest.mark.parametrize( + "field_name,bad_type,expected_type", + [ + ("filename", "int64", "string"), + ("size", "string", "int64"), + ], +) +def test_benchmark_result_validates_binary_summary_field_types( + field_name, bad_type, expected_type +): + summary = sample_times_summary("result.json-bin/0.bin", 3) + for value_data in summary["data"]: + if value_data["name"] == field_name: + value_data["type"] = bad_type + if field_name == "filename": + value_data["value"] = "123" + + with pytest.raises( + ValueError, + match=rf"field '{field_name}' has type '{bad_type}'; expected '{expected_type}'", + ): + results.SubBenchmarkResult( + { + "name": "copy", + "axes": [], + "states": [ + { + "name": "Device=0", + "axis_values": [], + "summaries": [summary], + "is_skipped": False, + } + ], + }, + "", + ) + + def test_benchmark_result_uses_none_for_unavailable_samples(tmp_path): json_fn = tmp_path / "result.json" write_json( diff --git a/python/test/test_nvbench_json_summary.py b/python/test/test_nvbench_json_summary.py index 0507ae0..0489e5d 100644 --- a/python/test/test_nvbench_json_summary.py +++ b/python/test/test_nvbench_json_summary.py @@ -1,18 +1,5 @@ -# Copyright 2026 NVIDIA Corporation -# -# Licensed under the Apache License, Version 2.0 with the LLVM exception -# (the "License"); you may not use this file except in compliance with -# the License. -# -# You may obtain a copy of the License at -# -# http://llvm.org/foundation/relicensing/LICENSE.txt -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. +# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception import importlib.util import json