quantizer_annotations
fastforward.quantization.quantizer_annotations.TraceTensor(data)
#
Bases: Tensor
A torch.Tensor subclass that tracks the preceding operation and quantizer.
TraceTensor instances store a reference to the previous_function (the operation that produced this tensor, set via __torch_function__) and
the previous_quantizer (the quantizer layer that consumed or produced this tensor,
set via quantizer_override). This tracking facilitates the annotation of Quantizer
layers with Tag(before/after:func_name) metadata, allowing the user to search specific layers in the graph.
Source code in fastforward/quantization/quantizer_annotations.py
26 27 28 29 | |
previous_function = None
instance-attribute
#
previous_quantizer = None
instance-attribute
#
__new__(data)
#
Source code in fastforward/quantization/quantizer_annotations.py
23 24 | |
__torch_function__(func, types, args=(), kwargs=None)
classmethod
#
Intercepts PyTorch operations on TraceTensor instances.
This method enables the tracking of computational graph flow. It annotates the previous_quantizer of input
TraceTensors with a Tag("before") / func.__name__ before the operation is performed. It also records the func
(the PyTorch operation) as the _previous_function on the resulting TraceTensor.
Source code in fastforward/quantization/quantizer_annotations.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
fastforward.quantization.quantizer_annotations.annotate_operator_metadata(model, sample_input)
#
Trace model execution and annotate Quantizer metadata with preceding and succeeding operators.
This function performs a lightweight tracing of the model's forward pass using a special
tracing tensor (TraceTensor) and quantizer overrides. It identifies the operators that immediately
precede and follow each ff.nn.Quantizer instance in the execution flow and adds
this information as Tags to the metadata (quant_metadata) within the Quantizer objects.
The annotation happens in-place on the Quantizer objects within the provided model.
We assume that ff.quantize has been called on the model to ensure that the model
contains Quantizer instances that need to be annotated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The |
required |
sample_input
|
Tensor
|
A sample input tensor to the model. This is used to drive
the forward pass for tracing purposes. If the module has conditional logic based on
input data, ensure that |
required |
Source code in fastforward/quantization/quantizer_annotations.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
fastforward.quantization.quantizer_annotations.quantizer_override(quantizer, callback, args, kwargs)
#
Register the operations that precede and follow this quantizer forward pass as Tags.
Combined with TraceTensor, this function will add both the preceeding and following operators
to each quantizer layer as Tag objects: (Tag("before") / func.__name__)andTag("after") / func.name`).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quantizer
|
Quantizer
|
A quantizer layer. |
required |
callback
|
Callable[..., Any]
|
The quantizer layer forward call. |
required |
args
|
tuple[Any, ...]
|
The arguments passed to the quantizer layer forward call. |
required |
kwargs
|
dict[str, Any]
|
Any key-word arguments passed to the quantizer layer forward call. |
required |
Source code in fastforward/quantization/quantizer_annotations.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |