-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add marshalling for 'torch'/'luz' models; TODO: add example and tests […
…#5]
- Loading branch information
1 parent
15dcb0a
commit 168e355
Showing
4 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#' Marshalling of 'torch' objects | ||
#' | ||
#' @param model | ||
#' A `luz_module_fitted` object. | ||
#' | ||
#' @param \dots Not used. | ||
#' | ||
#' @return | ||
#' A `marshalled` object as described in [marshal()]. | ||
#' | ||
#' @details | ||
#' [luz::luz_save()] is used to produce a marshalled version | ||
#' of the original object. | ||
#' [luz::luz_load()] is used to reconstruct a version of the | ||
#' original object from the marshalled object. | ||
#' | ||
#' @rdname marshal.torch | ||
#' @aliases marshal.luz_module_fitted | ||
#' @export | ||
marshal.luz_module_fitted <- function(model, ...) { | ||
raw <- suppressWarnings(local({ | ||
con <- rawConnection(raw(), open = "wb") | ||
on.exit(close(con)) | ||
luz::luz_save(model, con) | ||
rawConnectionValue(con) | ||
})) | ||
|
||
res <- list( | ||
marshalled = raw | ||
) | ||
class(res) <- marshal_class(model) | ||
|
||
## IMPORTANT: We don't want any of the input arguments | ||
## to be part of the unmarshal() environment | ||
rm(list = c("model", names(list(...)))) | ||
|
||
res[["unmarshal"]] <- unmarshal_luz_module_fitted | ||
assert_no_references(res) | ||
res | ||
} | ||
|
||
unmarshal_luz_module_fitted <- function(model, ...) { | ||
object <- model[["marshalled"]] | ||
|
||
res <- local({ | ||
con <- rawConnection(object) | ||
on.exit(close(con)) | ||
luz::luz_load(con) | ||
}) | ||
stopifnot(all.equal(class(res), marshal_unclass(model), check.attributes = FALSE)) | ||
res | ||
} | ||
|
||
|
||
#' @rdname marshal.torch | ||
#' @aliases marshallable.luz_module_fitted | ||
#' @export | ||
marshallable.luz_module_fitted <- function(...) { | ||
TRUE | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.