Skip to content

PY-TORC0001 · fluent_tensor_call_chain

Count nested Torch function calls that a fluent tensor chain states more directly.

This is a deterministic rule for python. Read its implementation.

Resolve unshadowed Torch functions whose behavior a tensor already exposes as a method, then fold each nested application over one tensor into the chain it is equivalent to. Report a call whose chain reaches minimum_operations operations, because that is the point where the nested form reverses reading order and hides the tensor the operations act on. A power over a literal base folds into the base method that names it, so torch.pow(2.0, value) joins the chain as exp2. The value is the number of nested calls found.

Reading order is the whole argument. A fluent chain names the tensor once and then reads left to right in the order the operations run, while the nested form names the tensor last and reads inside out.

Each finding records the outer call range, the resolved tensor, and the ordered methods the chain folds into. The rewrite chooses the in-place method of each operation only when the whole expression is rebound to the tensor it reads, since that assignment already discards the prior value and no other alias can observe the difference.

A shadowed Torch alias, a keyword argument, an operation with no method form, and a chain shorter than minimum_operations are all left alone. A single call such as torch.log2(value) stays valid because there is no reading order to reverse. The rule does not claim that in-place operations are generally faster, only that a rebound value cannot observe them.

sigma = torch.pow(2.0, torch.round(torch.log2(sigma)))
scaled = torch.sqrt(torch.abs(weights))
sigma = sigma.log2_().round_().exp2_()
scaled = weights.abs().sqrt()
  • Cites “PyTorch documentation”, Tensor method reference, including the in-place variants. Open reference
  • Cites “PyTorch documentation”, autograd notes on in-place operations. Open reference
  • Cites “PyTorch documentation”, torch.pow. Open reference