optim icon indicating copy to clipboard operation
optim copied to clipboard

Last step of GD leads to crash (eigen version)

Open FROL256 opened this issue 3 years ago • 2 comments

The problem is in "error_reporting.ipp", line 80

//settings_inp->opt_fn_value     = opt_objfn(x_p,nullptr,opt_data);

https://drive.google.com/file/d/1XyAOyllOcnY3q10nHETtd6gvrWqGU4ov/view?usp=sharing https://drive.google.com/file/d/12qemu3_UEhJGcZ21mwypk5RPIFuXCXgf/view?usp=sharing

Seems something was "std::moved" out of memory. Probably this is Eigen problem but may be you can work around it.

Ubuntu 21.10. PS: could provide the full source if needed Btw: many thanks for the library )

FROL256 avatar Feb 02 '22 13:02 FROL256

I cannot replicate this issue. The gd tests include calls to this line of code.

What version of Eigen are you using? (If those links contain additional information, can you please paste the text here?)

kthohr avatar Feb 05 '22 18:02 kthohr

Eigen 3.4 I have all my code here https://github.com/FROL256/diffrender_tutorials

FROL256 avatar Feb 17 '22 07:02 FROL256

Hi, I also have this problem. It seems that each time the optimization stops due to one of the parameters I set (e.g. iter_max), it gives "segmentation fault". Strangely enough, if I configure optim using the -g flag, I won't run in the same issue, and the optimization ends without segmentation fault. I am using Eigen 3.4.0 under Ubuntu 20.04. Thank you!

DavideCarminati avatar Oct 12 '22 10:10 DavideCarminati

Can you help me identify the issue by providing a minimal reproducible example?

kthohr avatar Oct 16 '22 15:10 kthohr

Sure.

#define OPTIM_ENABLE_EIGEN_WRAPPERS
#include <optim.hpp>
#include <Eigen/Dense>

using namespace Eigen;

inline double fun_optim(const Eigen::VectorXd& theta, Eigen::VectorXd* grad_out, void* opt_data)
{
    MatrixXd K = MatrixXd::Random(4,4);
    double out = theta.transpose() * K * theta; // Classic paraboloid

    if (grad_out)
    {
        VectorXd grad = (K + K.transpose()) * theta;
        *grad_out = grad;
    }

    return out;
}

int main(int argc, char** argv)
{
    Eigen::VectorXd theta0(4);
    theta0 << 10, 2, 0.1, 5;
    optim::gd_settings_t gd_setting;
    gd_setting.method = 6; // ADAM optimizer
    optim::algo_settings_t algo_settings;
    algo_settings.print_level = 2;
    algo_settings.iter_max = 100;
    algo_settings.gd_settings = gd_setting;

    // Running solver
    bool success = optim::gd(theta0, fun_optim, nullptr, algo_settings);
    std::cout << "Success: " << success << std::endl;
    
    return 0;
}

While I was writing this code, I noticed that if theta has size greater than 3, it will give me segmentation fault. Thank you very much for helping me!

DavideCarminati avatar Oct 17 '22 12:10 DavideCarminati

Thanks for the example. I am unable to replicate the seg fault on my system--your example compiles and runs without issue. Can you provide the compiler flags you're using?

kthohr avatar Oct 19 '22 13:10 kthohr

Thank you for trying my code. I am using CMake to build the code, I paste here the CMakeLists.txt:

cmake_minimum_required(VERSION 3.0.0)
project(NLLoptim VERSION 0.1.0)

add_compile_options(-std=c++17)
add_compile_options( -DEIGEN_NO_DEBUG -msse -msse2 -O3 -fopenmp)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")
set(CMAKE_BUILD_TYPE Release)

include(CTest)
enable_testing()

find_package(Eigen3)
find_package(OpenMP)

find_path(OPTIMLIB_INCLUDES
           NAMES optim.hpp
           HINTS ~/.local/include/optim
           )
find_library(OPTIMLIB_LIB
           NAMES liboptim.so
           HINTS ~/.local/lib
           )
include_directories(${EIGEN_INCLUDE_DIRS}
                    ${OPTIMLIB_INCLUDES}
                    include
                    )

add_executable(mwe mwe.cpp)

target_link_libraries(  mwe
                        Eigen3::Eigen
                        OpenMP::OpenMP_CXX
                        ${OPTIMLIB_LIB}
                        )           

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

The compiler I use is g++ 9.4.0 and I have OpenMP 4.5.

DavideCarminati avatar Oct 20 '22 08:10 DavideCarminati

I wonder if a mismatch between SSE/AVX flags might be causing issues. By default, on x86 systems, OptimLib uses -march=native. Unless there's a particular reason why you need to limit to SSE2, can you change this line:

add_compile_options( -DEIGEN_NO_DEBUG -msse -msse2 -O3 -fopenmp)

to

add_compile_options(-march=native -O3 -ffp-contract=fast -fopenmp)

and try again?

kthohr avatar Oct 21 '22 20:10 kthohr

I am sorry for replying this late. Now it works! I changed those compile options as you suggested and the segmentation fault is gone. Thank you very much.

DavideCarminati avatar Nov 03 '22 10:11 DavideCarminati