• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CUDNN_FUSED_CONV_REWRITER_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CUDNN_FUSED_CONV_REWRITER_H_
18 
19 #include "tensorflow/compiler/xla/service/hlo_instructions.h"
20 #include "tensorflow/compiler/xla/service/hlo_pass_interface.h"
21 
22 namespace xla {
23 namespace gpu {
24 
25 // Rewrite the custom call targeting cudnnConvolutionForward to
26 // cudnnConvolutionBiasActivationForward by fusing applicable point-wise
27 // operations following forward convolution.  This transform must run after
28 // cudnn_conv_rewriter.
29 // It is straightforward for floating point convolutions:
30 // transforming
31 //   max(0, alpha1 * conv(x, w) + alpha2 * side_input + broadcast(bias))
32 // to
33 //   cudnnConvolutionBiasActivationForward(x, w, bias, alpha1, alpha2, side)
34 //
35 // Integer convolution requires additional patterns to match CuDNN semantics:
36 //   #1 from
37 //   cast<int8>(clamp<-128, 127>(conv(int8_x, int8_w)))
38 //   to
39 //   cudnnConvolutionForward<int8>(int8_x, int8_w)
40 // or #2 from
41 //   cast<float>(conv(int8_x, int8_w))
42 //   to
43 //   cudnnConvolutionForward<float>(int8_x, int8_w)
44 // or #3 from
45 //   cast<int8>(clamp<-128, 127>(max(0, alpha1 *
46 //                           cast<float>(conv(int8_x, int8_w)) +
47 //                           alpha2 * cast<float>(int8_side) +
48 //                           broadcast(bias)))
49 //   to
50 //   cudnnConvolutionBiasActivationForward<int8>(int8_x, int8_w, bias, alpha1,
51 //   alpha2, int8_side)
52 // or #4 from
53 //   max(0, alpha1 * cast<float>(conv(int8_x, int8_w)) +
54 //          alpha2 * float_side + broadcast(bias))
55 //   to
56 //   cudnnConvolutionBiasActivationForward<float>(int8_x, int8_w, bias, alpha1,
57 //   alpha2, float_side)
58 
59 class CudnnFusedConvRewriter : public HloModulePass {
60  public:
name()61   absl::string_view name() const override {
62     return "cudnn-fused-convolution-rewriter";
63   }
64 
65   StatusOr<bool> Run(HloModule* module) override;
66 };
67 
68 }  // namespace gpu
69 }  // namespace xla
70 
71 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CUDNN_FUSED_CONV_REWRITER_H_
72