learning.neighborhood_attention3d

learning.neighborhood_attention3d#

bartorch.learning.neighborhood_attention3d()#

Scaled dot-product attention of each voxel over the window^3 voxels around it.

The window is shifted inwards at an edge rather than cut, so each voxel sees the same number of neighbours. On a card with the library’s CUDA kernels, and for 8, 16, 32 or 64 channels per head, the passes and their gradients run there, with sums in float32 and memory linear in the volume; anywhere else a plain-torch version serves, which gathers every neighbour and needs window^3 times the memory of k.

Parameters:
  • q (torch.Tensor) – (n, heads, z, y, x, channels): queries, keys and values.

  • k (torch.Tensor) – (n, heads, z, y, x, channels): queries, keys and values.

  • v (torch.Tensor) – (n, heads, z, y, x, channels): queries, keys and values.

  • window (int, default=5) – Odd edge of the window, at most 9 and at most each extent.

  • bias (torch.Tensor, default=None) – (heads, window^3) added to the logits: a bias for each relative position.

Returns:

(n, heads, z, y, x, channels), of q’s dtype.

Return type:

torch.Tensor