Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/main/apex/csrc/multi_tensor_scale_kernel.cu
Views: 792
#include <ATen/ATen.h>1#include <ATen/AccumulateType.h>2#include <ATen/cuda/CUDAContext.h>3#include <ATen/cuda/Exceptions.h>4// Another possibility:5// #include <torch/all.h>67#include <assert.h>8// Stringstream is a big hammer, but I want to rely on operator<< for dtype.9#include <sstream>1011#include "type_shim.h"12#include "multi_tensor_apply.cuh"1314#define BLOCK_SIZE 51215#define ILP 41617template<typename T>18__device__ __forceinline__ bool is_aligned(T* p){19return ((uint64_t)p) % (ILP*sizeof(T)) == 0;20}2122template<typename T>23__device__ __forceinline__ void load_store(T* dst, T* src, int dst_offset, int src_offset){24typedef typename std::aligned_storage<ILP*sizeof(T), ILP*alignof(T)>::type LT;25((LT*)dst)[dst_offset] = ((LT*)src)[src_offset];26}2728template<typename in_t, typename out_t>29struct ScaleFunctor30{31__device__ __forceinline__ void operator()(32int chunk_size,33volatile int* noop_gmem,34TensorListMetadata<2>& tl,35float scale)36{37// I'd like this kernel to propagate infs/nans.38// if(*noop_gmem == 1)39// return;4041int tensor_loc = tl.block_to_tensor[blockIdx.x];42int chunk_idx = tl.block_to_chunk[blockIdx.x];43int n = tl.sizes[tensor_loc];4445in_t* in = (in_t*)tl.addresses[0][tensor_loc];46in += chunk_idx*chunk_size;4748out_t* out = (out_t*)tl.addresses[1][tensor_loc];49out += chunk_idx*chunk_size;5051n -= chunk_idx*chunk_size;5253bool finite = true;54in_t r_in[ILP];55out_t r_out[ILP];5657// to make things simple, we put aligned case in a different code path58if(n % ILP == 0 && chunk_size % ILP == 0 && is_aligned(in) && is_aligned(out))59{60for(int i_start = threadIdx.x; i_start*ILP < n && i_start*ILP < chunk_size; i_start += blockDim.x)61{62// load63load_store(r_in, in, 0 , i_start);64#pragma unroll65for(int ii = 0; ii < ILP; ii++)66{67r_out[ii] = static_cast<float>(r_in[ii]) * scale;68finite = finite && isfinite(r_in[ii]);69}70// store71load_store(out, r_out, i_start, 0);72}73}74else75{76// Non-divergent exit condition for __syncthreads, not necessary here77for(int i_start = 0; i_start < n && i_start < chunk_size; i_start += blockDim.x*ILP)78{79#pragma unroll80for(int ii = 0; ii < ILP; ii++)81{82r_in[ii] = 0;83int i = i_start + threadIdx.x + ii*blockDim.x;84if(i < n && i < chunk_size)85r_in[ii] = in[i];86}87// note for clarification to future michael:88// From a pure memory dependency perspective, there's likely no point unrolling89// the write loop, since writes just fire off once their LDGs arrive.90// Put another way, the STGs are dependent on the LDGs, but not on each other.91// There is still compute ILP benefit from unrolling the loop though.92#pragma unroll93for(int ii = 0; ii < ILP; ii++)94{95r_out[ii] = static_cast<float>(r_in[ii]) * scale;96finite = finite && isfinite(r_in[ii]);97}98#pragma unroll99for(int ii = 0; ii < ILP; ii++)100{101int i = i_start + threadIdx.x + ii*blockDim.x;102if(i < n && i < chunk_size)103out[i] = r_out[ii];104}105}106}107if(!finite)108*noop_gmem = 1; // Blindly fire off a write. These will race but that's ok.109}110};111112void multi_tensor_scale_cuda(113int chunk_size,114at::Tensor noop_flag,115std::vector<std::vector<at::Tensor>> tensor_lists,116float scale)117{118using namespace at;119// The output (downscaled) type is always float.120// If build times suffer, think about where to put this dispatch,121// and what logic should be moved out of multi_tensor_apply.122123DISPATCH_FLOAT_AND_HALF(tensor_lists[0][0].scalar_type(), 0, "multi_tensor_scale_cuda",124DISPATCH_FLOAT_AND_HALF(tensor_lists[1][0].scalar_type(), 1, "multi_tensor_scale_cuda",125multi_tensor_apply<2>(126BLOCK_SIZE,127chunk_size,128noop_flag,129tensor_lists,130ScaleFunctor<scalar_t_0, scalar_t_1>(),131scale); ))132AT_CUDA_CHECK(cudaGetLastError());133134// AT_CUDA_CHECK(cudaDeviceSynchronize());135}136137138