aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/training/quantize_training.i
blob: 1ab600bb2265f587106f603ba75b4e8fc6c1a16e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
61
62
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
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

%include "tensorflow/python/platform/base.i"

%{
#include "tensorflow/core/graph/quantize_training.h"
#include "tensorflow/core/lib/core/status.h"

static PyObject* DoQuantizeTrainingOnGraphDefHelper(
    const string& input_graph,
    int num_bits,
    TF_Status* out_status) {
  string result;
  // TODO(suharshs): Make the QuantizeAndDequantizeV2 configurable.
  tensorflow::Status status =
      tensorflow::DoQuantizeTrainingOnSerializedGraphDef(input_graph, num_bits,
      "QuantizeAndDequantizeV2", &result);
  if (!status.ok()) {
    Set_TF_Status_from_Status(out_status, status);
    Py_RETURN_NONE;
  }
  PyObject* py_str = PyBytes_FromStringAndSize(result.data(), result.size());
  if (!py_str) {
    Set_TF_Status_from_Status(out_status,
        tensorflow::Status(tensorflow::error::INTERNAL,
            "Failed to generate serialized string of the rewritten graph."));
    Py_RETURN_NONE;
  }

  return py_str;
}
%}

%ignoreall
%unignore DoQuantizeTrainingOnGraphDefHelper;

// Wrap this function
PyObject* DoQuantizeTrainingOnGraphDefHelper(
    const string& input_graph,
    int num_bits,
    TF_Status* out_status);


%insert("python") %{
from tensorflow.python.util import deprecation
from tensorflow.python.util.tf_export import tf_export

@deprecation.deprecated(
    None,
    "GraphDef quantized training rewriter is deprecated in the long term")
@tf_export(v1=["train.do_quantize_training_on_graphdef"])
def do_quantize_training_on_graphdef(input_graph, num_bits):
  """A general quantization scheme is being developed in `tf.contrib.quantize`.

  Consider using that instead, though since it is in the tf.contrib namespace,
  it is not subject to backward compatibility guarantees.
  """
  from tensorflow.core.framework.graph_pb2 import GraphDef
  from tensorflow.python.framework import errors
  with errors.raise_exception_on_not_ok_status() as status:
    graph = GraphDef()
    result_graph_string = DoQuantizeTrainingOnGraphDefHelper(
        input_graph.SerializeToString(), num_bits, status)

  graph.ParseFromString(result_graph_string)
  return graph

do_quantize_training_on_graphdef._tf_api_names = [
    'train.do_quantize_training_on_graphdef']
do_quantize_training_on_graphdef._tf_api_names_v1 = [
    'train.do_quantize_training_on_graphdef']
%}

%unignoreall