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

matrix_exp_multiply seg faults #1073

Closed
syclik opened this issue Dec 4, 2018 · 16 comments
Closed

matrix_exp_multiply seg faults #1073

syclik opened this issue Dec 4, 2018 · 16 comments

Comments

@syclik
Copy link
Member

syclik commented Dec 4, 2018

Description

matrix_exp_multiply, as written, currently seg faults.

Example

This test current seg faults on my machine. I think it may be memory dependent, but not certain.

#include <stan/math/rev/mat.hpp>
#include <gtest/gtest.h>

TEST(MathRev, matrix_multiply_exp__vd) {
  Eigen::Matrix<stan::math::var, -1, -1> A(5, 5);
  A << -0.96871, 0.398827, 0.241306, 0.741373, 0.108926,
    0.888077, -0.915624, -0.373344, 0.255238, 0.717304,
    -0.0899219, -0.898862, -0.800546, -0.222652, -0.271382,
    0.683227, 0.827031, -0.780702, -0.104228, 0.885106,
    -0.996585, -0.097802, 0.739617, 0.235266, -0.0247717;


  Eigen::Matrix<double, -1, -1> B(5, 5);
  B << -0.96871, 0.398827, 0.241306, 0.741373, 0.108926, 0.888077, -0.915624,
    -0.373344, 0.255238, 0.717304, -0.0899219, -0.898862, -0.800546,
    -0.222652, -0.271382, 0.683227, 0.827031, -0.780702, -0.104228, 0.885106,
    -0.996585, -0.097802, 0.739617, 0.235266, -0.0247717;

  Eigen::Matrix<stan::math::var, -1, -1> result = stan::math::matrix_exp_multiply(A, B);

  std::vector<double> gradients;
  stan::math::print_stack(std::cout);

  std::vector<stan::math::var> vars = stan::math::to_array_1d(A);
  result(0, 0).grad(vars, gradients);

  std::cout << "gradients = " << std::endl;
  for (size_t j = 0; j < gradients.size(); ++j)
    std::cout << gradients[j] << " ";
  std::cout << std::endl;

  FAIL() << "There are no asserts in the tests." << std::endl
         << "This test seg faults with the existing implementation." <<	std::endl;
}

Expected Output

Not seg fault. Return the same result as doing the actions separately.

Additional Information

Thank you, @rok-cesnovar, for pushing the Windows tests to the point where we were able to find this.

@yizhang-cae, can you look at the implementation and help identify why it's seg faulting? I'm going to patch this by using the naive implementation.

There is a new branch: bugfix/issue-1073-matrix_exp_mulitply and the failing test has been included to the test/unit/math/rev/mat/fun/matrix_exp_multiply_test.cpp file. If you run:

$ ./runTests.py test/unit/math/rev/mat/fun/matrix_exp_multiply_test.cpp 

it should fail.

Current Math Version

v2.18.0

@bob-carpenter
Copy link
Contributor

bob-carpenter commented Dec 4, 2018 via email

@syclik
Copy link
Member Author

syclik commented Dec 4, 2018

See the updated branch. I put that original test in there to demonstrate the seg fault. It's been updated already.

@syclik
Copy link
Member Author

syclik commented Dec 4, 2018

@rok-cesnovar, I just verified that this patch gets us past the matrix_exp_multiply problem in Windows. Thank you for helping with that PR... it's been a lot of help. (even though it's been slow; I didn't expect we'd run into a seg fault issue that is trapped by Windows and not Mac or Linux.)

@rok-cesnovar
Copy link
Member

No problem, glad too help. Besides this, all the remaining Windows issue were fixed so I think we are clear! Great stuff!

@yizhang-yiz
Copy link
Contributor

Sorry for being late on this. Is the failure compiler/platform dependent? Because on my mac

test/unit/math/rev/mat/fun/matrix_exp_multiply_test --gtest_output="xml:test/unit/math/rev/mat/fun/matrix_exp_multiply_test.xml"
Running main() from lib/gtest_1.8.1/src/gtest_main.cc
[==========] Running 5 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 1 test from MathRev
[ RUN      ] MathRev.matrix_multiply_exp__vd__segfault
[       OK ] MathRev.matrix_multiply_exp__vd__segfault (0 ms)
[----------] 1 test from MathRev (0 ms total)

[----------] 4 tests from MathMatrix
[ RUN      ] MathMatrix.matrix_exp_multiply_dv
[       OK ] MathMatrix.matrix_exp_multiply_dv (1 ms)
[ RUN      ] MathMatrix.matrix_exp_multiply_vd
[       OK ] MathMatrix.matrix_exp_multiply_vd (4 ms)
[ RUN      ] MathMatrix.matrix_exp_multiply_vv
[       OK ] MathMatrix.matrix_exp_multiply_vv (15 ms)
[ RUN      ] MathMatrix.matrix_exp_multiply_exception
[       OK ] MathMatrix.matrix_exp_multiply_exception (0 ms)
[----------] 4 tests from MathMatrix (20 ms total)

[----------] Global test environment tear-down
[==========] 5 tests from 2 test cases ran. (20 ms total)
[  PASSED  ] 5 tests.

@syclik
Copy link
Member Author

syclik commented Dec 5, 2018 via email

@syclik
Copy link
Member Author

syclik commented Dec 6, 2018

@yizhang-cae: to be more specific, can you check out this commit and see if it fails? 1784c1c. That's the first one that includes the test (and leaves the code alone).

@yizhang-yiz
Copy link
Contributor

You're right. I can reproduce the failure. I'm looking for the cause.

@syclik
Copy link
Member Author

syclik commented Dec 6, 2018 via email

@yizhang-yiz
Copy link
Contributor

I've added a branch https://github.com/yizhang-cae/math/tree/matrix_multiply_seg_fault
to show that seg fault can be introduced to multiply function if two lines are added. In that branch run

./runTests.py test/unit/math/rev/mat/fun/multiply_failure

will produce a seg fault.

@syclik
Copy link
Member Author

syclik commented Dec 7, 2018 via email

@yizhang-yiz
Copy link
Contributor

yizhang-yiz commented Dec 7, 2018

That's my suspicion, and it won't fail if it's

    Eigen::Matrix<stan::math::var, -1, -1> A_temp(1, 1);
    A_temp << 1.0;

so it's might be related to the assignment.

@bob-carpenter
Copy link
Contributor

bob-carpenter commented Dec 7, 2018 via email

@syclik
Copy link
Member Author

syclik commented Dec 10, 2018 via email

@bob-carpenter
Copy link
Contributor

bob-carpenter commented Dec 10, 2018 via email

@rok-cesnovar
Copy link
Member

@yizhang-yiz example no longer segfaults (not sure what fixed it but Math lib has been refactored a few times in the last 2 years):

TEST(MathMatrix, multiply_seg_fault_vd) {
  Eigen::Matrix<stan::math::var, -1, -1> A=Eigen::Matrix<stan::math::var, -1, -1>::Random(1, 1);
  Eigen::Matrix<double, -1, -1> B=Eigen::Matrix<double, -1, -1>::Random(1, 1);

  Eigen::Matrix<stan::math::var, -1, -1> result = stan::math::multiply(A, B);

  std::vector<double> gradients;

  std::vector<stan::math::var> vars = stan::math::to_array_1d(A);
  result(0, 0).grad(vars, gradients);
}

Closing.

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

4 participants