Skip to content

Add Python frontend for Neura hardware custom ops#273

Open
YanzhouTang wants to merge 1 commit intocoredac:mainfrom
YanzhouTang:mem_cgra
Open

Add Python frontend for Neura hardware custom ops#273
YanzhouTang wants to merge 1 commit intocoredac:mainfrom
YanzhouTang:mem_cgra

Conversation

@YanzhouTang
Copy link
Collaborator

Summary

  • Introduces python/neura_ops.py as the Python frontend interface for Neura hardware primitives.
  • Programmers can explicitly call custom ops (e.g., torch.ops.neura.gather()) to annotate hardware-specific memory access patterns that standard PyTorch ops cannot express.
  • Custom ops behave identically to standard PyTorch operations at runtime, but are preserved as-is through torch_mlir tracing so that the Neura compiler backend can recognize and lower them to hardware primitives.

Example: neura::gather in hash encoding

  • hash_encode kernel uses indirect addressing (level_embeddings[indices]) for hash-table lookups. After torch_mlir tracing, this becomes aten.index.Tensor, which loses the gather semantics needed by CGRA.
  • With the custom op, programmers write torch.ops.neura.gather(table, indices) instead. torch_mlir preserves it as torch.operator "neura.gather", giving the compiler a clear signal.
  • Refactored trilinear_interpolation to batch all 8 corner indices into a single gather per level (from 8 calls down to 1).

Changes

  1. python/neura_ops.py (new): Registers neura::gather with CPU and Meta implementations, compatible with PyTorch 2.1+.
  2. nerf_kernels.py (new): Imports neura_ops, replaces fancy indexing with torch.ops.neura.gather(), two-phase trilinear interpolation.
  3. compile_hash_encode.py (new): Compilation script that exports clean ~930-line Torch Dialect MLIR with neura.gather preserved.

Verification

  • neura.gather appears exactly 2 times in output MLIR (one per level).
  • aten.index.Tensor fully eliminated.
  • PyTorch-side correctness verified (semantically equivalent to the original implementation).

@YanzhouTang YanzhouTang self-assigned this Feb 23, 2026
@YanzhouTang YanzhouTang added the enhancement New feature or request label Feb 23, 2026
@tancheng
Copy link
Contributor

neura.gather appears exactly 2 times in output MLIR (one per level).

I don't see MLIR output?

PyTorch-side correctness verified (semantically equivalent to the original implementation).

The correctness is based on "semantic", instead of execution, right?

@YanzhouTang
Copy link
Collaborator Author

neura.gather appears exactly 2 times in output MLIR (one per level).

I don't see MLIR output?

MLIR output is generated by running compile_hash_encode.py

PyTorch-side correctness verified (semantically equivalent to the original implementation).

The correctness is based on "semantic", instead of execution, right?

Current correctness verification is semantic-level. Maybe we could register neura.gather in the Neura interpreter, which will enable execution-level correctness verification at the MLIR layer?

@tancheng
Copy link
Contributor

neura.gather appears exactly 2 times in output MLIR (one per level).

I don't see MLIR output?

MLIR output is generated by running compile_hash_encode.py

PyTorch-side correctness verified (semantically equivalent to the original implementation).

The correctness is based on "semantic", instead of execution, right?

Current correctness verification is semantic-level. Maybe we could register neura.gather in the Neura interpreter, which will enable execution-level correctness verification at the MLIR layer?

Then can we have a lit test to run the .py and check its output?

neura.gather in the Neura interpreter

We can do this later (another PR once your design/infra is almost done).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants