You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.7 KiB

6 years ago
  1. # Copyright 2018 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """Functions for quantized training and evaluation."""
  16. import tensorflow as tf
  17. def build(graph_rewriter_config, is_training):
  18. """Returns a function that modifies default graph based on options.
  19. Args:
  20. graph_rewriter_config: graph_rewriter_pb2.GraphRewriter proto.
  21. is_training: whether in training of eval mode.
  22. """
  23. def graph_rewrite_fn():
  24. """Function to quantize weights and activation of the default graph."""
  25. if (graph_rewriter_config.quantization.weight_bits != 8 or
  26. graph_rewriter_config.quantization.activation_bits != 8):
  27. raise ValueError('Only 8bit quantization is supported')
  28. # Quantize the graph by inserting quantize ops for weights and activations
  29. if is_training:
  30. tf.contrib.quantize.create_training_graph(
  31. input_graph=tf.get_default_graph(),
  32. quant_delay=graph_rewriter_config.quantization.delay)
  33. else:
  34. tf.contrib.quantize.create_eval_graph(input_graph=tf.get_default_graph())
  35. tf.contrib.layers.summarize_collection('quant_vars')
  36. return graph_rewrite_fn