Skip to content

Commit

Permalink
Fix custom fwd and bwd for older PyTorch versions (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
KokeCacao authored Oct 27, 2024
1 parent 83a5c90 commit 2017c98
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions mamba_ssm/utils/torch.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import torch
from functools import partial
from typing import Callable


def custom_amp_decorator(dec, cuda_amp_deprecated):
def decorator(func):
return dec(func) if not cuda_amp_deprecated else partial(dec, func, device_type="cuda")
def custom_amp_decorator(dec: Callable, cuda_amp_deprecated: bool):
def decorator(*args, **kwargs):
if cuda_amp_deprecated:
kwargs["device_type"] = "cuda"
return dec(*args, **kwargs)
return decorator


if hasattr(torch.amp, "custom_fwd"):
if hasattr(torch.amp, "custom_fwd"): # type: ignore[attr-defined]
deprecated = True
from torch.amp import custom_fwd, custom_bwd
from torch.amp import custom_fwd, custom_bwd # type: ignore[attr-defined]
else:
deprecated = False
from torch.cuda.amp import custom_fwd, custom_bwd
Expand Down

0 comments on commit 2017c98

Please sign in to comment.