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

Fixes for torch_tile #905

Merged
merged 2 commits into from
Oct 14, 2022
Merged
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
12 changes: 1 addition & 11 deletions R/codegen-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@ as_1_based_tensor_list <- function(x) {
}

as_1_based_tensor <- function(x) {
with_no_grad({
if (!any(x$shape == 0)) {
e <- torch_min(torch_abs(x))$to(dtype = torch_int())
if (e$item() == 0) {
runtime_error("Indices/Index start at 1 and got a 0.")
}
}

out <- x - (x > 0)$to(dtype = x$dtype)
})
out
to_index_tensor(x)
}

clean_chars <- c("'", "\"", "%", "#", ":", ">", "<", ",", " ", "*", "&")
Expand Down
2 changes: 1 addition & 1 deletion R/gen-method.R
Original file line number Diff line number Diff line change
Expand Up @@ -4582,7 +4582,7 @@ call_c_function(
return_types = return_types,
fun_type = 'method'
)})
Tensor$set("public", "movedim", function(source, destination) { args <- mget(x = c("source", "destination"))
Tensor$set("private", "_movedim", function(source, destination) { args <- mget(x = c("source", "destination"))
args <- c(list(self = self), args)
expected_types <- list(self = "Tensor", source = c("IntArrayRef", "int64_t"), destination = c("IntArrayRef",
"int64_t"))
Expand Down
3 changes: 3 additions & 0 deletions R/tensor.R
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ Tensor <- R7Class(
},
bincount = function(weights = list(), minlength = 0L) {
to_index_tensor(self)$private$`_bincount`(weights = weights, minlength = minlength)
},
movedim = function(source, destination) {
private$`_movedim`(as_1_based_dim(source), as_1_based_dim(destination))
}
),
active = list(
Expand Down
8 changes: 4 additions & 4 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5755,13 +5755,13 @@ BEGIN_RCPP
END_RCPP
}
// cpp_torch_method_tile_self_Tensor_dims_IntArrayRef
XPtrTorchTensor cpp_torch_method_tile_self_Tensor_dims_IntArrayRef(XPtrTorchTensor self, XPtrTorchIndexIntArrayRef dims);
XPtrTorchTensor cpp_torch_method_tile_self_Tensor_dims_IntArrayRef(XPtrTorchTensor self, XPtrTorchIntArrayRef dims);
RcppExport SEXP _torch_cpp_torch_method_tile_self_Tensor_dims_IntArrayRef(SEXP selfSEXP, SEXP dimsSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< XPtrTorchTensor >::type self(selfSEXP);
Rcpp::traits::input_parameter< XPtrTorchIndexIntArrayRef >::type dims(dimsSEXP);
Rcpp::traits::input_parameter< XPtrTorchIntArrayRef >::type dims(dimsSEXP);
rcpp_result_gen = Rcpp::wrap(cpp_torch_method_tile_self_Tensor_dims_IntArrayRef(self, dims));
return rcpp_result_gen;
END_RCPP
Expand Down Expand Up @@ -22005,13 +22005,13 @@ BEGIN_RCPP
END_RCPP
}
// cpp_torch_namespace_tile_self_Tensor_dims_IntArrayRef
XPtrTorchTensor cpp_torch_namespace_tile_self_Tensor_dims_IntArrayRef(XPtrTorchTensor self, XPtrTorchIndexIntArrayRef dims);
XPtrTorchTensor cpp_torch_namespace_tile_self_Tensor_dims_IntArrayRef(XPtrTorchTensor self, XPtrTorchIntArrayRef dims);
RcppExport SEXP _torch_cpp_torch_namespace_tile_self_Tensor_dims_IntArrayRef(SEXP selfSEXP, SEXP dimsSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< XPtrTorchTensor >::type self(selfSEXP);
Rcpp::traits::input_parameter< XPtrTorchIndexIntArrayRef >::type dims(dimsSEXP);
Rcpp::traits::input_parameter< XPtrTorchIntArrayRef >::type dims(dimsSEXP);
rcpp_result_gen = Rcpp::wrap(cpp_torch_namespace_tile_self_Tensor_dims_IntArrayRef(self, dims));
return rcpp_result_gen;
END_RCPP
Expand Down
4 changes: 2 additions & 2 deletions src/gen-namespace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2443,7 +2443,7 @@ return XPtrTorchTensor(r_out);
}

// [[Rcpp::export]]
XPtrTorchTensor cpp_torch_method_tile_self_Tensor_dims_IntArrayRef (XPtrTorchTensor self, XPtrTorchIndexIntArrayRef dims) {
XPtrTorchTensor cpp_torch_method_tile_self_Tensor_dims_IntArrayRef (XPtrTorchTensor self, XPtrTorchIntArrayRef dims) {
auto r_out = lantern_Tensor_tile_tensor_intarrayref(self.get(), dims.get());
return XPtrTorchTensor(r_out);
}
Expand Down Expand Up @@ -9991,7 +9991,7 @@ return XPtrTorchTensor(r_out);
}

// [[Rcpp::export]]
XPtrTorchTensor cpp_torch_namespace_tile_self_Tensor_dims_IntArrayRef (XPtrTorchTensor self, XPtrTorchIndexIntArrayRef dims) {
XPtrTorchTensor cpp_torch_namespace_tile_self_Tensor_dims_IntArrayRef (XPtrTorchTensor self, XPtrTorchIntArrayRef dims) {
auto r_out = lantern_tile_tensor_intarrayref(self.get(), dims.get());
return XPtrTorchTensor(r_out);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-gen-namespace.R
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,9 @@ test_that("zeros_out", {
expect_tensor(torch_zeros_out(x, c(2)))
expect_equal_to_tensor(x, torch_tensor(c(0, 0)))
})

test_that("tile works correctly", {
x <- torch_tensor(c(1, 2, 3))
expect_true(length(x$tile(2)) == 6)
expect_true(length(torch_tile(x, 2)) == 6)
})
2 changes: 2 additions & 0 deletions tests/testthat/test-wrapers.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ test_that("movedim", {
x <- torch_randn(3, 2, 1)
expect_tensor_shape(torch_movedim(x, 1, 2), c(2, 3, 1))
expect_tensor_shape(torch_movedim(x, c(1, 2), c(2, 3)), c(1, 3, 2))
expect_tensor_shape(x$movedim(1, 2), c(2, 3, 1))
expect_tensor_shape(x$movedim(c(1, 2), c(2, 3)), c(1, 3, 2))
})

test_that("norm", {
Expand Down
27 changes: 19 additions & 8 deletions tools/torchgen/R/cpp.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,21 @@ cpp_function_name <- function(method, type) {
make_cpp_function_name(method$name, arg_types, type)
}

indexing_special_cases <- function(argument) {
!(argument$decl_name %in% c("tile"))
}

cpp_parameter_type <- function(argument) {

if (argument$name %in% c("index", "indices", "dims") &&
if (indexing_special_cases(argument) &&
argument$name %in% c("index", "indices", "dims") &&
argument$dynamic_type == "Tensor")
{
return("XPtrTorchIndexTensor")
}

if (argument$name %in% c("dims", "dims_self", "dims_other", "dim") &&
if (indexing_special_cases(argument) &&
argument$name %in% c("dims", "dims_self", "dims_other", "dim") &&
argument$dynamic_type == "IntArrayRef")
{
if (argument$type %in% c("c10::optional<IntArrayRef>", "OptionalIntArrayRef")) {
Expand All @@ -123,21 +129,23 @@ cpp_parameter_type <- function(argument) {
}
}

if (argument$name %in% c("dim", "dim0", "dim1", "dim2", "start_dim", "end_dim", "index") &&
if (indexing_special_cases(argument) &&
argument$name %in% c("dim", "dim0", "dim1", "dim2", "start_dim", "end_dim", "index") &&
argument$dynamic_type == "int64_t") {

if (argument$type == "c10::optional<int64_t>")
return("XPtrTorchoptional_index_int64_t")
else
return("XPtrTorchindex_int64_t")
}

if (argument$name == "indices" &&
if (indexing_special_cases(argument) &&
argument$name == "indices" &&
argument$dynamic_type == "TensorList") {
return("XPtrTorchIndexTensorList")
}

if (argument$name == "indices" &&
if (indexing_special_cases(argument) &&
argument$name == "indices" &&
argument$dynamic_type == "const c10::List<c10::optional<Tensor>> &") {
return("XPtrTorchOptionalIndexTensorList")
}
Expand Down Expand Up @@ -333,8 +341,11 @@ cpp_parameter <- function(argument) {
}

cpp_signature <- function(decl) {

res <- purrr::map_chr(decl$arguments, cpp_parameter) %>%
name <- decl$name
res <- purrr::map_chr(decl$arguments, function(x) {
x$decl_name <- name #expose de declaration name
cpp_parameter(x)
}) %>%
glue::glue_collapse(sep = ", ")

if(length(res) == 0)
Expand Down
3 changes: 2 additions & 1 deletion tools/torchgen/R/r.R
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ internal_methods <- c("_backward", "retain_grad", "size", "to", "stride",
"copy_", "topk", "scatter_", "scatter", "rename",
"rename_", "narrow", "narrow_copy", "is_leaf", "max",
"min", "argsort", "argmax", "argmin", "norm", "split",
"nonzero", "nonzero_numpy", "view", "sort", "bincount")
"nonzero", "nonzero_numpy", "view", "sort", "bincount",
"movedim")

r_method_env <- function(decls) {
if (decls[[1]]$name %in% internal_methods)
Expand Down