Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
renzhc
diffusers_dcu
Commits
3abf4bc4
Unverified
Commit
3abf4bc4
authored
Jul 04, 2022
by
Tanishq Abraham
Committed by
GitHub
Jul 04, 2022
Browse files
EMA model stepping updated to keep track of current step (#64)
ema model stepping done automatically now
parent
94566e6d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
5 additions
and
3 deletions
+5
-3
examples/train_unconditional.py
examples/train_unconditional.py
+1
-1
src/diffusers/training_utils.py
src/diffusers/training_utils.py
+4
-2
No files found.
examples/train_unconditional.py
View file @
3abf4bc4
...
@@ -130,7 +130,7 @@ def main(args):
...
@@ -130,7 +130,7 @@ def main(args):
torch
.
nn
.
utils
.
clip_grad_norm_
(
model
.
parameters
(),
1.0
)
torch
.
nn
.
utils
.
clip_grad_norm_
(
model
.
parameters
(),
1.0
)
optimizer
.
step
()
optimizer
.
step
()
lr_scheduler
.
step
()
lr_scheduler
.
step
()
ema_model
.
step
(
model
,
global_step
)
ema_model
.
step
(
model
)
optimizer
.
zero_grad
()
optimizer
.
zero_grad
()
progress_bar
.
update
(
1
)
progress_bar
.
update
(
1
)
progress_bar
.
set_postfix
(
progress_bar
.
set_postfix
(
...
...
src/diffusers/training_utils.py
View file @
3abf4bc4
...
@@ -43,6 +43,7 @@ class EMAModel:
...
@@ -43,6 +43,7 @@ class EMAModel:
self
.
averaged_model
=
self
.
averaged_model
.
to
(
device
=
device
)
self
.
averaged_model
=
self
.
averaged_model
.
to
(
device
=
device
)
self
.
decay
=
0.0
self
.
decay
=
0.0
self
.
optimization_step
=
0
def
get_decay
(
self
,
optimization_step
):
def
get_decay
(
self
,
optimization_step
):
"""
"""
...
@@ -57,11 +58,11 @@ class EMAModel:
...
@@ -57,11 +58,11 @@ class EMAModel:
return
max
(
self
.
min_value
,
min
(
value
,
self
.
max_value
))
return
max
(
self
.
min_value
,
min
(
value
,
self
.
max_value
))
@
torch
.
no_grad
()
@
torch
.
no_grad
()
def
step
(
self
,
new_model
,
optimization_step
):
def
step
(
self
,
new_model
):
ema_state_dict
=
{}
ema_state_dict
=
{}
ema_params
=
self
.
averaged_model
.
state_dict
()
ema_params
=
self
.
averaged_model
.
state_dict
()
self
.
decay
=
self
.
get_decay
(
optimization_step
)
self
.
decay
=
self
.
get_decay
(
self
.
optimization_step
)
for
key
,
param
in
new_model
.
named_parameters
():
for
key
,
param
in
new_model
.
named_parameters
():
if
isinstance
(
param
,
dict
):
if
isinstance
(
param
,
dict
):
...
@@ -85,3 +86,4 @@ class EMAModel:
...
@@ -85,3 +86,4 @@ class EMAModel:
ema_state_dict
[
key
]
=
param
ema_state_dict
[
key
]
=
param
self
.
averaged_model
.
load_state_dict
(
ema_state_dict
,
strict
=
False
)
self
.
averaged_model
.
load_state_dict
(
ema_state_dict
,
strict
=
False
)
self
.
optimization_step
+=
1
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment