Skip to content

[REFACTOR] Implement Hardware Abstraction Layer for Operator Wrappers - #37

Merged
Flink-ddd merged 5 commits into
mainfrom
refactor/ops-hal-restructure
May 31, 2026
Merged

Flink-ddd merged 5 commits into
mainfrom
refactor/ops-hal-restructure

Conversation

@Flink-ddd

Copy link
Copy Markdown
Collaborator

Purpose

Resolves #36

Motivation:
As Kernel-Align expands to support multiple heterogeneous backends (NVIDIA CUDA, AMD ROCm/Aiter, Triton), jamming all operator wrappers into a single cuda.py or pytorch.py file will lead to severe management issues and merge conflicts. To ensure the project remains scalable, clean, and cross-platform ready, we introduced a Hardware Abstraction Layer in the Python wrapper layer.

Architectural Changes:

  1. Directory Symmetry: Restructured rl_engine/kernels/ops/ into isolated sub-packages for cuda/, rocm/, triton/, and pytorch/.
  2. Sub-domain Categorization: Introduced loss/ and attention/ sub-packages to manage hundreds of future kernels properly (e.g., ops/cuda/loss/logp.py).
  3. Pure PyTorch Ground Truth: Extracted pure PyTorch implementations (e.g., NativeLogpOp) into the pytorch/ backend to serve as the absolute mathematical baseline for C++/Triton unit testing and CPU fallback.
  4. Lazy Dispatching: Updated KernelRegistry to route operators dynamically using module path strings without breaking lazy-loading principles.
  5. Strict CI/CD Enforcement: Added .pre-commit-config.yaml with ruff, black, and isort to strictly enforce code quality and prevent namespace package issues (init.py enforcement).

Test Plan

  1. Verified zero-overhead routing in a non-GPU environment forcing fallback to pure PyTorch backend.
  2. Verified the mathematical correctness of NativeLogpOp (Cross-Entropy gather logic) against mock logits.
  3. Executed the new pre-commit pipeline to ensure all files conform to PEP-8 and Ruff strict standards.

Test Result

1. HAL CPU Fallback & Routing

(kernel-align) kernel-align % python tests/test_cpu_hal.py
WARNING 05-31 16:02:28 [Kernel-Align]: No GPU detected. RL-Engine is falling back to CPU mode.
INFO 05-31 16:02:28 [Kernel-Align]: KernelRegistry initialized for cpu

 [Test 1/2] Testing HAL Registry Routing...
Successfully routed to: NativeLogpOp

 [Test 2/2] Testing NativeLogpOp Math Execution...
Input Logits Shape: torch.Size([2, 3, 4])
Output LogP Shape: torch.Size([2, 3])
Output Values:
tensor([[-0.5542, -0.2677, -0.0397],
        [-0.5110, -0.1638, -0.3408]])

 CPU Fallback & HAL Architecture is working perfectly!

2. pre-commit test

(kernel-align) kernel-align % pre-commit run
trim trailing whitespace.................................................Passed
fix end of files.........................................................Passed
check yaml...............................................................Passed
check for added large files..............................................Passed
ruff.....................................................................Passed
ruff-format..............................................................Passed

@Flink-ddd
Flink-ddd force-pushed the refactor/ops-hal-restructure branch from 0adc945 to e902430 Compare May 31, 2026 08:45
@Flink-ddd

Copy link
Copy Markdown
Collaborator Author

cc @inaniloquentee @bitborne next some PR need trigger completely CI check

@Flink-ddd
Flink-ddd merged commit d2c55f2 into main May 31, 2026
4 checks passed
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.

[REFACTOR][kernels]: Implement Hardware Abstraction Layer for Operator Wrappers

1 participant