Open
Description
Because the default of parent_env
for nn_module
is parent.frame()
, defining an nn_module
in an environment that is not a special environment (such as the global environment or a package environment) can lead to exploding object sizes when serializing.
The special environments (global, pkg env) are no problem, because their serialization is handled special anyway.
library(torch)
nn_module1 = nn_module("nn_module1",
initialize = function(data) {
data
self$layer = nn_module("layer",
initialize = function() {
self$linear = nn_linear(10, 1)
},
forward = function(x) {
self$linear(x)
}
)()
},
forward = function(x) {
self$layer(x)
}
)
pryr::object_size(nn_module1(rnorm(1L)))
#> 499.06 kB
pryr::object_size(nn_module1(rnorm(10000000L)))
#> 80.50 MB
net = nn_module1(rnorm(1))
net$layer$forward |> environment() |> parent.env() |> parent.env() |> with(data)
#> [1] 1.308416
Created on 2025-05-28 with reprex v2.1.1
I also think this is not a crazy edge case, but something that can actually happen and some big object (like training data) can cause issues here.
I am not really sure what to do about this.
Metadata
Metadata
Assignees
Labels
No labels