- Notifications
You must be signed in to change notification settings - Fork14.2k
Added RISC-V Vector Support for K-Quants and improved the existing intrinsics#3453
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
…e existing dot product function for risc-v.The RVV intrinsics is added for the following quantize row functions quantize_row_q8_0 quantize_row_q8_1The following dot product functions have also been optimized by using LMUL = 1/2 instead of LMUL = 1 ggml_vec_dot_q4_0_q8_0 ggml_vec_dot_q4_1_q8_1 ggml_vec_dot_q5_0_q8_0 ggml_vec_dot_q5_1_q8_1And vector initialization in Q5 by temporary array is also replaced by the vid intrinsicsSigned-off-by: Ahmad Tameem <ahmad.tameem@10xengineers.ai>
This adds RISC-V Vector intrinsics support for the following K_quants functions for both QKK = 256 and QKK = 64 ggml_vec_dot_q2_K_q8_K ggml_vec_dot_q3_K_q8_K ggml_vec_dot_q4_K_q8_K ggml_vec_dot_q5_K_q8_K ggml_vec_dot_q6_K_q8_KSigned-off-by: Ahmad Tameem <ahmad.tameem@10xengineers.ai>
…example* 'master' of github.com:ggerganov/llama.cpp: (24 commits) convert : fix Baichuan2 models by using vocab size in config.json (ggml-org#3299) readme : add project status link ggml : fix build afterggml-org#3329 llm : add Refact model (ggml-org#3329) sync : ggml (conv 1d + 2d updates, UB fixes) (ggml-org#3468) finetune : readme fix typo (ggml-org#3465) ggml : add RISC-V Vector Support for K-Quants and improved the existing intrinsics (ggml-org#3453) main : consistent prefix/suffix coloring (ggml-org#3425) llama : fix session saving/loading (ggml-org#3400) llama : expose model's rope_freq_scale in the API (ggml-org#3418) metal : alibi for arbitrary number of heads (ggml-org#3426) cmake : make LLAMA_NATIVE flag actually use the instructions supported by the processor (ggml-org#3273) Work on the BPE tokenizer (ggml-org#3252) convert : fix vocab size when not defined in hparams (ggml-org#3421) cmake : increase minimum version for add_link_options (ggml-org#3444) CLBlast: Add broadcast support for matrix multiplication (ggml-org#3402) gguf : add BERT, MPT, and GPT-J arch info (ggml-org#3408) gguf : general usability improvements (ggml-org#3409) cmake : make CUDA flags more similar to the Makefile (ggml-org#3420) finetune :fixggml-org#3404 (ggml-org#3437) ...
…ng intrinsics (ggml-org#3453)* Added RVV intrinsics support for Q8 quantize row and also improved the existing dot product function for risc-v.The RVV intrinsics is added for the following quantize row functions quantize_row_q8_0 quantize_row_q8_1The following dot product functions have also been optimized by using LMUL = 1/2 instead of LMUL = 1 ggml_vec_dot_q4_0_q8_0 ggml_vec_dot_q4_1_q8_1 ggml_vec_dot_q5_0_q8_0 ggml_vec_dot_q5_1_q8_1And vector initialization in Q5 by temporary array is also replaced by the vid intrinsicsSigned-off-by: Ahmad Tameem <ahmad.tameem@10xengineers.ai>* Added RVV intrinsics support for k_quantsThis adds RISC-V Vector intrinsics support for the following K_quants functions for both QKK = 256 and QKK = 64 ggml_vec_dot_q2_K_q8_K ggml_vec_dot_q3_K_q8_K ggml_vec_dot_q4_K_q8_K ggml_vec_dot_q5_K_q8_K ggml_vec_dot_q6_K_q8_KSigned-off-by: Ahmad Tameem <ahmad.tameem@10xengineers.ai>---------Signed-off-by: Ahmad Tameem <ahmad.tameem@10xengineers.ai>
grigohas commentedJul 10, 2024
hello, i am doing what you suggested and i have results. I have 2 questions, when i want to run it without vector proccesor in qemu, what comand do i have to run? also , how can i check that those 2 runs are different and the one with vector proccesor working like i wanted to ? sorry, i am new to this |
Tameem-10xE commentedJul 10, 2024
Hi, for running on CPU (scalar) provide the path to risc-v toolchain and then use qemu You can set the seed to get the same results i.e; llama-cli -s (some_seed number) ... More details:RVV article Thank you |
grigohas commentedJul 10, 2024
yeah i read this article but when i do the make you provided , i get an error for "march=native" error and from what i searched on makefile, i have to do RISCV_CROSS_COMPILE=1 RISCV=1 |
Tameem-10xE commentedJul 10, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Sorry yes, I just identified the makefile has been reorder and RISCV=1 is required in current version |
Tameem-10xE commentedJul 10, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
After line 432 in makefile, update the flags for vector version with scalar, i.e MK_CFLAGS += -march=rv64gc -mabi=lp64d and then run qemu with, |
grigohas commentedJul 10, 2024
okay okay, one last question, i use the same seed and i have results for both with vector and without, but the only difference in log print is the print time . with vector is 2-2.5X more than without. Is it correct? |
Tameem-10xE commentedJul 10, 2024
Yes, on qemu the vector emulation time is much slower (the actual reason is not known to me, could be due to qemu has to additionally emulate vector processor with the scalar one or parallel processing issues, and also the log use the real-time for comparison), but this should not be the case with actual RISC-V vector board |
grigohas commentedSep 11, 2024
hello again, i am running llama with vector extension on gem5 but since there isnt something on log to check if vector extension is enabled , how do i know ? |
Tameem-10xE commentedSep 11, 2024
Hi, I’ve submitted a PR (#9442) which will print RISCV_VECT=1 on the terminal, if the vector processor is found. Also, I slightly changed Makefile so it no longer requires a flag for RISC-V vector boards—only RISCV_CROSS_COMPILATION=1 is needed for the emulator (i.e. QEMU). The following is the output from the RISC-V BPI-F3 board with vector support, |
grigohas commentedNov 26, 2024
Hello, i have a question, why the load time is less when rvv is enabled ? load time is the loading time of the model right ? how the rvv affects it ? |
Tameem-10xE commentedNov 27, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Hi, Actually that was due to the quantization happening while loading the weights into memory (Not all weights are quantized, some are done while loading them into memory; specifically check the function quantize_row_q8_0_reference in GGML quants.c file or quantize_row_q4_K), also enabling auto-vectorization can affect the load time. |
grigohas commentedNov 28, 2024
okay but why the quantization affects the loadimg time ? can you explain me what changes with the rvv enabled ? |
Tameem-10xE commentedNov 28, 2024
During model's initialization phase, there could be several reasons, such as auto-vectorization of memory operations by compiler, recalculation, decompression, and data alignment. However, during benchmarking, I noticed the most change due to the quantize_row function, since it was performing some computations before the weights were fully loaded into memory. |
grigohas commentedNov 28, 2024
okay so when the rvv is enabled , whats happening with the quantize_row function and the load time is less ? also if the model is already quantized is there any change ? |
grigohas commentedNov 29, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
when you enable the rvv , the quantize_row function that you mentioned that is perforforming computations doesnt execute ? |
Tameem-10xE commentedNov 30, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Sorry, I last worked on this project a year ago and did not go deeper into how it works except for what I could quantify. I might be mistaken or unclear about which functions are affecting the load time (also many things has been change). I think you should ask in the GitHub discussion or Discord if they have one. I also noticed a reduction in load time on x86 with vector (~5 times), and I think the most probable cause must be compiler auto-vectorization. If not, they may be able to provide the exact reason. |
grigohas commentedDec 2, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Okay, thank you very much. are the weights quantized differently in scalar than with rvv on ? I mean, does it make sense to compare the results of scalar with the rvv on or do they follow a different loading procedure ? |
Tameem-10xE commentedDec 2, 2024
Welcome! |
grigohas commentedJan 2, 2025
Hello again, i am simulating through gem5 a riscv environment and i am running llama-cli with a llama 4b model. When i change the vlen and elen of the rvv to greater than 256bit and 64 bit , the generated phrase i get , it doesnt make sense. Is the llama implementation working only on rvv with vlen=256 and elen=64 bit ? |
Tameem-10xE commentedJan 2, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Hi, What Output did you got? |
grigohas commentedJan 2, 2025
Tameem-10xE commentedJan 2, 2025
..., this could be a bug, I missed something or the second half of the register could be interfering with the output causing undefined or junk text. Sorry, due to other tasks, I will not be able to look into this for now, but meanwhile you can file an issue or ask further it about in RISC-V intrinsic repo. |
grigohas commentedJan 5, 2025
Now that we build llama.cpp with cmake instead of make command , what file do i have to change to build it for riscv but without vector extension ? i changed the -march on makefile but it was built again with the vector extension |
Tameem-10xE commentedJan 6, 2025
You can use this flag -DGGML_RVV=OFF i.e; |
grigohas commentedJan 22, 2025
Hello again. What do you think i can change to fix this bug and the exe can run with higher vlen with correct results? |
Tameem-10xE commentedJan 22, 2025
Usually, this could be due to the vl variable, outer or inner for loop (for (int i = 0; i < nb; i++) not completed or missed edge elements) or vsetvli instruction. Also, I am unsure about GEMM5 simulation, which could be different from the QEMU or RV board I tested on (with 256-bit VLEN). Sorry, I have this in my mind, but I will try to fix this next week. |
grigohas commentedFeb 3, 2025
Any news ? |
Tameem-10xE commentedFeb 3, 2025
Really sorry for the late response. I was inactive last week. |
grigohas commentedFeb 3, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Can you explain how do you run it on qemu ? I just run llama-cli for vlen=512 and i got this generated phrase with q4_k model |
Tameem-10xE commentedFeb 3, 2025
Sorry, I plugged the wrong weights above (need to change my naming convention...). Identified the issue in the function named |
grigohas commentedFeb 3, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I changed the vl to 16 and 32 and it still doesnt work correctly. The same happens for other models too. I tried a q2_k model for 512 and the generated phrase is not correct |





Uh oh!
There was an error while loading.Please reload this page.
Hi,
In#2929, we have added the RISC-V intrinsics for the dot product functions in GGML, this PR improves these existing dot product functions in ggml.c and also adds the new risc-v vector intrinsics for k_quants and row quantize (Q8_0 and Q8_1) functions. Now LLaMa.cpp fully supports to run on RISC-V vector processor with GGUF.
In future, this will enable GGML and LLaMa.cpp to run efficiently on RISC-V hardware with vector support and also open a way to compare its performance with other vector processors like Intel AVX and Arm Neon.
Update: Got access to RISC-V vector board with 8 cores and 4GB RAM, the performance boost is 6-7 times against the scalar version on the same board.
Runining llama.cpp AI model on RVV1.0 vs RISC-V Scalar
The RISC-V Vector intrinsics support is added for the following K_quants functions with both QKK = 256 and QKK = 64 block size
The RVV intrinsics is also added for the following Q8 quantize row functions
The following dot product functions have also been optimized by using fractional LMUL (i.e. 1/2) instead of LMUL = 1. I am a little skeptical of this since it works correctly but I have noticed some decrease in inference accuracy, which I think could be a problem with my system or weights. Although I prefer to stick with it since it utilizes a much less number of vector registers after product
And finally, the vector initialization in Q5 by the temporary array is also replaced by the vid_v intrinsics
[Compilation]
Ubuntu: 22.10
riscv-toolchain: 2023.07.05 riscv64 linux glibc
To compile it for RISC-V run,
[Directly on RISC-V CPU]
[QEMU]
Note: Running on qemu emulator could be very slow and may take 2-5 minutes per token
Any feedback is welcome, if you have any suggestions or improvements, especially for fractional LMUL change, please share.
Thanks!