Add BufferView: safe sub-range ndarray access for kernels#445
Add BufferView: safe sub-range ndarray access for kernels#445alanray-tech wants to merge 1 commit intoGenesis-Embodied-AI:mainfrom
Conversation
BufferView provides a safe, zero-copy sub-range view into an ndarray for kernel arguments. It rewrites view[i] to arr[offset + i] at AST-translation time with zero IR changes. In debug mode, inserts runtime bounds assertions with full callstack diagnostics (kernel name, thread ID, file:line per frame). Can be passed directly as a kernel parameter via qd.types.buffer_view(dtype), which auto-decomposes into (ndarray, offset, count) at compile time. Minor: improve boundary enum error message to list valid options.
7013b74 to
1d2fa27
Compare
|
This API looks terrible. I'm strongly against merging this. |
any better API for buffer view or just remove buffer view? |
Yes! this: N = 32
data = qd.ndarray(qd.f32, shape=(N,))
data.from_numpy(np.zeros(N, dtype=np.float32))
slice = data[:16] |
|
|
This API is also terrible. It should look like standard numpy code. Then it is IR translation layer (well, probably AST transform instead) that should be take care of all the boilerplate to translate python native style into the actual low-level python and/or assembly instructions. |
Agree, something I want to achieve is an automatic check on the triplet index(I), the submatrix index (row, col), to prevent any possible "logical out of bound" behaviour. I will appreciate it if you can design the API (I'm not so familiar with pythonic way of coding) |
|
Ok let's work together on this starting from next week? Sounds like a nice project :) |
|
sounds good. |
|
To summarize my concern:
We should be cautious at any point in large numerical system. |
Summary
BufferViewprovides a safe, zero-copy sub-range view into an ndarray for kernel arguments. It rewritesview[i]→arr[offset + i]at AST-translation time, requiring no IR modifications.In debug mode (
debug=True), it inserts runtime bounds assertions that report the kernel name, thread ID, file and line for every frame in the callstack.Usage example:
Output — the OOB access is caught at runtime with a clear diagnostic:
BufferView also composes with higher-level abstractions. For example, the Block COO Sparse matrix library in
qipc(IPC on quadrants) can use BufferView internally for itsTriplet.MatrixView,BCOO.MatrixView, andDense.VectorViewtypes, enabling safe sub-range kernel dispatch:Files changed
python/quadrants/lang/buffer_view.pypython/quadrants/types/buffer_view_type.pypython/quadrants/lang/impl.pysubscript/assignpython/quadrants/lang/_func_base.pypython/quadrants/lang/_template_mapper_hotpath.pypython/quadrants/lang/ast/.../function_def_transformer.pypython/quadrants/lang/__init__.pypython/quadrants/types/__init__.pypython/quadrants/types/enums.pyDesign notes
qd_assertat the AST level (Python-side), gated bycfg.debug. It does not use the IR-levelCheckOutOfBoundpass — this is intentional to avoid coupling with the globalcheck_out_of_boundflag and to keep the implementation self-contained.Test plan