Skip to content

Allow CTMRG methods to use an allocator - #482

Open
kshyatt wants to merge 25 commits into
mainfrom
ksh/ctmrg_cache
Open

kshyatt wants to merge 25 commits into
mainfrom
ksh/ctmrg_cache

Conversation

@kshyatt

@kshyatt kshyatt commented Oct 30, 2026

Copy link
Copy Markdown
Member

Needs the TensorOperations branch merged and tagged first, also happy if people want this allocator to be a kwarg. I think the scheduler should also possibly be a kwarg at least at top level? Anyway starting CI just to see how much works.

@kshyatt
kshyatt requested review from leburgel and lkdvos October 30, 2026 18:04

@lkdvos lkdvos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To give some information about the decision process of what I did in MPSKit:

The idea was that since allocators have state and it is important to not make them long-lived (don't accidentally hold on to the large buffers), nor incorrectly multithread with them (these are absolutely not thread-safe), I basically decided to not really expose that as part of the algorithms, to avoid mis-use. The claim being that there really isn't a good reason to not use the BufferAllocator, and you should be protected from memory corruption etc. In other words, this is not something you can configure in MPSKit.

As a result, within the body of the toplevel/outward-facing functions I simply create the bufferallocators, and then pass that down to all internal functions. In that sense it doesn't really matter all that much if it is keyword or regular argument, and decided this based on what "looked most natural" given the other arguments/keywords.

I agree that in principle you could reuse buffers between different runs of an algorithm, but this is ultimately just the cost of allocating/deallocating a small amount of large vectors, and I don't think we are at the point where that overhead really is dominant.

I do actually still think this might be the correct initial approach, it is not too hard to change afterwards and already gets the benefits, so I'd recommend/suggest following a similar approach here. As far as I can tell, this is exactly what you did, so honestly looks like a great PR to me.

Any chance we can get some benchmarks on this? I'd love to see also how much the CPU version improves from this, and how much of the allocations are still left. Otherwise for me this is good to go, of course with the caveat that we release the TensorOperations version

@kshyatt

kshyatt commented Oct 30, 2026

Copy link
Copy Markdown
Member Author

Let me first make sure CI passes 😅 but then yes benchmarks for sure! I think we should allow the scheduler to be settable though, rather than hardcoded.

@kshyatt

kshyatt commented Oct 31, 2026

Copy link
Copy Markdown
Member Author

this is ultimately just the cost of allocating/deallocating a small amount of large vectors, and I don't think we are at the point where that overhead really is dominant.

For AMD it might still be because they have no warm pool there, but we can benchmark that

@kshyatt

kshyatt commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

Doing some benchmarking now. This needs QuantumKitHub/TensorKit.jl#608 (or something like it) and a TK release, as well as the TO release (which is not yet tagged)

@kshyatt

kshyatt commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

One annoying complication I hit was that there's a huge slowdown on consumer cards like the 5090 I'm testing on for double precision. This won't be a problem on real HPC hardware but it is annoying. I'll try to get some fp32 benchmarks so we can get a rough idea.

@kshyatt
kshyatt marked this pull request as ready for review November 5, 2026 14:04
Comment thread src/algorithms/ctmrg/initialization.jl Resolved
@@ -131,24 +131,24 @@ function renormalize_simultaneously(enlarged_corners, projectors, network, env)
dtmap!!(corners_edges′, coordinates) do (dir, r, c)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at this, it might make sense to create some new primitive for threaded maps with allocators, since this is quite easy to accidentally pass a wrong allocator that would break. It's also not that simple to assert the allocator is correct so I'd be slightly more comfortable if we could end up fixing that in one spot. I'm not claiming this has to be in this PR though, opening an issue might actually already suffice

Comment thread Project.toml Resolved
@kshyatt
kshyatt force-pushed the ksh/ctmrg_cache branch 3 times, most recently from 09d0419 to ff417d8 Compare November 6, 2026 10:26
@kshyatt

kshyatt commented Nov 7, 2026

Copy link
Copy Markdown
Member Author

Some benchmarking info: this + using CUDABufferAllocator cuts C4v CTMRG runtimes by up to 22% per iteration (D = 4, chi = 128), even in the worst case (D = 2, chi = 1024) there's a 5% gain.

@kshyatt
kshyatt force-pushed the ksh/ctmrg_cache branch 2 times, most recently from 6292e53 to 53408c5 Compare November 8, 2026 17:14
@Yue-Zhengyuan

Copy link
Copy Markdown
Member

Since CTMRG gauge fixing will also get allocator support, do we want to merge this after or before #320 which does a big refactor of gauge fixing?

@kshyatt

kshyatt commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

My personal preference is to merge this ASAP as for the CUDA runs, it cuts memory usage by like 2/3rds. Would that be too much of a hurdle for your PR? Fine with me if #320 wins out in the end.

@kshyatt kshyatt changed the title [WIP] Allow CTMRG methods to use an allocator Allow CTMRG methods to use an allocator Nov 9, 2026
Comment thread src/algorithms/ctmrg/ctmrg.jl Resolved
@Yue-Zhengyuan

Copy link
Copy Markdown
Member

@kshyatt I'm ok to follow up immediately; it might be better to first resolve issues on allocators and parallel threads here.

@kshyatt

kshyatt commented Nov 10, 2026

Copy link
Copy Markdown
Member Author

Some benchmarking info: this + using CUDABufferAllocator cuts C4v CTMRG runtimes by up to 22% per iteration (D = 4, chi = 128), even in the worst case (D = 2, chi = 1024) there's a 5% gain.

It also cuts memory usage by 2/3rds :D

@Yue-Zhengyuan

Copy link
Copy Markdown
Member

How's the saving on CPU?

@kshyatt

kshyatt commented Nov 10, 2026

Copy link
Copy Markdown
Member Author

@Yue-Zhengyuan

CPU memory: Default vs Buffer vs Manual (one Julia thread, sequential CTMRG)

Case χ Default alloc/iter Buffer alloc/iter Manual alloc/iter Buffer size
J1-J2 dense 32 2084 MiB 651 MiB 651 MiB 20 MiB
J1-J2 dense 64 8312 MiB 2584 MiB 2584 MiB 81 MiB
J1-J2 U(1) 32 426 MiB 147 MiB 147 MiB 7 MiB
J1-J2 U(1) 64 1682 MiB 545 MiB 545 MiB 26 MiB
J1-J2 U(1) 128 6570 MiB 2088 MiB 2088 MiB 104 MiB

here's what I've got so far

@kshyatt

kshyatt commented Nov 10, 2026

Copy link
Copy Markdown
Member Author

And the SU(2) data:

Heisenberg SU(2), χ Default Buffer Manual Buffer size
32 140 MiB 54 MiB 51 MiB 2 MiB
64 571 MiB 215 MiB 200 MiB 8 MiB
128 2209 MiB 807 MiB 752 MiB 34 MiB

Looks p good!

allocator owned by a single task (e.g. a buffer), any other scheduler gets an allocator
that can be shared between tasks; see `MPSKit.default_allocator`.
"""
scheduler_allocator(x) = default_allocator(x, Defaults.scheduler[])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably works indeed, although it is still slightly uncomfortable to me that we select the allocator so far up the call stack while using the scheduler much more down. Also, I think this means that in many cases we will just be using the manual allocator, which is a bit cheating in the benchmarks because that just hides the allocations and calls malloc manually. Ultimately, this requires a buffer pool or something similar to really fix, but that probably shouldn't be holding up this PR

@kshyatt

kshyatt commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

@leburgel I think the GPU fails are going to be fixed by QuantumKitHub/TensorKit.jl#637. What do you think about me adding that to [sources], doing another run to check, then removing it and merging if so?

@leburgel

Copy link
Copy Markdown
Member

Sure, sounds good to me.

@kshyatt

kshyatt commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

Sorry that stuff keeps breaking. It's apparently harder to keep multiple PRs in sync than to prove GRH...

This branch has not been deployed

No deployments
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.

4 participants