Thanks for your kind help. Actually, the code is the simplified version of my problem. In the real situation, I cannot assign B[0, :, :].mm(A) to z. However, following your inspiration, I find use .clone() can solve my problem