xtensor-stack / xtensor

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

xt::view assignment to another xt::view results in segmentation fault #872

Closed iamthebot closed 6 years ago

iamthebot commented 6 years ago
0   native.cpython-36m-darwin.so    0x0000000a14bd18de void xt::stepper_tools<(xt::layout_type)1>::increment_stepper<xt::data_assigner<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>, xt::xview<xt::pyarray<unsigned char> const&, int>, (xt::layout_type)1>, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true> >(xt::data_assigner<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>, xt::xview<xt::pyarray<unsigned char> const&, int>, (xt::layout_type)1>&, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>&, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true> const&) + 74 (xiterator.hpp:463)
1   native.cpython-36m-darwin.so    0x0000000a14bc874d xt::data_assigner<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>, xt::xview<xt::pyarray<unsigned char> const&, int>, (xt::layout_type)1>::run() + 155 (xassign.hpp:364)
2   native.cpython-36m-darwin.so    0x0000000a14bbf227 void xt::xexpression_assigner_base<xt::xtensor_expression_tag>::assign_data<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>, xt::xview<xt::pyarray<unsigned char> const&, int> >(xt::xexpression<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag> >&, xt::xexpression<xt::xview<xt::pyarray<unsigned char> const&, int> > const&, bool) + 191 (xassign.hpp:269)
3   native.cpython-36m-darwin.so    0x0000000a14bb272b void xt::xexpression_assigner<xt::xtensor_expression_tag>::assign_xexpression<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>, xt::xview<xt::pyarray<unsigned char> const&, int> >(xt::xexpression<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag> >&, xt::xexpression<xt::xview<xt::pyarray<unsigned char> const&, int> > const&) + 61 (xassign.hpp:280)
4   native.cpython-36m-darwin.so    0x0000000a14ba5218 auto void xt::assign_xexpression<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>, xt::xview<xt::pyarray<unsigned char> const&, int> >(xt::xexpression<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag> >&, xt::xexpression<xt::xview<xt::pyarray<unsigned char> const&, int> > const&)::'lambda0'(xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>)::operator()<xtl::identity>(xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>) const + 38 (xassign.hpp:167)
5   native.cpython-36m-darwin.so    0x0000000a14ba523c decltype(auto) xtl::mpl::static_if<void xt::assign_xexpression<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>, xt::xview<xt::pyarray<unsigned char> const&, int> >(xt::xexpression<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag> >&, xt::xexpression<xt::xview<xt::pyarray<unsigned char> const&, int> > const&)::'lambda'(xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>), void xt::assign_xexpression<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>, xt::xview<xt::pyarray<unsigned char> const&, int> >(xt::xexpression<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag> >&, xt::xexpression<xt::xview<xt::pyarray<unsigned char> const&, int> > const&)::'lambda0'(xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>)>(std::integral_constant<bool, false>, xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag> const&, xt::xview<xt::pyarray<unsigned char> const&, int> const&) + 33 (xmeta_utils.hpp:471)
6   native.cpython-36m-darwin.so    0x0000000a14ba526b decltype(auto) xtl::mpl::static_if<false, void xt::assign_xexpression<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>, xt::xview<xt::pyarray<unsigned char> const&, int> >(xt::xexpression<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag> >&, xt::xexpression<xt::xview<xt::pyarray<unsigned char> const&, int> > const&)::'lambda'(xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>), void xt::assign_xexpression<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>, xt::xview<xt::pyarray<unsigned char> const&, int> >(xt::xexpression<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag> >&, xt::xexpression<xt::xview<xt::pyarray<unsigned char> const&, int> > const&)::'lambda0'(xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>)>(xt::xview<xt::pyarray<unsigned char> const&, int> const&, void xt::assign_xexpression<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>, xt::xview<xt::pyarray<unsigned char> const&, int> >(xt::xexpression<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag> >&, xt::xexpression<xt::xview<xt::pyarray<unsigned char> const&, int> > const&)::'lambda0'(xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>) const&) + 40 (xmeta_utils.hpp:477)
7   native.cpython-36m-darwin.so    0x0000000a14ba52b5 void xt::assign_xexpression<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>, xt::xview<xt::pyarray<unsigned char> const&, int> >(xt::xexpression<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag> >&, xt::xexpression<xt::xview<xt::pyarray<unsigned char> const&, int> > const&) + 67 (xassign.hpp:168)
8   native.cpython-36m-darwin.so    0x0000000a14b90cb5 xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>& xt::xcontainer_semantic<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag> >::assign_xexpression<xt::xview<xt::pyarray<unsigned char> const&, int> >(xt::xexpression<xt::xview<xt::pyarray<unsigned char> const&, int> > const&) + 35 (xsemantic.hpp:566)
9   native.cpython-36m-darwin.so    0x0000000a14b78642 xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>& xt::xsemantic_base<xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag> >::assign<xt::xview<xt::pyarray<unsigned char> const&, int> >(xt::xexpression<xt::xview<xt::pyarray<unsigned char> const&, int> > const&) + 46 (xsemantic.hpp:471)
10  native.cpython-36m-darwin.so    0x0000000a14b5b40d xt::xarray_container<xt::uvector<float, xsimd::aligned_allocator<float, 32ul> >, (xt::layout_type)0, xt::svector<unsigned long, 4ul, std::allocator<unsigned long>, true>, xt::xtensor_expression_tag>::xarray_container<xt::xview<xt::pyarray<unsigned char> const&, int> >(xt::xexpression<xt::xview<xt::pyarray<unsigned char> const&, int> > const&) + 119 (xarray.hpp:412)
11  native.cpython-36m-darwin.so    0x0000000a14b3a9b3 xt::xview<xt::pyarray<float>&, int>& xt::xsemantic_base<xt::xview<xt::pyarray<float>&, int> >::operator=<xt::xview<xt::pyarray<unsigned char> const&, int> >(xt::xexpression<xt::xview<xt::pyarray<unsigned char> const&, int> > const&) + 51 (xsemantic.hpp:543)
12  native.cpython-36m-darwin.so    0x0000000a14b1ffed xt::xview<xt::pyarray<float>&, int>& xt::xview_semantic<xt::xview<xt::pyarray<float>&, int> >::operator=<xt::xview<xt::pyarray<unsigned char> const&, int> >(xt::xexpression<xt::xview<xt::pyarray<unsigned char> const&, int> > const&) + 317 (xsemantic.hpp:652)
13  native.cpython-36m-darwin.so    0x0000000a14b12f63 xt::xview<xt::pyarray<float>&, int>& xt::xview<xt::pyarray<float>&, int>::operator=<xt::xview<xt::pyarray<unsigned char> const&, int> >(xt::xexpression<xt::xview<xt::pyarray<unsigned char> const&, int> > const&) + 35 (xview.hpp:460)
14  native.cpython-36m-darwin.so    0x0000000a14c0914b bighead::transformers::IMGMeanScaler::transform(xt::pyarray<unsigned char> const&) const (._omp_fn.29) + 247 (images.hpp:529)
15  libgomp.1.dylib                 0x0000000a156e300f GOMP_parallel + 63
16  native.cpython-36m-darwin.so    0x0000000a14b0a116 bighead::transformers::IMGMeanScaler::transform(xt::pyarray<unsigned char> const&) const + 870 (images.hpp:534)

This is the result of attaching an xt::view from a pyarray to another view on an xt::pyarray. This was working fine in previous releases but no longer working when I upgraded xtensor-python and xtensor.

iamthebot commented 6 years ago

Confirmed that this started breaking in 0.16.0 w/ the corresponding xtensor-python release.

wolfv commented 6 years ago

Any chance you could post what the slices are in the view? Will investigate today

wolfv commented 6 years ago

I tried to replicate with this code (using xarray) but didn't manage to produce a segfault.

    xt::xarray<float> x3 = xt::arange(0, 100);
    x3.reshape({5, 5, 4});
    xarray<unsigned char> x4 = x3;
    xt::view(x3, 0) = xt::view(x4, 1);
    xt::view(x3, 1) = xt::view(x4, 0);
    xt::view(x3, 2) = xt::view(x4, 2);
    xt::view(x3, 3) = xt::view(x4, 4);
    auto v = xt::view(x4, 3);
    xt::view(x3, 4, 0) = xt::view(v, 2);
    std::cout << x3 << std::endl;
wolfv commented 6 years ago

(using 0.16.0 and master)

wolfv commented 6 years ago

This was my attempt to recreate in python but I still don't get a segfault (feeded it from python with arr = np.arange(3 * 10 * 10, dtype=np.int32).reshape(3, 10, 10); print(tz.vass(arr)).

xt::pyarray<float>
test_view_assign(xt::pyarray<int>& arr)
{
    xt::pyarray<float> res = xt::zeros<int>({3, 10, 10});

    for (int i = 0; i < arr.shape()[0]; ++i)
    {
        std::cout << "I == " << i % 3 << std::endl;
        auto v1 = xt::view(res, i % 3);
        auto v2 = xt::view(arr, i);
        v1 = v2;
    }

    return res;
}

PYBIND11_PLUGIN(tzwo)
{
    xt::import_numpy();
    py::module m("tzwo", "Test module for xtensor python bindings");
    m.def("view_ass", test_view_assign);
    return m.ptr();
}
wolfv commented 6 years ago

@iamthebot do you still see this issue with the latest releases?

iamthebot commented 6 years ago

@wolfv yeah this is still broken in 0.16.2. Here is a minimal example:

   xt::xarray<std::uint8_t> images;
   xt::xarray<float> output;
   images.resize({{100,512,512,3}});
   output.resize({{100,512,512,3}});

   for(int i=0; i<100; ++i)
   {
       auto img_view = xt::view(images, i);
       //copy input to output as floats
       auto out_view = xt::view(output, i);
       std::printf("created views\n");
       out_view = img_view; //this causes a segfault
       std::printf("copied input to output\n");
   }

edited to simplify failing example

iamthebot commented 6 years ago

Also, changing output to be an xt::xtensor<float, 4> prevents the segfault for some reason.