#include "b60_weight_cache.hpp" #include "b60_layout_state.hpp" #include "strata/kernels/q8_1_finite.hpp" // src/kernels/cuda/fused_gr.cu - see include/strata/kernels/fused_gr.hpp. #define DPCT_PROFILING_ENABLED #include #include "strata/kprof.hpp" #include #include #include "strata/core/geometry.hpp" #include #include #include #include #include #include "strata/sycl_queue.hpp" #include "strata/workload_trace.hpp" #include "strata/core/emulate.hpp" #include "strata/kernels/fused_gr.hpp" #include "strata/kernels/bf16_bits.hpp" #include "strata/kernels/verify_kernels.hpp" #include #include #include #include #include #include #include namespace strata::kernels { namespace { constexpr int N = strata::core::kCompiledGeometry.n_embd; // 2560 in Flash-Next constexpr int HC = strata::core::kCompiledGeometry.hc; // streams constexpr int D = N * HC; // 10240 constexpr int LR = strata::core::kCompiledGeometry.hc_lr; // 320 static_assert(N % 512 == 0 && LR % 64 == 0 && LR >= 256 && LR <= 512, "fused_gr: n_embd must be a multiple of 512 and hc_lr one of 256..512 in steps of 64 (the lane rule `lane < LR / 8 - 32` and the 128-column slices)"); constexpr int THREADS = 256; constexpr int WARPS = THREADS / 32; #if defined(__HIPCC__) constexpr int TILE = N / 2; // eight-token tile fits gfx1100's 64 KiB LDS limit #else constexpr int TILE = N; // xn floats per token staged at a time: 320 chunks of 8, 10 per lane (Flash-Next) #endif // The single-token down kernel keeps the whole normalised hidden row (D floats) in work-group local memory: static while that // fits 48 KB (Flash-Next: 40 KB). Beyond it (hidden 3072 and up) fused_gr_read takes the multi-token read of one token, which is // bitwise the same read (the sliced / split kernels need 4 KB of local memory) and the default path anyway. constexpr bool kDownXnTooBig = (size_t) D * 4 + 512 > 48 * 1024; constexpr int TQ = TILE / 8 / 32; // uint4 weight chunks per lane per tile // SYCL port: the down kernel's 41 work-groups filled 15% of the B70's threads (metrics 2026-09-30); each row's // D = 4 tiles are split over DOWN_SPLIT work-groups that write partial sums, summed in a fixed order by the up // kernel (deterministic, unlike atomics) before the row's nonlinearity. #ifndef STRATA_GR_DOWN_SPLIT #define STRATA_GR_DOWN_SPLIT (D / TILE) // 4: one tile per split (1: the unsplit order, for A/B) #endif constexpr int DOWN_SPLIT = STRATA_GR_DOWN_SPLIT; constexpr int DOWN_BLOCKS = LR / WARPS; // 40 blocks of 8 rows; one more for the inject rows constexpr int UP_COLS = 32; // columns d per `up` block (x 4 streams = 128 rows) constexpr int UP_BLOCKS = N / UP_COLS; // 80 __dpct_inline__ float warp_sum(float v) { #pragma unroll /* DPCT1108: '__shfl_xor_sync' was migrated with the experimental feature masked sub_group function which may not be supported by all compilers or runtimes. You may need to adjust the code. */ for (int o = 16; o > 0; o >>= 1) v += dpct::experimental::permute_sub_group_by_xor( 0xffffffffu, sycl::ext::oneapi::this_work_item::get_sub_group(), v, o); return v; } int g_gr_cached_norm_mode=[](){ const char* e=std::getenv("STRATA_SYCL_GR_CACHED_NORM"); return e&&std::atoi(e)==3?3:0; }(); bool gr_overlap(const void* a,size_t as,const void* b,size_t bs){ if(!a||!b||!as||!bs)return false; const uintptr_t av=reinterpret_cast(a),bv=reinterpret_cast(b); return av<=bv?bv-av(out)&15u)||gr_overlap(out,D*4,xn,size_t(T)*D*4))return false; for(int j=0;j(v[j] << 16), (float)(x[2 * j]), acc); acc = sycl::fma(sycl::bit_cast(v[j] & 0xffff0000u), (float)(x[2 * j + 1]), acc); } return acc; } /* DPCT1110: The total declared local variable size in device function gr_down_kernel exceeds 128 bytes and may cause high register pressure. Consult with your hardware vendor to find the total register size available and adjust the code, or use smaller sub-group size to avoid high register pressure. */ __dpct_inline__ void gr_down_kernel(FusedGrArgs a) { auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); auto &xn = *sycl::ext::oneapi::group_local_memory_for_overwrite( sycl::ext::oneapi::this_work_item::get_work_group<3>()); auto &part = *sycl::ext::oneapi::group_local_memory_for_overwrite( sycl::ext::oneapi::this_work_item::get_work_group<3>()); auto &s_rs = *sycl::ext::oneapi::group_local_memory_for_overwrite( sycl::ext::oneapi::this_work_item::get_work_group<3>()); const int t = item_ct1.get_local_id(2), lane = t & 31, warp = t >> 5; float gw[HC]; #pragma unroll for (int c = 0; c < HC; ++c) gw[c] = a.apply ? 2.0f * sigmoidf_(a.inj_prev[c] / (float) HC) : 0.0f; // 1. R' * w_norm into shared memory, and the per-stream sums of squares of R'. float ss[HC] = {0.0f, 0.0f, 0.0f, 0.0f}; for (int i = t * 4; i < D; i += THREADS * 4) { const int c = i / N, d = i - c * N; sycl::float4 r = *reinterpret_cast(a.R + i); if (a.apply) { const sycl::float4 b = *reinterpret_cast(a.bo_prev + d); r.x() = sycl::fma((float)(b.x()), gw[c], r.x()); r.y() = sycl::fma((float)(b.y()), gw[c], r.y()); r.z() = sycl::fma((float)(b.z()), gw[c], r.z()); r.w() = sycl::fma((float)(b.w()), gw[c], r.w()); } const sycl::float4 g = *reinterpret_cast(a.w_norm + i); float sq = r.x() * r.x() + r.y() * r.y() + r.z() * r.z() + r.w() * r.w(); #pragma unroll for (int cc = 0; cc < HC; ++cc) if (cc == c) ss[cc] += sq; *reinterpret_cast(xn + i) = sycl::float4( r.x() * g.x(), r.y() * g.y(), r.z() * g.z(), r.w() * g.w()); } #pragma unroll for (int c = 0; c < HC; ++c) { const float v = warp_sum(ss[c]); if (lane == 0) part[warp][c] = v; } /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); if (t < HC) { float s = 0.0f; #pragma unroll for (int w = 0; w < WARPS; ++w) s += part[w][t]; s_rs[t] = sycl::rsqrt(s / (float)N + a.eps); if (item_ct1.get_group(2) == 0) a.rs[t] = s_rs[t]; } /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); #pragma unroll for (int i = t; i < D; i += THREADS) xn[i] *= s_rs[i / N]; /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); // 2. one warp per output row: 10240 bf16 = 1280 chunks of 8, 40 per lane. const bool inject_block = item_ct1.get_group(2) == DOWN_BLOCKS; const int row = inject_block ? warp : item_ct1.get_group(2) * WARPS + warp; if (inject_block && (a.w_inject == nullptr || warp >= HC)) return; const uint16_t* wrow = (inject_block ? a.w_inject : a.w_down) + (size_t) row * D; const sycl::uint4 *w4 = reinterpret_cast(wrow); // SYCL port: the multi kernel's order - per-tile partial sums for the split rows (gr_parity: bit-identical), // the inject rows still one accumulation over the whole row (their block is not split there) float acc = 0.0f; if (inject_block) { #pragma unroll 4 for (int j = lane; j < D / 8; j += 32) acc += dot8(*(w4 + j), xn + j * 8); acc = warp_sum(acc); } else { for (int sp = 0; sp < DOWN_SPLIT; ++sp) { float part = 0.0f; #pragma unroll 5 for (int j = lane + sp * (TILE / 8); j < (sp + 1) * (TILE / 8); j += 32) part += dot8(*(w4 + j), xn + j * 8); acc += warp_sum(part); } } if (lane != 0) return; if (inject_block) { a.inject_out[row] = acc; } else { const float x = acc / (float) HC; a.lo[row] = x / (1.0f + sycl::native::exp(-x)); } } __dpct_inline__ void gr_up_kernel(FusedGrArgs a) { auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); auto &lo = *sycl::ext::oneapi::group_local_memory_for_overwrite( sycl::ext::oneapi::this_work_item::get_work_group<3>()); auto &g = *sycl::ext::oneapi::group_local_memory_for_overwrite< float[HC][UP_COLS]>( sycl::ext::oneapi::this_work_item::get_work_group<3>()); const int t = item_ct1.get_local_id(2), lane = t & 31, warp = t >> 5; const int d0 = item_ct1.get_group(2) * UP_COLS; #pragma unroll for (int k = t; k < LR; k += THREADS) lo[k] = a.lo[k]; /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); // 128 rows (4 streams x 32 columns), 16 per warp: 320 bf16 = 40 chunks of 8. for (int r = warp; r < HC * UP_COLS; r += WARPS) { const int c = r / UP_COLS, dd = r - c * UP_COLS, i = c * N + d0 + dd; const sycl::uint4 *w4 = reinterpret_cast(a.w_up + (size_t)i * LR); /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ float acc = dot8(*(w4 + lane), lo + lane * 8); /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ if (lane < LR / 8 - 32) acc += dot8(*(w4 + 32 + lane), lo + (32 + lane) * 8); acc = warp_sum(acc); if (lane == 0) { float rv = a.R[i]; if (a.apply) { rv = sycl::fma((float)(a.bo_prev[d0 + dd]), 2.0f * sigmoidf_(a.inj_prev[c] / (float)HC), rv); a.R_out[i] = rv; // this block owns column d0+dd of every stream } const float x = rv * a.w_norm[i] * a.rs[c]; g[c][dd] = x * sigmoidf_(acc); } } /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); if (t < UP_COLS) { float s = 0.0f; #pragma unroll for (int c = 0; c < HC; ++c) s += g[c][t]; a.mixed[d0 + t] = s / (float) HC; } } // ================================ plan v0.3 P6: T tokens, one weight read ================================ struct GrMulti { FusedGrArgs a[kFusedGrMaxT]; float* xn; int T; float* part; ///< [DOWN_SPLIT][kFusedGrMaxT][LR] partial row sums of the down kernel int nsplit = DOWN_SPLIT; ///< SYCL port: splits the up kernel sums (1 after the sliced down kernel's reduce) float* part2 = nullptr; ///< SYCL port: [GRS_SLICES][kFusedGrMaxT][LR + HC] slice partials of the sliced kernel bool cached_norm = false; bool cached_lo = false; }; // gr_overlap: defined once above (the #3153 helper; the #4185 port's copy of it was the same function) bool gr_reuse_safe(const FusedGrArgs* a, int T, const float* xn) { for (int k = 0; k < T; ++k) { const std::pair spans[] = { {a[k].R, D * sizeof(float)}, {a[k].R_out, D * sizeof(float)}, {a[k].bo_prev, N * sizeof(float)}, {a[k].inj_prev, HC * sizeof(float)}, {a[k].w_norm, D * sizeof(float)}, {a[k].w_down, size_t(LR) * D * sizeof(uint16_t)}, {a[k].w_up, size_t(D) * LR * sizeof(uint16_t)}, {a[k].w_inject, size_t(HC) * D * sizeof(uint16_t)}, {a[k].rs, HC * sizeof(float)}, {a[k].inject_out, HC * sizeof(float)}, {a[k].mixed, N * sizeof(float)}, }; if (gr_overlap(xn, size_t(T) * D * sizeof(float), a[k].lo, LR * sizeof(float))) return false; for (const auto& span : spans) { if (gr_overlap(xn, size_t(T) * D * sizeof(float), span.first, span.second)) return false; for (int j = 0; j < T; ++j) if (gr_overlap(a[j].lo, LR * sizeof(float), span.first, span.second)) return false; } for (int j = 0; j < k; ++j) if (gr_overlap(a[j].lo, LR * sizeof(float), a[k].lo, LR * sizeof(float))) return false; } return true; } // SYCL port (Arc): a RUNTIME index into the by-value kernel argument (m.a[k]) makes IGC copy the whole GrMulti // (~1.2 KB) into every work-item's private scratch - unitrace: ~40 KB private per SIMD32 thread on the A770 for // gr_up_multi / gr_down_reduce / gr_norm_split. GR_PICK selects ONE field with compile-time indices only (a chain // of selects over uniform kernel arguments), so nothing is copied. Addressing only: values and orders unchanged. template __dpct_inline__ auto gr_pick_impl(const GrMulti& m, int k, F f, std::index_sequence) { auto v = f(m.a[0]); ((v = (k == (int) J) ? f(m.a[J]) : v), ...); return v; } #define GR_PICK(m, k, field) \ gr_pick_impl((m), (k), [](const FusedGrArgs& x_) { return x_.field; }, std::make_index_sequence{}) // Step 1 of `gr_down_kernel`, one block per token, same threads and reduction order: rs[t] and xn[t] to global. /* DPCT1110: The total declared local variable size in device function gr_norm_multi_kernel exceeds 128 bytes and may cause high register pressure. Consult with your hardware vendor to find the total register size available and adjust the code, or use smaller sub-group size to avoid high register pressure. */ __dpct_inline__ void gr_norm_multi_kernel(GrMulti m) { auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); auto &part = *sycl::ext::oneapi::group_local_memory_for_overwrite( sycl::ext::oneapi::this_work_item::get_work_group<3>()); auto &s_rs = *sycl::ext::oneapi::group_local_memory_for_overwrite( sycl::ext::oneapi::this_work_item::get_work_group<3>()); const FusedGrArgs &a = m.a[item_ct1.get_group(2)]; float *xn = m.xn + (size_t)item_ct1.get_group(2) * D; const int t = item_ct1.get_local_id(2), lane = t & 31, warp = t >> 5; float gw[HC]; #pragma unroll for (int c = 0; c < HC; ++c) gw[c] = a.apply ? 2.0f * sigmoidf_(a.inj_prev[c] / (float) HC) : 0.0f; float ss[HC] = {0.0f, 0.0f, 0.0f, 0.0f}; for (int i = t * 4; i < D; i += THREADS * 4) { const int c = i / N, d = i - c * N; sycl::float4 r = *reinterpret_cast(a.R + i); if (a.apply) { const sycl::float4 b = *reinterpret_cast(a.bo_prev + d); r.x() = sycl::fma((float)(b.x()), gw[c], r.x()); r.y() = sycl::fma((float)(b.y()), gw[c], r.y()); r.z() = sycl::fma((float)(b.z()), gw[c], r.z()); r.w() = sycl::fma((float)(b.w()), gw[c], r.w()); } const sycl::float4 g = *reinterpret_cast(a.w_norm + i); float sq = r.x() * r.x() + r.y() * r.y() + r.z() * r.z() + r.w() * r.w(); #pragma unroll for (int cc = 0; cc < HC; ++cc) if (cc == c) ss[cc] += sq; *reinterpret_cast(xn + i) = sycl::float4( r.x() * g.x(), r.y() * g.y(), r.z() * g.z(), r.w() * g.w()); } #pragma unroll for (int c = 0; c < HC; ++c) { const float v = warp_sum(ss[c]); if (lane == 0) part[warp][c] = v; } /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); if (t < HC) { float s = 0.0f; #pragma unroll for (int w = 0; w < WARPS; ++w) s += part[w][t]; s_rs[t] = sycl::rsqrt(s / (float)N + a.eps); a.rs[t] = s_rs[t]; } /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); #pragma unroll for (int i = t; i < D; i += THREADS) xn[i] *= s_rs[i / N]; } // SYCL port: gr_norm_multi_kernel with one work-group per (token, stream) instead of per token: T groups left the // B70 nearly idle (16 us a call). rs is per stream, so a group owns its stream outright. Thread t visits the same // elements of stream c in the same order as in the per-token kernel (i = 4t + 1024k), and the warp and cross-warp // sums run in the same order, so rs and xn are bitwise the old kernel's. The scaled values stay in registers // (at most 3 float4 per thread) instead of a second pass over global memory. __dpct_inline__ void gr_norm_split_port_kernel(GrMulti m) { auto item = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); auto& part = *sycl::ext::oneapi::group_local_memory_for_overwrite(item.get_group()); auto& s_rs = *sycl::ext::oneapi::group_local_memory_for_overwrite(item.get_group()); const int tok = (int) item.get_group(2) / HC, c = (int) item.get_group(2) % HC; const float* const aR = GR_PICK(m, tok, R); const bool aapply = GR_PICK(m, tok, apply); const float* const abo = GR_PICK(m, tok, bo_prev); const float* const awn = GR_PICK(m, tok, w_norm); const float aeps = GR_PICK(m, tok, eps); float* const ars = GR_PICK(m, tok, rs); float* xn = m.xn + (size_t) tok * D; const int t = (int) item.get_local_id(2), lane = t & 31, warp = t >> 5; const float gw = aapply ? 2.0f * sigmoidf_(GR_PICK(m, tok, inj_prev)[c] / (float) HC) : 0.0f; constexpr int MAXK = (N + THREADS * 4 - 1) / (THREADS * 4) + 1; // 3: float4s of one stream per thread sycl::float4 keep[MAXK]; int kept = 0; float ss = 0.0f; for (int i = t * 4; i < D; i += THREADS * 4) { if (i / N != c) continue; const int d = i - c * N; sycl::float4 r = *reinterpret_cast(aR + i); if (aapply) { const sycl::float4 b = *reinterpret_cast(abo + d); r.x() = sycl::fma((float) (b.x()), gw, r.x()); r.y() = sycl::fma((float) (b.y()), gw, r.y()); r.z() = sycl::fma((float) (b.z()), gw, r.z()); r.w() = sycl::fma((float) (b.w()), gw, r.w()); } const sycl::float4 g = *reinterpret_cast(awn + i); ss += r.x() * r.x() + r.y() * r.y() + r.z() * r.z() + r.w() * r.w(); if (kept < MAXK) keep[kept++] = sycl::float4(r.x() * g.x(), r.y() * g.y(), r.z() * g.z(), r.w() * g.w()); } const float v = warp_sum(ss); if (lane == 0) part[warp] = v; item.barrier(sycl::access::fence_space::local_space); if (t == 0) { float sum = 0.0f; #pragma unroll for (int w = 0; w < WARPS; ++w) sum += part[w]; s_rs = sycl::rsqrt(sum / (float) N + aeps); ars[c] = s_rs; } item.barrier(sycl::access::fence_space::local_space); const float rs = s_rs; int k = 0; for (int i = t * 4; i < D && k < kept; i += THREADS * 4) { if (i / N != c) continue; const sycl::float4 x = keep[k++]; *reinterpret_cast(xn + i) = sycl::float4(x.x() * rs, x.y() * rs, x.z() * rs, x.w() * rs); } } // Visit only the owned stream, preserving thread ownership and reduction order. __dpct_inline__ void gr_norm_bounds_port_kernel(GrMulti m) { auto item = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); auto& part = *sycl::ext::oneapi::group_local_memory_for_overwrite(item.get_group()); auto& s_rs = *sycl::ext::oneapi::group_local_memory_for_overwrite(item.get_group()); const int tok = (int) item.get_group(2) / HC, c = (int) item.get_group(2) % HC; const FusedGrArgs& a = m.a[tok]; float* xn = m.xn + (size_t) tok * D; const int t = (int) item.get_local_id(2), lane = t & 31, warp = t >> 5; const float gw = a.apply ? 2.0f * sigmoidf_(a.inj_prev[c] / (float) HC) : 0.0f; constexpr int MAXK = (N + THREADS * 4 - 1) / (THREADS * 4) + 1; // 3: float4s of one stream per thread sycl::float4 keep[MAXK]; int kept = 0; float ss = 0.0f; const int first_i = t * 4 + ((c * N - t * 4 + THREADS * 4 - 1) / (THREADS * 4)) * (THREADS * 4); const int end_i = (c + 1) * N; for (int i = first_i; i < end_i; i += THREADS * 4) { const int d = i - c * N; sycl::float4 r = *reinterpret_cast(a.R + i); if (a.apply) { const sycl::float4 b = *reinterpret_cast(a.bo_prev + d); r.x() = sycl::fma((float) (b.x()), gw, r.x()); r.y() = sycl::fma((float) (b.y()), gw, r.y()); r.z() = sycl::fma((float) (b.z()), gw, r.z()); r.w() = sycl::fma((float) (b.w()), gw, r.w()); } const sycl::float4 g = *reinterpret_cast(a.w_norm + i); ss += r.x() * r.x() + r.y() * r.y() + r.z() * r.z() + r.w() * r.w(); if (kept < MAXK) keep[kept++] = sycl::float4(r.x() * g.x(), r.y() * g.y(), r.z() * g.z(), r.w() * g.w()); } const float v = warp_sum(ss); if (lane == 0) part[warp] = v; item.barrier(sycl::access::fence_space::local_space); if (t == 0) { float sum = 0.0f; #pragma unroll for (int w = 0; w < WARPS; ++w) sum += part[w]; s_rs = sycl::rsqrt(sum / (float) N + a.eps); a.rs[c] = s_rs; } item.barrier(sycl::access::fence_space::local_space); const float rs = s_rs; int k = 0; for (int i = first_i; i < end_i && k < kept; i += THREADS * 4) { const sycl::float4 x = keep[k++]; *reinterpret_cast(xn + i) = sycl::float4(x.x() * rs, x.y() * rs, x.z() * rs, x.w() * rs); } } // Cached normalized product with the original optional pending write. __dpct_inline__ void gr_norm_cached_write_kernel(GrMulti m) { auto item = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); auto& part = *sycl::ext::oneapi::group_local_memory_for_overwrite(item.get_group()); auto& s_rs = *sycl::ext::oneapi::group_local_memory_for_overwrite(item.get_group()); const int tok = (int) item.get_group(2) / HC, c = (int) item.get_group(2) % HC; const FusedGrArgs& a = m.a[tok]; float* xn = m.xn + (size_t) tok * D; const int t = (int) item.get_local_id(2), lane = t & 31, warp = t >> 5; const float gw = a.apply ? 2.0f * sigmoidf_(a.inj_prev[c] / (float) HC) : 0.0f; constexpr int MAXK = (N + THREADS * 4 - 1) / (THREADS * 4) + 1; // 3: float4s of one stream per thread sycl::float4 keep[MAXK]; int kept = 0; float ss = 0.0f; const int first_i = t * 4 + ((c * N - t * 4 + THREADS * 4 - 1) / (THREADS * 4)) * (THREADS * 4); const int end_i = (c + 1) * N; for (int i = first_i; i < end_i; i += THREADS * 4) { const int d = i - c * N; sycl::float4 r = *reinterpret_cast(a.R + i); if (a.apply) { const sycl::float4 b = *reinterpret_cast(a.bo_prev + d); r.x() = sycl::fma((float) (b.x()), gw, r.x()); r.y() = sycl::fma((float) (b.y()), gw, r.y()); r.z() = sycl::fma((float) (b.z()), gw, r.z()); r.w() = sycl::fma((float) (b.w()), gw, r.w()); } if(a.apply)*reinterpret_cast(a.R_out+i)=r; const sycl::float4 g = *reinterpret_cast(a.w_norm + i); ss += r.x() * r.x() + r.y() * r.y() + r.z() * r.z() + r.w() * r.w(); if (kept < MAXK) keep[kept++] = sycl::float4(r.x() * g.x(), r.y() * g.y(), r.z() * g.z(), r.w() * g.w()); } const float v = warp_sum(ss); if (lane == 0) part[warp] = v; item.barrier(sycl::access::fence_space::local_space); if (t == 0) { float sum = 0.0f; #pragma unroll for (int w = 0; w < WARPS; ++w) sum += part[w]; s_rs = sycl::rsqrt(sum / (float) N + a.eps); a.rs[c] = s_rs; } item.barrier(sycl::access::fence_space::local_space); const float rs = s_rs; int k = 0; for (int i = first_i; i < end_i && k < kept; i += THREADS * 4) { const sycl::float4 x = keep[k++]; *reinterpret_cast(xn + i) = sycl::float4(x.x() * rs, x.y() * rs, x.z() * rs, x.w() * rs); } } const bool g_gr_norm_direct_bounds = [] { const char* value = std::getenv("STRATA_SYCL_GR_NORM_BOUNDS"); return value && value[0] == '1'; }(); bool gr_norm_split() { // default; STRATA_GR_NORM_SPLIT=0: one work-group per token static const bool v = std::getenv("STRATA_GR_NORM_SPLIT") == nullptr || std::atoi(std::getenv("STRATA_GR_NORM_SPLIT")) != 0; return v; } // Step 2 of `gr_down_kernel` for T tokens. One warp per row (so each lane accumulates the same chunks in the // same order as the single-token kernel); per tile the lane's weight chunks are loaded BEFORE the activation // tile is staged, so the DRAM and L2 traffic are in flight together. // // TILEV = xn floats per token staged at a time. The tile only changes the staging granularity: the lane's chunk // order (lane + 32*q within the tile, tiles ascending) is strictly increasing for either value, so the results are // bitwise identical to `gr_down_kernel` for both. 2560 stages 320 chunks of 8 per tile (10 per lane); cards whose // opt-in below 8 * 2560 * 4 B slices the tokens - sm_75 (64 KiB) carries 6 tokens of it - run TILEV 1280 instead, // which fits all eight tokens in one 40 KiB launch and stages 160 chunks of 8 (5 per lane, half the registers held // for the weight prefetch); smaller tiles raise how many blocks share an SM (the 41-block grid), e.g. three blocks // of four tokens instead of one on sm_75. template /* DPCT1110: The total declared local variable size in device function gr_down_multi_kernel exceeds 128 bytes and may cause high register pressure. Consult with your hardware vendor to find the total register size available and adjust the code, or use smaller sub-group size to avoid high register pressure. */ __dpct_inline__ void gr_down_multi_kernel(GrMulti m, uint8_t *dpct_local) { auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); constexpr int TQ = TILEV / 8 / 32; // uint4 weight chunks per lane per tile auto tile = (float *)dpct_local; // [T][TILEV] const int t = item_ct1.get_local_id(2), lane = t & 31, warp = t >> 5; const int T = m.T; const int gid = (int) item_ct1.get_group(2); const bool inject_block = gid == DOWN_BLOCKS * DOWN_SPLIT; const int split = inject_block ? 0 : gid % DOWN_SPLIT, rb = inject_block ? 0 : gid / DOWN_SPLIT; const int row = inject_block ? warp : rb * WARPS + warp; const bool active = !(inject_block && (m.a[0].w_inject == nullptr || warp >= HC)); const uint16_t* wrow = (inject_block ? m.a[0].w_inject : m.a[0].w_down) + (size_t) (active ? row : 0) * D; const sycl::uint4 *w4 = reinterpret_cast(wrow); float acc[MAX_T]; #pragma unroll for (int k = 0; k < MAX_T; ++k) acc[k] = 0.0f; for (int base = inject_block ? 0 : split * TILEV; base < D; base += inject_block ? TILEV : TILEV * DOWN_SPLIT) { sycl::uint4 wv[TQ]; if (active) { #pragma unroll /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ for (int q = 0; q < TQ; ++q) wv[q] = *(w4 + base / 8 + lane + 32 * q); } /* DPCT1118: SYCL group functions and algorithms must be encountered in converged control flow. You may need to adjust the code. */ /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); // the previous tile is consumed const sycl::float4 *src4 = reinterpret_cast(m.xn); sycl::float4 *tile4 = reinterpret_cast(tile); for (int i = t; i < T * (TILEV / 4); i += THREADS) { const int k = i / (TILEV / 4), off = i - k * (TILEV / 4); tile4[i] = src4[((size_t) k * D + base) / 4 + off]; } /* DPCT1118: SYCL group functions and algorithms must be encountered in converged control flow. You may need to adjust the code. */ /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); if (!active) continue; #pragma unroll for (int q = 0; q < TQ; ++q) { const int j = lane + 32 * q; #pragma unroll for (int k = 0; k < MAX_T; ++k) if (k < T) acc[k] += dot8(wv[q], tile + k * TILEV + j * 8); } } if (!active) return; float s[MAX_T]; #pragma unroll for (int k = 0; k < MAX_T; ++k) s[k] = k < T ? warp_sum(acc[k]) : 0.0f; // lane k writes token k (every lane holds every sum after the xor reduction) #pragma unroll for (int k = 0; k < MAX_T; ++k) { if (k >= T || lane != k) continue; if (inject_block) { m.a[k].inject_out[row] = s[k]; } else { m.part[((size_t) split * kFusedGrMaxT + k) * LR + row] = s[k]; // the up kernel sums the splits } } } // SYCL port: gr_down_multi_kernel without the shared-memory tile. The tiled kernel stages T x 2560 floats per // work-group (60 KB at T = 6) for 8 rows (40 KB of weights): the copy outweighs the weights and the SLM caps the // B70 at ~2 groups per Xe core (unitrace: 64 us per call, ~100 GB/s). Here each lane reads its 8 xn floats per // token straight from global memory (T x 40 KB in all, L1/L2-resident, shared by every group). Same per-lane // order of the sums, so the outputs are bitwise the tiled kernel's. __dpct_inline__ void gr_down_multi_direct_kernel(GrMulti m) { auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); const int t = item_ct1.get_local_id(2), lane = t & 31, warp = t >> 5; const int T = m.T; const int gid = (int) item_ct1.get_group(2); const bool inject_block = gid == DOWN_BLOCKS * DOWN_SPLIT; const int split = inject_block ? 0 : gid % DOWN_SPLIT, rb = inject_block ? 0 : gid / DOWN_SPLIT; const int row = inject_block ? warp : rb * WARPS + warp; const bool active = !(inject_block && (m.a[0].w_inject == nullptr || warp >= HC)); if (!active) return; const uint16_t* wrow = (inject_block ? m.a[0].w_inject : m.a[0].w_down) + (size_t) row * D; const sycl::uint4* w4 = reinterpret_cast(wrow); float acc[kFusedGrMaxT]; #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) acc[k] = 0.0f; for (int base = inject_block ? 0 : split * TILE; base < D; base += inject_block ? TILE : TILE * DOWN_SPLIT) { sycl::uint4 wv[TQ]; #pragma unroll for (int q = 0; q < TQ; ++q) wv[q] = *(w4 + base / 8 + lane + 32 * q); #pragma unroll for (int q = 0; q < TQ; ++q) { const int j = lane + 32 * q; #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) if (k < T) { const sycl::float4* x4 = reinterpret_cast(m.xn + (size_t) k * D + base + j * 8); const sycl::float4 a0 = x4[0], a1 = x4[1]; const float xv[8] = {a0.x(), a0.y(), a0.z(), a0.w(), a1.x(), a1.y(), a1.z(), a1.w()}; acc[k] += dot8(wv[q], xv); } } } float s[kFusedGrMaxT]; #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) s[k] = k < T ? warp_sum(acc[k]) : 0.0f; #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) { if (k >= T || lane != k) continue; if (inject_block) m.a[k].inject_out[row] = s[k]; else m.part[((size_t) split * kFusedGrMaxT + k) * LR + row] = s[k]; } } // SYCL port: the down projection sliced by columns. The direct kernel above has each warp read T x 32 bytes of // activations (L2) per 16 bytes of weights - 12x the weight traffic at 6 tokens, ~134 GB/s. Here work-group s owns // columns [128 s, 128 s + 128) of all 324 rows (320 down + 4 inject): it stages that slice of xn for the T tokens // once (<= 4 KB of SLM) and streams the rows through it, 4 lanes per row (one 64-byte line per step), reducing over // those 4 lanes once per row. gr_down_reduce then sums the 80 slice partials in a fixed order. constexpr int GRS_COLS = 128, GRS_SLICES = D / GRS_COLS, GRS_ROWS = LR + HC; // 80 slices, 324 rows constexpr int GRS_RB = (GRS_ROWS + WARPS * 8 - 1) / (WARPS * 8); // 6 blocks of 64 rows // MORE (gr_more_groups): one work-group per (slice, row block) - 480 groups instead of 80; every row's sum is the same template __dpct_inline__ void gr_down_sliced_kernel(GrMulti m, float* tile) { auto item = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); const int t = (int) item.get_local_id(2), lane = t & 31, warp = t >> 5; const int g = (int) item.get_group(2); const int T = m.T, sl = MORE ? g / GRS_RB : g, c0 = sl * GRS_COLS; const int r_first = MORE ? (g % GRS_RB) * WARPS * 8 : 0, r_end = MORE ? r_first + WARPS * 8 : GRS_ROWS; for (int i = t; i < T * GRS_COLS; i += THREADS) { const int k = i / GRS_COLS, c = i - k * GRS_COLS; tile[i] = m.xn[(size_t) k * D + c0 + c]; } item.barrier(sycl::access::fence_space::local_space); const int rsub = lane >> 2, q = lane & 3; const bool inj = m.a[0].w_inject != nullptr; for (int r0 = r_first; r0 < r_end; r0 += WARPS * 8) { const int r = r0 + warp * 8 + rsub; const bool ok = r < LR || (r < GRS_ROWS && inj); const uint16_t* wrow = r < LR ? m.a[0].w_down + (size_t) r * D : m.a[0].w_inject + (size_t) (ok ? r - LR : 0) * D; sycl::uint4 wv[4]; #pragma unroll for (int cc = 0; cc < 4; ++cc) wv[cc] = ok ? reinterpret_cast(wrow + c0)[q + 4 * cc] : sycl::uint4(0, 0, 0, 0); float acc[kFusedGrMaxT]; #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) { acc[k] = 0.0f; if (k < T) { #pragma unroll for (int cc = 0; cc < 4; ++cc) acc[k] += dot8(wv[cc], tile + k * GRS_COLS + (q + 4 * cc) * 8); } } auto sg = item.get_sub_group(); #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) { if (k >= T) break; float v = acc[k]; v += sycl::permute_group_by_xor(sg, v, 1); v += sycl::permute_group_by_xor(sg, v, 2); if (q == 0 && ok) m.part2[((size_t) sl * kFusedGrMaxT + k) * GRS_ROWS + r] = v; } } } static_assert(GRS_SLICES % 8 == 0, "the 8-at-a-time reduce reads whole groups of 8 slices"); template __dpct_inline__ void gr_down_reduce_kernel(GrMulti m) { auto item = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); const int i = (int) item.get_global_id(2); if (i >= m.T * GRS_ROWS) return; const int k = i / GRS_ROWS, r = i - k * GRS_ROWS; if (r >= LR && m.a[0].w_inject == nullptr) return; float x = 0.0f; if constexpr (MORE) { // 8 loads in flight per step (the one-at-a-time loop was load-latency bound on the A770), then added one by // one: x += slice 0, 1, 2, ... 79, the same sequence of float adds as the loop below (-fp-model=precise) const float* src = m.part2 + (size_t) k * GRS_ROWS + r; constexpr size_t SL_STRIDE = (size_t) kFusedGrMaxT * GRS_ROWS; for (int sl = 0; sl < GRS_SLICES; sl += 8) { float pv[8]; #pragma unroll for (int j = 0; j < 8; ++j) pv[j] = src[(size_t) (sl + j) * SL_STRIDE]; #pragma unroll for (int j = 0; j < 8; ++j) x += pv[j]; } } else { for (int sl = 0; sl < GRS_SLICES; ++sl) x += m.part2[((size_t) sl * kFusedGrMaxT + k) * GRS_ROWS + r]; } if (r < LR) { m.part[(size_t) k * LR + r] = x; // split 0 if constexpr (REUSE_LO) { float v = 0.0f; v += x; v /= (float) HC; GR_PICK(m, k, lo)[r] = v / (1.0f + sycl::native::exp(-v)); } } else GR_PICK(m, k, inject_out)[r - LR] = x; } // Large-GRF mode (256 registers per thread instead of 128) for the sliced down kernel and the multi-token up kernel. // On the A770 the up kernel spills ~3.4 KB per thread at 128 (unitrace). Large GRF halves the threads an Xe core keeps // resident, so it is the default on Arc A-series (Alchemist) only, where it was measured; Battlemage and anything else // keep the compiler's default GRF and the same kernels as before. STRATA_GR_LARGE_GRF=1 forces it on, =0 off. bool gr_large_grf(sycl::queue* q) { static const bool v = [q] { const char* e = std::getenv("STRATA_GR_LARGE_GRF"); if (e != nullptr) return std::atoi(e) != 0; return strata::intel_gpu_gen(q->get_device()) == strata::IntelGpuGen::Alchemist; }(); return v; } // More, smaller work-groups for the multi-token read on Arc A-series: the sliced down kernel as (slice, row block) // = 480 groups instead of 80, its reduce with 8 loads in flight, and the up kernel with 8 columns per group (320 // groups instead of 160), where only group 0 writes the per-token lo values (every group computes the same ones). Every // output is the same float sum in the same order (gr_bench checksums unchanged). A770, gr_bench 1-6 tokens: // 117-180 -> 87-149 us; decode 28.04-28.54 -> 29.99-30.00 tok/s. Default on Alchemist only, where it was measured; // elsewhere the 80 / 160-group kernels as before. STRATA_GR_MORE_GROUPS=1 forces it on, =0 off. bool gr_more_groups(sycl::queue* q) { static const bool v = [q] { const char* e = std::getenv("STRATA_GR_MORE_GROUPS"); if (e != nullptr) return std::atoi(e) != 0; return strata::intel_gpu_gen(q->get_device()) == strata::IntelGpuGen::Alchemist; }(); return v; } bool gr_down_sliced() { // default (gr_bench: GR read 108.6 -> 76.5 us at 6 tokens); STRATA_GR_DOWN_SLICED=0: direct static const bool v = std::getenv("STRATA_GR_DOWN_SLICED") == nullptr || std::atoi(std::getenv("STRATA_GR_DOWN_SLICED")) != 0; return v; } float* slice_partials(sycl::queue* q) { static std::mutex mu; static std::unordered_map bufs; std::lock_guard lk(mu); auto it = bufs.find(q); if (it != bufs.end()) return it->second; float* p = sycl::malloc_device((size_t) GRS_SLICES * kFusedGrMaxT * GRS_ROWS, *q); bufs[q] = p; return p; } bool gr_down_direct() { // default; STRATA_GR_DOWN_DIRECT=0: the tiled kernel (gr_bench: 13-19% slower at 3-6 tokens) static const bool v = std::getenv("STRATA_GR_DOWN_DIRECT") == nullptr || std::atoi(std::getenv("STRATA_GR_DOWN_DIRECT")) != 0; return v; } constexpr int UPM_COLS = 16; // columns per block (x 4 streams = 64 rows, 8 per warp) constexpr int UPM_BLOCKS = N / UPM_COLS; // 160 constexpr int UPM_COLS_MORE = 8; // gr_more_groups: 8 columns (32 rows, 4 per warp) constexpr int UPM_BLOCKS_MORE = N / UPM_COLS_MORE; // 320 // `gr_up_kernel` for T tokens: each row of w_up read once; the T dots reduced by xor so every lane holds every // sum, and lane k runs token k's epilogue - the T epilogues in parallel instead of one after another. /* DPCT1110: The total declared local variable size in device function gr_up_multi_kernel exceeds 128 bytes and may cause high register pressure. Consult with your hardware vendor to find the total register size available and adjust the code, or use smaller sub-group size to avoid high register pressure. */ // COLS: columns per block (UPM_COLS, or UPM_COLS_MORE with LO_ONCE under gr_more_groups) template __dpct_inline__ void gr_up_multi_kernel(GrMulti m) { // stream sum unchanged, so the same bits; more blocks, fewer serial rows per warp auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); auto &lo = *sycl::ext::oneapi::group_local_memory_for_overwrite< float[kFusedGrMaxT][LR]>( sycl::ext::oneapi::this_work_item::get_work_group<3>()); auto &g = *sycl::ext::oneapi::group_local_memory_for_overwrite< float[kFusedGrMaxT][HC][COLS]>( sycl::ext::oneapi::this_work_item::get_work_group<3>()); const int t = item_ct1.get_local_id(2), lane = t & 31, warp = t >> 5; const int T = m.T; const int d0 = item_ct1.get_group(2) * COLS; #pragma unroll for (int i = t; i < T * LR; i += THREADS) { // the down kernel's partial sums, in split order, then silu const int k = i / LR, r = i % LR; if constexpr (REUSE) { if (m.cached_lo) { lo[k][r] = GR_PICK(m, k, lo)[r]; continue; } } float x = 0.0f; #pragma unroll for (int sp = 0; sp < DOWN_SPLIT; ++sp) if (sp < m.nsplit) x += m.part[((size_t) sp * kFusedGrMaxT + k) * LR + r]; x /= (float) HC; const float v = x / (1.0f + sycl::native::exp(-x)); lo[k][r] = v; if (!LO_ONCE || item_ct1.get_group(2) == 0) GR_PICK(m, k, lo)[r] = v; // the same values in every block } /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); // this lane's token (lane < T), picked once - not per row const int pk = lane < T ? lane : 0; const float* const pR = GR_PICK(m, pk, R); const float* const pWn = GR_PICK(m, pk, w_norm); const float* const pRs = GR_PICK(m, pk, rs); const bool pApply = GR_PICK(m, pk, apply); const float* const pBo = GR_PICK(m, pk, bo_prev); const float* const pIp = GR_PICK(m, pk, inj_prev); float* const pRout = GR_PICK(m, pk, R_out); for (int r = warp; r < HC * COLS; r += WARPS) { const int c = r / COLS, dd = r - c * COLS, i = c * N + d0 + dd; const sycl::uint4 *w4 = reinterpret_cast(m.a[0].w_up + (size_t)i * LR); /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ const sycl::uint4 wa = *(w4 + lane); /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ const sycl::uint4 wb = lane < LR / 8 - 32 ? *(w4 + 32 + lane) : sycl::uint4(0, 0, 0, 0); // the epilogue inputs of this lane's token, fetched while the dots run float rv = 0.0f, wn = 0.0f, rsc = 0.0f, bo = 0.0f, ip = 0.0f; bool apply = false; if (lane < T) { if (!REUSE || !m.cached_norm || pApply) rv = pR[i]; if (!REUSE || !m.cached_norm) { wn = pWn[i]; rsc = pRs[c]; } apply = pApply; if (apply) { bo = pBo[d0 + dd]; ip = pIp[c]; } } float mine = 0.0f; if constexpr (ILP) { // STRATA_GR_UP_ILP: the T dots first, then their butterflies interleaved step by step - float accs[kFusedGrMaxT]; // each value sees warp_sum's shuffles in warp_sum's order: the same bits #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) { float acc = 0.0f; if (k < T) { acc = dot8(wa, lo[k] + lane * 8); if (lane < LR / 8 - 32) acc += dot8(wb, lo[k] + (32 + lane) * 8); } accs[k] = acc; } #pragma unroll for (int o = 16; o > 0; o >>= 1) #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) if (k < T) accs[k] += dpct::experimental::permute_sub_group_by_xor( 0xffffffffu, sycl::ext::oneapi::this_work_item::get_sub_group(), accs[k], o); #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) if (k < T && lane == k) mine = accs[k]; } else { #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) { if (k >= T) break; float acc = dot8(wa, lo[k] + lane * 8); if (lane < LR / 8 - 32) acc += dot8(wb, lo[k] + (32 + lane) * 8); acc = warp_sum(acc); if (lane == k) mine = acc; } } if (lane < T) { if (apply) { rv = sycl::fma(bo, 2.0f * sigmoidf_(ip / (float)HC), rv); pRout[i] = rv; } const float x = REUSE && m.cached_norm ? m.xn[(size_t) lane * D + i] : rv * wn * rsc; g[lane][c][dd] = x * sigmoidf_(mine); } } /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); for (int i = t; i < T * COLS; i += THREADS) { const int k = i / COLS, col = i - k * COLS; float s = 0.0f; #pragma unroll for (int c = 0; c < HC; ++c) s += g[k][c][col]; GR_PICK(m, k, mixed)[d0 + col] = s / (float) HC; } } // Consume the normalized product without changing its multiply order. template __dpct_inline__ void gr_up_cached_norm_kernel(GrMulti m) { auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); auto &lo = *sycl::ext::oneapi::group_local_memory_for_overwrite< float[kFusedGrMaxT][LR]>( sycl::ext::oneapi::this_work_item::get_work_group<3>()); auto &g = *sycl::ext::oneapi::group_local_memory_for_overwrite< float[kFusedGrMaxT][HC][UPM_COLS]>( sycl::ext::oneapi::this_work_item::get_work_group<3>()); const int t = item_ct1.get_local_id(2), lane = t & 31, warp = t >> 5; const int T = m.T; const int d0 = item_ct1.get_group(2) * UPM_COLS; #pragma unroll for (int i = t; i < T * LR; i += THREADS) { // the down kernel's partial sums, in split order, then silu const int k = i / LR, r = i % LR; if constexpr (LoReady) { lo[k][r] = m.a[k].lo[r]; } else { float x = 0.0f; #pragma unroll for (int sp = 0; sp < DOWN_SPLIT; ++sp) if (sp < m.nsplit) x += m.part[((size_t) sp * kFusedGrMaxT + k) * LR + r]; x /= (float) HC; const float v = x / (1.0f + sycl::native::exp(-x)); lo[k][r] = v; m.a[k].lo[r] = v; } } /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); for (int r = warp; r < HC * UPM_COLS; r += WARPS) { const int c = r / UPM_COLS, dd = r - c * UPM_COLS, i = c * N + d0 + dd; const sycl::uint4 *w4 = reinterpret_cast(m.a[0].w_up + (size_t)i * LR); /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ const sycl::uint4 wa = *(w4 + lane); /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ const sycl::uint4 wb = lane < LR / 8 - 32 ? *(w4 + 32 + lane) : sycl::uint4(0, 0, 0, 0); float normalized=0.f; if(lane= T) break; float acc = dot8(wa, lo[k] + lane * 8); if (lane < LR / 8 - 32) acc += dot8(wb, lo[k] + (32 + lane) * 8); acc = warp_sum(acc); if (lane == k) mine = acc; } if(lane(); auto &lo = *sycl::ext::oneapi::group_local_memory_for_overwrite< float[kFusedGrMaxT][LR]>( sycl::ext::oneapi::this_work_item::get_work_group<3>()); auto &g = *sycl::ext::oneapi::group_local_memory_for_overwrite< float[kFusedGrMaxT][HC][UPM_COLS]>( sycl::ext::oneapi::this_work_item::get_work_group<3>()); const int t = item_ct1.get_local_id(2), lane = t & 31, warp = t >> 5; const int T = m.T; const int d0 = item_ct1.get_group(2) * UPM_COLS; #pragma unroll for (int i = t; i < T * LR; i += THREADS) { // the down kernel's partial sums, in split order, then silu const int k = i / LR, r = i % LR; lo[k][r]=m.lo[k][r]; } /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); for (int r = warp; r < HC * UPM_COLS; r += WARPS) { const int c = r / UPM_COLS, dd = r - c * UPM_COLS, i = c * N + d0 + dd; const sycl::uint4 *w4 = reinterpret_cast(m.w_up + (size_t)i * LR); /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ const sycl::uint4 wa = *(w4 + lane); /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ const sycl::uint4 wb = lane < LR / 8 - 32 ? *(w4 + 32 + lane) : sycl::uint4(0, 0, 0, 0); float normalized=0.f; if(lane= T) break; float acc = dot8(wa, lo[k] + lane * 8); if (lane < LR / 8 - 32) acc += dot8(wb, lo[k] + (32 + lane) * 8); acc = warp_sum(acc); if (lane == k) mine = acc; } if(laneparallel_for>( sycl::nd_range<3>(sycl::range(1, 1, UPM_BLOCKS) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), properties, [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_up_multi_kernel(m); }); } // gr_more_groups launches (BIG: large GRF, as gr_large_grf) template void launch_gr_down_more(dpct::queue_ptr st, const GrMulti& m) { strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler& cgh) { sycl::local_accessor tl(sycl::range<1>((size_t) kFusedGrMaxT * GRS_COLS), cgh); const sycl::nd_range<3> nr(sycl::range(1, 1, GRS_SLICES * GRS_RB) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)); auto body = [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_down_sliced_kernel(m, tl.get_multi_ptr().get()); }; if constexpr (BIG) cgh.parallel_for>>( nr, sycl::ext::oneapi::experimental::properties{sycl::ext::intel::experimental::grf_size<256>}, body); else cgh.parallel_for>>(nr, body); }); const unsigned nred = unsigned((m.T * GRS_ROWS + 127) / 128); strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>>( sycl::nd_range<3>(sycl::range(1, 1, nred) * sycl::range(1, 1, 128), sycl::range(1, 1, 128)), [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_down_reduce_kernel(m); }); } template void launch_gr_up_more(dpct::queue_ptr st, const GrMulti& m) { const sycl::nd_range<3> nr(sycl::range(1, 1, UPM_BLOCKS_MORE) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)); auto body = [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_up_multi_kernel(m); }; if constexpr (BIG) strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>>( nr, sycl::ext::oneapi::experimental::properties{sycl::ext::oneapi::experimental::use_root_sync, sycl::ext::intel::experimental::grf_size<256>}, body); else strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>>( nr, sycl::ext::oneapi::experimental::properties{sycl::ext::oneapi::experimental::use_root_sync}, body); } // SYCL port: the up step with COLS columns per block. 16 (160 blocks, 8 rows per warp one after another) was // latency-bound on Xe2 (B70: 23.7 us for 6.55 MB of BF16, 46% of the bandwidth). STRATA_GR_UP_COLS=8 / 4 run 320 / // 640 blocks with 4 / 2 rows per warp; every row's dot and butterfly and every column's stream sum are the same. inline int gr_up_cols() { static const int v = [] { const char* e = std::getenv("STRATA_GR_UP_COLS"); const int c = e ? std::atoi(e) : UPM_COLS; return c == 4 || c == 8 || c == 32 || c == 64 ? c : UPM_COLS; }(); return v; } inline bool gr_up_ilp() { static const bool v = [] { const char* e = std::getenv("STRATA_GR_UP_ILP"); return e && e[0] == '1'; }(); return v; } template void launch_gr_up_cols(dpct::queue_ptr st, const GrMulti& m, bool grf) { auto exp_props = sycl::ext::oneapi::experimental::properties{sycl::ext::oneapi::experimental::use_root_sync}; const sycl::nd_range<3> r(sycl::range(1, 1, N / C) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)); if constexpr (C == UPM_COLS) { if (m.cached_norm || m.cached_lo) { st->parallel_for>(r, exp_props, [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_up_multi_kernel(m); }); return; } } if (grf) { // Arc A-series: large-GRF mode (256 regs/thread); at 128 this kernel spills ~3.4 KB/thread on the A770 strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>>(r, sycl::ext::oneapi::experimental::properties{sycl::ext::oneapi::experimental::use_root_sync, sycl::ext::intel::experimental::grf_size<256>}, [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_up_multi_kernel(m); }); return; } if (gr_up_ilp()) strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>>(r, exp_props, [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_up_multi_kernel(m); }); else strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>>(r, exp_props, [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_up_multi_kernel(m); }); } inline void launch_gr_up_multi(dpct::queue_ptr st, const GrMulti& m, bool grf = false) { switch (gr_up_cols()) { case 4: launch_gr_up_cols<4>(st, m, grf); return; case 8: launch_gr_up_cols<8>(st, m, grf); return; case 32: launch_gr_up_cols<32>(st, m, grf); return; case 64: launch_gr_up_cols<64>(st, m, grf); return; default: launch_gr_up_cols(st, m, grf); return; } } // ================================ hc read v3 (opt-in: STRATA_GR_V3=1) - two kernels, stream-split ================ // The norm kernel runs on only T blocks (~16 us of pure latency per call) and `down` on 41 blocks (~280 GB/s). // v3: `down` is split over (row group, stream[, column half]) = 164 or 328 blocks; each block stages its slice of // R' * w_norm for the T tokens, reduces that slice's sum of squares itself, and writes UNSCALED partial dots. The // rms scale is per stream, so w_down . xn = sum_c rs[c] * (w_down[:, c] . (R'[c] * w_norm[c])) - `up` applies it // in its prologue (lo, inject, rs). Same maths, ANOTHER SUMMATION ORDER: not bitwise the default kernels, hence // opt-in. Dynamic shared memory is T * (N / S) floats: S (1 or 2 column halves) is the smallest that fits the // card's opt-in limit at kFusedGrMaxT tokens (Ampere 99 KB: S = 1; Turing / HIP 64 KB: S = 2); a card where // neither fits keeps the default kernels. constexpr int PR = LR + HC; // partial rows per (token, stream): 320 down + 4 inject constexpr int TQ3 = N / 8 / 32; // uint4 weight chunks per lane in one stream's slice (10) template /* DPCT1110: The total declared local variable size in device function gr_down_v3_kernel exceeds 128 bytes and may cause high register pressure. Consult with your hardware vendor to find the total register size available and adjust the code, or use smaller sub-group size to avoid high register pressure. */ __dpct_inline__ void gr_down_v3_kernel(GrMulti m, float *__restrict__ part, float *__restrict__ ssg, uint8_t *dpct_local) { auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); auto xs = (float *)dpct_local; // [T][N / S] constexpr int R2 = 1; // down rows per warp auto &red = *sycl::ext::oneapi::group_local_memory_for_overwrite< float[WARPS][kFusedGrMaxT]>( sycl::ext::oneapi::this_work_item::get_work_group<3>()); const int t = item_ct1.get_local_id(2), lane = t & 31, warp = t >> 5; const int T = m.T; // S = 2: each stream's 2560 columns in two halves (blockIdx.y = stream * S + half): twice the blocks constexpr int SL = N / S, TQS = SL / 8 / 32; const int rg = item_ct1.get_group(2), c = item_ct1.get_group(1) / S, h = item_ct1.get_group(1) - (item_ct1.get_group(1) / S) * S; constexpr int NDB = LR / (WARPS * R2); // down row blocks per stream; block NDB = the inject rows const bool inject_block = rg == NDB; // warp w owns rows row0 + w * R2 + r (r < R2); the inject block: warps 0-3, one row each const int row0 = inject_block ? warp : (rg * WARPS + warp) * R2; const int nrows = inject_block ? ((m.a[0].w_inject != nullptr && warp < HC) ? 1 : 0) : R2; const bool active = nrows > 0; const uint16_t* wbase = inject_block ? m.a[0].w_inject : m.a[0].w_down; sycl::uint4 wv[R2][TQS]; #pragma unroll for (int r = 0; r < R2; ++r) { if (r >= nrows) break; const sycl::uint4 *w4 = reinterpret_cast( wbase + (size_t)(row0 + r) * D + (size_t)c * N + (size_t)h * SL); #pragma unroll /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ for (int q = 0; q < TQS; ++q) wv[r][q] = *(w4 + lane + 32 * q); } float ssp[kFusedGrMaxT]; #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) { ssp[k] = 0.0f; if (k >= T) continue; const FusedGrArgs& a = m.a[k]; const float gw = a.apply ? 2.0f * sigmoidf_(a.inj_prev[c] / (float) HC) : 0.0f; const sycl::float4 *R4 = reinterpret_cast( a.R + (size_t)c * N + (size_t)h * SL); const sycl::float4 *G4 = reinterpret_cast( a.w_norm + (size_t)c * N + (size_t)h * SL); const sycl::float4 *B4 = reinterpret_cast(a.bo_prev + (size_t)h * SL); sycl::float4 *X4 = reinterpret_cast(xs + (size_t)k * SL); for (int i = t; i < SL / 4; i += THREADS) { sycl::float4 r = R4[i]; if (a.apply) { const sycl::float4 b = B4[i]; r.x() = sycl::fma((float)(b.x()), (float)gw, r.x()); r.y() = sycl::fma((float)(b.y()), (float)gw, r.y()); r.z() = sycl::fma((float)(b.z()), (float)gw, r.z()); r.w() = sycl::fma((float)(b.w()), (float)gw, r.w()); } const sycl::float4 g = G4[i]; ssp[k] += r.x() * r.x() + r.y() * r.y() + r.z() * r.z() + r.w() * r.w(); X4[i] = sycl::float4(r.x() * g.x(), r.y() * g.y(), r.z() * g.z(), r.w() * g.w()); } } #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) { if (k >= T) break; const float v = warp_sum(ssp[k]); if (lane == 0) red[warp][k] = v; } /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); if (rg == 0 && t < T) { float sum = 0.0f; #pragma unroll for (int w = 0; w < WARPS; ++w) sum += red[w][t]; ssg[(t * HC + c) * S + h] = sum; } if (!active) return; #pragma unroll for (int r = 0; r < R2; ++r) { if (r >= nrows) break; float acc[kFusedGrMaxT]; #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) acc[k] = 0.0f; #pragma unroll for (int q = 0; q < TQS; ++q) { const int j = lane + 32 * q; #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) if (k < T) acc[k] += dot8(wv[r][q], xs + (size_t) k * SL + j * 8); } const int prow = inject_block ? LR + warp : row0 + r; #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) { if (k >= T) break; const float v = warp_sum(acc[k]); if (lane == 0) part[(((size_t) k * HC + c) * S + h) * PR + prow] = v; } } } template /* DPCT1110: The total declared local variable size in device function gr_up_v3_kernel exceeds 128 bytes and may cause high register pressure. Consult with your hardware vendor to find the total register size available and adjust the code, or use smaller sub-group size to avoid high register pressure. */ __dpct_inline__ void gr_up_v3_kernel(GrMulti m, const float *__restrict__ part, const float *__restrict__ ssg) { auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); auto &lo = *sycl::ext::oneapi::group_local_memory_for_overwrite< float[kFusedGrMaxT][LR]>( sycl::ext::oneapi::this_work_item::get_work_group<3>()); auto &rsS = *sycl::ext::oneapi::group_local_memory_for_overwrite< float[kFusedGrMaxT][HC]>( sycl::ext::oneapi::this_work_item::get_work_group<3>()); auto &g = *sycl::ext::oneapi::group_local_memory_for_overwrite< float[kFusedGrMaxT][HC][UPM_COLS]>( sycl::ext::oneapi::this_work_item::get_work_group<3>()); const int t = item_ct1.get_local_id(2), lane = t & 31, warp = t >> 5; const int T = m.T; const int d0 = item_ct1.get_group(2) * UPM_COLS; constexpr int RPW = HC * UPM_COLS / WARPS; // 8 rows per warp if (t < T * HC) { const int k = t / HC, c = t - k * HC; float ss = 0.0f; #pragma unroll for (int h = 0; h < S; ++h) ss += ssg[t * S + h]; const float r = sycl::rsqrt(ss / (float)N + m.a[k].eps); rsS[k][c] = r; if (item_ct1.get_group(2) == 0) m.a[k].rs[c] = r; } /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); for (int i = t; i < T * LR; i += THREADS) { const int k = i / LR, r = i - k * LR; float sum = 0.0f; #pragma unroll for (int c = 0; c < HC; ++c) { float p = 0.0f; #pragma unroll for (int h = 0; h < S; ++h) p += part[(((size_t) k * HC + c) * S + h) * PR + r]; sum = sycl::fma(rsS[k][c], p, sum); } const float x = sum / (float) HC; lo[k][r] = x / (1.0f + sycl::native::exp(-x)); } if (item_ct1.get_group(2) == 0 && t < T * HC) { const int k = t / HC, cc = t - k * HC; if (m.a[k].w_inject != nullptr) { float sum = 0.0f; #pragma unroll for (int c = 0; c < HC; ++c) { float p = 0.0f; #pragma unroll for (int h = 0; h < S; ++h) p += part[(((size_t) k * HC + c) * S + h) * PR + LR + cc]; sum = sycl::fma(rsS[k][c], p, sum); } m.a[k].inject_out[cc] = sum; } } /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); #pragma unroll for (int q = 0; q < RPW; ++q) { const int r = warp + q * WARPS; const int c = r / UPM_COLS, dd = r - c * UPM_COLS, i = c * N + d0 + dd; const sycl::uint4 *w4 = reinterpret_cast(m.a[0].w_up + (size_t)i * LR); /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ const sycl::uint4 wa = *(w4 + lane); /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ const sycl::uint4 wb = lane < LR / 8 - 32 ? *(w4 + 32 + lane) : sycl::uint4(0, 0, 0, 0); float rv = 0.0f, wn = 0.0f, bo = 0.0f, ip = 0.0f; bool apply = false; if (lane < T) { const FusedGrArgs& a = m.a[lane]; rv = a.R[i]; wn = a.w_norm[i]; apply = a.apply; if (apply) { bo = a.bo_prev[d0 + dd]; ip = a.inj_prev[c]; } } float mine = 0.0f; #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) { if (k >= T) break; float acc = dot8(wa, lo[k] + lane * 8); if (lane < LR / 8 - 32) acc += dot8(wb, lo[k] + (32 + lane) * 8); acc = warp_sum(acc); if (lane == k) mine = acc; } if (lane < T) { if (apply) { rv = sycl::fma(bo, 2.0f * sigmoidf_(ip / (float)HC), rv); m.a[lane].R_out[i] = rv; } const float x = rv * wn * rsS[lane][c]; g[lane][c][dd] = x * sigmoidf_(mine); } } /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); for (int i = t; i < T * UPM_COLS; i += THREADS) { const int k = i / UPM_COLS, col = i - k * UPM_COLS; float sum = 0.0f; #pragma unroll for (int c = 0; c < HC; ++c) sum += g[k][c][col]; m.a[k].mixed[d0 + col] = sum / (float) HC; } } // ================================ #315: split / staged - the same arithmetic, split differently ================== // The multi read's variants beside the plain read (0.1.31's default; not main's opt-in STRATA_GR_V3 read, which sums // in another order). Every variant computes each output with exactly the plain read's operations in its order (so // bitwise the plain read and the single-token read); only who does what, and when, changes. `fused_gr_check` // compares them on the card before a verify window uses them (STRATA_HC_SPLIT below). // - the norm (split and staged): one block per token AND stream instead of one per token. Each thread visits // exactly the elements, in the order, it visited for that stream in the plain read, so every sum of squares and rs // is the same; then it recomputes its R' * w_norm and writes it times rs (the plain read stored the product and // scaled it in place: the same two roundings). On an RTX 4080 SUPER, the plain read's norm took 0.58 ms per // 4-token round (96 launches of 1-4 blocks), this one 0.23 ms. // - the down projection, split: the plain read's kernel. // - the down projection, staged: the plain read's 8 rows per block and lane order, but the activations arrive by // cp.async in half-stream tiles, two in flight, so no thread waits on a chain of loads, and they sit in shared // memory as two planes (floats 0-3 and 4-7 of every 8), so a warp's reads hit no bank twice. The next tile's // weights are loaded while the current one is used. A tile is 160 = 5 x 32 chunks of 8, so a lane still // accumulates its chunks lane + 32 q in ascending order, as in the plain read with either tile. Before sm_80 // (and on HIP) the staging is a plain copy: the same bits, only not asynchronous. constexpr int kHcPlain = 1, kHcSplit = 2, kHcStaged = 3; constexpr int H_TILE = N / 2; // staged tile: half a stream = 160 chunks of 8, 5 per lane (1280 for Flash-Next) constexpr int HQ = H_TILE / 8 / 32; constexpr int N_HTILES = D / H_TILE; // 8 static_assert(N % H_TILE == 0 && N % 512 == 0, "a staged tile never straddles two streams"); static_assert(H_TILE % 256 == 0, "a staged tile holds whole rounds of 32 chunks: the plain read's lane order"); /* DPCT1110: The total declared local variable size in device function gr_norm_split_kernel exceeds 128 bytes and may cause high register pressure. Consult with your hardware vendor to find the total register size available and adjust the code, or use smaller sub-group size to avoid high register pressure. */ __dpct_inline__ void gr_norm_split_kernel(GrMulti m) { auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); auto &part = *sycl::ext::oneapi::group_local_memory_for_overwrite( sycl::ext::oneapi::this_work_item::get_work_group<3>()); auto &s_rs = *sycl::ext::oneapi::group_local_memory_for_overwrite( sycl::ext::oneapi::this_work_item::get_work_group<3>()); const FusedGrArgs &a = m.a[item_ct1.get_group(2)]; const int c = item_ct1.get_group(1); float *xn = m.xn + (size_t)item_ct1.get_group(2) * D; const int t = item_ct1.get_local_id(2), lane = t & 31, warp = t >> 5; const float gw = a.apply ? 2.0f * sigmoidf_(a.inj_prev[c] / (float) HC) : 0.0f; float ss = 0.0f; for (int i = t * 4; i < D; i += THREADS * 4) { if (i / N != c) continue; // another stream's element: another block's const int d = i - c * N; sycl::float4 r = *reinterpret_cast(a.R + i); if (a.apply) { const sycl::float4 b = *reinterpret_cast(a.bo_prev + d); r.x() = sycl::fma((float)(b.x()), (float)gw, r.x()); r.y() = sycl::fma((float)(b.y()), (float)gw, r.y()); r.z() = sycl::fma((float)(b.z()), (float)gw, r.z()); r.w() = sycl::fma((float)(b.w()), (float)gw, r.w()); } const float sq = r.x() * r.x() + r.y() * r.y() + r.z() * r.z() + r.w() * r.w(); ss += sq; } const float v = warp_sum(ss); if (lane == 0) part[warp] = v; /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); if (t == 0) { float s = 0.0f; #pragma unroll for (int w = 0; w < WARPS; ++w) s += part[w]; s_rs = sycl::rsqrt(s / (float)N + a.eps); a.rs[c] = s_rs; } /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); const float rs = s_rs; for (int i = t * 4; i < D; i += THREADS * 4) { if (i / N != c) continue; const int d = i - c * N; sycl::float4 r = *reinterpret_cast(a.R + i); if (a.apply) { const sycl::float4 b = *reinterpret_cast(a.bo_prev + d); r.x() = sycl::fma((float)(b.x()), (float)gw, r.x()); r.y() = sycl::fma((float)(b.y()), (float)gw, r.y()); r.z() = sycl::fma((float)(b.z()), (float)gw, r.z()); r.w() = sycl::fma((float)(b.w()), (float)gw, r.w()); } const sycl::float4 g = *reinterpret_cast(a.w_norm + i); const float px = r.x() * g.x(), py = r.y() * g.y(), pz = r.z() * g.z(), pw = r.w() * g.w(); *reinterpret_cast(xn + i) = sycl::float4(px * rs, py * rs, pz * rs, pw * rs); } } #if defined(DPCT_COMPATIBILITY_TEMP) && DPCT_COMPATIBILITY_TEMP >= 800 && \ !defined(__HIPCC__) #define STRATA_GR_CP_ASYNC 1 #endif __dpct_inline__ void cp_async16(void *smem, const void *gmem) { #if defined(STRATA_GR_CP_ASYNC) auto sa = smem; #if defined(__SYCL_DEVICE_ONLY__) && defined(__NVPTX__) asm volatile("cp.async.cg.shared.global [%0], [%1], 16;\n" ::"r"(sa), "l"(gmem) : "memory"); #else *(((uint32_t *)(uintptr_t)sa)) = *(((uint32_t *)(uintptr_t)gmem)); if (16 > 4) *(((uint32_t *)(uintptr_t)sa) + 1) = *(((uint32_t *)(uintptr_t)gmem) + 1); if (16 > 8) *(((uint32_t *)(uintptr_t)sa) + 2) = *(((uint32_t *)(uintptr_t)gmem) + 2); if (16 > 12) *(((uint32_t *)(uintptr_t)sa) + 3) = *(((uint32_t *)(uintptr_t)gmem) + 3); #endif #else *reinterpret_cast(smem) = *reinterpret_cast(gmem); #endif } __dpct_inline__ void cp_async_commit() { #if defined(STRATA_GR_CP_ASYNC) #if defined(__SYCL_DEVICE_ONLY__) && defined(__NVPTX__) asm volatile("cp.async.commit_group;\n" ::: "memory"); #else #endif #endif } __dpct_inline__ void cp_async_wait1() { #if defined(STRATA_GR_CP_ASYNC) #if defined(__SYCL_DEVICE_ONLY__) && defined(__NVPTX__) asm volatile("cp.async.wait_group 1;\n" ::: "memory"); #else #endif #endif } __dpct_inline__ void cp_async_wait0() { #if defined(STRATA_GR_CP_ASYNC) #if defined(__SYCL_DEVICE_ONLY__) && defined(__NVPTX__) asm volatile("cp.async.wait_group 0;\n" ::: "memory"); #else #endif #endif } // `dot8` with its 8 activations as two float4: the same eight fmaf in the same order __dpct_inline__ float dot8v(const sycl::uint4 w, const sycl::float4 x0, const sycl::float4 x1) { float acc = 0.0f; acc = sycl::fma(sycl::bit_cast(w.x() << 16), (float)(x0.x()), acc); acc = sycl::fma(sycl::bit_cast(w.x() & 0xffff0000u), (float)(x0.y()), acc); acc = sycl::fma(sycl::bit_cast(w.y() << 16), (float)(x0.z()), acc); acc = sycl::fma(sycl::bit_cast(w.y() & 0xffff0000u), (float)(x0.w()), acc); acc = sycl::fma(sycl::bit_cast(w.z() << 16), (float)(x1.x()), acc); acc = sycl::fma(sycl::bit_cast(w.z() & 0xffff0000u), (float)(x1.y()), acc); acc = sycl::fma(sycl::bit_cast(w.w() << 16), (float)(x1.z()), acc); acc = sycl::fma(sycl::bit_cast(w.w() & 0xffff0000u), (float)(x1.w()), acc); return acc; } // Stage tile `h` of every token into `buf`: [T][2 planes][160 chunks] float4, plane 0 = floats 0-3 of a chunk. __dpct_inline__ void stage_htile(const GrMulti &m, int T, int h, sycl::float4 *buf, int t) { for (int i = t; i < T * (H_TILE / 4); i += THREADS) { const int k = i / (H_TILE / 4), s4 = i - k * (H_TILE / 4); // s4: float4 of the tile, chunk s4/2, half s4&1 const float* src = m.xn + (size_t) k * D + (size_t) h * H_TILE + (size_t) s4 * 4; cp_async16(buf + (size_t) k * (H_TILE / 4) + (s4 & 1) * (H_TILE / 8) + (s4 >> 1), src); } } template /* DPCT1110: The total declared local variable size in device function gr_down_staged_kernel exceeds 128 bytes and may cause high register pressure. Consult with your hardware vendor to find the total register size available and adjust the code, or use smaller sub-group size to avoid high register pressure. */ __dpct_inline__ void gr_down_staged_kernel(GrMulti m, uint8_t *dpct_local) { auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); auto hbuf = (sycl::float4 *)dpct_local; // 2 buffers x [T][2][160] float4 const int t = item_ct1.get_local_id(2), lane = t & 31, warp = t >> 5; const int T = m.T; const bool inject_block = item_ct1.get_group(2) == DOWN_BLOCKS; const int row = inject_block ? warp : item_ct1.get_group(2) * WARPS + warp; const bool active = !(inject_block && (m.a[0].w_inject == nullptr || warp >= HC)); const uint16_t* wrow = (inject_block ? m.a[0].w_inject : m.a[0].w_down) + (size_t) (active ? row : 0) * D; const sycl::uint4 *w4 = reinterpret_cast(wrow); const size_t buf_f4 = (size_t) T * (H_TILE / 4); // float4 per buffer (the second one follows the first) float acc[MAX_T]; #pragma unroll for (int k = 0; k < MAX_T; ++k) acc[k] = 0.0f; sycl::uint4 wv[HQ], wnext[HQ]; if (active) { #pragma unroll /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ for (int q = 0; q < HQ; ++q) wv[q] = *(w4 + lane + 32 * q); } stage_htile(m, T, 0, hbuf, t); cp_async_commit(); stage_htile(m, T, 1, hbuf + buf_f4, t); cp_async_commit(); #pragma unroll 1 for (int h = 0; h < N_HTILES; ++h) { if (active && h + 1 < N_HTILES) { #pragma unroll /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ for (int q = 0; q < HQ; ++q) wnext[q] = *(w4 + (h + 1) * (H_TILE / 8) + lane + 32 * q); } if (h + 1 < N_HTILES) cp_async_wait1(); // tile h has landed (h + 1 may still be on its way) else cp_async_wait0(); /* DPCT1118: SYCL group functions and algorithms must be encountered in converged control flow. You may need to adjust the code. */ /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); const sycl::float4 *cur = hbuf + (h & 1) * buf_f4; if (active) { #pragma unroll for (int q = 0; q < HQ; ++q) { const int j = lane + 32 * q; #pragma unroll for (int k = 0; k < MAX_T; ++k) { if (k < T) { const sycl::float4 *pk = cur + (size_t)k * (H_TILE / 4); acc[k] += dot8v(wv[q], pk[j], pk[H_TILE / 8 + j]); } } } } /* DPCT1118: SYCL group functions and algorithms must be encountered in converged control flow. You may need to adjust the code. */ /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); // every warp is done with buffer h & 1 if (h + 2 < N_HTILES) stage_htile(m, T, h + 2, hbuf + (h & 1) * buf_f4, t); cp_async_commit(); // an empty group at the end keeps the wait counts simple if (h + 1 < N_HTILES) { #pragma unroll for (int q = 0; q < HQ; ++q) wv[q] = wnext[q]; } } if (!active) return; float s[MAX_T]; #pragma unroll for (int k = 0; k < MAX_T; ++k) s[k] = k < T ? warp_sum(acc[k]) : 0.0f; // lane k writes token k (every lane holds every sum after the xor reduction) #pragma unroll for (int k = 0; k < MAX_T; ++k) { if (k >= T || lane != k) continue; if (inject_block) { m.a[k].inject_out[row] = s[k]; } else { const float x = s[k] / (float) HC; m.a[k].lo[row] = x / (1.0f + sycl::native::exp(-x)); } } } // The tokens a down kernel may carry in one launch on the current card, and the plain read's tile: the plain read // stages n_tok * TILEV floats (1280 on sm_75, 2560 elsewhere), staged two tiles of n_tok * H_TILE. The shared-memory // opt-in is set here once per device (a per-DEVICE setting: a layer split runs these kernels on two cards). int down_chunk(bool staged, int* tile_out) { static bool attr[64] = {}; static int chunk[64] = {}; // tokens the plain down kernel may carry in one launch on this card static int chunk_staged[64] = {}; // the same for staged static int tile[64] = {}; // the plain down kernel's TILEV on this card (1280 on sm_75, 2560 elsewhere) int dev = 0; dev = dpct::get_current_device_id(); if (dev < 0 || dev >= 64) { *tile_out = N; return kFusedGrMaxT; } if (!attr[dev]) { int optin = 0; /* DPCT1019: local_mem_size in SYCL is not a complete equivalent of cudaDevAttrMaxSharedMemoryPerBlockOptin in CUDA. You may need to adjust the code. */ optin = dpct::get_device(dev).get_local_mem_size(); optin = strata::smem_optin_of(optin); // STRATA_EMULATE_CC (tests only) // the down kernel stages n_tok*TILEV floats of dynamic shared memory - 80 KB at the full 8 tokens of the // CUDA tile. sm_75 gets the smaller tile: all eight tokens fit one 40 KiB launch there (no more slicing), // the TQ-5 prefetch holds half the registers, and the smaller blocks raise how many of the 41-block grid // share an SM. Cards whose opt-in is still below that (or that report no opt-in at all) slice the tokens; // the down kernel's outputs (lo, inject_out) are strictly per-token, so the chunk boundaries are safe, and // the up kernel below still sees every token of the batch in one launch. #if defined(__HIPCC__) const bool small_tile = true; // all eight tokens fit gfx1100's 64 KiB LDS at this tile #else int cc_maj = 0, cc_min = 0; cc_maj = dpct::get_device(dev).get_major_version(); cc_min = dpct::get_device(dev).get_minor_version(); const bool small_tile = strata::cc_major_of(cc_maj) * 10 + strata::cc_minor_of(cc_min) == 75; #endif const int tv = small_tile ? (N / 2) : N; tile[dev] = tv; int want = (int) (kFusedGrMaxT * tv * sizeof(float)); if (optin > 0 && want > optin) want = optin; if (small_tile) { /* DPCT1026: The call to cudaFuncSetAttribute was removed because SYCL currently does not support corresponding setting. */ } else { /* DPCT1026: The call to cudaFuncSetAttribute was removed because SYCL currently does not support corresponding setting. */ } int want_staged = (int) (2 * kFusedGrMaxT * H_TILE * sizeof(float)); if (optin > 0 && want_staged > optin) want_staged = optin; /* DPCT1026: The call to cudaFuncSetAttribute was removed because SYCL currently does not support corresponding setting. */ /* DPCT1026: The call to cudaGetLastError was removed because this functionality is redundant in SYCL. */ ; // drop any error the attempt left behind // The opt-in is a promise a pre-Volta card does not keep: an sm_60 answers // 65536 and accepts the cudaFuncSetAttribute for 61440 B, then fails the // LAUNCH with "invalid argument". What such a card will launch is its // per-block limit, so the capacity comes from that below sm_70 - the same // tokens the "no opt-in" branch assumes, but taken from the attribute that is // actually enforced. #if defined(__HIPCC__) const int usable = optin > 0 ? optin : 48 * 1024; #else int cc = 0, per_block = 0; cc = dpct::get_device(dev).get_major_version(); per_block = (int) dpct::get_device(dev).get_local_mem_size(); // SYCL: the work-group local memory cc = strata::cc_major_of(cc); const int usable = (cc >= 7 && optin > 0) ? optin : per_block; #endif const int capacity = usable / (int) (tv * sizeof(float)); chunk[dev] = capacity < 1 ? 1 : (capacity > kFusedGrMaxT ? kFusedGrMaxT : capacity); const int capacity_staged = usable / (int) (2 * H_TILE * sizeof(float)); chunk_staged[dev] = capacity_staged < 1 ? 1 : (capacity_staged > kFusedGrMaxT ? kFusedGrMaxT : capacity_staged); attr[dev] = true; } *tile_out = tile[dev] ? tile[dev] : N; return staged ? chunk_staged[dev] : chunk[dev]; } // The multi read as `variant` (kHcPlain, kHcSplit or kHcStaged): the norm, the down projection in launches of as // many tokens as fit the card, the up projection; the profile's stamps after the norm and after the down projection. // kHcPlain is the default read exactly as before (never main's opt-in STRATA_GR_V3 path, which fused_gr_read_multi // takes first). void launch_multi(const GrMulti &m, int variant, dpct::queue_ptr st, unsigned long long *stamp_buf, int stamp_i0) { const int n_tok = m.T; if (variant >= kHcSplit) { auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>( sycl::nd_range<3>(sycl::range(1, HC, (unsigned)n_tok) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { gr_norm_split_kernel(m); }); } else { auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>( sycl::nd_range<3>(sycl::range(1, 1, n_tok) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { gr_norm_multi_kernel(m); }); } if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0, (void*) st); const bool staged = variant >= kHcStaged; int tv = N; const int chunk_tok = down_chunk(staged, &tv); /* DPCT1083: The size of local memory in the migrated code may be different from the original code. Check that the allocated memory size in the migrated code is correct. */ const size_t per_tok = (staged ? (size_t)2 * H_TILE : (size_t)tv) * sizeof(float); for (int c0 = 0; c0 < n_tok; c0 += chunk_tok) { const int ct = n_tok - c0 < chunk_tok ? n_tok - c0 : chunk_tok; GrMulti c{}; if (ct == n_tok) { c = m; } else { c.xn = m.xn + (size_t) c0 * D; c.T = ct; for (int k = 0; k < ct; ++k) c.a[k] = m.a[c0 + k]; } const size_t smem = (size_t) ct * per_tok; // #443, opt-in STRATA_GR_DOWN_MAX4=1: launches of up to 4 tokens hold 4 tokens' sums per thread instead of // kFusedGrMaxT - the same tile, block size, accumulation order and plain/split/staged path, so the same bits // (read once: this runs per layer when decode is not captured) static const bool max4_on = [] { const char* v = std::getenv("STRATA_GR_DOWN_MAX4"); return v && std::atoi(v) != 0; }(); const bool max4 = max4_on && ct <= 4; if (staged) { if (max4) { auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) { sycl::local_accessor dpct_local_acc_ct1( sycl::range(smem), cgh); cgh.parallel_for< dpct_kernel_name>>( sycl::nd_range<3>(sycl::range(1, 1, DOWN_BLOCKS + 1) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { gr_down_staged_kernel<4>( c, dpct_local_acc_ct1 .get_multi_ptr< sycl::access::decorated::no>() .get()); }); }); } else { auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) { sycl::local_accessor dpct_local_acc_ct1( sycl::range(smem), cgh); /* DPCT1050: The template argument of the dpct_kernel_name could not be deduced. You need to update this code. */ cgh.parallel_for>( sycl::nd_range<3>(sycl::range(1, 1, DOWN_BLOCKS + 1) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { gr_down_staged_kernel( c, dpct_local_acc_ct1 .get_multi_ptr< sycl::access::decorated::no>() .get()); }); }); } } else if (tv == (N / 2)) { if (max4) { auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) { sycl::local_accessor dpct_local_acc_ct1( sycl::range(smem), cgh); cgh.parallel_for, dpct_kernel_scalar<4>>>( sycl::nd_range<3>(sycl::range(1, 1, DOWN_BLOCKS + 1) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { gr_down_multi_kernel<(N / 2), 4>( c, dpct_local_acc_ct1 .get_multi_ptr< sycl::access::decorated::no>() .get()); }); }); } else { auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) { sycl::local_accessor dpct_local_acc_ct1( sycl::range(smem), cgh); /* DPCT1050: The template argument of the dpct_kernel_name could not be deduced. You need to update this code. */ cgh.parallel_for, dpct_kernel_scalar<(N / 2)>>>( sycl::nd_range<3>(sycl::range(1, 1, DOWN_BLOCKS + 1) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { gr_down_multi_kernel<(N / 2)>( c, dpct_local_acc_ct1 .get_multi_ptr< sycl::access::decorated::no>() .get()); }); }); } } else { if (max4) { auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) { sycl::local_accessor dpct_local_acc_ct1( sycl::range(smem), cgh); cgh.parallel_for, dpct_kernel_scalar<4>>>( sycl::nd_range<3>(sycl::range(1, 1, DOWN_BLOCKS + 1) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { gr_down_multi_kernel( c, dpct_local_acc_ct1 .get_multi_ptr< sycl::access::decorated::no>() .get()); }); }); } else { auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) { sycl::local_accessor dpct_local_acc_ct1( sycl::range(smem), cgh); /* DPCT1050: The template argument of the dpct_kernel_name could not be deduced. You need to update this code. */ cgh.parallel_for, dpct_kernel_scalar>>( sycl::nd_range<3>(sycl::range(1, 1, DOWN_BLOCKS + 1) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { gr_down_multi_kernel( c, dpct_local_acc_ct1 .get_multi_ptr< sycl::access::decorated::no>() .get()); }); }); } } } if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0 + 1, (void*) st); launch_gr_up_multi(st, m); // STRATA_GR_UP_COLS (16 default, 8, 4): the same bits } /// STRATA_HC_SPLIT: unset or 2 = the newest the check accepts (staged), 1 = at most split, 0 = the plain read int env_variant() { const char* e = std::getenv("STRATA_HC_SPLIT"); if (e == nullptr || e[0] == '\0') return kHcStaged; if (e[0] == '0') return kHcPlain; if (e[0] == '1') return kHcSplit; return kHcStaged; } // per device: the variant `fused_gr_check` chose (0 = not checked yet) std::atomic g_variant[64]; // BEGIN PRIVATE UP FROM LO __dpct_inline__ void gr_up_from_lo_kernel(GrMulti m) { // stream sum unchanged, so the same bits; more blocks, fewer serial rows per warp constexpr int COLS=UPM_COLS; constexpr bool ILP=false; auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); auto &lo = *sycl::ext::oneapi::group_local_memory_for_overwrite< float[kFusedGrMaxT][LR]>( sycl::ext::oneapi::this_work_item::get_work_group<3>()); auto &g = *sycl::ext::oneapi::group_local_memory_for_overwrite< float[kFusedGrMaxT][HC][COLS]>( sycl::ext::oneapi::this_work_item::get_work_group<3>()); const int t = item_ct1.get_local_id(2), lane = t & 31, warp = t >> 5; const int T = m.T; const int d0 = item_ct1.get_group(2) * COLS; #pragma unroll 1 for (int i=t;i(m.a[0].w_up + (size_t)i * LR); /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ const sycl::uint4 wa = *(w4 + lane); /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ const sycl::uint4 wb = lane < LR / 8 - 32 ? *(w4 + 32 + lane) : sycl::uint4(0, 0, 0, 0); // the epilogue inputs of this lane's token, fetched while the dots run float rv = 0.0f, wn = 0.0f, rsc = 0.0f, bo = 0.0f, ip = 0.0f; bool apply = false; if (lane < T) { rv = pR[i]; wn = pWn[i]; rsc = pRs[c]; apply = pApply; if (apply) { bo = pBo[d0 + dd]; ip = pIp[c]; } } float mine = 0.0f; if constexpr (ILP) { // STRATA_GR_UP_ILP: the T dots first, then their butterflies interleaved step by step - float accs[kFusedGrMaxT]; // each value sees warp_sum's shuffles in warp_sum's order: the same bits #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) { float acc = 0.0f; if (k < T) { acc = dot8(wa, lo[k] + lane * 8); if (lane < LR / 8 - 32) acc += dot8(wb, lo[k] + (32 + lane) * 8); } accs[k] = acc; } #pragma unroll for (int o = 16; o > 0; o >>= 1) #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) if (k < T) accs[k] += dpct::experimental::permute_sub_group_by_xor( 0xffffffffu, sycl::ext::oneapi::this_work_item::get_sub_group(), accs[k], o); #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) if (k < T && lane == k) mine = accs[k]; } else { #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) { if (k >= T) break; float acc = dot8(wa, lo[k] + lane * 8); if (lane < LR / 8 - 32) acc += dot8(wb, lo[k] + (32 + lane) * 8); acc = warp_sum(acc); if (lane == k) mine = acc; } } if (lane < T) { if (apply) { rv = sycl::fma(bo, 2.0f * sigmoidf_(ip / (float)HC), rv); pRout[i] = rv; } const float x = rv * wn * rsc; g[lane][c][dd] = x * sigmoidf_(mine); } } /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); for (int i = t; i < T * COLS; i += THREADS) { const int k = i / COLS, col = i - k * COLS; float s = 0.0f; #pragma unroll for (int c = 0; c < HC; ++c) s += g[k][c][col]; GR_PICK(m, k, mixed)[d0 + col] = s / (float) HC; } } // END PRIVATE UP FROM LO // BEGIN PRIVATE LO VEC4 struct alignas(16) GrLoVec4Storage {float values[kFusedGrMaxT][2][LR/2];}; static_assert(sizeof(GrLoVec4Storage)==kFusedGrMaxT*LR*sizeof(float)); __dpct_inline__ float gr_lo_vec4_dot(sycl::uint4 w,const float* low,const float* high) { const sycl::float4 a(low[0],low[1],low[2],low[3]); const sycl::float4 b(high[0],high[1],high[2],high[3]); return dot8v(w,a,b); } __dpct_inline__ void gr_up_lo_vec4_kernel(GrMulti m) { // stream sum unchanged, so the same bits; more blocks, fewer serial rows per warp constexpr int COLS=UPM_COLS; constexpr bool ILP=false; auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); auto &lo = (*sycl::ext::oneapi::group_local_memory_for_overwrite< GrLoVec4Storage>(sycl::ext::oneapi::this_work_item::get_work_group<3>())).values; auto &g = *sycl::ext::oneapi::group_local_memory_for_overwrite< float[kFusedGrMaxT][HC][COLS]>( sycl::ext::oneapi::this_work_item::get_work_group<3>()); const int t = item_ct1.get_local_id(2), lane = t & 31, warp = t >> 5; const int T = m.T; const int d0 = item_ct1.get_group(2) * COLS; #pragma unroll 1 for (int i=t;i>2)&1][(r>>3)*4+(r&3)]=GR_PICK(m,k,lo)[r]; } /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); // this lane's token (lane < T), picked once - not per row const int pk = lane < T ? lane : 0; const float* const pR = GR_PICK(m, pk, R); const float* const pWn = GR_PICK(m, pk, w_norm); const float* const pRs = GR_PICK(m, pk, rs); const bool pApply = GR_PICK(m, pk, apply); const float* const pBo = GR_PICK(m, pk, bo_prev); const float* const pIp = GR_PICK(m, pk, inj_prev); float* const pRout = GR_PICK(m, pk, R_out); for (int r = warp; r < HC * COLS; r += WARPS) { const int c = r / COLS, dd = r - c * COLS, i = c * N + d0 + dd; const sycl::uint4 *w4 = reinterpret_cast(m.a[0].w_up + (size_t)i * LR); /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ const sycl::uint4 wa = *(w4 + lane); /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ const sycl::uint4 wb = lane < LR / 8 - 32 ? *(w4 + 32 + lane) : sycl::uint4(0, 0, 0, 0); // the epilogue inputs of this lane's token, fetched while the dots run float rv = 0.0f, wn = 0.0f, rsc = 0.0f, bo = 0.0f, ip = 0.0f; bool apply = false; if (lane < T) { rv = pR[i]; wn = pWn[i]; rsc = pRs[c]; apply = pApply; if (apply) { bo = pBo[d0 + dd]; ip = pIp[c]; } } float mine = 0.0f; if constexpr (ILP) { // STRATA_GR_UP_ILP: the T dots first, then their butterflies interleaved step by step - float accs[kFusedGrMaxT]; // each value sees warp_sum's shuffles in warp_sum's order: the same bits #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) { float acc = 0.0f; if (k < T) { acc = gr_lo_vec4_dot(wa,lo[k][0]+lane*4,lo[k][1]+lane*4); if (lane < LR / 8 - 32) acc += gr_lo_vec4_dot(wb,lo[k][0]+(32+lane)*4,lo[k][1]+(32+lane)*4); } accs[k] = acc; } #pragma unroll for (int o = 16; o > 0; o >>= 1) #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) if (k < T) accs[k] += dpct::experimental::permute_sub_group_by_xor( 0xffffffffu, sycl::ext::oneapi::this_work_item::get_sub_group(), accs[k], o); #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) if (k < T && lane == k) mine = accs[k]; } else { #pragma unroll for (int k = 0; k < kFusedGrMaxT; ++k) { if (k >= T) break; float acc = gr_lo_vec4_dot(wa,lo[k][0]+lane*4,lo[k][1]+lane*4); if (lane < LR / 8 - 32) acc += gr_lo_vec4_dot(wb,lo[k][0]+(32+lane)*4,lo[k][1]+(32+lane)*4); acc = warp_sum(acc); if (lane == k) mine = acc; } } if (lane < T) { if (apply) { rv = sycl::fma(bo, 2.0f * sigmoidf_(ip / (float)HC), rv); pRout[i] = rv; } const float x = rv * wn * rsc; g[lane][c][dd] = x * sigmoidf_(mine); } } /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); for (int i = t; i < T * COLS; i += THREADS) { const int k = i / COLS, col = i - k * COLS; float s = 0.0f; #pragma unroll for (int c = 0; c < HC; ++c) s += g[k][c][col]; GR_PICK(m, k, mixed)[d0 + col] = s / (float) HC; } } class GrUpLoVec4; inline bool gr_lo_vec4_enabled() { static const bool on=[] {const char* p=std::getenv("STRATA_PRIVATE_GR_LO_VEC4");return p&&std::atoi(p)!=0;}(); return on; } void launch_gr_up_lo_vec4(dpct::queue_ptr st,const GrMulti& m) { const sycl::nd_range<3> r(sycl::range<3>(1,1,UPM_BLOCKS*THREADS),sycl::range<3>(1,1,THREADS)); const auto props=sycl::ext::oneapi::experimental::properties{sycl::ext::oneapi::experimental::use_root_sync}; st->parallel_for(r,props,[=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] {gr_up_lo_vec4_kernel(m);}); } // END PRIVATE LO VEC4 // Match the original quantizer type name: AOT coalesces identical layouts. struct Q81Block {sycl::half2 ds;int8_t qs[32];}; static_assert(sizeof(Q81Block)==36 && offsetof(Q81Block,qs)==4); __dpct_inline__ float b60_gr_q8_warp_max(float value){ #pragma unroll for(int off=16;off>0;off>>=1)value=sycl::fmax(value,dpct::experimental::permute_sub_group_by_xor(0xffffffffu,sycl::ext::oneapi::this_work_item::get_sub_group(),value,off)); return value; } inline bool b60_gr_q8_up32_enabled(){static const bool on=[] {const char*p=std::getenv("STRATA_PRIVATE_GR_Q8_UP32");return p&&p[0]=='1';}();return on&&b60_layout_requested();} __dpct_inline__ void b60_gr_q8_up32_t1_kernel(GrMulti m) { // stream sum unchanged, so the same bits; more blocks, fewer serial rows per warp constexpr int COLS=32; constexpr bool ILP=false; auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>(); auto &lo = *sycl::ext::oneapi::group_local_memory_for_overwrite< float[1][LR]>( sycl::ext::oneapi::this_work_item::get_work_group<3>()); auto &g = *sycl::ext::oneapi::group_local_memory_for_overwrite< float[1][HC][COLS]>( sycl::ext::oneapi::this_work_item::get_work_group<3>()); const int t = item_ct1.get_local_id(2), lane = t & 31, warp = t >> 5; constexpr int T = 1; const int d0 = item_ct1.get_group(2) * COLS; #pragma unroll 1 for (int i=t;i(m.a[0].w_up + (size_t)i * LR); /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ const sycl::uint4 wa = *(w4 + lane); /* DPCT1098: The '*' expression is used instead of the __ldg call. These two expressions do not provide the exact same functionality. Check the generated code for potential precision and/or performance issues. */ const sycl::uint4 wb = lane < LR / 8 - 32 ? *(w4 + 32 + lane) : sycl::uint4(0, 0, 0, 0); // the epilogue inputs of this lane's token, fetched while the dots run float rv = 0.0f, wn = 0.0f, rsc = 0.0f, bo = 0.0f, ip = 0.0f; bool apply = false; if (lane < T) { rv = pR[i]; wn = pWn[i]; rsc = pRs[c]; apply = pApply; if (apply) { bo = pBo[d0 + dd]; ip = pIp[c]; } } float mine = 0.0f; if constexpr (ILP) { // STRATA_GR_UP_ILP: the T dots first, then their butterflies interleaved step by step - float accs[1]; // each value sees warp_sum's shuffles in warp_sum's order: the same bits #pragma unroll for (int k = 0; k < 1; ++k) { float acc = 0.0f; if (k < T) { acc = dot8(wa, lo[k] + lane * 8); if (lane < LR / 8 - 32) acc += dot8(wb, lo[k] + (32 + lane) * 8); } accs[k] = acc; } #pragma unroll for (int o = 16; o > 0; o >>= 1) #pragma unroll for (int k = 0; k < 1; ++k) if (k < T) accs[k] += dpct::experimental::permute_sub_group_by_xor( 0xffffffffu, sycl::ext::oneapi::this_work_item::get_sub_group(), accs[k], o); #pragma unroll for (int k = 0; k < 1; ++k) if (k < T && lane == k) mine = accs[k]; } else { #pragma unroll for (int k = 0; k < 1; ++k) { if (k >= T) break; float acc = dot8(wa, lo[k] + lane * 8); if (lane < LR / 8 - 32) acc += dot8(wb, lo[k] + (32 + lane) * 8); acc = warp_sum(acc); if (lane == k) mine = acc; } } if (lane < T) { if (apply) { rv = sycl::fma(bo, 2.0f * sigmoidf_(ip / (float)HC), rv); pRout[i] = rv; } const float x = rv * wn * rsc; g[lane][c][dd] = x * sigmoidf_(mine); } } /* DPCT1065: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory. */ item_ct1.barrier(); for (int i = t; i < T * COLS; i += THREADS) { const int k = i / COLS, col = i - k * COLS; float s = 0.0f; #pragma unroll for (int c = 0; c < HC; ++c) s += g[k][c][col]; const float xi=s/(float)HC; GR_PICK(m,k,mixed)[d0+col]=xi; const float amax=b60_gr_q8_warp_max(sycl::fabs(xi)); const float sum=warp_sum(xi); const float scale=q8_1_finite(amax/127.0f); const int8_t quant=q8_1_quant(xi,scale,amax); auto* image=reinterpret_cast(GR_PICK(m,k,q8_mixed)); image[(d0+col)/32].qs[col&31]=quant; if((col&31)==0)image[(d0+col)/32].ds=q8_1_ds(scale,sum); } } class B60GrQ8Up32T1; void launch_b60_gr_q8_up32_t1(dpct::queue_ptr st,GrMulti m){ const sycl::nd_range<3> r(sycl::range<3>(1,1,(N/32)*THREADS),sycl::range<3>(1,1,THREADS)); const auto props=sycl::ext::oneapi::experimental::properties{sycl::ext::oneapi::experimental::use_root_sync}; st->parallel_for(r,props,[=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] {b60_gr_q8_up32_t1_kernel(m);}); } inline bool gr_up_rows16_enabled(){static const bool on=[] {const char*p=std::getenv("STRATA_PRIVATE_GR_UP_ROWS16");return p&&p[0]=='1';}();return on&&b60_layout_requested()&&b60_main_actor();} void gf_dispatch_up(dpct::queue_ptr,GrMulti); class GrUpFromLo; class GrReduceSilu; void launch_gr_up_from_lo(dpct::queue_ptr st,const GrMulti& m) { if(gr_up_rows16_enabled() && m.T==4){gf_dispatch_up(st,m);return;} if(gr_lo_vec4_enabled() && m.T>=2 && m.T<=4) {launch_gr_up_lo_vec4(st,m);return;} const sycl::nd_range<3> r(sycl::range<3>(1,1,UPM_BLOCKS*THREADS),sycl::range<3>(1,1,THREADS)); const auto p=sycl::ext::oneapi::experimental::properties{sycl::ext::oneapi::experimental::use_root_sync}; st->parallel_for(r,p,[=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] {gr_up_from_lo_kernel(m);}); } inline bool gr_combo_silu_enabled() { static const bool enabled=[] {const char* p=std::getenv("STRATA_PRIVATE_GR_SILU_FROM_LO");return p&&std::atoi(p)!=0;}(); return enabled; } // Private decode-only GR experiment. FP32 inputs; original dot8 and XOR trees. namespace gf = sycl::ext::intel::esimd; namespace gfm = sycl::ext::intel::experimental::esimd; template SYCL_ESIMD_FUNCTION ESIMD_INLINE gf::simd gf_xor(gf::simd v) { gf::simd w; #pragma unroll for(int i=0;i<32;i+=2*O) { w.template select(i)=v.template select(i+O); w.template select(i+O)=v.template select(i); } return v+w; } template SYCL_ESIMD_FUNCTION ESIMD_INLINE void gf_up(GrMulti m,int d) { #pragma clang fp contract(off) gf::simd lo[E]; #pragma unroll for(int e=0;e(0)=gf::block_load(m.a[e].lo); lo[e].template select<128,1>(128)=gf::block_load(m.a[e].lo+128); lo[e].template select<64,1>(256)=gf::block_load(m.a[e].lo+256); } for(int c=0;c(m.a[0].w_up+(size_t)(c*N+d)*LR); auto wa=gf::block_load(p); auto wb=gf::block_load(p+128); #pragma unroll for(int e=0;e a=0.f; #pragma unroll for(int j=0;j<4;++j) { gf::simd bits=wa.template select<32,4>(j); gf::simd low=bits<<16,high=bits&0xffff0000u; gf::simd w0=low.template bit_cast_view(),w1=high.template bit_cast_view(); gf::simd x0=lo[e].template select<32,8>(2*j),x1=lo[e].template select<32,8>(2*j+1); a=gfm::fma(w0,x0,a); a=gfm::fma(w1,x1,a); } gf::simd b=0.f; #pragma unroll for(int j=0;j<4;++j) { gf::simd bits=wb.template select<8,4>(j); gf::simd low=bits<<16,high=bits&0xffff0000u; gf::simd w0=low.template bit_cast_view(),w1=high.template bit_cast_view(); gf::simd x0=lo[e].template select<8,8>(256+2*j),x1=lo[e].template select<8,8>(257+2*j); b=gfm::fma(w0,x0,b); b=gfm::fma(w1,x1,b); } a.template select<8,1>(0)=a.template select<8,1>(0)+b; a=gf_xor<16>(a);a=gf_xor<8>(a);a=gf_xor<4>(a);a=gf_xor<2>(a);a=gf_xor<1>(a); // xn is no longer read by down. Postop restores every scratch value. m.xn[(size_t)e*D+c*N+d]=a[e]; } } } template SYCL_ESIMD_FUNCTION ESIMD_INLINE void gf_down(GrMulti m,int sl,int tile) { #pragma clang fp contract(off) if(tile>=21 || (tile==20 && !m.a[0].w_inject))return; gf::simd rows(tile*16,1); auto valid=rows(tile==20?m.a[0].w_inject:m.a[0].w_down); gf::simd off=(rows-uint32_t(tile==20?LR:0))*uint32_t(D*2)+uint32_t(sl*GRS_COLS*2); gf::simd x[E]; gf::simd acc[E][4]; #pragma unroll for(int e=0;e(m.xn+(size_t)e*D+sl*GRS_COLS); #pragma unroll for(int q=0;q<4;++q)acc[e][q]=0.f; } #pragma unroll for(int cc=0;cc<4;++cc) { auto first=gf::gather(base,off+uint32_t(cc*64),valid,gf::simd(0)); auto last=gf::gather(base,off+uint32_t(cc*64+32),valid,gf::simd(0)); #pragma unroll for(int q=0;q<4;++q) { gf::simd dot[E]; #pragma unroll for(int e=0;e bits; // VS8 gather returns component-major: word j across all16 rows. if(q<2)bits=first.template select<16,1>((q*4+j)*16); else bits=last.template select<16,1>(((q-2)*4+j)*16); gf::simd low=bits<<16,high=bits&0xffff0000u; gf::simd w0=low.template bit_cast_view(),w1=high.template bit_cast_view(); #pragma unroll for(int e=0;e(x[e][cc*32+q*8+2*j]),dot[e]); dot[e]=gfm::fma(w1,gf::simd(x[e][cc*32+q*8+2*j+1]),dot[e]); } } #pragma unroll for(int e=0;e(m.part2,((uint32_t(sl*kFusedGrMaxT+e)*GRS_ROWS)+rows)*4u,value,valid); } } __dpct_inline__ void gf_reduce_silu(GrMulti m) { const int i=int(sycl::ext::oneapi::this_work_item::get_nd_item<3>().get_global_id(2)); if(i>=m.T*GRS_ROWS)return; const int k=i/GRS_ROWS,r=i%GRS_ROWS; if(r>=LR && !m.a[0].w_inject)return; float x=0.f; for(int sl=0;sl().get_global_id(2)); if(i>=m.T*N)return; const int k=i/N,d=i%N; const float* R=GR_PICK(m,k,R); const float* wn=GR_PICK(m,k,w_norm); const float* rs=GR_PICK(m,k,rs); float sum=0.f; #pragma unroll for(int c=0;c=1 && m.T<=4 && gr_down_sliced() && !gr_more_groups(st) && !gr_large_grf(st) && gr_norm_split() && gr_up_cols()==16 && !gr_up_ilp() && strata::intel_gpu_gen(st->get_device())==strata::IntelGpuGen::Battlemage; } bool gf_phase(const char* phase) { const char* p=std::getenv("STRATA_PRIVATE_GR_F32_PHASE"); return !p || std::strcmp(p,"both")==0 || std::strcmp(p,phase)==0; } template class GfDown; template class GfUp; class GfReduce; template class GfPostop; template void gf_launch_down(dpct::queue_ptr st,GrMulti m) { st->parallel_for>(sycl::nd_range<1>(GRS_SLICES*24,8),[=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL { const int id=int(it.get_global_id(0));gf_down(m,id/24,id%24); }); } // SIMD across16 output rows, preserving the original BF16 dot8/XOR arithmetic. template SYCL_ESIMD_FUNCTION ESIMD_INLINE gf::simd gp_leaf(GrMulti m,int row) { #pragma clang fp contract(off) const auto* w=reinterpret_cast(m.a[0].w_up); auto wa=gf::load_2d(w,LR*2-1,D-1,LR*2-1,Lane*4,row); gf::simd x[E];gf::simd dot[E]; #pragma unroll for(int e=0;e(m.a[e].lo+Lane*8);dot[e]=0.f;} #pragma unroll for(int j=0;j<4;++j){ gf::simd bits=wa.template select<16,1>(j*16),low=bits<<16,high=bits&0xffff0000u; gf::simd a=low.template bit_cast_view(),b=high.template bit_cast_view(); #pragma unroll for(int e=0;e(x[e][2*j]),dot[e]);dot[e]=gfm::fma(b,gf::simd(x[e][2*j+1]),dot[e]);} } if constexpr(Lane<8){ auto wb=gf::load_2d(w,LR*2-1,D-1,LR*2-1,128+Lane*4,row); gf::simd extra[E]; #pragma unroll for(int e=0;e(m.a[e].lo+256+Lane*8);extra[e]=0.f;} #pragma unroll for(int j=0;j<4;++j){ gf::simd bits=wb.template select<16,1>(j*16),low=bits<<16,high=bits&0xffff0000u; gf::simd a=low.template bit_cast_view(),b=high.template bit_cast_view(); #pragma unroll for(int e=0;e(x[e][2*j]),extra[e]);extra[e]=gfm::fma(b,gf::simd(x[e][2*j+1]),extra[e]);} } #pragma unroll for(int e=0;e result; #pragma unroll for(int e=0;e(e*16)=dot[e]; return result; } template SYCL_ESIMD_FUNCTION ESIMD_INLINE gf::simd gp_tree(GrMulti m,int row){ #pragma clang fp contract(off) if constexpr(Offset==32)return gp_leaf(m,row); else return gp_tree(m,row)+gp_tree(m,row); } templateclass GrUpRows16; templatevoid gp_launch_compute(dpct::queue_ptr st,GrMulti m){ st->parallel_for>(sycl::nd_range<1>(D/16,4),[=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL { #pragma clang fp contract(off) const int row=int(it.get_global_id(0))*16; gf::simd partial[4]; partial[0]=gp_tree(m,row);partial[1]=gp_tree(m,row); partial[2]=gp_tree(m,row);partial[3]=gp_tree(m,row); #pragma unroll for(int e=0;e a=partial[e].template select<16,1>(e*16),b=partial[e^2].template select<16,1>(e*16); gf::simd c=partial[e^1].template select<16,1>(e*16),d=partial[e^3].template select<16,1>(e*16); gf::simd result=(a+b)+(c+d); gf::block_store(m.xn+size_t(e)*D+row,result); } }); } inline bool gr_2d_pair_enabled(){static const bool enabled=[] {const char*p=std::getenv("STRATA_PRIVATE_GR_2D_PAIR");return p&&p[0]=='1';}();return enabled&&b60_main_actor();} template SYCL_ESIMD_FUNCTION ESIMD_INLINE void gf_down_2d(GrMulti m,int sl,int tile) { #pragma clang fp contract(off) if(tile>=21 || (tile==20 && !m.a[0].w_inject))return; gf::simd rows(tile*16,1); auto valid=rows(tile==20?m.a[0].w_inject:m.a[0].w_down); gf::simd off=(rows-uint32_t(tile==20?LR:0))*uint32_t(D*2)+uint32_t(sl*GRS_COLS*2); gf::simd x[E]; gf::simd acc[E][4]; #pragma unroll for(int e=0;e(m.xn+(size_t)e*D+sl*GRS_COLS); #pragma unroll for(int q=0;q<4;++q)acc[e][q]=0.f; } #pragma unroll for(int cc=0;cc<4;++cc) { const int height=(tile==20?HC:LR); const int row0=tile==20?0:tile*16; const int col0=sl*(GRS_COLS/2)+cc*16; auto first=gf::load_2d(base,D*2-1,height-1,D*2-1,col0,row0); auto last=gf::load_2d(base,D*2-1,height-1,D*2-1,col0+8,row0); #pragma unroll for(int q=0;q<4;++q) { gf::simd dot[E]; #pragma unroll for(int e=0;e bits; // VS8 gather returns component-major: word j across all16 rows. if(q<2)bits=first.template select<16,1>((q*4+j)*16); else bits=last.template select<16,1>(((q-2)*4+j)*16); gf::simd low=bits<<16,high=bits&0xffff0000u; gf::simd w0=low.template bit_cast_view(),w1=high.template bit_cast_view(); #pragma unroll for(int e=0;e(x[e][cc*32+q*8+2*j]),dot[e]); dot[e]=gfm::fma(w1,gf::simd(x[e][cc*32+q*8+2*j+1]),dot[e]); } } #pragma unroll for(int e=0;e(m.part2,((uint32_t(sl*kFusedGrMaxT+e)*GRS_ROWS)+rows)*4u,value,valid); } } template SYCL_ESIMD_FUNCTION ESIMD_INLINE gf::simd gp_leaf_pair(GrMulti m,int row){ #pragma clang fp contract(off) const auto*w=reinterpret_cast(m.a[0].w_up); auto wa=gf::load_2d(w,LR*2-1,D-1,LR*2-1,Even*4,row); gf::simd wb; if constexpr(Even<8)wb=gf::load_2d(w,LR*2-1,D-1,LR*2-1,128+Even*4,row); gf::simd result; #pragma unroll for(int parity=0;parity<2;++parity){ gf::simd x[E];gf::simd dot[E]; #pragma unroll for(int e=0;e(m.a[e].lo+(Even+parity)*8);dot[e]=0.f;} #pragma unroll for(int j=0;j<4;++j){ gf::simd bits=wa.template select<16,1>((parity*4+j)*16),low=bits<<16,high=bits&0xffff0000u; gf::simd a=low.template bit_cast_view(),b=high.template bit_cast_view(); #pragma unroll for(int e=0;e(x[e][2*j]),dot[e]);dot[e]=gfm::fma(b,gf::simd(x[e][2*j+1]),dot[e]);} } if constexpr(Even<8){ gf::simd extra[E]; #pragma unroll for(int e=0;e(m.a[e].lo+256+(Even+parity)*8);extra[e]=0.f;} #pragma unroll for(int j=0;j<4;++j){ gf::simd bits=wb.template select<16,1>((parity*4+j)*16),low=bits<<16,high=bits&0xffff0000u; gf::simd a=low.template bit_cast_view(),b=high.template bit_cast_view(); #pragma unroll for(int e=0;e(x[e][2*j]),extra[e]);extra[e]=gfm::fma(b,gf::simd(x[e][2*j+1]),extra[e]);} } #pragma unroll for(int e=0;e((parity*E+e)*16)=dot[e]; } return result; } template SYCL_ESIMD_FUNCTION ESIMD_INLINE gf::simd gp_tree_pair(GrMulti m,int row){ #pragma clang fp contract(off) if constexpr(Offset==32)return gp_leaf_pair(m,row); else return gp_tree_pair(m,row)+gp_tree_pair(m,row); } templateclass GrUpRows16Pair; templatevoid gp_launch_compute_pair(dpct::queue_ptr st,GrMulti m){ st->parallel_for>(sycl::nd_range<1>(D/16,4),[=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL { #pragma clang fp contract(off) const int row=int(it.get_global_id(0))*16; gf::simd partial[4]; auto p01=gp_tree_pair(m,row),p23=gp_tree_pair(m,row); partial[0]=p01.template select(0);partial[1]=p01.template select(E*16); partial[2]=p23.template select(0);partial[3]=p23.template select(E*16); #pragma unroll for(int e=0;e a=partial[e].template select<16,1>(e*16),b=partial[e^2].template select<16,1>(e*16); gf::simd c=partial[e^1].template select<16,1>(e*16),d=partial[e^3].template select<16,1>(e*16); gf::simd result=(a+b)+(c+d); gf::block_store(m.xn+size_t(e)*D+row,result); } }); } templateclass GfDown2D; templatevoid gf_launch_down_2d(dpct::queue_ptr st,GrMulti m){ st->parallel_for>(sycl::nd_range<1>(GRS_SLICES*24,8),[=](sycl::nd_item<1>it) SYCL_ESIMD_KERNEL { const int id=int(it.get_global_id(0));gf_down_2d(m,id/24,id%24); }); } struct GcDown {const uint16_t *w_down,*w_inject;float *xn,*part2;}; // Upstream added two optional HC-Q8 scale pointers to each FusedGrArgs. // Merge with the head: GrMulti gained two bools (cached_norm, cached_lo, the #4185 port) after the PR measured 1384; they sit at the end, so every earlier // field keeps its offset and the PR's kernels (which read fields by name) are unaffected; the size is 1384 plus at most the padding of the two bools. static_assert(sizeof(FusedGrArgs)==168 && sizeof(GrMulti)==1392 && sizeof(GcDown)==32); inline bool gc_enabled(){static const bool on=[] {const char*p=std::getenv("STRATA_PRIVATE_B60_NEXT_POOL");return p&&p[0]=='1';}();return on&&b60_main_actor();} template SYCL_ESIMD_FUNCTION ESIMD_INLINE void gc_down(GcDown m,int sl,int tile) { #pragma clang fp contract(off) if(tile>=21 || (tile==20 && !m.w_inject))return; gf::simd rows(tile*16,1); auto valid=rows(tile==20?m.w_inject:m.w_down); gf::simd off=(rows-uint32_t(tile==20?LR:0))*uint32_t(D*2)+uint32_t(sl*GRS_COLS*2); gf::simd x[E]; gf::simd acc[E][4]; #pragma unroll for(int e=0;e(m.xn+(size_t)e*D+sl*GRS_COLS); #pragma unroll for(int q=0;q<4;++q)acc[e][q]=0.f; } #pragma unroll for(int cc=0;cc<4;++cc) { const int height=(tile==20?HC:LR); const int row0=tile==20?0:tile*16; const int col0=sl*(GRS_COLS/2)+cc*16; gf::simd first,last; if(tile==20){ first=gf::load_2d(base,D*2-1,height-1,D*2-1,col0,row0); last=gf::load_2d(base,D*2-1,height-1,D*2-1,col0+8,row0); }else{ const auto* packed=base+size_t(tile)*16*(D/2); first=gf::block_load(packed+size_t(col0)*16); last=gf::block_load(packed+size_t(col0+8)*16); } #pragma unroll for(int q=0;q<4;++q) { gf::simd dot[E]; #pragma unroll for(int e=0;e bits; // VS8 gather returns component-major: word j across all16 rows. if(q<2)bits=first.template select<16,1>((q*4+j)*16); else bits=last.template select<16,1>(((q-2)*4+j)*16); gf::simd low=bits<<16,high=bits&0xffff0000u; gf::simd w0=low.template bit_cast_view(),w1=high.template bit_cast_view(); #pragma unroll for(int e=0;e(x[e][cc*32+q*8+2*j]),dot[e]); dot[e]=gfm::fma(w1,gf::simd(x[e][cc*32+q*8+2*j+1]),dot[e]); } } #pragma unroll for(int e=0;e(m.part2,((uint32_t(sl*kFusedGrMaxT+e)*GRS_ROWS)+rows)*4u,value,valid); } } templateclass GcDownKernel; templatevoid gc_launch_down(dpct::queue_ptr st,GrMulti full){ GcDown m{full.a[0].w_down,full.a[0].w_inject,full.xn,full.part2}; const unsigned device=dpct::get_device_id(st->get_device()); static std::atomic reported{0}; if(device<8 && !(reported.fetch_or(1u<parallel_for>(sycl::nd_range<1>(GRS_SLICES*24,8),[=](sycl::nd_item<1>it) SYCL_ESIMD_KERNEL { const int id=int(it.get_global_id(0));gc_down(m,id/24,id%24); }); } inline bool gr_soa_enabled(){static const bool enabled=[] {const char*p=std::getenv("STRATA_PRIVATE_GR_SOA_CACHE");return p&&p[0]=='1';}();return enabled&&b60_main_actor();} template SYCL_ESIMD_FUNCTION ESIMD_INLINE void gf_down_soa(GrMulti m,int sl,int tile) { #pragma clang fp contract(off) if(tile>=21 || (tile==20 && !m.a[0].w_inject))return; gf::simd rows(tile*16,1); auto valid=rows(tile==20?m.a[0].w_inject:m.a[0].w_down); gf::simd off=(rows-uint32_t(tile==20?LR:0))*uint32_t(D*2)+uint32_t(sl*GRS_COLS*2); gf::simd x[E]; gf::simd acc[E][4]; #pragma unroll for(int e=0;e(m.xn+(size_t)e*D+sl*GRS_COLS); #pragma unroll for(int q=0;q<4;++q)acc[e][q]=0.f; } #pragma unroll for(int cc=0;cc<4;++cc) { const int height=(tile==20?HC:LR); const int row0=tile==20?0:tile*16; const int col0=sl*(GRS_COLS/2)+cc*16; gf::simd first,last; if(tile==20){ first=gf::load_2d(base,D*2-1,height-1,D*2-1,col0,row0); last=gf::load_2d(base,D*2-1,height-1,D*2-1,col0+8,row0); }else{ const auto* packed=base+size_t(tile)*16*(D/2); first=gf::block_load(packed+size_t(col0)*16); last=gf::block_load(packed+size_t(col0+8)*16); } #pragma unroll for(int q=0;q<4;++q) { gf::simd dot[E]; #pragma unroll for(int e=0;e bits; // VS8 gather returns component-major: word j across all16 rows. if(q<2)bits=first.template select<16,1>((q*4+j)*16); else bits=last.template select<16,1>(((q-2)*4+j)*16); gf::simd low=bits<<16,high=bits&0xffff0000u; gf::simd w0=low.template bit_cast_view(),w1=high.template bit_cast_view(); #pragma unroll for(int e=0;e(x[e][cc*32+q*8+2*j]),dot[e]); dot[e]=gfm::fma(w1,gf::simd(x[e][cc*32+q*8+2*j+1]),dot[e]); } } #pragma unroll for(int e=0;e(m.part2,((uint32_t(sl*kFusedGrMaxT+e)*GRS_ROWS)+rows)*4u,value,valid); } } template SYCL_ESIMD_FUNCTION ESIMD_INLINE gf::simd gp_leaf_soa(GrMulti m,int row){ #pragma clang fp contract(off) const auto*w=reinterpret_cast(m.a[0].w_up); const auto* packed=w+size_t(row)*(LR/2); auto wa=gf::block_load(packed+size_t(Even*4)*16); gf::simd wb; if constexpr(Even<8)wb=gf::block_load(packed+size_t(128+Even*4)*16); gf::simd result; #pragma unroll for(int parity=0;parity<2;++parity){ gf::simd x[E];gf::simd dot[E]; #pragma unroll for(int e=0;e(m.a[e].lo+(Even+parity)*8);dot[e]=0.f;} #pragma unroll for(int j=0;j<4;++j){ gf::simd bits=wa.template select<16,1>((parity*4+j)*16),low=bits<<16,high=bits&0xffff0000u; gf::simd a=low.template bit_cast_view(),b=high.template bit_cast_view(); #pragma unroll for(int e=0;e(x[e][2*j]),dot[e]);dot[e]=gfm::fma(b,gf::simd(x[e][2*j+1]),dot[e]);} } if constexpr(Even<8){ gf::simd extra[E]; #pragma unroll for(int e=0;e(m.a[e].lo+256+(Even+parity)*8);extra[e]=0.f;} #pragma unroll for(int j=0;j<4;++j){ gf::simd bits=wb.template select<16,1>((parity*4+j)*16),low=bits<<16,high=bits&0xffff0000u; gf::simd a=low.template bit_cast_view(),b=high.template bit_cast_view(); #pragma unroll for(int e=0;e(x[e][2*j]),extra[e]);extra[e]=gfm::fma(b,gf::simd(x[e][2*j+1]),extra[e]);} } #pragma unroll for(int e=0;e((parity*E+e)*16)=dot[e]; } return result; } template SYCL_ESIMD_FUNCTION ESIMD_INLINE gf::simd gp_tree_soa(GrMulti m,int row){ #pragma clang fp contract(off) if constexpr(Offset==32)return gp_leaf_soa(m,row); else return gp_tree_soa(m,row)+gp_tree_soa(m,row); } templateclass GrUpRows16Soa; templatevoid gp_launch_compute_soa(dpct::queue_ptr st,GrMulti m){ st->parallel_for>(sycl::nd_range<1>(D/16,4),[=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL { #pragma clang fp contract(off) const int row=int(it.get_global_id(0))*16; gf::simd partial[4]; auto p01=gp_tree_soa(m,row),p23=gp_tree_soa(m,row); partial[0]=p01.template select(0);partial[1]=p01.template select(E*16); partial[2]=p23.template select(0);partial[3]=p23.template select(E*16); #pragma unroll for(int e=0;e a=partial[e].template select<16,1>(e*16),b=partial[e^2].template select<16,1>(e*16); gf::simd c=partial[e^1].template select<16,1>(e*16),d=partial[e^3].template select<16,1>(e*16); gf::simd result=(a+b)+(c+d); gf::block_store(m.xn+size_t(e)*D+row,result); } }); } templateclass GfDownSoa; templatevoid gf_launch_down_soa(dpct::queue_ptr st,GrMulti m){ if constexpr(E==1 || E==2 || E==4){if(gc_enabled()){gc_launch_down(st,m);return;}} st->parallel_for>(sycl::nd_range<1>(GRS_SLICES*24,8),[=](sycl::nd_item<1>it) SYCL_ESIMD_KERNEL { const int id=int(it.get_global_id(0));gf_down_soa(m,id/24,id%24); }); } template void gf_launch_up(dpct::queue_ptr st,GrMulti m) { if(gr_up_rows16_enabled() && m.T==4){if(gr_soa_enabled())gp_launch_compute_soa(st,m);else if(gr_2d_pair_enabled())gp_launch_compute_pair(st,m);else gp_launch_compute(st,m);} else st->parallel_for>(sycl::nd_range<1>(N,8),[=](sycl::nd_item<1> it) SYCL_ESIMD_KERNEL {gf_up(m,int(it.get_global_id(0)));}); st->parallel_for>(sycl::nd_range<3>(sycl::range<3>(1,1,((m.T*N+127)/128)*128),sycl::range<3>(1,1,128)), [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] {gf_postop(m);}); } void gf_dispatch_down(dpct::queue_ptr st,GrMulti m) { if(gr_soa_enabled()){ switch(m.T){case 1:gf_launch_down_soa<1>(st,m);return;case 2:gf_launch_down_soa<2>(st,m);return;case 4:gf_launch_down_soa<4>(st,m);return;} } if(gr_2d_pair_enabled() && m.T==4){switch(m.T){case 1:gf_launch_down_2d<1>(st,m);break;case 2:gf_launch_down_2d<2>(st,m);break;case 4:gf_launch_down_2d<4>(st,m);break;}return;} switch(m.T) {case 1:gf_launch_down<1>(st,m);break;case 2:gf_launch_down<2>(st,m);break;case 3:gf_launch_down<3>(st,m);break;case 4:gf_launch_down<4>(st,m);break;} } void gf_dispatch_up(dpct::queue_ptr st,GrMulti m) { switch(m.T) {case 1:gf_launch_up<1>(st,m);break;case 2:gf_launch_up<2>(st,m);break;case 3:gf_launch_up<3>(st,m);break;case 4:gf_launch_up<4>(st,m);break;} } } // namespace // one partials buffer per queue (the verifier's and the drafter's launches never share a queue; within a queue the // launches are ordered, and a graph capture records the pointer) static float* down_partials(sycl::queue* q) { static std::mutex mu; static std::unordered_map bufs; std::lock_guard lk(mu); auto it = bufs.find(q); if (it != bufs.end()) return it->second; float* p = sycl::malloc_device((size_t) DOWN_SPLIT * kFusedGrMaxT * LR, *q); bufs[q] = p; return p; } bool fused_gr_read_multi(const FusedGrArgs* a, int n_tok, float* xn_scratch, void* stream, unsigned long long* stamp_buf, int stamp_i0) { strata::workload_trace::Scope trace("decode.gr_read_api", stream, n_tok, D, LR, HC, N, 30); if (n_tok < 1 || n_tok > kFusedGrMaxT || xn_scratch == nullptr) { std::fprintf(stderr, "fused_gr_read_multi: invalid arguments\n"); std::exit(1); } GrMulti m; for (int t = 0; t < n_tok; ++t) { m.a[t] = a[t]; const FusedGrArgs& x = a[t]; if (!x.R || !x.w_norm || !x.w_down || !x.w_up || !x.lo || !x.rs || !x.mixed || (x.w_inject && !x.inject_out) || (x.apply && (!x.bo_prev || !x.inj_prev || !x.R_out)) || x.w_down != a[0].w_down || x.w_up != a[0].w_up || x.w_inject != a[0].w_inject || x.w_norm != a[0].w_norm) { std::fprintf(stderr, "fused_gr_read_multi: invalid arguments for token %d\n", t); std::exit(1); } } m.xn = xn_scratch; m.T = n_tok; dpct::queue_ptr st = strata::q_of(stream); if(gr_soa_enabled() && (n_tok==1||n_tok==2||n_tok==4)){ if(!gf_enabled(st,m)||!gf_phase("down")||gf_phase("up")||!gr_combo_silu_enabled()||!gr_up_rows16_enabled()) throw std::logic_error("B60 GR cache requires the measured GR control combination"); const auto* down=static_cast(b60_cached_weight(a[0].w_down,30)); const auto* up=static_cast(b60_cached_weight(a[0].w_up,30)); if(!down||!up)throw std::logic_error("B60 missing GR decode cache"); for(int t=0;t= 0 && dev3 < 64 && split3[dev3] == 0) { int optin = 0; // SYCL port: the device's local memory is the limit (no per-kernel opt-in to raise) optin = (int) dpct::get_device(dev3).get_local_mem_size(); const int limit = optin > 0 ? optin : 48 * 1024; const int need1 = (int) (kFusedGrMaxT * N * sizeof(float)), need2 = need1 / 2; int split = -1; if (need1 <= limit && /* DPCT1027: The call to cudaFuncSetAttribute was replaced with 0 because SYCL currently does not support corresponding setting. */ 0 == 0) split = 1; else if (need2 <= limit) // #375 (kenh0u): the S = 2 split (a 64 KB opt-in card: Turing) disagrees with itself in gr_parity // (graph replay vs direct call) - such a card keeps the default read until that split is fixed std::fprintf(stderr, "strata: STRATA_GR_V3=1 needs the two-half split on this card, which fails its " "checks (#375): the default read is used\n"); /* DPCT1026: The call to cudaGetLastError was removed because this functionality is redundant in SYCL. */ ; // drop any error the attempts left behind split3[dev3] = split; } } const int split = v3 && dev3 >= 0 && dev3 < 64 ? split3[dev3] : -1; if (split > 0) { // 2 kernels; scratch = partials + sums of squares float* part = xn_scratch; float* ssg = xn_scratch + (size_t) n_tok * HC * 2 * PR; // room for S = 2 /* DPCT1083: The size of local memory in the migrated code may be different from the original code. Check that the allocated memory size in the migrated code is correct. */ const size_t sm = (size_t)n_tok * (N / split) * sizeof(float); if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0, stream); if (split == 2) { auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) { sycl::local_accessor dpct_local_acc_ct1( sycl::range(sm), cgh); cgh.parallel_for>>( sycl::nd_range<3>(sycl::range(1, HC * 2, LR / WARPS + 1) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size( 32)]] { gr_down_v3_kernel<2>( m, part, ssg, dpct_local_acc_ct1 .get_multi_ptr() .get()); }); }); } else { auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) { sycl::local_accessor dpct_local_acc_ct1( sycl::range(sm), cgh); cgh.parallel_for>>( sycl::nd_range<3>(sycl::range(1, HC, LR / WARPS + 1) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size( 32)]] { gr_down_v3_kernel<1>( m, part, ssg, dpct_local_acc_ct1 .get_multi_ptr() .get()); }); }); } if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0 + 1, stream); if (split == 2) { auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>>( sycl::nd_range<3>(sycl::range(1, 1, UPM_BLOCKS) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { gr_up_v3_kernel<2>(m, part, ssg); }); } else { auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>>( sycl::nd_range<3>(sycl::range(1, 1, UPM_BLOCKS) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { gr_up_v3_kernel<1>(m, part, ssg); }); } /* DPCT1010: SYCL uses exceptions to report errors and does not use the error codes. The cudaGetLastError function call was replaced with 0. You need to rewrite this code. */ const dpct::err0 e3 = 0; return false; // SYCL port: never writes the q8_1 images (STRATA_QFUSE is not supported here) } const bool cached_norm=!gf_enabled(st,m)&&g_gr_cached_norm_mode>0&&g_gr_norm_direct_bounds&&gr_norm_split()&& gr_down_sliced()&&gr_cached_norm_safe(a,n_tok,xn_scratch); m.part = down_partials(st); static const bool reuse_norm = strata::bm_default_on("STRATA_GR_CACHED_NORM", st->get_device(), false); static const bool reuse_lo = strata::bm_default_on("STRATA_GR_LO_REUSE", st->get_device()); if ((reuse_norm || reuse_lo) && !cached_norm && !gf_enabled(st, m) && gr_norm_split() && gr_down_sliced() && !gr_more_groups(st) && !gr_large_grf(st) && !gr_up_ilp() && gr_up_cols() == UPM_COLS && strata::intel_gpu_gen(st->get_device()) == strata::IntelGpuGen::Battlemage && gr_reuse_safe(a, n_tok, xn_scratch)) { m.cached_norm = reuse_norm; m.cached_lo = reuse_lo; static const bool note = [] { std::fprintf(stderr, "strata: GR reuse enabled: normalized activation %d, low-rank SiLU %d\n", (int) reuse_norm, (int) reuse_lo); return true; }(); (void) note; } { auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; if(cached_norm) strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>( sycl::nd_range<3>(sycl::range(1,1,n_tok*HC)*sycl::range(1,1,THREADS),sycl::range(1,1,THREADS)), [=](sycl::nd_item<3>)[[sycl::reqd_sub_group_size(32)]]{gr_norm_cached_write_kernel(m);}); else if (g_gr_norm_direct_bounds && gr_norm_split()) strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>( sycl::nd_range<3>(sycl::range(1, 1, n_tok * HC) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_norm_bounds_port_kernel(m); }); else if (gr_norm_split()) strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>( sycl::nd_range<3>(sycl::range(1, 1, n_tok * HC) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_norm_split_port_kernel(m); }); else strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>( sycl::nd_range<3>(sycl::range(1, 1, n_tok) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { gr_norm_multi_kernel(m); }); } if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0, stream); // the local-memory capacity is a per-DEVICE property: once per device (a layer split runs this on two cards). // SYCL port: upstream picks TILEV 1280 on sm_75 and takes the per-block limit below sm_70 (CUDA compute // capabilities, cudaFuncSetAttribute opt-ins); here the tile is the port's TILE and the device's local memory is // the only limit. The tiled kernel is the fallback (STRATA_GR_DOWN_SLICED=0 STRATA_GR_DOWN_DIRECT=0); the // default is the sliced kernel below. static bool attr[64] = {}; static int chunk[64] = {}; // tokens the tiled down kernel may carry in one launch on this card int dev = 0; dev = dpct::get_current_device_id(); if (dev >= 0 && dev < 64 && !attr[dev]) { const int optin = (int) dpct::get_device(dev).get_local_mem_size(); const int capacity = (optin > 0 ? optin : 48 * 1024) / (int) (TILE * sizeof(float)); chunk[dev] = capacity < 1 ? 1 : (capacity > kFusedGrMaxT ? kFusedGrMaxT : capacity); attr[dev] = true; } const int chunk_tok = (dev >= 0 && dev < 64 && chunk[dev]) ? chunk[dev] : kFusedGrMaxT; const bool lo_once = (g_gr_lo_once||(cached_norm&&g_gr_cached_norm_mode>=2))&&gr_down_sliced(); if (gr_down_sliced() && gr_more_groups(st) && !lo_once) { m.part2 = slice_partials(st); m.nsplit = 1; if (gr_large_grf(st)) launch_gr_down_more(st, m); else launch_gr_down_more(st, m); } else if (gr_down_sliced()) { m.part2 = slice_partials(st); m.nsplit = 1; const bool big = gr_large_grf(st); if(gf_enabled(st,m) && gf_phase("down")) gf_dispatch_down(st,m); else strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler& cgh) { sycl::local_accessor tl(sycl::range<1>((size_t) kFusedGrMaxT * GRS_COLS), cgh); const sycl::nd_range<3> nr(sycl::range(1, 1, GRS_SLICES) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)); if (big) cgh.parallel_for>( nr, sycl::ext::oneapi::experimental::properties{sycl::ext::intel::experimental::grf_size<256>}, [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_down_sliced_kernel(m, tl.get_multi_ptr().get()); }); else cgh.parallel_for>( nr, [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_down_sliced_kernel(m, tl.get_multi_ptr().get()); }); }); const unsigned nred = unsigned((n_tok * GRS_ROWS + 127) / 128); const sycl::nd_range<3> nr(sycl::range(1, 1, nred) * sycl::range(1, 1, 128), sycl::range(1, 1, 128)); if(gf_enabled(st,m) && (gf_phase("up") || gr_combo_silu_enabled())) { st->parallel_for( sycl::nd_range<3>(sycl::range(1,1,nred*128),sycl::range(1,1,128)), [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] {gf_reduce_silu(m);}); } else if (lo_once) { // #3153: the reduce writes the low-rank SiLU rows (PrepareLo, the same kernel as the head's REUSE_LO) strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>(nr, [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_down_reduce_kernel(m); }); } else if (m.cached_lo) { // #4185 port (Battlemage default / STRATA_GR_LO_REUSE) static const bool reduce_ilp = strata::bm_default_on("STRATA_GR_REDUCE_ILP", st->get_device(), false); if (reduce_ilp) { strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>(nr, [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_down_reduce_kernel(m); }); } else { strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>(nr, [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_down_reduce_kernel(m); }); } } else strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>(nr, [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_down_reduce_kernel(m); }); } else if (gr_down_direct()) { strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>( sycl::nd_range<3>(sycl::range(1, 1, DOWN_BLOCKS * DOWN_SPLIT + 1) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), [=](sycl::nd_item<3>) [[sycl::reqd_sub_group_size(32)]] { gr_down_multi_direct_kernel(m); }); } else if (chunk_tok >= n_tok) { auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) { sycl::local_accessor dpct_local_acc_ct1( sycl::range((size_t)n_tok * TILE * sizeof(float)), cgh); cgh.parallel_for< dpct_kernel_name>>( sycl::nd_range<3>(sycl::range(1, 1, DOWN_BLOCKS * DOWN_SPLIT + 1) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { gr_down_multi_kernel( m, dpct_local_acc_ct1 .get_multi_ptr() .get()); }); }); } else { for (int c0 = 0; c0 < n_tok; c0 += chunk_tok) { const int ct = n_tok - c0 < chunk_tok ? n_tok - c0 : chunk_tok; GrMulti c{}; c.xn = xn_scratch + (size_t) c0 * D; c.T = ct; for (int k = 0; k < ct; ++k) c.a[k] = a[c0 + k]; c.part = m.part; auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) { sycl::local_accessor dpct_local_acc_ct1( sycl::range((size_t)ct * TILE * sizeof(float)), cgh); cgh.parallel_for< dpct_kernel_name>>( sycl::nd_range<3>(sycl::range(1, 1, DOWN_BLOCKS * DOWN_SPLIT + 1) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { gr_down_multi_kernel( c, dpct_local_acc_ct1 .get_multi_ptr() .get()); }); }); } } if (stamp_buf) gpu_stamp(stamp_buf, stamp_i0 + 1, stream); const bool private_q8=b60_gr_q8_up32_enabled() && n_tok==1 && a[0].q8_mixed && !a[0].q8_down && !a[0].q8_up && gf_enabled(st,m) && gf_phase("down") && !gf_phase("up") && gr_combo_silu_enabled(); if(private_q8)launch_b60_gr_q8_up32_t1(st,m); else if(gf_enabled(st,m) && gf_phase("up")) gf_dispatch_up(st,m); else if(gf_enabled(st,m) && gr_combo_silu_enabled()) launch_gr_up_from_lo(st,m); else if(cached_norm){ auto exp_props=sycl::ext::oneapi::experimental::properties{sycl::ext::oneapi::experimental::use_root_sync}; if(g_gr_cached_norm_mode==3){ GrUpCompactReady u{};u.w_up=m.a[0].w_up;u.xn=m.xn;u.T=m.T; for(int k=0;kparallel_for>( sycl::nd_range<3>(sycl::range(1,1,UPM_BLOCKS)*sycl::range(1,1,THREADS),sycl::range(1,1,THREADS)),exp_props, [=](sycl::nd_item<3>)[[sycl::reqd_sub_group_size(32)]]{gr_up_compact_ready_kernel(u);}); }else if(lo_once)st->parallel_for>( sycl::nd_range<3>(sycl::range(1,1,UPM_BLOCKS)*sycl::range(1,1,THREADS),sycl::range(1,1,THREADS)),exp_props, [=](sycl::nd_item<3>)[[sycl::reqd_sub_group_size(32)]]{gr_up_cached_norm_kernel(m);}); else st->parallel_for>( sycl::nd_range<3>(sycl::range(1,1,UPM_BLOCKS)*sycl::range(1,1,THREADS),sycl::range(1,1,THREADS)),exp_props, [=](sycl::nd_item<3>)[[sycl::reqd_sub_group_size(32)]]{gr_up_cached_norm_kernel(m);}); }else if (lo_once) { launch_gr_up_prepared_lo(m, st); } else if (gr_more_groups(st)) { if (gr_large_grf(st)) launch_gr_up_more(st, m); else launch_gr_up_more(st, m); } else { launch_gr_up_multi(st, m, gr_large_grf(st)); // STRATA_GR_UP_COLS (16 default, 8, 4): the same bits; Arc A-series: large GRF } /* DPCT1010: SYCL uses exceptions to report errors and does not use the error codes. The cudaGetLastError function call was replaced with 0. You need to rewrite this code. */ const dpct::err0 e = 0; return private_q8; // only the private T1 branch writes the image; all other paths retain fallback } // the bench only (AMD latency-hidden kernels): nothing to switch on the SYCL port void fused_gr_set_fast(int) {} bool fused_gr_supported(int64_t n_embd, int64_t hc, int64_t hc_lr) { return n_embd == N && hc == HC && hc_lr == LR; } void fused_gr_read(const FusedGrArgs& a, void* stream) { if (!a.R || !a.w_norm || !a.w_down || !a.w_up || !a.lo || !a.rs || !a.mixed || (a.w_inject && !a.inject_out) || (a.apply && (!a.bo_prev || !a.inj_prev || !a.R_out)) || (a.apply && a.inj_prev == a.inject_out)) { std::fprintf(stderr, "fused_gr_read: invalid arguments\n"); std::exit(1); } dpct::queue_ptr st = strata::q_of(stream); // SYCL port: the multi-token read's sliced down kernel and per-(token, stream) norm sum in their own orders, so a // window's token would differ in its last bits from this kernel's. The header promises "every token's outputs // are bitwise fused_gr_read(a[t])" (gr_parity checks it): with those paths on, the single read IS the multi read // of one token (each token's sums run in the same order whatever the window size). if (kDownXnTooBig || gr_down_sliced() || gr_norm_split() || gr_down_direct()) { static std::mutex mu; static std::unordered_map xn; float* scratch; { std::lock_guard lk(mu); float*& p = xn[st]; if (!p) p = sycl::malloc_device((size_t) D, *st); scratch = p; } fused_gr_read_multi(&a, 1, scratch, stream); return; } { auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>( sycl::nd_range<3>(sycl::range(1, 1, DOWN_BLOCKS + 1) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { gr_down_kernel(a); }); } { auto exp_props = sycl::ext::oneapi::experimental::properties{ sycl::ext::oneapi::experimental::use_root_sync}; strata::kprof::kp(st, __FILE__, __func__, __LINE__)->template parallel_for>( sycl::nd_range<3>(sycl::range(1, 1, UP_BLOCKS) * sycl::range(1, 1, THREADS), sycl::range(1, 1, THREADS)), exp_props, [=](sycl::nd_item<3> item_ct1) [[sycl::reqd_sub_group_size(32)]] { gr_up_kernel(a); }); } /* DPCT1010: SYCL uses exceptions to report errors and does not use the error codes. The cudaGetLastError function call was replaced with 0. You need to rewrite this code. */ const dpct::err0 e = 0; } namespace { /// The plain read, split, staged and (for one token) the single-token read on random bf16 weights and random inputs, /// 1..8 tokens, with and without the pending write; every output of split and staged compared with the plain read's /// bit for bit (and the plain read's with the single-token read's). `why[v]` gets the first difference of variant /// v; false if the check itself could not run. bool fused_gr_selftest(bool ok_variant[4], std::string why[4]) try { constexpr int TM = kFusedGrMaxT; constexpr int NV = 4; // sets: 0 = plain, 1 = split, 2 = staged, 3 = single-token std::mt19937 rng(20260930u); std::normal_distribution nd(0.0f, 1.0f); std::vector h_down((size_t) LR * D), h_up((size_t) D * LR), h_inj((size_t) HC * D); std::vector h_norm(D), h_R((size_t) TM * D), h_bo((size_t) TM * N), h_ip((size_t) TM * HC); for (auto& w : h_down) w = bf16_from_f32(0.02f * nd(rng)); for (auto& w : h_up) w = bf16_from_f32(0.05f * nd(rng)); for (auto& w : h_inj) w = bf16_from_f32(0.02f * nd(rng)); for (auto& w : h_norm) w = 1.0f + 0.1f * nd(rng); for (auto& x : h_R) x = nd(rng); for (auto& x : h_bo) x = 0.5f * nd(rng); for (auto& x : h_ip) x = 2.0f * nd(rng); for (int v = 0; v < 4; ++v) { ok_variant[v] = v == 1; why[v].clear(); } const size_t n_set = (size_t) TM * (D + D + LR + HC + HC + N); // R_out, xn, lo, rs, inject, mixed (floats) const size_t bytes = h_down.size() * 2 + h_up.size() * 2 + h_inj.size() * 2 + (h_norm.size() + h_R.size() + h_bo.size() + h_ip.size() + NV * n_set) * 4 + 32 * 256; uint8_t* base = nullptr; if (DPCT_CHECK_ERROR(base = (uint8_t *)sycl::malloc_device( bytes, dpct::get_in_order_queue())) != 0) { /* DPCT1026: The call to cudaGetLastError was removed because this functionality is redundant in SYCL. */ why[0] = "no room for the check (" + std::to_string(bytes >> 20) + " MiB)"; return false; } size_t off = 0; auto take = [&](size_t b) { void* p = base + off; off += (b + 255) / 256 * 256; return p; }; uint16_t* d_down = (uint16_t*) take(h_down.size() * 2); uint16_t* d_up = (uint16_t*) take(h_up.size() * 2); uint16_t* d_inj = (uint16_t*) take(h_inj.size() * 2); float* d_norm = (float*) take(h_norm.size() * 4); float* d_R = (float*) take(h_R.size() * 4); float* d_bo = (float*) take(h_bo.size() * 4); float* d_ip = (float*) take(h_ip.size() * 4); struct Set { float *R_out, *xn, *lo, *rs, *inj, *mixed; } set[NV]; for (Set& x : set) { x.R_out = (float*) take((size_t) TM * D * 4); x.xn = (float*) take((size_t) TM * D * 4); x.lo = (float*) take((size_t) TM * LR * 4); x.rs = (float*) take((size_t) TM * HC * 4); x.inj = (float*) take((size_t) TM * HC * 4); x.mixed = (float*) take((size_t) TM * N * 4); } dpct::queue_ptr st = &dpct::get_in_order_queue(); /* DPCT1025: The SYCL queue is created ignoring the flag and priority options. */ bool ok = off <= bytes && DPCT_CHECK_ERROR(st = dpct::get_current_device().create_queue(true)) == 0 && /* DPCT1124: cudaMemcpyAsync is migrated to asynchronous memcpy API. While the origin API might be synchronous, it depends on the type of operand memory, so you may need to call wait() on event return by memcpy API to ensure synchronization behavior. */ DPCT_CHECK_ERROR( st->memcpy(d_down, h_down.data(), h_down.size() * 2)) == 0 && /* DPCT1124: cudaMemcpyAsync is migrated to asynchronous memcpy API. While the origin API might be synchronous, it depends on the type of operand memory, so you may need to call wait() on event return by memcpy API to ensure synchronization behavior. */ DPCT_CHECK_ERROR(st->memcpy(d_up, h_up.data(), h_up.size() * 2)) == 0 && /* DPCT1124: cudaMemcpyAsync is migrated to asynchronous memcpy API. While the origin API might be synchronous, it depends on the type of operand memory, so you may need to call wait() on event return by memcpy API to ensure synchronization behavior. */ DPCT_CHECK_ERROR(st->memcpy(d_inj, h_inj.data(), h_inj.size() * 2)) == 0 && /* DPCT1124: cudaMemcpyAsync is migrated to asynchronous memcpy API. While the origin API might be synchronous, it depends on the type of operand memory, so you may need to call wait() on event return by memcpy API to ensure synchronization behavior. */ DPCT_CHECK_ERROR( st->memcpy(d_norm, h_norm.data(), h_norm.size() * 4)) == 0 && /* DPCT1124: cudaMemcpyAsync is migrated to asynchronous memcpy API. While the origin API might be synchronous, it depends on the type of operand memory, so you may need to call wait() on event return by memcpy API to ensure synchronization behavior. */ DPCT_CHECK_ERROR(st->memcpy(d_R, h_R.data(), h_R.size() * 4)) == 0 && /* DPCT1124: cudaMemcpyAsync is migrated to asynchronous memcpy API. While the origin API might be synchronous, it depends on the type of operand memory, so you may need to call wait() on event return by memcpy API to ensure synchronization behavior. */ DPCT_CHECK_ERROR(st->memcpy(d_bo, h_bo.data(), h_bo.size() * 4)) == 0 && /* DPCT1124: cudaMemcpyAsync is migrated to asynchronous memcpy API. While the origin API might be synchronous, it depends on the type of operand memory, so you may need to call wait() on event return by memcpy API to ensure synchronization behavior. */ DPCT_CHECK_ERROR(st->memcpy(d_ip, h_ip.data(), h_ip.size() * 4)) == 0; if (!ok) why[0] = "setting up the check failed"; std::vector h1, h2; // true when equal; false with the first difference in `w` (or a read-back failure in `ok`) auto same = [&](const float* d1, const float* d2, size_t n, const char* what, int T, int apply, std::string& w) { h1.resize(n); h2.resize(n); /* DPCT1124: cudaMemcpyAsync is migrated to asynchronous memcpy API. While the origin API might be synchronous, it depends on the type of operand memory, so you may need to call wait() on event return by memcpy API to ensure synchronization behavior. */ if (DPCT_CHECK_ERROR(st->memcpy(h1.data(), d1, n * 4)) != 0 || /* DPCT1124: cudaMemcpyAsync is migrated to asynchronous memcpy API. While the origin API might be synchronous, it depends on the type of operand memory, so you may need to call wait() on event return by memcpy API to ensure synchronization behavior. */ DPCT_CHECK_ERROR(st->memcpy(h2.data(), d2, n * 4)) != 0 || DPCT_CHECK_ERROR(st->wait()) != 0) { why[0] = "reading back the check failed"; ok = false; return false; } for (size_t i = 0; i < n; ++i) { uint32_t u1, u2; std::memcpy(&u1, &h1[i], 4); std::memcpy(&u2, &h2[i], 4); if (u1 != u2) { char buf[192]; std::snprintf(buf, sizeof buf, "%s differs for %d token(s)%s at %zu (%08x, not %08x)", what, T, apply ? " with the pending write" : "", i, u2, u1); w = buf; return false; } } return true; }; auto all_same = [&](const Set& s1, const Set& s2, int T, int apply, std::string& w) { return same(s1.lo, s2.lo, (size_t) T * LR, "lo", T, apply, w) && same(s1.rs, s2.rs, (size_t) T * HC, "rs", T, apply, w) && same(s1.inj, s2.inj, (size_t) T * HC, "inject", T, apply, w) && same(s1.mixed, s2.mixed, (size_t) T * N, "mixed", T, apply, w) && same(s1.R_out, s2.R_out, (size_t) T * D, "R", T, apply, w); }; ok_variant[2] = ok_variant[3] = ok; bool single_ok = ok; for (int apply = 0; apply < 2 && ok; ++apply) { for (int T = 1; T <= TM && ok; ++T) { FusedGrArgs a[NV][TM]; for (int v = 0; v < NV; ++v) { const size_t span = (size_t) ((uint8_t*) (set[v].mixed + (size_t) TM * N) - (uint8_t*) set[v].R_out); ok = ok && DPCT_CHECK_ERROR(st->memset(set[v].R_out, 0xFF, span)) == 0; // the whole set for (int k = 0; k < T; ++k) { FusedGrArgs& x = a[v][k]; x.R = d_R + (size_t) k * D; x.R_out = set[v].R_out + (size_t) k * D; x.apply = apply != 0; x.bo_prev = d_bo + (size_t) k * N; x.inj_prev = d_ip + (size_t) k * HC; x.w_norm = d_norm; x.w_down = d_down; x.w_up = d_up; x.w_inject = d_inj; x.eps = 1e-6f; x.lo = set[v].lo + (size_t) k * LR; x.rs = set[v].rs + (size_t) k * HC; x.inject_out = set[v].inj + (size_t) k * HC; x.mixed = set[v].mixed + (size_t) k * N; } } for (int v = 0; v < 3; ++v) { GrMulti m; for (int k = 0; k < T; ++k) m.a[k] = a[v][k]; m.xn = set[v].xn; m.T = T; launch_multi(m, v + 1, st, nullptr, 0); } if (T == 1) fused_gr_read(a[3][0], st); /* DPCT1010: SYCL uses exceptions to report errors and does not use the error codes. The cudaGetLastError function call was replaced with 0. You need to rewrite this code. */ if (0 != 0 || DPCT_CHECK_ERROR(st->wait()) != 0) { why[0] = "a kernel of the check failed"; ok = false; break; } if (ok_variant[2] && !all_same(set[0], set[1], T, apply, why[2])) ok_variant[2] = false; if (ok && ok_variant[3] && !all_same(set[0], set[2], T, apply, why[3])) ok_variant[3] = false; if (ok && T == 1 && single_ok && !all_same(set[3], set[0], T, apply, why[1])) single_ok = false; } } if (!single_ok && ok) { // the plain read itself disagrees with the single-token read why[2] = why[3] = "the plain read differs from the single-token read: " + why[1]; ok_variant[2] = ok_variant[3] = false; } if (st != &dpct::get_in_order_queue()) { st->wait(); dpct::get_current_device().destroy_queue(st); } sycl::free(base, dpct::get_in_order_queue()); /* DPCT1026: The call to cudaGetLastError was removed because this functionality is redundant in SYCL. */ if (!ok) ok_variant[2] = ok_variant[3] = false; return ok; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } } // namespace int fused_gr_variant() { int dev = 0; dev = dpct::get_current_device_id(); const int v = dev >= 0 && dev < 64 ? g_variant[dev].load() : 0; if (v > 0) return v; // not checked on this card: the plain read, unless STRATA_HC_SPLIT names a variant (a test such as gr_parity) const char* e = std::getenv("STRATA_HC_SPLIT"); return e != nullptr && (e[0] == '1' || e[0] == '2') ? env_variant() : kHcPlain; } void fused_gr_check() { int dev = 0; dev = dpct::get_current_device_id(); if (dev < 0 || dev >= 64 || g_variant[dev].load() > 0) return; // SYCL port: the port's own read (sliced down / split norm, STRATA_GR_DOWN_SLICED etc.) does not use upstream's // variants, and their self-test crashes on the B70 (0.1.32 merge, under investigation): opt-in only. if (std::getenv("STRATA_HC_CHECK") == nullptr) { g_variant[dev].store(kHcPlain); return; } const int want = env_variant(); if (want == kHcPlain) { g_variant[dev].store(kHcPlain); std::fprintf(stderr, "strata hc: CUDA%d: the hyper-connection read runs as the plain one (STRATA_HC_SPLIT=0)\n", dev); return; } bool okv[4]; std::string why[4]; const bool ran = fused_gr_selftest(okv, why); int use = kHcPlain; if (want >= kHcStaged && okv[kHcStaged]) use = kHcStaged; else if (okv[kHcSplit]) use = kHcSplit; g_variant[dev].store(use); if (!ran) std::fprintf(stderr, "strata hc: CUDA%d: the check of split/staged could not run (%s)\n", dev, why[0].c_str()); static const char* const name[4] = {"", "plain", "split", "staged"}; for (int v = kHcStaged; v >= kHcSplit; --v) if (v <= want && !okv[v] && ran) std::fprintf(stderr, "strata hc: CUDA%d: the %s read differs from the plain read on this card - not used: " "%s\n", dev, name[v], why[v].c_str()); static const char* const what[4] = { "", "the plain read (the norm per token, the down projection on 41 blocks)", "split (the norm per token and stream, then the plain read's down projection)", "staged (the norm per token and stream, the down projection's activations staged ahead by cp.async)"}; std::fprintf(stderr, "strata hc: CUDA%d: the hyper-connection read runs as %s%s\n", dev, what[use], use >= kHcSplit ? "; checked bit for bit against the plain read on this card (STRATA_HC_SPLIT=1 or 0 " "for the earlier ones)" : ""); } } // namespace strata::kernels