xtensor-stack / xtensor

C++ tensors with broadcasting and lazy computing
BSD 3-Clause "New" or "Revised" License
3.33k stars 398 forks source link

Bad interaction between xtensor/view/zeros. #768

Closed chavid closed 6 years ago

chavid commented 6 years ago

Hi. Another small challenge for you below, which took me few days to get to the bone. To sump up, when subtracting some zeros to a 2D xtensor, through a view, it switches the indices.

#include <xtensor/xtensor.hpp>
#include <xtensor/xio.hpp>

int main( int argc, char** argv ){

    xt::xtensor<float, 2> xtensor_matrix = {
        { 1, 2, 3 },
        { 4, 5, 6 },
        { 7, 8, 9 }
    } ;
    xt::xtensor<float, 2> xtensor_zeros = xt::zeros<float>(xtensor_matrix.shape()) ;
    auto xtensor_view = xt::view(xtensor_matrix,xt::all(),xt::all()) ;

    std::cout << "matrix\n" << xt::view(xtensor_matrix,xt::all(),xt::all()) << std::endl ;
    std::cout << "matrix - zeros\n" << (xt::view(xtensor_matrix,xt::all(),xt::all())-xtensor_zeros) << std::endl ;

    return 0 ;
}
JohanMabille commented 6 years ago

This is a bug in the xstrided_view, used by operator<<. The following code should print the correct result:

xt::xtensor<float, 2> xtensor_matrix = {
        { 1, 2, 3 },
        { 4, 5, 6 },
        { 7, 8, 9 }
    } ;
    xt::xtensor<float, 2> xtensor_zeros = xt::zeros<float>(xtensor_matrix.shape()) ;
    auto xtensor_view = xt::view(xtensor_matrix,xt::all(),xt::all()) ;
    xt::xtensor<float, 2> res = xtensor_view - xtensor_zeros;
    std::cout << "matrix - zeros\n" << res << std::endl;
SylvainCorlay commented 6 years ago

Hopefully #769 should be a sufficient fix.

Basically, we were printing in column major layout :)

Waiting on the build to pass and we will make a patch release.

SylvainCorlay commented 6 years ago

Thanks for the report!

chavid commented 6 years ago

Do you think this problem in xstrided_view may affect other operation ? Before I came to this small demo code, I was originally having a computing problem in a more complex code, heavily relying on xt::views.

SylvainCorlay commented 6 years ago

The issue concerns strided views on expressions that have a dynamic layout.

JohanMabille commented 6 years ago

Also notice that xt::view returns an xview and not an xstrided_view, so this bug should not impact your code heavily relying on xt::view.

SylvainCorlay commented 6 years ago

@chavid xtensor 0.15.8 was released with that fix included.