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

Unexpected warning (about resizing) from torch_matrix_exp() only when input tensor values are < 1, despite output being correct #969

Closed
maspotts opened this issue Jan 25, 2023 · 2 comments

Comments

@maspotts
Copy link

maspotts commented Jan 25, 2023

I've encountered an unexpected warning during torch_matrix_exp() which is triggered only when the argument (x) has values in a range (< 1) which causes print(x) to display in "scientific notation" mode (ie. by printing an exponential scaling factor (eg. 0.001 *) before printing the columns). This is consistent across 1000s of calls to torch_matrix_exp(): scientifically-notated inputs x always trigger the warning; non-scientifically-notated inputs x never trigger the warning. The warning is:

[W Resize.cpp:24] Warning: An output with one or more elements was resized since it had shape [1, 16, 16], which does not match the required output shape [1, 1, 16, 16]. This behavior is deprecated, and in a future PyTorch release outputs will not be resized unless they have zero elements. You can explicitly reuse an out tensor t by resizing it, inplace, to zero elements with t.resize_(0). (function resize_output_check)

(my inputs are 16x16 tensors). Note: despite the warning, the calculated matrix exponential is still correct (when compared with the output of expm::expm() applied to the corresponding matrix (non-tensor). I'm using torch 0.9.1 under R 4.2.2.

Code to replicate the problem:

a <- matrix(c(1, 2, 5, 3), ncol = 2)
aa <- torch_tensor(a)
b <- a * 1e-3
bb <- torch_tensor(b)
print("a: Not scientific, no warning:")
aa
torch_matrix_exp(aa)
print("b: Scientific, triggers warning:")
bb
torch_matrix_exp(bb)

Output:

[1] "a: Not scientific, no warning:"
> aa
torch_tensor
 1  5
 2  3
[ CPUFloatType{2,2} ]
> torch_matrix_exp(aa)
torch_tensor
  71.3138  153.3390
  61.3356  132.6494
[ CPUFloatType{2,2} ]
> print("b: Scientific, no warning:")
[1] "b: Scientific, triggers warning:"
> bb
torch_tensor
0.001 *
 1.0000  5.0000
 2.0000  3.0000
[ CPUFloatType{2,2} ]
> torch_matrix_exp(bb)
[W Resize.cpp:24] Warning: An output with one or more elements was resized since it had shape [1, 2, 2], which does not match the required output shape [1, 1, 2, 2]. This behavior is deprecated, and in a future PyTorch release outputs will not be resized unless they have zero elements. You can explicitly reuse an out tensor t by resizing it, inplace, to zero elements with t.resize_(0). (function resize_output_check)
torch_tensor
 1.0010  0.0050
 0.0020  1.0030
[ CPUFloatType{2,2} ]

I'm concerned that this warning may turn into an error in a future release, and obviously would prefer not to have to worry about an (apparently spurious) warning based on the scale of the inputs. Any ideas?

Session info:

R version 4.2.2 (2022-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats4    graphics  datasets  stats     utils     grDevices methods
[8] base

other attached packages:
 [1] torch_0.9.1      expm_0.999-6     Matrix_1.5-1     withr_2.5.0
 [5] jsonlite_1.8.3   bbmle_1.0.25     optimx_2022-4.30 assertthat_0.2.1
 [9] plotly_4.10.1    patchwork_1.1.2  scales_1.2.1     gridExtra_2.3
[13] lattice_0.20-45  deSolve_1.34     glue_1.6.2       rlist_0.4.6.2
[17] fuzzyjoin_0.1.6  broom_1.0.1      ggvis_0.4.7      ggplot2_3.4.0
[21] forcats_0.5.2    hms_1.1.2        lubridate_1.9.0  timechange_0.1.1
[25] stringr_1.4.1    readr_2.1.3      purrr_0.3.5      tidyr_1.2.1
[29] dplyr_1.0.10     magrittr_2.0.3   tibble_3.1.8

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.9          bdsmatrix_1.3-6     mvtnorm_1.1-3
 [4] ps_1.7.2            digest_0.6.30       utf8_1.2.2
 [7] mime_0.12           R6_2.5.1            backports_1.4.1
[10] httr_1.4.4          pillar_1.8.1        rlang_1.0.6
[13] lazyeval_0.2.2      data.table_1.14.6   callr_3.7.3
[16] htmlwidgets_1.5.4   bit_4.0.5           munsell_0.5.0
[19] shiny_1.7.3         compiler_4.2.2      numDeriv_2016.8-1.1
[22] httpuv_1.6.6        pkgconfig_2.0.3     htmltools_0.5.3
[25] tidyselect_1.2.0    fansi_1.0.3         viridisLite_0.4.1
[28] tzdb_0.3.0          later_1.3.0         MASS_7.3-58.1
[31] grid_4.2.2          xtable_1.8-4        gtable_0.3.1
[34] lifecycle_1.0.3     DBI_1.1.3           coro_1.0.3
[37] cli_3.4.1           stringi_1.7.8       promises_1.2.0.1
[40] ellipsis_0.3.2      generics_0.1.3      vctrs_0.5.1
[43] tools_4.2.2         bit64_4.0.5         processx_3.8.0
[46] fastmap_1.1.0       colorspace_2.0-3
@skeydan
Copy link
Collaborator

skeydan commented Feb 6, 2023

Hi,
thanks for asking! This is an internal message originating from libtorch (so, not on the R side).
In fact, this message goes away in the next libtorch version (1.13.1), which we're adding support for right now (#977).

I've checked this in Python:

PyTorch 1.12.1

Python 3.10.9 (main, Dec 20 2022, 00:00:00) [GCC 12.2.1 20221121 (Red Hat 12.2.1-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> a = torch.tensor((2., 3., 4., 5.)).reshape(2,2)
>>> torch.matrix_exp(a)
tensor([[ 435.5261,  573.3393],
        [ 764.4525, 1008.8654]])
>>> b = a * 1e-3
>>> torch.matrix_exp(b)
<stdin>:1: UserWarning: An output with one or more elements was resized since it had shape [1, 2, 2], which does not match the required output shape [1, 1, 2, 2]. This behavior is deprecated, and in a future PyTorch release outputs will not be resized unless they have zero elements. You can explicitly reuse an out tensor t by resizing it, inplace, to zero elements with t.resize_(0). (Triggered internally at  ../aten/src/ATen/native/Resize.cpp:17.)
tensor([[1.0020, 0.0030],
        [0.0040, 1.0050]])

Pytorch 1.13.1

Python 3.11.1 (main, Jan  6 2023, 00:00:00) [GCC 12.2.1 20221121 (Red Hat 12.2.1-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = torch.tensor((2., 3., 4., 5.)).reshape(2,2)
>>> torch.matrix_exp(a)
tensor([[ 435.5261,  573.3393],
        [ 764.4525, 1008.8654]])
>>> b = a * 1e-3
>>> torch.matrix_exp(b)
tensor([[1.0020, 0.0030],
        [0.0040, 1.0050]])
>>> 


@maspotts
Copy link
Author

maspotts commented Feb 6, 2023

Great to know: thanks very much!

@maspotts maspotts closed this as completed Feb 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants