Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Resolve #19 by fixing broken import #20

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions interpol/autograd.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
grid_grad, grid_grad_backward)
from .utils import fake_decorator
try:
from torch.cuda.amp import custom_fwd, custom_bwd
from torch.amp import custom_fwd, custom_bwd
except (ModuleNotFoundError, ImportError):
custom_fwd = custom_bwd = fake_decorator

# Use cuda if available
device_type = 'cuda' if torch.cuda.is_available() else 'cpu'


def make_list(x):
if not isinstance(x, (list, tuple)):
Expand Down Expand Up @@ -125,7 +128,7 @@ def inter_to_nitorch(inter, as_type='str'):
class GridPull(torch.autograd.Function):

@staticmethod
@custom_fwd(cast_inputs=torch.float32)
@custom_fwd(cast_inputs=torch.float32, device_type=device_type)
def forward(ctx, input, grid, interpolation, bound, extrapolate):

bound = bound_to_nitorch(make_list(bound), as_type='int')
Expand All @@ -143,7 +146,7 @@ def forward(ctx, input, grid, interpolation, bound, extrapolate):
return output

@staticmethod
@custom_bwd
@custom_bwd(device_type=device_type)
def backward(ctx, grad):
var = ctx.saved_tensors
opt = ctx.opt
Expand All @@ -155,7 +158,7 @@ def backward(ctx, grad):
class GridPush(torch.autograd.Function):

@staticmethod
@custom_fwd(cast_inputs=torch.float32)
@custom_fwd(cast_inputs=torch.float32, device_type=device_type)
def forward(ctx, input, grid, shape, interpolation, bound, extrapolate):

bound = bound_to_nitorch(make_list(bound), as_type='int')
Expand All @@ -173,7 +176,7 @@ def forward(ctx, input, grid, shape, interpolation, bound, extrapolate):
return output

@staticmethod
@custom_bwd
@custom_bwd(device_type=device_type)
def backward(ctx, grad):
var = ctx.saved_tensors
opt = ctx.opt
Expand All @@ -185,7 +188,7 @@ def backward(ctx, grad):
class GridCount(torch.autograd.Function):

@staticmethod
@custom_fwd(cast_inputs=torch.float32)
@custom_fwd(cast_inputs=torch.float32, device_type=device_type)
def forward(ctx, grid, shape, interpolation, bound, extrapolate):

bound = bound_to_nitorch(make_list(bound), as_type='int')
Expand All @@ -203,7 +206,7 @@ def forward(ctx, grid, shape, interpolation, bound, extrapolate):
return output

@staticmethod
@custom_bwd
@custom_bwd(device_type=device_type)
def backward(ctx, grad):
var = ctx.saved_tensors
opt = ctx.opt
Expand All @@ -216,7 +219,7 @@ def backward(ctx, grad):
class GridGrad(torch.autograd.Function):

@staticmethod
@custom_fwd(cast_inputs=torch.float32)
@custom_fwd(cast_inputs=torch.float32, device_type=device_type)
def forward(ctx, input, grid, interpolation, bound, extrapolate):

bound = bound_to_nitorch(make_list(bound), as_type='int')
Expand All @@ -234,7 +237,7 @@ def forward(ctx, input, grid, interpolation, bound, extrapolate):
return output

@staticmethod
@custom_bwd
@custom_bwd(device_type=device_type)
def backward(ctx, grad):
var = ctx.saved_tensors
opt = ctx.opt
Expand All @@ -248,7 +251,7 @@ def backward(ctx, grad):
class SplineCoeff(torch.autograd.Function):

@staticmethod
@custom_fwd
@custom_fwd(device_type=device_type)
def forward(ctx, input, bound, interpolation, dim, inplace):

bound = bound_to_nitorch(make_list(bound)[0], as_type='int')
Expand All @@ -265,7 +268,7 @@ def forward(ctx, input, bound, interpolation, dim, inplace):
return output

@staticmethod
@custom_bwd
@custom_bwd(device_type=device_type)
def backward(ctx, grad):
# symmetric filter -> backward == forward
# (I don't know if I can write into grad, so inplace=False to be safe)
Expand All @@ -276,7 +279,7 @@ def backward(ctx, grad):
class SplineCoeffND(torch.autograd.Function):

@staticmethod
@custom_fwd
@custom_fwd(device_type=device_type)
def forward(ctx, input, bound, interpolation, dim, inplace):

bound = bound_to_nitorch(make_list(bound), as_type='int')
Expand All @@ -293,7 +296,7 @@ def forward(ctx, input, bound, interpolation, dim, inplace):
return output

@staticmethod
@custom_bwd
@custom_bwd(device_type=device_type)
def backward(ctx, grad):
# symmetric filter -> backward == forward
# (I don't know if I can write into grad, so inplace=False to be safe)
Expand Down