Upload apex-master/csrc/multi_tensor_apply.cuh with huggingface_hub
Browse files
apex-master/csrc/multi_tensor_apply.cuh
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <ATen/ATen.h>
|
| 2 |
+
#include <ATen/AccumulateType.h>
|
| 3 |
+
#include <ATen/cuda/CUDAContext.h>
|
| 4 |
+
#include <ATen/cuda/Exceptions.h>
|
| 5 |
+
#include <c10/cuda/CUDAGuard.h>
|
| 6 |
+
#include "compat.h"
|
| 7 |
+
|
| 8 |
+
#include <assert.h>
|
| 9 |
+
|
| 10 |
+
// #include <iostream>
|
| 11 |
+
|
| 12 |
+
// This header is the one-stop shop for all your multi-tensor apply needs.
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
// TODO: Kernel arg size limit may be <4KB for some other cards (ie Jetson)
|
| 16 |
+
constexpr int depth_to_max_tensors[6] = {110, 64, 48, 36, 30, 24};
|
| 17 |
+
constexpr int depth_to_max_blocks[6] = {320, 320, 320, 320, 320, 320};
|
| 18 |
+
|
| 19 |
+
template<int n> struct TensorListMetadata
|
| 20 |
+
{
|
| 21 |
+
void* addresses[n][depth_to_max_tensors[n-1]];
|
| 22 |
+
int64_t sizes[depth_to_max_tensors[n-1]];
|
| 23 |
+
unsigned char block_to_tensor[depth_to_max_blocks[n-1]];
|
| 24 |
+
int block_to_chunk[depth_to_max_blocks[n-1]]; // I fear this needs to be a full int.
|
| 25 |
+
int start_tensor_this_launch;
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
template<typename T, typename U, typename... ArgTypes>
|
| 30 |
+
__global__ void multi_tensor_apply_kernel(
|
| 31 |
+
int64_t chunk_size,
|
| 32 |
+
volatile int* noop_flag,
|
| 33 |
+
T tl,
|
| 34 |
+
U callable,
|
| 35 |
+
ArgTypes... args)
|
| 36 |
+
{
|
| 37 |
+
// Hand the chunk information to the user-supplied functor to process however it likes.
|
| 38 |
+
callable(chunk_size, noop_flag, tl, args...);
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
template<int depth, typename T, typename... ArgTypes>
|
| 42 |
+
void multi_tensor_apply(
|
| 43 |
+
int64_t block_size,
|
| 44 |
+
int64_t chunk_size,
|
| 45 |
+
const at::Tensor& noop_flag,
|
| 46 |
+
const std::vector<std::vector<at::Tensor>>& tensor_lists,
|
| 47 |
+
T callable,
|
| 48 |
+
ArgTypes... args)
|
| 49 |
+
{
|
| 50 |
+
TORCH_CHECK(tensor_lists.size() == depth, "tensor_lists.size() != depth");
|
| 51 |
+
int len0 = tensor_lists[0].size();
|
| 52 |
+
TORCH_CHECK(len0 > 0, "tensor_lists[0].size() is not > 0");
|
| 53 |
+
auto ref_device = tensor_lists[0][0].device();
|
| 54 |
+
TORCH_CHECK(ref_device.type() == at::kCUDA, "expected input to be on cuda");
|
| 55 |
+
for (int l = 0; l < tensor_lists.size(); l++) // No range-based for because I need indices
|
| 56 |
+
{
|
| 57 |
+
TORCH_CHECK(tensor_lists[l].size() == len0, "Size mismatch among tensor lists");
|
| 58 |
+
for(int t = 0; t < tensor_lists[l].size(); t++)
|
| 59 |
+
{
|
| 60 |
+
// TODO: Print which tensor fails.
|
| 61 |
+
bool contiguous_memory = tensor_lists[l][t].is_contiguous();
|
| 62 |
+
#ifdef VERSION_GE_1_5
|
| 63 |
+
contiguous_memory = (contiguous_memory || tensor_lists[l][t].is_contiguous(at::MemoryFormat::ChannelsLast) || tensor_lists[l][t].is_contiguous(at::MemoryFormat::ChannelsLast3d));
|
| 64 |
+
#endif
|
| 65 |
+
TORCH_CHECK(contiguous_memory, "A tensor was not contiguous.");
|
| 66 |
+
TORCH_CHECK(tensor_lists[l][t].device() == ref_device, "A tensor was not on the same device as the first tensor");
|
| 67 |
+
TORCH_CHECK(tensor_lists[l][t].numel() == tensor_lists[0][t].numel(), "Size mismatch");
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
int ntensors = tensor_lists[0].size();
|
| 72 |
+
|
| 73 |
+
TensorListMetadata<depth> tl;
|
| 74 |
+
|
| 75 |
+
const at::cuda::OptionalCUDAGuard device_guard(device_of(tensor_lists[0][0]));
|
| 76 |
+
auto stream = at::cuda::getCurrentCUDAStream();
|
| 77 |
+
|
| 78 |
+
tl.start_tensor_this_launch = 0;
|
| 79 |
+
int loc_block_info = 0;
|
| 80 |
+
int loc_tensor_info = 0;
|
| 81 |
+
for(int t = 0; t < ntensors; t++)
|
| 82 |
+
{
|
| 83 |
+
tl.sizes[loc_tensor_info] = tensor_lists[0][t].numel();
|
| 84 |
+
for(int d = 0; d < depth; d++)
|
| 85 |
+
tl.addresses[d][loc_tensor_info] = tensor_lists[d][t].data_ptr();
|
| 86 |
+
loc_tensor_info++;
|
| 87 |
+
|
| 88 |
+
auto chunks_this_tensor = (tensor_lists[0][t].numel() + chunk_size - 1)/chunk_size;
|
| 89 |
+
|
| 90 |
+
for(auto chunk = 0; chunk < chunks_this_tensor; chunk++)
|
| 91 |
+
{
|
| 92 |
+
// std::cout << chunks_this_tensor << std::endl;
|
| 93 |
+
tl.block_to_tensor[loc_block_info] = loc_tensor_info - 1;
|
| 94 |
+
tl.block_to_chunk[loc_block_info] = chunk;
|
| 95 |
+
loc_block_info++;
|
| 96 |
+
|
| 97 |
+
bool tensors_full = (loc_tensor_info == depth_to_max_tensors[depth-1] &&
|
| 98 |
+
chunk == chunks_this_tensor - 1);
|
| 99 |
+
bool blocks_full = (loc_block_info == depth_to_max_blocks[depth-1]);
|
| 100 |
+
bool last_chunk = (t == ntensors - 1 && chunk == chunks_this_tensor - 1);
|
| 101 |
+
if(tensors_full || blocks_full || last_chunk)
|
| 102 |
+
{
|
| 103 |
+
// using accscalar_t = acc_type<scalar_t, true>;
|
| 104 |
+
multi_tensor_apply_kernel<<<loc_block_info, block_size, 0, stream>>>(
|
| 105 |
+
chunk_size,
|
| 106 |
+
noop_flag.DATA_PTR<int>(),
|
| 107 |
+
tl,
|
| 108 |
+
callable,
|
| 109 |
+
args...);
|
| 110 |
+
|
| 111 |
+
AT_CUDA_CHECK(cudaGetLastError());
|
| 112 |
+
|
| 113 |
+
// Reset. The control flow possibilities here make my brain hurt.
|
| 114 |
+
loc_block_info = 0;
|
| 115 |
+
if(chunk == chunks_this_tensor - 1)
|
| 116 |
+
{
|
| 117 |
+
// std::cout << "Hit case 1 " << cond1 << " " << cond2 << " " << cond3 << std::endl;
|
| 118 |
+
loc_tensor_info = 0;
|
| 119 |
+
tl.start_tensor_this_launch = t + 1;
|
| 120 |
+
}
|
| 121 |
+
else
|
| 122 |
+
{
|
| 123 |
+
// std::cout << "Hit case 2 " << cond1 << " " << cond2 << " " << cond3 << std::endl;
|
| 124 |
+
tl.sizes[0] = tl.sizes[loc_tensor_info-1];
|
| 125 |
+
for(int d = 0; d < depth; d++)
|
| 126 |
+
tl.addresses[d][0] = tl.addresses[d][loc_tensor_info-1];
|
| 127 |
+
loc_tensor_info = 1;
|
| 128 |
+
tl.start_tensor_this_launch = t;
|
| 129 |
+
}
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
}
|
| 133 |
+
}
|