octree_attention
OctFormer octree window attention with relative position encoding.
Classes:
-
OctreeTβAn enhanced Octree with transformer-specific capabilities (patching, dilation, masking).
-
OctreeRelativePositionEncodingβRelative Position Encoding (RPE) module used within the
OctreeAttentionmodule. -
OctreeAttentionβMulti-head self-attention restricted to windows of
patch_sizeconsecutive octree nodes.
OctreeT [source] ΒΆ
OctreeT(
depth: int,
patch_size: int,
dilation: int,
full_depth: int = 2,
batch_size: int = 1,
device: Union[device, str] = "cpu",
**kwargs: Any,
)
Bases: Octree
An enhanced Octree with transformer-specific capabilities (patching, dilation, masking).
Can be instantiated directly like a standard Octree, or created from an existing Octree instance using OctreeT.from_octree().
Once a OctreeT is instantiated, you can build the transformer context (i.e. all attention masks and relative positions) by calling the method construct_all_attention_context().
Example
>>> octree_t = OctreeT.from_octree(octree, patch_size=26, dilation=4) # doctest: +SKIP
>>> octree_t.construct_all_attention_context( # doctest: +SKIP
... nempty=True,
... min_depth=6,
... max_depth=10,
... )
>>> octree_t.masks[6].shape # doctest: +SKIP
>>> octree_t.dilated_masks[6].shape # doctest: +SKIP
>>> octree_t.rel_pos[6].shape # doctest: +SKIP
>>> octree_t.dilated_rel_pos[6].shape # doctest: +SKIP
Methods:
-
resetβResets the
OctreeTto its initial state. -
from_octreeβCreates an
OctreeTfrom anOctree. -
construct_all_attention_contextβConstructs all attention context for the octree.
-
construct_attention_contextβCalculates attention masks, relative positions, and padding indices
-
pad_to_patch_sizeβPads
xalong its first dimension so the node count atdepthis a whole number of patches. -
unpadβDrops the padding added by
pad_to_patch_size, restoring the real node count atdepth.
Attributes:
-
block_size(int) βNumber of octree nodes a dilated patch spans, \(\text{patch\_size} \cdot \text{dilation}\).
block_size property ΒΆ
Number of octree nodes a dilated patch spans, \(\text{patch\_size} \cdot \text{dilation}\).
from_octree classmethod [source] ΒΆ
from_octree(
octree: Octree,
patch_size: int,
dilation: int,
**kwargs: Any,
) -> OctreeT
Creates an OctreeT from an Octree.
Parameters:
-
octree(Octree) βThe
Octreeto create theOctreeTfrom. -
patch_size(int) βThe patch size to use for the
OctreeT. -
dilation(int) βThe dilation to use for the
OctreeT. -
**kwargs(Any, default:{}) βAdditional keyword arguments to pass to the
OctreeTconstructor.
construct_all_attention_context [source] ΒΆ
construct_all_attention_context(
nempty: bool = False,
min_depth: Optional[int] = None,
max_depth: Optional[int] = None,
) -> None
Constructs all attention context for the octree.
Parameters:
-
nempty(bool, default:False) βWhether to use non-empty nodes.
-
min_depth(Optional[int], default:None) βThe start depth of the octree to construct the context for.
-
max_depth(Optional[int], default:None) βThe end depth of the octree to construct the context for.
construct_attention_context [source] ΒΆ
Calculates attention masks, relative positions, and padding indices required for attention operations.
Parameters:
-
depth(int) βThe depth of the octree to construct the context for.
-
nempty(bool, default:False) βWhether to use non-empty nodes.
OctreeRelativePositionEncoding [source] ΒΆ
Bases: Module
Relative Position Encoding (RPE) module used within the OctreeAttention module.
Parameters:
-
patch_size(int) βThe patch size to use for the RPE.
-
num_heads(int) βThe number of heads to use for the RPE.
-
dilation(int, default:1) βThe dilation to use for the RPE.
Methods:
-
pos_to_idxβClamps the relative positions to \(\pm\)
pos_bndand maps each axis to its slice of the RPE table.
OctreeAttention [source] ΒΆ
OctreeAttention(
channels: int,
patch_size: int,
num_heads: int,
dilation: int = 1,
qkv_bias: bool = True,
qk_scale: Optional[float] = None,
attn_drop: float = 0.0,
proj_drop: float = 0.0,
use_rpe: bool = True,
)
Bases: Module
Multi-head self-attention restricted to windows of patch_size consecutive octree nodes.
A dilation above \(1\) interleaves the nodes before windowing, so a patch spans a wider region at the same cost. The attention masks and relative positions come from the OctreeT, which must have been built with construct_all_attention_context().
Parameters:
-
channels(int) βNumber of input and output channels.
-
patch_size(int) βNumber of octree nodes attending to each other.
-
num_heads(int) βNumber of attention heads.
-
dilation(int, default:1) βStride between the nodes of a patch.
-
qkv_bias(bool, default:True) βWhether to use a bias in the QKV projection.
-
qk_scale(Optional[float], default:None) βScaling factor for the QK matrix. Defaults to the inverse square root of the head dimension.
-
attn_drop(float, default:0.0) βDropout rate for the attention.
-
proj_drop(float, default:0.0) βDropout rate for the output projection.
-
use_rpe(bool, default:True) βWhether to add the relative position encoding to the attention logits.
Methods:
-
forward_attnβRuns the windowed attention on patched features of shape \((N, K, C)\), returning \((N \cdot K, C)\).
-
forward_projβApplies the output projection and its dropout.
-
forward_rpeβAdds the relative position bias to the attention logits, or returns them unchanged when
use_rpeis off.
Attributes:
-
dilated(bool) βWhether the patches interleave nodes rather than taking them consecutively.