Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist training attribute #633

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions R/script_module.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ nn_ScriptModule <- R6::R6Class(
env = private
)

rm(list = "training", envir = self)
makeActiveBinding(
"training",
fun = function() {
cpp_jit_script_module_is_training(private$ptr)
},
env = self
)

},
register_parameter = function(name, param) {
private$ptr$register_parameter(name, param)
Expand All @@ -133,6 +142,10 @@ nn_ScriptModule <- R6::R6Class(
},
..ptr.. = function() {
private$ptr
},
train = function(mode = TRUE) {
private$ptr$train(mode = mode)
invisible(create_nn_module_callable(self))
}
),
private = list(
Expand Down
1 change: 1 addition & 0 deletions R/trace.R
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ create_script_module <- function(mod) {
module$register_module(name, create_script_module(child))
})

module$train(mod$training)

# Let's not keep the constants in the module right now as it might cause more
# problems than benefits. In pytorch they are only added if their name is in
Expand Down
8 changes: 4 additions & 4 deletions src/script_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ XPtrTorchjit_named_buffer_list cpp_jit_script_module_buffers (XPtrTorchScriptMod
// [[Rcpp::export]]
void cpp_jit_script_module_train (XPtrTorchScriptModule self, bool on)
{
_lantern_ScriptModule_train(self.get(), on);
lantern_ScriptModule_train(self.get(), on);
}

// [[Rcpp::export]]
void cpp_jit_script_module_set_optimized (XPtrTorchScriptModule self, bool on)
{
_lantern_ScriptModule_set_optimized(self.get(), on);
lantern_ScriptModule_set_optimized(self.get(), on);
}

// [[Rcpp::export]]
bool cpp_jit_script_module_is_training (XPtrTorchScriptModule self)
{
return _lantern_ScriptModule_is_training(self.get());
return lantern_ScriptModule_is_training(self.get());
}

// [[Rcpp::export]]
bool cpp_jit_script_module_is_optimized (XPtrTorchScriptModule self)
{
return _lantern_ScriptModule_is_optimized(self.get());
return lantern_ScriptModule_is_optimized(self.get());
}

// [[Rcpp::export]]
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-script_module.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ test_that("can print the graph", {

})

test_that("training attribute is persisted", {
model <- nn_sequential(
nn_linear(10, 10),
nn_relu(),
nn_dropout(),
nn_linear(10, 1)
)

model$eval()
model$training
test_model <- jit_trace(model, torch_randn(10, 10))

expect_true(!test_model$training)
expect_true(!test_model[["0"]]$training)
})

test_that("graph_for", {
testthat::local_edition(3)

Expand All @@ -141,4 +157,5 @@ test_that("graph_for", {
expect_snapshot_output({
traced$graph_for(torch_randn(10, 10))
})

})