|
| 1 | +import mock |
1 | 2 | import pytest |
| 3 | +from schema_salad.avro import schema |
2 | 4 |
|
| 5 | +from cwltool.builder import Builder |
| 6 | +from cwltool.context import LoadingContext, RuntimeContext |
3 | 7 | from cwltool.cuda import cuda_version_and_device_count |
| 8 | +from cwltool.errors import WorkflowException |
| 9 | +from cwltool.job import CommandLineJob |
| 10 | +from cwltool.load_tool import load_tool |
4 | 11 | from cwltool.main import main |
| 12 | +from cwltool.pathmapper import MapperEnt, PathMapper |
| 13 | +from cwltool.process import use_custom_schema, use_standard_schema |
| 14 | +from cwltool.stdfsaccess import StdFsAccess |
| 15 | +from cwltool.update import INTERNAL_VERSION, ORIGINAL_CWLVERSION |
| 16 | +from cwltool.utils import CWLObjectType |
5 | 17 |
|
6 | 18 | from .util import get_data, needs_docker, needs_singularity_3_or_newer |
7 | 19 |
|
| 20 | +from unittest.mock import MagicMock |
| 21 | + |
8 | 22 | cuda_version = cuda_version_and_device_count() |
9 | 23 |
|
10 | 24 |
|
@@ -39,7 +53,127 @@ def test_cuda_singularity() -> None: |
39 | 53 | def test_cuda_no_container() -> None: |
40 | 54 | params = [ |
41 | 55 | "--enable-ext", |
42 | | - "--singularity", |
43 | 56 | get_data("tests/wf/nvidia-smi.cwl"), |
44 | 57 | ] |
45 | 58 | assert main(params) == 0 |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.skipif( |
| 62 | + cuda_version[0] == "", reason="nvidia-smi required for CUDA not detected" |
| 63 | +) |
| 64 | +def test_cuda_cc_list() -> None: |
| 65 | + params = [ |
| 66 | + "--enable-ext", |
| 67 | + get_data("tests/wf/nvidia-smi-cc.cwl"), |
| 68 | + ] |
| 69 | + assert main(params) == 0 |
| 70 | + |
| 71 | + |
| 72 | +def _makebuilder(cudaReq: CWLObjectType) -> Builder: |
| 73 | + return Builder( |
| 74 | + {}, |
| 75 | + [], |
| 76 | + [], |
| 77 | + {}, |
| 78 | + schema.Names(), |
| 79 | + [cudaReq], |
| 80 | + [], |
| 81 | + {"cudaDeviceCount": 1}, |
| 82 | + None, |
| 83 | + None, |
| 84 | + StdFsAccess, |
| 85 | + StdFsAccess(""), |
| 86 | + None, |
| 87 | + 0.1, |
| 88 | + False, |
| 89 | + False, |
| 90 | + False, |
| 91 | + "", |
| 92 | + "", |
| 93 | + "", |
| 94 | + "", |
| 95 | + INTERNAL_VERSION, |
| 96 | + "docker", |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +@mock.patch("subprocess.check_output") |
| 101 | +@mock.patch("os.makedirs") |
| 102 | +def test_cuda_job_setup_check(makedirs: MagicMock, check_output: MagicMock) -> None: |
| 103 | + |
| 104 | + runtime_context = RuntimeContext({}) |
| 105 | + |
| 106 | + cudaReq: CWLObjectType = { |
| 107 | + "class": "http://commonwl.org/cwltool#CUDARequirement", |
| 108 | + "cudaVersionMin": "1.0", |
| 109 | + "cudaComputeCapability": "1.0", |
| 110 | + } |
| 111 | + builder = _makebuilder(cudaReq) |
| 112 | + |
| 113 | + check_output.return_value = """ |
| 114 | +<nvidia> |
| 115 | +<attached_gpus>1</attached_gpus> |
| 116 | +<cuda_version>1.0</cuda_version> |
| 117 | +</nvidia> |
| 118 | +""" |
| 119 | + |
| 120 | + jb = CommandLineJob(builder, {}, PathMapper, [], [], "") |
| 121 | + jb._setup(runtime_context) |
| 122 | + |
| 123 | + |
| 124 | +@mock.patch("subprocess.check_output") |
| 125 | +@mock.patch("os.makedirs") |
| 126 | +def test_cuda_job_setup_check_err(makedirs: MagicMock, check_output: MagicMock) -> None: |
| 127 | + |
| 128 | + runtime_context = RuntimeContext({}) |
| 129 | + |
| 130 | + cudaReq: CWLObjectType = { |
| 131 | + "class": "http://commonwl.org/cwltool#CUDARequirement", |
| 132 | + "cudaVersionMin": "2.0", |
| 133 | + "cudaComputeCapability": "1.0", |
| 134 | + } |
| 135 | + builder = _makebuilder(cudaReq) |
| 136 | + |
| 137 | + check_output.return_value = """ |
| 138 | +<nvidia> |
| 139 | +<attached_gpus>1</attached_gpus> |
| 140 | +<cuda_version>1.0</cuda_version> |
| 141 | +</nvidia> |
| 142 | +""" |
| 143 | + jb = CommandLineJob(builder, {}, PathMapper, [], [], "") |
| 144 | + with pytest.raises(WorkflowException): |
| 145 | + jb._setup(runtime_context) |
| 146 | + |
| 147 | + |
| 148 | +def test_cuda_eval_resource_range() -> None: |
| 149 | + with open(get_data("cwltool/extensions-v1.1.yml")) as res: |
| 150 | + use_custom_schema("v1.2", "http://commonwl.org/cwltool", res.read()) |
| 151 | + |
| 152 | + joborder = {} # type: CWLObjectType |
| 153 | + loadingContext = LoadingContext({"do_update": True}) |
| 154 | + runtime_context = RuntimeContext({}) |
| 155 | + |
| 156 | + tool = load_tool(get_data("tests/wf/nvidia-smi-range.cwl"), loadingContext) |
| 157 | + builder = _makebuilder(tool.requirements[0]) |
| 158 | + builder.job = joborder |
| 159 | + |
| 160 | + resources = tool.evalResources(builder, runtime_context) |
| 161 | + |
| 162 | + assert resources["cudaDeviceCount"] == 2 |
| 163 | + |
| 164 | + |
| 165 | +def test_cuda_eval_resource_max() -> None: |
| 166 | + with open(get_data("cwltool/extensions-v1.1.yml")) as res: |
| 167 | + use_custom_schema("v1.2", "http://commonwl.org/cwltool", res.read()) |
| 168 | + |
| 169 | + joborder = {} # type: CWLObjectType |
| 170 | + loadingContext = LoadingContext({"do_update": True}) |
| 171 | + runtime_context = RuntimeContext({}) |
| 172 | + |
| 173 | + tool = load_tool(get_data("tests/wf/nvidia-smi-max.cwl"), loadingContext) |
| 174 | + builder = _makebuilder(tool.requirements[0]) |
| 175 | + builder.job = joborder |
| 176 | + |
| 177 | + resources = tool.evalResources(builder, runtime_context) |
| 178 | + |
| 179 | + assert resources["cudaDeviceCount"] == 4 |
0 commit comments