Skip to content

hipblaslt-bench performance improvements - #1202

Merged
pghysels merged 7 commits into
ROCm:developfrom
pghysels:bench_performance
Oct 13, 2024
Merged

pghysels merged 7 commits into
ROCm:developfrom
pghysels:bench_performance

Conversation

@pghysels

@pghysels pghysels commented Oct 3, 2024

Copy link
Copy Markdown
Contributor

This improves performance of hipblaslt-bench when timing a large number of matmuls.

There are 2 main changes

  • Initialization of A, B and C matrices is done on the device instead of the host. This also avoids the hipMemcpy from host to device. But when a host copy is needed for the correctness check, the device matrix is still copied back to the host.
  • A memory pool to reuse memory allocated with hipMalloc/hipMallocManaged or hipHostMalloc. This might slightly increase memory use.

Both changes together significantly improve performance.
I have a test yaml input file with ~500K tests.
With these changes, the code runs in ~42 minutes, compared to 8 hours before (on MI300).

I initially used rocRAND, but then removed this dependency. I can add that back if it is preferred over my own naive random number generator.

This is based on a commit by @bethune-bryant

@jichangjichang

Copy link
Copy Markdown
Collaborator

Do we have the execution time of hipblaslt-test with/wo this PR?

@pghysels

pghysels commented Oct 4, 2024

Copy link
Copy Markdown
Contributor Author

With these changes ./hipblaslt-test took

real    19m58.491s
user    35m32.051s
sys     0m38.180s

while the develop branch took

real    21m12.295s
user    114m49.692s
sys     2m34.752s

On an MI210, 16 OpenMP threads.

bethune-bryant
bethune-bryant previously approved these changes Oct 5, 2024

@bethune-bryant bethune-bryant left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is significantly speeding up my benchmark collection!
Thank you for getting this finished!

@bethune-bryant

Copy link
Copy Markdown
Contributor

Both changes together significantly improve performance. I have a test yaml input file with ~500K tests. With these changes, the code runs in ~42 minutes, compared to 8 hours before (on MI300).

In my testing it basically reduces total benchmark time to the sum of all kernel execution times, effectively removing all overhead.

@pghysels

pghysels commented Oct 7, 2024

Copy link
Copy Markdown
Contributor Author

Sorry, I had to make a few small fixes. But I think this is ready now.

bethune-bryant
bethune-bryant previously approved these changes Oct 7, 2024
@bethune-bryant

Copy link
Copy Markdown
Contributor

@jichangjichang @KKyang
Do y'all have any feedback on this?

@pghysels

pghysels commented Oct 9, 2024

Copy link
Copy Markdown
Contributor Author

Sorry, I got confused about num_batches[i] and block_count.
But I think it is correct now.
Can you please review this logic?

@pghysels

Copy link
Copy Markdown
Contributor Author

Do we have the execution time of hipblaslt-test with/wo this PR?

I can try to address execution time of hipblaslt-test in a follow up PR.
For now we were focused on improving hipblaslt-bench time for timing a large number of matmul operations.

@jichangjichang jichangjichang added the gfx94x Run CI on gfx94x label Oct 11, 2024

@jichangjichang jichangjichang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

lgtm

@pghysels
pghysels merged commit f0953dd into ROCm:develop Oct 13, 2024
cmingch pushed a commit to cmingch/hipBLASLt that referenced this pull request Nov 13, 2024
* Add a memory pool, to reuse hip memory, either from hipMalloc/hipMallocManaged or hipHostMalloc

* Initialize A, B, C on the device
tony-davis added a commit to ROCm/rocm-libraries that referenced this pull request Nov 6, 2026
… them (#14608)

JIRA ID : AIHPBLAS-5352

## Motivation

`hipblaslt-test`, the hipBLASLt test client, keeps the host buffers that
each test frees in a pool, so the next test can reuse them instead of
allocating again. These buffers are pinned (allocated with
`hipHostMalloc`, so the operating system can neither page them out nor
reclaim them), and the pool kept every one until the process exited. On
the gfx1250 hardware test host, which has about 499 GiB of memory, the
93 cases with 2^31- and 2^32-element sizes enabled in #13504 left about
123 GiB pinned in one process. With 4 CI runners each splitting the
suite into 4 parallel test processes, the out-of-memory killer took the
runners down on 10-23 and 10-27. Separately, a failed host allocation
returns a null buffer, and the test then writes through it.

I don't think this stack guarantees the gfx1250 host never runs short.
What it does is make the client hold much less pinned memory, give it
back sooner, and skip a case with the amounts instead of getting killed,
which is the part of the problem the client can fix. If pressure comes
back, the 110 GiB container caps from #14569 keep a kill inside one job,
and putting the lock back (#14982 removes it) is a one-file revert. So
I'm not planning a long soak test before merging.

The pool comes from ROCm/hipBLASLt#1202, which reused allocations to
speed up `hipblaslt-bench` across about 500,000 small matmuls. Reuse
still matters for large buffers: on the test host below, `hipHostMalloc`
takes about 0.18 s per GiB and `hipHostFree` about 0.06 s per GiB.

## Technical Details

- A pooled host buffer of 1 GiB or more (`kHostPoolRetainLimit` in
`d_vector.hpp`) now stays cached only while consecutive tests reuse it.
The pool records the test count when a buffer comes back, and
`HostPoolReleaseListener`, a new hook that Google Test calls after every
test, frees the large buffers that the test just ended did not use. A
run of same-shape cases, such as the four transpose combinations (NN,
NT, TN, TT) of one size, still shares one allocation.
- When a test asks for a large buffer that no cached buffer fits, the
pool first frees the large buffers that earlier tests left, then pins
new memory. This keeps the peak near one test's footprint when the shape
changes.
- A host buffer no longer gets the pool's 20% extra capacity when the
extra would take it to 1 GiB or more (`host_pool_capacity()`), so a
buffer under the limit stays under it. The existing cutoff for skipping
the extra is a quarter of device memory, so on gfx1250 nearly every host
buffer got it.
- `HipHostBuffer::memcheck()` reports a failed pinned allocation, and
`testing_matmul` checks every host buffer after allocating, including
the four temporary buffers a swizzled A or B is built in. A failure now
skips the test with the requested size ("could not allocate N GiB of
pinned host memory").
- A pooled host buffer under 1 GiB is released once 100 consecutive
tests have not reused it (`kHostPoolSmallIdleTests`). The pool reuses a
buffer only for requests between half its size and its size, so without
this a run of shrinking requests kept every earlier buffer pinned.
- In `hipblaslt-bench`, a failed host allocation now exits with a
failure, as a failed device allocation already did, instead of reporting
success.
- Device memory behaves as before. `hipblaslt-bench` does not install
the listener, so it never frees large buffers early; it does see the 20%
change for host buffers of 1 GiB or more. The hipBLASLt library itself
is unchanged.

This is the first of four stacked PRs for AIHPBLAS-5352. #14609 makes
the client's host-memory query respect container memory limits, #14610
runs the memory preflight for every matmul case, and #14982 drops the
host-wide lock in the gfx1250 workflow.

## Test Plan

- `HostMemory_pre_checkin` (new,
`clients/tests/src/host_memory_gtest.cpp`, 6 tests; the `_pre_checkin`
suffix puts it in the standard tier the PR checks run). The tests check
that a large buffer survives one test boundary and is freed after the
next, that a same-shape test reuses it (what the first test wrote is
still there), that a large miss frees two buffers earlier tests left,
that a 1 GiB request gets exactly 1 GiB, that a small buffer is released
after 100 tests that do not reuse it (and kept when one does), and that
a failed allocation leaves a null buffer whose size is reported. A 0.9
GiB request gets exactly 0.9 GiB (the old rule gave it 1.08 GiB, over
the limit). The tests skip, naming the buffer, on a host that cannot pin
that much, and the reuse test skips on a Windows host where the pool
frees its idle buffers before every request. A temporary variant without
the small-buffer aging failed the new test.
- To confirm the tests can fail, I built two temporary variants of the
pool. One undid four behaviors at once (no release at the end of a test,
no release on a miss, the 20% extra restored, `memcheck()` always
succeeding), and it failed 4 of the 5 tests. The other freed large
buffers whether or not a test used them, and it failed 3. Every test
failed under at least one variant.
- The 82 large-memory cases that apply to gfx90a (the five name patterns
from #14545), and the `smoke` and `quick` tiers, on develop and on this
branch, for wall time and peak resident memory.
- Handle leaks: #14714 and #14700 on develop give the matrix layouts,
matmul descriptors and HIP events owners, so an early return no longer
leaks them. To check that with this stack's new early returns, a
preloaded library counted creates and destroys of all three, on the full
stack, over 1,892 matmul cases, once normally and once under a 1 MiB
host limit so that most cases skip at the preflight.
- Adjacent: the `FastCheck_pre_checkin` and
`FastCheckDevice_pre_checkin` suites, including
`memory_shortfall_reclaims_idle_buffers`, which uses the pool's existing
`ReleaseCached()`. `hipblaslt-bench` was built, because it compiles the
non-gtest form of the new check, but not run.

## Test Result

One MI210 (gfx90a) on a shared host with 2.2 TB of memory. This branch's
clients were built against the 10-25 TheRock nightly hipBLASLt gfx90a
device library. Each pair of runs went back to back, but the host was
busy (load average near 300), so the times carry some noise.

| Measurement | develop | this PR |
| --- | --- | --- |
| 82 large-memory cases, wall time | 316.9 s | 313.8 s |
| 82 large-memory cases, peak resident memory | 81.9 GiB | 49.2 GiB |
| 82 large-memory cases, mean resident memory | 66.9 GiB | 16.6 GiB |
| Resident memory after the last large case | 74 GiB | 2 GiB |
| `smoke` + `quick` (9,171 tests), two runs | 66.6 s, 62.0 s | 63.6 s,
64.1 s |

- Large-memory cases: 82 of 82 passed on both. `smoke` + `quick`: 9,169
passed and 2 skipped (`GroupedMatmul.smoke_Fp16NN` and `smoke_Bf16NN`,
which skip on this GPU) on both.
- `HostMemory_pre_checkin`, `FastCheck_pre_checkin` and
`FastCheckDevice_pre_checkin`: 64 of 64 passed.
- A first version that freed every large buffer at the end of each test
took 449.6 s for the large cases against 318.2 s on develop, which is
why the pool keeps a buffer while the next test reuses it.
- Handle leaks: 0 layouts, descriptors or events left, both normally
(13,580 created) and with 409 cases skipped at the preflight (11,235
created).
- With the small-buffer aging, `smoke` + `quick` passed 9,225 tests in
59.3 s with peak resident memory 2.18 GiB.
- gfx1250: this PR alone passed the hardware workflow's hipBLASLt tests
in
[40934683157](https://github.com/ROCm/rocm-libraries/actions/runs/40934683157),
and Archana dispatched the full stack in
[41582794047](https://github.com/ROCm/rocm-libraries/actions/runs/41582794047),
which passed hipBLASLt twice with no kills and no memory skips. Her run
on the rebased stack,
[41717825380](https://github.com/ROCm/rocm-libraries/actions/runs/41717825380),
was clean too. All three kept the host-wide lock; #14982 covers the run
without it.
- Automated GitHub checks are pending.

## Submission Checklist

- [x] Look over the contributing guidelines at
https://github.com/ROCm/TheRock/blob/main/GOVERNANCE.md#pull-requests.

## Risk level

Medium (3/5). Every `hipblaslt-test` case passes through the new
listener and the host pool, so a mistake here would affect the whole
suite. The hipBLASLt library and device behavior are unchanged; only the
test and bench clients change. The bench sees the 20% change near 1 GiB,
and now exits with a failure when a host allocation fails.

## Related

- Work tracking:
[AIHPBLAS-5352](https://amd-hub.atlassian.net/browse/AIHPBLAS-5352),
fixes 1 (pool retention) and 4 (null host buffer) of its list.
- Cause: #13504. CI workaround this work is meant to replace: #14545,
merged on 10-28.
- Where the pool came from: ROCm/hipBLASLt#1202. Windows already clears
the pool on smaller-memory hosts for the same reason: #4793, #6121.

## Device / Architecture Coverage

Host-side test-client code only, with no kernel, library or
kernel-selection change, so it does not depend on the GPU architecture
and the standard PR checks are enough. Measured on gfx90a.

## Flags / Guardrails

None. The 1 GiB threshold is the constant `kHostPoolRetainLimit`.


[AIHPBLAS-5352]:
https://amd-hub.atlassian.net/browse/AIHPBLAS-5352?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gfx94x Run CI on gfx94x

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants