Do we really need Module.Init?
wangkuiyi opened this issue · 1 comments
From the definition as the following, I understand the only purpose for user-defined modules to call Module.Init
in their newers is to let each sub-module know about its parent.
Lines 46 to 64 in 047d424
Is this purpose due to the requirement that when the user calls a module's To
or ZeroGrad
method, we can trace up to the top ancestor of the sub-module hierarchy and make sure that all modules in the hierarchy move to the specified device or have all parameter gradients cleared?
If this is the reasoning behind Module.Init
, I am afraid that the implementation of To
and ZeroGrad
are not tracing up to the root; instead, I see them simply call m.outer
.
Lines 97 to 101 in 047d424
Oh. I got it -- I misunderstood the design.
The purpose of Module.Init
is to store the address of the newly instantiated user-defined module, say BatchNormModule
, into its first field, Module
, as the outer
field. Later, when users call BatchNormModule.To("cuda")
, they actually call Module.To
, which could follow Module.outer
to know the address of the BatchNormModule
instance and recursively move all tensor-typed fields and submodules to the specified device "cuda".