3-D Scatter (Add)

Is there a way to implement 3-D scatter_/scatter_add_ in pytorch?
Specifically, I have a tensor X (shape (B, M, N)), an index tensor I of shape (B, C, D) giving indices into the X, and a values tensor V the same shape of as indices, (B, C, D). For indices (b, c, d) in I, I want to add V[b,c,d] to X[b,c,d].

Is there a way to implement this? The regular scatter_ function only indices using a single dim.

Is I holding the indices or are the actual indices stored in b, c and d?
In the latter case, would this work?

b = 10
m, n = 5, 6
c, d = 4, 2
x = torch.zeros(b, m, n)
b_idx = torch.empty(b, 1, 1, dtype=torch.long).random_(b)
c_idx = torch.empty(c, 1, dtype=torch.long).random_(c)
d_idx = torch.empty(d, dtype=torch.long).random_(d)
v = torch.randn(b, c, d)

x[b_idx, c_idx, d_idx] += v[b_idx, c_idx, d_idx]

If not, could you post a small sample, what’s stored in I?

I want to do the same thing. However, its in my training so I want to retain the gradient. I get a RuntimeError: a leaf Variable that requires grad is being used in an in-place operation. because X is my CNN outputs. So I have to use torch.scatter but I can’t wrap my head on how to do that ?