Skip to content

Commit 98a15dc

Browse files
Augustin-Zidekcopybara-github
authored andcommitted
Replace --use_cpu_only with --jax_backend for future expandability
PiperOrigin-RevId: 952803413 Change-Id: Ia5ae477f5169b07727fddde418acd15958afa67b
1 parent 4e62292 commit 98a15dc

2 files changed

Lines changed: 31 additions & 16 deletions

File tree

docs/installation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,14 +521,14 @@ resort to running AlphaFold 3 in the slow CPU-only mode even though it has a GPU
521521
uv run python run_alphafold_data_test.py
522522
```
523523
524-
7. You can now run AlphaFold 3. The flags that need to be set for CPU-only
525-
inference are `--use_cpu_only` and `--flash_attention_implementation="xla"`:
524+
7. You can now run AlphaFold 3. Make sure to set flags `--jax_backend="cpu"`
525+
and `--flash_attention_implementation="xla"`:
526526
527527
```sh
528528
uv run run_alphafold.py \
529529
--json_path="..." \
530530
--output_dir="..." \
531531
--model_dir="..."
532-
--use_cpu_only \
532+
--jax_backend="cpu" \
533533
--flash_attention_implementation="xla" \
534534
```

run_alphafold.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import csv
3333
import dataclasses
3434
import datetime
35+
import enum
3536
import functools
3637
import io
3738
import os
@@ -67,6 +68,13 @@
6768
_DEFAULT_MODEL_DIR = _HOME_DIR / 'models'
6869
_DEFAULT_DB_DIR = _HOME_DIR / 'public_databases'
6970

71+
72+
@enum.unique
73+
class JaxBackend(enum.StrEnum):
74+
CPU = enum.auto()
75+
GPU = enum.auto()
76+
77+
7078
# Input and output paths.
7179
_JSON_PATH = epath.DEFINE_path(
7280
'json_path',
@@ -324,13 +332,17 @@ def _num_cpus_for_msa_tools() -> int:
324332
' pre-filtered by the environment (e.g. by using CUDA_VISIBLE_DEVICES),'
325333
' this flag refers to the GPU index after the filtering has been done.',
326334
)
327-
_USE_CPU_ONLY = flags.DEFINE_bool(
328-
'use_cpu_only',
329-
False,
330-
'If True, use CPU only for inference. This is much slower than using a GPU,'
331-
' but can be useful for testing or running on systems without a GPU'
332-
' supported by JAX. If you set this flag, you must also set'
333-
' --flash_attention_implementation=xla.',
335+
_JAX_BACKEND = flags.DEFINE_enum_class(
336+
'jax_backend',
337+
default=JaxBackend.GPU,
338+
enum_class=JaxBackend,
339+
help=(
340+
'JAX backend to use. "gpu" uses a GPU for inference. "cpu" uses a CPU'
341+
' only for inference. This is much slower than using a GPU, but can be'
342+
' useful for testing or running on systems without a GPU supported by'
343+
' JAX. If you set this flag to "cpu", you must also set'
344+
' --flash_attention_implementation=xla.'
345+
),
334346
)
335347
_BUCKETS = flags.DEFINE_list(
336348
'buckets',
@@ -922,13 +934,13 @@ def main(_):
922934

923935
if _RUN_INFERENCE.value:
924936
# Fail early on incompatible devices, but only if we're running inference.
925-
if _USE_CPU_ONLY.value:
937+
if _JAX_BACKEND.value == JaxBackend.CPU:
926938
if _FLASH_ATTENTION_IMPLEMENTATION.value != 'xla':
927939
raise ValueError(
928940
'For CPU-only inference, the --flash_attention_implementation must'
929941
' be set to "xla".'
930942
)
931-
else:
943+
elif _JAX_BACKEND.value == JaxBackend.GPU:
932944
gpu_devices = jax.local_devices(backend='gpu')
933945
if gpu_devices:
934946
compute_capability = float(
@@ -956,6 +968,8 @@ def main(_):
956968
' https://developer.nvidia.com/cuda-gpus) the'
957969
' --flash_attention_implementation must be set to "xla".'
958970
)
971+
else:
972+
raise ValueError(f'Unsupported JAX backend: {_JAX_BACKEND.value}')
959973

960974
notice = textwrap.wrap(
961975
'Running AlphaFold 3. Please note that standard AlphaFold 3 model'
@@ -1007,17 +1021,18 @@ def main(_):
10071021
data_pipeline_config = None
10081022

10091023
if _RUN_INFERENCE.value:
1010-
if _USE_CPU_ONLY.value:
1011-
devices = jax.local_devices(backend='cpu')
1024+
devices = jax.local_devices(backend=_JAX_BACKEND.value)
1025+
if _JAX_BACKEND.value == JaxBackend.CPU:
10121026
device = devices[0]
10131027
print(f'Found local CPU devices: {devices}, using device 0: {device}')
1014-
else:
1015-
devices = jax.local_devices(backend='gpu')
1028+
elif _JAX_BACKEND.value == JaxBackend.GPU:
10161029
print(
10171030
f'Found local GPU devices: {devices}, using device '
10181031
f'{_GPU_DEVICE.value}: {devices[_GPU_DEVICE.value]}'
10191032
)
10201033
device = devices[_GPU_DEVICE.value]
1034+
else:
1035+
raise ValueError(f'Unsupported JAX backend: {_JAX_BACKEND.value}')
10211036

10221037
print('Building model from scratch...')
10231038
model_runner = ModelRunner(

0 commit comments

Comments
 (0)