Skip to content

Commit be2ec18

Browse files
authored
Merge pull request #560 from borisfom/fix-cueq-scaling
Fixed cuequivariance external fallback and scaling
2 parents 56da08e + 6839eaf commit be2ec18

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

openfold/model/primitives.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ def __init__(
370370
c_hidden: int,
371371
no_heads: int,
372372
gating: bool = True,
373+
inf:float = 1e9,
373374
):
374375
"""
375376
Args:
@@ -394,6 +395,7 @@ def __init__(
394395
self.c_hidden = c_hidden
395396
self.no_heads = no_heads
396397
self.gating = gating
398+
self.inf = inf
397399

398400
# DISCREPANCY: c_hidden is not the per-head channel dimension, as
399401
# stated in the supplement, but the overall channel dimension.
@@ -539,12 +541,23 @@ def forward(
539541
if biases is None:
540542
biases = []
541543

542-
# DeepSpeed attention kernel applies scaling internally
543-
q, k, v = self._prep_qkv(q_x, kv_x,
544-
apply_scale=not use_deepspeed_evo_attention or use_cuequivariance_attention)
545-
546544
if is_fp16_enabled():
547545
use_memory_efficient_kernel = False
546+
547+
if use_cuequivariance_attention:
548+
# cuEquivariance -> Torch fallback for small sequence length and some shapes
549+
if cueq_would_fall_back(q_x.shape[-2], q_x.shape[-1] // self.no_heads, q_x.dtype):
550+
# convert the mask from boolean to float pre-mul
551+
biases[0] = (self.inf * (biases[0] - 1))
552+
use_cuequivariance_attention = False
553+
554+
# The EvoformerAttention kernel can only be used for sequence lengths > 16
555+
if use_deepspeed_evo_attention and q_x.shape[-2] <= 16:
556+
use_deepspeed_evo_attention = False
557+
558+
# DeepSpeed attention kernel applies scaling internally
559+
q, k, v = self._prep_qkv(q_x, kv_x,
560+
apply_scale = not (use_deepspeed_evo_attention or use_cuequivariance_attention))
548561

549562
# cuequivariance kernel takes precedence over use_deepspeed_evo_attention
550563
if use_cuequivariance_attention:

0 commit comments

Comments
 (0)