Skip to content

Commit 7ecd64f

Browse files
Augustin-Zidekcopybara-github
authored andcommitted
Add a flag to run on CPU only
PiperOrigin-RevId: 949000038 Change-Id: I14871ff35a313a8e97b2d24bd92e8b2931b8e97a
1 parent 27f4d6d commit 7ecd64f

1 file changed

Lines changed: 49 additions & 26 deletions

File tree

run_alphafold.py

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,14 @@ def _num_cpus_for_msa_tools() -> int:
324324
' pre-filtered by the environment (e.g. by using CUDA_VISIBLE_DEVICES),'
325325
' this flag refers to the GPU index after the filtering has been done.',
326326
)
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.',
334+
)
327335
_BUCKETS = flags.DEFINE_list(
328336
'buckets',
329337
# pyformat: disable
@@ -914,31 +922,40 @@ def main(_):
914922

915923
if _RUN_INFERENCE.value:
916924
# Fail early on incompatible devices, but only if we're running inference.
917-
gpu_devices = jax.local_devices(backend='gpu')
918-
if gpu_devices:
919-
compute_capability = float(
920-
gpu_devices[_GPU_DEVICE.value].compute_capability
921-
)
922-
if compute_capability < 6.0:
925+
if _USE_CPU_ONLY.value:
926+
if _FLASH_ATTENTION_IMPLEMENTATION.value != 'xla':
923927
raise ValueError(
924-
'AlphaFold 3 requires at least GPU compute capability 6.0 (see'
925-
' https://developer.nvidia.com/cuda-gpus).'
928+
'For CPU-only inference, the --flash_attention_implementation must'
929+
' be set to "xla".'
930+
)
931+
else:
932+
gpu_devices = jax.local_devices(backend='gpu')
933+
if gpu_devices:
934+
compute_capability = float(
935+
gpu_devices[_GPU_DEVICE.value].compute_capability
926936
)
927-
elif 7.0 <= compute_capability < 8.0:
928-
xla_flags = os.environ.get('XLA_FLAGS')
929-
required_flag = '--xla_disable_hlo_passes=custom-kernel-fusion-rewriter'
930-
if not xla_flags or required_flag not in xla_flags:
937+
if compute_capability < 6.0:
931938
raise ValueError(
932-
'For devices with GPU compute capability 7.x (see'
933-
' https://developer.nvidia.com/cuda-gpus) the ENV XLA_FLAGS must'
934-
f' include "{required_flag}".'
939+
'AlphaFold 3 requires at least GPU compute capability 6.0 (see'
940+
' https://developer.nvidia.com/cuda-gpus).'
935941
)
936-
if _FLASH_ATTENTION_IMPLEMENTATION.value != 'xla':
937-
raise ValueError(
938-
'For devices with GPU compute capability 7.x (see'
939-
' https://developer.nvidia.com/cuda-gpus) the'
940-
' --flash_attention_implementation must be set to "xla".'
942+
elif 7.0 <= compute_capability < 8.0:
943+
xla_flags = os.environ.get('XLA_FLAGS')
944+
required_flag = (
945+
'--xla_disable_hlo_passes=custom-kernel-fusion-rewriter'
941946
)
947+
if not xla_flags or required_flag not in xla_flags:
948+
raise ValueError(
949+
'For devices with GPU compute capability 7.x (see'
950+
' https://developer.nvidia.com/cuda-gpus) the ENV XLA_FLAGS'
951+
f' must include "{required_flag}".'
952+
)
953+
if _FLASH_ATTENTION_IMPLEMENTATION.value != 'xla':
954+
raise ValueError(
955+
'For devices with GPU compute capability 7.x (see'
956+
' https://developer.nvidia.com/cuda-gpus) the'
957+
' --flash_attention_implementation must be set to "xla".'
958+
)
942959

943960
notice = textwrap.wrap(
944961
'Running AlphaFold 3. Please note that standard AlphaFold 3 model'
@@ -990,11 +1007,17 @@ def main(_):
9901007
data_pipeline_config = None
9911008

9921009
if _RUN_INFERENCE.value:
993-
devices = jax.local_devices(backend='gpu')
994-
print(
995-
f'Found local devices: {devices}, using device {_GPU_DEVICE.value}:'
996-
f' {devices[_GPU_DEVICE.value]}'
997-
)
1010+
if _USE_CPU_ONLY.value:
1011+
devices = jax.local_devices(backend='cpu')
1012+
device = devices[0]
1013+
print(f'Found local CPU devices: {devices}, using device 0: {device}')
1014+
else:
1015+
devices = jax.local_devices(backend='gpu')
1016+
print(
1017+
f'Found local GPU devices: {devices}, using device '
1018+
f'{_GPU_DEVICE.value}: {devices[_GPU_DEVICE.value]}'
1019+
)
1020+
device = devices[_GPU_DEVICE.value]
9981021

9991022
print('Building model from scratch...')
10001023
model_runner = ModelRunner(
@@ -1008,7 +1031,7 @@ def main(_):
10081031
return_embeddings=_SAVE_EMBEDDINGS.value,
10091032
return_distogram=_SAVE_DISTOGRAM.value,
10101033
),
1011-
device=devices[_GPU_DEVICE.value],
1034+
device=device,
10121035
model_dir=MODEL_DIR.value,
10131036
)
10141037
# Check we can load the model parameters before launching anything.

0 commit comments

Comments
 (0)