Skip to content

Commit 9a4c548

Browse files
committed
Replace shell commands with subprocess.run
Signed-off-by: Bruno Alvisio <[email protected]>
1 parent 2d73084 commit 9a4c548

File tree

7 files changed

+103
-13
lines changed

7 files changed

+103
-13
lines changed

Dockerfile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,15 @@ RUN if [ "$TARGETARCH" = "arm64" ]; then \
123123
pip install .; \
124124
fi
125125

126-
# On ARM, bits and bytes needs to be built from scratch
127-
RUN if [ "$TARGETARCH" = "arm64" ]; then \
128-
cd / && pip uninstall bitsandbytes && \
129-
git clone --single-branch --branch 0.45.5 https://github.com/bitsandbytes-foundation/bitsandbytes.git && \
130-
cd bitsandbytes && cmake -DCOMPUTE_BACKEND=cuda -S . && make && pip install . && cd .. && rm -rf bitsandbytes; \
131-
fi
132126
###############################################################################
133127
# /end ARM
134128
###############################################################################
129+
130+
# Bits and bytes needs to be built from scratch
131+
RUN cd / && pip uninstall bitsandbytes && \
132+
git clone --single-branch --branch 0.45.5 https://github.com/bitsandbytes-foundation/bitsandbytes.git && \
133+
cd bitsandbytes && cmake -DCOMPUTE_BACKEND=cuda -S . && make && pip install . && cd .. && rm -rf bitsandbytes
134+
135135
# Fix the version of scikit-misc to 0.3.1 because newer versions of scikit-misc require numpy >= 2.0 to be built.
136136
# Since there are not pre-built wheels for arm64, we need to install this specific version.
137137
# Once bionemo is compatible with numpy >= 2.0, we can remove this.

ci/scripts/pytest_runner.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ SKIP_SLOW=false
5757
ONLY_SLOW=false
5858
ALLOW_NO_TESTS=false
5959
# TODO(@cspades): Ignore this Evo2 notebook test, which has a tendency to leave a 32GB orphaned process in GPU.
60-
declare -a IGNORE_FILES=("sub-packages/bionemo-evo2/examples/fine-tuning-tutorial.ipynb")
60+
declare -a IGNORE_FILES=()
6161
error=false
6262

6363
# Parse command line arguments

ci/scripts/run_pytest_notebooks.sh

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
# Enable strict mode with better error handling
2020
set -euox pipefail
2121

22-
pytest -v --nbval-lax -p no:python docs/ sub-packages/
22+
pytest -v --nbval-lax -x -p no:python docs/ sub-packages/

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ known-first-party = ["bionemo", "infra_bionemo"]
114114
convention = "google"
115115

116116
[tool.pytest.ini_options]
117+
xfail_strict = true
117118
norecursedirs = ["3rdparty"]
118119
addopts = [
119120
"--durations-min=30.0",
120121
"--durations=0",
121122
"--ignore=3rdparty",
122-
"--ignore-glob=sub-packages/bionemo-evo2/examples/fine-tuning-tutorial.ipynb",
123123
"--ignore-glob=sub-packages/bionemo-moco/examples/discrete_data_interpolant_tutorial.ipynb"
124124
]
125125
markers = ["slow: marks tests as slow (deselect with '-m \"not slow\"')"]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: LicenseRef-Apache2
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
17+
import logging
18+
import shlex
19+
import subprocess
20+
from typing import Any, Dict
21+
22+
23+
logger = logging.getLogger(__name__)
24+
25+
26+
def run_subprocess_safely(command: str, timeout: int = 2000) -> Dict[str, Any]:
27+
"""Run a subprocess and raise an error if it fails.
28+
29+
Args:
30+
command: The command to run.
31+
timeout: The timeout for the command.
32+
33+
Returns:
34+
The result of the subprocess.
35+
"""
36+
try:
37+
result = subprocess.run(shlex.split(command), capture_output=True, timeout=timeout, check=True, text=True)
38+
return {"stdout": result.stdout, "stderr": result.stderr, "returncode": result.returncode}
39+
except subprocess.TimeoutExpired as e:
40+
logger.error(f"Command timed out. Command: {command}\nstdout:\n{e.stdout}\nstderr:\n{e.stderr}")
41+
return {"error": "timeout", "stdout": e.stdout, "stderr": e.stderr, "returncode": None}
42+
43+
except subprocess.CalledProcessError as e:
44+
logger.error(
45+
f"Command failed. Command: {command}\nreturncode: {e.returncode}\nstdout:\n{e.stdout}\nstderr:\n{e.stderr}"
46+
)
47+
return {"error": "non-zero exit", "stdout": e.stdout, "stderr": e.stderr, "returncode": e.returncode}
48+
49+
except FileNotFoundError as e:
50+
logger.error(f"Command not found. Command: {command}\nstderr:\n{str(e)}")
51+
return {"error": "not found", "stdout": "", "stderr": str(e), "returncode": None}
52+
53+
except Exception as e:
54+
# catch-all for other unexpected errors
55+
return {"error": "other", "message": str(e), "stdout": "", "stderr": "", "returncode": None}

sub-packages/bionemo-evo2/examples/fine-tuning-tutorial.ipynb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@
9696
"%%capture\n",
9797
"import os\n",
9898
"\n",
99+
"from bionemo.core.utils.subprocess_utils import run_subprocess_safely\n",
100+
"\n",
99101
"\n",
100102
"concat_path = \"chr20_21_22.fa\"\n",
101103
"if not os.path.exists(concat_path):\n",
@@ -301,7 +303,18 @@
301303
" --create-tensorboard-logger \\\n",
302304
" --ckpt-async-save\"\"\"\n",
303305
"\n",
304-
"!{train_cmd}"
306+
"print(f\"Running command: {train_cmd}\")\n",
307+
"\n",
308+
"result = run_subprocess_safely(train_cmd)"
309+
]
310+
},
311+
{
312+
"cell_type": "code",
313+
"execution_count": null,
314+
"metadata": {},
315+
"outputs": [],
316+
"source": [
317+
"assert result[\"returncode\"] == 0, result"
305318
]
306319
},
307320
{

sub-packages/bionemo-evo2/examples/zeroshot_brca1.ipynb

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
"import seaborn as sns\n",
5151
"import torch\n",
5252
"from Bio import SeqIO\n",
53-
"from sklearn.metrics import auc, roc_auc_score, roc_curve"
53+
"from sklearn.metrics import auc, roc_auc_score, roc_curve\n",
54+
"\n",
55+
"from bionemo.core.utils.subprocess_utils import run_subprocess_safely"
5456
]
5557
},
5658
{
@@ -697,7 +699,17 @@
697699
"source": [
698700
"%%capture\n",
699701
"print(f\"Running command: {predict_ref_command}\")\n",
700-
"!{predict_ref_command}"
702+
"\n",
703+
"result = run_subprocess_safely(predict_ref_command)"
704+
]
705+
},
706+
{
707+
"cell_type": "code",
708+
"execution_count": null,
709+
"metadata": {},
710+
"outputs": [],
711+
"source": [
712+
"assert result[\"returncode\"] == 0, result"
701713
]
702714
},
703715
{
@@ -715,7 +727,17 @@
715727
"source": [
716728
"%%capture\n",
717729
"print(f\"Running command: {predict_var_command}\")\n",
718-
"!{predict_var_command}"
730+
"\n",
731+
"result = run_subprocess_safely(predict_var_command)"
732+
]
733+
},
734+
{
735+
"cell_type": "code",
736+
"execution_count": null,
737+
"metadata": {},
738+
"outputs": [],
739+
"source": [
740+
"assert result[\"returncode\"] == 0, result"
719741
]
720742
},
721743
{

0 commit comments

Comments
 (0)