Skip to content

Commit

Permalink
able to forward methods to ema model from wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
lucidrains committed Jul 22, 2024
1 parent 2c36ca6 commit c427d1b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions ema_pytorch/ema_pytorch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import annotations
from typing import Set, Tuple

from copy import deepcopy
from functools import partial
Expand All @@ -7,8 +8,6 @@
from torch import nn, Tensor
from torch.nn import Module

from typing import Set

def exists(val):
return val is not None

Expand Down Expand Up @@ -60,7 +59,8 @@ def __init__(
ignore_startswith_names: Set[str] = set(),
include_online_model = True, # set this to False if you do not wish for the online model to be saved along with the ema model (managed externally)
allow_different_devices = False, # if the EMA model is on a different device (say CPU), automatically move the tensor
use_foreach = False
use_foreach = False,
forward_method_names: Tuple[str, ...] = ()
):
super().__init__()
self.beta = beta
Expand Down Expand Up @@ -91,6 +91,12 @@ def __init__(
for p in self.ema_model.parameters():
p.detach_()

# forwarding methods

for forward_method_name in forward_method_names:
fn = getattr(self.ema_model, forward_method_name)
setattr(self, forward_method_name, fn)

# parameter and buffer names

self.parameter_names = {name for name, param in self.ema_model.named_parameters() if torch.is_floating_point(param) or torch.is_complex(param)}
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name = 'ema-pytorch',
packages = find_packages(exclude=[]),
version = '0.5.1',
version = '0.5.2',
license='MIT',
description = 'Easy way to keep track of exponential moving average version of your pytorch module',
author = 'Phil Wang',
Expand Down

0 comments on commit c427d1b

Please sign in to comment.