Skip to content

ggml-cpu : implement Kronecker and clean up SYCL test special-casing - #28490

Open
philip-jingxin wants to merge 1 commit into
ggml-org:masterfrom
philip-jingxin:fix/remove-special-casing-test
Open

philip-jingxin wants to merge 1 commit into
ggml-org:masterfrom
philip-jingxin:fix/remove-special-casing-test

Conversation

@philip-jingxin

Copy link
Copy Markdown
Contributor

Overview

Follow-up to #28254.
Implemented Kronecker product to support non-power-of-two dimensions on cpu.

Additional information

Changes

  • Implemented CPU backend support for the Kronecker product of multiples of 12 and 20.
  • Removed SYCL-specific #ifdef directives in tests/test-backend-ops.cpp. All non power-of-two tests now run on CPU.

Testing & Verification

Environment

  • Device: 13th Gen Intel(R) Core(TM) i9-13900K

Correctness Test

Tested with the following commands:

cmake -B build_cpu -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Release -DLLAMA_FATAL_WARNINGS=ON -DLLAMA_OPENSSL=OFF -DGGML_SCHED_NO_REALLOC=ON -DGGML_METAL=OFF -DGGML_BLAS=OFF .
cmake --build build_cpu --config Debug -j32
./build_cpu/bin/test-backend-ops test -o MUL_MAT_HADAMARD -b CPU

Passed 16/16 verification tests, including non-power-of-two sizes (m= 384, 640, 768, 1280) across various configurations:

Backend 1/1: CPU
  MUL_MAT_HADAMARD(m=384,  n=1,  k=384):  OK
  MUL_MAT_HADAMARD(m=384,  n=32, k=384):  OK
  MUL_MAT_HADAMARD(m=384,  n=4,  k=384):  OK
  MUL_MAT_HADAMARD(m=640,  n=1,  k=640):  OK
  MUL_MAT_HADAMARD(m=640,  n=32, k=640):  OK
  MUL_MAT_HADAMARD(m=768,  n=1,  k=768):  OK
  MUL_MAT_HADAMARD(m=1280, n=1,  k=1280): OK
  16/16 tests passed

Performance Benchmarks

Measured on 13th Gen Intel(R) Core(TM) i9-13900K:

Size Batch Latency (us/run) Throughput
384 1 25.15 us 11.72 GFLOPS
384 32 43.80 us 215.47 GFLOPS
640 1 10.67 us 76.78 GFLOPS
640 32 43.50 us 602.59 GFLOPS
768 1 27.61 us 42.73 GFLOPS
1280 1 9.79 us 334.87 GFLOPS

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES - Gemini was used to debug and ensure the correctness of the implementation.

@github-actions github-actions Bot added testing Everything test related ggml changes relating to the ggml tensor library for machine learning labels Sep 6, 2026

@arthw arthw 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.

It's good job!

Thank you!

@arthw

arthw commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

@philip-jingxin
Please resolve the conflict!

Thank you!

@philip-jingxin

Copy link
Copy Markdown
Contributor Author

@philip-jingxin Please resolve the conflict!

Thank you!

@arthw Resolved. Thanks for reviewing!

@philip-jingxin

Copy link
Copy Markdown
Contributor Author

@ggerganov Restored the missing Hadamard matrix definitions dropped during the recent merge. Cleaned up and ready for review again. Thanks!

@philip-jingxin

Copy link
Copy Markdown
Contributor Author

Note: The CI failures in gpu-rocm and gpu-vulkan appear to be unrelated to this PR (could be some existing issues in ARGSORT)

@arthw arthw added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Sep 14, 2026
@philip-jingxin

philip-jingxin commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor Author

@ggerganov @arthw
The code is merged with the latest master. Conflicts resolved.
The Kronecker operator is now adapted to the new implementation in the master.
Also new mixed precision tests are added to cater for the changes.
All related tests are passed on my local machine.

Details philip@dev:llama.cpp$ ./build_cpu/bin/test-backend-ops test -o MUL_MAT_HADAMARD -b CPU Testing 1 devices

Backend 1/1: CPU
Device description: 13th Gen Intel(R) Core(TM) i9-13900K
Device memory: 62771 MB (62771 MB free)

MUL_MAT_HADAMARD(type_a=f32,type_b=f32,m=128,n=1,k=128,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f32,m=64,n=1,k=64,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f32,m=256,n=1,k=256,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f32,m=512,n=1,k=512,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f32,m=128,n=32,k=128,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f32,m=128,n=4,k=128,bs=[2,3],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f32,m=256,n=512,k=256,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f32,m=32,n=1,k=32,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f32,m=1024,n=1,k=1024,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f32,m=384,n=1,k=384,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f32,m=384,n=32,k=384,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f32,m=384,n=4,k=384,bs=[2,3],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f32,m=768,n=1,k=768,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f32,m=640,n=1,k=640,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f32,m=640,n=32,k=640,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f32,m=1280,n=1,k=1280,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f16,m=64,n=1,k=64,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f16,m=128,n=1,k=128,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f16,m=256,n=1,k=256,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f16,m=512,n=1,k=512,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f16,m=128,n=32,k=128,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f16,m=128,n=4,k=128,bs=[2,3],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f16,m=256,n=512,k=256,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f16,m=384,n=1,k=384,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f16,m=384,n=32,k=384,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f16,m=640,n=1,k=640,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
MUL_MAT_HADAMARD(type_a=f32,type_b=f16,m=640,n=32,k=640,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0): OK
27/27 tests passed
Backend CPU: OK
1/1 backends passed
OK

@arthw

arthw commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

@philip-jingxin
I have approved this PR.
Please resolve the conflicts.

Thank you!

@philip-jingxin

Copy link
Copy Markdown
Contributor Author

@philip-jingxin I have approved this PR. Please resolve the conflicts.

Thank you!

Resolved, thanks for your review!

@Titaniumtown

Copy link
Copy Markdown
Contributor

Seems this PR fell through the cracks. @philip-jingxin can you rebase and make sure this PR is still relevant?

@Titaniumtown Titaniumtown removed the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Oct 14, 2026
@philip-jingxin
philip-jingxin force-pushed the fix/remove-special-casing-test branch from d3a3030 to d3cbd21 Compare October 16, 2026 08:35
@philip-jingxin

Copy link
Copy Markdown
Contributor Author

Seems this PR fell through the cracks. @philip-jingxin can you rebase and make sure this PR is still relevant?

Hi! Thanks for the reminder. I've rebased the branch to the latest master. Also confirmed the PR is still relevant, as it extends the existing power-of-two path to 12×2^k and 20×2^k dimensions.

I rebuilt the code from a clean build directory and ran the CPU tests; all 40 tests passed.

@arthw arthw added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Oct 19, 2026
@arthw

arthw commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

I tested and all related UT cases are passed.

Thank you!

Comment on lines +5337 to +5381
#define P 1.0f
#define N -1.0f

// constant Hadamard matrix via Paley I construction
static constexpr float H12[12][12] = {
{ P, P, P, P, P, P, P, P, P, P, P, P },
{ P, N, P, N, P, P, P, N, N, N, P, N },
{ P, N, N, P, N, P, P, P, N, N, N, P },
{ P, P, N, N, P, N, P, P, P, N, N, N },
{ P, N, P, N, N, P, N, P, P, P, N, N },
{ P, N, N, P, N, N, P, N, P, P, P, N },
{ P, N, N, N, P, N, N, P, N, P, P, P },
{ P, P, N, N, N, P, N, N, P, N, P, P },
{ P, P, P, N, N, N, P, N, N, P, N, P },
{ P, P, P, P, N, N, N, P, N, N, P, N },
{ P, N, P, P, P, N, N, N, P, N, N, P },
{ P, P, N, P, P, P, N, N, N, P, N, N }
};

static constexpr float H20[20][20] = {
{ P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P, P },
{ P, N, P, N, N, P, P, P, P, N, P, N, P, N, N, N, N, P, P, N },
{ P, N, N, P, N, N, P, P, P, P, N, P, N, P, N, N, N, N, P, P },
{ P, P, N, N, P, N, N, P, P, P, P, N, P, N, P, N, N, N, N, P },
{ P, P, P, N, N, P, N, N, P, P, P, P, N, P, N, P, N, N, N, N },
{ P, N, P, P, N, N, P, N, N, P, P, P, P, N, P, N, P, N, N, N },
{ P, N, N, P, P, N, N, P, N, N, P, P, P, P, N, P, N, P, N, N },
{ P, N, N, N, P, P, N, N, P, N, N, P, P, P, P, N, P, N, P, N },
{ P, N, N, N, N, P, P, N, N, P, N, N, P, P, P, P, N, P, N, P },
{ P, P, N, N, N, N, P, P, N, N, P, N, N, P, P, P, P, N, P, N },
{ P, N, P, N, N, N, N, P, P, N, N, P, N, N, P, P, P, P, N, P },
{ P, P, N, P, N, N, N, N, P, P, N, N, P, N, N, P, P, P, P, N },
{ P, N, P, N, P, N, N, N, N, P, P, N, N, P, N, N, P, P, P, P },
{ P, P, N, P, N, P, N, N, N, N, P, P, N, N, P, N, N, P, P, P },
{ P, P, P, N, P, N, P, N, N, N, N, P, P, N, N, P, N, N, P, P },
{ P, P, P, P, N, P, N, P, N, N, N, N, P, P, N, N, P, N, N, P },
{ P, P, P, P, P, N, P, N, P, N, N, N, N, P, P, N, N, P, N, N },
{ P, N, P, P, P, P, N, P, N, P, N, N, N, N, P, P, N, N, P, N },
{ P, N, N, P, P, P, P, N, P, N, P, N, N, N, N, P, P, N, N, P },
{ P, P, N, N, P, P, P, P, N, P, N, P, N, N, N, N, P, P, N, N }
};

#undef P
#undef N

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.

I'm not sure I understand why we need to replicate these matrices in the test-backend-ops?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The test needs to use the same matrices to validate the implementation.

test-backend-ops.cpp is separate from ops.cpp. Put implementations in a shared header doesn't seem to fit the current design.

I did consider putting the matrices in ggml.h. But changing a public header would have a wider impact, especially since these matrices are only used in one place in the implementation. That's why I kept separate copies.

Another option would be generating the matrices in the test using the Paley I construction. However, we still need to make sure the generated matrices exactly match the matrices in the implementation. With small matrices, I think checking two copies of constants is easier than checking the constant against the code.

If we run into more cases like this, we could move the shared numerical constants into a separate header.

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.

The purpose of the test-backend-ops tests is to compare reference CPU implementation with the backend implementation. It should not require these matrices (unless I am missing something?).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There's no known general construction that covers every order where a Hadamard matrix exists. The current master test code only constructs Hadamard matrices of order 2^k. I added H12 and H20 to cover orders 12*2^k and 20*2^k.

This allows the tests to construct reference matrices for all dimension cases currently supported by the CPU and SYCL backends. There's also a CUDA implementation PR in progress.

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.

@ggerganov
Yes, I understand.
Could you guide how to handle such case?
Like:
When add new OP in CPU, where to save the test code?
When/who should add UT case in test-backend-ops.cpp?

Thank you!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @ggerganov, just following up on the discussion above. Could you take another look and advise on the preferred approach for the Hadamard reference tests? Happy to make any necessary changes. Thanks!

@ggerganov ggerganov removed the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Oct 20, 2026

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

ggml changes relating to the ggml tensor library for machine learning testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants