facebookresearch / theseus

A library for differentiable nonlinear optimization
MIT License
1.74k stars 124 forks source link

inplace arithmetic is not allowed #650

Closed tyhuang98 closed 2 months ago

tyhuang98 commented 2 months ago

❓ Questions and Help

Sorry to bother you! I meet the following problem when writing my error function:

“RuntimeError: vmap: inplace arithmetic(self, *extra_args) is not possible because there exists a Tensor other in extra_args that has more elements than self. This happened due to other being vmapped over but self not being vmapped over in a vmap. Please try to use out-of-place operators instead of inplace arithmetic. If said operator is being called inside the PyTorch framework, please file a bug report instead."

The code is in the following:

def quad_error_fn(optim_vars, aux_vars): [maps_opt] = optim_vars [maps_ij_va] = aux_vars

maps_opt = maps_opt.tensor
maps_ij_va = maps_ij_va.tensor

map_size = maps_ij_va.shape[1]
num_scalemap = int(maps_opt.shape[1] / map_size)

error = torch.zeros((1, map_size * num_scalemap * (num_scalemap - 1)), dtype=torch.float32)

for i in range(num_scalemap):
    for j in range(num_scalemap):
        if j != i:
            index_flag = 0 if j < i else 1
            index = i * (num_scalemap - 1) + j - index_flag
            error[:, map_size*index:map_size*(index+1)] = maps_ij_va[index, :] * maps_opt[:, i*map_size:(i+1)*map_size] - \
                                   maps_opt[:, j*map_size:(j+1)*map_size]       \\ where the error occurs

return error
tyhuang98 commented 2 months ago

Problem solved! Thanks!