Skip to content

Generalize quantize_fns for simpler FP16 handling - #1237

Merged
ggerganov merged 4 commits into
ggml-org:masterfrom
sw:type-handling
Jul 5, 2023
Merged

ggerganov merged 4 commits into
ggml-org:masterfrom
sw:type-handling

Conversation

@sw

@sw sw commented Apr 29, 2023 •

Copy link
Copy Markdown
Contributor

ref: ggml-org/ggml#286

This is hopefully in line with the goal of refactoring in the May roadmap.

In my opinion, quantize_fns is too specific: what matters is not whether a type is quantized, but whether it requires a row-wise conversion, either to vec_dot_type for the vector dot-product, or to float for some other operations.

By making this more generic, some specific code paths for FP16 can be eliminated, most importantly ggml_compute_forward_mul_mat_f16_f32.

This is not finished; CUDA and Accelerate may not work, and also the work buffer allocation may be wrong.

@ggerganov

Copy link
Copy Markdown
Member

This is hopefully in line with the goal of refactoring in the May roadmap.

Yes, this looks great

@slaren

slaren commented Apr 29, 2023

Copy link
Copy Markdown
Member

It would be great if mul_mat_f32 could also be unified like this, maybe by making dummy from/to_float functions. I would be happy to move all the cuBLAS mul mat handling to a single mul mat function to simplify this change.

@sw
sw force-pushed the type-handling branch from 508a181 to 7a633bf Compare April 30, 2023 13:21
@sw

sw commented Apr 30, 2023

Copy link
Copy Markdown
Contributor Author

Tried to get the BLAS stuff right as best as I could, at this point I need the help of someone with Accelerate and cuBLAS hardware and experience.

@sw sw added help wanted Needs help from the community refactoring Refactoring labels Apr 30, 2023
@slaren

slaren commented Apr 30, 2023

Copy link
Copy Markdown
Member

@sw I have moved all the cuBLAS mat mul code to a single function that should make your changes here easier, but it depends on #1233. Once that is merged, I will open a PR.

@ggerganov

Copy link
Copy Markdown
Member

The Accelerate path works - tested the perplexity tool for quantized and F16 models

@slaren

slaren commented May 1, 2023 •

Copy link
Copy Markdown
Member

cuBLAS has different paths depending on the mat mul type, so this does not look right currently. #1259 should make this much easier for you since it coalesces all cuBLAS mat mul code in a function call to ggml_cuda_mul_mat such as:

#if defined(GGML_USE_CUBLAS)
    if (ggml_cuda_can_mul_mat(src0, src1, dst)) {
        if (params->ith == 0 && params->type == GGML_TASK_COMPUTE) {
            ggml_cuda_mul_mat(src0, src1, dst, params->wdata, params->wsize);
        }
        return;
    }
#endif

Incidentally, this also means that it is possible to fall back to other BLAS if cuBLAS cannot perform a mat mul, but currently that is never the case, but opens the door to conditional usage of CUDA.

@sw
sw force-pushed the type-handling branch from 7a633bf to 7d58c84 Compare May 1, 2023 17:29
@sw
sw force-pushed the type-handling branch from 7d58c84 to 86503a5 Compare May 12, 2023 11:22
@sw
sw force-pushed the type-handling branch 2 times, most recently from 62c2f37 to 8cb93d6 Compare June 9, 2023 19:16
@ggerganov ggerganov added the high priority Very important issue label Jun 10, 2023
@goerch

goerch commented Jun 27, 2023 •

Copy link
Copy Markdown
Contributor

@sw : now that this is high priority and on the roadmap, do you intend to update this patch? Otherwise I'll consider to look into it once ggml-org/ggml#309 is resolved.

But my means to check non-CPU stuff will be severely limited, too.

@sw
sw force-pushed the type-handling branch from 8cb93d6 to 3770f4f Compare June 30, 2023 12:21
@sw

sw commented Jun 30, 2023

Copy link
Copy Markdown
Contributor Author

I've rebased the branch onto latest master. I'm not particularly happy with the name type_handling though, any suggestions?

@slaren

slaren commented Jun 30, 2023

Copy link
Copy Markdown
Member

How about something like type_traits. In the future, it could be expanded to include other details that currently are spread around everywhere, such as the sizes or the name.

@goerch

goerch commented Jul 2, 2023 •

Copy link
Copy Markdown
Contributor

I've rebased the branch onto latest master. I'm not particularly happy with the name type_handling though, any suggestions?

Sorry, I missed that completely. In my PR against ggml I simply keep the original names until @ggerganov objects. But on to a more important question: I just checked and it seems parts of our changes are not covered by the existing tests. How do you validate your changes?

Edit: it seems we are going with type_traits...

@ggerganov

Copy link
Copy Markdown
Member

@goerch
Yes, I think type_traits is a good suggestion.
I think we should proceed and merge this PR first as it deduplicates the forward matrix multiplication code which is nice.

@sw
Let us know when this is ready for review - it seems in a good shape to me.

@sw
sw force-pushed the type-handling branch from 3770f4f to f9c585f Compare July 2, 2023 19:34
@sw

sw commented Jul 2, 2023

Copy link
Copy Markdown
Contributor Author

Rebased and renamed as suggested. I'll mark this as ready now, but as mentioned before I don't have a wide range of hardware to test the non-Intel stuff.

@sw
sw marked this pull request as ready for review July 2, 2023 19:38
@sw
sw requested review from ggerganov and slaren July 3, 2023 19:23
@ggerganov

Copy link
Copy Markdown
Member

Thanks - will look to merge tomorrow

Comment thread ggml.c Outdated
if (ggml_cuda_can_mul_mat(node->src0, node->src1, node)) {
node->n_tasks = 1; // TODO: this actually is doing nothing
// the threads are still spinning
cur = ggml_cuda_mul_mat_get_wsize(node->src0, node->src1, node);

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 line can be removed as it is no longer necessary. That should fix the cuBLAS build errors.
The function and a few others should also be removed from ggml-cuda.h, but that's a different issue.

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.

You're right, I didn't notice that I introduced a regression there because those builds take so much time. Removed the line and it builds fine but I can't test it on my machine. The failure on macOS on the other hand was already on master but seems to have been fixed in the meantime.

@ggerganov

Copy link
Copy Markdown
Member

I'll rebase now and run a few tests before merging

@ggerganov

Copy link
Copy Markdown
Member

Strange - no idea why the MacOS CI is failing. It works locally both on x86 and arm

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

Will merge this first thing tomorrow and give it some more time for testing and hopefully resolving the CI

@mqy

mqy commented Jul 4, 2023 •

Copy link
Copy Markdown
Contributor

Strange - no idea why the MacOS CI is failing. It works locally both on x86 and arm

If I remember correctly, I've experienced similar situation when bench mul_mat for f16 (the rope case) -- only macOS-latest-cmake fail with Illegal. I'll pull this branch and have a try.

[EDIT] macOS 13.4.1, intel CPU.

cmake .. -DLLAMA_AVX2=OFF  <--- NOTE macOS-latest-make does not disable AVX2
cmake --build . --config Release
./bin/test-quantize-fns    <-- OK
./bin/test-quantize-perf   <-- OK

The OS of macos-latest-cmake:

Operating System
  macOS
  12.6.7
  21G651

@ggerganov
ggerganov merged commit 1b107b8 into ggml-org:master Jul 5, 2023
Comment thread ggml.c
if (node->src1->type != vec_dot_type) {
cur = GGML_TYPE_SIZE[vec_dot_type]*ggml_nelements(node->src1)/GGML_BLCK_SIZE[vec_dot_type];
} else {
GGML_ASSERT(false);

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.

We hit this assert when both src0 and src1 are F32

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.

Pushed the following fix for now: ec326d3

Probably would be best to add a unit test.
Will make an issue

ggerganov added a commit that referenced this pull request Jul 5, 2023
@sw
sw deleted the type-handling branch July 5, 2023 18:09
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
…1237)

* Generalize quantize_fns for simpler FP16 handling

* Remove call to ggml_cuda_mul_mat_get_wsize

* ci : disable FMA for mac os actions

---------

Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
phuongncn pushed a commit to phuongncn/llama.cpp-gx10-dgx-sparks-deepseekv4 that referenced this pull request Apr 28, 2026
…1237)

* Generalize quantize_fns for simpler FP16 handling

* Remove call to ggml_cuda_mul_mat_get_wsize

* ci : disable FMA for mac os actions

---------

Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
phuongncn pushed a commit to phuongncn/llama.cpp-gx10-dgx-sparks-deepseekv4 that referenced this pull request Apr 28, 2026
phuongncn pushed a commit to phuongncn/llama.cpp-gx10-dgx-sparks-deepseekv4 that referenced this pull request Apr 28, 2026
ljubomirj pushed a commit to ljubomirj/llama.cpp that referenced this pull request May 6, 2026
…1237)

* Generalize quantize_fns for simpler FP16 handling

* Remove call to ggml_cuda_mul_mat_get_wsize

* ci : disable FMA for mac os actions

---------

Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
ljubomirj pushed a commit to ljubomirj/llama.cpp that referenced this pull request May 6, 2026
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
…1237)

* Generalize quantize_fns for simpler FP16 handling

* Remove call to ggml_cuda_mul_mat_get_wsize

* ci : disable FMA for mac os actions

---------

Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
…1237)

* Generalize quantize_fns for simpler FP16 handling

* Remove call to ggml_cuda_mul_mat_get_wsize

* ci : disable FMA for mac os actions

---------

Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

help wanted Needs help from the community high priority Very important issue refactoring Refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants