diff --git a/DESCRIPTION b/DESCRIPTION index d09ab9be6..b4088f0d8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: mirai Type: Package Title: Minimalist Async Evaluation Framework for R -Version: 0.12.1.9000 +Version: 0.12.1.9001 Description: Lightweight parallel code execution and distributed computing. Designed for simplicity, a 'mirai' evaluates an R expression asynchronously, on local or network resources, resolving automatically upon completion. @@ -23,7 +23,7 @@ Encoding: UTF-8 Depends: R (>= 3.5) Imports: - nanonext (>= 0.12.0) + nanonext (>= 0.13.2.9001) Enhances: parallel, promises diff --git a/NEWS.md b/NEWS.md index 2342baf3a..7f1a62183 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,6 @@ -# mirai 0.12.1.9000 (development) +# mirai 0.12.1.9001 (development) + +* `serialization` adds arguments 'class' and 'list' for custom serialisation of all types of reference object / serialization function signature. # mirai 0.12.1 diff --git a/R/daemons.R b/R/daemons.R index 5156969fc..41e5efcad 100644 --- a/R/daemons.R +++ b/R/daemons.R @@ -425,11 +425,18 @@ status <- function(.compute = "default") { #' receiving external pointer reference objects. #' #' @param refhook \strong{either} a list or pairlist of two functions: the -#' signature for the first must accept a list of external pointer type -#' objects and return a raw vector, e.g. \code{torch::torch_serialize}, and -#' the second must accept a raw vector and return a list of external pointer -#' type objects, e.g. \code{torch::torch_load},\cr \strong{or else} NULL to -#' reset. +#' signature for the first must accept a reference object inheriting from +#' 'class' (or a list of such objects) and return a raw vector, and the +#' second must accept a raw vector and return reference objects (or a list +#' of such objects), \cr \strong{or else} NULL to reset. +#' @param class [default ""] a character string representing the class of object +#' that these serialization function will be applied to, e.g. 'ArrowTabular' +#' or 'torch_tensor'. +#' @param list [default FALSE] the serialization functions accept and return +#' reference object individually e.g. \code{arrow::write_to_raw} and +#' \code{arrow::read_ipc_stream}. If TRUE, the serialization functions +#' accept and return a list of reference objects, e.g. +#' \code{torch::torch_serialize} and \code{torch::torch_load}. #' #' @return Invisibly, the pairlist of currently-registered 'refhook' functions. #' A message is printed to the console when functions are successfully @@ -451,10 +458,10 @@ status <- function(.compute = "default") { #' #' @export #' -serialization <- function(refhook = list()) { +serialization <- function(refhook = list(), class = "", list = FALSE) { register <- !missing(refhook) - cfg <- next_config(refhook = refhook) + cfg <- next_config(refhook = refhook, class = class, list = list) if (register) { if (is.list(refhook) && length(refhook) == 2L && is.function(refhook[[1L]]) && is.function(refhook[[2L]])) @@ -462,7 +469,8 @@ serialization <- function(refhook = list()) { if (is.null(refhook)) cat("mirai serialization functions cancelled\n", file = stderr()) else stop(._[["refhook_invalid"]]) - register_everywhere(refhook) + `[[<-`(., "refhook", list(refhook, class, list)) + register_everywhere(refhook = refhook, class = class, list = list) } invisible(cfg) @@ -583,11 +591,13 @@ query_status <- function(envir) { dimnames = list(envir[["urls"]], c("i", "online", "instance", "assigned", "complete")))) } -register_everywhere <- function(refhook) +register_everywhere <- function(refhook, class, list) for (.compute in names(..)) - everywhere(mirai::serialization(refhook), refhook = refhook, .compute = .compute) + everywhere(mirai::serialization(refhook = refhook, class = class, list = list), + refhook = refhook, class = class, list = list, .compute = .compute) -serialization_refhook <- function(refhook = next_config()) - if (length(refhook[[1L]])) register_everywhere(refhook) +serialization_refhook <- function(refhook = .[["refhook"]]) + if (length(refhook[[1L]])) + register_everywhere(refhook = refhook[[1L]], class = refhook[[2L]], list = refhook[[3L]]) ._scm_. <- as.raw(c(0x07, 0x00, 0x00, 0x00, 0x42, 0x0a, 0x03, 0x00, 0x00, 0x00, 0x02, 0x03, 0x04, 0x00, 0x00, 0x05, 0x03, 0x00, 0x05, 0x00, 0x00, 0x00, 0x55, 0x54, 0x46, 0x2d, 0x38, 0xfc, 0x00, 0x00, 0x00)) diff --git a/man/serialization.Rd b/man/serialization.Rd index f83ad32df..d623f4996 100644 --- a/man/serialization.Rd +++ b/man/serialization.Rd @@ -4,15 +4,24 @@ \alias{serialization} \title{Custom Serialization Functions} \usage{ -serialization(refhook = list()) +serialization(refhook = list(), class = "", list = FALSE) } \arguments{ \item{refhook}{\strong{either} a list or pairlist of two functions: the -signature for the first must accept a list of external pointer type -objects and return a raw vector, e.g. \code{torch::torch_serialize}, and -the second must accept a raw vector and return a list of external pointer -type objects, e.g. \code{torch::torch_load},\cr \strong{or else} NULL to -reset.} +signature for the first must accept a reference object inheriting from +'class' (or a list of such objects) and return a raw vector, and the +second must accept a raw vector and return reference objects (or a list +of such objects), \cr \strong{or else} NULL to reset.} + +\item{class}{[default ""] a character string representing the class of object +that these serialization function will be applied to, e.g. 'ArrowTabular' +or 'torch_tensor'.} + +\item{list}{[default FALSE] the serialization functions accept and return +reference object individually e.g. \code{arrow::write_to_raw} and +\code{arrow::read_ipc_stream}. If TRUE, the serialization functions +accept and return a list of reference objects, e.g. +\code{torch::torch_serialize} and \code{torch::torch_load}.} } \value{ Invisibly, the pairlist of currently-registered 'refhook' functions. diff --git a/vignettes/torch.Rmd b/vignettes/torch.Rmd index 7da1eabae..3eb9e3fb0 100644 --- a/vignettes/torch.Rmd +++ b/vignettes/torch.Rmd @@ -26,7 +26,9 @@ This allows tensors from the [{torch}](https://torch.mlverse.org/) package to be library(mirai) library(torch) -serialization(refhook = list(torch:::torch_serialize, torch::torch_load)) +serialization(refhook = list(torch:::torch_serialize, torch::torch_load), + class = "torch_tensor", + list = TRUE) daemons(1) #> [1] 1 everywhere(library(torch)) @@ -71,35 +73,35 @@ The returned model is an object containing many tensor elements. m$data$parameters$conv1.weight #> torch_tensor #> (1,1,.,.) = -#> -0.0086 0.0254 0.0687 -0.0105 -0.0753 -#> -0.1362 0.0654 -0.0027 -0.1823 0.0603 -#> -0.0062 -0.1302 -0.1665 0.1108 0.0812 -#> 0.1842 -0.1088 -0.1089 -0.1304 0.1951 -#> -0.0307 -0.0876 0.1167 0.0421 -0.0779 +#> -0.1377 -0.1946 -0.0898 -0.0106 0.1031 +#> 0.0351 -0.1507 0.0192 -0.0240 0.1700 +#> 0.1107 -0.1232 -0.0690 -0.0868 0.0884 +#> -0.0267 -0.0022 0.1997 -0.1383 0.0435 +#> -0.1809 -0.1085 -0.0297 -0.0644 -0.0401 #> #> (2,1,.,.) = -#> -0.1645 -0.1112 0.1982 -0.1861 0.0050 -#> -0.0487 -0.0871 0.0554 0.1073 -0.0756 -#> -0.1663 0.0077 -0.1920 -0.0195 -0.0595 -#> 0.0111 -0.0025 0.1631 -0.0315 0.1216 -#> 0.1886 -0.1567 0.0554 0.0150 -0.0205 +#> -0.1002 0.1828 0.0886 -0.1793 0.1938 +#> -0.0046 0.1006 -0.0480 -0.0389 0.0083 +#> 0.0761 0.0242 0.0283 -0.1859 0.1711 +#> 0.0402 -0.1847 0.1351 0.1842 0.0094 +#> 0.1114 -0.1828 -0.1846 -0.0650 0.1380 #> #> (3,1,.,.) = -#> 0.0171 0.0304 -0.1556 0.1295 0.1315 -#> -0.1445 0.0111 0.1528 0.1383 0.0425 -#> -0.0562 0.1270 0.0210 0.1337 0.0721 -#> 0.1621 0.0851 -0.0242 -0.1696 0.0396 -#> 0.1895 0.1438 -0.0873 0.0403 0.1860 +#> 0.1659 -0.0655 0.0936 0.1089 0.0514 +#> -0.0058 0.1683 -0.0303 -0.0817 -0.1813 +#> -0.0236 -0.1817 -0.1238 0.1651 0.1937 +#> -0.1627 -0.0650 0.1760 0.0215 -0.0887 +#> -0.0851 0.1430 0.1322 -0.1617 -0.1646 #> #> (4,1,.,.) = -#> -0.0101 -0.1460 0.0260 0.0511 0.0726 -#> 0.1379 -0.1271 -0.0677 -0.1966 0.1740 -#> -0.0882 -0.0774 -0.1440 0.0796 0.0129 -#> 0.0191 0.0340 -0.1444 0.1827 0.1365 -#> -0.1977 -0.0124 0.0987 0.0948 0.0799 +#> -0.0520 -0.0200 0.0354 -0.1181 -0.1892 +#> 0.1204 0.0877 0.1280 -0.0056 0.0088 +#> -0.1765 -0.0036 -0.0664 0.0417 0.0189 +#> 0.1327 -0.1689 0.0132 0.0849 -0.0634 +#> 0.0072 -0.1516 0.0633 0.0185 0.0992 #> #> (5,1,.,.) = -#> -0.1495 -0.0418 0.0334 0.0977 -0.1629 +#> 0.0992 0.0912 -0.1982 0.1880 -0.0986 #> ... [the output was truncated (use n=-1 to disable)] #> [ CPUFloatType{20,1,5,5} ][ requires_grad = TRUE ] ``` diff --git a/vignettes/torch.Rmd.orig b/vignettes/torch.Rmd.orig index be9f37933..e106fa121 100644 --- a/vignettes/torch.Rmd.orig +++ b/vignettes/torch.Rmd.orig @@ -31,7 +31,9 @@ This allows tensors from the [{torch}](https://torch.mlverse.org/) package to be library(mirai) library(torch) -serialization(refhook = list(torch:::torch_serialize, torch::torch_load)) +serialization(refhook = list(torch:::torch_serialize, torch::torch_load), + class = "torch_tensor", + list = TRUE) daemons(1) everywhere(library(torch)) ```