/* Copyright 2019 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/lite/delegates/gpu/metal/kernels/pooling.h"

#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "absl/strings/substitute.h"
#include "tensorflow/lite/delegates/gpu/common/model.h"
#include "tensorflow/lite/delegates/gpu/common/operations.h"
#include "tensorflow/lite/delegates/gpu/common/shape.h"
#include "tensorflow/lite/delegates/gpu/common/util.h"
#include "tensorflow/lite/delegates/gpu/metal/compute_task_descriptor.h"
#include "tensorflow/lite/delegates/gpu/metal/kernels/util.h"

namespace tflite {
namespace gpu {
namespace metal {
namespace {

std::string GetMaxPoolingCode(const HW& kernel_size) {
  std::string shader_source = R"(
    #include <metal_stdlib>
    using namespace metal;
    constant int window_w = $0;
    constant int window_h = $1;
    struct uniforms {
      int4 src_size;
      int4 dst_size;
      int2 stride;
      int2 offset;
    };

    $$0
    kernel void ComputeFunction(
                                $$1
                                uint3 gid[[thread_position_in_grid]]) {
      if (static_cast<int>(gid.x) >= params.dst_size.x ||
          static_cast<int>(gid.y) >= params.dst_size.y ||
          static_cast<int>(gid.z) >= params.dst_size.z) {
        return;
      }

      FLT4 maximum = FLT4(-10000.0);
      for (int a = 0; a < window_h; ++a) {
        for (int b = 0; b < window_w; ++b) {
          const int2 coords = int2(gid.xy) * params.stride - params.offset + int2(b, a);
          bool outside = coords.x < 0 || coords.y < 0 || coords.x >= params.src_size.x ||
            coords.y >= params.src_size.y;
          const int buffer_index = (gid.z * params.src_size.y + coords.y) *
            params.src_size.x + coords.x;
          FLT4 src_color = outside ? FLT4(-10000.0) : src_tensor[buffer_index];
          maximum = max(maximum, src_color);
        }
      }
      const int linear_index = (gid.z * params.dst_size.y + int(gid.y)) * params.dst_size.x +
        int(gid.x);
      FLT4 value = maximum;
      $$2
      dst_tensor[linear_index] = value;
    }
  )";
  return absl::Substitute(shader_source, kernel_size.w, kernel_size.h);
}

std::string GetMaxPoolingIndicesCode(const HW& kernel_size) {
  std::string shader_source = R"(
    #include <metal_stdlib>
    using namespace metal;
    constant int window_w = $0;
    constant int window_h = $1;
    struct uniforms {
      int4 src_size;
      int4 dst_size;
      int2 stride;
      int2 offset;
    };

    $$0
    kernel void ComputeFunction(
                                $$1
                                uint3 gid[[thread_position_in_grid]]) {
      if (static_cast<int>(gid.x) >= params.dst_size.x ||
          static_cast<int>(gid.y) >= params.dst_size.y ||
          static_cast<int>(gid.z) >= params.dst_size.z) {
        return;
      }

      FLT4 maximum = FLT4(-10000.0);
      ushort4 indexes = ushort4(0);
      ushort index_counter = 0;
      for (int a = 0; a < window_h; ++a) {
        for (int b = 0; b < window_w; ++b) {
          const int2 coords = int2(gid.xy) * params.stride - params.offset + int2(b, a);
          bool outside = coords.x < 0 || coords.y < 0 || coords.x >= params.src_size.x ||
            coords.y >= params.src_size.y;
          const int buffer_index = (gid.z * params.src_size.y + coords.y) *
            params.src_size.x + coords.x;
          FLT4 src_color = outside ? FLT4(-10000.0) : src_tensor[buffer_index];
          if (src_color.x > maximum.x) {
            indexes.x = index_counter;
            maximum.x = src_color.x;
          }
          if (src_color.y > maximum.y) {
            indexes.y = index_counter;
            maximum.y = src_color.y;
          }
          if (src_color.z > maximum.z) {
            indexes.z = index_counter;
            maximum.z = src_color.z;
          }
          if (src_color.w > maximum.w) {
            indexes.w = index_counter;
            maximum.w = src_color.w;
          }
          index_counter++;
        }
      }
      const int linear_index = (gid.z * params.dst_size.y + int(gid.y)) * params.dst_size.x +
        int(gid.x);
      FLT4 value = static_cast<FLT4>(indexes);
      $$2
      dst_tensor[linear_index] = value;
    }
  )";
  return absl::Substitute(shader_source, kernel_size.w, kernel_size.h);
}

std::string GetAveragePoolingCode(const HW& kernel_size) {
  std::string shader_source = R"(
  #include <metal_stdlib>
  using namespace metal;
  constant int window_w = $0;
  constant int window_h = $1;
  struct uniforms {
    int4 src_size;
    int4 dst_size;
    int2 stride;
    int2 offset;
  };
  $$0
  kernel void ComputeFunction(
                              $$1
                              uint tid[[thread_index_in_threadgroup]],
                              uint3 gid[[thread_position_in_grid]]) {
    if (static_cast<int>(gid.x) >= params.dst_size.x ||
        static_cast<int>(gid.y) >= params.dst_size.y ||
        static_cast<int>(gid.z) >= params.dst_size.z) {
      return;
    }

    float4 sum = float4(0.0f);
    float window_size = 0.0f;
    for (int a = 0; a < window_h; ++a) {
      for (int b = 0; b < window_w; ++b) {
        const int2 coords = int2(gid.xy) * params.stride - params.offset + int2(b, a);
        bool outside = coords.x < 0 || coords.y < 0 || coords.x >= params.src_size.x ||
          coords.y >= params.src_size.y;
        const int buffer_index = (gid.z * params.src_size.y + coords.y) *
          params.src_size.x + coords.x;
        const float4 src_color = outside ? float4(0.0f) : float4(src_tensor[buffer_index]);
        window_size += outside ? 0.0f : 1.0f;
        sum += src_color;
      }
    }
    const int linear_index = (gid.z * params.dst_size.y + int(gid.y)) * params.dst_size.x +
      int(gid.x);
    // If window_size==0, window covered nothing. This situation is a sign of
    // incorrectly constructed operation. NaNs are expected as output.
    FLT4 value = FLT4(sum / window_size);
    $$2
    dst_tensor[linear_index] = value;
  }
)";
  return absl::Substitute(shader_source, kernel_size.w, kernel_size.h);
}

}  // namespace

ComputeTaskDescriptor Pooling(const OperationDef& definition,
                              const Pooling2DAttributes& params,
                              bool generate_indices) {
  ComputeTaskDescriptor desc(definition);
  if (params.type == PoolingType::MAX) {
    desc.shader_source = generate_indices
                             ? GetMaxPoolingIndicesCode(params.kernel)
                             : GetMaxPoolingCode(params.kernel);
  } else if (params.type == PoolingType::AVERAGE) {
    desc.shader_source = GetAveragePoolingCode(params.kernel);
  }

  desc.AddSrcTensor("src_tensor", definition.src_tensors[0]);
  desc.AddDstTensor("dst_tensor", definition.dst_tensors[0]);

  desc.uniform_buffers = {
      {"constant uniforms& params",
       [params](const std::vector<BHWC>& src_shapes,
                const std::vector<BHWC>& dst_shapes) {
         std::vector<int> uniform_params = {
             src_shapes[0].w,
             src_shapes[0].h,
             DivideRoundUp(src_shapes[0].c, 4),
             src_shapes[0].w * src_shapes[0].h,
             dst_shapes[0].w,
             dst_shapes[0].h,
             DivideRoundUp(dst_shapes[0].c, 4),
             dst_shapes[0].w * dst_shapes[0].h,
             params.strides.w,
             params.strides.h,
             params.padding.prepended.w,
             params.padding.prepended.h,
         };
         return GetByteBuffer(uniform_params);
       }},
  };

  desc.resize_function = [](const std::vector<BHWC>& src_shapes,
                            const std::vector<BHWC>& dst_shapes) {
    const uint3 grid = uint3(dst_shapes[0].w, dst_shapes[0].h,
                             DivideRoundUp(dst_shapes[0].c, 4));
    const uint3 groups_size = GetWorkGroupSizeForGrid(grid);
    int groups_x = DivideRoundUp(grid.x, groups_size.x);
    int groups_y = DivideRoundUp(grid.y, groups_size.y);
    int groups_z = DivideRoundUp(grid.z, groups_size.z);
    return std::make_pair(groups_size, uint3{groups_x, groups_y, groups_z});
  };

  return desc;
}

}  // namespace metal
}  // namespace gpu
}  // namespace tflite
