Skip to content

Memory Management for MLIR - #685

Merged
yusheng036 merged 10 commits into
mainfrom
ys/fresh
Oct 6, 2026
Merged

yusheng036 merged 10 commits into
mainfrom
ys/fresh

Conversation

@yusheng036

@yusheng036 yusheng036 commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #636 Fixes #637

@yusheng036

Copy link
Copy Markdown
Collaborator Author

Hi @JoelMathewC @willow-ahrens @kylebd99, i have pushed the memory management model in these branch. Please take a look and let me know if you have any comments or feedback. Thanks!

@yusheng036 yusheng036 self-assigned this Sep 24, 2026
@codspeed

codspeed Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 20 untouched benchmarks
⏩ 17 skipped benchmarks1


Comparing ys/fresh (ca22302) with main (10ff149)

Open in CodSpeed

Footnotes

  1. 17 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@JoelMathewC

Copy link
Copy Markdown
Collaborator

I took a look at this and it should work! Although I'm concerned about the overhead from the python callback.

I do think restricting ownership to either python or MLIR rather than both is a good choice. With the Julia backend we gave Julia full ownership (that's because of reasons that are finch.jl specific). I think Pytorch which lowers to C code has C managing the memory. Could you take a look at why they chose to do that, maybe they had a reason that we might not be seeing right now.

willow-ahrens
willow-ahrens previously approved these changes Sep 27, 2026
@willow-ahrens
willow-ahrens dismissed their stale review September 27, 2026 18:31

needs implementation

@willow-ahrens

Copy link
Copy Markdown
Member

I think we allow frameworks to choose who allocs memory by changing buffers. so the malloc-buffer is handled by C, but the numpy-buffer uses callbacks. we could just have an mlir-buffer and then have mlir support a numpy-buffer with callbacks.

@yusheng036

Copy link
Copy Markdown
Collaborator Author

@willow-ahrens I will make a memref buffer to follow the theme of MLIR and MLIR would own this buffer. MLIR would use callbacks for numpy buffer for the Python end.

@JoelMathewC The problem with giving MLIR full ownership of the buffer is that we currently handle serialized buffer and numpy buffer as two seperate copies of the same address. If the serialized buffer gets realloc without updating the numpy buffer, both would go out of sync and cause segfaults. We fix this error by using a callback.

@yusheng036

Copy link
Copy Markdown
Collaborator Author

Hi everyone,

I have finished this PR. I have made some changes from the previous md. The biggest change was making the slots from immutable to mutable. I made this change because it would support resizing more flexibly. This warrants some refactoring in mlir.py.

This PR enables many things: firstly, we can finally support sparse outputs in mlir (as seen in the test_sparse_update.py). Secondly, mlir supports zero-copy construction of mlir buffer. Thirdly, we have a precise write-up of the MLIR resizing documentation

In this PR, I had to change one of the test as Resize was lowered to an assert. My PR changed Resize so I had to change that specific test. When I was testing for the sparse output, I was returning errors in the loop nodes for incorrect assignment of loop variables so I added a function called loop_carried_name to fix that error.

@yusheng036
yusheng036 marked this pull request as ready for review October 3, 2026 04:34
@yusheng036
yusheng036 merged commit eb70c17 into main Oct 6, 2026
17 checks passed
@yusheng036
yusheng036 deleted the ys/fresh branch October 6, 2026 15:00
Ruchika-2003 pushed a commit that referenced this pull request Oct 9, 2026
* Added dev_doc.md for mlir codegen

* First draft of memref_buffer

* Edit: added sparse output and finished memeory management

* fix description

* Added test for memrefbuffer

* Fin
andystopia pushed a commit that referenced this pull request Oct 13, 2026
* Added dev_doc.md for mlir codegen

* First draft of memref_buffer

* Edit: added sparse output and finished memeory management

* fix description

* Added test for memrefbuffer

* Fin
willow-ahrens added a commit that referenced this pull request Oct 14, 2026
…ngs (#703)

* fixing LRU implementation

* precommit error fixing

* cached_matrix form implemented

* [pre-commit.ci] pre-commit autoupdate (#712)

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.16.9 → v0.16.10](astral-sh/ruff-pre-commit@v0.16.9...v0.16.10)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* `pixi update` and fix new `ty` lints (#710)

* `pixi update`

* Fix type checking errors caught by new version of `ty`.

* Memory Management for MLIR (#685)

* Added dev_doc.md for mlir codegen

* First draft of memref_buffer

* Edit: added sparse output and finished memeory management

* fix description

* Added test for memrefbuffer

* Fin

* Add sponsors section to README (#714)

Added sponsors section with acknowledgments for NSF and Codspeed.

* Update sponsorship section in README.md (#715)

* Fix stale public exports blocking wildcard imports (#686)

* Fix stale public exports blocking wildcard imports

Signed-off-by: 李永祺 <[email protected]>

* Preserve importable public exports and validate the export list

Restore the existing Reflector and element_type exports from their public submodules. Keep unavailable property names out of __all__, normalize its static order and duplicates, and cover availability, uniqueness, and wildcard imports.

Assisted-by: OpenAI Codex (model: GPT-6, autonomous)
Signed-off-by: 李永祺 <[email protected]>

* Keep the requested public export invariant test

Remove the redundant object identity and wildcard import tests, along with their unused imports, as requested in review.

Assisted-by: OpenAI Codex (model: GPT-6, autonomous)
Signed-off-by: 李永祺 <[email protected]>

* Keep Ruff export ordering after rebasing main

Remove the RUF022 suppression and redundant ordering assertion as requested in review. Preserve export availability and uniqueness checks. Validate 3093 public-export and interface tests, with 54 skips, and all repository pre-commit hooks.

Assisted-by: OpenAI Codex (model: GPT-6, autonomous)
Signed-off-by: 李永祺 <[email protected]>

---------

Signed-off-by: 李永祺 <[email protected]>

* precommig

* fix

---------

Signed-off-by: 李永祺 <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Hameer Abbasi <[email protected]>
Co-authored-by: Yu Sheng <[email protected]>
Co-authored-by: Willow Ahrens <[email protected]>
Co-authored-by: Dori <[email protected]>
Co-authored-by: Willow Ahrens <[email protected]>
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.

Avoid .copy() in construct_from_mlir for in-place outputs Create a memory ownership model to fix deserialize_from_mlir

3 participants