From c27cd56ddfac3b047caf21586a5ae889e3f92736 Mon Sep 17 00:00:00 2001 From: lvyufeng Date: Thu, 26 Mar 2026 21:07:46 +0800 Subject: [PATCH] fix(_cython): disable stale PTA cache reuse in NPU fast add Guard the NPU fast add PTA executor cache because it can reuse stale tensor bindings across calls, which corrupts later view-based adds after a prior NPU add/profiler session. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/candle/_cython/_npu_ops.pyx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/candle/_cython/_npu_ops.pyx b/src/candle/_cython/_npu_ops.pyx index dc4580c8..1a600019 100644 --- a/src/candle/_cython/_npu_ops.pyx +++ b/src/candle/_cython/_npu_ops.pyx @@ -345,6 +345,12 @@ cdef dict _alpha_one_handles = {} # dtype_code -> alpha=1 scalar handle ( cdef dict _alpha_one_bytes_cache = {} # dtype_code -> (bytes, alpha_dtype_code) for PTA hash cdef object _pta_cache_begin_fn = None # _aclnn_ffi.pta_begin_add_cache_lookup cdef object _pta_cache_end_fn = None # _aclnn_ffi.pta_end_cache_lookup +# Disabled by default: the PTA cached-executor path can reuse stale bound tensor +# addresses across NPU add calls, which corrupts later view-based adds +# (e.g. a prior x+x can make a later view+ones behave like view+view). +# Keep the integration wired but guarded so it can be re-enabled once the +# cached executor path safely rebinds descriptors/addresses. +cdef bint _use_add_pta_cache = False cdef inline void _ensure_ffi_binary() except *: @@ -516,7 +522,7 @@ def fast_add(a, b): cdef uintptr_t alpha_handle # 9. Try PTA executor cache (torch_npu-aligned hit_cache_v2 path) - if _pta_cache_begin_fn is not None: + if _use_add_pta_cache and _pta_cache_begin_fn is not None: alpha_bytes_pair = _get_alpha_one_bytes(dtype_code) state = _pta_cache_begin_fn( py_a_shape, a.stride,