Buffers and constant tensors#
Buffers are useful for two different jobs, and LEAPP treats them differently depending on whether they change during tracing:
State buffers are reassigned during execution and become feedback state.
Constant buffers are read but not reassigned and are baked into the exported model.
For modules, module() handles both cases automatically.
Static outputs#
Sometimes a node needs to output a constant tensor that is not derived
from any input. The static_outputs parameter on output_tensors()
handles this case:
import torch
import leapp
from leapp import annotate
leapp.start(name="static_example")
input_tensor = torch.tensor([1.0, 2.0, 3.0])
traced_input = annotate.input_tensors('my_node',
{'input': input_tensor})
# Computed output -- derived from the traced input.
computed_output = traced_input + 1.0
# Static output -- a constant, NOT derived from any input.
static_tensor = torch.tensor([4.0, 5.0, 6.0])
annotate.output_tensors(
'my_node',
{'computed': computed_output},
static_outputs={'static': static_tensor},
export_with="jit",
)
leapp.stop()
leapp.compile_graph()
The exported model returns both outputs: computed (input-dependent) and
static (always [4, 5, 6]).
static_outputs follows the same top-level naming contract as
output_tensors():
pass a dict of named raw tensors for plain static outputs, or
pass
TensorSemantics(...)/ a list ofTensorSemantics(...)if the static outputs should carry semantic metadata in the exported YAML.
Warning
Static outputs must be raw
torch.Tensorvalues. Using aTracedTensorwill raise an error.Bare top-level tensors are not accepted. Pass a dict of named raw tensors or
TensorSemantics(...)/ a list ofTensorSemantics(...).Static outputs are merged with the regular outputs in the compiled model — downstream nodes can consume them like any other output.