|
| 1 | +# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES |
| 2 | +# Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +from typing import Any, List, Optional, Union |
| 20 | + |
| 21 | +from pydantic import Field |
| 22 | + |
| 23 | +from cloudai.core import DockerImage, Installable, JobStatusResult, TestRun |
| 24 | +from cloudai.models.workload import CmdArgs, TestDefinition |
| 25 | + |
| 26 | + |
| 27 | +class OSUBenchCmdArgs(CmdArgs): |
| 28 | + """Command line arguments for a OSU Benchmark test.""" |
| 29 | + |
| 30 | + docker_image_url: str |
| 31 | + """URL of the Docker image to use for the test.""" |
| 32 | + |
| 33 | + benchmarks_dir: str |
| 34 | + """Directory with the OSU Benchmark binaries inside the container. """ |
| 35 | + |
| 36 | + benchmark: Union[str, List[str]] |
| 37 | + """Name of the benchmark to run. """ |
| 38 | + |
| 39 | + message_size: Optional[Union[str, List[str]]] = Field(default=None) |
| 40 | + """Message size for the benchmark. |
| 41 | +
|
| 42 | + Examples:: |
| 43 | +
|
| 44 | + 128 // min = default, max = 128 |
| 45 | + 2:128 // min = 2, max = 128 |
| 46 | + 2: // min 2, max = default |
| 47 | + """ |
| 48 | + |
| 49 | + iterations: Optional[int] = Field(default=None) |
| 50 | + """Number of iterations for the benchmark.""" |
| 51 | + |
| 52 | + warmup: Optional[int] = Field(default=None) |
| 53 | + """Number of warmup iterations to skip before timing.""" |
| 54 | + |
| 55 | + mem_limit: Optional[int] = Field(default=None) |
| 56 | + """Per-process maximum memory consumption in bytes.""" |
| 57 | + |
| 58 | + full: bool = Field(default=True) |
| 59 | + """Print full format listing of results.""" |
| 60 | + |
| 61 | + |
| 62 | +class OSUBenchTestDefinition(TestDefinition): |
| 63 | + """Test definition for OSU Benchmark test.""" |
| 64 | + |
| 65 | + cmd_args: OSUBenchCmdArgs |
| 66 | + _osu_image: DockerImage | None = None |
| 67 | + |
| 68 | + @property |
| 69 | + def docker_image(self) -> DockerImage: |
| 70 | + if not self._osu_image: |
| 71 | + self._osu_image = DockerImage(url=self.cmd_args.docker_image_url) |
| 72 | + |
| 73 | + return self._osu_image |
| 74 | + |
| 75 | + @property |
| 76 | + def installables(self) -> list[Installable]: |
| 77 | + return [self.docker_image] |
| 78 | + |
| 79 | + @property |
| 80 | + def cmd_args_dict(self) -> dict[str, Any]: |
| 81 | + return self.cmd_args.model_dump(exclude={"docker_image_url", "benchmarks_dir", "benchmark"}) |
| 82 | + |
| 83 | + def was_run_successful(self, tr: TestRun) -> JobStatusResult: |
| 84 | + stdout_path = tr.output_path / "stdout.txt" |
| 85 | + stderr_path = tr.output_path / "stderr.txt" |
| 86 | + |
| 87 | + if not stdout_path.is_file(): |
| 88 | + return JobStatusResult( |
| 89 | + is_successful=False, |
| 90 | + error_message=( |
| 91 | + f"stdout.txt file not found in the specified output directory {tr.output_path}. " |
| 92 | + "This file is expected to be created as a result of the OSU Benchmark test run." |
| 93 | + ), |
| 94 | + ) |
| 95 | + |
| 96 | + with open(stdout_path, "r") as f: |
| 97 | + content = f.read() |
| 98 | + |
| 99 | + if not content.strip(): |
| 100 | + return JobStatusResult( |
| 101 | + is_successful=False, |
| 102 | + error_message=( |
| 103 | + f"stdout.txt file is empty in the specified output directory {tr.output_path}. " |
| 104 | + f"Please check for fatal errors in {stderr_path}" |
| 105 | + ), |
| 106 | + ) |
| 107 | + |
| 108 | + # Check for basic OSU benchmark output format |
| 109 | + if "# Size" not in content: |
| 110 | + return JobStatusResult( |
| 111 | + is_successful=False, |
| 112 | + error_message=( |
| 113 | + f"Expected OSU benchmark output marker not found in stdout.txt in {tr.output_path}. " |
| 114 | + f"Check for errors in the execution or for a different output format." |
| 115 | + ), |
| 116 | + ) |
| 117 | + |
| 118 | + # Additional validation could be added here to verify specific benchmark types |
| 119 | + # based on the full header format once benchmark-specific validation is needed |
| 120 | + |
| 121 | + return JobStatusResult(is_successful=True) |
0 commit comments