14bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
24bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang#
34bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang# Licensed under the Apache License, Version 2.0 (the "License");
44bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang# you may not use this file except in compliance with the License.
54bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang# You may obtain a copy of the License at
64bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang#
74bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang#    http://www.apache.org/licenses/LICENSE-2.0
84bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang#
94bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang# Unless required by applicable law or agreed to in writing, software
104bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang# distributed under the License is distributed on an "AS IS" BASIS,
110815de21239955e346b562e899640649c8d2b9cbBenoit Steiner# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang# See the License for the specific language governing permissions and
134bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang# limitations under the License.
144bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang# ============================================================================
154bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang"""Tensorflow op performing fused conv2d bias_add and relu."""
164bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang
174bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wangfrom __future__ import absolute_import
184bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wangfrom __future__ import division
194bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wangfrom __future__ import print_function
204bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang
214bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wangfrom tensorflow.contrib.fused_conv.ops import gen_fused_conv2d_bias_activation_op
224bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wangfrom tensorflow.contrib.util import loader
234bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wangfrom tensorflow.python.platform import resource_loader
244bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang
254bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang_fused_conv2d_bias_activation_op_so = loader.load_op_library(
264bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang    resource_loader.get_path_to_datafile("_fused_conv2d_bias_activation_op.so"))
274bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang
284bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang
292b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower# pylint: disable=redefined-builtin
302b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlowerdef fused_conv2d_bias_activation(conv_input,
312b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower                                 filter,
324bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang                                 bias,
332b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower                                 strides=None,
342b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower                                 padding=None,
352b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower                                 conv_input_scale=1.0,
362b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower                                 side_input_scale=0.0,
372b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower                                 side_input=None,
382b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower                                 activation_mode="Relu",
394bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang                                 data_format=None,
402b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower                                 filter_format=None,
414bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang                                 name=None):
422b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower  """Fused 2D conv, bias and activation with optional side input.
434bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang
442b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower  Computes a fused 2-D convolution scaled by conv_input_scale,
452b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower  adds an optional side input scaled by side_input_scale, adds biases,
462b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower  and applies ReLU. As an equation:
472b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower  output = ReLU(conv_input_scale * Conv(conv_input, filter) +
482b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower                side_input_scale * side_input + bias)
492b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower  Note: In int8 mode, The ReLU will clip the output to the range [0..127].
504bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang
514bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang  Args:
522b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower    conv_input: A `Tensor` of the format specified by `data_format`.
532b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower    filter: A `Tensor` whose format depends on `data_format`:
542b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        if `data_format` is "NCHW_VECT_C", filter should be "OIHW_VECT_I"
552b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        otherwise, it should be "HWIO" format.
562b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower    bias: A 1-D `Tensor` of type `float32`, and dimensions equal to the
572b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        number of output channels.
582b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower    strides: A list of 4 `ints` specifying convolution strides.
592b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        if `data_format` is "NCHW" or "NCHW_VECT_C", the order should be NCHW.
602b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        if `data_format` is "NHWC", the order should be NHWC.
614bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang    padding: A `string` from: `"SAME", "VALID"`.
622b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower    conv_input_scale: A scalar `float32` that will be multiplied by conv_input.
632b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        This is optional and defaults to 1. However it should be set to
642b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        specify the quantization scale when `data_format` is "NCHW_VECT_C".
652b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower    side_input_scale: A scalar `float32` that will be multiplied by side_input.
662b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        This is optional and defaults to 0.
672b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower    side_input: A `Tensor` of the format specified by `data_format`.
682b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        This is useful for imlementing ResNet blocks.
692b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower    activation_mode: (optional) currently must be the default "Relu".
702b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        Note that in qint8 mode, it also clips to 127, so acts like ReluX.
712b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower    data_format: Specifies the data format.
722b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        Possible values are:
732b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        "NHWC" float [batch, height, width, channels]
742b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        "NCHW" float [batch, channels, height, width]
752b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        "NCHW_VECT_C" qint8 [batch, channels / 4, height, width, channels % 4]
762b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        Defaults to `"NHWC"`.
772b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        Performance is worst for `"NHWC"` and best for `"NCHW_VECT_C"`.
782b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower    filter_format: Specifies the filter format.
792b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        Possible values are:
802b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        "HWIO" float [kernel_height, kernel_width, input_channels,
812b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower                      output_channels ]
822b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        "OIHW" float [output_channels, input_channels, kernel_height,
832b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower                      kernel_width ]
842b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        "OIHW_VECT_I" qint8 [ output_channels, input_channels / 4,
852b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower                              kernel_height, kernel_width, input_channels % 4 ]
862b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower        Defaults to `"HWIO"`.
874bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang    name: A name for the operation (optional).
884bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang
894bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang  Returns:
902b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower    A `Tensor` of the format specified by `data_format`.
914bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang  """
922b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower  if strides is None:
932b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower    strides = [1, 1, 1, 1]
942b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower  if side_input is None:
952b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower    side_input = []
964bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang  return gen_fused_conv2d_bias_activation_op.fused_conv2d_bias_activation(
972b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower      conv_input,
982b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower      filter,
992b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower      bias,
10060f15462bef286c83087805a5b2592d201bd26deA. Unique TensorFlower      side_input,
10160f15462bef286c83087805a5b2592d201bd26deA. Unique TensorFlower      conv_input_scale,
10260f15462bef286c83087805a5b2592d201bd26deA. Unique TensorFlower      side_input_scale,
10391b00a110feb83b7307ea9c280142007090f3cd9A. Unique TensorFlower      padding=padding,
1042b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower      strides=strides,
1054bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang      activation_mode=activation_mode,
1064bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang      data_format=data_format,
1072b15badd96c651d4d191426975a1773dff4a03b8A. Unique TensorFlower      filter_format=filter_format,
1084bbd9bd11fb52ebe0e3de6f8553a2372c13146bbYangzihao Wang      name=name)
109