<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.
==============================================================================*/
#include <stdio.h>
#include <math.h>
#include <algorithm>

#define EIGEN_USE_GPU

#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
#include "./layer_norm_fused_op.h"

#if !defined(_MSC_VER)
#define UNROLL _Pragma("unroll")
#else
#define UNROLL 
#endif

#if !defined(__CUDA_ARCH__) || __CUDA_ARCH__ >= 600
#else
__device__ double atomicAdd(double* a, double b) { return b; }
#endif


#define MAX_GRID_SIZE 480
inline int get_num_blocks(const int n_slices,const int slice_per_block)
{
  const int _num_blocks = n_slices/slice_per_block;
  if(_num_blocks*slice_per_block==n_slices)
    return _num_blocks;
  else
    return _num_blocks+1;
}

inline int get_block_size(const int slice_size,int& block_size,
  int& mult)
{
  const int _warp_size=32;
  int _block_size = _warp_size;
  int _mult = slice_size/_block_size;
  mult = _mult*_block_size>=slice_size?_mult:_mult+1;
  while (mult>5)
  {
    _block_size+=_warp_size;
    _mult = slice_size/_block_size;
    mult = _mult*_block_size>=slice_size?_mult:_mult+1;
  }
  block_size=_block_size;
  return 0;
}

template <typename T>
__global__ void fillZeros(T* __restrict__ output,const int n_inputs)
{
  for(int thread_id=threadIdx.x+blockDim.x*blockIdx.x;thread_id<n_inputs;thread_id+=blockDim.x*gridDim.x)
    output[thread_id] = static_cast<T>(0.0f);
}

template <typename T>
__device__ __inline__ void warpSum(T& val1, T& val2)
{
  val1 += __shfl_xor(val1, 16);
  val2 += __shfl_xor(val2, 16);
  val1 += __shfl_xor(val1, 8);
  val2 += __shfl_xor(val2, 8);
  val1 += __shfl_xor(val1, 4);
  val2 += __shfl_xor(val2, 4);
  val1 += __shfl_xor(val1, 2);
  val2 += __shfl_xor(val2, 2);
  val1 += __shfl_xor(val1, 1);
  val2 += __shfl_xor(val2, 1);
}


template <typename T>
__device__ __inline__ T get_value(const T* index, const int bound_check,const int up_bound)
{
    if (bound_check < up_bound) return __ldg(index);
    else return static_cast<T>(0.0f);
}

namespace tensorflow {

  typedef Eigen::GpuDevice GPUDevice;
  namespace 
  {


    template<typename T, int mult>
    __global__ void LayerNormGPUKernel(const LayerNormFusedArgs args,
      const T* __restrict__ input,
        T* __restrict__ output,
        const int num_blocks
        )
    {
        const int in_depth = args.depth;
        const int slice_size = args.slice_size;
        const int n_inputs = args.n_inputs;
        const T epsilon = args.epsilon;

        const int tWarpIdx = threadIdx.x%warpSize;

        const int tSliceIdx = threadIdx.x%slice_size;

        extern __shared__ __align__(sizeof(T)) unsigned char my_smem[];
        T* mean_cache = (T*)my_smem;
        T* std_cache = (T*)&my_smem[sizeof(T)];

        const T i_n = static_cast<T>(1.0f)/static_cast<T>(in_depth);
        T inp[mult];
        int thread_id[mult];

        T sum;<commit_msg>possible fix for undefined symbol problem<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.
==============================================================================*/
#include <stdio.h>
#include <math.h>
#include <algorithm>

#define EIGEN_USE_GPU

#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
#include "./layer_norm_fused_op.h"

#if !defined(_MSC_VER)
#define UNROLL _Pragma("unroll")
#else
#define UNROLL 
#endif

#if !defined(__CUDA_ARCH__) || __CUDA_ARCH__ >= 600
#else
__device__ double atomicAdd(double* a, double b) { return b; }
#endif


#define MAX_GRID_SIZE 480
inline int get_num_blocks(const int n_slices,const int slice_per_block)
{
  const int _num_blocks = n_slices/slice_per_block;
  if(_num_blocks*slice_per_block==n_slices)
    return _num_blocks;
  else
    return _num_blocks+1;
}

inline int get_block_size(const int slice_size,int& block_size,
  int& mult)
{
  const int _warp_size=32;
  int _block_size = _warp_size;
  int _mult = slice_size/_block_size;
  mult = _mult*_block_size>=slice_size?_mult:_mult+1;
  while (mult>5)
  {
    _block_size+=_warp_size;
    _mult = slice_size/_block_size;
    mult = _mult*_block_size>=slice_size?_mult:_mult+1;
  }
  block_size=_block_size;
  return 0;
}

template <typename T>
__global__ void fillZeros(T* __restrict__ output,const int n_inputs)
{
  for(int thread_id=threadIdx.x+blockDim.x*blockIdx.x;thread_id<n_inputs;thread_id+=blockDim.x*gridDim.x)
    output[thread_id] = static_cast<T>(0.0f);
}

template <typename T>
__device__ __inline__ void warpSum(T& val1, T& val2)
{
  val1 += __shfl_xor(val1, 16);
  val2 += __shfl_xor(val2, 16);
  val1 += __shfl_xor(val1, 8);
  val2 += __shfl_xor(val2, 8);
  val1 += __shfl_xor(val1, 4);
  val2 += __shfl_xor(val2, 4);
  val1 += __shfl_xor(val1, 2);
  val2 += __shfl_xor(val2, 2);
  val1 += __shfl_xor(val1, 1);
  val2 += __shfl_xor(val2, 1);
}


template <typename T>
__device__ __inline__ T get_value(const T* index, const int bound_check,const int up_bound)
{
    if (bound_check < up_bound) return __ldg(index);
    else return static_cast<T>(0.0f);
}

namespace tensorflow {

  typedef Eigen::GpuDevice GPUDevice;
  namespace 
  {
    template<typename T, int mult>
    __global__ void LayerNormGPUKernel(const LayerNormFusedArgs args,
      const T* __restrict__ input,
        T* __restrict__ output,
        const int num_blocks
        )
    {
        const int in_depth = args.depth;
        const int slice_size = args.slice_size;
        const int n_inputs = args.n_inputs;
        const T epsilon = args.epsilon;

        const int tWarpIdx = threadIdx.x%warpSize;

        const int tSliceIdx = threadIdx.x%slice_size;

        extern __shared__ __align__(sizeof(T)) unsigned char my_smem[];
        T* mean_cache = (T*)my_smem;
        T* std_cache = (T*)&my_smem[sizeof(T)];

        const T i_n = static_cast<T>(1.0f)/static_cast<T>(in_depth);
        T inp[mult];
        int thread_id[mult];

        T sum;
{'fraction_non_alphanumeric': 0.12289225492997999, 'fraction_numerical': 0.018290940268648186, 'mean_word_length': 4.207589285714286, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4599795', 'n_tokens_mistral': 2572, 'n_tokens_neox': 2448, 'n_words': 640}