-
Notifications
You must be signed in to change notification settings - Fork 3
[tests] remove cuda/hip test duplication with a templated fixture #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
harz05
wants to merge
7
commits into
ML4EP:dev
Choose a base branch
from
harz05:test/gpu-fixtures
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
248fdf0
test: deduplicate cuda and hip tests into a tag templated fixture test
harz05 7d1aa90
build: detect gpu architectures and skip unavailable backends
harz05 57df42c
Merge remote-tracking branch 'upstream/dev' into test/gpu-fixtures
harz05 2405df0
tests: move cpu and gpu tests into unit_test.tpp files and share the …
harz05 6e9728f
tests: identify the gpu backend from the template tag instead of a na…
harz05 e8dd17b
ci: run the cpu tests against both OpenBLAS and MKL
harz05 4730fda
ci: use the sequential MKL threading layer so libmkl_rt needs no Open…
harz05 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| // CPU backend tests, included from test.cc | ||
|
|
||
| static void runCpuTests() { | ||
| std::cout << "\n=== CPU Tests ===\n"; | ||
|
|
||
| alpaka::PlatformCpu platform{}; | ||
| auto dev = alpaka::getDevByIdx(platform, 0u); | ||
| alpaka::Queue<alpaka::DevCpu, alpaka::Blocking> queue{dev}; | ||
| sofieBLAS<alpaka::TagCpuSerial> blas(queue); | ||
|
|
||
| constexpr int M = 4, N = 3, K = 5; | ||
|
|
||
| // Allocate host buffers | ||
| auto hA = alpaka::allocBuf<float, Idx>(dev, static_cast<Idx>(M * K)); | ||
| auto hB = alpaka::allocBuf<float, Idx>(dev, static_cast<Idx>(K * N)); | ||
| auto hC = alpaka::allocBuf<float, Idx>(dev, static_cast<Idx>(M * N)); | ||
| auto hBias = alpaka::allocBuf<float, Idx>(dev, static_cast<Idx>(M * N)); | ||
|
|
||
| float *A = alpaka::getPtrNative(hA); | ||
| float *B = alpaka::getPtrNative(hB); | ||
| float *C = alpaka::getPtrNative(hC); | ||
| float *bias = alpaka::getPtrNative(hBias); | ||
|
|
||
| fillSeq(A, M * K); | ||
| fillSeq(B, K * N, 1.f, 0.5f); | ||
| fillSeq(bias, M * N, 0.1f, 0.1f); | ||
|
|
||
| std::vector<float> ref(M * N); | ||
|
|
||
| // --- matmul NN --- | ||
| fillVal(C, M * N, 0.f); | ||
| blas.matmul('N', 'N', M, N, K, 1.f, hA, hB, 0.f, hC); | ||
| std::copy(C, C + M * N, ref.data()); | ||
| std::fill(ref.begin(), ref.end(), 0.f); | ||
| refMatmul(ref.data(), A, B, M, N, K, 1.f, 0.f, false, false); | ||
| checkClose(C, ref.data(), M * N, "cpu::matmul NN"); | ||
|
|
||
| // --- matmul TN (A^T: K×M physical → M×K logical) --- | ||
| { | ||
| auto hAt = alpaka::allocBuf<float, Idx>(dev, static_cast<Idx>(K * M)); | ||
| float *At = alpaka::getPtrNative(hAt); | ||
| fillSeq(At, K * M); | ||
| fillVal(C, M * N, 0.f); | ||
| blas.matmul('T', 'N', M, N, K, 1.f, hAt, hB, 0.f, hC); | ||
| std::fill(ref.begin(), ref.end(), 0.f); | ||
| refMatmul(ref.data(), At, B, M, N, K, 1.f, 0.f, true, false); | ||
| checkClose(C, ref.data(), M * N, "cpu::matmul TN"); | ||
| } | ||
|
|
||
| // --- matmul NT --- | ||
| { | ||
| auto hBt = alpaka::allocBuf<float, Idx>(dev, static_cast<Idx>(N * K)); | ||
| float *Bt = alpaka::getPtrNative(hBt); | ||
| fillSeq(Bt, N * K, 1.f, 0.5f); | ||
| fillVal(C, M * N, 0.f); | ||
| blas.matmul('N', 'T', M, N, K, 1.f, hA, hBt, 0.f, hC); | ||
| std::fill(ref.begin(), ref.end(), 0.f); | ||
| refMatmul(ref.data(), A, Bt, M, N, K, 1.f, 0.f, false, true); | ||
| checkClose(C, ref.data(), M * N, "cpu::matmul NT"); | ||
| } | ||
|
|
||
| // --- matmul TT --- | ||
| { | ||
| auto hAt = alpaka::allocBuf<float, Idx>(dev, static_cast<Idx>(K * M)); | ||
| auto hBt = alpaka::allocBuf<float, Idx>(dev, static_cast<Idx>(N * K)); | ||
| float *At = alpaka::getPtrNative(hAt); | ||
| float *Bt = alpaka::getPtrNative(hBt); | ||
| fillSeq(At, K * M); | ||
| fillSeq(Bt, N * K, 1.f, 0.5f); | ||
| fillVal(C, M * N, 0.f); | ||
| blas.matmul('T', 'T', M, N, K, 1.f, hAt, hBt, 0.f, hC); | ||
| std::fill(ref.begin(), ref.end(), 0.f); | ||
| refMatmul(ref.data(), At, Bt, M, N, K, 1.f, 0.f, true, true); | ||
| checkClose(C, ref.data(), M * N, "cpu::matmul TT"); | ||
| } | ||
|
|
||
| // --- matmul: alpha scaling --- | ||
| fillVal(C, M * N, 0.f); | ||
| blas.matmul('N', 'N', M, N, K, 2.5f, hA, hB, 0.f, hC); | ||
| std::fill(ref.begin(), ref.end(), 0.f); | ||
| refMatmul(ref.data(), A, B, M, N, K, 2.5f, 0.f, false, false); | ||
| checkClose(C, ref.data(), M * N, "cpu::matmul alpha=2.5"); | ||
|
|
||
| // --- matmul: beta accumulation --- | ||
| fillSeq(C, M * N, 10.f); // pre-fill C | ||
| blas.matmul('N', 'N', M, N, K, 1.f, hA, hB, 0.5f, hC); | ||
| { | ||
| std::vector<float> C0(M * N); | ||
| fillSeq(C0.data(), M * N, 10.f); | ||
| refMatmul(ref.data(), A, B, M, N, K, 1.f, 0.5f, false, false); | ||
| std::copy(C0.begin(), C0.end(), ref.data()); | ||
| refMatmul(ref.data(), A, B, M, N, K, 1.f, 0.5f, false, false); | ||
| } | ||
| checkClose(C, ref.data(), M * N, "cpu::matmul beta=0.5"); | ||
|
|
||
| // --- gemm NN (beta=0, no prior accumulation) --- | ||
| fillVal(C, M * N, 0.f); | ||
| fillSeq(bias, M * N, 0.1f, 0.1f); | ||
| blas.gemm('N', 'N', M, N, K, 1.f, hA, hB, 0.f, hBias, hC); | ||
| std::fill(ref.begin(), ref.end(), 0.f); | ||
| refGemm(ref.data(), A, B, bias, M, N, K, 1.f, 0.f, false, false); | ||
| checkClose(C, ref.data(), M * N, "cpu::gemm NN beta=0"); | ||
|
|
||
| // --- gemm NN (beta=1 accumulation) --- | ||
| fillVal(C, M * N, 0.f); | ||
| blas.gemm('N', 'N', M, N, K, 1.f, hA, hB, 1.f, hBias, hC); | ||
| std::fill(ref.begin(), ref.end(), 0.f); | ||
| refGemm(ref.data(), A, B, bias, M, N, K, 1.f, 1.f, false, false); | ||
| checkClose(C, ref.data(), M * N, "cpu::gemm NN beta=1"); | ||
|
|
||
| // --- gemm TN --- | ||
| { | ||
| auto hAt = alpaka::allocBuf<float, Idx>(dev, static_cast<Idx>(K * M)); | ||
| float *At = alpaka::getPtrNative(hAt); | ||
| fillSeq(At, K * M); | ||
| fillVal(C, M * N, 0.f); | ||
| blas.gemm('T', 'N', M, N, K, 1.f, hAt, hB, 0.f, hBias, hC); | ||
| std::fill(ref.begin(), ref.end(), 0.f); | ||
| refGemm(ref.data(), At, B, bias, M, N, K, 1.f, 0.f, true, false); | ||
| checkClose(C, ref.data(), M * N, "cpu::gemm TN"); | ||
| } | ||
|
|
||
| // --- gemmrelu: all-positive matmul result stays unchanged --- | ||
| { | ||
| // A and B with positive values ensure result is positive before bias | ||
| auto hAp = alpaka::allocBuf<float, Idx>(dev, static_cast<Idx>(M * K)); | ||
| auto hBp = alpaka::allocBuf<float, Idx>(dev, static_cast<Idx>(K * N)); | ||
| auto hBiasp = alpaka::allocBuf<float, Idx>(dev, static_cast<Idx>(M * N)); | ||
| float *Ap = alpaka::getPtrNative(hAp); | ||
| float *Bp = alpaka::getPtrNative(hBp); | ||
| float *biasp = alpaka::getPtrNative(hBiasp); | ||
| fillSeq(Ap, M * K, 0.1f, 0.1f); | ||
| fillSeq(Bp, K * N, 0.1f, 0.1f); | ||
| fillVal(biasp, M * N, 0.f); | ||
| fillVal(C, M * N, 0.f); | ||
| blas.gemmrelu('N', 'N', M, N, K, 1.f, hAp, hBp, 0.f, hBiasp, hC); | ||
| std::fill(ref.begin(), ref.end(), 0.f); | ||
| refGemmRelu(ref.data(), Ap, Bp, biasp, M, N, K, 1.f, 0.f, false, false); | ||
| checkClose(C, ref.data(), M * N, "cpu::gemmrelu all-positive"); | ||
| } | ||
|
|
||
| // --- gemmrelu: negative values clamped to zero --- | ||
| { | ||
| // Use alpha=-1 to force negative results | ||
| auto hBiasz = alpaka::allocBuf<float, Idx>(dev, static_cast<Idx>(M * N)); | ||
| fillVal(alpaka::getPtrNative(hBiasz), M * N, 0.f); | ||
| fillVal(C, M * N, 0.f); | ||
| blas.gemmrelu('N', 'N', M, N, K, -1.f, hA, hB, 0.f, hBiasz, hC); | ||
| std::fill(ref.begin(), ref.end(), 0.f); | ||
| refGemmRelu(ref.data(), A, B, alpaka::getPtrNative(hBiasz), M, N, K, -1.f, | ||
| 0.f, false, false); | ||
| checkClose(C, ref.data(), M * N, "cpu::gemmrelu alpha=-1 (clamped)"); | ||
| } | ||
|
|
||
| // --- gemmrelu with bias --- | ||
| fillVal(C, M * N, 0.f); | ||
| fillSeq(bias, M * N, -5.f, 2.f); | ||
| blas.gemmrelu('N', 'N', M, N, K, 1.f, hA, hB, 0.f, hBias, hC); | ||
| std::fill(ref.begin(), ref.end(), 0.f); | ||
| refGemmRelu(ref.data(), A, B, bias, M, N, K, 1.f, 0.f, false, false); | ||
| checkClose(C, ref.data(), M * N, "cpu::gemmrelu with mixed bias"); | ||
|
|
||
| // --- gemmgelu NN --- | ||
| fillVal(C, M * N, 0.f); | ||
| fillVal(bias, M * N, 0.f); | ||
| blas.gemmgelu('N', 'N', M, N, K, 1.f, hA, hB, 0.f, hBias, hC); | ||
| std::fill(ref.begin(), ref.end(), 0.f); | ||
| refGemmGelu(ref.data(), A, B, bias, M, N, K, 1.f, 0.f, false, false); | ||
| checkClose(C, ref.data(), M * N, "cpu::gemmgelu NN"); | ||
|
|
||
| // --- gemmgelu with bias --- | ||
| fillVal(C, M * N, 0.f); | ||
| fillSeq(bias, M * N, -2.f, 0.5f); | ||
| blas.gemmgelu('N', 'N', M, N, K, 1.f, hA, hB, 0.f, hBias, hC); | ||
| std::fill(ref.begin(), ref.end(), 0.f); | ||
| refGemmGelu(ref.data(), A, B, bias, M, N, K, 1.f, 0.f, false, false); | ||
| checkClose(C, ref.data(), M * N, "cpu::gemmgelu with bias"); | ||
|
|
||
| // --- gemmgelu TN --- | ||
| { | ||
| auto hAt = alpaka::allocBuf<float, Idx>(dev, static_cast<Idx>(K * M)); | ||
| float *At = alpaka::getPtrNative(hAt); | ||
| fillSeq(At, K * M); | ||
| fillVal(C, M * N, 0.f); | ||
| fillVal(bias, M * N, 0.f); | ||
| blas.gemmgelu('T', 'N', M, N, K, 1.f, hAt, hB, 0.f, hBias, hC); | ||
| std::fill(ref.begin(), ref.end(), 0.f); | ||
| refGemmGelu(ref.data(), At, B, bias, M, N, K, 1.f, 0.f, true, false); | ||
| checkClose(C, ref.data(), M * N, "cpu::gemmgelu TN"); | ||
| } | ||
|
|
||
| // --- edge: zero matrix --- | ||
| { | ||
| auto hZ = alpaka::allocBuf<float, Idx>(dev, static_cast<Idx>(M * K)); | ||
| fillVal(alpaka::getPtrNative(hZ), M * K, 0.f); | ||
| fillVal(C, M * N, 99.f); | ||
| fillVal(bias, M * N, 0.f); | ||
| blas.matmul('N', 'N', M, N, K, 1.f, hZ, hB, 0.f, hC); | ||
| std::fill(ref.begin(), ref.end(), 0.f); | ||
| checkClose(C, ref.data(), M * N, "cpu::matmul zero-A"); | ||
| } | ||
|
|
||
| // --- edge: identity-like (square, known result) --- | ||
| { | ||
| constexpr int S = 3; | ||
| auto hI = alpaka::allocBuf<float, Idx>(dev, static_cast<Idx>(S * S)); | ||
| auto hX = alpaka::allocBuf<float, Idx>(dev, static_cast<Idx>(S * S)); | ||
| auto hY = alpaka::allocBuf<float, Idx>(dev, static_cast<Idx>(S * S)); | ||
| float *I = alpaka::getPtrNative(hI); | ||
| float *X = alpaka::getPtrNative(hX); | ||
| float *Y = alpaka::getPtrNative(hY); | ||
| fillVal(I, S * S, 0.f); | ||
| for (int i = 0; i < S; ++i) | ||
| I[i * S + i] = 1.f; | ||
| fillSeq(X, S * S); | ||
| fillVal(Y, S * S, 0.f); | ||
| blas.matmul('N', 'N', S, S, S, 1.f, hI, hX, 0.f, hY); | ||
| checkClose(Y, X, S * S, "cpu::matmul identity×X=X"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.