Spaces:
Sleeping
Sleeping
Update DNABERT_Local/bert_layers.py
Browse files- DNABERT_Local/bert_layers.py +11 -6
DNABERT_Local/bert_layers.py
CHANGED
|
@@ -389,18 +389,23 @@ class BertEncoder(nn.Module):
|
|
| 389 |
slopes_b = slopes_b[0::2][:n_heads - closest_power_of_2]
|
| 390 |
return slopes_a + slopes_b
|
| 391 |
|
| 392 |
-
|
| 393 |
-
|
|
|
|
|
|
|
|
|
|
| 394 |
relative_position = torch.abs(memory_position - context_position)
|
|
|
|
| 395 |
# [n_heads, max_token_length, max_token_length]
|
| 396 |
-
relative_position = relative_position.unsqueeze(0).expand(
|
| 397 |
-
|
| 398 |
-
slopes = torch.
|
| 399 |
alibi = slopes.unsqueeze(1).unsqueeze(1) * -relative_position
|
|
|
|
| 400 |
# [1, n_heads, max_token_length, max_token_length]
|
| 401 |
alibi = alibi.unsqueeze(0)
|
| 402 |
assert alibi.shape == torch.Size([1, n_heads, size, size])
|
| 403 |
-
|
| 404 |
self._current_alibi_size = size
|
| 405 |
self.alibi = alibi
|
| 406 |
|
|
|
|
| 389 |
slopes_b = slopes_b[0::2][:n_heads - closest_power_of_2]
|
| 390 |
return slopes_a + slopes_b
|
| 391 |
|
| 392 |
+
# 🚨 THE ZEROGPU CPU-LOCK PATCH 🚨
|
| 393 |
+
# We force all ALiBi math to strictly use the CPU, completely bypassing the "meta" device collisions.
|
| 394 |
+
# The forward() function will automatically move this to the A100 GPU later when needed!
|
| 395 |
+
context_position = torch.arange(size, device='cpu')[:, None]
|
| 396 |
+
memory_position = torch.arange(size, device='cpu')[None, :]
|
| 397 |
relative_position = torch.abs(memory_position - context_position)
|
| 398 |
+
|
| 399 |
# [n_heads, max_token_length, max_token_length]
|
| 400 |
+
relative_position = relative_position.unsqueeze(0).expand(n_heads, -1, -1)
|
| 401 |
+
|
| 402 |
+
slopes = torch.tensor(_get_alibi_head_slopes(n_heads), dtype=torch.float32, device='cpu')
|
| 403 |
alibi = slopes.unsqueeze(1).unsqueeze(1) * -relative_position
|
| 404 |
+
|
| 405 |
# [1, n_heads, max_token_length, max_token_length]
|
| 406 |
alibi = alibi.unsqueeze(0)
|
| 407 |
assert alibi.shape == torch.Size([1, n_heads, size, size])
|
| 408 |
+
|
| 409 |
self._current_alibi_size = size
|
| 410 |
self.alibi = alibi
|
| 411 |
|