<commit_before>/* Copyright 2015 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.
==============================================================================*/

#ifndef TENSORFLOW_CORE_KERNELS_CONV_2D_H_
#define TENSORFLOW_CORE_KERNELS_CONV_2D_H_

#include "absl/strings/string_view.h"
#include "tensorflow/core/framework/tensor_types.h"
#include "tensorflow/core/kernels/eigen_backward_spatial_convolutions.h"
#include "tensorflow/core/kernels/eigen_spatial_convolutions.h"
#include "tensorflow/core/util/tensor_format.h"
#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"

// Returns true if TF_CONV2D_USE_FP16_ACCUMULATE == 1, false otherwise.
static bool Conv2dUseFp16Accumulate() {
  static bool use_fp16_accumulate = []() {
    const char* env = std::getenv("TF_CONV2D_USE_FP16_ACCUMULATE");
    return (env != nullptr) && (absl::string_view(env) == "1");
  }();
  return use_fp16_accumulate;
}

namespace tensorflow {
namespace functor {

template <typename Device, typename Input, typename Filter, typename Output,
          typename OutputKernel>
void SpatialConvolutionFunc(const Device& d, Output output, Input input,
                            Filter filter, int row_stride, int col_stride,
                            int row_dilation, int col_dilation,
                            const Eigen::PaddingType& padding,
                            const OutputKernel& output_kernel,
                            int padding_top = 0, int padding_bottom = 0,
                            int padding_left = 0, int padding_right = 0) {
  // Need to swap row/col, padding_top/padding_left, and
  // padding_bottom/padding_right when calling Eigen. Eigen expects the tensor
  // in NWHC format, but the tensor given is in NHWC.
  output.device(d) = Eigen::SpatialConvolution(
      input, filter, col_stride, row_stride, padding, col_dilation,
      row_dilation, output_kernel, padding_left, padding_right, padding_top,
      padding_bottom);
}

// TODO(ezhulenev): Non-templated `operator()` are required by explicit template
// instantiations for the GPU device. However they are almost certainly not used
// in any of the kernel implementation. Check if they can be removed.
template <typename Device, typename T,
          typename OutputKernel = const Eigen::NoOpOutputKernel>
struct SpatialConvolution {
  void operator()(const Device& d, typename TTypes<T, 4>::Tensor output,
                  typename TTypes<T, 4>::ConstTensor input,
                  typename TTypes<T, 4>::ConstTensor filter, int row_stride,
                  int col_stride, int row_dilation, int col_dilation,
                  const Eigen::PaddingType& padding,
                  const OutputKernel& output_kernel = OutputKernel()) {
    SpatialConvolutionFunc(d, output, input, filter, row_stride, col_stride,
                           row_dilation, col_dilation, padding, output_kernel);
  }

  template <typename Input, typename Filter, typename Output>
  void operator()(const Device& d, Output output, Input input, Filter filter,
                  int row_stride, int col_stride, int row_dilation,
                  int col_dilation, const Eigen::PaddingType& padding,
                  const OutputKernel& output_kernel = OutputKernel()) {
    SpatialConvolutionFunc(d, output, input, filter, row_stride, col_stride,
                           row_dilation, col_dilation, padding, output_kernel);
  }

  void operator()(const Device& d, typename TTypes<T, 4>::Tensor output,
                  typename TTypes<T, 4>::ConstTensor input,
                  typename TTypes<T, 4>::ConstTensor filter, int row_stride,
                  int col_stride, int row_dilation, int col_dilation,
                  int padding_top, int padding_bottom, int padding_left,
                  int padding_right,
                  const OutputKernel& output_kernel = OutputKernel()) {
    SpatialConvolutionFunc(
        d, output, input, filter, row_stride, col_stride, row_dilation,
        col_dilation, Eigen::PaddingType::PADDING_VALID, output_kernel,
        padding_top, padding_bottom, padding_left, padding_right);
  }

  template <typename Input, typename Filter, typename Output>
  void operator()(const Device& d, Output output, Input input, Filter filter,
                  int row_stride, int col_stride, int row_dilation,
                  int col_dilation, int padding_top, int padding_bottom,
                  int padding_left, int padding_right,
                  const OutputKernel& output_kernel = OutputKernel()) {
    SpatialConvolutionFunc(
        d, output, input, filter, row_stride, col_stride, row_dilation,
        col_dilation, Eigen::PaddingType::PADDING_VALID, output_kernel,
        padding_top, padding_bottom, padding_left, padding_right);
  }
};

template <typename Device, typename OutputKernel>
struct SpatialConvolution<Device, Eigen::half, OutputKernel> {
  void operator()(const Device& d,
                  typename TTypes<Eigen::half, 4>::Tensor output,
                  typename TTypes<Eigen::half, 4>::ConstTensor input,
                  typename TTypes<Eigen::half, 4>::ConstTensor filter,
                  int row_stride, int col_stride, int row_dilation,
                  int col_dilation, const Eigen::PaddingType& padding,
                  const OutputKernel& output_kernel = OutputKernel()) {
    if (Conv2dUseFp16Accumulate()) {
      output.device(d) = Eigen::SpatialConvolution(
          input, filter, col_stride, row_stride, padding, col_dilation,
          row_dilation, output_kernel);
    } else {
      output.device(d) =
          Eigen::SpatialConvolution(input.cast<float>(), filter.cast<float>(),
                                    col_stride, row_stride, padding,
                                    col_dilation, row_dilation, output_kernel)
              .template cast<Eigen::half>();
    }
  }

  template <typename Input, typename Filter, typename Output>
  void operator()(const Device& d, Output output, Input input, Filter filter,
                  int row_stride, int col_stride, int row_dilation,
                  int col_dilation, const Eigen::PaddingType& padding,
                  const OutputKernel& output_kernel = OutputKernel()) {
    if (Conv2dUseFp16Accumulate()) {
      output.device(d) = Eigen::SpatialConvolution(
          input, filter, col_stride, row_stride, padding, col_dilation,
          row_dilation, output_kernel);
    } else {
      output.device(d) =
          Eigen::SpatialConvolution(input.template cast<float>(),
                                    filter.template cast<float>(), col_stride,
                                    row_stride, padding, col_dilation,
                                    row_dilation, output_kernel)
              .template cast<Eigen::half>();
    }
  }

  void operator()(const Device& d,
                  typename TTypes<Eigen::half, 4>::Tensor output,
                  typename TTypes<Eigen::half, 4>::ConstTensor input,
                  typename TTypes<Eigen::half, 4>::ConstTensor filter,
                  int row_stride, int col_stride, int row_dilation,
                  int col_dilation, int padding_top, int padding_bottom,
                  int padding_left, int padding_right,
                  const OutputKernel& output_kernel = OutputKernel()) {
    if (Conv2dUseFp16Accumulate()) {
      output.device(d) = Eigen::SpatialConvolution(
          input, filter, col_stride, row_stride,
          Eigen::PaddingType::PADDING_VALID, col_dilation, row_dilation,
          output_kernel, padding_left, padding_right, padding_top,
          padding_bottom);
    } else {
      Eigen::SpatialConvolution(input.cast<float>(), filter.cast<float>(),
                                col_stride, row_stride,
                                Eigen::PaddingType::PADDING_VALID, col_dilation,
                                row_dilation, output_kernel, padding_left,
                                padding_right, padding_top, padding_bottom)
          .template cast<Eigen::half>();
    }
  }

  template <typename Input, typename Filter, typename Output>
  void operator()(const Device& d, Output output, Input input, Filter filter,
                  int row_stride, int col_stride, int row_dilation,
                  int col_dilation, int padding_top, int padding_bottom,
                  int padding_left, int padding_right,
                  const OutputKernel& output_kernel = OutputKernel()) {
    if (Conv2dUseFp16Accumulate()) {
      output.device(d) = Eigen::SpatialConvolution(<commit_msg>Update conv_2d.h<commit_after>/* Copyright 2015 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.
==============================================================================*/

#ifndef TENSORFLOW_CORE_KERNELS_CONV_2D_H_
#define TENSORFLOW_CORE_KERNELS_CONV_2D_H_

#include "absl/strings/string_view.h"
#include "tensorflow/core/framework/tensor_types.h"
#include "tensorflow/core/kernels/eigen_backward_spatial_convolutions.h"
#include "tensorflow/core/kernels/eigen_spatial_convolutions.h"
#include "tensorflow/core/util/tensor_format.h"
#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"

// Returns true if TF_CONV2D_USE_FP16_ACCUMULATE == 1, false otherwise.
static bool Conv2dUseFp16Accumulate() {
  static bool use_fp16_accumulate = []() {
    const char* env = std::getenv("TF_CONV2D_USE_FP16_ACCUMULATE");
    return (env != nullptr) && (absl::string_view(env) == "1");
  }();
  return use_fp16_accumulate;
}

namespace tensorflow {
namespace functor {

template <typename Device, typename Input, typename Filter, typename Output,
          typename OutputKernel>
void SpatialConvolutionFunc(const Device& d, Output output, Input input,
                            Filter filter, int row_stride, int col_stride,
                            int row_dilation, int col_dilation,
                            const Eigen::PaddingType& padding,
                            const OutputKernel& output_kernel,
                            int padding_top = 0, int padding_bottom = 0,
                            int padding_left = 0, int padding_right = 0) {
  // Need to swap row/col, padding_top/padding_left, and
  // padding_bottom/padding_right when calling Eigen. Eigen expects the tensor
  // in NWHC format, but the tensor given is in NHWC.
  output.device(d) = Eigen::SpatialConvolution(
      input, filter, col_stride, row_stride, padding, col_dilation,
      row_dilation, output_kernel, padding_left, padding_right, padding_top,
      padding_bottom);
}

// TODO(ezhulenev): Non-templated `operator()` are required by explicit template
// instantiations for the GPU device. However they are almost certainly not used
// in any of the kernel implementation. Check if they can be removed.
template <typename Device, typename T,
          typename OutputKernel = const Eigen::NoOpOutputKernel>
struct SpatialConvolution {
  void operator()(const Device& d, typename TTypes<T, 4>::Tensor output,
                  typename TTypes<T, 4>::ConstTensor input,
                  typename TTypes<T, 4>::ConstTensor filter, int row_stride,
                  int col_stride, int row_dilation, int col_dilation,
                  const Eigen::PaddingType& padding,
                  const OutputKernel& output_kernel = OutputKernel()) {
    SpatialConvolutionFunc(d, output, input, filter, row_stride, col_stride,
                           row_dilation, col_dilation, padding, output_kernel);
  }

  template <typename Input, typename Filter, typename Output>
  void operator()(const Device& d, Output output, Input input, Filter filter,
                  int row_stride, int col_stride, int row_dilation,
                  int col_dilation, const Eigen::PaddingType& padding,
                  const OutputKernel& output_kernel = OutputKernel()) {
    SpatialConvolutionFunc(d, output, input, filter, row_stride, col_stride,
                           row_dilation, col_dilation, padding, output_kernel);
  }

  void operator()(const Device& d, typename TTypes<T, 4>::Tensor output,
                  typename TTypes<T, 4>::ConstTensor input,
                  typename TTypes<T, 4>::ConstTensor filter, int row_stride,
                  int col_stride, int row_dilation, int col_dilation,
                  int padding_top, int padding_bottom, int padding_left,
                  int padding_right,
                  const OutputKernel& output_kernel = OutputKernel()) {
    SpatialConvolutionFunc(
        d, output, input, filter, row_stride, col_stride, row_dilation,
        col_dilation, Eigen::PaddingType::PADDING_VALID, output_kernel,
        padding_top, padding_bottom, padding_left, padding_right);
  }

  template <typename Input, typename Filter, typename Output>
  void operator()(const Device& d, Output output, Input input, Filter filter,
                  int row_stride, int col_stride, int row_dilation,
                  int col_dilation, int padding_top, int padding_bottom,
                  int padding_left, int padding_right,
                  const OutputKernel& output_kernel = OutputKernel()) {
    SpatialConvolutionFunc(
        d, output, input, filter, row_stride, col_stride, row_dilation,
        col_dilation, Eigen::PaddingType::PADDING_VALID, output_kernel,
        padding_top, padding_bottom, padding_left, padding_right);
  }
};

template <typename Device, typename OutputKernel>
struct SpatialConvolution<Device, Eigen::half, OutputKernel> {
  void operator()(const Device& d,
                  typename TTypes<Eigen::half, 4>::Tensor output,
                  typename TTypes<Eigen::half, 4>::ConstTensor input,
                  typename TTypes<Eigen::half, 4>::ConstTensor filter,
                  int row_stride, int col_stride, int row_dilation,
                  int col_dilation, const Eigen::PaddingType& padding,
                  const OutputKernel& output_kernel = OutputKernel()) {
    if (Conv2dUseFp16Accumulate()) {
      output.device(d) = Eigen::SpatialConvolution(
          input, filter, col_stride, row_stride, padding, col_dilation,
          row_dilation, output_kernel);
    } else {
      output.device(d) =
          Eigen::SpatialConvolution(input.cast<float>(), filter.cast<float>(),
                                    col_stride, row_stride, padding,
                                    col_dilation, row_dilation, output_kernel)
              .template cast<Eigen::half>();
    }
  }

  template <typename Input, typename Filter, typename Output>
  void operator()(const Device& d, Output output, Input input, Filter filter,
                  int row_stride, int col_stride, int row_dilation,
                  int col_dilation, const Eigen::PaddingType& padding,
                  const OutputKernel& output_kernel = OutputKernel()) {
    if (Conv2dUseFp16Accumulate()) {
      output.device(d) = Eigen::SpatialConvolution(
          input, filter, col_stride, row_stride, padding, col_dilation,
          row_dilation, output_kernel);
    } else {
      output.device(d) =
          Eigen::SpatialConvolution(input.template cast<float>(),
                                    filter.template cast<float>(), col_stride,
                                    row_stride, padding, col_dilation,
                                    row_dilation, output_kernel)
              .template cast<Eigen::half>();
    }
  }

  void operator()(const Device& d,
                  typename TTypes<Eigen::half, 4>::Tensor output,
                  typename TTypes<Eigen::half, 4>::ConstTensor input,
                  typename TTypes<Eigen::half, 4>::ConstTensor filter,
                  int row_stride, int col_stride, int row_dilation,
                  int col_dilation, int padding_top, int padding_bottom,
                  int padding_left, int padding_right,
                  const OutputKernel& output_kernel = OutputKernel()) {
    if (Conv2dUseFp16Accumulate()) {
      output.device(d) = Eigen::SpatialConvolution(
          input, filter, col_stride, row_stride,
          Eigen::PaddingType::PADDING_VALID, col_dilation, row_dilation,
          output_kernel, padding_left, padding_right, padding_top,
          padding_bottom);
    } else {
      output.device(d) =
      Eigen::SpatialConvolution(input.cast<float>(), filter.cast<float>(),
                                col_stride, row_stride,
                                Eigen::PaddingType::PADDING_VALID, col_dilation,
                                row_dilation, output_kernel, padding_left,
                                padding_right, padding_top, padding_bottom)
          .template cast<Eigen::half>();
    }
  }

  template <typename Input, typename Filter, typename Output>
  void operator()(const Device& d, Output output, Input input, Filter filter,
                  int row_stride, int col_stride, int row_dilation,
                  int col_dilation, int padding_top, int padding_bottom,
                  int padding_left, int padding_right,
                  const OutputKernel& output_kernel = OutputKernel()) {
    if (Conv2dUseFp16Accumulate()) {
      output.device(d) = Eigen::SpatialConvolution(
{'fraction_non_alphanumeric': 0.09255788313120177, 'fraction_numerical': 0.0062293274531422275, 'mean_word_length': 2.7022448979591838, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3726095', 'n_tokens_mistral': 5116, 'n_tokens_neox': 4820, 'n_words': 1351}