-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnimble_converter.R
More file actions
185 lines (161 loc) · 6.37 KB
/
nimble_converter.R
File metadata and controls
185 lines (161 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# First steps towards a bridge from nimbleFunction to nFunction and nClass
#
# This file contains initial sketches of how we can maintain backward
# compatibility for nimbleFunctions and divert their compilation pathway
# to nFunctions and nClasses. It also contains comments to try to explain
# some nimble internals.
library(nimble)
# What we call a "simple" nimbleFunction is one with no setup code.
# This is converted into a C++ function.
# In nimble internals, a simple nimbleFunction is called an RCfunction.
# Toy example:
nimFoo <- nimbleFunction(
run = function(x = double(1)) {
y = sum(x)
return(y)
returnType(double())
}
)
# I see two pathways to convert this to an nCompiler::nFunction.
# Option 1: Extract the run function from nimFoo without using
# any of nimble's compilation steps at all. Put that into a
# nFunction. This should work because the basic type declarations
# are already backward(i.e. nimble)-compatible.
# This will need to be updated for predefined types.
# Prototype of what this will turn out to be:
nCfoo <- nFunction(
fun = function(x = double(1)) {
y = sum(x)
return(y)
returnType(double())
}
)
# Demo that it compiles
CnCfoo <- nCompile(nCfoo)
# Example of extracting the code for this from nimFoo.
# The internals are stored in an "nfMethodRC"
# that is in the closure of nimFoo
environment(nimFoo)$nfMethodRCobject
# The information maps easily into an nFunction
nfMethodRCobj <- environment(nimFoo)$nfMethodRCobject
fun <- function() {}
body(fun) <- nfMethodRCobj$code
formals(fun) <- nfMethodRCobj$argInfo
nCfoo2 <- nFunction(
name = nfMethodRCobj$uniqueName, # This might be helpful
fun = fun,
returnType = nfMethodRCobj$returnType
)
CnCfoo2 <- nCompile(nCfoo2)
CnCfoo2(1:10)
RCfun_2_nFun <- function(RCfun) {
nfMethodRCobj <- environment(RCfun)$nfMethodRCobject
fun <- function() {}
body(fun) <- nfMethodRCobj$code
formals(fun) <- nfMethodRCobj$argInfo
nFun <- nFunction(
name = nfMethodRCobj$uniqueName, # This might be helpful
fun = fun,
returnType = nfMethodRCobj$returnType
)
nFun
}
compile_RCfun_as_nFun <- function(RCfun, dirName = tempdir()) {
nFun <- RCfun_2_nFun(RCfun)
CnFun <- nCompile(nFun) # Something is broken about the dir argument.
CnFun
}
CnFoo3 <- compile_RCfun_as_nFun(nimFoo)
# Option 2: Use the first steps of nimble's compilation
# process, stopping before generating C++ code.
nimProj <- nimble:::nimbleProjectClass( tempdir(), "my_nimProj")
RCfunProc <- nimProj$compileRCfun(nimFoo, initialTypeInference = TRUE)
ls(RCfunProc)
# I am not going to develop this just now, waiting to see
# how necessary might become.
###############################
test_math_via_nCompiler <- function(param, caseName, verbose = nimbleOptions('verbose'), size = 3, dirName = NULL) {
info <- paste0(caseName, ': ', param$name)
## in some cases, expect_error does not suppress error messages (I believe this has
## to do with how we trap errors in compilation), so make sure user realizes expectation
if('knownFailureReport' %in% names(param) && param$knownFailureReport)
cat("\nBegin expected error message:\n")
test_that(info, {
## wrap_if_matches(param$xfail, paste0(info, ': compiles and runs'), expect_error, {
test_math_internal_via_nCompiler(param, info, verbose, size, dirName)
## })
})
if('knownFailureReport' %in% names(param) && param$knownFailureReport)
cat("End expected error message.\n")
invisible(NULL)
}
test_math_internal_via_nCompiler <- function(param, info, verbose = nimbleOptions('verbose'), size = 3, dirName = NULL) {
if(verbose) cat("### Testing", param$name, "###\n")
nArgs <- length(param$inputDim)
logicalArgs <- rep(FALSE, nArgs)
if("logicalArgs" %in% names(param))
logicalArgs <- param$logicalArgs
returnType <- "double"
if("returnType" %in% names(param))
returnType <- param$returnType
runFun <- gen_runFun(param, logicalArgs, returnType)
wrap_if_matches(param$expectWarnings, "builds", expect_warning, {
nfR <- nimbleFunction(
run = runFun)
})
info <- paste0(info, ": compiles")
## need expect_error not expect_failure(expect_something()) because otherwise
## R error will stop execution
wrap_if_matches(param$knownFailure, info, expect_error, {
nfC <- compile_RCfun_as_nFun(nfR, dirName = dirName)
arg1 <- make_input(param$inputDim[1], size = size, logicalArgs[1])
if(nArgs > 1)
arg2 <- make_input(param$inputDim[2], size = size, logicalArgs[2])
if(nArgs > 2)
arg3 <- make_input(param$inputDim[3], size = size, logicalArgs[3])
if(nArgs > 3)
stop("test_math not set up for >3 args yet")
if("Rcode" %in% names(param)) {
eval(param$Rcode)
} else {
eval(param$expr)
}
info <- paste0(info, ": runs")
wrap_if_matches(param$knownFailure, info, expect_failure, {
if(nArgs == 3) {
expect_silent(out_nfR <- nfR(arg1, arg2, arg3))
expect_silent(out_nfC <- nfC(arg1, arg2, arg3))
}
if(nArgs == 2) {
expect_silent(out_nfR <- nfR(arg1, arg2))
expect_silent(out_nfC <- nfC(arg1, arg2))
}
if(nArgs == 1) {
expect_silent(out_nfR <- nfR(arg1))
expect_silent(out_nfC <- nfC(arg1))
}
attributes(out) <- attributes(out_nfR) <- attributes(out_nfC) <- NULL
infoR <- paste0(info, ": R vs Nimble DSL")
wrap_if_matches(param$knownFailure, infoR, expect_failure, {
expect_equal(out, out_nfR, info = infoR)
})
infoC <- paste0(info, ": R vs Nimble Cpp")
wrap_if_matches(param$knownFailure, infoC, expect_failure, {
expect_equal(out, out_nfC, info = infoC)
})
})
})
invisible(NULL)
}
source(system.file(file.path('tests', 'testthat', 'test_utils.R'), package = 'nimble'))
options(warn = 0)
nimbleOptions(verbose = FALSE)
source(system.file(file.path('tests', 'testthat', 'mathTestLists.R'), package = 'nimble'))
set.seed(0)
debug(test_math_internal_via_nCompiler)
ans1 <- sapply(testsVaried, test_math, 'math') ## 12 # All pass!
ans2 <- sapply(testsBasicMath, test_math_via_nCompiler, 'math') ## 70 # nimStep is first failure
ans3 <- sapply(testsMoreMath, test_math, 'math') ## 41 # All pass!
ans4 <- sapply(testsReduction, test_math, 'math') ## 13 # All pass!
ans5 <- sapply(testsComparison, test_math, 'math')## 12 # All pass!
ans6 <- sapply(testsMatrix, test_math, 'math') ## 19 # All pass!