Skip to content

Commit b8dfa48

Browse files
authored
Merge pull request #375 from bigict/xpu
refactor: code reformat
2 parents 6573080 + 6fd91d1 commit b8dfa48

5 files changed

Lines changed: 29 additions & 27 deletions

File tree

profold2/command/evaluator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def data_eval(idx, batch):
7070
# predict - out is (batch, L * 3, 3)
7171
with timing(f'Running model on {fasta_name} {fasta_len}', logging.debug):
7272
with torch.no_grad():
73-
with worker.autocast_ctx(args.amp_enabled):
73+
with accelerator.amp(args.amp_enabled): # Automatic Mixed Precision
7474
r = ReturnValues(
7575
**model(
7676
batch=batch, # pylint: disable=not-callable

profold2/command/predictor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from profold2.data import dataset
2020
from profold2.data.dataset import ProteinSequenceDataset
2121
from profold2.data.parsers import parse_fasta
22-
from profold2.data.utils import parse_seq_index, pdb_from_prediction, str_seq_index
22+
from profold2.data.utils import pdb_from_prediction
2323
from profold2.model import accelerator, profiler, snapshot, FeatureBuilder, ReturnValues
2424
from profold2.utils import exists, timing
2525

@@ -222,7 +222,7 @@ def process_structure(model_name, pdb_str, plddt=None):
222222
timing_callback, timings, f'predict_{model_name}'
223223
)
224224
):
225-
with worker.autocast_ctx(args.amp_enabled):
225+
with accelerator.amp(args.amp_enabled): # Automatic Mixed Precision
226226
r = ReturnValues(
227227
**model(
228228
batch=feats,

profold2/command/trainer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def _step(data_loader, it, writer, stage='train', batch_callback=None):
352352
with no_sync_ctx(
353353
it != global_step and jt + 1 != args.gradient_accumulate_every, model
354354
):
355-
with worker.autocast_ctx(grad_scaler.is_enabled()):
355+
with accelerator.amp(grad_scaler.is_enabled()): # Automatic Mixed Precision
356356
r = ReturnValues(
357357
**model(
358358
batch=batch,

profold2/command/worker.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Wrap distibuted env
22
"""
33
import os
4-
import contextlib
54
import functools
65
import logging
76
from logging.handlers import QueueHandler, QueueListener
@@ -14,24 +13,7 @@
1413

1514
from profold2.model import accelerator, AlphaFold2
1615
from profold2.model.commons import torch_allow_tf32
17-
from profold2.utils import default, env, exists, version_cmp
18-
19-
20-
@contextlib.contextmanager
21-
def autocast_ctx(cond):
22-
if cond:
23-
dtype = torch.float16
24-
if hasattr(torch.cuda, 'is_bf16_supported'):
25-
if torch.cuda.is_bf16_supported():
26-
dtype = torch.bfloat16
27-
ctx = functools.partial(accelerator.autocast, dtype=dtype)
28-
# FIXED ME: cache_enabled=True will crash :(
29-
if version_cmp(torch.__version__, '1.10.0') >= 0:
30-
ctx = functools.partial(ctx, cache_enabled=False)
31-
with ctx():
32-
yield
33-
else:
34-
yield
16+
from profold2.utils import exists, version_cmp
3517

3618

3719
class _WorkerLogRecordFactory(object):

profold2/model/accelerator.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,51 @@
11
"""Wrap GPU, MLU etc
22
"""
3+
import contextlib
34
from datetime import timedelta
45
import functools
56
import logging
67
from typing import Optional
78

89
import torch
910

10-
from profold2.utils import default, env
11+
from profold2.utils import default, env, version_cmp
1112

1213

1314
def device_count():
1415
return torch.cuda.device_count()
1516

17+
1618
def device_type():
1719
if torch.cuda.is_available():
1820
return 'cuda'
1921
return 'cpu'
2022

23+
2124
def world_size(nnodes: Optional[int] = None):
2225
return env('WORLD_SIZE', defval=device_count() * default(nnodes, 1), dtype=int)
2326

27+
2428
autocast = functools.partial(torch.amp.autocast, device_type())
2529
GradScaler = functools.partial(torch.amp.GradScaler, device_type())
2630

31+
32+
@contextlib.contextmanager
33+
def amp(enabled: bool = True):
34+
if enabled:
35+
dtype = torch.float16
36+
if hasattr(torch.cuda, 'is_bf16_supported'):
37+
if torch.cuda.is_bf16_supported():
38+
dtype = torch.bfloat16
39+
ctx = functools.partial(autocast, dtype=dtype)
40+
# FIXED ME: cache_enabled=True will crash :(
41+
if version_cmp(torch.__version__, '1.10.0') >= 0:
42+
ctx = functools.partial(ctx, cache_enabled=False)
43+
with ctx():
44+
yield
45+
else:
46+
yield
47+
48+
2749
class XPU(object):
2850
"""Wrap distibuted XPU(GPU,MLU etc)
2951
"""
@@ -51,9 +73,7 @@ def device(self):
5173
@property
5274
def rank(self):
5375
return env(
54-
'RANK',
55-
defval=device_count() * self.node_rank + self.local_rank,
56-
dtype=int
76+
'RANK', defval=device_count() * self.node_rank + self.local_rank, dtype=int
5777
)
5878

5979
def memory_summary(self):

0 commit comments

Comments
 (0)