FreeToken brings datacenter-scale model serving to your desktop. Run massive models locally, fast and efficiently.
fix(kernel): correct jit tensor matcher checks and pdl arch guards (#638)
Xiaoze Fan803a3b48023b
diff --git a/python/freetoken/kernel/csrc/include/freetoken/tensor.h b/python/freetoken/kernel/csrc/include/freetoken/tensor.hindex 9b591a87..3f711a5f 100644--- a/python/freetoken/kernel/csrc/include/freetoken/tensor.h+++ b/python/freetoken/kernel/csrc/include/freetoken/tensor.h@@ -34,7 +34,7 @@ struct DeviceRef;inline constexpr auto kAnyDeviceID = -1;inline constexpr auto kAnySize = static_cast<int64_t>(-1);-inline constexpr auto kNullSize = static_cast<int64_t>(0);+inline constexpr auto kNullSize = static_cast<int64_t>(-1);inline constexpr auto kNullDType = static_cast<DLDataTypeCode>(18u);inline constexpr auto kNullDevice = static_cast<DLDeviceType>(-1);@@ -147,7 +147,7 @@ public:RuntimeCheck(!this->has_value(), "Size value already set");m_value = value;}- auto has_value() const -> bool { return m_value != 0; }+ auto has_value() const -> bool { return m_value != details::kNullSize; }auto get_value() const -> std::optional<int64_t> {return this->has_value() ? std::optional{m_value} : std::nullopt;}@@ -385,6 +385,10 @@ public:auto with_dtype(DTypeRef &&dtype) && -> TensorMatcher && {m_init_dtype();m_dtype.rebind(*dtype);+ // no options given: keep the ones already set on the shared symbol+ if constexpr (sizeof...(Ts) > 0) {+ m_dtype->set_options<Ts...>();+ }return std::move(*this);}@@ -400,6 +404,9 @@ public:auto with_device(DeviceRef &&device) && -> TensorMatcher && {m_init_device();m_device.rebind(*device);+ if constexpr (sizeof...(Codes) > 0) {+ m_device->set_options<Codes...>();+ }return std::move(*this);}diff --git a/python/freetoken/kernel/csrc/include/freetoken/utils.cuh b/python/freetoken/kernel/csrc/include/freetoken/utils.cuhindex 72a49596..091642a8 100644--- a/python/freetoken/kernel/csrc/include/freetoken/utils.cuh+++ b/python/freetoken/kernel/csrc/include/freetoken/utils.cuh@@ -43,16 +43,21 @@ __always_inline __device__ auto offset(const T *ptr, U... offset) -> constnamespace PDL {+// griddepcontrol needs sm_90+; older archs and HIP keep normal stream ordering.template <bool kUsePDL> __always_inline __device__ void wait() {+#if !FREETOKEN_USE_ROCM && __CUDA_ARCH__ >= 900if constexpr (kUsePDL) {asm volatile("griddepcontrol.wait;" ::: "memory");}+#endif}template <bool kUsePDL> __always_inline __device__ void launch() {+#if !FREETOKEN_USE_ROCM && __CUDA_ARCH__ >= 900if constexpr (kUsePDL) {asm volatile("griddepcontrol.launch_dependents;" :::);}+#endif}} // namespace PDL@@ -144,7 +149,9 @@ private:return config;}cudaLaunchConfig_t m_config;+#if !FREETOKEN_USE_ROCMcudaLaunchAttribute m_attr_cache;+#endif};} // namespace hostdiff --git a/python/freetoken/kernel/csrc/jit/index.cu b/python/freetoken/kernel/csrc/jit/index.cuindex ca0e1db2..4832e0a4 100644--- a/python/freetoken/kernel/csrc/jit/index.cu+++ b/python/freetoken/kernel/csrc/jit/index.cu@@ -35,9 +35,10 @@ __global__ __launch_bounds__(kNumThreads, kMaxOccupancy) void //using namespace device;constexpr auto kSize = kElementSize;constexpr auto kSizePerWarp = kSize / kNumSplits;- constexpr auto kWarpPerBlock = static_cast<unsigned>(kNumThreads / 32);+ constexpr auto kWarpPerBlock =+ static_cast<unsigned>(kNumThreads / kWarpThreads);- static_assert(kNumThreads % 32 == 0);+ static_assert(kNumThreads % kWarpThreads == 0);static_assert(std::has_single_bit(kNumSplits));static_assert(kElementSize % kNumSplits == 0);@@ -66,9 +67,10 @@ __global__ __launch_bounds__(kNumThreads, kMaxOccupancy) void //using namespace device;constexpr auto kSize = kElementSize;constexpr auto kSizePerWarp = kSize / kNumSplits;- constexpr auto kWarpPerBlock = static_cast<unsigned>(kNumThreads / 32);+ constexpr auto kWarpPerBlock =+ static_cast<unsigned>(kNumThreads / kWarpThreads);- static_assert(kNumThreads % 32 == 0);+ static_assert(kNumThreads % kWarpThreads == 0);static_assert(std::has_single_bit(kNumSplits));static_assert(kElementSize % kNumSplits == 0);@@ -114,15 +116,15 @@ struct IndexKernel {TensorMatcher({-1, D}) //.with_dtype(weights_dtype_)- .with_device<kDLCUDA>(device_)+ .with_device<kDLCUDA, kDLROCM>(device_).verify(weights);TensorMatcher({L, D}) //.with_dtype(weights_dtype_)- .with_device<kDLCUDA>(device_)+ .with_device<kDLCUDA, kDLROCM>(device_).verify(output);TensorMatcher({L}) //.with_dtype<int32_t, int64_t>(indices_dtype_)- .with_device<kDLCUDA>(device_)+ .with_device<kDLCUDA, kDLROCM>(device_).verify(indices);const auto device = device_.unwrap();@@ -132,7 +134,12 @@ struct IndexKernel {RuntimeCheck(entry_size == element_size,"IndexKernel: element_size mismatch.");- constexpr auto kWarpPerBlock = num_threads / 32;+ // Empty batches are valid no-ops; do not launch a zero-sized grid.+ if (num_indices == 0) {+ return;+ }++ constexpr auto kWarpPerBlock = num_threads / device::kWarpThreads;const auto num_warps = num_splits * num_indices;const auto num_blocks = div_ceil(num_warps, kWarpPerBlock);const auto params = IndexKernelParams{diff --git a/python/freetoken/kernel/csrc/jit/store.cu b/python/freetoken/kernel/csrc/jit/store.cuindex 8d84d76e..18fd4534 100644--- a/python/freetoken/kernel/csrc/jit/store.cu+++ b/python/freetoken/kernel/csrc/jit/store.cu@@ -72,18 +72,18 @@ struct StoreKernel {TensorMatcher({-1, D}) //.with_strides({X, 1})- .with_device<kDLCUDA>(device_)+ .with_device<kDLCUDA, kDLROCM>(device_).with_dtype(dtype_).verify(k_cache).verify(v_cache);TensorMatcher({L, D}) //.with_strides({Y, 1})- .with_device<kDLCUDA>(device_)+ .with_device<kDLCUDA, kDLROCM>(device_).with_dtype(dtype_).verify(k).verify(v);TensorMatcher({L}) //- .with_device<kDLCUDA>(device_)+ .with_device<kDLCUDA, kDLROCM>(device_).with_dtype<int32_t, int64_t>(indices_dtype_).verify(indices);@@ -96,6 +96,11 @@ struct StoreKernel {const auto kv_cache_stride = X.unwrap() * dtype_size;const auto kv_input_stride = Y.unwrap() * dtype_size;+ // Empty batches are valid no-ops; do not launch a zero-sized grid.+ if (length == 0) {+ return;+ }+const auto params = StoreKernelParams{.k_cache = k_cache.data_ptr(),.v_cache = v_cache.data_ptr(),@@ -107,8 +112,8 @@ struct StoreKernel {.length = length,};- constexpr auto kWarpPerBlock = num_threads / 32;- static_assert(num_threads % 32 == 0);+ constexpr auto kWarpPerBlock = num_threads / device::kWarpThreads;+ static_assert(num_threads % device::kWarpThreads == 0);const auto num_blocks = div_ceil(length, kWarpPerBlock);const auto kernel = use_int32? store_kv_cache<num_threads, max_concurrency,