Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 8 additions & 8 deletions lightstream/core/scnn/scnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from dataclasses import dataclass
from typing import List

import numpy as np
import torch
import torch.autograd
import torch.backends
Expand Down Expand Up @@ -331,6 +330,7 @@ def _slice_reducer_mask(

def _configure(self):
# Save current model and cudnn flags, since we need to change them and restore later
original_device = self.device
state_dict = self._save_parameters()
(
old_deterministic_flag,
Expand Down Expand Up @@ -362,8 +362,8 @@ def _configure(self):

# TODO; temp hack for tile sizes too big on gpu,
if self.statistics_on_cpu:
self.stream_module = self.stream_module.cuda()
self.device = torch.device("cuda") # type:ignore
self.stream_module = self.stream_module.to(original_device)
self.device = original_device

# Remove all hooks and add hooks for correcting gradients
# during lightstream
Expand Down Expand Up @@ -2335,15 +2335,15 @@ def _backward_gather_statistics_hook(self, module, grad_in, grad_out):

f_grad = torch.sum(grad_out[0], dim=1)[0]
f_grad = f_grad * new_outpt
f_grad = f_grad.cpu()
f_grad = np.repeat(f_grad, stride[1], axis=0)
f_grad = np.repeat(f_grad, stride[2], axis=1)
grad = np.zeros(grad_in[0].shape[2:])
f_grad = torch.repeat_interleave(f_grad, int(stride[1]), dim=0)
f_grad = torch.repeat_interleave(f_grad, int(stride[2]), dim=1)
grad = torch.zeros(
grad_in[0].shape[2:], device=f_grad.device, dtype=f_grad.dtype
)

self._print_verbose("testing shape gradient fix")
grad[: f_grad.shape[0], : f_grad.shape[1]] = f_grad[: grad.shape[0], : grad.shape[1]]

f_grad = torch.from_numpy(grad)
f_grad = f_grad.to(self.device)

if grad_out[0].numel() == 0 or torch.count_nonzero(grad_out[0]) == 0:
Expand Down
17 changes: 13 additions & 4 deletions lightstream/modules/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
from .lightningstreaming import LightningStreamingModule
from .imagenet_template import ImageNetClassifier


__all__ = [
"LightningStreamingModule",
"ImageNetClassifier",
]


def __getattr__(name):
"""Load Lightning-dependent convenience classes only when requested."""
if name == "LightningStreamingModule":
from .lightningstreaming import LightningStreamingModule

return LightningStreamingModule
if name == "ImageNetClassifier":
from .imagenet_template import ImageNetClassifier

return ImageNetClassifier
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")