|
32 | 32 | import csv |
33 | 33 | import dataclasses |
34 | 34 | import datetime |
| 35 | +import enum |
35 | 36 | import functools |
36 | 37 | import io |
37 | 38 | import os |
|
67 | 68 | _DEFAULT_MODEL_DIR = _HOME_DIR / 'models' |
68 | 69 | _DEFAULT_DB_DIR = _HOME_DIR / 'public_databases' |
69 | 70 |
|
| 71 | + |
| 72 | +@enum.unique |
| 73 | +class JaxBackend(enum.StrEnum): |
| 74 | + CPU = enum.auto() |
| 75 | + GPU = enum.auto() |
| 76 | + |
| 77 | + |
70 | 78 | # Input and output paths. |
71 | 79 | _JSON_PATH = epath.DEFINE_path( |
72 | 80 | 'json_path', |
@@ -324,13 +332,17 @@ def _num_cpus_for_msa_tools() -> int: |
324 | 332 | ' pre-filtered by the environment (e.g. by using CUDA_VISIBLE_DEVICES),' |
325 | 333 | ' this flag refers to the GPU index after the filtering has been done.', |
326 | 334 | ) |
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 | + ), |
334 | 346 | ) |
335 | 347 | _BUCKETS = flags.DEFINE_list( |
336 | 348 | 'buckets', |
@@ -922,13 +934,13 @@ def main(_): |
922 | 934 |
|
923 | 935 | if _RUN_INFERENCE.value: |
924 | 936 | # 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: |
926 | 938 | if _FLASH_ATTENTION_IMPLEMENTATION.value != 'xla': |
927 | 939 | raise ValueError( |
928 | 940 | 'For CPU-only inference, the --flash_attention_implementation must' |
929 | 941 | ' be set to "xla".' |
930 | 942 | ) |
931 | | - else: |
| 943 | + elif _JAX_BACKEND.value == JaxBackend.GPU: |
932 | 944 | gpu_devices = jax.local_devices(backend='gpu') |
933 | 945 | if gpu_devices: |
934 | 946 | compute_capability = float( |
@@ -956,6 +968,8 @@ def main(_): |
956 | 968 | ' https://developer.nvidia.com/cuda-gpus) the' |
957 | 969 | ' --flash_attention_implementation must be set to "xla".' |
958 | 970 | ) |
| 971 | + else: |
| 972 | + raise ValueError(f'Unsupported JAX backend: {_JAX_BACKEND.value}') |
959 | 973 |
|
960 | 974 | notice = textwrap.wrap( |
961 | 975 | 'Running AlphaFold 3. Please note that standard AlphaFold 3 model' |
@@ -1007,17 +1021,18 @@ def main(_): |
1007 | 1021 | data_pipeline_config = None |
1008 | 1022 |
|
1009 | 1023 | 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: |
1012 | 1026 | device = devices[0] |
1013 | 1027 | 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: |
1016 | 1029 | print( |
1017 | 1030 | f'Found local GPU devices: {devices}, using device ' |
1018 | 1031 | f'{_GPU_DEVICE.value}: {devices[_GPU_DEVICE.value]}' |
1019 | 1032 | ) |
1020 | 1033 | device = devices[_GPU_DEVICE.value] |
| 1034 | + else: |
| 1035 | + raise ValueError(f'Unsupported JAX backend: {_JAX_BACKEND.value}') |
1021 | 1036 |
|
1022 | 1037 | print('Building model from scratch...') |
1023 | 1038 | model_runner = ModelRunner( |
|
0 commit comments