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

C++ API RNN modules(RNN, LSTM, GRU) crash by segfault with num_layers <= 0 #108223

Closed
Sehun0819 opened this issue Aug 30, 2023 · 1 comment
Closed
Assignees
Labels
actionable module: cpp Related to C++ API module: nn Related to torch.nn triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@Sehun0819
Copy link
Contributor

Sehun0819 commented Aug 30, 2023

🐛 Describe the bug

C++ API torch::nn::{RNN, LSTM, GRU} crash when num_layers is not positive. A check should be inserted to prevent it. Note that Python API needs same check as well. Even it does not crash accidentally, current error message(IndexError: list index out of range) does not give a useful information.

Test code:

#include <stdint.h>
#include <stddef.h>
#include <c10/util/irange.h>
#include <cassert>
#include <torch/torch.h>

using namespace torch::nn;

int main() {
  try {
    torch::TensorOptions toptions = torch::TensorOptions();

    auto moptions =
      torch::nn::RNNOptions(1,1)
        .num_layers(0);

    auto m = torch::nn::RNN(moptions);

    auto result =
      m->forward(
        torch::randn({1,1}, toptions),
        torch::randn({1,1}, toptions));
  } catch (::c10::Error& e) {
    std::cout << "PyTorch exception: " << e.what();
  }

  return 0;
}

Error log:

AddressSanitizer:DEADLYSIGNAL
=================================================================
==3338431==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7f8a66771392 bp 0x7ffcfedc1810 sp 0x7ffcfedc17e0 T0)
==3338431==The signal is caused by a READ memory access.
==3338431==Hint: address points to the zero page.
    #0 0x7f8a66771392 in c10::intrusive_ptr<c10::TensorImpl, c10::UndefinedTensorImpl>::intrusive_ptr(c10::intrusive_ptr<c10::TensorImpl, c10::UndefinedTensorImpl> const&) /home/sehoon/pytorch-latest-asan/c10/util/intrusive_ptr.h:353:57
    #1 0x7f8a6677133c in at::TensorBase::TensorBase(at::TensorBase const&) /home/sehoon/pytorch-latest-asan/aten/src/ATen/core/TensorBase.h:104:3
    #2 0x7f8a6677130c in at::Tensor::Tensor(at::Tensor const&) /home/sehoon/pytorch-latest-asan/build/aten/src/ATen/core/TensorBody.h:109:3
    #3 0x7f8a7bcf6662 in torch::nn::detail::RNNImplBase<torch::nn::RNNImpl>::flatten_parameters() /home/sehoon/pytorch-latest-asan/torch/csrc/api/src/nn/modules/rnn.cpp:193:19
    #4 0x7f8a7bcf5f5e in torch::nn::detail::RNNImplBase<torch::nn::RNNImpl>::reset() /home/sehoon/pytorch-latest-asan/torch/csrc/api/src/nn/modules/rnn.cpp:174:9
    #5 0x7f8a7bcf14ff in torch::nn::detail::RNNImplBase<torch::nn::RNNImpl>::RNNImplBase(torch::nn::detail::RNNOptionsBase const&) /home/sehoon/pytorch-latest-asan/torch/csrc/api/src/nn/modules/rnn.cpp:59:3
    #6 0x7f8a7bcca143 in torch::nn::RNNImpl::RNNImpl(torch::nn::RNNOptions const&) /home/sehoon/pytorch-latest-asan/torch/csrc/api/src/nn/modules/rnn.cpp:415:7
    #7 0x408595 in torch::nn::ModuleHolder<torch::nn::RNNImpl>::ModuleHolder<torch::nn::RNNOptions&, void>(torch::nn::RNNOptions&) /home/sehoon/pytorch-latest-asan/torch/csrc/api/include/torch/nn/pimpl.h:65:19
    #8 0x40695c in torch::nn::RNN::RNN<torch::nn::RNNOptions&, void>(torch::nn::RNNOptions&) /home/sehoon/pytorch-latest-asan/torch/csrc/api/include/torch/nn/modules/rnn.h:138:1
    #9 0x4058d9 in main /home/sehoon/pytorch-latest-asan/test/cpp/reproduce/RNN_bo.cpp:17:14
    #10 0x7f8a59d66d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    #11 0x7f8a59d66e3f in __libc_start_main csu/../csu/libc-start.c:392:3
    #12 0x405484 in _start (/home/sehoon/pytorch-latest-asan/build/bin/reproduce_RNN_bo+0x405484)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /home/sehoon/pytorch-latest-asan/c10/util/intrusive_ptr.h:353:57 in c10::intrusive_ptr<c10::TensorImpl, c10::UndefinedTensorImpl>::intrusive_ptr(c10::intrusive_ptr<c10::TensorImpl, c10::UndefinedTensorImpl> const&)
==3338431==ABORTING

Error location:

auto first_fw = flat_weights_[0];

When num_layers is not positive, flat_weights_ is empty. We should add a check for preventing execution of out-of-bound access, and giving more useful diagnostics.

Versions

PyTorch version: 2.1.0a0+git5e0ec03
Is debug build: True
CUDA used to build PyTorch: Could not collect
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.2 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: 12.0.1 (git@github.com:starlab-unist/llvm-project.git 2a8037cf73a0379212f2e04e19e17e3082432d5d)
CMake version: version 3.26.4
Libc version: glibc-2.35

Python version: 3.9.17 (main, Jul 5 2023, 20:41:20) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.0-78-generic-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: 11.7.99
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration:
GPU 0: NVIDIA GeForce RTX 3090
GPU 1: NVIDIA GeForce RTX 3090
GPU 2: NVIDIA GeForce RTX 3090
GPU 3: NVIDIA GeForce RTX 3090

Nvidia driver version: 535.86.10
cuDNN version: Probably one of the following:
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn.so.8.9.2
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.9.2
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.9.2
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.9.2
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.9.2
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.9.2
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.9.2
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 46 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 96
On-line CPU(s) list: 0-95
Vendor ID: GenuineIntel
Model name: Intel(R) Xeon(R) Gold 6248R CPU @ 3.00GHz
CPU family: 6
Model: 85
Thread(s) per core: 2
Core(s) per socket: 24
Socket(s): 2
Stepping: 7
CPU max MHz: 4000.0000
CPU min MHz: 1200.0000
BogoMIPS: 6000.00
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts pku ospke avx512_vnni md_clear flush_l1d arch_capabilities
Virtualization: VT-x
L1d cache: 1.5 MiB (48 instances)
L1i cache: 1.5 MiB (48 instances)
L2 cache: 48 MiB (48 instances)
L3 cache: 71.5 MiB (2 instances)
NUMA node(s): 2
NUMA node0 CPU(s): 0-23,48-71
NUMA node1 CPU(s): 24-47,72-95
Vulnerability Itlb multihit: KVM: Mitigation: VMX disabled
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed: Mitigation; Enhanced IBRS
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Mitigation; TSX disabled

Versions of relevant libraries:
[pip3] numpy==1.25.2
[pip3] torch==2.1.0a0+git5e0ec03
[conda] mkl 2023.1.0 h6d00ec8_46342
[conda] mkl-include 2023.1.0 h06a4308_46342
[conda] numpy 1.25.2 pypi_0 pypi
[conda] torch 2.1.0a0+git5e0ec03 dev_0

cc @jbschlosser @albanD @mruberry @walterddr @mikaylagawarecki

@colesbury colesbury added module: cpp Related to C++ API triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Aug 30, 2023
@mikaylagawarecki
Copy link
Contributor

Actionable to add the check discussed in the cpp api

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
actionable module: cpp Related to C++ API module: nn Related to torch.nn triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants