Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
ICV-mmengine_basecode
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Florian Schiffel
ICV-mmengine_basecode
Commits
dc931fd2
Unverified
Commit
dc931fd2
authored
1 year ago
by
Mashiro
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
[Fix] Initialize nested modules in ddp which define 'init_weights' method (#1045)
parent
fd84c210
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
mmengine/model/base_module.py
+3
-0
3 additions, 0 deletions
mmengine/model/base_module.py
tests/test_model/test_base_module.py
+17
-1
17 additions, 1 deletion
tests/test_model/test_base_module.py
with
20 additions
and
1 deletion
mmengine/model/base_module.py
+
3
−
0
View file @
dc931fd2
...
...
@@ -11,6 +11,7 @@ import torch.nn as nn
from
mmengine.dist
import
master_only
from
mmengine.logging
import
MMLogger
,
print_log
from
.weight_init
import
initialize
,
update_init_info
from
.wrappers.utils
import
is_model_wrapper
class
BaseModule
(
nn
.
Module
,
metaclass
=
ABCMeta
):
...
...
@@ -123,6 +124,8 @@ class BaseModule(nn.Module, metaclass=ABCMeta):
initialize
(
self
,
other_cfgs
)
for
m
in
self
.
children
():
if
is_model_wrapper
(
m
)
and
not
hasattr
(
m
,
'
init_weights
'
):
m
=
m
.
module
if
hasattr
(
m
,
'
init_weights
'
):
m
.
init_weights
()
# users may overload the `init_weights`
...
...
This diff is collapsed.
Click to expand it.
tests/test_model/test_base_module.py
+
17
−
1
View file @
dc931fd2
...
...
@@ -4,6 +4,7 @@ import logging
import
os.path
as
osp
import
tempfile
from
unittest
import
TestCase
from
unittest.mock
import
patch
import
torch
from
torch
import
nn
...
...
@@ -145,7 +146,6 @@ class TestBaseModule(TestCase):
├──conv1d (FooConv1d, weight=3, bias=4)
├──reg (nn.Linear, weight=1, bias=2)
"""
self
.
model
.
init_weights
()
assert
torch
.
equal
(
...
...
@@ -222,6 +222,22 @@ class TestBaseModule(TestCase):
self
.
assertTrue
((
ori_layer_weight
!=
model
.
linear
.
linear
.
weight
).
any
())
self
.
assertTrue
((
ori_layer_bias
!=
model
.
linear
.
linear
.
bias
).
any
())
class
FakeDDP
(
nn
.
Module
):
def
__init__
(
self
,
module
)
->
None
:
super
().
__init__
()
self
.
module
=
module
# Test initialization of nested modules in DDPModule which define
# `init_weights`.
with
patch
(
'
mmengine.model.base_module.is_model_wrapper
'
,
lambda
x
:
isinstance
(
x
,
FakeDDP
)):
model
=
FOOMODELS
.
build
(
model_cfg
)
model
.
ddp
=
FakeDDP
(
CustomLinear
())
model
.
init_weights
()
self
.
assertTrue
((
model
.
ddp
.
module
.
linear
.
weight
==
1
).
all
())
self
.
assertTrue
((
model
.
ddp
.
module
.
linear
.
bias
==
2
).
all
())
def
test_dump_init_info
(
self
):
import
os
import
shutil
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment