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

Substitute instead of mask #14

Open
wants to merge 2 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# Generated by roxygen2: do not edit by hand

S3method("[[<-",tensorflow.python.ops.tensor_array_ops.TensorArray)
export(ag_break)
export(ag_for)
export(ag_if)
export(ag_if_vars)
export(ag_loop_vars)
export(ag_name)
export(ag_next)
export(ag_on.exit)
export(ag_stopifnot)
export(ag_while)
export(ag_while_opts)
export(autograph)
export(tf_assert)
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# tfautograph (development version)

* `ag_if()` and related autographing control-flow functions are now exported,
allowing for precise control of which expressions produce graph nodes.

* `autograph()` now works by substituting symbols in language objects rather than
masking them by splicing in an environment. This will make it easier to discover
what `autograph()` is doing in interactive contexts.

* `autograph()` now returns functions and language objects like expressions,
calls, and formulas, with an S3 class attribute "tfautographed".

* `for` in an eager `autograph()` context gains the ability to process
arbitrary python iterables, including `tf.distribute.DistributedDataset`.

Expand Down
3 changes: 2 additions & 1 deletion R/break-next.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ uncaught_loop_control_flow_condition <-
}



#' @export
ag_break <- function() {
env <- parent.frame()
tryCatch(
Expand All @@ -37,6 +37,7 @@ ag_break <- function() {
)
}

#' @export
ag_next <- function() {
env <- parent.frame()
tryCatch(
Expand Down
2 changes: 1 addition & 1 deletion R/ctrl-deps.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@


#' @export
ag_on.exit <- function(expr = NULL, add = TRUE, after = FALSE) {

env <- parent.frame()
Expand Down
2 changes: 1 addition & 1 deletion R/for.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@


#' @export
ag_for <- function(var, iterable, body) {
var <- substitute(var)
body <- substitute(body)
Expand Down
2 changes: 1 addition & 1 deletion R/if.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@



#' @export
#' @importFrom reticulate dict
ag_if <- function(cond, true, false = NULL) {
true <- substitute(true)
Expand Down
1 change: 1 addition & 0 deletions R/stopifnot.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#' @export
ag_stopifnot <- function(..., exprs, local = TRUE) {
if(missing(exprs)) {
dots <- eval(substitute(alist(...)))
Expand Down
2 changes: 1 addition & 1 deletion R/while.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@


#' @export
ag_while <- function(cond, body) {
cond <- substitute(cond)
body <- substitute(body)
Expand Down
37 changes: 34 additions & 3 deletions R/zzz-autograph.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ ag_mask_list <- list(
)


ag_mask_subtitution_list <- alist(
`if` = tfautograph::ag_if,
`for` = tfautograph::ag_for,
`while` = tfautograph::ag_while,
`break` = tfautograph::ag_break,
`next` = tfautograph::ag_next,
`stopifnot` = tfautograph::ag_stopifnot,
`on.exit` = tfautograph::ag_on.exit
)



#' Autograph R code
Expand Down Expand Up @@ -40,16 +50,36 @@ autograph <- function(x) {
xe <- substitute(x)
env <- parent.frame()


if (is.symbol(xe)) {
# function, formula, or something with `environment<-` method
environment(x) <- new_ag_mask(parent = environment(x))
# function, language, or something with `environment<-` method

if (inherits(xe, "python.builtin.object"))
stop("Only R objects can be autographed")

if (is.function(x))
body(x) <- eval(call("substitute", body(x), ag_mask_subtitution_list))
else if (is.language(x)) # formula, expression(), or call() (probably to `{`)
x <- eval(call("substitute", x, ag_mask_subtitution_list))
else
environment(x) <- new_ag_mask(parent = environment(x))

class(x) <- unique(c("tfautographed", class(x)))
return(x)
}

# in line expression
fn <- as_outcome_fn(xe, new_ag_mask(parent = env))
# fn <- as_outcome_fn(xe, new_ag_mask(parent = env))

xe <- eval(call("substitute", xe, ag_mask_subtitution_list))
fn <- as_outcome_fn(xe, env)
outcome <- fn()

outcome <- rapply(list(outcome), function(x) {
class(x) <- unique(c("tfautographed", class(x)))
x
}, how = "replace", classes = c("function", "language"))[[1L]]

export_modified(outcome$modified, env)

if(isFALSE(outcome$visible) ||
Expand All @@ -60,6 +90,7 @@ autograph <- function(x) {
}



new_ag_mask <- function(parent = parent.frame()) {

ag_mask <- list2env(ag_mask_list, parent = parent)
Expand Down