Repository navigation
Conversation
…KI interpreter Implements, with NKI's semantics, the nisa instructions that AWS's NKI samples use: nc_matmul, nc_transpose, activation, activation_reduce, tensor_tensor, tensor_scalar, tensor_scalar_reduce, scalar_tensor_tensor, tensor_reduce, tensor_tensor_scan, tensor_copy, memset, affine_select, reciprocal, iota, dma_copy, dma_transpose, get_nc_version; and nl.ds, num_programs, program_ndim, full, ones, shared_constant, shared_identity_matrix, transpose, floor, mod. nisa.dma_copy and nisa.dma_transpose read and write through load/store, so clients see their HBM accesses; NKITransformer passes their subscripted operands as keys. Also: a 1-D tile (partition only) broadcasts along the free axes; shapes accept par_dim objects imported from NKI; nl.mgrid results have .p/.x; assigning a [P, 1] result to a [P] slot reshapes it; a slice past the tensor keeps the tile's size instead of being truncated by numpy, and an unmasked out-of-bounds load raises IndexError as in NKI; shared-HBM tensors were all named after the op-patching wrapper's frame and so aliased one array; the tracer accepts boolean masks and slices with computed bounds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0169QRxKtsivkLDCS5f7rrsm
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #539 (base branch
fix/nki-official-kernels); retarget tomainafter #539 merges.Summary
#539 lets the legacy NKI interpreter (
frontend="nki") run kernels written in the style of AWS's NKI samples, but the samples' larger kernels use NKI ISA instructions (nisa.*) that the interpreter does not implement. Without them, the kernel calls NKI's real implementation and fails withRuntimeError: calling NKI API outside of NKI kernels is not supported. This PR implements them, plus thenloperations those kernels use, with NKI's semantics:nisa:nc_matmul(stationary.T @ moving, fp32),nc_transpose,activation/activation_reduce(op(data * scale + bias), optional reduction intoreduce_res),tensor_tensor,tensor_scalar/tensor_scalar_reduce,scalar_tensor_tensor,tensor_reduce,tensor_tensor_scan,tensor_copy,memset,affine_select,reciprocal,iota,dma_copy,dma_transpose,get_nc_version(gen2, a Trn1 NeuronCore).nl:ds,num_programs,program_ndim,full,ones,shared_constant,shared_identity_matrix,transpose,floor,mod.nlfunctions (nl.add,nl.max,nl.sum, ...) map to numpy ufuncs by name.nisa.dma_copyandnisa.dma_transposeread and write throughload/store, so analysis clients see their HBM accesses as loads and stores.NKITransformerrewritesnisa.dma_copy(dst=o[...], src=x[...])andnisa.dma_transpose(x[...])to pass the subscripts asdst_keys/src_keys.The samples' kernels also needed these fixes:
nisa.activation(op=nl.exp, data=t, scale=A[c0:c1, j])failed withoperands could not be broadcast together with shapes (128,512) (128,). Binary operations,scale/bias, and scalar operands now align a 1-D tile with axis 0.par_dim. Kernels import it directly (from neuronxcc.nki.language import par_dim), sonl.ndarray((par_dim(128), n), ...)received NKI'sParDimobjects; shapes now take their value.nl.mgrid. Its result has.pand.x(and.y,.z) for its axes, as in NKI.[P, 1]result to a[P]slot reshapes it, asstorealready does.masked_load. An unmasked out-of-bounds load now raisesIndexErrorthere, as NKI's simulator does, instead of failing onmask.shape.Builder.ndarraynamed a shared-HBM tensor after its caller's frame so that every program of the grid gets the same array. That caller was the op-patching wrapper, so every shared-HBM tensor of a kernel shared one name, and one array. It now uses the kernel's allocation site.nl.ds(start, size)).Tests
tests/nki/test_nki_isa.py: six kernels run under TileLens and undernki.simulate_kernel, and their outputs must match. They cover matmul with DMA, the vector-engine instructions, a grid withnl.ds, DMA with subscripts, miscellaneous operations, and partition vectors withmgridand the scan. Two more tests check that a DMA is traced as a load and a store, and that an unmasked slice past the tensor raisesIndexError, as it does in NKI. All eight fail on [FIX] Run NKI kernels written like the official samples in the NKI interpreter #539's head.nki.simulate_kernelfor the eleven attention tutorial kernels (attn_fwd_v1...v11), the three fused Mamba kernels, and the Stable Diffusion attention tutorial, besides [FIX] Run NKI kernels written like the official samples in the NKI interpreter #539's kernels.pytest tests/ -m ""(NKI tests included): 626 passed, 2 skipped on [FIX] Run NKI kernels written like the official samples in the NKI interpreter #539; 634 passed, 2 skipped with this PR. Environment: Python 3.12, Triton 3.8.0, neuronx-cc 2.22.12471, nki 0.2.0.ruff checkandruff format --check(v0.1.9) pass on the changed files.Not modeled: kernels that allocate SBUF by address (
ncc.sbuf.mod_alloc) and rely on two tiles sharing the same memory. Such tiles are separate arrays here.🤖 Generated with Claude Code
https://claude.ai/code/session_0169QRxKtsivkLDCS5f7rrsm