#include #include #include #include // Another possibility: // #include #include #include "type_shim.h" #include "multi_tensor_apply.cuh" #define BLOCK_SIZE 512 #define ILP 4 template struct L2NormFunctor { __device__ __forceinline__ void operator()( int chunk_size, volatile int* noop_gmem, TensorListMetadata<1>& tl, float* output) { // I'd like this kernel to propagate infs/nans. // if(*noop_gmem == 1) // return; int tensor_loc = tl.block_to_tensor[blockIdx.x]; int chunk_idx = tl.block_to_chunk[blockIdx.x]; int n = tl.sizes[tensor_loc]; x_t* x = (x_t*)tl.addresses[0][tensor_loc]; x += chunk_idx*chunk_size; n -= chunk_idx*chunk_size; __shared__ float vals[512]; // Non-divergent exit condition for __syncthreads, not necessary here float val = 0; for(int i = threadIdx.x; i < n && i < chunk_size; i += blockDim.x) { float next = static_cast(x[i]); val += next*next; } float final = reduce_block_into_lanes(vals, val); if(threadIdx.x == 0) { if(!isfinite(final)) *noop_gmem = 1; // Blindly fire off a write. These will race but that's ok. output[blockIdx.x] += final; } } }; at::Tensor multi_tensor_l2norm_cuda( int chunk_size, at::Tensor noop_flag, std::vector> tensor_lists) { auto output = at::zeros({320}, tensor_lists[0][0].options().dtype(at::ScalarType::Float)); DISPATCH_FLOAT_AND_HALF(tensor_lists[0][0].scalar_type(), 0, "multi_tensor_l2norm_cuda", multi_tensor_apply<1>( BLOCK_SIZE, chunk_size, noop_flag, tensor_lists, L2NormFunctor(), output.data());) AT_CUDA_CHECK(cudaGetLastError()); // AT_CUDA_CHECK(cudaDeviceSynchronize()); // This involves two more small kernel launches, but will be negligible end to end. // I could get rid of these by hacking the functor + multi tensor harness with persistence // logic, but keeping it simple for now return output.sum().sqrt(); }