TensorPlay
latest (dev)
Copy
View Markdown

Latest development documentation · Updated 2026-10-08

tensorplay.func.functionalize

tensorplay.func.functionalize(func: Callable, *, remove: str = 'mutations') → Callable

Returns a version of func that leaves its arguments untouched.

func may write into its inputs in place; the wrapper hands it private copies instead, so the caller’s tensors – and any views onto them – come back exactly as they went in, while the returned value is unchanged.

Parameters:
  • func (Callable) – the function to make side-effect free.

  • remove (str) – "mutations" copies every tensor argument. "mutations_and_views" additionally detaches the copies from any aliasing they arrived with, so writes through a view of an argument cannot reach another argument either.

Example

>>> def f(x):
...     x.add_(1)
...     return x
>>> x = tensorplay.zeros(3)
>>> out = functionalize(f)(x)
>>> x  # unchanged
tensor([0., 0., 0.])

On this page

Ask DeepWiki