model-as-a-kernel v1 source
Browse files- mak_cuda/mak_kernels.h +4 -1
- mak_cuda/megakernel.cu +12 -3
- torch-ext/torch_binding.cpp +27 -3
mak_cuda/mak_kernels.h
CHANGED
|
@@ -11,10 +11,13 @@ int mak_grid_blocks_impl(int smem_bytes);
|
|
| 11 |
// two in [1, 16], chosen from the device's shared-memory budget).
|
| 12 |
int mak_ring_stages_impl(int stage_bytes);
|
| 13 |
|
|
|
|
|
|
|
| 14 |
cudaError_t mak_launch_mega(const long long* prog, int n_phases, int pos0,
|
| 15 |
int total, int slot0, int prompt_len,
|
| 16 |
int chunk_m, int ring_stages, int* bar, int grid,
|
| 17 |
-
int smem_bytes, cudaStream_t stream
|
|
|
|
| 18 |
|
| 19 |
cudaError_t mak_launch_phase(const long long* prog, int phase, int pos,
|
| 20 |
int step_slot, int ring_stages, int grid,
|
|
|
|
| 11 |
// two in [1, 16], chosen from the device's shared-memory budget).
|
| 12 |
int mak_ring_stages_impl(int stage_bytes);
|
| 13 |
|
| 14 |
+
// ts (nullable): block 0 stamps clock64() at entry (slot 0) and after each
|
| 15 |
+
// phase's barrier crossing of the first step (slot p+1); size n_phases+1.
|
| 16 |
cudaError_t mak_launch_mega(const long long* prog, int n_phases, int pos0,
|
| 17 |
int total, int slot0, int prompt_len,
|
| 18 |
int chunk_m, int ring_stages, int* bar, int grid,
|
| 19 |
+
int smem_bytes, cudaStream_t stream,
|
| 20 |
+
long long* ts);
|
| 21 |
|
| 22 |
cudaError_t mak_launch_phase(const long long* prog, int phase, int pos,
|
| 23 |
int step_slot, int ring_stages, int grid,
|
mak_cuda/megakernel.cu
CHANGED
|
@@ -940,10 +940,15 @@ __device__ void grid_barrier(int* bar, int* local_sense) {
|
|
| 940 |
__global__ void __launch_bounds__(BLOCK_THREADS, 2)
|
| 941 |
mega_kernel(const long long* __restrict__ prog, int n_phases,
|
| 942 |
int pos0, int total, int slot0, int prompt_len,
|
| 943 |
-
int chunk_m, int ring_stages, int* bar
|
|
|
|
| 944 |
__shared__ int s_sense;
|
| 945 |
if (threadIdx.x == 0) s_sense = atomicAdd(bar + BAR_STRIPES + 1, 0);
|
| 946 |
__syncthreads();
|
|
|
|
|
|
|
|
|
|
|
|
|
| 947 |
int pos = pos0;
|
| 948 |
const int pos_end = pos0 + total;
|
| 949 |
while (pos < pos_end) {
|
|
@@ -984,6 +989,9 @@ __global__ void __launch_bounds__(BLOCK_THREADS, 2)
|
|
| 984 |
}
|
| 985 |
grid_barrier(bar, &s_sense);
|
| 986 |
}
|
|
|
|
|
|
|
|
|
|
| 987 |
}
|
| 988 |
pos += M;
|
| 989 |
}
|
|
@@ -1089,10 +1097,11 @@ int mak_grid_blocks_impl(int smem_bytes) {
|
|
| 1089 |
cudaError_t mak_launch_mega(const long long* prog, int n_phases, int pos0,
|
| 1090 |
int total, int slot0, int prompt_len,
|
| 1091 |
int chunk_m, int ring_stages, int* bar, int grid,
|
| 1092 |
-
int smem_bytes, cudaStream_t stream
|
|
|
|
| 1093 |
mak::mega_kernel<<<grid, BLOCK_THREADS, smem_bytes, stream>>>(
|
| 1094 |
prog, n_phases, pos0, total, slot0, prompt_len, chunk_m, ring_stages,
|
| 1095 |
-
bar);
|
| 1096 |
return cudaGetLastError();
|
| 1097 |
}
|
| 1098 |
|
|
|
|
| 940 |
__global__ void __launch_bounds__(BLOCK_THREADS, 2)
|
| 941 |
mega_kernel(const long long* __restrict__ prog, int n_phases,
|
| 942 |
int pos0, int total, int slot0, int prompt_len,
|
| 943 |
+
int chunk_m, int ring_stages, int* bar,
|
| 944 |
+
long long* __restrict__ ts) {
|
| 945 |
__shared__ int s_sense;
|
| 946 |
if (threadIdx.x == 0) s_sense = atomicAdd(bar + BAR_STRIPES + 1, 0);
|
| 947 |
__syncthreads();
|
| 948 |
+
// diagnostic: block 0 stamps its SM clock at entry and after each
|
| 949 |
+
// barrier crossing of the first step (pure observation, no data effect)
|
| 950 |
+
if (ts != nullptr && blockIdx.x == 0 && threadIdx.x == 0)
|
| 951 |
+
ts[0] = (long long)clock64();
|
| 952 |
int pos = pos0;
|
| 953 |
const int pos_end = pos0 + total;
|
| 954 |
while (pos < pos_end) {
|
|
|
|
| 989 |
}
|
| 990 |
grid_barrier(bar, &s_sense);
|
| 991 |
}
|
| 992 |
+
if (ts != nullptr && pos == pos0 && blockIdx.x == 0 &&
|
| 993 |
+
threadIdx.x == 0)
|
| 994 |
+
ts[p + 1] = (long long)clock64();
|
| 995 |
}
|
| 996 |
pos += M;
|
| 997 |
}
|
|
|
|
| 1097 |
cudaError_t mak_launch_mega(const long long* prog, int n_phases, int pos0,
|
| 1098 |
int total, int slot0, int prompt_len,
|
| 1099 |
int chunk_m, int ring_stages, int* bar, int grid,
|
| 1100 |
+
int smem_bytes, cudaStream_t stream,
|
| 1101 |
+
long long* ts) {
|
| 1102 |
mak::mega_kernel<<<grid, BLOCK_THREADS, smem_bytes, stream>>>(
|
| 1103 |
prog, n_phases, pos0, total, slot0, prompt_len, chunk_m, ring_stages,
|
| 1104 |
+
bar, ts);
|
| 1105 |
return cudaGetLastError();
|
| 1106 |
}
|
| 1107 |
|
torch-ext/torch_binding.cpp
CHANGED
|
@@ -71,7 +71,7 @@ void mak_run(at::Tensor prog, at::Tensor barrier, int64_t pos,
|
|
| 71 |
const cudaError_t err = mak_launch_mega(
|
| 72 |
reinterpret_cast<const long long*>(prog.data_ptr<int64_t>()),
|
| 73 |
(int)prog.size(0), (int)pos, 1, (int)step_slot, -1, 1, sh.ring_stages,
|
| 74 |
-
bar, mak_grid_blocks_impl(sh.smem), sh.smem, stream.stream());
|
| 75 |
TORCH_CHECK(err == cudaSuccess, "mak_run launch failed: ",
|
| 76 |
cudaGetErrorString(err));
|
| 77 |
}
|
|
@@ -91,7 +91,8 @@ void mak_run_steps(at::Tensor prog, at::Tensor barrier, int64_t pos0,
|
|
| 91 |
for (int64_t i = 0; i < steps; ++i) {
|
| 92 |
const cudaError_t err =
|
| 93 |
mak_launch_mega(pp, n, (int)(pos0 + i), 1, (int)(slot0 + i), 0, 1,
|
| 94 |
-
sh.ring_stages, bar, grid, sh.smem, stream.stream()
|
|
|
|
| 95 |
TORCH_CHECK(err == cudaSuccess, "mak_run_steps launch failed: ",
|
| 96 |
cudaGetErrorString(err));
|
| 97 |
}
|
|
@@ -112,11 +113,31 @@ void mak_run_seq(at::Tensor prog, at::Tensor barrier, int64_t pos0,
|
|
| 112 |
reinterpret_cast<const long long*>(prog.data_ptr<int64_t>()),
|
| 113 |
(int)prog.size(0), (int)pos0, (int)steps, (int)slot0, (int)prompt_len,
|
| 114 |
(int)chunk_m, sh.ring_stages, bar, mak_grid_blocks_impl(sh.smem),
|
| 115 |
-
sh.smem, stream.stream());
|
| 116 |
TORCH_CHECK(err == cudaSuccess, "mak_run_seq launch failed: ",
|
| 117 |
cudaGetErrorString(err));
|
| 118 |
}
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
at::Tensor mak_run_phased(at::Tensor prog, int64_t pos, int64_t step_slot,
|
| 121 |
bool timed, int64_t max_k) {
|
| 122 |
check_prog(prog);
|
|
@@ -169,6 +190,8 @@ TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, ops) {
|
|
| 169 |
"int max_k) -> Tensor");
|
| 170 |
ops.def("mak_num_blocks(Tensor prog, int max_k) -> int");
|
| 171 |
ops.def("mak_bw_per_sm(Tensor probe) -> float");
|
|
|
|
|
|
|
| 172 |
|
| 173 |
#if defined(CUDA_KERNEL) || defined(ROCM_KERNEL)
|
| 174 |
ops.impl("mak_run", torch::kCUDA, &mak_run);
|
|
@@ -177,6 +200,7 @@ TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, ops) {
|
|
| 177 |
ops.impl("mak_run_phased", torch::kCUDA, &mak_run_phased);
|
| 178 |
ops.impl("mak_num_blocks", torch::kCUDA, &mak_num_blocks);
|
| 179 |
ops.impl("mak_bw_per_sm", torch::kCUDA, &mak_bw_per_sm);
|
|
|
|
| 180 |
#endif
|
| 181 |
}
|
| 182 |
|
|
|
|
| 71 |
const cudaError_t err = mak_launch_mega(
|
| 72 |
reinterpret_cast<const long long*>(prog.data_ptr<int64_t>()),
|
| 73 |
(int)prog.size(0), (int)pos, 1, (int)step_slot, -1, 1, sh.ring_stages,
|
| 74 |
+
bar, mak_grid_blocks_impl(sh.smem), sh.smem, stream.stream(), nullptr);
|
| 75 |
TORCH_CHECK(err == cudaSuccess, "mak_run launch failed: ",
|
| 76 |
cudaGetErrorString(err));
|
| 77 |
}
|
|
|
|
| 91 |
for (int64_t i = 0; i < steps; ++i) {
|
| 92 |
const cudaError_t err =
|
| 93 |
mak_launch_mega(pp, n, (int)(pos0 + i), 1, (int)(slot0 + i), 0, 1,
|
| 94 |
+
sh.ring_stages, bar, grid, sh.smem, stream.stream(),
|
| 95 |
+
nullptr);
|
| 96 |
TORCH_CHECK(err == cudaSuccess, "mak_run_steps launch failed: ",
|
| 97 |
cudaGetErrorString(err));
|
| 98 |
}
|
|
|
|
| 113 |
reinterpret_cast<const long long*>(prog.data_ptr<int64_t>()),
|
| 114 |
(int)prog.size(0), (int)pos0, (int)steps, (int)slot0, (int)prompt_len,
|
| 115 |
(int)chunk_m, sh.ring_stages, bar, mak_grid_blocks_impl(sh.smem),
|
| 116 |
+
sh.smem, stream.stream(), nullptr);
|
| 117 |
TORCH_CHECK(err == cudaSuccess, "mak_run_seq launch failed: ",
|
| 118 |
cudaGetErrorString(err));
|
| 119 |
}
|
| 120 |
|
| 121 |
+
void mak_run_ts(at::Tensor prog, at::Tensor barrier, int64_t pos,
|
| 122 |
+
int64_t step_slot, int64_t max_k, at::Tensor ts) {
|
| 123 |
+
check_prog(prog);
|
| 124 |
+
check_barrier(barrier);
|
| 125 |
+
TORCH_CHECK(ts.is_cuda() && ts.scalar_type() == at::kLong &&
|
| 126 |
+
ts.numel() >= prog.size(0) + 1,
|
| 127 |
+
"ts must be a CUDA int64 tensor with n_phases+1 elements");
|
| 128 |
+
const c10::cuda::CUDAGuard guard(prog.device());
|
| 129 |
+
auto stream = at::cuda::getCurrentCUDAStream();
|
| 130 |
+
int* bar = barrier.data_ptr<int>();
|
| 131 |
+
const LaunchShape sh = shape_for(max_k);
|
| 132 |
+
const cudaError_t err = mak_launch_mega(
|
| 133 |
+
reinterpret_cast<const long long*>(prog.data_ptr<int64_t>()),
|
| 134 |
+
(int)prog.size(0), (int)pos, 1, (int)step_slot, -1, 1, sh.ring_stages,
|
| 135 |
+
bar, mak_grid_blocks_impl(sh.smem), sh.smem, stream.stream(),
|
| 136 |
+
reinterpret_cast<long long*>(ts.data_ptr<int64_t>()));
|
| 137 |
+
TORCH_CHECK(err == cudaSuccess, "mak_run_ts launch failed: ",
|
| 138 |
+
cudaGetErrorString(err));
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
at::Tensor mak_run_phased(at::Tensor prog, int64_t pos, int64_t step_slot,
|
| 142 |
bool timed, int64_t max_k) {
|
| 143 |
check_prog(prog);
|
|
|
|
| 190 |
"int max_k) -> Tensor");
|
| 191 |
ops.def("mak_num_blocks(Tensor prog, int max_k) -> int");
|
| 192 |
ops.def("mak_bw_per_sm(Tensor probe) -> float");
|
| 193 |
+
ops.def("mak_run_ts(Tensor prog, Tensor barrier, int pos, int step_slot, "
|
| 194 |
+
"int max_k, Tensor ts) -> ()");
|
| 195 |
|
| 196 |
#if defined(CUDA_KERNEL) || defined(ROCM_KERNEL)
|
| 197 |
ops.impl("mak_run", torch::kCUDA, &mak_run);
|
|
|
|
| 200 |
ops.impl("mak_run_phased", torch::kCUDA, &mak_run_phased);
|
| 201 |
ops.impl("mak_num_blocks", torch::kCUDA, &mak_num_blocks);
|
| 202 |
ops.impl("mak_bw_per_sm", torch::kCUDA, &mak_bw_per_sm);
|
| 203 |
+
ops.impl("mak_run_ts", torch::kCUDA, &mak_run_ts);
|
| 204 |
#endif
|
| 205 |
}
|
| 206 |
|