mitsuba-renderer / drjit

Dr.Jit — A Just-In-Time-Compiler for Differentiable Rendering
BSD 3-Clause "New" or "Revised" License
593 stars 43 forks source link

fix bug in tensor broadcasting #146

Closed Andy3531 closed 1 year ago

Andy3531 commented 1 year ago

The original code will fail on multipling two tensors of shape (512,512,4) and (1,1,4) because of the wrong recursive direction in the for loop.

njroussel commented 1 year ago

Hi @Andy3531

Could you provide us with a reproducer for the original issue? I tried something naive and it seems to be working as intended.

Andy3531 commented 1 year ago

Yes, if we run the following code,

size_t shape0[3]={512,512,4}; 
TensorXf tensor0(dr::zeros<typename TensorXf::Array>(512*512*4), 3, shape0);`

size_t shape1[3]={1,1,4};
TensorXf tensor1(dr::zeros<typename TensorXf::Array>(4), 3, shape1);

tensor0 = tensor0.mul_(tensor1);

we will get an error: Caught a critical exception: ArrayBase: out of range access (tried to access index 4 in an array of size 4)

where type TensorXf is defined here

https://github.com/mitsuba-renderer/mitsuba3/blob/9273af4798f27a9235fc275753a2779088fb4bfd/include/mitsuba/core/fwd.h#L179

Andy3531 commented 1 year ago

And similar problem is encountered in this issue: @Speierers in https://github.com/mitsuba-renderer/drjit/issues/85#issuecomment-1272843300_

njroussel commented 1 year ago

It took me a while to figure it out, but I finally was able to reproduce this... So, in JIT variants this doesn't crash, but produces a potentially wrong output. Only in scalar mode does the error message appear.