Work and Note

Tf Quantize Training

Follow me on GitHub

intro

This post includes some details on quantize aware training.

contrib/python/quantize/python/quantize_graph.py:create_eval_graph _create_graph get_default_graph / get_concrete_function

  • contrib/python/quantize/python/quantize.py:Quantize
  • _FindLayersToQuantize
    • graph_matcher algorithm ?
    • a list of complex matching pattern is recorded in the _FindLayersToQuantize
    • find layer_match
  • _InsertQuantOp
    • will quantize the following target:
      • layer_match.weight_tensor
      • layer_match.activation_op
      • layer_match.bias_add_op
      • layer_match.bypass_op,
    • will add the following node:
      • quant_ops.py:MovingAvgQuantize
      • quant_ops.py:LastValueQuantize

If it is training, minmax is updated with some math operations:

Reference

Then, the following ops are added based on per-channel setting.

  • fake_quant_with_min_max_vars_per_channel
  • fake_quant_with_min_max_vars

After the fake quant node is inserted, the graph can be used as a normal tensorflow graph.

Reference

The forward path:

Reference

The backward path:

Reference

side note on how variable is stored

  • base on this post
  • _ModelVariable -> minmax value
  • vars_collections -> default value ops.GraphKeys.GLOBAL_VARIABLES

tf 2.0

python/eager/def_function.py:function -> @tf.function

  • make decorator make it possible to insert function into class

ResourceVariable ?

tensorflow graph collection

ops.py::Graph:get_collection(_VARSCOPESTORE_KEY)