Skip to content

PR14: Java Backend Implementations (CUDA + CPU)#10447

Open
agibsonccc wants to merge 8 commits into
masterfrom
pr/14-java-backend-impls
Open

PR14: Java Backend Implementations (CUDA + CPU)#10447
agibsonccc wants to merge 8 commits into
masterfrom
pr/14-java-backend-impls

Conversation

@agibsonccc

@agibsonccc agibsonccc commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Summary

PR 14 of 22 PRs in the ag_new_release_updates_2 branch split. Merge after Layer 3 (native platform backends + DSP engine).

  • CudaDeviceContextProvider: New single canonical path for device-switching; replaces 15+ duplicated sequences (update affinity map → nativeOps.setDevice() → reset CudaZeroHandler ThreadLocal → fetch stream pointers) across CudaAffinityManager, AtomicAllocator, CudaZeroHandler, and multiple executioner paths
  • Coherence-aware data buffers: BaseCudaDataBuffer tracks CoherenceState per buffer; syncToHost()/syncToDevice() are no-ops when state is already valid; all 14 typed CUDA buffer subclasses updated
  • AtomicAllocator integration: allocateMemory() now calls DeviceMemoryManager.trackAllocation() before native allocation; enables cap enforcement and eviction callbacks without polling
  • 4 new backends scaffolded: TPU (PJRT/Cloud TPU v4/v5, BF16 default, HLO compilation caching, priority=50), Hexagon (SNPE/QNN dispatch), ZLUDA (AMD GPUs via HIP→CUDA translation, reuses existing JCublas backend), SDX (Snapdragon X Elite NPU, early-stage scaffold)
  • GraalVM Native Image support: jni-config.json, reflect-config.json, resource-config.json, native-image.properties added to CPU backend under META-INF/native-image/
  • CPU backend: Updated for DeviceContextProvider delegation, DeviceMemoryManager registration, OpaqueDataBufferDeallocator, MultiBackendWorkspace interface
  • AllocationPoint: Added getCoherenceState()/setCoherenceState() methods; deallocation unified via OpaqueNDArrayDeallocator

What Changed

CPU Backend — nd4j-cpu-backend-common (18 files)

  • CpuAffinityManager.java — delegates thread affinity to CpuDeviceContextProvider
  • CpuMemoryManager.java — registers with DeviceMemoryManager; honors memory caps
  • CpuNDArrayFactory.java — routes through DeviceAwareNDArrayFactory
  • BaseCpuDataBuffer.java — coherence state tracking; OpaqueDataBufferDeallocator
  • NativeOpExecutioner.java — integrates DeviceAwareOpExecutioner, BackendRoutingStrategy, HelperRouter; DSP plan path updated
  • CpuWorkspace.java — implements MultiBackendWorkspace interface
  • CpuTADManager.java, DirectShapeInfoProvider.java, CpuLapack.java, CpuLevel1.java, DefaultDataBufferFactory.java, CpuDeallocator.java, ConstantBuffersCache.java, CpuOpContext.java, CpuOpContextDeallocator.java, CpuNativeRandom.java — API alignment and coherence/deallocator updates
  • module-info.java — Java 9 module descriptor

CUDA Backend — nd4j-cuda (40+ files)

  • CudaDeviceContextProvider.javasingle canonical device-switch path: updates affinity map, traces via MultiGpuTracer, calls nativeOps.setDevice() (triggers C++ ContextBuffers::release() + lazy reinit), resets CudaZeroHandler ThreadLocal, fetches stream pointers via buildContext(); replaces 15+ duplicated call sites
  • AllocationPoint.javagetCoherenceState()/setCoherenceState() methods
  • AtomicAllocator.javaallocateMemory() and free() update DeviceMemoryManager atomics for cap enforcement
  • CudaAffinityManager.java — now delegates to CudaDeviceContextProvider; retains only high-level API
  • BaseCudaDataBuffer.java — coherence state per buffer; syncToHost()/syncToDevice() guarded by CoherenceState; OpaqueDataBufferDeallocator
  • CudaFloatDataBuffer.java, CudaHalfDataBuffer.java, CudaBfloat16DataBuffer.java, CudaDoubleDataBuffer.java, CudaIntDataBuffer.java, CudaLongDataBuffer.java, CudaByteDataBuffer.java, CudaBoolDataBuffer.java, CudaShortDataBuffer.java, CudaUByteDataBuffer.java, CudaUInt16DataBuffer.java, CudaUInt32DataBuffer.java, CudaUInt64DataBuffer.java, CudaUtf8Buffer.java — all updated for coherence tracking and new deallocator chain
  • CudaDataBufferFactory.java — produces coherence-aware buffers
  • MemoryHandler.java, CudaZeroHandler.java — reset context via CudaDeviceContextProvider on device switch
  • CudaMemoryManager.java — registers allocations in DeviceMemoryManager; checks device caps before allocating
  • SynchronousFlowController.java — uses tl_dspGapStream for gap op stream synchronization
  • CudaWorkspace.java — implements MultiBackendWorkspace with coherence-aware buffer management
  • CudaExecutioner.java — integrates DeviceAwareOpExecutioner, MultiBackendExecutioner; DSP execution path updated
  • CudaEnvironment.java, JCublasBackend.java — updated for Environment subsystem config classes
  • JCublasNDArray.java, JCublasNDArrayFactory.java — coherence tracking; device-routed factory
  • JcublasLapack.java, JcublasLevel1.java — routed through DeviceAwareOpExecutioner
  • CudaDeallocator.java, CudaContext.java, CudaOpContext.java, CudaOpContextDeallocator.java — updated deallocator chain and device context propagation
  • Configuration.java — extended with multi-device routing, FP8 support, Triton section fusion properties
  • BasicTADManager.java, DeviceTADManager.java — updated for device descriptor abstraction
  • Nd4jCudaPresets.java — updated preset annotations for FP8/Triton/paged attention entry points
  • module-info.java, nd4j-jcublas.properties — module exports and properties updated

CUDA Platform and Presets

  • nd4j-cuda-platform/pom.xml, nd4j-cuda-preset/pom.xml — updated platform BOM and preset build
  • Nd4jMinimalPresets.java, Nd4jMinimal.java, CpuStatisticsProvider.java — updated minimizer backend

New: TPU Backend — nd4j-tpu (7 files)

  • JTpuBackend.java — PJRT (Portable Runtime) for Cloud TPU v4/v5; auto-detects TPU via JNI probe; priority=50
  • JTpuNDArray.java — NDArray backed by XLA buffer handles
  • JTpuNDArrayFactory.java — produces JTpuNDArray instances
  • TpuEnvironment.java — HLO compilation caching, BF16 defaults
  • TpuExecutioner.java — routes ops to PJRT XLA execution
  • TpuOpContext.java, nd4j-jtpu.properties

New: TPU Preset — nd4j-tpu-preset (2 files)

  • Nd4jTpuPresets.java / Nd4jTpuHelper.java — JavaCPP preset and helper for PJRT bindings

New: Hexagon Backend — nd4j-hexagon (6 files)

  • HexagonBackend.java, HexagonEnvironment.java, HexagonExecutioner.java, HexagonOpContext.java — Qualcomm Hexagon DSP via SNPE/QNN
  • META-INF/services/org.nd4j.linalg.factory.Nd4jBackend — service registration
  • nd4j-hexagon-preset/Nd4jHexagonPresets.java — JavaCPP preset for SNPE/QNN bindings

New: ZLUDA Backend — nd4j-zluda (4 files)

  • JZludaBackend.java — CUDA-compatible backend via ZLUDA HIP→CUDA translation for AMD GPUs; reuses JCublas backend without Java-layer changes
  • ZludaEnvironment.java, module-info.java, nd4j-jzluda.properties

New: SDX Backend — nd4j-sdx (1 file)

  • SdxRuntime.java — Snapdragon X Elite NPU integration (early-stage scaffold)

GraalVM Native Image — nd4j-native/META-INF/native-image/ (4 files)

  • jni-config.json, reflect-config.json, resource-config.json, native-image.properties — GraalVM Native Image configuration for CPU backend AOT compilation

Build Infrastructure

  • nd4j-backend-impls/pom.xml — adds tpu, hexagon, zluda, sdx modules
  • nd4j-presets-common/pom.xml, nd4j-native-platform/pom.xml — updated dependencies

Dependencies

  • Depends on: PR12 (provides DeviceContextProvider, DeviceMemoryManager, DeviceDescriptor, CoherenceState, MultiBackendWorkspace interfaces); PR13 (new op impls tested through updated executioners)
  • Required by: PR15 (SameDiff session execution depends on updated CudaExecutioner and NativeOpExecutioner), PR16 (DSP runtime uses CudaDeviceContextProvider for stream management)

Merge Order

This PR is in Layer 4 (Java backend implementations — parallel with PR12/PR13/PR15, all needed before PR16).

Layer PRs
0 (no deps) PR01, PR02, PR20
1 (build/infra) PR03, PR04
2 (native core) PR05, PR06, PR07
3 (native feat) PR08, PR09, PR10, PR11
4 (java core) PR12, PR13, PR14, PR15
5 (java feat) PR16
6 (import/gen) PR17, PR18, PR19, PR21
7 (validation) PR22

Part of the 22-PR split of ag_new_release_updates_2 branch.
Merge layer: 4 (java core)
Files: 117

See pr-plans/00-master-plan.md for the full split plan and merge order.
…impls

# Conflicts:
#	nd4j/nd4j-backends/nd4j-backend-impls/nd4j-cuda/src/main/java/org/nd4j/linalg/jcublas/context/CudaContext.java
@agibsonccc

Copy link
Copy Markdown
Contributor Author

Architecture Overview

This PR implements the Java backend layer for CUDA and CPU, plus scaffolding for 4 new target platforms. The key architectural change is CudaDeviceContextProvider — a single canonical device-switching path replacing 15+ duplicated sequences scattered across CudaAffinityManager, AtomicAllocator, CudaZeroHandler, and multiple executioner paths.

Highlights

  • Unified device-switching via CudaDeviceContextProvider — replaces 15+ duplicated (update affinity → nativeOps.setDevice() → reset ThreadLocal → fetch stream pointers) sequences with one canonical path; all 14 typed CUDA buffer subclasses now track CoherenceState so syncToHost()/syncToDevice() are no-ops when already valid
  • 4 new backend scaffolds — TPU (PJRT/Cloud TPU v4/v5, BF16 default, HLO compilation caching), Hexagon (SNPE/QNN dispatch), ZLUDA (AMD GPUs via HIP→CUDA translation, reuses JCublas), SDX (Snapdragon X Elite NPU); plus GraalVM Native Image support for the CPU backend

- Add Info entries for SD_VALIDATE_PTR/OpTraits in CPU and CUDA presets
- Fix nd4j-native pom.xml: disable --release to resolve --add-exports conflict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant