Instruction file imported from bitstormFA/rew (
.github/instructions/buffer-device.instructions.md). Copyright stays with the author.
Layer 2 — Buffer ownership, device, dtype, sharding
The thin layer between the raw C API and everything above.
Hard rules
BufferHandleis the onlyref objectallowed in this layer (and one of only a few in the entire codebase). Its=destroyhook is the single point that calls into PJRT to release a buffer; it must be exception-safe and idempotent (donated/already-released handles are a no-op).- A
BufferHandlecarries a state field with at leastLiveandDonated. Any operation on aDonatedhandle must raiseBufferDonatedErrorwith a message pointing at the originatingjitcall. Deviceis a value type ((target, ordinal)). The default-target resolution happens once per thread (cuda13 > cuda12 > rocm > cpu) and is overridable viaREW_TARGETandsetDefaultDevice.Shardingcarries replicated, partitioned, or manual annotations. Keep it metadata-first: validation is allowed, but sharding must not introduce implicit host or cross-device transfers.DTypeis a closed enum; conversions to/from native Nim scalar types are exhaustivecasematches.
Skills to follow
nim-ownership-hooksfor=destroy/=copy/=sinksemantics onBufferHandle.nim-api-designfor the public surface (initDevice,defaultDevice, etc.).nim-error-handlingforBufferDonatedErrorand other failure modes.