MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / forward

Method forward

python/paddle/nn/quant/quant_layers.py:106–169  ·  view source on GitHub ↗
(self, input: Tensor)

Source from the content-addressed store, hash-verified

104 self._scale = None
105
106 def forward(self, input: Tensor) -> Tensor:
107 if in_dynamic_mode():
108 attrs = ('bit_length', self._quant_bits)
109 quant_out = _create_tensor(
110 type=input.type,
111 name=f"{input.name}.quantized.dequantized",
112 shape=input.shape,
113 dtype=input.dtype,
114 persistable=False,
115 )
116 out_scale = self._scale
117 if self._reduce_type == "max":
118 paddle.distributed.all_reduce(
119 out_scale, op=paddle.distributed.ReduceOp.MAX
120 )
121
122 if not out_scale:
123 out_scale = _create_tensor(
124 type=core.VarDesc.VarType.DENSE_TENSOR,
125 name=self._scale_name,
126 shape=[1],
127 dtype=self._dtype,
128 persistable=False,
129 )
130 out_scale.stop_gradient = True
131 (
132 out1,
133 out2,
134 ) = _C_ops.fake_quantize_dequantize_abs_max(
135 input, self._quant_bits, 1
136 )
137 _C_ops.assign_out_(out1, quant_out)
138 _C_ops.assign_out_(out2, out_scale)
139 return quant_out
140
141 check_variable_and_dtype(input, 'input', ['float32'], "FakeQuantAbsMax")
142 attrs = {'bit_length': self._quant_bits}
143 inputs = {"X": [input]}
144 quant_out = self._helper.create_variable(
145 name=f"{input.name}.quantized.dequantized",
146 dtype=input.dtype,
147 type=core.VarDesc.VarType.DENSE_TENSOR,
148 persistable=False,
149 stop_gradient=False,
150 )
151 out_scale = self._scale
152 if not out_scale:
153 out_scale = self._helper.create_variable(
154 name=self._scale_name,
155 dtype=self._dtype,
156 type=core.VarDesc.VarType.DENSE_TENSOR,
157 persistable=False,
158 stop_gradient=True,
159 )
160 outputs = {"Out": [quant_out], "OutScale": [out_scale]}
161
162 self._helper.append_op(
163 type="fake_quantize_dequantize_abs_max",

Callers

nothing calls this directly

Calls 5

_create_tensorFunction · 0.90
check_variable_and_dtypeFunction · 0.90
all_reduceMethod · 0.45
create_variableMethod · 0.45
append_opMethod · 0.45

Tested by

no test coverage detected