TensorPlay
Reference guides
latest (dev)
Copy
View Markdown

Latest development documentation · Updated 2026-10-08

tensorplay.overrides

This module exposes various helper functions for the __tensorplay_function__ protocol. See Extending TensorPlay for more details on the __tensorplay_function__ protocol.

The protocol is how non-tensor objects participate in tensor operations: when any argument of a tensorplay operation implements __tensorplay_function__, the operation delegates to that method instead of running its default path. The functions here are the plumbing both sides use — the predicates that detect such arguments, the dispatcher that hands control to them, and the utilities for testing and wrapping.

The gate and the delegation, as an operation’s public entry point uses them:

import tensorplay as tp
from tensorplay.overrides import has_tensorplay_function, handle_tensorplay_function

m = tp.ones(2)

assert not has_tensorplay_function((m,))   # plain tensors do not intercept

# inside a public API, the two calls pair up exactly like this:
if has_tensorplay_function((m, m)):        # False here — the default path runs
    result = handle_tensorplay_function(tp.add, (m, m), m, m)
else:
    result = tp.add(m, m)

handle_tensorplay_function is only meaningful behind the gate: the second argument is the tuple inspected for interceptors, everything after it is the actual call, and with no interceptor present it raises TypeError — “no implementation found” — rather than falling back. Operations expose this pairing so one custom argument reroutes the whole call to your implementation.

Functions

tensorplay.overrides.get_ignored_functions() → set[Callable[[...], Any]][source]

Return public callables that do not receive function-hook dispatch.

tensorplay.overrides.get_overridable_functions() → dict[Any, list[Callable[[...], Any]]][source]

Return namespaces and callables that participate in function hooks.

tensorplay.overrides.resolve_name(f: Any) → str | None[source]

Return the stable public name registered for a callable.

tensorplay.overrides.get_testing_overrides() → dict[Callable[[...], Any], Callable[[...], int]][source]

Return signature-preserving sentinels for every overridable callable.

tensorplay.overrides.handle_tensorplay_function(public_api: Callable[[...], _R], relevant_args: Iterable[Any], *args: Any, **kwargs: Any) → _R[source]

Dispatch a composite API through function hooks and active modes.

Distinct operand types are ordered from the most-derived type to the least-derived type; unrelated types retain their first-seen order. A hook may return NotImplemented to pass control to the next candidate.

tensorplay.overrides.has_tensorplay_function(relevant_args: Iterable[Any]) → bool[source]

Return whether any argument supplies a Python dispatch hook.

tensorplay.overrides.is_tensor_like(inp: Any) → bool[source]

Return whether inp is a Tensor (any subclass included) or implements a dispatch hook.

tensorplay.overrides.is_tensor_method_or_property(func: Callable[[...], Any]) → bool[source]

Return whether func is a Tensor method or property getter.

tensorplay.overrides.wrap_tensorplay_function(dispatcher: Callable[[...], Iterable[Any]]) → Callable[[Callable[[...], _R]], Callable[[...], _R]][source]

Decorate a composite function with hook discovery and dispatch.

tensorplay.overrides.redispatch_function(func: Any, types_: Iterable[type], args: Iterable[Any] | None, kwargs: dict[str, Any] | None) → _R[source]

Skip the current native dispatch layer and invoke func again.

Modes

TensorPlayFunctionMode

Context manager that handles function hooks for a dynamic scope.

On this page

Ask DeepWiki