Functions¶
NestedTensor operation support is split by dispatch layer, not by a single
utils.py module. The public documentation follows that structure:
Torch Functions¶
danling.tensors.torch_functions registers torch.* handlers through
__torch_function__, such as torch.cat, torch.stack, reductions, indexing,
and matrix operations.
danling.tensors.torch_functions
¶
torch.* function overrides for NestedTensor via __torch_function__.
This module is the Level 2 dispatch layer. When a torch.* call
(e.g. torch.cat, torch.mean, torch.einsum) involves a
NestedTensor, __torch_function__ checks
NestedTensorFuncRegistry for a
registered handler.
Handlers here use several strategies depending on the op’s needs:
- Packed fast-path — ops that work directly on the concatenated
concattensor viaNestedTensor.from_packedwithout knowing element boundaries. - Per-element dispatch — ops that must be applied to each element
individually via
_map_storage_serial, e.g. when dimension indices need translation or output shapes differ per element.
If no handler is registered here, the call falls through to aten
decomposition and then to __torch_dispatch__ (see aten_functions).
NN Functions¶
danling.tensors.nn_functions registers torch.nn.functional.* handlers such
as attention, embedding, normalization, pooling, convolution, and loss
functions.
danling.tensors.nn_functions
¶
torch.nn.functional.* overrides for NestedTensor via __torch_function__.
This module is the Level 3 dispatch layer, registering handlers for
F.linear, F.conv*, F.max_pool*, F.embedding,
F.layer_norm, F.scaled_dot_product_attention, and other
torch.nn.functional ops.
Handlers preserve logical sample boundaries, native dtypes and differentiation semantics. Compiled paths use packed tensor operations or segmented native kernels with explicit metadata and autograd registrations. Compile support is recorded per operator and layout; remaining per-element handlers still require packed implementations before they can enter fullgraph execution.
danling.tensors.nn_functions.create_flex_block_mask
¶
create_flex_block_mask(mask_mod: Callable, query: NestedTensor, key: NestedTensor | None = None, *, num_heads: int | None = None, block_size: int | tuple[int, int] = 128, compile_mask: bool = False)
Create a FlexAttention block mask directly from DanLing ragged attention storage.
Source code in danling/tensors/nn_functions.py
| Python | |
|---|---|
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 | |
Aten Functions¶
danling.tensors.aten_functions registers packed-storage
__torch_dispatch__ handlers and fallback behavior for aten ops.
danling.tensors.aten_functions
¶
__torch_dispatch__ handlers for NestedTensor aten ops (Level 1 dispatch).
This module implements the dispatch table that maps aten ops to optimized handlers operating on the packed representation (_values, _offsets, _physical_shape).
Architecture
- Elementwise ops operate directly on
_values(no unpack/repack overhead) - Structural ops (clone, detach, to_copy) operate on all inner tensors
- Unregistered ops fall back to per-element application via
_storage
Dispatch Registries¶
danling.tensors.ops provides registry types, dispatch tables, and diagnostic
helpers used to extend or test NestedTensor operation support.
danling.tensors.ops
¶
Internal helpers shared across NestedTensor function registrations.
TorchFuncRegistry
¶
Bases: dict
Plain dict mapping functions/ops to their NestedTensor handlers.
Uses dict directly for O(1) lookup with minimal overhead (~30 ns)
instead of chanfig.Registry (~700-2300 ns).
Used for both __torch_function__ (torch/nn ops) and
__torch_dispatch__ (aten ops) dispatch tables.
Source code in danling/tensors/ops.py
| Python | |
|---|---|
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
register
¶
register(func: Callable, handler: Callable, *, compile_safe: bool = False, compile_guard: Callable[[tuple, dict[str, object]], bool] | None = None) -> Callable
Register handler for func and record whether the path is compile-safe by default.
Source code in danling/tensors/ops.py
| Python | |
|---|---|
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
implement
¶
implement(func: Callable, *, compile_safe: bool = False, compile_guard: Callable[[tuple, dict[str, object]], bool] | None = None) -> Callable
Decorator to register a handler for func.
Source code in danling/tensors/ops.py
| Python | |
|---|---|
206 207 208 209 210 211 212 213 214 215 216 217 218 | |
is_compile_safe
¶
is_compile_safe(func: Callable, args: tuple | None = None, kwargs: dict[str, object] | None = None) -> bool
Return whether func is allowed to run while torch.compile is tracing.
Source code in danling/tensors/ops.py
| Python | |
|---|---|
220 221 222 223 224 225 226 227 228 229 | |
set_compile_safe
¶
Update compile policy for an already-registered handler.
Source code in danling/tensors/ops.py
| Python | |
|---|---|
231 232 233 234 235 | |
set_compile_guard
¶
set_compile_guard(func: Callable, guard: Callable[[tuple, dict[str, object]], bool] | None) -> None
Set or clear the runtime compile guard for an already-registered handler.
Source code in danling/tensors/ops.py
| Python | |
|---|---|
237 238 239 240 241 242 243 244 | |
get_compile_guard
¶
Return the runtime compile guard for func, if any.
Source code in danling/tensors/ops.py
| Python | |
|---|---|
246 247 248 | |
nested_execution_guard
¶
nested_execution_guard(*, forbid_iteration: bool = False, forbid_storage_map: bool = False, forbid_eager_fallback: bool = False, forbid_padded_materialization: bool = False, forbid_dense_repack: bool = False)
Temporarily forbid selected slow paths while exercising NestedTensor hot paths.
This is intended for transformer-critical regression checks, where falling back to Python loops or padded materialization is considered a bug.
Source code in danling/tensors/ops.py
| Python | |
|---|---|
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
The files under danling.tensors.functions are specialized implementations
used by nn_functions for convolution, pooling, and channel operators. They are
kept out of the docs navigation because users normally call the corresponding
PyTorch or torch.nn.functional API directly.