diff --git a/CMakeLists.txt b/CMakeLists.txt index c987d40e..95a378c6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -372,6 +372,10 @@ if(RAD_SYCL_FOUND) install(FILES ${RAD_SYCL_BINARY_DIR}/librad_sycl.so DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES ${RAD_PLUGIN_OUT}/kernels/libxe2.so DESTINATION ${CMAKE_INSTALL_DATADIR}/radiance/kernels) + # its large-register kernels, a library of their own that libxe2 loads from its own libxe2.d + # (libxe2/CMakeLists.txt, xe2_grf256) + install(FILES ${RAD_PLUGIN_OUT}/kernels/libxe2.d/libxe2_grf256.so + DESTINATION ${CMAKE_INSTALL_DATADIR}/radiance/kernels/libxe2.d) endif() add_subdirectory(vendor) diff --git a/libxe2/CMakeLists.txt b/libxe2/CMakeLists.txt index 9c3aa4b6..964aba42 100644 --- a/libxe2/CMakeLists.txt +++ b/libxe2/CMakeLists.txt @@ -20,6 +20,9 @@ if(NOT RAD_SYCL_TARGETS) endif() file(GLOB XE2_SRC CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) +# The sources whose kernels need the large register file, built apart (xe2_grf256, below). +set(XE2_GRF256_SRC ${CMAKE_CURRENT_SOURCE_DIR}/xe2_attn_flash_esimd.cpp) +list(REMOVE_ITEM XE2_SRC ${XE2_GRF256_SRC}) rad_add_plugin(libxe2 kernels SOURCES ${XE2_SRC}) # -ffp-contract=off is a correctness flag here as in libref: kernels are checked against it, so @@ -35,23 +38,47 @@ rad_add_plugin(libxe2 kernels SOURCES ${XE2_SRC}) # add unless its own option says strict, whatever the front end's -ffp-contract (probe fmac.cpp: # t = (s * a) * b; c += t came back fused in 14062 of 65536 lanes, 0 with the option; SIMT kernels # are unaffected either way). Without it an ESIMD epilogue that copies libref's chain is not exact. -# -ze-intel-enable-auto-large-GRF-mode: a kernel's grf_size<256> property never reached the card. -# sycl-post-link records it on the device image (sycl-grf-size), but only the runtime's JIT reads that -# and adds the large-register option; this build compiles AOT at link time with the options here -# alone, so every kernel got 128 registers, and the 256-register ones spilled (prefill attention: 4 -# KiB a thread, 66% of its stall samples on the scoreboard). The auto mode gives a kernel the large -# file when it needs it: the attention kernels get 256 and no spill (probe fa.cpp at 2048 tokens 7.3 -# -> 13.5 TFLOPS, output identical), the w4 GEMMs keep 128 and their eight threads an EU. set(_xe2_sycl -fsycl "-fsycl-targets=${RAD_SYCL_TARGETS}" -ffp-model=precise -ffp-contract=off -Wno-overriding-option -foffload-fp32-prec-div -foffload-fp32-prec-sqrt "SHELL:-Xspirv-translator -spirv-ext=+SPV_INTEL_subgroup_matrix_multiply_accumulate" - "SHELL:-Xsycl-target-backend \"-options '-ffp-contract=off -ze-intel-enable-auto-large-GRF-mode'\"") -target_compile_options(libxe2 PRIVATE ${_xe2_sycl} -fvisibility=hidden) + "SHELL:-Xspirv-translator -spirv-ext=+SPV_INTEL_subgroup_matrix_multiply_accumulate") +set(_xe2_be "SHELL:-Xsycl-target-backend \"-options -ffp-contract=off\"") +target_compile_options(libxe2 PRIVATE ${_xe2_sycl} ${_xe2_be} -fvisibility=hidden) get_filename_component(_xe2_icpx_bin "${CMAKE_CXX_COMPILER}" DIRECTORY) get_filename_component(_xe2_oneapi_lib "${_xe2_icpx_bin}/../lib" ABSOLUTE) -target_link_options(libxe2 PRIVATE ${_xe2_sycl} "LINKER:-rpath,${_xe2_oneapi_lib}" +target_link_options(libxe2 PRIVATE ${_xe2_sycl} ${_xe2_be} "LINKER:-rpath,${_xe2_oneapi_lib}" "LINKER:--no-undefined") +# xe2_grf256: the kernels that need 256 registers a thread. A kernel's grf_size<256> property never +# reaches the card in this build: sycl-post-link records it on the device image (sycl-grf-size), but +# only the runtime's JIT reads that and adds the large-register option, and libxe2 compiles AOT at +# link time with one option string for every image (neither per-kernel device code split nor +# --offload-new-driver changes that, 2026.2). Every kernel got 128 registers and the 256-register +# ones spilled: prefill attention 4 KiB a thread, 66% of its stall samples on the scoreboard. So +# they are their own device link, with the large file forced, in a library libxe2 loads from +# kernels/libxe2.d (beside the plugins, not among them: the loader would take it for one). The +# auto-large-GRF mode over all of libxe2 instead also moved ~25 SIMT kernels that never spilled to +# 256 and half their threads (gdn_conv_prep 434 -> 687 us a call). Probe fa.cpp (24/4 heads, +# head_dim 256): 2048 tokens 7.3 -> 13.5 TFLOPS, 8192 tokens 8.3 -> 17.8, output identical. +# Its refusals go through libxe2's refuse(), exported for it and found in libxe2 at load time. +add_library(xe2_grf256 SHARED ${XE2_GRF256_SRC}) +target_link_libraries(xe2_grf256 PRIVATE radiance::abi) +set(_xe2_be256 "SHELL:-Xsycl-target-backend \"-options '-ffp-contract=off -ze-opt-large-register-file'\"") +target_compile_options(xe2_grf256 PRIVATE ${_xe2_sycl} ${_xe2_be256}) +target_link_options(xe2_grf256 PRIVATE ${_xe2_sycl} ${_xe2_be256} "LINKER:-rpath,${_xe2_oneapi_lib}") +set_target_properties(xe2_grf256 PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${RAD_PLUGIN_OUT}/kernels/libxe2.d) +target_link_libraries(libxe2 PRIVATE xe2_grf256) +target_link_options(libxe2 PRIVATE "LINKER:-rpath,$ORIGIN/libxe2.d") +# Installed beside libxe2 wherever it goes: by this file standalone, by the main project from the +# sycl/ sub-build (which installs nothing itself). +if(NOT DEFINED RAD_PLUGIN_INSTALL OR RAD_PLUGIN_INSTALL) + if(RAD_PLUGIN_INSTALL_HOME) + install(TARGETS xe2_grf256 LIBRARY DESTINATION "${RAD_PLUGIN_INSTALL_HOME}/kernels/libxe2.d") + else() + install(TARGETS xe2_grf256 LIBRARY DESTINATION "${CMAKE_INSTALL_DATADIR}/radiance/kernels/libxe2.d") + endif() +endif() + # oneMKL's SYCL BLAS for the prefill-sized fp16 GEMMs (xe2_gemm_xmx.cpp, xmx_f16_scaled): ~85-90 # TFLOPS at the 27B's shapes against ~25 for the hand-tiled kernel, deterministic run to run. Part # of oneAPI, found beside icpx; without it the hand-tiled kernel serves (RAD_XE2_MKL=OFF forces that). diff --git a/libxe2/xe2_common.h b/libxe2/xe2_common.h index 74d649ad..d824e852 100644 --- a/libxe2/xe2_common.h +++ b/libxe2/xe2_common.h @@ -36,7 +36,9 @@ constexpr int kSub = 16; /* Why the last launch on this thread refused, for the engine's error report and the tests. */ const char* last_refusal(); -int refuse(const char* fmt, ...) __attribute__((format(printf, 1, 2))); +/* Exported: xe2_grf256 (the large-register kernels, a library of their own; CMakeLists.txt) refuses + * through libxe2's, so last_refusal() sees its reasons too. */ +int refuse(const char* fmt, ...) __attribute__((format(printf, 1, 2), visibility("default"))); /* A row-major view of an operand read as rows of `n` elements: the base, and the element distance * between consecutive rows. A tensor whose last axis is `n` may have padded rows, provided every