xtensor-stack / xtensor

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

When using adapt with strides, the assignment operator doesn't work correctly #2330

Open vakokako opened 3 years ago

vakokako commented 3 years ago

When using adapt with strides, the assignment operator doesn't work correctly.

#include <iostream>
#include <xtensor/xadapt.hpp>
#include <xtensor/xio.hpp>
#include <xtensor/xtensor.hpp>
#include <xtensor/xview.hpp>

int main() {
    xt::xtensor<float, 2> tensor_input = xt::arange(25).reshape({5, 5});
    xt::xtensor<float, 2> tensor_output  = xt::arange(9).reshape({3, 3});

    auto column_output = xt::adapt(tensor_output.data(), 3lu, xt::no_ownership(), std::array{3lu, 1lu}, tensor_output.strides());

    const auto* ptr  = &tensor_input[{0, 0}];
    auto column_input = xt::adapt(ptr + 2, 3lu, xt::no_ownership(), std::array{3lu, 1lu}, tensor_input.strides());

    std::cout << "tensor_output before: \n" << tensor_output << "\n";
    std::cout << "column_output : \n" << column_output << "\n";
    std::cout << "column_input : \n" << column_input << "\n";

    // Error: assignes column_input to the first row of tensor_output instead of column
    column_output = column_input;
    std::cout << "tensor_output after: \n" << tensor_output << "\n";

}

Output:

tensor_output before: 
{{ 0.,  1.,  2.},
 { 3.,  4.,  5.},
 { 6.,  7.,  8.}}
column_output : 
{{ 0.},
 { 3.},
 { 6.}}
column_input : 
{{  2.},
 {  7.},
 { 12.}}
tensor_output after: 
{{  2.,   7.,  12.},
 {  3.,   4.,   5.},
 {  6.,   7.,   8.}}
JohanMabille commented 3 years ago

Looks like a bug (I can reproduce).

vakokako commented 3 years ago

Hi Johan, we have just discovered this again, so just kindly bumping this up...

emmenlau commented 2 years ago

Hi there, I'm just kindly kindly bumping this up again...

emmenlau commented 1 year ago

Just kindly bumping this up again. It's a nasty nasty bug, after all... :-) :-)

emmenlau commented 10 months ago

Just kindly bumping this up again...