yobx.torch.new_tracing.tensor#
- class yobx.torch.new_tracing.tensor.TracingTensor(size: Tuple[int, ...] | TracingShape, dtype: dtype, device: str | device | None = None, requires_grad: bool = False, tracer: GraphTracer | None = None)[source]#
A
torch.Tensorsubclass that records all dispatch-level operations into atorch.fx.Graphvia__torch_dispatch__.TracingTensoruses__torch_dispatch__to intercept all tensor operations at the C++ dispatcher level and records them as nodes in atorch.fx.Graph. This produces a full computation graph without requiring Python-level symbolic proxy objects.Note
TracingTensorinstances are created internally byDispatchTracer. Usetrace()to trace a callable rather than constructingTracingTensordirectly.It contains two attributes:
_tracer: TheDispatchTracermanaging this tensor’s graph._node: Thetorch.fx.Nodecorresponding to this tensor in the graph.
- classmethod from_tensor(t: Tensor, dynamic_shapes: Dict[int, Any] | None = None, tracer: GraphTracer | None = None) TracingTensor[source]#
Creates a tracing tensor.
- item() int | float | TracingInt[source]#
Intercepts
.item()during tracing to return a symbolicTracingIntbacked by a graph node instead of raising an error or returning a bare Python scalar.When a tracer is active, this method delegates to
_handle_local_scalar_dense(), which emits anaten._local_scalar_denseFX node and wraps the result in aTracingInt. The caller can then use the returnedTracingIntas a dynamic slice endpoint (e.g.x[..., :shape.item()]), which is recognised by_index_has_symbolic_tracing_int()and routed through_handle_symbolic_getitem().- Returns:
A
TracingInt(symbolic) when a tracer is active, or the actual Python scalar when no tracer is present (falls back totorch.Tensor.item()).
- make_empty_instance(dyanmic_shape_values: Dict[str, int] | None = None) Tensor[source]#
Allocates an uninitialised
torch.empty()tensor whose dtype and device match thisTracingTensor.Concrete integer dimensions are used as-is. Symbolic (string) dimensions must be resolved by supplying dyanmic_shape_values, a mapping from dimension name to its concrete integer value. A missing entry for any symbolic dimension raises
AssertionError.- Parameters:
dyanmic_shape_values – Optional mapping from symbolic dimension names (e.g.
"batch") to their concrete integer sizes.- Returns:
A real
torch.Tensorwith the resolved shape, the samedtype, and the samedeviceas thisTracingTensor. The tensor is uninitialised (contents are undefined).- Raises:
AssertionError – If a symbolic dimension name is not present in dyanmic_shape_values.
NotImplementedError – If a dimension has an unexpected type (i.e. neither
intnorstr).
- new_zeros(size: Any, **kwargs: Any) Any[source]#
Accepts a
TracingShapeas the size argument in addition to the types accepted by the base class.When a module attribute is temporarily replaced with a
TracingTensorduring tracing,self.attr.shapereturns aTracingShapeinstead of atorch.Size. The C++ pybind fornew_zerosdoes not accept custom sequence types, so this override converts concreteTracingShapeobjects to plaintupleofintbefore delegating to the parent implementation (which routes through__torch_dispatch__).- Parameters:
size – The desired output shape. A
TracingShapewith concrete integer dimensions is converted to a plaintuple; all other values are forwarded unchanged.kwargs – Additional keyword arguments forwarded to
torch.Tensor.new_zeros().
- Returns:
A
TracingTensorrepresenting the zero-filled tensor.
- numel() int | TracingInt[source]#
Computes the total number of elements from
_tracing_shape.Concrete integer dimensions contribute their actual value directly. Symbolic (string-valued)
TracingIntdimensions are folded into the product as symbolic terms, yielding aTracingIntreturn value.This ensures that guards of the form
if x.numel() == 0:can be resolved at trace time via theTracingBoolmechanism: models should usetorch._check(x.numel() != 0)to register the non-empty constraint (as withControlFlowShapeCheck), after which__bool__()resolves the equality toFalsevia its negation lookup.A concrete dimension of
0still causes an immediate return of0so that genuinely empty static shapes are identified correctly.When a tracer is active and the result is symbolic, FX nodes are emitted for the numel computation and stored on the returned
TracingInt(see_emit_numel_node()). Subsequent comparisons such asnumel() > 0then also emit comparison FX nodes, allowing the result to serve as atorch.condpredicate.- Returns:
Plain
intwhen every dimension is concrete;TracingIntwhen any dimension is symbolic.- Return type:
Union[int, TracingInt]
- property shape: TracingShape#
Returns the shape as a TracingShape.