cyclops-community / ctf

Cyclops Tensor Framework: parallel arithmetic on multidimensional arrays
Other
199 stars 53 forks source link

Reshaping sparse tensor into a shape containing a dimension of 1 would fail in python lib #111

Closed nickirk closed 3 years ago

nickirk commented 4 years ago

Hi Edgar,

I found another bug related to the sparse tensor in the python lib. Below is the minimum code to reproduce it.

import ctf
a = ctf.tensor([4,2],sp=1)
a.reshape(8,1)

or

import ctf
a = ctf.tensor([4,2],sp=1)
a.reshape(2,1,-1)

will result in segmentation fault and the tracing-back gives:

[Mac:10473] *** Process received signal ***
[Mac:10473] Signal: Segmentation fault: 11 (11)
[Mac:10473] Signal code: Address not mapped (1)
[Mac:10473] Failing at address: 0x0
[Mac:10473] [ 0] 0   libsystem_platform.dylib            0x00007fff70a945fd _sigtramp + 29
[Mac:10473] [ 1] 0   ???                                 0x00007f8cf2e98000 0x0 + 140243347537920
[Mac:10473] [ 2] 0   libctf.so                           0x000000010192bde9 _ZN7CTF_int6tensor8set_zeroEv + 761
[Mac:10473] [ 3] 0   libctf.so                           0x000000010193223d _ZN7CTF_int6tensor7reshapeEPKS0_PKcS4_ + 669
[Mac:10473] [ 4] 0   libctf.so                           0x0000000101931fdb _ZN7CTF_int6tensor7reshapeEPKS0_PKcS4_ + 59
[Mac:10473] [ 5] 0   core.cpython-37m-darwin.so          0x00000001015390c1 _ZL37__pyx_pw_3ctf_4core_6tensor_79reshapeP7_objectS0_S0_ + 5729
[Mac:10473] [ 6] 0   Python                              0x0000000100fb7f01 _PyMethodDef_RawFastCallKeywords + 537
[Mac:10473] [ 7] 0   Python                              0x0000000100fbc701 _PyMethodDescr_FastCallKeywords + 81
...

I managed to nail down the bug to these two lines in the core.pyx file:

def reshape(tensor self, *integer):
...
    if nega == 0:
...
        (<ctensor*>B.dt).reshape(<ctensor*>self.dt, alpha, beta)
...
    if nega == 1:
        (<ctensor*>B.dt).reshape(<ctensor*>self.dt, alpha, beta)
...

One straightforward solution would be checking if the tensor is sparse, then use your commented out lines to write the values to the reshaped tensor:

    inds, vals = self.read_local_nnz()
    B.write(inds, vals)

But I don't know how efficient it will be. Maybe it is better to correct the behaviour of the underlying c++ function.

solomonik commented 4 years ago

Thanks for reporting this, should be corrected by the latest commit 29885ba