mirror of
https://github.com/ggerganov/whisper.cpp.git
synced 2025-07-04 00:11:12 +02:00
Compare commits
2 Commits
v1.5.5
...
gg/alloc-e
Author | SHA1 | Date | |
---|---|---|---|
66df44b0b7 | |||
c6c94de43a |
@ -28,8 +28,6 @@ COPY .. .
|
|||||||
RUN make
|
RUN make
|
||||||
|
|
||||||
FROM ${BASE_CUDA_RUN_CONTAINER} AS runtime
|
FROM ${BASE_CUDA_RUN_CONTAINER} AS runtime
|
||||||
ENV CUDA_MAIN_VERSION=12.3
|
|
||||||
ENV LD_LIBRARY_PATH /usr/local/cuda-${CUDA_MAIN_VERSION}/compat:$LD_LIBRARY_PATH
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
RUN apt-get update && \
|
RUN apt-get update && \
|
||||||
|
58
.github/workflows/build.yml
vendored
58
.github/workflows/build.yml
vendored
@ -250,64 +250,6 @@ jobs:
|
|||||||
cmake -DWHISPER_SYCL_F16=ON -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx ..
|
cmake -DWHISPER_SYCL_F16=ON -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx ..
|
||||||
cmake --build . --config Release -j $(nproc)
|
cmake --build . --config Release -j $(nproc)
|
||||||
|
|
||||||
windows-msys2:
|
|
||||||
runs-on: windows-latest
|
|
||||||
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
include:
|
|
||||||
- { sys: UCRT64, env: ucrt-x86_64, build: Release }
|
|
||||||
- { sys: CLANG64, env: clang-x86_64, build: Release }
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Clone
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
|
|
||||||
- name: Setup ${{ matrix.sys }}
|
|
||||||
uses: msys2/setup-msys2@v2
|
|
||||||
with:
|
|
||||||
update: true
|
|
||||||
msystem: ${{matrix.sys}}
|
|
||||||
install: >-
|
|
||||||
base-devel
|
|
||||||
mingw-w64-${{matrix.env}}-toolchain
|
|
||||||
mingw-w64-${{matrix.env}}-cmake
|
|
||||||
mingw-w64-${{matrix.env}}-SDL2
|
|
||||||
mingw-w64-${{matrix.env}}-openblas
|
|
||||||
|
|
||||||
- name: Build using make
|
|
||||||
shell: msys2 {0}
|
|
||||||
run: |
|
|
||||||
make -j $(nproc)
|
|
||||||
|
|
||||||
- name: Clean after building using make
|
|
||||||
shell: msys2 {0}
|
|
||||||
run: |
|
|
||||||
make clean
|
|
||||||
|
|
||||||
- name: Build using make w/ OpenBLAS
|
|
||||||
shell: msys2 {0}
|
|
||||||
run: |
|
|
||||||
make WHISPER_OPENBLAS=1 -j $(nproc)
|
|
||||||
|
|
||||||
- name: Build using CMake
|
|
||||||
shell: msys2 {0}
|
|
||||||
run: |
|
|
||||||
cmake -B build
|
|
||||||
cmake --build build --config ${{ matrix.build }} -j $(nproc)
|
|
||||||
|
|
||||||
- name: Clean after building using CMake
|
|
||||||
shell: msys2 {0}
|
|
||||||
run: |
|
|
||||||
rm -rf build
|
|
||||||
|
|
||||||
- name: Build using CMake w/ OpenBLAS
|
|
||||||
shell: msys2 {0}
|
|
||||||
run: |
|
|
||||||
cmake -B build -DWHISPER_OPENBLAS=ON
|
|
||||||
cmake --build build --config ${{ matrix.build }} -j $(nproc)
|
|
||||||
|
|
||||||
windows:
|
windows:
|
||||||
runs-on: windows-latest
|
runs-on: windows-latest
|
||||||
|
|
||||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,7 +6,6 @@
|
|||||||
.vs/
|
.vs/
|
||||||
.vscode/
|
.vscode/
|
||||||
.DS_Store
|
.DS_Store
|
||||||
.vimspector.json
|
|
||||||
|
|
||||||
build/
|
build/
|
||||||
build-coreml/
|
build-coreml/
|
||||||
|
301
AUTHORS
301
AUTHORS
@ -1,301 +0,0 @@
|
|||||||
# date: Tue Apr 9 20:27:03 EEST 2024
|
|
||||||
# this file is auto-generated by scripts/gen-authors.sh
|
|
||||||
|
|
||||||
0/0 <zero@imaskeleton.me>
|
|
||||||
0cc4m <picard12@live.de>
|
|
||||||
0xsourcecode <134374803+0xsourcecode@users.noreply.github.com>
|
|
||||||
AT <manyoso@users.noreply.github.com>
|
|
||||||
Aarni Koskela <akx@iki.fi>
|
|
||||||
Aaron Pham <29749331+aarnphm@users.noreply.github.com>
|
|
||||||
Aaron Taylor <aaron@exphat.com>
|
|
||||||
Abhilash Majumder <30946547+abhilash1910@users.noreply.github.com>
|
|
||||||
Abitofevrything <54505189+abitofevrything@users.noreply.github.com>
|
|
||||||
AfryMask <AfryMask@163.com>
|
|
||||||
Ahmad Bilal <ahmad.bilal@empglabs.com>
|
|
||||||
AidanBeltonS <87009434+AidanBeltonS@users.noreply.github.com>
|
|
||||||
Akash Mahajan <akash7190@gmail.com>
|
|
||||||
Akash Mahajan <akashmjn@stanford.edu>
|
|
||||||
Al Hoang <3811822-hoanga@users.noreply.gitlab.com>
|
|
||||||
Alan <unknown>
|
|
||||||
Aleksander Andrzejewski <18704749+aleksanderandrzejewski@users.noreply.github.com>
|
|
||||||
Alex Azarov <alex@azarov.by>
|
|
||||||
Alex Bacart <13940752+alex-bacart@users.noreply.github.com>
|
|
||||||
Alex Evgrashin <aevgrashin@yandex.ru>
|
|
||||||
Alexandr Graschenkov <alexandr.graschenkov91@gmail.com>
|
|
||||||
Alexandru Mariuti <alex@mariuti.com>
|
|
||||||
Alexey Kharlamov <alexey@kharlamov.biz>
|
|
||||||
Alfredo Montesinos <alfredo.montesinos@g.austincc.edu>
|
|
||||||
Ali Alameh <ali.alameh@isae.edu.lb>
|
|
||||||
Ananta Bastola <anantarajbastola@gmail.com>
|
|
||||||
Andreu Huguet <andreuhuguet@gmail.com>
|
|
||||||
Andrew Huynh <a5thuynh@gmail.com>
|
|
||||||
Andrew S <andrews54757@gmail.com>
|
|
||||||
Andy Maloney <asmaloney@gmail.com>
|
|
||||||
Anton Kostin <masguit42@users.noreply.github.com>
|
|
||||||
Artyom Mezin <psycho.fading@gmail.com>
|
|
||||||
Asad Memon <asad.lionpk@gmail.com>
|
|
||||||
Ashraful Islam <ashraful.meche@gmail.com>
|
|
||||||
AsukaMinato <asukaminato@nyan.eu.org>
|
|
||||||
AustinMroz <austinmroz@utexas.edu>
|
|
||||||
Avik Sengupta <avik@sengupta.net>
|
|
||||||
Bader-eddine Ouaich <49657842+baderouaich@users.noreply.github.com>
|
|
||||||
Baffin Lee <baffinlee@gmail.com>
|
|
||||||
Ben Nortier <bjnortier@gmail.com>
|
|
||||||
Benjamin Heiniger <benjamin.heiniger@bluewin.ch>
|
|
||||||
Bo-Yi Wu <appleboy.tw@gmail.com>
|
|
||||||
Boris Bliznioukov <blib@mail.com>
|
|
||||||
Borislav Stanimirov <b.stanimirov@abv.bg>
|
|
||||||
Brad Murray <59848399+bradmurray-dt@users.noreply.github.com>
|
|
||||||
Brian Murray <brian@bmurray.ca>
|
|
||||||
CRD716 <crd716@gmail.com>
|
|
||||||
Canis Lupus <Canis-UK@users.noreply.github.com>
|
|
||||||
Carolinabanana <140120812+Carolinabanana@users.noreply.github.com>
|
|
||||||
ChangSeok Oh <shivamidow@users.noreply.github.com>
|
|
||||||
Chaoqun <27287694+OpenWaygate@users.noreply.github.com>
|
|
||||||
Chia-Hsiang Cheng <88014292+garychia@users.noreply.github.com>
|
|
||||||
Chidi Williams <williamschidi1@gmail.com>
|
|
||||||
Christian <12550267+iceychris@users.noreply.github.com>
|
|
||||||
Clifford Heath <clifford.heath@gmail.com>
|
|
||||||
Colin <github@whoisc.cc>
|
|
||||||
DGdev91 <DGdev91@users.noreply.github.com>
|
|
||||||
Damian Czaja <trojan295@protonmail.com>
|
|
||||||
Daniel Bevenius <daniel.bevenius@gmail.com>
|
|
||||||
David <dnhkng@gmail.com>
|
|
||||||
David Thorpe <djt@mutablelogic.com>
|
|
||||||
Davidson Francis <davidsondfgl@gmail.com>
|
|
||||||
Dener Stassun <denerstassun@gmail.com>
|
|
||||||
Didzis Gosko <didzis@users.noreply.github.com>
|
|
||||||
Digipom <admin@digipom.com>
|
|
||||||
Dimo <dimo@ieee.org>
|
|
||||||
Dody Suria Wijaya <dodysw@gmail.com>
|
|
||||||
Dr. Tom Murphy VII Ph.D <499244+tom7@users.noreply.github.com>
|
|
||||||
Duncan McConnell <ddmcconnell4@gmail.com>
|
|
||||||
Egor Egorov <me@egorfine.com>
|
|
||||||
Elkana Bardugo <ttv200@gmail.com>
|
|
||||||
Emmanuel Schmidbauer <eschmidbauer@gmail.com>
|
|
||||||
Engininja2 <139037756+Engininja2@users.noreply.github.com>
|
|
||||||
Eric Swanson <eswanson@alloscomp.com>
|
|
||||||
Eric Tendian <erictendian@gmail.com>
|
|
||||||
Erik Scholz <Green-Sky@users.noreply.github.com>
|
|
||||||
Evan Jones <evan.q.jones@gmail.com>
|
|
||||||
Evan Martin <evan.martin@gmail.com>
|
|
||||||
Eve <139727413+netrunnereve@users.noreply.github.com>
|
|
||||||
Evgeny Kuznetsov <evgeny@kuznetsov.md>
|
|
||||||
F1L1P <78918286+F1L1Pv2@users.noreply.github.com>
|
|
||||||
Fangjun Kuang <csukuangfj@gmail.com>
|
|
||||||
Felix <stenbackfelix@gmail.com>
|
|
||||||
Finn Voorhees <finnvoorhees@gmail.com>
|
|
||||||
FlippFuzz <41221030+FlippFuzz@users.noreply.github.com>
|
|
||||||
Gang Chen <goncha@gmail.com>
|
|
||||||
Gavin Cai <gavin1818@hotmail.com>
|
|
||||||
George Hindle <george@georgehindle.com>
|
|
||||||
Georgi Gerganov <ggerganov@gmail.com>
|
|
||||||
GitAritron <103900385+GitAritron@users.noreply.github.com>
|
|
||||||
GiviMAD <GiviMAD@users.noreply.github.com>
|
|
||||||
Gleicon Moraes <gleicon@gmail.com>
|
|
||||||
Gregor Jasny <gjasny@googlemail.com>
|
|
||||||
Guillaume Wenzek <gwenzek@users.noreply.github.com>
|
|
||||||
HY. Kelvin Lee <34256578+hykelvinlee42@users.noreply.github.com>
|
|
||||||
Halalaluyafail3 <55773281+Halalaluyafail3@users.noreply.github.com>
|
|
||||||
Hang <bebound@gmail.com>
|
|
||||||
Herman Semenov <GermanAizek@yandex.ru>
|
|
||||||
Hrishikesh Barman <geekodour@users.noreply.github.com>
|
|
||||||
Ian Bicking <ian@ianbicking.org>
|
|
||||||
Ian Bull <irbull@eclipsesource.com>
|
|
||||||
Ikko Ashimine <eltociear@gmail.com>
|
|
||||||
InconsolableCellist <23345188+InconsolableCellist@users.noreply.github.com>
|
|
||||||
Ismatulla Mansurov <47342870+sapoepsilon@users.noreply.github.com>
|
|
||||||
Ivan Gorin <ivangorin21@gmail.com>
|
|
||||||
JJ <103335846+computerscienceiscool@users.noreply.github.com>
|
|
||||||
Jack Mousseau <jmousseau@users.noreply.github.com>
|
|
||||||
JacobLinCool <jacoblincool@gmail.com>
|
|
||||||
Jakub Ráček <blizzcz@gmail.com>
|
|
||||||
Jared Van Bortel <jared@nomic.ai>
|
|
||||||
Jay Binks <jaybinks@gmail.com>
|
|
||||||
Jhen-Jie Hong <developer@jhen.me>
|
|
||||||
Jhen-Jie Hong <iainst0409@gmail.com>
|
|
||||||
JidongZhang-THU <1119708529@qq.com>
|
|
||||||
Jo Liss <joliss42@gmail.com>
|
|
||||||
Johan <jr.raffin@gmail.com>
|
|
||||||
Johannes Gäßler <johannesg@5d6.de>
|
|
||||||
John Balis <phobossystems@gmail.com>
|
|
||||||
Jonathan Soo <jcsoo@agora.com>
|
|
||||||
Jonno <1160532+razodactyl@users.noreply.github.com>
|
|
||||||
Joonas Pihlajamaa <joonas.pihlajamaa@iki.fi>
|
|
||||||
Jose <34888496+Jerry-Master@users.noreply.github.com>
|
|
||||||
Josh Bleecher Snyder <josharian@gmail.com>
|
|
||||||
Judd <foldl@users.noreply.github.com>
|
|
||||||
Jumper775 <78500318+jumpers775@users.noreply.github.com>
|
|
||||||
Justine Tunney <jtunney@gmail.com>
|
|
||||||
KP Kaiser <kirk@zothcorp.com>
|
|
||||||
Kamilake <exjang0@gmail.com>
|
|
||||||
Kartik Saranathan <278928+Kartiku@users.noreply.github.com>
|
|
||||||
Kasumi <90275229+kasumi-1@users.noreply.github.com>
|
|
||||||
Kawrakow <48489457+ikawrakow@users.noreply.github.com>
|
|
||||||
Kevin Brothaler <admin@digipom.com>
|
|
||||||
Konstantin Zhuravlyov <konstantin.zhuravlyov@amd.com>
|
|
||||||
Kreijstal <rainb@tfwno.gf>
|
|
||||||
Kylin <56434533+KyL0N@users.noreply.github.com>
|
|
||||||
LBlue <153975653+lbluep@users.noreply.github.com>
|
|
||||||
Larry Battle <larry.battle.tech@gmail.com>
|
|
||||||
Laytan Laats <laytanlaats@hotmail.com>
|
|
||||||
Leo Moll <leo.moll@yeasoft.com>
|
|
||||||
Lexevolution <31176843+Lexevolution@users.noreply.github.com>
|
|
||||||
LittleLoli <26589867+WhichWho@users.noreply.github.com>
|
|
||||||
Lucas Zanek <57494138+LucasZNK@users.noreply.github.com>
|
|
||||||
Luis Herrera <herrera-luis@users.noreply.github.com>
|
|
||||||
Lukas Rist <glaslos@gmail.com>
|
|
||||||
M. A. Ali <73258591+MightyStud@users.noreply.github.com>
|
|
||||||
M. Eren Akbiyik <erenakbiyik@gmail.com>
|
|
||||||
Maciek <maciek.mab122@gmail.com>
|
|
||||||
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
|
|
||||||
Martin Warnaar <martinwarnaar@gmail.com>
|
|
||||||
Matheus de Sousa <23645013+keyehzy@users.noreply.github.com>
|
|
||||||
Mathijs de Bruin <mathijs@mathijsfietst.nl>
|
|
||||||
Matija Pevec <mightymatth@users.noreply.github.com>
|
|
||||||
Maximiliano Levi <8160966+maxilevi@users.noreply.github.com>
|
|
||||||
Meng, Hengyu <hengyu.meng@intel.com>
|
|
||||||
Michael Podvitskiy <podvitskiymichael@gmail.com>
|
|
||||||
Michael Rienstra <mrienstra@gmail.com>
|
|
||||||
Mikhail Grigorev <sleuthhound@gmail.com>
|
|
||||||
Mohammadreza Hendiani <hendiani.mohammadreza@gmail.com>
|
|
||||||
Mohit Agarwal <mohit@sdf.org>
|
|
||||||
Murilo Santana <mvrilo@gmail.com>
|
|
||||||
Neil Chudleigh <nchudleigh@users.noreply.github.com>
|
|
||||||
Neo Zhang Jianyu <jianyu.zhang@intel.com>
|
|
||||||
Neuman Vong <neuman.vong@gmail.com>
|
|
||||||
Nicholas Albion <nalbion@yahoo.com>
|
|
||||||
Niels Mayer <Niels.Mayer@gmail.com>
|
|
||||||
Okabintaro <103938900+Okabintaro@users.noreply.github.com>
|
|
||||||
Oleg Sidorov <me@whitebox.io>
|
|
||||||
Oleg Sidorov <oleg@sidorov.nl>
|
|
||||||
Ondrej Kokes <ondrej.kokes@gmail.com>
|
|
||||||
Ouadie EL FAROUKI <ouadie.elfarouki@codeplay.com>
|
|
||||||
Paul Tsochantaris <ptsochantaris@icloud.com>
|
|
||||||
Philipp Zabel <philipp.zabel@gmail.com>
|
|
||||||
Philippe Normand <phil@base-art.net>
|
|
||||||
Przemysław Pawełczyk <przemoc@gmail.com>
|
|
||||||
Qianhe Chen <54462604+chenqianhe@users.noreply.github.com>
|
|
||||||
Radosław Gryta <radek.gryta@gmail.com>
|
|
||||||
Reinforce-II <fate@eastal.com>
|
|
||||||
Reinis Muiznieks <muiznieks.reinis@gmail.com>
|
|
||||||
RelatedTitle <r3latedtitle@gmail.com>
|
|
||||||
RhinoDevel <RhinoDevel@users.noreply.github.com>
|
|
||||||
Rich Jones <miserlou@gmail.com>
|
|
||||||
Robin <robin.xw@hotmail.com>
|
|
||||||
Roddur Dasgupta <roddurd@gmail.com>
|
|
||||||
Roland Rabien <figbug@gmail.com>
|
|
||||||
Rotem Dan <rotemdan@gmail.com>
|
|
||||||
Ryan Hitchman <hitchmanr@gmail.com>
|
|
||||||
Ryan Metcalfe <107415876+RyanMetcalfeInt8@users.noreply.github.com>
|
|
||||||
RyanChang <ftes90015@gmail.com>
|
|
||||||
Sam <49637763+Onlyartist9@users.noreply.github.com>
|
|
||||||
Sam Pullara <spullara@gmail.com>
|
|
||||||
Sanchit Gandhi <93869735+sanchit-gandhi@users.noreply.github.com>
|
|
||||||
Sergio López <slp@sinrega.org>
|
|
||||||
Siddharth Ramakrishnan <srr2141@columbia.edu>
|
|
||||||
Simon Moisselin <simon.moisstoll@gmail.com>
|
|
||||||
Sindre Sorhus <sindresorhus@gmail.com>
|
|
||||||
Slava Primenko <primenko.s@gmail.com>
|
|
||||||
Syahmi Azhar <prsyahmi@gmail.com>
|
|
||||||
Syed Jafri <syedjafri97@gmail.com>
|
|
||||||
Sơn Phan Trung <phantrungson17@gmail.com>
|
|
||||||
Taisei Mima <bhbstar.me@gmail.com>
|
|
||||||
Takeshi Inoue <inoue.takeshi@gmail.com>
|
|
||||||
Tamotsu Takahashi <ttakah+github@gmail.com>
|
|
||||||
Taras Glek <taras@thegp.com>
|
|
||||||
Tauseef Mohiuddin <35351464+tauseefmohammed2@users.noreply.github.com>
|
|
||||||
Thijs Raymakers <thijs@raymakers.nl>
|
|
||||||
Thomas Fitzsimmons <fitzsim@fitzsim.org>
|
|
||||||
Tiago Fassoni <tiagofassoni@users.noreply.github.com>
|
|
||||||
Tienshiao Ma <tienshiao@tienshiao.org>
|
|
||||||
Timothy Cronin <40186632+4imothy@users.noreply.github.com>
|
|
||||||
Tobrun <tobrun.van.nuland@gmail.com>
|
|
||||||
Todd <taf2@users.noreply.github.com>
|
|
||||||
Tong Li <31761981+litongjava@users.noreply.github.com>
|
|
||||||
Topping1 <78745143+Topping1@users.noreply.github.com>
|
|
||||||
Travis Cline <travis.cline@gmail.com>
|
|
||||||
UEXTM.com <84163508+uextm@users.noreply.github.com>
|
|
||||||
Vadim Peretokin <vperetokin@hey.com>
|
|
||||||
Valentin Gosu <1454649+valenting@users.noreply.github.com>
|
|
||||||
Vulcan <93451215+trholding@users.noreply.github.com>
|
|
||||||
WhiteOlivierus <36532695+WhiteOlivierus@users.noreply.github.com>
|
|
||||||
Xiang (Kevin) Li <kevinli020508@gmail.com>
|
|
||||||
Xiao-Yong Jin <jinxiaoyong@gmail.com>
|
|
||||||
XiaotaoChen <chenxiaotao1234@gmail.com>
|
|
||||||
Yajing Tang <phillis@google.com>
|
|
||||||
Yang Shen <aplshenyang@gmail.com>
|
|
||||||
Yunès <jean.baptiste.yunes@free.fr>
|
|
||||||
ZaBlazzingZephyrus <119159668+blazingzephyr@users.noreply.github.com>
|
|
||||||
Zigfrid Zvezdin <ziggerZZ@gmail.com>
|
|
||||||
Zollner <24618122+Zolliner@users.noreply.github.com>
|
|
||||||
ai-at-home <149282006+ai-at-home@users.noreply.github.com>
|
|
||||||
alonfaraj <alonfaraj@gmail.com>
|
|
||||||
andypayne <apayne@gmail.com>
|
|
||||||
ardfork <134447697+ardfork@users.noreply.github.com>
|
|
||||||
automaticcat <daogiatuank54@gmail.com>
|
|
||||||
be-next <jerome.ramette@gmail.com>
|
|
||||||
bert hubert <bert@hubertnet.nl>
|
|
||||||
bmwl <brian.marshall@tolko.com>
|
|
||||||
bobqianic <129547291+bobqianic@users.noreply.github.com>
|
|
||||||
bocytko <bocytko+github@gmail.com>
|
|
||||||
boolemancer <48014766+boolemancer@users.noreply.github.com>
|
|
||||||
boolemancer <boolemancer@gmail.com>
|
|
||||||
bradmit <151883577+bradmit@users.noreply.github.com>
|
|
||||||
brunofaustino <b.fa.amorim@gmail.com>
|
|
||||||
bssrdf <merlintiger@hotmail.com>
|
|
||||||
byte-6174 <88070277+byte-6174@users.noreply.github.com>
|
|
||||||
cdosoftei <ciprian.dosoftei@gmail.com>
|
|
||||||
clach04 <Chris.Clark@actian.com>
|
|
||||||
compilade <113953597+compilade@users.noreply.github.com>
|
|
||||||
conradg <conradjgodfrey@gmail.com>
|
|
||||||
ddpasa <112642920+ddpasa@users.noreply.github.com>
|
|
||||||
denersc <denerstassun@gmail.com>
|
|
||||||
dscripka <dscripka@users.noreply.github.com>
|
|
||||||
duthils <duthils@duthils.net>
|
|
||||||
ecneladis <ecneladis@users.noreply.github.com>
|
|
||||||
faker <nspyia2002@gmail.com>
|
|
||||||
fitzsim <fitzsim@fitzsim.org>
|
|
||||||
fraxy-v <65565042+fraxy-v@users.noreply.github.com>
|
|
||||||
genevera (she/her) <genevera@users.noreply.github.com>
|
|
||||||
geniusnut <geniusnut@gmail.com>
|
|
||||||
greeshmay <greeshmay@gmail.com>
|
|
||||||
hydai <z54981220@gmail.com>
|
|
||||||
iamthad <thadeus.j.fleming@gmail.com>
|
|
||||||
james wolf <contractorwolf@hotmail.com>
|
|
||||||
joecryptotoo <80373433+joecryptotoo@users.noreply.github.com>
|
|
||||||
jorismertz <35079666+jorismertz@users.noreply.github.com>
|
|
||||||
junkfood <69683722+JunkFood02@users.noreply.github.com>
|
|
||||||
jwijffels <jwijffels@bnosac.be>
|
|
||||||
kamranjon <kamranjon@gmail.com>
|
|
||||||
katsu560 <katsu560oo-@docomo.ne.jp>
|
|
||||||
kennethge <57784063+kenneth-ge@users.noreply.github.com>
|
|
||||||
keyehzy <msamuel@aluno.puc-rio.br>
|
|
||||||
leejet <leejet714@gmail.com>
|
|
||||||
litong <31761981+litongjava@users.noreply.github.com>
|
|
||||||
lnyan <lkwq007@gmail.com>
|
|
||||||
m.bell <m.bell@techsmith.com>
|
|
||||||
mkiol <mkiol@users.noreply.github.com>
|
|
||||||
novag <7754358+novag@users.noreply.github.com>
|
|
||||||
pajowu <pajowu@pajowu.de>
|
|
||||||
polarmoon <90010972+polarmoon@users.noreply.github.com>
|
|
||||||
rlapray <lapray.romain@gmail.com>
|
|
||||||
sandrohanea <40202887+sandrohanea@users.noreply.github.com>
|
|
||||||
semiformal-net <84111142+semiformal-net@users.noreply.github.com>
|
|
||||||
shibukazu <61775791+shibukazu@users.noreply.github.com>
|
|
||||||
shikokuchuo <53399081+shikokuchuo@users.noreply.github.com>
|
|
||||||
slaren <slarengh@gmail.com>
|
|
||||||
slashlib <slashlib@users.noreply.github.com>
|
|
||||||
snadampal <87143774+snadampal@users.noreply.github.com>
|
|
||||||
st-gr <38470677+st-gr@users.noreply.github.com>
|
|
||||||
texmex76 <40733439+texmex76@users.noreply.github.com>
|
|
||||||
thefinaldegree <thefinaldegree@gmail.com>
|
|
||||||
trixirt <trix@redhat.com>
|
|
||||||
ulatekh <ulatekh@yahoo.com>
|
|
||||||
undef <undefdev@gmail.com>
|
|
||||||
venkr <venkateshrameshkumar+1@gmail.com>
|
|
||||||
vicalloy <zbirder@gmail.com>
|
|
||||||
xdrudis <xavierdrudis@yahoo.es>
|
|
||||||
zhouwg <6889919+zhouwg@users.noreply.github.com>
|
|
||||||
布客飞龙 <562826179@qq.com>
|
|
||||||
Артём Земляк <azemlyak@smart-consulting.ru>
|
|
157
CMakeLists.txt
157
CMakeLists.txt
@ -1,9 +1,6 @@
|
|||||||
cmake_minimum_required (VERSION 3.5)
|
cmake_minimum_required (VERSION 3.5)
|
||||||
|
|
||||||
# Allow for the creation of solution folders.
|
project(whisper.cpp VERSION 1.5.4)
|
||||||
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
|
|
||||||
|
|
||||||
project(whisper.cpp VERSION 1.5.5)
|
|
||||||
set(SOVERSION 1)
|
set(SOVERSION 1)
|
||||||
|
|
||||||
# Add path to modules
|
# Add path to modules
|
||||||
@ -61,9 +58,6 @@ option(WHISPER_SDL2 "whisper: support for libSDL2" OFF)
|
|||||||
|
|
||||||
option(WHISPER_NO_AVX "whisper: disable AVX" OFF)
|
option(WHISPER_NO_AVX "whisper: disable AVX" OFF)
|
||||||
option(WHISPER_NO_AVX2 "whisper: disable AVX2" OFF)
|
option(WHISPER_NO_AVX2 "whisper: disable AVX2" OFF)
|
||||||
option(WHISPER_NO_AVX512 "whisper: disable AVX512" ON)
|
|
||||||
option(WHISPER_NO_AVX512_VBMI "whisper: disable AVX512-VBMI" ON)
|
|
||||||
option(WHISPER_NO_AVX512_VNNI "whisper: disable AVX512-VNNI" ON)
|
|
||||||
option(WHISPER_NO_FMA "whisper: disable FMA" OFF)
|
option(WHISPER_NO_FMA "whisper: disable FMA" OFF)
|
||||||
option(WHISPER_NO_F16C "whisper: disable F16c" OFF)
|
option(WHISPER_NO_F16C "whisper: disable F16c" OFF)
|
||||||
|
|
||||||
@ -80,12 +74,9 @@ else()
|
|||||||
option(WHISPER_BLAS "whisper: use BLAS libraries" OFF)
|
option(WHISPER_BLAS "whisper: use BLAS libraries" OFF)
|
||||||
option(WHISPER_BLAS_VENDOR "whisper: BLAS library vendor" Generic)
|
option(WHISPER_BLAS_VENDOR "whisper: BLAS library vendor" Generic)
|
||||||
option(WHISPER_OPENBLAS "whisper: prefer OpenBLAS" OFF)
|
option(WHISPER_OPENBLAS "whisper: prefer OpenBLAS" OFF)
|
||||||
option(WHISPER_OPENBLAS_INTERFACE64 "whisper: use OpenBLAS w/ 64-bit interface" OFF)
|
option(WHISPER_CUBLAS "whisper: support for cuBLAS" OFF)
|
||||||
option(WHISPER_CUDA "whisper: support for CUDA" OFF)
|
|
||||||
option(WHISPER_CUBLAS "whisper: support for CUDA (deprecated)" OFF)
|
|
||||||
option(WHISPER_HIPBLAS "whisper: support for hipBLAS" OFF)
|
option(WHISPER_HIPBLAS "whisper: support for hipBLAS" OFF)
|
||||||
option(WHISPER_CLBLAST "whisper: use CLBlast" OFF)
|
option(WHISPER_CLBLAST "whisper: use CLBlast" OFF)
|
||||||
option(WHISPER_MKL "whisper: use Intel Math Kernel Library (MKL)" OFF)
|
|
||||||
option(WHISPER_SYCL "whisper: use SYCL" OFF)
|
option(WHISPER_SYCL "whisper: use SYCL" OFF)
|
||||||
option(WHISPER_SYCL_F16 "whisper: use 16 bit floats for sycl calculations" OFF)
|
option(WHISPER_SYCL_F16 "whisper: use 16 bit floats for sycl calculations" OFF)
|
||||||
endif()
|
endif()
|
||||||
@ -165,8 +156,7 @@ if (APPLE)
|
|||||||
|
|
||||||
set(GGML_SOURCES_METAL ggml-metal.m ggml-metal.h)
|
set(GGML_SOURCES_METAL ggml-metal.m ggml-metal.h)
|
||||||
|
|
||||||
# copy ggml-common.h and ggml-metal.metal to bin directory
|
# copy ggml-metal.metal to bin directory
|
||||||
configure_file(ggml-common.h bin/ggml-common.h COPYONLY)
|
|
||||||
configure_file(ggml-metal.metal bin/ggml-metal.metal COPYONLY)
|
configure_file(ggml-metal.metal bin/ggml-metal.metal COPYONLY)
|
||||||
|
|
||||||
if (WHISPER_METAL_EMBED_LIBRARY)
|
if (WHISPER_METAL_EMBED_LIBRARY)
|
||||||
@ -174,28 +164,19 @@ if (APPLE)
|
|||||||
set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_METAL_EMBED_LIBRARY)
|
set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_METAL_EMBED_LIBRARY)
|
||||||
|
|
||||||
set(METALLIB_SOURCE "${CMAKE_SOURCE_DIR}/ggml-metal.metal")
|
set(METALLIB_SOURCE "${CMAKE_SOURCE_DIR}/ggml-metal.metal")
|
||||||
set(COMMON_HEADER "${CMAKE_SOURCE_DIR}/ggml-common.h")
|
|
||||||
|
|
||||||
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/autogenerated")
|
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/autogenerated")
|
||||||
set(EMBED_METALLIB_ASSEMBLY "${CMAKE_BINARY_DIR}/autogenerated/ggml-embed-metallib.s")
|
set(EMBED_METALLIB_ASSEMBLY "${CMAKE_BINARY_DIR}/autogenerated/ggml-embed-metallib.s")
|
||||||
set(EMBED_METALLIB_SOURCE "${CMAKE_BINARY_DIR}/autogenerated/ggml-metal-combined.metal")
|
|
||||||
|
|
||||||
add_custom_command(
|
|
||||||
OUTPUT ${EMBED_METALLIB_SOURCE}
|
|
||||||
COMMAND sed -e "/^#include \\\"ggml-common.h\\\"/r ${COMMON_HEADER}" -e "/^#include \\\"ggml-common.h\\\"/d" ${METALLIB_SOURCE} > ${EMBED_METALLIB_SOURCE}
|
|
||||||
DEPENDS ${METALLIB_SOURCE} ${COMMON_HEADER}
|
|
||||||
COMMENT "Generating combined Metal library for embedding"
|
|
||||||
)
|
|
||||||
|
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
OUTPUT ${EMBED_METALLIB_ASSEMBLY}
|
OUTPUT ${EMBED_METALLIB_ASSEMBLY}
|
||||||
COMMAND echo ".section __DATA,__ggml_metallib" > ${EMBED_METALLIB_ASSEMBLY}
|
COMMAND echo ".section __DATA,__ggml_metallib" > ${EMBED_METALLIB_ASSEMBLY}
|
||||||
COMMAND echo ".globl _ggml_metallib_start" >> ${EMBED_METALLIB_ASSEMBLY}
|
COMMAND echo ".globl _ggml_metallib_start" >> ${EMBED_METALLIB_ASSEMBLY}
|
||||||
COMMAND echo "_ggml_metallib_start:" >> ${EMBED_METALLIB_ASSEMBLY}
|
COMMAND echo "_ggml_metallib_start:" >> ${EMBED_METALLIB_ASSEMBLY}
|
||||||
COMMAND echo ".incbin \\\"${EMBED_METALLIB_SOURCE}\\\"" >> ${EMBED_METALLIB_ASSEMBLY}
|
COMMAND echo ".incbin \\\"${METALLIB_SOURCE}\\\"" >> ${EMBED_METALLIB_ASSEMBLY}
|
||||||
COMMAND echo ".globl _ggml_metallib_end" >> ${EMBED_METALLIB_ASSEMBLY}
|
COMMAND echo ".globl _ggml_metallib_end" >> ${EMBED_METALLIB_ASSEMBLY}
|
||||||
COMMAND echo "_ggml_metallib_end:" >> ${EMBED_METALLIB_ASSEMBLY}
|
COMMAND echo "_ggml_metallib_end:" >> ${EMBED_METALLIB_ASSEMBLY}
|
||||||
DEPENDS ${EMBED_METALLIB_SOURCE}
|
DEPENDS ${METALLIB_SOURCE}
|
||||||
COMMENT "Generate assembly for embedded Metal library"
|
COMMENT "Generate assembly for embedded Metal library"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -224,82 +205,30 @@ endif()
|
|||||||
if (WHISPER_OPENBLAS)
|
if (WHISPER_OPENBLAS)
|
||||||
set(WHISPER_BLAS_VENDOR "OpenBLAS")
|
set(WHISPER_BLAS_VENDOR "OpenBLAS")
|
||||||
set(WHISPER_BLAS ON)
|
set(WHISPER_BLAS ON)
|
||||||
# BLA_PKGCONFIG_BLAS is supported since CMake 3.25.
|
|
||||||
# FindBLAS.cmake pkg-config logic seems incomplete, because when
|
|
||||||
# BLA_SIZEOF_INTEGER is 8, then it should search for blas64 instead of blas.
|
|
||||||
# blas.pc/blas64.pc are not always provided, so let's be more specific
|
|
||||||
# and go with openblas.pc/openblas64.pc if WHISPER_OPENBLAS is on.
|
|
||||||
if (WHISPER_OPENBLAS_INTERFACE64)
|
|
||||||
set(WHISPER_BLAS_LIB "openblas64")
|
|
||||||
else ()
|
|
||||||
set(WHISPER_BLAS_LIB "openblas")
|
|
||||||
endif ()
|
|
||||||
set(BLA_PKGCONFIG_BLAS ${WHISPER_BLAS_LIB})
|
|
||||||
# OpenBLAS prebuilt libraries for Windows do not have "64" suffix in filename.
|
|
||||||
# (But .pc file has "64" suffix in filename for USE_64BITINT=1 Windows build.)
|
|
||||||
if (MSVC)
|
|
||||||
set(WHISPER_BLAS_LIB "openblas")
|
|
||||||
endif ()
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if (WHISPER_BLAS)
|
if (WHISPER_BLAS)
|
||||||
if (NOT "$ENV{OPENBLAS_PATH}" STREQUAL "")
|
if (WIN32)
|
||||||
if (WHISPER_STATIC)
|
if(DEFINED ENV{OPENBLAS_PATH})
|
||||||
set(WHISPER_BLAS_LIB_PREFIX ${CMAKE_STATIC_LIBRARY_PREFIX})
|
set(BLAS_LIBRARIES $ENV{OPENBLAS_PATH}/lib/libopenblas.dll.a)
|
||||||
set(WHISPER_BLAS_LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX})
|
|
||||||
else ()
|
|
||||||
if (CMAKE_IMPORT_LIBRARY_SUFFIX)
|
|
||||||
set(WHISPER_BLAS_LIB_PREFIX ${CMAKE_IMPORT_LIBRARY_PREFIX})
|
|
||||||
set(WHISPER_BLAS_LIB_SUFFIX ${CMAKE_IMPORT_LIBRARY_SUFFIX})
|
|
||||||
else ()
|
|
||||||
set(WHISPER_BLAS_LIB_PREFIX ${CMAKE_SHARED_LIBRARY_PREFIX})
|
|
||||||
set(WHISPER_BLAS_LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
|
|
||||||
endif ()
|
|
||||||
endif ()
|
|
||||||
# OpenBLAS prebuilt libraries hardcode "lib" prefix in filename even on Windows
|
|
||||||
if (WHISPER_OPENBLAS)
|
|
||||||
set(WHISPER_BLAS_LIB_PREFIX "lib")
|
|
||||||
endif ()
|
|
||||||
message(STATUS "BLAS compatible library path provided")
|
|
||||||
set(BLAS_LIBRARIES "$ENV{OPENBLAS_PATH}/lib/${WHISPER_BLAS_LIB_PREFIX}${WHISPER_BLAS_LIB}${WHISPER_BLAS_LIB_SUFFIX}")
|
|
||||||
message(STATUS "Libraries ${BLAS_LIBRARIES}")
|
message(STATUS "Libraries ${BLAS_LIBRARIES}")
|
||||||
set(BLAS_INCLUDE_DIRS "$ENV{OPENBLAS_PATH}/include")
|
|
||||||
message(STATUS "Include dirs ${BLAS_INCLUDE_DIRS}")
|
|
||||||
if (NOT EXISTS "${BLAS_LIBRARIES}")
|
|
||||||
message(FATAL_ERROR "BLAS library was not found. Environment variable OPENBLAS_PATH misdefined.")
|
|
||||||
endif ()
|
|
||||||
set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_USE_OPENBLAS)
|
set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_USE_OPENBLAS)
|
||||||
include_directories(${BLAS_INCLUDE_DIRS})
|
include_directories($ENV{OPENBLAS_PATH}/include)
|
||||||
set(WHISPER_EXTRA_LIBS ${WHISPER_EXTRA_LIBS} ${BLAS_LIBRARIES})
|
set(WHISPER_EXTRA_LIBS ${WHISPER_EXTRA_LIBS} ${BLAS_LIBRARIES})
|
||||||
else ()
|
else ()
|
||||||
if (WHISPER_STATIC)
|
message(FATAL_ERROR "BLAS library was not found. Environment variable OPENBLAS_PATH not defined.")
|
||||||
# FindBLAS.cmake pkg-config logic seems incomplete, because when
|
|
||||||
# BLA_STATIC is on, then it should use pkg_check_modules_static
|
|
||||||
# instead of pkg_check_modules.
|
|
||||||
# Some manual variable overriding may be necessary if you don't
|
|
||||||
# achieve desired results.
|
|
||||||
set(BLA_STATIC 1)
|
|
||||||
endif ()
|
endif ()
|
||||||
set(BLA_VENDOR ${WHISPER_BLAS_VENDOR})
|
|
||||||
if (WHISPER_OPENBLAS_INTERFACE64)
|
|
||||||
set(BLA_SIZEOF_INTEGER 8)
|
|
||||||
else ()
|
else ()
|
||||||
set(BLA_SIZEOF_INTEGER 4)
|
set(BLA_STATIC 1)
|
||||||
endif()
|
set(BLA_VENDOR ${WHISPER_BLAS_VENDOR})
|
||||||
|
set(BLA_SIZEOF_INTEGER 8)
|
||||||
set(BLA_PREFER_PKGCONFIG 1)
|
set(BLA_PREFER_PKGCONFIG 1)
|
||||||
find_package(BLAS)
|
find_package(BLAS)
|
||||||
|
|
||||||
if(BLAS_FOUND)
|
if(BLAS_FOUND)
|
||||||
message(STATUS "BLAS compatible library found")
|
message(STATUS "BLAS compatible library found")
|
||||||
message(STATUS "Libraries ${BLAS_LIBRARIES}")
|
message(STATUS "Libraries ${BLAS_LIBRARIES}")
|
||||||
if (NOT DEFINED BLAS_INCLUDE_DIRS)
|
find_path(BLAS_INCLUDE_DIRS cblas.h /usr/include/openblas /usr/local/include/openblas $ENV{BLAS_HOME}/include)
|
||||||
if (PKGC_BLAS_FOUND)
|
|
||||||
set(BLAS_INCLUDE_DIRS "${PKGC_BLAS_INCLUDE_DIRS}")
|
|
||||||
else ()
|
|
||||||
find_path(BLAS_INCLUDE_DIRS cblas.h /usr/include/openblas)
|
|
||||||
endif()
|
|
||||||
endif()
|
|
||||||
message(STATUS "Include dirs ${BLAS_INCLUDE_DIRS}")
|
|
||||||
set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_USE_OPENBLAS)
|
set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_USE_OPENBLAS)
|
||||||
include_directories(${BLAS_INCLUDE_DIRS})
|
include_directories(${BLAS_INCLUDE_DIRS})
|
||||||
set(WHISPER_EXTRA_LIBS ${WHISPER_EXTRA_LIBS} ${BLAS_LIBRARIES})
|
set(WHISPER_EXTRA_LIBS ${WHISPER_EXTRA_LIBS} ${BLAS_LIBRARIES})
|
||||||
@ -309,19 +238,7 @@ if (WHISPER_BLAS)
|
|||||||
endif ()
|
endif ()
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
if (WHISPER_MKL)
|
|
||||||
find_package(MKL CONFIG REQUIRED PATHS $ENV{MKLROOT})
|
|
||||||
message(STATUS "Imported oneMKL targets: ${MKL_IMPORTED_TARGETS}")
|
|
||||||
set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_USE_OPENBLAS)
|
|
||||||
set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_BLAS_USE_MKL)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if (WHISPER_CUBLAS)
|
if (WHISPER_CUBLAS)
|
||||||
message(WARNING "WHISPER_CUBLAS is deprecated and will be removed in the future.\nUse WHISPER_CUDA instead")
|
|
||||||
set(WHISPER_CUDA ON)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if (WHISPER_CUDA)
|
|
||||||
cmake_minimum_required(VERSION 3.17)
|
cmake_minimum_required(VERSION 3.17)
|
||||||
|
|
||||||
find_package(CUDAToolkit)
|
find_package(CUDAToolkit)
|
||||||
@ -331,11 +248,9 @@ if (WHISPER_CUDA)
|
|||||||
|
|
||||||
enable_language(CUDA)
|
enable_language(CUDA)
|
||||||
|
|
||||||
file(GLOB GGML_SOURCES_CUDA "ggml-cuda/*.cu")
|
set(GGML_SOURCES_CUDA ggml-cuda.cu ggml-cuda.h)
|
||||||
list(APPEND GGML_SOURCES_CUDA ggml-cuda.h)
|
|
||||||
list(APPEND GGML_SOURCES_CUDA ggml-cuda.cu)
|
|
||||||
|
|
||||||
add_compile_definitions(GGML_USE_CUDA)
|
add_compile_definitions(GGML_USE_CUBLAS)
|
||||||
|
|
||||||
if (WHISPER_STATIC)
|
if (WHISPER_STATIC)
|
||||||
if (WIN32)
|
if (WIN32)
|
||||||
@ -370,7 +285,7 @@ if (WHISPER_HIPBLAS)
|
|||||||
|
|
||||||
if (${hipblas_FOUND} AND ${hip_FOUND})
|
if (${hipblas_FOUND} AND ${hip_FOUND})
|
||||||
message(STATUS "HIP and hipBLAS found")
|
message(STATUS "HIP and hipBLAS found")
|
||||||
add_compile_definitions(GGML_USE_HIPBLAS GGML_USE_CUDA)
|
add_compile_definitions(GGML_USE_HIPBLAS GGML_USE_CUBLAS)
|
||||||
add_library(ggml-rocm OBJECT ggml-cuda.cu ggml-cuda.h)
|
add_library(ggml-rocm OBJECT ggml-cuda.cu ggml-cuda.h)
|
||||||
set_property(TARGET ggml-rocm PROPERTY POSITION_INDEPENDENT_CODE ON)
|
set_property(TARGET ggml-rocm PROPERTY POSITION_INDEPENDENT_CODE ON)
|
||||||
set_source_files_properties(ggml-cuda.cu PROPERTIES LANGUAGE CXX)
|
set_source_files_properties(ggml-cuda.cu PROPERTIES LANGUAGE CXX)
|
||||||
@ -476,31 +391,17 @@ else()
|
|||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
|
||||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /utf-8")
|
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /utf-8")
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /utf-8")
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /utf-8")
|
||||||
if(NOT WHISPER_NO_AVX512)
|
if(NOT WHISPER_NO_AVX2)
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX512")
|
|
||||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /arch:AVX512")
|
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:AVX512")
|
|
||||||
# MSVC has no compile-time flags enabling specific
|
|
||||||
# AVX512 extensions, neither it defines the
|
|
||||||
# macros corresponding to the extensions.
|
|
||||||
# Do it manually.
|
|
||||||
if (NOT WHISPER_NO_AVX512_VBMI)
|
|
||||||
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VBMI__>)
|
|
||||||
add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VBMI__>)
|
|
||||||
endif()
|
|
||||||
if (NOT WHISPER_NO_AVX512_VNNI)
|
|
||||||
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VNNI__>)
|
|
||||||
add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VNNI__>)
|
|
||||||
endif()
|
|
||||||
elseif(NOT WHISPER_NO_AVX2)
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX2")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX2")
|
||||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /arch:AVX2")
|
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /arch:AVX2")
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:AVX2")
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:AVX2")
|
||||||
elseif(NOT WHISPER_NO_AVX)
|
else()
|
||||||
|
if(NOT WHISPER_NO_AVX)
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX")
|
||||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /arch:AVX")
|
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /arch:AVX")
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:AVX")
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /arch:AVX")
|
||||||
endif()
|
endif()
|
||||||
|
endif()
|
||||||
else()
|
else()
|
||||||
if (EMSCRIPTEN)
|
if (EMSCRIPTEN)
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread -s TOTAL_STACK=5242880")
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread -s TOTAL_STACK=5242880")
|
||||||
@ -512,15 +413,6 @@ else()
|
|||||||
if(NOT WHISPER_NO_AVX2)
|
if(NOT WHISPER_NO_AVX2)
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx2")
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx2")
|
||||||
endif()
|
endif()
|
||||||
if(NOT WHISPER_NO_AVX512)
|
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx512f -mavx512cd -mavx512vl -mavx512dq -mavx512bw")
|
|
||||||
endif()
|
|
||||||
if(NOT WHISPER_NO_AVX512_VBMI)
|
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx512vbmi")
|
|
||||||
endif()
|
|
||||||
if(NOT WHISPER_NO_AVX512_VNNI)
|
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx512vnni")
|
|
||||||
endif()
|
|
||||||
if(NOT WHISPER_NO_FMA)
|
if(NOT WHISPER_NO_FMA)
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfma")
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfma")
|
||||||
endif()
|
endif()
|
||||||
@ -607,7 +499,6 @@ if (WHISPER_COREML)
|
|||||||
set_target_properties(${TARGET} PROPERTIES
|
set_target_properties(${TARGET} PROPERTIES
|
||||||
COMPILE_FLAGS "-fobjc-arc"
|
COMPILE_FLAGS "-fobjc-arc"
|
||||||
)
|
)
|
||||||
set_target_properties(${TARGET} PROPERTIES FOLDER "libs")
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if (WHISPER_OPENVINO)
|
if (WHISPER_OPENVINO)
|
||||||
@ -626,7 +517,6 @@ if (WHISPER_OPENVINO)
|
|||||||
set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DWHISPER_USE_OPENVINO)
|
set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DWHISPER_USE_OPENVINO)
|
||||||
|
|
||||||
target_link_libraries(${TARGET} PRIVATE openvino::runtime)
|
target_link_libraries(${TARGET} PRIVATE openvino::runtime)
|
||||||
set_target_properties(${TARGET} PROPERTIES FOLDER "libs")
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -673,10 +563,6 @@ if (WHISPER_OPENVINO)
|
|||||||
target_link_libraries(${TARGET} PRIVATE whisper.openvino)
|
target_link_libraries(${TARGET} PRIVATE whisper.openvino)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if (WHISPER_MKL)
|
|
||||||
target_link_libraries(${TARGET} PUBLIC MKL::MKL)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if (MSVC)
|
if (MSVC)
|
||||||
target_link_libraries(${TARGET} PRIVATE ${WHISPER_EXTRA_LIBS} ${CMAKE_THREAD_LIBS_INIT})
|
target_link_libraries(${TARGET} PRIVATE ${WHISPER_EXTRA_LIBS} ${CMAKE_THREAD_LIBS_INIT})
|
||||||
|
|
||||||
@ -729,7 +615,6 @@ target_compile_definitions(${TARGET} PUBLIC
|
|||||||
)
|
)
|
||||||
|
|
||||||
set_target_properties(${TARGET} PROPERTIES PUBLIC_HEADER "ggml.h;whisper.h")
|
set_target_properties(${TARGET} PROPERTIES PUBLIC_HEADER "ggml.h;whisper.h")
|
||||||
set_target_properties(${TARGET} PROPERTIES FOLDER "libs")
|
|
||||||
|
|
||||||
include(GNUInstallDirs)
|
include(GNUInstallDirs)
|
||||||
|
|
||||||
|
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2023-2024 The ggml authors
|
Copyright (c) 2023 Georgi Gerganov
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
93
Makefile
93
Makefile
@ -144,24 +144,6 @@ ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686 amd64))
|
|||||||
CXXFLAGS += -mavx2
|
CXXFLAGS += -mavx2
|
||||||
endif
|
endif
|
||||||
|
|
||||||
AVX512F_M := $(shell $(CPUINFO_CMD) | grep -iw 'AVX512F')
|
|
||||||
ifneq (,$(AVX512F_M))
|
|
||||||
CFLAGS += -mavx512f -mavx512cd -mavx512vl -mavx512dq -mavx512bw
|
|
||||||
CXXFLAGS += -mavx512f -mavx512cd -mavx512vl -mavx512dq -mavx512bw
|
|
||||||
endif
|
|
||||||
|
|
||||||
AVX512VNNI_M := $(shell $(CPUINFO_CMD) | grep -iwE 'AVX512_VNNI|AVX512VNNI')
|
|
||||||
ifneq (,$(AVX512VNNI_M))
|
|
||||||
CFLAGS += -mavx512vnni
|
|
||||||
CXXFLAGS += -mavx512vnni
|
|
||||||
endif
|
|
||||||
|
|
||||||
AVX512VBMI_M := $(shell $(CPUINFO_CMD) | grep -iw 'AVX512VBMI')
|
|
||||||
ifneq (,$(AVX512VBMI_M))
|
|
||||||
CFLAGS += -mavx512vbmi
|
|
||||||
CXXFLAGS += -mavx512vbmi
|
|
||||||
endif
|
|
||||||
|
|
||||||
FMA_M := $(shell $(CPUINFO_CMD) | grep -iw 'FMA')
|
FMA_M := $(shell $(CPUINFO_CMD) | grep -iw 'FMA')
|
||||||
ifneq (,$(FMA_M))
|
ifneq (,$(FMA_M))
|
||||||
CFLAGS += -mfma
|
CFLAGS += -mfma
|
||||||
@ -228,54 +210,26 @@ ifndef WHISPER_NO_METAL
|
|||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifneq ($(filter-out 0,$(WHISPER_OPENBLAS)),) # OpenBLAS
|
ifdef WHISPER_OPENBLAS
|
||||||
WHISPER_OPENBLAS_INTERFACE64 ?= 0 # use 32-bit interface by default
|
CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/openblas -I/usr/include/openblas
|
||||||
ifneq ($(filter-out 0,$(WHISPER_OPENBLAS_INTERFACE64)),)
|
LDFLAGS += -lopenblas
|
||||||
WHISPER_BLAS_LIB := openblas64
|
|
||||||
else
|
|
||||||
WHISPER_BLAS_LIB := openblas
|
|
||||||
endif
|
|
||||||
ifneq ($(OPENBLAS_PATH),)
|
|
||||||
WHISPER_BLAS_CFLAGS := -I$(OPENBLAS_PATH)/include
|
|
||||||
WHISPER_BLAS_LDFLAGS := -L$(OPENBLAS_PATH)/lib -l$(WHISPER_BLAS_LIB)
|
|
||||||
else
|
|
||||||
WHISPER_BLAS_LIB_PC_EXISTS := $(shell pkg-config --exists $(WHISPER_BLAS_LIB) && echo 1)
|
|
||||||
ifneq ($(filter-out 0,$(WHISPER_BLAS_LIB_PC_EXISTS)),)
|
|
||||||
WHISPER_BLAS_CFLAGS := $(shell pkg-config --cflags $(WHISPER_BLAS_LIB))
|
|
||||||
WHISPER_BLAS_LDFLAGS := $(shell pkg-config --libs $(WHISPER_BLAS_LIB))
|
|
||||||
else
|
|
||||||
WHISPER_BLAS_CFLAGS := -I/usr/include/openblas
|
|
||||||
WHISPER_BLAS_LDFLAGS := -l$(WHISPER_BLAS_LIB)
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
CFLAGS += $(WHISPER_BLAS_CFLAGS) -DGGML_USE_OPENBLAS
|
|
||||||
LDFLAGS += $(WHISPER_BLAS_LDFLAGS)
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifdef WHISPER_CUBLAS
|
ifdef WHISPER_CUBLAS
|
||||||
# WHISPER_CUBLAS is deprecated and will be removed in the future
|
|
||||||
WHISPER_CUDA := 1
|
|
||||||
endif
|
|
||||||
|
|
||||||
ifdef WHISPER_CUDA
|
|
||||||
ifeq ($(shell expr $(NVCC_VERSION) \>= 11.6), 1)
|
ifeq ($(shell expr $(NVCC_VERSION) \>= 11.6), 1)
|
||||||
CUDA_ARCH_FLAG ?= native
|
CUDA_ARCH_FLAG ?= native
|
||||||
else
|
else
|
||||||
CUDA_ARCH_FLAG ?= all
|
CUDA_ARCH_FLAG ?= all
|
||||||
endif
|
endif
|
||||||
|
|
||||||
CFLAGS += -DGGML_USE_CUDA -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/$(UNAME_M)-linux/include
|
CFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/$(UNAME_M)-linux/include
|
||||||
CXXFLAGS += -DGGML_USE_CUDA -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/$(UNAME_M)-linux/include
|
CXXFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/$(UNAME_M)-linux/include
|
||||||
LDFLAGS += -lcuda -lcublas -lculibos -lcudart -lcublasLt -lpthread -ldl -lrt -L/usr/local/cuda/lib64 -L/opt/cuda/lib64 -L$(CUDA_PATH)/targets/$(UNAME_M)-linux/lib -L/usr/lib/wsl/lib
|
LDFLAGS += -lcuda -lcublas -lculibos -lcudart -lcublasLt -lpthread -ldl -lrt -L/usr/local/cuda/lib64 -L/opt/cuda/lib64 -L$(CUDA_PATH)/targets/$(UNAME_M)-linux/lib -L/usr/lib/wsl/lib
|
||||||
WHISPER_OBJ += ggml-cuda.o
|
WHISPER_OBJ += ggml-cuda.o
|
||||||
WHISPER_OBJ += $(patsubst %.cu,%.o,$(wildcard ggml-cuda/*.cu))
|
|
||||||
NVCC = nvcc
|
NVCC = nvcc
|
||||||
NVCCFLAGS = --forward-unknown-to-host-compiler -arch=$(CUDA_ARCH_FLAG)
|
NVCCFLAGS = --forward-unknown-to-host-compiler -arch=$(CUDA_ARCH_FLAG)
|
||||||
|
|
||||||
ggml-cuda/%.o: ggml-cuda/%.cu ggml-cuda/%.cuh ggml.h ggml-common.h ggml-cuda/common.cuh
|
ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
|
||||||
$(NVCC) $(NVCCFLAGS) $(CXXFLAGS) -c $< -o $@
|
|
||||||
|
|
||||||
ggml-cuda.o: ggml-cuda.cu ggml-cuda.h ggml.h ggml-backend.h ggml-backend-impl.h ggml-common.h $(wildcard ggml-cuda/*.cuh)
|
|
||||||
$(NVCC) $(NVCCFLAGS) $(CXXFLAGS) -Wno-pedantic -c $< -o $@
|
$(NVCC) $(NVCCFLAGS) $(CXXFLAGS) -Wno-pedantic -c $< -o $@
|
||||||
endif
|
endif
|
||||||
|
|
||||||
@ -283,18 +237,14 @@ ifdef WHISPER_HIPBLAS
|
|||||||
ROCM_PATH ?= /opt/rocm
|
ROCM_PATH ?= /opt/rocm
|
||||||
HIPCC ?= $(ROCM_PATH)/bin/hipcc
|
HIPCC ?= $(ROCM_PATH)/bin/hipcc
|
||||||
GPU_TARGETS ?= $(shell $(ROCM_PATH)/llvm/bin/amdgpu-arch)
|
GPU_TARGETS ?= $(shell $(ROCM_PATH)/llvm/bin/amdgpu-arch)
|
||||||
CFLAGS += -DGGML_USE_HIPBLAS -DGGML_USE_CUDA
|
CFLAGS += -DGGML_USE_HIPBLAS -DGGML_USE_CUBLAS
|
||||||
CXXFLAGS += -DGGML_USE_HIPBLAS -DGGML_USE_CUDA
|
CXXFLAGS += -DGGML_USE_HIPBLAS -DGGML_USE_CUBLAS
|
||||||
LDFLAGS += -L$(ROCM_PATH)/lib -Wl,-rpath=$(ROCM_PATH)/lib
|
LDFLAGS += -L$(ROCM_PATH)/lib -Wl,-rpath=$(ROCM_PATH)/lib
|
||||||
LDFLAGS += -lhipblas -lamdhip64 -lrocblas
|
LDFLAGS += -lhipblas -lamdhip64 -lrocblas
|
||||||
HIPFLAGS += $(addprefix --offload-arch=,$(GPU_TARGETS))
|
HIPFLAGS += $(addprefix --offload-arch=,$(GPU_TARGETS))
|
||||||
WHISPER_OBJ += ggml-cuda.o
|
WHISPER_OBJ += ggml-cuda.o
|
||||||
WHISPER_OBJ += $(patsubst %.cu,%.o,$(wildcard ggml-cuda/*.cu))
|
|
||||||
|
|
||||||
ggml-cuda/%.o: ggml-cuda/%.cu ggml-cuda/%.cuh ggml.h ggml-common.h ggml-cuda/common.cuh
|
ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
|
||||||
$(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
|
|
||||||
|
|
||||||
ggml-cuda.o: ggml-cuda.cu ggml-cuda.h ggml.h ggml-backend.h ggml-backend-impl.h ggml-common.h $(wildcard ggml-cuda/*.cuh)
|
|
||||||
$(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
|
$(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
|
||||||
endif
|
endif
|
||||||
|
|
||||||
@ -359,13 +309,6 @@ $(info I CC: $(CCV))
|
|||||||
$(info I CXX: $(CXXV))
|
$(info I CXX: $(CXXV))
|
||||||
$(info )
|
$(info )
|
||||||
|
|
||||||
ifdef WHISPER_CUBLAS
|
|
||||||
$(info !!!!)
|
|
||||||
$(info WHISPER_CUBLAS is deprecated and will be removed in the future. Use WHISPER_CUDA instead.)
|
|
||||||
$(info !!!!)
|
|
||||||
$(info )
|
|
||||||
endif
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Build library
|
# Build library
|
||||||
#
|
#
|
||||||
@ -408,19 +351,17 @@ WHISPER_OBJ += ggml-metal.o
|
|||||||
ifdef WHISPER_METAL_EMBED_LIBRARY
|
ifdef WHISPER_METAL_EMBED_LIBRARY
|
||||||
CFLAGS += -DGGML_METAL_EMBED_LIBRARY
|
CFLAGS += -DGGML_METAL_EMBED_LIBRARY
|
||||||
|
|
||||||
ggml-metal-embed.o: ggml-metal.metal ggml-common.h
|
ggml-metal-embed.o: ggml-metal.metal
|
||||||
@echo "Embedding Metal library"
|
@echo "Embedding Metal library"
|
||||||
$(eval TEMP_ASSEMBLY=$(shell mktemp))
|
$(eval TEMP_ASSEMBLY=$(shell mktemp))
|
||||||
$(eval TEMP_METALLIB=$(shell mktemp))
|
|
||||||
@sed "/^#include \"ggml-common.h\"/{r ggml-common.h"$$'\n'"d;}" ggml-metal.metal > $(TEMP_METALLIB)
|
|
||||||
@echo ".section __DATA, __ggml_metallib" > $(TEMP_ASSEMBLY)
|
@echo ".section __DATA, __ggml_metallib" > $(TEMP_ASSEMBLY)
|
||||||
@echo ".globl _ggml_metallib_start" >> $(TEMP_ASSEMBLY)
|
@echo ".globl _ggml_metallib_start" >> $(TEMP_ASSEMBLY)
|
||||||
@echo "_ggml_metallib_start:" >> $(TEMP_ASSEMBLY)
|
@echo "_ggml_metallib_start:" >> $(TEMP_ASSEMBLY)
|
||||||
@echo ".incbin \"$(TEMP_METALLIB)\"" >> $(TEMP_ASSEMBLY)
|
@echo ".incbin \"$<\"" >> $(TEMP_ASSEMBLY)
|
||||||
@echo ".globl _ggml_metallib_end" >> $(TEMP_ASSEMBLY)
|
@echo ".globl _ggml_metallib_end" >> $(TEMP_ASSEMBLY)
|
||||||
@echo "_ggml_metallib_end:" >> $(TEMP_ASSEMBLY)
|
@echo "_ggml_metallib_end:" >> $(TEMP_ASSEMBLY)
|
||||||
@$(AS) $(TEMP_ASSEMBLY) -o $@
|
@$(AS) $(TEMP_ASSEMBLY) -o $@
|
||||||
@rm -f $(TEMP_ASSEMBLY) $(TEMP_METALLIB)
|
@rm -f ${TEMP_ASSEMBLY}
|
||||||
|
|
||||||
WHISPER_OBJ += ggml-metal-embed.o
|
WHISPER_OBJ += ggml-metal-embed.o
|
||||||
endif
|
endif
|
||||||
@ -441,7 +382,7 @@ clean:
|
|||||||
|
|
||||||
CC_SDL=`sdl2-config --cflags --libs`
|
CC_SDL=`sdl2-config --cflags --libs`
|
||||||
|
|
||||||
SRC_COMMON = examples/common.cpp examples/common-ggml.cpp examples/grammar-parser.cpp
|
SRC_COMMON = examples/common.cpp examples/common-ggml.cpp
|
||||||
SRC_COMMON_SDL = examples/common-sdl.cpp
|
SRC_COMMON_SDL = examples/common-sdl.cpp
|
||||||
|
|
||||||
main: examples/main/main.cpp $(SRC_COMMON) $(WHISPER_OBJ)
|
main: examples/main/main.cpp $(SRC_COMMON) $(WHISPER_OBJ)
|
||||||
@ -460,8 +401,8 @@ server: examples/server/server.cpp $(SRC_COMMON) $(WHISPER_OBJ)
|
|||||||
stream: examples/stream/stream.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ)
|
stream: examples/stream/stream.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ)
|
||||||
$(CXX) $(CXXFLAGS) examples/stream/stream.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ) -o stream $(CC_SDL) $(LDFLAGS)
|
$(CXX) $(CXXFLAGS) examples/stream/stream.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ) -o stream $(CC_SDL) $(LDFLAGS)
|
||||||
|
|
||||||
command: examples/command/command.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ)
|
command: examples/command/command.cpp examples/grammar-parser.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ)
|
||||||
$(CXX) $(CXXFLAGS) examples/command/command.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ) -o command $(CC_SDL) $(LDFLAGS)
|
$(CXX) $(CXXFLAGS) examples/command/command.cpp examples/grammar-parser.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ) -o command $(CC_SDL) $(LDFLAGS)
|
||||||
|
|
||||||
lsp: examples/lsp/lsp.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ)
|
lsp: examples/lsp/lsp.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ)
|
||||||
$(CXX) $(CXXFLAGS) examples/lsp/lsp.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ) -o lsp $(CC_SDL) $(LDFLAGS)
|
$(CXX) $(CXXFLAGS) examples/lsp/lsp.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ) -o lsp $(CC_SDL) $(LDFLAGS)
|
||||||
@ -469,8 +410,8 @@ lsp: examples/lsp/lsp.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ)
|
|||||||
talk: examples/talk/talk.cpp examples/talk/gpt-2.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ)
|
talk: examples/talk/talk.cpp examples/talk/gpt-2.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ)
|
||||||
$(CXX) $(CXXFLAGS) examples/talk/talk.cpp examples/talk/gpt-2.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ) -o talk $(CC_SDL) $(LDFLAGS)
|
$(CXX) $(CXXFLAGS) examples/talk/talk.cpp examples/talk/gpt-2.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ) -o talk $(CC_SDL) $(LDFLAGS)
|
||||||
|
|
||||||
talk-llama: examples/talk-llama/talk-llama.cpp examples/talk-llama/llama.cpp examples/talk-llama/unicode.cpp examples/talk-llama/unicode-data.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ)
|
talk-llama: examples/talk-llama/talk-llama.cpp examples/talk-llama/llama.cpp examples/talk-llama/unicode.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ)
|
||||||
$(CXX) $(CXXFLAGS) examples/talk-llama/talk-llama.cpp examples/talk-llama/llama.cpp examples/talk-llama/unicode.cpp examples/talk-llama/unicode-data.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ) -o talk-llama $(CC_SDL) $(LDFLAGS)
|
$(CXX) $(CXXFLAGS) examples/talk-llama/talk-llama.cpp examples/talk-llama/llama.cpp examples/talk-llama/unicode.cpp $(SRC_COMMON) $(SRC_COMMON_SDL) $(WHISPER_OBJ) -o talk-llama $(CC_SDL) $(LDFLAGS)
|
||||||
|
|
||||||
#
|
#
|
||||||
# Audio samples
|
# Audio samples
|
||||||
|
27
README.md
27
README.md
@ -6,7 +6,7 @@
|
|||||||
[](https://opensource.org/licenses/MIT)
|
[](https://opensource.org/licenses/MIT)
|
||||||
[](https://www.npmjs.com/package/whisper.cpp/)
|
[](https://www.npmjs.com/package/whisper.cpp/)
|
||||||
|
|
||||||
Stable: [v1.5.5](https://github.com/ggerganov/whisper.cpp/releases/tag/v1.5.5) / [Roadmap | F.A.Q.](https://github.com/ggerganov/whisper.cpp/discussions/126)
|
Stable: [v1.5.4](https://github.com/ggerganov/whisper.cpp/releases/tag/v1.5.4) / [Roadmap | F.A.Q.](https://github.com/ggerganov/whisper.cpp/discussions/126)
|
||||||
|
|
||||||
High-performance inference of [OpenAI's Whisper](https://github.com/openai/whisper) automatic speech recognition (ASR) model:
|
High-performance inference of [OpenAI's Whisper](https://github.com/openai/whisper) automatic speech recognition (ASR) model:
|
||||||
|
|
||||||
@ -414,11 +414,11 @@ For more information about the Core ML implementation please refer to PR [#1037]
|
|||||||
With NVIDIA cards the processing of the models is done efficiently on the GPU via cuBLAS and custom CUDA kernels.
|
With NVIDIA cards the processing of the models is done efficiently on the GPU via cuBLAS and custom CUDA kernels.
|
||||||
First, make sure you have installed `cuda`: https://developer.nvidia.com/cuda-downloads
|
First, make sure you have installed `cuda`: https://developer.nvidia.com/cuda-downloads
|
||||||
|
|
||||||
Now build `whisper.cpp` with CUDA support:
|
Now build `whisper.cpp` with cuBLAS support:
|
||||||
|
|
||||||
```
|
```
|
||||||
make clean
|
make clean
|
||||||
WHISPER_CUDA=1 make -j
|
WHISPER_CUBLAS=1 make -j
|
||||||
```
|
```
|
||||||
|
|
||||||
## OpenCL GPU support via CLBlast
|
## OpenCL GPU support via CLBlast
|
||||||
@ -455,21 +455,6 @@ make clean
|
|||||||
WHISPER_OPENBLAS=1 make -j
|
WHISPER_OPENBLAS=1 make -j
|
||||||
```
|
```
|
||||||
|
|
||||||
## BLAS CPU support via Intel MKL
|
|
||||||
|
|
||||||
Encoder processing can be accelerated on the CPU via the BLAS compatible interface of Intel's Math Kernel Library.
|
|
||||||
First, make sure you have installed Intel's MKL runtime and development packages: https://www.intel.com/content/www/us/en/developer/tools/oneapi/onemkl-download.html
|
|
||||||
|
|
||||||
Now build `whisper.cpp` with Intel MKL BLAS support:
|
|
||||||
|
|
||||||
```
|
|
||||||
source /opt/intel/oneapi/setvars.sh
|
|
||||||
mkdir build
|
|
||||||
cd build
|
|
||||||
cmake -DWHISPER_MKL=ON ..
|
|
||||||
WHISPER_MKL=1 make -j
|
|
||||||
```
|
|
||||||
|
|
||||||
## Docker
|
## Docker
|
||||||
|
|
||||||
### Prerequisites
|
### Prerequisites
|
||||||
@ -744,10 +729,10 @@ https://user-images.githubusercontent.com/1991296/199337538-b7b0c7a3-2753-4a88-a
|
|||||||
|
|
||||||
## Video comparison of different models
|
## Video comparison of different models
|
||||||
|
|
||||||
Use the [scripts/bench-wts.sh](https://github.com/ggerganov/whisper.cpp/blob/master/scripts/bench-wts.sh) script to generate a video in the following format:
|
Use the [extra/bench-wts.sh](https://github.com/ggerganov/whisper.cpp/blob/master/extra/bench-wts.sh) script to generate a video in the following format:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
./scripts/bench-wts.sh samples/jfk.wav
|
./extra/bench-wts.sh samples/jfk.wav
|
||||||
ffplay ./samples/jfk.wav.all.mp4
|
ffplay ./samples/jfk.wav.all.mp4
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -768,7 +753,7 @@ Additionally a script to run whisper.cpp with different models and audio files i
|
|||||||
You can run it with the following command, by default it will run against any standard model in the models folder.
|
You can run it with the following command, by default it will run against any standard model in the models folder.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
python3 scripts/bench.py -f samples/jfk.wav -t 2,4,8 -p 1,2
|
python3 extra/bench.py -f samples/jfk.wav -t 2,4,8 -p 1,2
|
||||||
```
|
```
|
||||||
|
|
||||||
It is written in python with the intention of being easy to modify and extend for your benchmarking use case.
|
It is written in python with the intention of being easy to modify and extend for your benchmarking use case.
|
||||||
|
Submodule bindings/ios updated: 0c6cfa58a2...b21b6ff325
@ -148,9 +148,6 @@ public class WhisperFullParams extends Structure {
|
|||||||
tdrz_enable = enable ? CBool.TRUE : CBool.FALSE;
|
tdrz_enable = enable ? CBool.TRUE : CBool.FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Regular expression matching tokens to suppress. */
|
|
||||||
public String suppress_regex;
|
|
||||||
|
|
||||||
/** Tokens to provide to the whisper decoder as an initial prompt.
|
/** Tokens to provide to the whisper decoder as an initial prompt.
|
||||||
* These are prepended to any existing text context from a previous call. */
|
* These are prepended to any existing text context from a previous call. */
|
||||||
public String initial_prompt;
|
public String initial_prompt;
|
||||||
@ -322,7 +319,7 @@ public class WhisperFullParams extends Structure {
|
|||||||
"no_context", "single_segment", "no_timestamps",
|
"no_context", "single_segment", "no_timestamps",
|
||||||
"print_special", "print_progress", "print_realtime", "print_timestamps", "token_timestamps",
|
"print_special", "print_progress", "print_realtime", "print_timestamps", "token_timestamps",
|
||||||
"thold_pt", "thold_ptsum", "max_len", "split_on_word", "max_tokens", "speed_up", "audio_ctx",
|
"thold_pt", "thold_ptsum", "max_len", "split_on_word", "max_tokens", "speed_up", "audio_ctx",
|
||||||
"tdrz_enable", "suppress_regex", "initial_prompt", "prompt_tokens", "prompt_n_tokens", "language", "detect_language",
|
"tdrz_enable", "initial_prompt", "prompt_tokens", "prompt_n_tokens", "language", "detect_language",
|
||||||
"suppress_blank", "suppress_non_speech_tokens", "temperature", "max_initial_ts", "length_penalty",
|
"suppress_blank", "suppress_non_speech_tokens", "temperature", "max_initial_ts", "length_penalty",
|
||||||
"temperature_inc", "entropy_thold", "logprob_thold", "no_speech_thold", "greedy", "beam_search",
|
"temperature_inc", "entropy_thold", "logprob_thold", "no_speech_thold", "greedy", "beam_search",
|
||||||
"new_segment_callback", "new_segment_callback_user_data",
|
"new_segment_callback", "new_segment_callback_user_data",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "whisper.cpp",
|
"name": "whisper.cpp",
|
||||||
"version": "1.5.5",
|
"version": "1.5.4",
|
||||||
"description": "Whisper speech recognition",
|
"description": "Whisper speech recognition",
|
||||||
"main": "whisper.js",
|
"main": "whisper.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
@ -9,7 +9,6 @@ system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-alloc.c')} ."
|
|||||||
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-backend-impl.h')} .")
|
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-backend-impl.h')} .")
|
||||||
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-backend.h')} .")
|
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-backend.h')} .")
|
||||||
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-backend.c')} .")
|
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-backend.c')} .")
|
||||||
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-common.h')} .")
|
|
||||||
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-quants.h')} .")
|
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-quants.h')} .")
|
||||||
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-quants.c')} .")
|
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','ggml-quants.c')} .")
|
||||||
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','examples','dr_wav.h')} .")
|
system("cp #{File.join(File.dirname(__FILE__),'..','..','..','examples','dr_wav.h')} .")
|
||||||
|
@ -27,7 +27,6 @@ add_library(${TARGET} STATIC
|
|||||||
common.cpp
|
common.cpp
|
||||||
common-ggml.h
|
common-ggml.h
|
||||||
common-ggml.cpp
|
common-ggml.cpp
|
||||||
grammar-parser.h
|
|
||||||
grammar-parser.cpp
|
grammar-parser.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -36,7 +35,6 @@ include(DefaultTargetOptions)
|
|||||||
target_link_libraries(${TARGET} PRIVATE whisper)
|
target_link_libraries(${TARGET} PRIVATE whisper)
|
||||||
|
|
||||||
set_target_properties(${TARGET} PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
set_target_properties(${TARGET} PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
||||||
set_target_properties(${TARGET} PROPERTIES FOLDER "libs")
|
|
||||||
|
|
||||||
if (WHISPER_SDL2)
|
if (WHISPER_SDL2)
|
||||||
# common-sdl
|
# common-sdl
|
||||||
@ -54,12 +52,10 @@ if (WHISPER_SDL2)
|
|||||||
target_link_libraries(${TARGET} PRIVATE ${SDL2_LIBRARIES})
|
target_link_libraries(${TARGET} PRIVATE ${SDL2_LIBRARIES})
|
||||||
|
|
||||||
set_target_properties(${TARGET} PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
set_target_properties(${TARGET} PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
||||||
set_target_properties(${TARGET} PROPERTIES FOLDER "libs")
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# add json lib
|
# add json lib
|
||||||
add_library(json_cpp INTERFACE json.hpp)
|
add_library(json_cpp INTERFACE json.hpp)
|
||||||
set_target_properties(json_cpp PROPERTIES FOLDER "libs")
|
|
||||||
|
|
||||||
# examples
|
# examples
|
||||||
|
|
||||||
@ -67,50 +63,25 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
|||||||
|
|
||||||
if (EMSCRIPTEN)
|
if (EMSCRIPTEN)
|
||||||
add_subdirectory(whisper.wasm)
|
add_subdirectory(whisper.wasm)
|
||||||
set_target_properties(libmain PROPERTIES FOLDER "libs")
|
|
||||||
add_subdirectory(stream.wasm)
|
add_subdirectory(stream.wasm)
|
||||||
set_target_properties(libstream PROPERTIES FOLDER "libs")
|
|
||||||
add_subdirectory(command.wasm)
|
add_subdirectory(command.wasm)
|
||||||
set_target_properties(libcommand PROPERTIES FOLDER "libs")
|
|
||||||
add_subdirectory(talk.wasm)
|
add_subdirectory(talk.wasm)
|
||||||
set_target_properties(libtalk PROPERTIES FOLDER "libs")
|
|
||||||
add_subdirectory(bench.wasm)
|
add_subdirectory(bench.wasm)
|
||||||
set_target_properties(libbench PROPERTIES FOLDER "libs")
|
|
||||||
elseif(CMAKE_JS_VERSION)
|
elseif(CMAKE_JS_VERSION)
|
||||||
add_subdirectory(addon.node)
|
add_subdirectory(addon.node)
|
||||||
set_target_properties(addon.node PROPERTIES FOLDER "examples")
|
|
||||||
else()
|
else()
|
||||||
add_subdirectory(main)
|
add_subdirectory(main)
|
||||||
set_target_properties(main PROPERTIES FOLDER "examples")
|
|
||||||
if (WHISPER_SDL2)
|
|
||||||
add_subdirectory(stream)
|
add_subdirectory(stream)
|
||||||
set_target_properties(stream PROPERTIES FOLDER "examples")
|
|
||||||
endif (WHISPER_SDL2)
|
|
||||||
add_subdirectory(server)
|
add_subdirectory(server)
|
||||||
set_target_properties(server PROPERTIES FOLDER "examples")
|
|
||||||
if (WHISPER_SDL2)
|
|
||||||
add_subdirectory(command)
|
add_subdirectory(command)
|
||||||
set_target_properties(command PROPERTIES FOLDER "examples")
|
|
||||||
endif (WHISPER_SDL2)
|
|
||||||
add_subdirectory(bench)
|
add_subdirectory(bench)
|
||||||
set_target_properties(bench PROPERTIES FOLDER "examples")
|
|
||||||
add_subdirectory(quantize)
|
add_subdirectory(quantize)
|
||||||
set_target_properties(quantize PROPERTIES FOLDER "examples")
|
|
||||||
if (WHISPER_SDL2)
|
|
||||||
add_subdirectory(talk)
|
add_subdirectory(talk)
|
||||||
set_target_properties(talk PROPERTIES FOLDER "examples")
|
|
||||||
add_subdirectory(talk-llama)
|
add_subdirectory(talk-llama)
|
||||||
set_target_properties(talk-llama PROPERTIES FOLDER "examples")
|
|
||||||
add_subdirectory(lsp)
|
add_subdirectory(lsp)
|
||||||
set_target_properties(lsp PROPERTIES FOLDER "examples")
|
|
||||||
if (LLAMA_SYCL)
|
if (LLAMA_SYCL)
|
||||||
add_subdirectory(sycl)
|
add_subdirectory(sycl)
|
||||||
set_target_properties(sycl PROPERTIES FOLDER "examples")
|
|
||||||
endif()
|
endif()
|
||||||
endif (WHISPER_SDL2)
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if (WHISPER_SDL2)
|
|
||||||
add_subdirectory(wchess)
|
add_subdirectory(wchess)
|
||||||
set_target_properties(wchess PROPERTIES FOLDER "examples")
|
|
||||||
endif (WHISPER_SDL2)
|
|
||||||
|
@ -211,8 +211,6 @@ int run(whisper_params ¶ms, std::vector<std::vector<std::string>> &result) {
|
|||||||
|
|
||||||
wparams.initial_prompt = params.prompt.c_str();
|
wparams.initial_prompt = params.prompt.c_str();
|
||||||
|
|
||||||
wparams.no_timestamps = params.no_timestamps;
|
|
||||||
|
|
||||||
whisper_print_user_data user_data = { ¶ms, &pcmf32s };
|
whisper_print_user_data user_data = { ¶ms, &pcmf32s };
|
||||||
|
|
||||||
// this callback is called on each new segment
|
// this callback is called on each new segment
|
||||||
@ -300,13 +298,11 @@ Napi::Value whisper(const Napi::CallbackInfo& info) {
|
|||||||
std::string model = whisper_params.Get("model").As<Napi::String>();
|
std::string model = whisper_params.Get("model").As<Napi::String>();
|
||||||
std::string input = whisper_params.Get("fname_inp").As<Napi::String>();
|
std::string input = whisper_params.Get("fname_inp").As<Napi::String>();
|
||||||
bool use_gpu = whisper_params.Get("use_gpu").As<Napi::Boolean>();
|
bool use_gpu = whisper_params.Get("use_gpu").As<Napi::Boolean>();
|
||||||
bool no_timestamps = whisper_params.Get("no_timestamps").As<Napi::Boolean>();
|
|
||||||
|
|
||||||
params.language = language;
|
params.language = language;
|
||||||
params.model = model;
|
params.model = model;
|
||||||
params.fname_inp.emplace_back(input);
|
params.fname_inp.emplace_back(input);
|
||||||
params.use_gpu = use_gpu;
|
params.use_gpu = use_gpu;
|
||||||
params.no_timestamps = no_timestamps;
|
|
||||||
|
|
||||||
Napi::Function callback = info[1].As<Napi::Function>();
|
Napi::Function callback = info[1].As<Napi::Function>();
|
||||||
Worker* worker = new Worker(callback, params);
|
Worker* worker = new Worker(callback, params);
|
||||||
|
@ -37,13 +37,9 @@ https://user-images.githubusercontent.com/1991296/207435352-8fc4ed3f-bde5-4555-9
|
|||||||
The `command` tool depends on SDL2 library to capture audio from the microphone. You can build it like this:
|
The `command` tool depends on SDL2 library to capture audio from the microphone. You can build it like this:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Install SDL2
|
# Install SDL2 on Linux
|
||||||
# On Debian based linux distributions:
|
|
||||||
sudo apt-get install libsdl2-dev
|
sudo apt-get install libsdl2-dev
|
||||||
|
|
||||||
# On Fedora Linux:
|
|
||||||
sudo dnf install SDL2 SDL2-devel
|
|
||||||
|
|
||||||
# Install SDL2 on Mac OS
|
# Install SDL2 on Mac OS
|
||||||
brew install sdl2
|
brew install sdl2
|
||||||
|
|
||||||
|
@ -52,9 +52,6 @@ struct whisper_params {
|
|||||||
std::string prompt;
|
std::string prompt;
|
||||||
std::string context;
|
std::string context;
|
||||||
std::string grammar;
|
std::string grammar;
|
||||||
|
|
||||||
// A regular expression that matches tokens to suppress
|
|
||||||
std::string suppress_regex;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void whisper_print_usage(int argc, char ** argv, const whisper_params & params);
|
void whisper_print_usage(int argc, char ** argv, const whisper_params & params);
|
||||||
@ -88,7 +85,6 @@ bool whisper_params_parse(int argc, char ** argv, whisper_params & params) {
|
|||||||
else if (arg == "-ctx" || arg == "--context") { params.context = argv[++i]; }
|
else if (arg == "-ctx" || arg == "--context") { params.context = argv[++i]; }
|
||||||
else if ( arg == "--grammar") { params.grammar = argv[++i]; }
|
else if ( arg == "--grammar") { params.grammar = argv[++i]; }
|
||||||
else if ( arg == "--grammar-penalty") { params.grammar_penalty = std::stof(argv[++i]); }
|
else if ( arg == "--grammar-penalty") { params.grammar_penalty = std::stof(argv[++i]); }
|
||||||
else if ( arg == "--suppress-regex") { params.suppress_regex = argv[++i]; }
|
|
||||||
else {
|
else {
|
||||||
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
|
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
|
||||||
whisper_print_usage(argc, argv, params);
|
whisper_print_usage(argc, argv, params);
|
||||||
@ -126,7 +122,6 @@ void whisper_print_usage(int /*argc*/, char ** argv, const whisper_params & para
|
|||||||
fprintf(stderr, " -ctx, --context [%-7s] sample text to help the transcription\n", params.context.c_str());
|
fprintf(stderr, " -ctx, --context [%-7s] sample text to help the transcription\n", params.context.c_str());
|
||||||
fprintf(stderr, " --grammar GRAMMAR [%-7s] GBNF grammar to guide decoding\n", params.grammar.c_str());
|
fprintf(stderr, " --grammar GRAMMAR [%-7s] GBNF grammar to guide decoding\n", params.grammar.c_str());
|
||||||
fprintf(stderr, " --grammar-penalty N [%-7.1f] scales down logits of nongrammar tokens\n", params.grammar_penalty);
|
fprintf(stderr, " --grammar-penalty N [%-7.1f] scales down logits of nongrammar tokens\n", params.grammar_penalty);
|
||||||
fprintf(stderr, " --suppress-regex REGEX [%-7s] regular expression matching tokens to suppress\n", params.suppress_regex.c_str());
|
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,8 +167,6 @@ std::string transcribe(
|
|||||||
|
|
||||||
wparams.initial_prompt = params.context.data();
|
wparams.initial_prompt = params.context.data();
|
||||||
|
|
||||||
wparams.suppress_regex = params.suppress_regex.c_str();
|
|
||||||
|
|
||||||
const auto & grammar_parsed = params.grammar_parsed;
|
const auto & grammar_parsed = params.grammar_parsed;
|
||||||
auto grammar_rules = grammar_parsed.c_rules();
|
auto grammar_rules = grammar_parsed.c_rules();
|
||||||
|
|
||||||
|
@ -70,7 +70,6 @@ bool ggml_common_quantize_0(
|
|||||||
case GGML_FTYPE_MOSTLY_IQ1_S:
|
case GGML_FTYPE_MOSTLY_IQ1_S:
|
||||||
case GGML_FTYPE_MOSTLY_IQ4_NL:
|
case GGML_FTYPE_MOSTLY_IQ4_NL:
|
||||||
case GGML_FTYPE_MOSTLY_IQ4_XS:
|
case GGML_FTYPE_MOSTLY_IQ4_XS:
|
||||||
case GGML_FTYPE_MOSTLY_IQ1_M:
|
|
||||||
{
|
{
|
||||||
fprintf(stderr, "%s: invalid model type %d\n", __func__, ftype);
|
fprintf(stderr, "%s: invalid model type %d\n", __func__, ftype);
|
||||||
return false;
|
return false;
|
||||||
@ -194,8 +193,6 @@ bool ggml_common_quantize_0(
|
|||||||
case GGML_TYPE_I8:
|
case GGML_TYPE_I8:
|
||||||
case GGML_TYPE_I16:
|
case GGML_TYPE_I16:
|
||||||
case GGML_TYPE_I32:
|
case GGML_TYPE_I32:
|
||||||
case GGML_TYPE_I64:
|
|
||||||
case GGML_TYPE_F64:
|
|
||||||
case GGML_TYPE_Q8_1:
|
case GGML_TYPE_Q8_1:
|
||||||
case GGML_TYPE_Q8_K:
|
case GGML_TYPE_Q8_K:
|
||||||
case GGML_TYPE_IQ2_XXS:
|
case GGML_TYPE_IQ2_XXS:
|
||||||
@ -206,7 +203,6 @@ bool ggml_common_quantize_0(
|
|||||||
case GGML_TYPE_IQ1_S:
|
case GGML_TYPE_IQ1_S:
|
||||||
case GGML_TYPE_IQ4_NL:
|
case GGML_TYPE_IQ4_NL:
|
||||||
case GGML_TYPE_IQ4_XS:
|
case GGML_TYPE_IQ4_XS:
|
||||||
case GGML_TYPE_IQ1_M:
|
|
||||||
case GGML_TYPE_COUNT:
|
case GGML_TYPE_COUNT:
|
||||||
{
|
{
|
||||||
fprintf(stderr, "%s: unsupported quantization type %d (%s)\n", __func__, ttype, ggml_type_name((ggml_type) ttype));
|
fprintf(stderr, "%s: unsupported quantization type %d (%s)\n", __func__, ttype, ggml_type_name((ggml_type) ttype));
|
||||||
|
@ -19,11 +19,6 @@
|
|||||||
#pragma warning(disable: 4244 4267) // possible loss of data
|
#pragma warning(disable: 4244 4267) // possible loss of data
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <io.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Function to check if the next argument exists
|
// Function to check if the next argument exists
|
||||||
std::string get_next_arg(int& i, int argc, char** argv, const std::string& flag, gpt_params& params) {
|
std::string get_next_arg(int& i, int argc, char** argv, const std::string& flag, gpt_params& params) {
|
||||||
if (i + 1 < argc && argv[i + 1][0] != '-') {
|
if (i + 1 < argc && argv[i + 1][0] != '-') {
|
||||||
@ -641,10 +636,6 @@ bool read_wav(const std::string & fname, std::vector<float>& pcmf32, std::vector
|
|||||||
|
|
||||||
if (fname == "-") {
|
if (fname == "-") {
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
|
||||||
_setmode(_fileno(stdin), _O_BINARY);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
uint8_t buf[1024];
|
uint8_t buf[1024];
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
@ -676,25 +667,21 @@ bool read_wav(const std::string & fname, std::vector<float>& pcmf32, std::vector
|
|||||||
|
|
||||||
if (wav.channels != 1 && wav.channels != 2) {
|
if (wav.channels != 1 && wav.channels != 2) {
|
||||||
fprintf(stderr, "%s: WAV file '%s' must be mono or stereo\n", __func__, fname.c_str());
|
fprintf(stderr, "%s: WAV file '%s' must be mono or stereo\n", __func__, fname.c_str());
|
||||||
drwav_uninit(&wav);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stereo && wav.channels != 2) {
|
if (stereo && wav.channels != 2) {
|
||||||
fprintf(stderr, "%s: WAV file '%s' must be stereo for diarization\n", __func__, fname.c_str());
|
fprintf(stderr, "%s: WAV file '%s' must be stereo for diarization\n", __func__, fname.c_str());
|
||||||
drwav_uninit(&wav);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (wav.sampleRate != COMMON_SAMPLE_RATE) {
|
if (wav.sampleRate != COMMON_SAMPLE_RATE) {
|
||||||
fprintf(stderr, "%s: WAV file '%s' must be %i kHz\n", __func__, fname.c_str(), COMMON_SAMPLE_RATE/1000);
|
fprintf(stderr, "%s: WAV file '%s' must be %i kHz\n", __func__, fname.c_str(), COMMON_SAMPLE_RATE/1000);
|
||||||
drwav_uninit(&wav);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (wav.bitsPerSample != 16) {
|
if (wav.bitsPerSample != 16) {
|
||||||
fprintf(stderr, "%s: WAV file '%s' must be 16-bit\n", __func__, fname.c_str());
|
fprintf(stderr, "%s: WAV file '%s' must be 16-bit\n", __func__, fname.c_str());
|
||||||
drwav_uninit(&wav);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,7 +190,7 @@ namespace grammar_parser {
|
|||||||
pos = parse_space(pos + 1, is_nested);
|
pos = parse_space(pos + 1, is_nested);
|
||||||
} else if (*pos == '*' || *pos == '+' || *pos == '?') { // repetition operator
|
} else if (*pos == '*' || *pos == '+' || *pos == '?') { // repetition operator
|
||||||
if (last_sym_start == out_elements.size()) {
|
if (last_sym_start == out_elements.size()) {
|
||||||
throw std::runtime_error(std::string("expecting preceding item to */+/? at ") + pos);
|
throw std::runtime_error(std::string("expecting preceeding item to */+/? at ") + pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
// apply transformation to previous symbol (last_sym_start to end) according to
|
// apply transformation to previous symbol (last_sym_start to end) according to
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
#include "whisper.h"
|
#include "whisper.h"
|
||||||
#include "grammar-parser.h"
|
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <regex>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -43,7 +41,6 @@ struct whisper_params {
|
|||||||
float word_thold = 0.01f;
|
float word_thold = 0.01f;
|
||||||
float entropy_thold = 2.40f;
|
float entropy_thold = 2.40f;
|
||||||
float logprob_thold = -1.00f;
|
float logprob_thold = -1.00f;
|
||||||
float grammar_penalty = 100.0f;
|
|
||||||
|
|
||||||
bool speed_up = false;
|
bool speed_up = false;
|
||||||
bool debug_mode = false;
|
bool debug_mode = false;
|
||||||
@ -73,23 +70,14 @@ struct whisper_params {
|
|||||||
std::string prompt;
|
std::string prompt;
|
||||||
std::string font_path = "/System/Library/Fonts/Supplemental/Courier New Bold.ttf";
|
std::string font_path = "/System/Library/Fonts/Supplemental/Courier New Bold.ttf";
|
||||||
std::string model = "models/ggml-base.en.bin";
|
std::string model = "models/ggml-base.en.bin";
|
||||||
std::string grammar;
|
|
||||||
std::string grammar_rule;
|
|
||||||
|
|
||||||
// [TDRZ] speaker turn string
|
// [TDRZ] speaker turn string
|
||||||
std::string tdrz_speaker_turn = " [SPEAKER_TURN]"; // TODO: set from command line
|
std::string tdrz_speaker_turn = " [SPEAKER_TURN]"; // TODO: set from command line
|
||||||
|
|
||||||
// A regular expression that matches tokens to suppress
|
|
||||||
std::string suppress_regex;
|
|
||||||
|
|
||||||
std::string openvino_encode_device = "CPU";
|
std::string openvino_encode_device = "CPU";
|
||||||
|
|
||||||
std::string dtw = "";
|
|
||||||
|
|
||||||
std::vector<std::string> fname_inp = {};
|
std::vector<std::string> fname_inp = {};
|
||||||
std::vector<std::string> fname_out = {};
|
std::vector<std::string> fname_out = {};
|
||||||
|
|
||||||
grammar_parser::parse_state grammar_parsed;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void whisper_print_usage(int argc, char ** argv, const whisper_params & params);
|
void whisper_print_usage(int argc, char ** argv, const whisper_params & params);
|
||||||
@ -129,7 +117,7 @@ bool whisper_params_parse(int argc, char ** argv, whisper_params & params) {
|
|||||||
else if (arg == "-ml" || arg == "--max-len") { params.max_len = std::stoi(argv[++i]); }
|
else if (arg == "-ml" || arg == "--max-len") { params.max_len = std::stoi(argv[++i]); }
|
||||||
else if (arg == "-bo" || arg == "--best-of") { params.best_of = std::stoi(argv[++i]); }
|
else if (arg == "-bo" || arg == "--best-of") { params.best_of = std::stoi(argv[++i]); }
|
||||||
else if (arg == "-bs" || arg == "--beam-size") { params.beam_size = std::stoi(argv[++i]); }
|
else if (arg == "-bs" || arg == "--beam-size") { params.beam_size = std::stoi(argv[++i]); }
|
||||||
else if (arg == "-ac" || arg == "--audio-ctx") { params.audio_ctx = std::stoi(argv[++i]); }
|
else if (arg == "-ac" || arg == "--audio-context") { params.audio_ctx = std::stoi(argv[++i]); }
|
||||||
else if (arg == "-wt" || arg == "--word-thold") { params.word_thold = std::stof(argv[++i]); }
|
else if (arg == "-wt" || arg == "--word-thold") { params.word_thold = std::stof(argv[++i]); }
|
||||||
else if (arg == "-et" || arg == "--entropy-thold") { params.entropy_thold = std::stof(argv[++i]); }
|
else if (arg == "-et" || arg == "--entropy-thold") { params.entropy_thold = std::stof(argv[++i]); }
|
||||||
else if (arg == "-lpt" || arg == "--logprob-thold") { params.logprob_thold = std::stof(argv[++i]); }
|
else if (arg == "-lpt" || arg == "--logprob-thold") { params.logprob_thold = std::stof(argv[++i]); }
|
||||||
@ -161,13 +149,8 @@ bool whisper_params_parse(int argc, char ** argv, whisper_params & params) {
|
|||||||
else if (arg == "-m" || arg == "--model") { params.model = argv[++i]; }
|
else if (arg == "-m" || arg == "--model") { params.model = argv[++i]; }
|
||||||
else if (arg == "-f" || arg == "--file") { params.fname_inp.emplace_back(argv[++i]); }
|
else if (arg == "-f" || arg == "--file") { params.fname_inp.emplace_back(argv[++i]); }
|
||||||
else if (arg == "-oved" || arg == "--ov-e-device") { params.openvino_encode_device = argv[++i]; }
|
else if (arg == "-oved" || arg == "--ov-e-device") { params.openvino_encode_device = argv[++i]; }
|
||||||
else if (arg == "-dtw" || arg == "--dtw") { params.dtw = argv[++i]; }
|
|
||||||
else if (arg == "-ls" || arg == "--log-score") { params.log_score = true; }
|
else if (arg == "-ls" || arg == "--log-score") { params.log_score = true; }
|
||||||
else if (arg == "-ng" || arg == "--no-gpu") { params.use_gpu = false; }
|
else if (arg == "-ng" || arg == "--no-gpu") { params.use_gpu = false; }
|
||||||
else if ( arg == "--suppress-regex") { params.suppress_regex = argv[++i]; }
|
|
||||||
else if ( arg == "--grammar") { params.grammar = argv[++i]; }
|
|
||||||
else if ( arg == "--grammar-rule") { params.grammar_rule = argv[++i]; }
|
|
||||||
else if ( arg == "--grammar-penalty") { params.grammar_penalty = std::stof(argv[++i]); }
|
|
||||||
else {
|
else {
|
||||||
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
|
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
|
||||||
whisper_print_usage(argc, argv, params);
|
whisper_print_usage(argc, argv, params);
|
||||||
@ -221,17 +204,12 @@ void whisper_print_usage(int /*argc*/, char ** argv, const whisper_params & para
|
|||||||
fprintf(stderr, " -nt, --no-timestamps [%-7s] do not print timestamps\n", params.no_timestamps ? "true" : "false");
|
fprintf(stderr, " -nt, --no-timestamps [%-7s] do not print timestamps\n", params.no_timestamps ? "true" : "false");
|
||||||
fprintf(stderr, " -l LANG, --language LANG [%-7s] spoken language ('auto' for auto-detect)\n", params.language.c_str());
|
fprintf(stderr, " -l LANG, --language LANG [%-7s] spoken language ('auto' for auto-detect)\n", params.language.c_str());
|
||||||
fprintf(stderr, " -dl, --detect-language [%-7s] exit after automatically detecting language\n", params.detect_language ? "true" : "false");
|
fprintf(stderr, " -dl, --detect-language [%-7s] exit after automatically detecting language\n", params.detect_language ? "true" : "false");
|
||||||
fprintf(stderr, " --prompt PROMPT [%-7s] initial prompt (max n_text_ctx/2 tokens)\n", params.prompt.c_str());
|
fprintf(stderr, " --prompt PROMPT [%-7s] initial prompt\n", params.prompt.c_str());
|
||||||
fprintf(stderr, " -m FNAME, --model FNAME [%-7s] model path\n", params.model.c_str());
|
fprintf(stderr, " -m FNAME, --model FNAME [%-7s] model path\n", params.model.c_str());
|
||||||
fprintf(stderr, " -f FNAME, --file FNAME [%-7s] input WAV file path\n", "");
|
fprintf(stderr, " -f FNAME, --file FNAME [%-7s] input WAV file path\n", "");
|
||||||
fprintf(stderr, " -oved D, --ov-e-device DNAME [%-7s] the OpenVINO device used for encode inference\n", params.openvino_encode_device.c_str());
|
fprintf(stderr, " -oved D, --ov-e-device DNAME [%-7s] the OpenVINO device used for encode inference\n", params.openvino_encode_device.c_str());
|
||||||
fprintf(stderr, " -dtw MODEL --dtw MODEL [%-7s] compute token-level timestamps\n", params.dtw.c_str());
|
|
||||||
fprintf(stderr, " -ls, --log-score [%-7s] log best decoder scores of tokens\n", params.log_score?"true":"false");
|
fprintf(stderr, " -ls, --log-score [%-7s] log best decoder scores of tokens\n", params.log_score?"true":"false");
|
||||||
fprintf(stderr, " -ng, --no-gpu [%-7s] disable GPU\n", params.use_gpu ? "false" : "true");
|
fprintf(stderr, " -ng, --no-gpu [%-7s] disable GPU\n", params.use_gpu ? "false" : "true");
|
||||||
fprintf(stderr, " --suppress-regex REGEX [%-7s] regular expression matching tokens to suppress\n", params.suppress_regex.c_str());
|
|
||||||
fprintf(stderr, " --grammar GRAMMAR [%-7s] GBNF grammar to guide decoding\n", params.grammar.c_str());
|
|
||||||
fprintf(stderr, " --grammar-rule RULE [%-7s] top-level GBNF grammar rule name\n", params.grammar_rule.c_str());
|
|
||||||
fprintf(stderr, " --grammar-penalty N [%-7.1f] scales down logits of nongrammar tokens\n", params.grammar_penalty);
|
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -671,8 +649,7 @@ bool output_json(
|
|||||||
times_o(token.t0, token.t1, false);
|
times_o(token.t0, token.t1, false);
|
||||||
}
|
}
|
||||||
value_i("id", token.id, false);
|
value_i("id", token.id, false);
|
||||||
value_f("p", token.p, false);
|
value_f("p", token.p, true);
|
||||||
value_f("t_dtw", token.t_dtw, true);
|
|
||||||
end_obj(j == (n - 1));
|
end_obj(j == (n - 1));
|
||||||
}
|
}
|
||||||
end_arr(!params.diarize && !params.tinydiarize);
|
end_arr(!params.diarize && !params.tinydiarize);
|
||||||
@ -867,35 +844,6 @@ void cb_log_disable(enum ggml_log_level , const char * , void * ) { }
|
|||||||
int main(int argc, char ** argv) {
|
int main(int argc, char ** argv) {
|
||||||
whisper_params params;
|
whisper_params params;
|
||||||
|
|
||||||
// If the only argument starts with "@", read arguments line-by-line
|
|
||||||
// from the given file.
|
|
||||||
std::vector<std::string> vec_args;
|
|
||||||
if (argc == 2 && argv != nullptr && argv[1] != nullptr && argv[1][0] == '@') {
|
|
||||||
// Save the name of the executable.
|
|
||||||
vec_args.push_back(argv[0]);
|
|
||||||
|
|
||||||
// Open the response file.
|
|
||||||
char const * rspfile = argv[1] + sizeof(char);
|
|
||||||
std::ifstream fin(rspfile);
|
|
||||||
if (fin.is_open() == false) {
|
|
||||||
fprintf(stderr, "error: response file '%s' not found\n", rspfile);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read the entire response file.
|
|
||||||
std::string line;
|
|
||||||
while (std::getline(fin, line)) {
|
|
||||||
vec_args.push_back(line);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use the contents of the response file as the command-line arguments.
|
|
||||||
argc = static_cast<int>(vec_args.size());
|
|
||||||
argv = static_cast<char **>(alloca(argc * sizeof (char *)));
|
|
||||||
for (int i = 0; i < argc; ++i) {
|
|
||||||
argv[i] = const_cast<char *>(vec_args[i].c_str());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (whisper_params_parse(argc, argv, params) == false) {
|
if (whisper_params_parse(argc, argv, params) == false) {
|
||||||
whisper_print_usage(argc, argv, params);
|
whisper_print_usage(argc, argv, params);
|
||||||
return 1;
|
return 1;
|
||||||
@ -941,28 +889,6 @@ int main(int argc, char ** argv) {
|
|||||||
struct whisper_context_params cparams = whisper_context_default_params();
|
struct whisper_context_params cparams = whisper_context_default_params();
|
||||||
cparams.use_gpu = params.use_gpu;
|
cparams.use_gpu = params.use_gpu;
|
||||||
|
|
||||||
if (!params.dtw.empty()) {
|
|
||||||
cparams.dtw_token_timestamps = true;
|
|
||||||
cparams.dtw_aheads_preset = WHISPER_AHEADS_NONE;
|
|
||||||
|
|
||||||
if (params.dtw == "tiny") cparams.dtw_aheads_preset = WHISPER_AHEADS_TINY;
|
|
||||||
if (params.dtw == "tiny.en") cparams.dtw_aheads_preset = WHISPER_AHEADS_TINY_EN;
|
|
||||||
if (params.dtw == "base") cparams.dtw_aheads_preset = WHISPER_AHEADS_BASE;
|
|
||||||
if (params.dtw == "base.en") cparams.dtw_aheads_preset = WHISPER_AHEADS_BASE_EN;
|
|
||||||
if (params.dtw == "small") cparams.dtw_aheads_preset = WHISPER_AHEADS_SMALL;
|
|
||||||
if (params.dtw == "small.en") cparams.dtw_aheads_preset = WHISPER_AHEADS_SMALL_EN;
|
|
||||||
if (params.dtw == "medium") cparams.dtw_aheads_preset = WHISPER_AHEADS_MEDIUM;
|
|
||||||
if (params.dtw == "medium.en") cparams.dtw_aheads_preset = WHISPER_AHEADS_MEDIUM_EN;
|
|
||||||
if (params.dtw == "large.v1") cparams.dtw_aheads_preset = WHISPER_AHEADS_LARGE_V1;
|
|
||||||
if (params.dtw == "large.v2") cparams.dtw_aheads_preset = WHISPER_AHEADS_LARGE_V2;
|
|
||||||
if (params.dtw == "large.v3") cparams.dtw_aheads_preset = WHISPER_AHEADS_LARGE_V3;
|
|
||||||
|
|
||||||
if (cparams.dtw_aheads_preset == WHISPER_AHEADS_NONE) {
|
|
||||||
fprintf(stderr, "error: unknown DTW preset '%s'\n", params.dtw.c_str());
|
|
||||||
return 3;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
|
struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
|
||||||
|
|
||||||
if (ctx == nullptr) {
|
if (ctx == nullptr) {
|
||||||
@ -973,29 +899,6 @@ int main(int argc, char ** argv) {
|
|||||||
// initialize openvino encoder. this has no effect on whisper.cpp builds that don't have OpenVINO configured
|
// initialize openvino encoder. this has no effect on whisper.cpp builds that don't have OpenVINO configured
|
||||||
whisper_ctx_init_openvino_encoder(ctx, nullptr, params.openvino_encode_device.c_str(), nullptr);
|
whisper_ctx_init_openvino_encoder(ctx, nullptr, params.openvino_encode_device.c_str(), nullptr);
|
||||||
|
|
||||||
if (!params.grammar.empty()) {
|
|
||||||
auto & grammar = params.grammar_parsed;
|
|
||||||
if (is_file_exist(params.grammar.c_str())) {
|
|
||||||
// read grammar from file
|
|
||||||
std::ifstream ifs(params.grammar.c_str());
|
|
||||||
const std::string txt = std::string((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
|
|
||||||
grammar = grammar_parser::parse(txt.c_str());
|
|
||||||
} else {
|
|
||||||
// read grammar from string
|
|
||||||
grammar = grammar_parser::parse(params.grammar.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
// will be empty (default) if there are parse errors
|
|
||||||
if (grammar.rules.empty()) {
|
|
||||||
fprintf(stderr, "error: failed to parse grammar \"%s\"\n", params.grammar.c_str());
|
|
||||||
return 4;
|
|
||||||
} else {
|
|
||||||
fprintf(stderr, "%s: grammar:\n", __func__);
|
|
||||||
grammar_parser::print_grammar(stderr, grammar);
|
|
||||||
fprintf(stderr, "\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int f = 0; f < (int) params.fname_inp.size(); ++f) {
|
for (int f = 0; f < (int) params.fname_inp.size(); ++f) {
|
||||||
const auto fname_inp = params.fname_inp[f];
|
const auto fname_inp = params.fname_inp[f];
|
||||||
const auto fname_out = f < (int) params.fname_out.size() && !params.fname_out[f].empty() ? params.fname_out[f] : params.fname_inp[f];
|
const auto fname_out = f < (int) params.fname_out.size() && !params.fname_out[f].empty() ? params.fname_out[f] : params.fname_inp[f];
|
||||||
@ -1042,8 +945,7 @@ int main(int argc, char ** argv) {
|
|||||||
{
|
{
|
||||||
whisper_full_params wparams = whisper_full_default_params(WHISPER_SAMPLING_GREEDY);
|
whisper_full_params wparams = whisper_full_default_params(WHISPER_SAMPLING_GREEDY);
|
||||||
|
|
||||||
const bool use_grammar = (!params.grammar_parsed.rules.empty() && !params.grammar_rule.empty());
|
wparams.strategy = params.beam_size > 1 ? WHISPER_SAMPLING_BEAM_SEARCH : WHISPER_SAMPLING_GREEDY;
|
||||||
wparams.strategy = (params.beam_size > 1 || use_grammar) ? WHISPER_SAMPLING_BEAM_SEARCH : WHISPER_SAMPLING_GREEDY;
|
|
||||||
|
|
||||||
wparams.print_realtime = false;
|
wparams.print_realtime = false;
|
||||||
wparams.print_progress = params.print_progress;
|
wparams.print_progress = params.print_progress;
|
||||||
@ -1068,8 +970,6 @@ int main(int argc, char ** argv) {
|
|||||||
|
|
||||||
wparams.tdrz_enable = params.tinydiarize; // [TDRZ]
|
wparams.tdrz_enable = params.tinydiarize; // [TDRZ]
|
||||||
|
|
||||||
wparams.suppress_regex = params.suppress_regex.c_str();
|
|
||||||
|
|
||||||
wparams.initial_prompt = params.prompt.c_str();
|
wparams.initial_prompt = params.prompt.c_str();
|
||||||
|
|
||||||
wparams.greedy.best_of = params.best_of;
|
wparams.greedy.best_of = params.best_of;
|
||||||
@ -1083,20 +983,6 @@ int main(int argc, char ** argv) {
|
|||||||
|
|
||||||
whisper_print_user_data user_data = { ¶ms, &pcmf32s, 0 };
|
whisper_print_user_data user_data = { ¶ms, &pcmf32s, 0 };
|
||||||
|
|
||||||
const auto & grammar_parsed = params.grammar_parsed;
|
|
||||||
auto grammar_rules = grammar_parsed.c_rules();
|
|
||||||
|
|
||||||
if (use_grammar) {
|
|
||||||
if (grammar_parsed.symbol_ids.find(params.grammar_rule) == grammar_parsed.symbol_ids.end()) {
|
|
||||||
fprintf(stderr, "%s: warning: grammar rule '%s' not found - skipping grammar sampling\n", __func__, params.grammar_rule.c_str());
|
|
||||||
} else {
|
|
||||||
wparams.grammar_rules = grammar_rules.data();
|
|
||||||
wparams.n_grammar_rules = grammar_rules.size();
|
|
||||||
wparams.i_start_rule = grammar_parsed.symbol_ids.at(params.grammar_rule);
|
|
||||||
wparams.grammar_penalty = params.grammar_penalty;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// this callback is called on each new segment
|
// this callback is called on each new segment
|
||||||
if (!wparams.print_realtime) {
|
if (!wparams.print_realtime) {
|
||||||
wparams.new_segment_callback = whisper_print_segment_callback;
|
wparams.new_segment_callback = whisper_print_segment_callback;
|
||||||
|
@ -87,8 +87,6 @@ struct whisper_params {
|
|||||||
std::string tdrz_speaker_turn = " [SPEAKER_TURN]"; // TODO: set from command line
|
std::string tdrz_speaker_turn = " [SPEAKER_TURN]"; // TODO: set from command line
|
||||||
|
|
||||||
std::string openvino_encode_device = "CPU";
|
std::string openvino_encode_device = "CPU";
|
||||||
|
|
||||||
std::string dtw = "";
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void whisper_print_usage(int /*argc*/, char ** argv, const whisper_params & params, const server_params& sparams) {
|
void whisper_print_usage(int /*argc*/, char ** argv, const whisper_params & params, const server_params& sparams) {
|
||||||
@ -128,7 +126,6 @@ void whisper_print_usage(int /*argc*/, char ** argv, const whisper_params & para
|
|||||||
fprintf(stderr, " -m FNAME, --model FNAME [%-7s] model path\n", params.model.c_str());
|
fprintf(stderr, " -m FNAME, --model FNAME [%-7s] model path\n", params.model.c_str());
|
||||||
fprintf(stderr, " -oved D, --ov-e-device DNAME [%-7s] the OpenVINO device used for encode inference\n", params.openvino_encode_device.c_str());
|
fprintf(stderr, " -oved D, --ov-e-device DNAME [%-7s] the OpenVINO device used for encode inference\n", params.openvino_encode_device.c_str());
|
||||||
// server params
|
// server params
|
||||||
fprintf(stderr, " -dtw MODEL --dtw MODEL [%-7s] compute token-level timestamps\n", params.dtw.c_str());
|
|
||||||
fprintf(stderr, " --host HOST, [%-7s] Hostname/ip-adress for the server\n", sparams.hostname.c_str());
|
fprintf(stderr, " --host HOST, [%-7s] Hostname/ip-adress for the server\n", sparams.hostname.c_str());
|
||||||
fprintf(stderr, " --port PORT, [%-7d] Port number for the server\n", sparams.port);
|
fprintf(stderr, " --port PORT, [%-7d] Port number for the server\n", sparams.port);
|
||||||
fprintf(stderr, " --public PATH, [%-7s] Path to the public folder\n", sparams.public_path.c_str());
|
fprintf(stderr, " --public PATH, [%-7s] Path to the public folder\n", sparams.public_path.c_str());
|
||||||
@ -154,7 +151,7 @@ bool whisper_params_parse(int argc, char ** argv, whisper_params & params, serve
|
|||||||
else if (arg == "-ml" || arg == "--max-len") { params.max_len = std::stoi(argv[++i]); }
|
else if (arg == "-ml" || arg == "--max-len") { params.max_len = std::stoi(argv[++i]); }
|
||||||
else if (arg == "-bo" || arg == "--best-of") { params.best_of = std::stoi(argv[++i]); }
|
else if (arg == "-bo" || arg == "--best-of") { params.best_of = std::stoi(argv[++i]); }
|
||||||
else if (arg == "-bs" || arg == "--beam-size") { params.beam_size = std::stoi(argv[++i]); }
|
else if (arg == "-bs" || arg == "--beam-size") { params.beam_size = std::stoi(argv[++i]); }
|
||||||
else if (arg == "-ac" || arg == "--audio-ctx") { params.audio_ctx = std::stoi(argv[++i]); }
|
else if (arg == "-ac" || arg == "--audio-context") { params.audio_ctx = std::stoi(argv[++i]); }
|
||||||
else if (arg == "-wt" || arg == "--word-thold") { params.word_thold = std::stof(argv[++i]); }
|
else if (arg == "-wt" || arg == "--word-thold") { params.word_thold = std::stof(argv[++i]); }
|
||||||
else if (arg == "-et" || arg == "--entropy-thold") { params.entropy_thold = std::stof(argv[++i]); }
|
else if (arg == "-et" || arg == "--entropy-thold") { params.entropy_thold = std::stof(argv[++i]); }
|
||||||
else if (arg == "-lpt" || arg == "--logprob-thold") { params.logprob_thold = std::stof(argv[++i]); }
|
else if (arg == "-lpt" || arg == "--logprob-thold") { params.logprob_thold = std::stof(argv[++i]); }
|
||||||
@ -176,7 +173,6 @@ bool whisper_params_parse(int argc, char ** argv, whisper_params & params, serve
|
|||||||
else if ( arg == "--prompt") { params.prompt = argv[++i]; }
|
else if ( arg == "--prompt") { params.prompt = argv[++i]; }
|
||||||
else if (arg == "-m" || arg == "--model") { params.model = argv[++i]; }
|
else if (arg == "-m" || arg == "--model") { params.model = argv[++i]; }
|
||||||
else if (arg == "-oved" || arg == "--ov-e-device") { params.openvino_encode_device = argv[++i]; }
|
else if (arg == "-oved" || arg == "--ov-e-device") { params.openvino_encode_device = argv[++i]; }
|
||||||
else if (arg == "-dtw" || arg == "--dtw") { params.dtw = argv[++i]; }
|
|
||||||
else if (arg == "-ng" || arg == "--no-gpu") { params.use_gpu = false; }
|
else if (arg == "-ng" || arg == "--no-gpu") { params.use_gpu = false; }
|
||||||
// server params
|
// server params
|
||||||
else if ( arg == "--port") { sparams.port = std::stoi(argv[++i]); }
|
else if ( arg == "--port") { sparams.port = std::stoi(argv[++i]); }
|
||||||
@ -503,49 +499,6 @@ int main(int argc, char ** argv) {
|
|||||||
// whisper init
|
// whisper init
|
||||||
struct whisper_context_params cparams = whisper_context_default_params();
|
struct whisper_context_params cparams = whisper_context_default_params();
|
||||||
cparams.use_gpu = params.use_gpu;
|
cparams.use_gpu = params.use_gpu;
|
||||||
if (!params.dtw.empty()) {
|
|
||||||
cparams.dtw_token_timestamps = true;
|
|
||||||
cparams.dtw_aheads_preset = WHISPER_AHEADS_NONE;
|
|
||||||
|
|
||||||
if (params.dtw == "tiny") {
|
|
||||||
cparams.dtw_aheads_preset = WHISPER_AHEADS_TINY;
|
|
||||||
}
|
|
||||||
if (params.dtw == "tiny.en") {
|
|
||||||
cparams.dtw_aheads_preset = WHISPER_AHEADS_TINY_EN;
|
|
||||||
}
|
|
||||||
if (params.dtw == "base") {
|
|
||||||
cparams.dtw_aheads_preset = WHISPER_AHEADS_BASE;
|
|
||||||
}
|
|
||||||
if (params.dtw == "base.en") {
|
|
||||||
cparams.dtw_aheads_preset = WHISPER_AHEADS_BASE_EN;
|
|
||||||
}
|
|
||||||
if (params.dtw == "small") {
|
|
||||||
cparams.dtw_aheads_preset = WHISPER_AHEADS_SMALL;
|
|
||||||
}
|
|
||||||
if (params.dtw == "small.en") {
|
|
||||||
cparams.dtw_aheads_preset = WHISPER_AHEADS_SMALL_EN;
|
|
||||||
}
|
|
||||||
if (params.dtw == "medium") {
|
|
||||||
cparams.dtw_aheads_preset = WHISPER_AHEADS_MEDIUM;
|
|
||||||
}
|
|
||||||
if (params.dtw == "medium.en") {
|
|
||||||
cparams.dtw_aheads_preset = WHISPER_AHEADS_MEDIUM_EN;
|
|
||||||
}
|
|
||||||
if (params.dtw == "large.v1") {
|
|
||||||
cparams.dtw_aheads_preset = WHISPER_AHEADS_LARGE_V1;
|
|
||||||
}
|
|
||||||
if (params.dtw == "large.v2") {
|
|
||||||
cparams.dtw_aheads_preset = WHISPER_AHEADS_LARGE_V2;
|
|
||||||
}
|
|
||||||
if (params.dtw == "large.v3") {
|
|
||||||
cparams.dtw_aheads_preset = WHISPER_AHEADS_LARGE_V3;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cparams.dtw_aheads_preset == WHISPER_AHEADS_NONE) {
|
|
||||||
fprintf(stderr, "error: unknown DTW preset '%s'\n", params.dtw.c_str());
|
|
||||||
return 3;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
|
struct whisper_context * ctx = whisper_init_from_file_with_params(params.model.c_str(), cparams);
|
||||||
|
|
||||||
@ -912,7 +865,6 @@ int main(int argc, char ** argv) {
|
|||||||
if (!params.no_timestamps) {
|
if (!params.no_timestamps) {
|
||||||
word["start"] = token.t0 * 0.01;
|
word["start"] = token.t0 * 0.01;
|
||||||
word["end"] = token.t1 * 0.01;
|
word["end"] = token.t1 * 0.01;
|
||||||
word["t_dtw"] = token.t_dtw;
|
|
||||||
}
|
}
|
||||||
word["probability"] = token.p;
|
word["probability"] = token.p;
|
||||||
total_logprob += token.plog;
|
total_logprob += token.plog;
|
||||||
|
@ -30,13 +30,9 @@ a transcription block that is suitable for parsing.
|
|||||||
The `stream` tool depends on SDL2 library to capture audio from the microphone. You can build it like this:
|
The `stream` tool depends on SDL2 library to capture audio from the microphone. You can build it like this:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Install SDL2
|
# Install SDL2 on Linux
|
||||||
# On Debian based linux distributions:
|
|
||||||
sudo apt-get install libsdl2-dev
|
sudo apt-get install libsdl2-dev
|
||||||
|
|
||||||
# On Fedora Linux:
|
|
||||||
sudo dnf install SDL2 SDL2-devel
|
|
||||||
|
|
||||||
# Install SDL2 on Mac OS
|
# Install SDL2 on Mac OS
|
||||||
brew install sdl2
|
brew install sdl2
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
if (WHISPER_SDL2)
|
if (WHISPER_SDL2)
|
||||||
# talk-llama
|
# talk-llama
|
||||||
set(TARGET talk-llama)
|
set(TARGET talk-llama)
|
||||||
add_executable(${TARGET} talk-llama.cpp llama.cpp unicode.cpp unicode-data.cpp)
|
add_executable(${TARGET} talk-llama.cpp llama.cpp unicode.cpp)
|
||||||
target_include_directories(${TARGET} PRIVATE ${SDL2_INCLUDE_DIRS})
|
target_include_directories(${TARGET} PRIVATE ${SDL2_INCLUDE_DIRS})
|
||||||
|
|
||||||
if (WHISPER_CLBLAST)
|
if (WHISPER_CLBLAST)
|
||||||
|
@ -15,13 +15,9 @@ https://github.com/ggerganov/whisper.cpp/assets/1991296/d97a3788-bf2a-4756-9a43-
|
|||||||
The `talk-llama` tool depends on SDL2 library to capture audio from the microphone. You can build it like this:
|
The `talk-llama` tool depends on SDL2 library to capture audio from the microphone. You can build it like this:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Install SDL2
|
# Install SDL2 on Linux
|
||||||
# On Debian based linux distributions:
|
|
||||||
sudo apt-get install libsdl2-dev
|
sudo apt-get install libsdl2-dev
|
||||||
|
|
||||||
# On Fedora Linux:
|
|
||||||
sudo dnf install SDL2 SDL2-devel
|
|
||||||
|
|
||||||
# Install SDL2 on Mac OS
|
# Install SDL2 on Mac OS
|
||||||
brew install sdl2
|
brew install sdl2
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -39,7 +39,7 @@
|
|||||||
#define LLAMA_FILE_MAGIC_GGSN 0x6767736eu // 'ggsn'
|
#define LLAMA_FILE_MAGIC_GGSN 0x6767736eu // 'ggsn'
|
||||||
|
|
||||||
#define LLAMA_SESSION_MAGIC LLAMA_FILE_MAGIC_GGSN
|
#define LLAMA_SESSION_MAGIC LLAMA_FILE_MAGIC_GGSN
|
||||||
#define LLAMA_SESSION_VERSION 5
|
#define LLAMA_SESSION_VERSION 4
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@ -60,9 +60,9 @@ extern "C" {
|
|||||||
|
|
||||||
enum llama_vocab_type {
|
enum llama_vocab_type {
|
||||||
LLAMA_VOCAB_TYPE_NONE = 0, // For models without vocab
|
LLAMA_VOCAB_TYPE_NONE = 0, // For models without vocab
|
||||||
LLAMA_VOCAB_TYPE_SPM = 1, // LLaMA tokenizer based on byte-level BPE with byte fallback
|
LLAMA_VOCAB_TYPE_SPM = 1, // SentencePiece
|
||||||
LLAMA_VOCAB_TYPE_BPE = 2, // GPT-2 tokenizer based on byte-level BPE
|
LLAMA_VOCAB_TYPE_BPE = 2, // Byte Pair Encoding
|
||||||
LLAMA_VOCAB_TYPE_WPM = 3, // BERT tokenizer based on WordPiece
|
LLAMA_VOCAB_TYPE_WPM = 3, // WordPiece
|
||||||
};
|
};
|
||||||
|
|
||||||
// note: these values should be synchronized with ggml_rope
|
// note: these values should be synchronized with ggml_rope
|
||||||
@ -117,7 +117,6 @@ extern "C" {
|
|||||||
LLAMA_FTYPE_MOSTLY_IQ2_S = 28, // except 1d tensors
|
LLAMA_FTYPE_MOSTLY_IQ2_S = 28, // except 1d tensors
|
||||||
LLAMA_FTYPE_MOSTLY_IQ2_M = 29, // except 1d tensors
|
LLAMA_FTYPE_MOSTLY_IQ2_M = 29, // except 1d tensors
|
||||||
LLAMA_FTYPE_MOSTLY_IQ4_XS = 30, // except 1d tensors
|
LLAMA_FTYPE_MOSTLY_IQ4_XS = 30, // except 1d tensors
|
||||||
LLAMA_FTYPE_MOSTLY_IQ1_M = 31, // except 1d tensors
|
|
||||||
|
|
||||||
LLAMA_FTYPE_GUESSED = 1024, // not specified in the model file
|
LLAMA_FTYPE_GUESSED = 1024, // not specified in the model file
|
||||||
};
|
};
|
||||||
@ -278,14 +277,11 @@ extern "C" {
|
|||||||
typedef struct llama_model_quantize_params {
|
typedef struct llama_model_quantize_params {
|
||||||
int32_t nthread; // number of threads to use for quantizing, if <=0 will use std::thread::hardware_concurrency()
|
int32_t nthread; // number of threads to use for quantizing, if <=0 will use std::thread::hardware_concurrency()
|
||||||
enum llama_ftype ftype; // quantize to this llama_ftype
|
enum llama_ftype ftype; // quantize to this llama_ftype
|
||||||
enum ggml_type output_tensor_type; // output tensor type
|
|
||||||
enum ggml_type token_embedding_type; // itoken embeddings tensor type
|
|
||||||
bool allow_requantize; // allow quantizing non-f32/f16 tensors
|
bool allow_requantize; // allow quantizing non-f32/f16 tensors
|
||||||
bool quantize_output_tensor; // quantize output.weight
|
bool quantize_output_tensor; // quantize output.weight
|
||||||
bool only_copy; // only copy tensors - ftype, allow_requantize and quantize_output_tensor are ignored
|
bool only_copy; // only copy tensors - ftype, allow_requantize and quantize_output_tensor are ignored
|
||||||
bool pure; // quantize all tensors to the default type
|
bool pure; // quantize all tensors to the default type
|
||||||
void * imatrix; // pointer to importance matrix data
|
void * imatrix; // pointer to importance matrix data
|
||||||
void * kv_overrides; // pointer to vector containing overrides
|
|
||||||
} llama_model_quantize_params;
|
} llama_model_quantize_params;
|
||||||
|
|
||||||
// grammar types
|
// grammar types
|
||||||
@ -392,7 +388,6 @@ extern "C" {
|
|||||||
LLAMA_API int32_t llama_n_vocab (const struct llama_model * model);
|
LLAMA_API int32_t llama_n_vocab (const struct llama_model * model);
|
||||||
LLAMA_API int32_t llama_n_ctx_train(const struct llama_model * model);
|
LLAMA_API int32_t llama_n_ctx_train(const struct llama_model * model);
|
||||||
LLAMA_API int32_t llama_n_embd (const struct llama_model * model);
|
LLAMA_API int32_t llama_n_embd (const struct llama_model * model);
|
||||||
LLAMA_API int32_t llama_n_layer (const struct llama_model * model);
|
|
||||||
|
|
||||||
// Get the model's RoPE frequency scaling factor
|
// Get the model's RoPE frequency scaling factor
|
||||||
LLAMA_API float llama_rope_freq_scale_train(const struct llama_model * model);
|
LLAMA_API float llama_rope_freq_scale_train(const struct llama_model * model);
|
||||||
@ -445,20 +440,6 @@ extern "C" {
|
|||||||
const char * path_base_model,
|
const char * path_base_model,
|
||||||
int32_t n_threads);
|
int32_t n_threads);
|
||||||
|
|
||||||
// Apply a loaded control vector to a llama_context, or if data is NULL, clear
|
|
||||||
// the currently loaded vector.
|
|
||||||
// n_embd should be the size of a single layer's control, and data should point
|
|
||||||
// to an n_embd x n_layers buffer starting from layer 1.
|
|
||||||
// il_start and il_end are the layer range the vector should apply to (both inclusive)
|
|
||||||
// See llama_control_vector_load in common to load a control vector.
|
|
||||||
LLAMA_API int32_t llama_control_vector_apply(
|
|
||||||
struct llama_context * lctx,
|
|
||||||
const float * data,
|
|
||||||
size_t len,
|
|
||||||
int32_t n_embd,
|
|
||||||
int32_t il_start,
|
|
||||||
int32_t il_end);
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// KV cache
|
// KV cache
|
||||||
//
|
//
|
||||||
@ -678,29 +659,23 @@ extern "C" {
|
|||||||
LLAMA_API void llama_synchronize(struct llama_context * ctx);
|
LLAMA_API void llama_synchronize(struct llama_context * ctx);
|
||||||
|
|
||||||
// Token logits obtained from the last call to llama_decode()
|
// Token logits obtained from the last call to llama_decode()
|
||||||
// The logits for which llama_batch.logits[i] != 0 are stored contiguously
|
// The logits for the last token are stored in the last row
|
||||||
// in the order they have appeared in the batch.
|
// Logits for which llama_batch.logits[i] == 0 are undefined
|
||||||
// Rows: number of tokens for which llama_batch.logits[i] != 0
|
// Rows: n_tokens provided with llama_batch
|
||||||
// Cols: n_vocab
|
// Cols: n_vocab
|
||||||
LLAMA_API float * llama_get_logits(struct llama_context * ctx);
|
LLAMA_API float * llama_get_logits(struct llama_context * ctx);
|
||||||
|
|
||||||
// Logits for the ith token. Equivalent to:
|
// Logits for the ith token. Equivalent to:
|
||||||
// llama_get_logits(ctx) + ctx->output_ids[i]*n_vocab
|
// llama_get_logits(ctx) + i*n_vocab
|
||||||
// returns NULL for invalid ids.
|
|
||||||
LLAMA_API float * llama_get_logits_ith(struct llama_context * ctx, int32_t i);
|
LLAMA_API float * llama_get_logits_ith(struct llama_context * ctx, int32_t i);
|
||||||
|
|
||||||
// Get all output token embeddings.
|
// Get all output token embeddings
|
||||||
// when pooling_type == LLAMA_POOLING_TYPE_NONE or when using a generative model,
|
// shape: [n_tokens*n_embd] (1-dimensional)
|
||||||
// the embeddings for which llama_batch.logits[i] != 0 are stored contiguously
|
|
||||||
// in the order they have appeared in the batch.
|
|
||||||
// shape: [n_outputs*n_embd]
|
|
||||||
// Otherwise, returns NULL.
|
|
||||||
LLAMA_API float * llama_get_embeddings(struct llama_context * ctx);
|
LLAMA_API float * llama_get_embeddings(struct llama_context * ctx);
|
||||||
|
|
||||||
// Get the embeddings for the ith token. Equivalent to:
|
// Get the embeddings for the ith token
|
||||||
// llama_get_embeddings(ctx) + ctx->output_ids[i]*n_embd
|
// llama_get_embeddings(ctx) + i*n_embd
|
||||||
// shape: [n_embd] (1-dimensional)
|
// shape: [n_embd] (1-dimensional)
|
||||||
// returns NULL for invalid ids.
|
|
||||||
LLAMA_API float * llama_get_embeddings_ith(struct llama_context * ctx, int32_t i);
|
LLAMA_API float * llama_get_embeddings_ith(struct llama_context * ctx, int32_t i);
|
||||||
|
|
||||||
// Get the embeddings for a sequence id
|
// Get the embeddings for a sequence id
|
||||||
@ -970,16 +945,6 @@ extern "C" {
|
|||||||
int32_t n_past,
|
int32_t n_past,
|
||||||
int32_t n_predict);
|
int32_t n_predict);
|
||||||
|
|
||||||
/// @details Build a split GGUF final path for this chunk.
|
|
||||||
/// llama_split_path(split_path, sizeof(split_path), "/models/ggml-model-q4_0", 2, 4) => split_path = "/models/ggml-model-q4_0-00002-of-00004.gguf"
|
|
||||||
// Returns the split_path length.
|
|
||||||
LLAMA_API int llama_split_path(char * split_path, size_t maxlen, const char * path_prefix, int split_no, int split_count);
|
|
||||||
|
|
||||||
/// @details Extract the path prefix from the split_path if and only if the split_no and split_count match.
|
|
||||||
/// llama_split_prefix(split_prefix, 64, "/models/ggml-model-q4_0-00002-of-00004.gguf", 2, 4) => split_prefix = "/models/ggml-model-q4_0"
|
|
||||||
// Returns the split_prefix length.
|
|
||||||
LLAMA_API int llama_split_prefix(char * split_prefix, size_t maxlen, const char * split_path, int split_no, int split_count);
|
|
||||||
|
|
||||||
// Performance information
|
// Performance information
|
||||||
LLAMA_API struct llama_timings llama_get_timings(struct llama_context * ctx);
|
LLAMA_API struct llama_timings llama_get_timings(struct llama_context * ctx);
|
||||||
|
|
||||||
@ -1007,38 +972,10 @@ extern "C" {
|
|||||||
|
|
||||||
struct ggml_tensor;
|
struct ggml_tensor;
|
||||||
|
|
||||||
struct llama_partial_utf8 {
|
|
||||||
uint32_t value; // bit value so far (unshifted)
|
|
||||||
int n_remain; // num bytes remaining; -1 indicates invalid sequence
|
|
||||||
};
|
|
||||||
|
|
||||||
struct llama_grammar {
|
|
||||||
const std::vector<std::vector<llama_grammar_element>> rules;
|
|
||||||
std::vector<std::vector<const llama_grammar_element *>> stacks;
|
|
||||||
|
|
||||||
// buffer for partially generated UTF-8 sequence from accepted tokens
|
|
||||||
llama_partial_utf8 partial_utf8;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct llama_grammar_candidate {
|
|
||||||
size_t index;
|
|
||||||
const uint32_t * code_points;
|
|
||||||
llama_partial_utf8 partial_utf8;
|
|
||||||
};
|
|
||||||
|
|
||||||
const std::vector<std::pair<std::string, struct ggml_tensor *>> & llama_internal_get_tensor_map(
|
const std::vector<std::pair<std::string, struct ggml_tensor *>> & llama_internal_get_tensor_map(
|
||||||
struct llama_context * ctx
|
struct llama_context * ctx
|
||||||
);
|
);
|
||||||
|
|
||||||
std::vector<std::vector<const llama_grammar_element *>> llama_grammar_accept(
|
|
||||||
const std::vector<std::vector<llama_grammar_element>> & rules,
|
|
||||||
const std::vector<std::vector<const llama_grammar_element *>> & stacks,
|
|
||||||
const uint32_t chr);
|
|
||||||
|
|
||||||
std::pair<std::vector<uint32_t>, llama_partial_utf8> decode_utf8(
|
|
||||||
const std::string & src,
|
|
||||||
llama_partial_utf8 partial_start);
|
|
||||||
|
|
||||||
#endif // LLAMA_API_INTERNAL
|
#endif // LLAMA_API_INTERNAL
|
||||||
|
|
||||||
#endif // LLAMA_H
|
#endif // LLAMA_H
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,16 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
#include <map>
|
|
||||||
#include <utility>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
extern const std::vector<std::pair<uint32_t, uint32_t>> unicode_ranges_digit;
|
|
||||||
extern const std::vector<std::pair<uint32_t, uint32_t>> unicode_ranges_letter;
|
|
||||||
extern const std::vector<std::pair<uint32_t, uint32_t>> unicode_ranges_whitespace;
|
|
||||||
extern const std::vector<std::pair<uint32_t, uint32_t>> unicode_ranges_accent_mark;
|
|
||||||
extern const std::vector<std::pair<uint32_t, uint32_t>> unicode_ranges_punctuation;
|
|
||||||
extern const std::vector<std::pair<uint32_t, uint32_t>> unicode_ranges_symbol;
|
|
||||||
extern const std::vector<std::pair<uint32_t, uint32_t>> unicode_ranges_control;
|
|
||||||
extern const std::multimap<uint32_t, uint32_t> unicode_map_nfd;
|
|
||||||
extern const std::map<char32_t, char32_t> unicode_map_lowercase;
|
|
File diff suppressed because it is too large
Load Diff
@ -24,5 +24,3 @@ int unicode_cpt_type(const std::string & utf8);
|
|||||||
std::string unicode_byte_to_utf8(uint8_t byte);
|
std::string unicode_byte_to_utf8(uint8_t byte);
|
||||||
uint8_t unicode_utf8_to_byte(const std::string & utf8);
|
uint8_t unicode_utf8_to_byte(const std::string & utf8);
|
||||||
|
|
||||||
// simple tolower that only implements one-to-one mapping, not one-to-many
|
|
||||||
char32_t unicode_tolower(char32_t cp);
|
|
||||||
|
@ -11,13 +11,9 @@ Web version: [examples/talk.wasm](/examples/talk.wasm)
|
|||||||
The `talk` tool depends on SDL2 library to capture audio from the microphone. You can build it like this:
|
The `talk` tool depends on SDL2 library to capture audio from the microphone. You can build it like this:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Install SDL2
|
# Install SDL2 on Linux
|
||||||
# On Debian based linux distributions:
|
|
||||||
sudo apt-get install libsdl2-dev
|
sudo apt-get install libsdl2-dev
|
||||||
|
|
||||||
# On Fedora Linux:
|
|
||||||
sudo dnf install SDL2 SDL2-devel
|
|
||||||
|
|
||||||
# Install SDL2 on Mac OS
|
# Install SDL2 on Mac OS
|
||||||
brew install sdl2
|
brew install sdl2
|
||||||
|
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
|
set(CMAKE_CXX_STANDARD 11)
|
||||||
|
|
||||||
add_subdirectory(libwchess)
|
add_subdirectory(libwchess)
|
||||||
set_target_properties(wchess-core PROPERTIES FOLDER "libs")
|
|
||||||
|
|
||||||
if (EMSCRIPTEN)
|
if (EMSCRIPTEN)
|
||||||
add_subdirectory(wchess.wasm)
|
add_subdirectory(wchess.wasm)
|
||||||
set_target_properties(wchess.wasm PROPERTIES FOLDER "libs")
|
|
||||||
else()
|
else()
|
||||||
add_subdirectory(wchess.cmd)
|
add_subdirectory(wchess.cmd)
|
||||||
set_target_properties(wchess PROPERTIES FOLDER "libs")
|
|
||||||
endif()
|
endif()
|
||||||
|
@ -45,6 +45,6 @@ if [ ! -f ./models/ggml-${model}.bin ] ; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# fine-tune the parameters according to your machine specs
|
# fine-tune the parameters according to your machine specs
|
||||||
./stream -t 8 -m models/ggml-${model}.bin --step 350 --length 10000 -f /tmp/whisper.nvim 2> /dev/null
|
./stream -t 8 -m models/ggml-base.en.bin --step 350 --length 10000 -f /tmp/whisper.nvim 2> /dev/null
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
# Run from the build directory:
|
# Run from the build directory:
|
||||||
#
|
#
|
||||||
# cd build-em
|
# cd build-em
|
||||||
# ../scripts/deploy-wasm.sh
|
# ../extra/deploy-wasm.sh
|
||||||
#
|
#
|
||||||
|
|
||||||
# check if emcmake is available
|
# check if emcmake is available
|
@ -5,7 +5,7 @@
|
|||||||
# Usage:
|
# Usage:
|
||||||
#
|
#
|
||||||
# $ cd /path/to/whisper.cpp
|
# $ cd /path/to/whisper.cpp
|
||||||
# $ ./scripts/sync-ggml-am.sh -skip hash0,hash1,hash2...
|
# $ ./extra/sync-ggml-am.sh -skip hash0,hash1,hash2...
|
||||||
#
|
#
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
@ -21,7 +21,7 @@ if [ ! -d $SRC_GGML ]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
lc=$(cat $SRC_WHISPER/scripts/sync-ggml.last)
|
lc=$(cat $SRC_WHISPER/extra/sync-ggml.last)
|
||||||
echo "Syncing ggml changes since commit $lc"
|
echo "Syncing ggml changes since commit $lc"
|
||||||
|
|
||||||
to_skip=""
|
to_skip=""
|
||||||
@ -60,19 +60,14 @@ while read c; do
|
|||||||
src/ggml*.m \
|
src/ggml*.m \
|
||||||
src/ggml*.metal \
|
src/ggml*.metal \
|
||||||
src/ggml*.cu \
|
src/ggml*.cu \
|
||||||
src/ggml-cuda/* \
|
|
||||||
examples/common.h \
|
examples/common.h \
|
||||||
examples/common.cpp \
|
examples/common.cpp \
|
||||||
examples/common-ggml.h \
|
examples/common-ggml.h \
|
||||||
examples/common-ggml.cpp \
|
examples/common-ggml.cpp \
|
||||||
examples/whisper/grammar-parser.h \
|
|
||||||
examples/whisper/grammar-parser.cpp \
|
|
||||||
examples/whisper/whisper.h \
|
examples/whisper/whisper.h \
|
||||||
examples/whisper/whisper.cpp \
|
examples/whisper/whisper.cpp \
|
||||||
examples/whisper/main.cpp \
|
examples/whisper/main.cpp \
|
||||||
examples/whisper/quantize.cpp \
|
examples/whisper/quantize.cpp \
|
||||||
LICENSE \
|
|
||||||
scripts/gen-authors.sh \
|
|
||||||
>> $SRC_WHISPER/ggml-src.patch
|
>> $SRC_WHISPER/ggml-src.patch
|
||||||
done < $SRC_WHISPER/ggml-commits
|
done < $SRC_WHISPER/ggml-commits
|
||||||
|
|
||||||
@ -103,7 +98,6 @@ if [ -f $SRC_WHISPER/ggml-src.patch ]; then
|
|||||||
# src/ggml-backend-impl.h -> ggml-backend-impl.h
|
# src/ggml-backend-impl.h -> ggml-backend-impl.h
|
||||||
# src/ggml-backend.c -> ggml-backend.c
|
# src/ggml-backend.c -> ggml-backend.c
|
||||||
# src/ggml-common.h -> ggml-common.h
|
# src/ggml-common.h -> ggml-common.h
|
||||||
# src/ggml-cuda/* -> ggml-cuda/
|
|
||||||
# src/ggml-cuda.cu -> ggml-cuda.cu
|
# src/ggml-cuda.cu -> ggml-cuda.cu
|
||||||
# src/ggml-cuda.h -> ggml-cuda.h
|
# src/ggml-cuda.h -> ggml-cuda.h
|
||||||
# src/ggml-impl.h -> ggml-impl.h
|
# src/ggml-impl.h -> ggml-impl.h
|
||||||
@ -129,16 +123,11 @@ if [ -f $SRC_WHISPER/ggml-src.patch ]; then
|
|||||||
# examples/common.cpp -> examples/common.cpp
|
# examples/common.cpp -> examples/common.cpp
|
||||||
# examples/common-ggml.h -> examples/common-ggml.h
|
# examples/common-ggml.h -> examples/common-ggml.h
|
||||||
# examples/common-ggml.cpp -> examples/common-ggml.cpp
|
# examples/common-ggml.cpp -> examples/common-ggml.cpp
|
||||||
# examples/whisper/grammar-parser.h -> examples/grammar-parser.h
|
|
||||||
# examples/whisper/grammar-parser.cpp -> examples/grammar-parser.cpp
|
|
||||||
#
|
#
|
||||||
# examples/whisper/whisper.h -> whisper.h
|
# examples/whisper/whisper.h -> whisper.h
|
||||||
# examples/whisper/whisper.cpp -> whisper.cpp
|
# examples/whisper/whisper.cpp -> whisper.cpp
|
||||||
# examples/whisper/main.cpp -> examples/main/main.cpp
|
# examples/whisper/main.cpp -> examples/main/main.cpp
|
||||||
# examples/whisper/quantize.cpp -> examples/quantize/quantize.cpp
|
# examples/whisper/quantize.cpp -> examples/quantize/quantize.cpp
|
||||||
#
|
|
||||||
# LICENSE -> LICENSE
|
|
||||||
# ggml/scripts/gen-authors.sh -> scripts/gen-authors.sh
|
|
||||||
|
|
||||||
cat ggml-src.patch | sed \
|
cat ggml-src.patch | sed \
|
||||||
-e 's/src\/ggml\.c/ggml.c/g' \
|
-e 's/src\/ggml\.c/ggml.c/g' \
|
||||||
@ -146,7 +135,6 @@ if [ -f $SRC_WHISPER/ggml-src.patch ]; then
|
|||||||
-e 's/src\/ggml-backend-impl\.h/ggml-backend-impl.h/g' \
|
-e 's/src\/ggml-backend-impl\.h/ggml-backend-impl.h/g' \
|
||||||
-e 's/src\/ggml-backend\.c/ggml-backend.c/g' \
|
-e 's/src\/ggml-backend\.c/ggml-backend.c/g' \
|
||||||
-e 's/src\/ggml-common\.h/ggml-common.h/g' \
|
-e 's/src\/ggml-common\.h/ggml-common.h/g' \
|
||||||
-e 's/src\/ggml-cuda\//ggml-cuda\//g' \
|
|
||||||
-e 's/src\/ggml-cuda\.cu/ggml-cuda.cu/g' \
|
-e 's/src\/ggml-cuda\.cu/ggml-cuda.cu/g' \
|
||||||
-e 's/src\/ggml-cuda\.h/ggml-cuda.h/g' \
|
-e 's/src\/ggml-cuda\.h/ggml-cuda.h/g' \
|
||||||
-e 's/src\/ggml-impl\.h/ggml-impl.h/g' \
|
-e 's/src\/ggml-impl\.h/ggml-impl.h/g' \
|
||||||
@ -171,14 +159,10 @@ if [ -f $SRC_WHISPER/ggml-src.patch ]; then
|
|||||||
-e 's/examples\/common\.cpp/examples\/common.cpp/g' \
|
-e 's/examples\/common\.cpp/examples\/common.cpp/g' \
|
||||||
-e 's/examples\/common-ggml\.h/examples\/common-ggml.h/g' \
|
-e 's/examples\/common-ggml\.h/examples\/common-ggml.h/g' \
|
||||||
-e 's/examples\/common-ggml\.cpp/examples\/common-ggml.cpp/g' \
|
-e 's/examples\/common-ggml\.cpp/examples\/common-ggml.cpp/g' \
|
||||||
-e 's/examples\/whisper\/grammar-parser\.h/examples\/grammar-parser.h/g' \
|
|
||||||
-e 's/examples\/whisper\/grammar-parser\.cpp/examples\/grammar-parser.cpp/g' \
|
|
||||||
-e 's/examples\/whisper\/whisper\.h/whisper.h/g' \
|
-e 's/examples\/whisper\/whisper\.h/whisper.h/g' \
|
||||||
-e 's/examples\/whisper\/whisper\.cpp/whisper.cpp/g' \
|
-e 's/examples\/whisper\/whisper\.cpp/whisper.cpp/g' \
|
||||||
-e 's/examples\/whisper\/main\.cpp/examples\/main\/main.cpp/g' \
|
-e 's/examples\/whisper\/main\.cpp/examples\/main\/main.cpp/g' \
|
||||||
-e 's/examples\/whisper\/quantize\.cpp/examples\/quantize\/quantize.cpp/g' \
|
-e 's/examples\/whisper\/quantize\.cpp/examples\/quantize\/quantize.cpp/g' \
|
||||||
-e 's/LICENSE/LICENSE/g' \
|
|
||||||
-e 's/ggml\/scripts\/gen-authors\.sh/scripts\/gen-authors.sh/g' \
|
|
||||||
> ggml-src.patch.tmp
|
> ggml-src.patch.tmp
|
||||||
mv ggml-src.patch.tmp ggml-src.patch
|
mv ggml-src.patch.tmp ggml-src.patch
|
||||||
|
|
||||||
@ -189,7 +173,7 @@ fi
|
|||||||
|
|
||||||
# update last commit
|
# update last commit
|
||||||
cd $SRC_GGML
|
cd $SRC_GGML
|
||||||
git log -1 --format=%H > $SRC_WHISPER/scripts/sync-ggml.last
|
git log -1 --format=%H > $SRC_WHISPER/extra/sync-ggml.last
|
||||||
|
|
||||||
echo "Done"
|
echo "Done"
|
||||||
|
|
1
extra/sync-ggml.last
Normal file
1
extra/sync-ggml.last
Normal file
@ -0,0 +1 @@
|
|||||||
|
7652115c79fa0ffedb58a28c09cd2871403d5a20
|
@ -6,7 +6,6 @@ cp -rpv ../ggml/src/ggml-alloc.c ./ggml-alloc.c
|
|||||||
cp -rpv ../ggml/src/ggml-backend-impl.h ./ggml-backend-impl.h
|
cp -rpv ../ggml/src/ggml-backend-impl.h ./ggml-backend-impl.h
|
||||||
cp -rpv ../ggml/src/ggml-backend.c ./ggml-backend.c
|
cp -rpv ../ggml/src/ggml-backend.c ./ggml-backend.c
|
||||||
cp -rpv ../ggml/src/ggml-common.h ./ggml-common.h
|
cp -rpv ../ggml/src/ggml-common.h ./ggml-common.h
|
||||||
cp -rpv ../ggml/src/ggml-cuda/* ./ggml-cuda/
|
|
||||||
cp -rpv ../ggml/src/ggml-cuda.cu ./ggml-cuda.cu
|
cp -rpv ../ggml/src/ggml-cuda.cu ./ggml-cuda.cu
|
||||||
cp -rpv ../ggml/src/ggml-cuda.h ./ggml-cuda.h
|
cp -rpv ../ggml/src/ggml-cuda.h ./ggml-cuda.h
|
||||||
cp -rpv ../ggml/src/ggml-kompute.cpp ./ggml-kompute.cpp
|
cp -rpv ../ggml/src/ggml-kompute.cpp ./ggml-kompute.cpp
|
||||||
@ -33,13 +32,9 @@ cp -rpv ../ggml/examples/common.h ./examples/common.h
|
|||||||
cp -rpv ../ggml/examples/common.cpp ./examples/common.cpp
|
cp -rpv ../ggml/examples/common.cpp ./examples/common.cpp
|
||||||
cp -rpv ../ggml/examples/common-ggml.h ./examples/common-ggml.h
|
cp -rpv ../ggml/examples/common-ggml.h ./examples/common-ggml.h
|
||||||
cp -rpv ../ggml/examples/common-ggml.cpp ./examples/common-ggml.cpp
|
cp -rpv ../ggml/examples/common-ggml.cpp ./examples/common-ggml.cpp
|
||||||
cp -rpv ../ggml/examples/whisper/grammar-parser.h ./examples/grammar-parser.h
|
|
||||||
cp -rpv ../ggml/examples/whisper/grammar-parser.cpp ./examples/grammar-parser.cpp
|
|
||||||
|
|
||||||
cp -rpv ../ggml/examples/whisper/whisper.h ./whisper.h
|
cp -rpv ../ggml/examples/whisper/whisper.h ./whisper.h
|
||||||
cp -rpv ../ggml/examples/whisper/whisper.cpp ./whisper.cpp
|
cp -rpv ../ggml/examples/whisper/whisper.cpp ./whisper.cpp
|
||||||
cp -rpv ../ggml/examples/whisper/main.cpp ./examples/main/main.cpp
|
cp -rpv ../ggml/examples/whisper/main.cpp ./examples/main/main.cpp
|
||||||
cp -rpv ../ggml/examples/whisper/quantize.cpp ./examples/quantize/quantize.cpp
|
cp -rpv ../ggml/examples/whisper/quantize.cpp ./examples/quantize/quantize.cpp
|
||||||
|
|
||||||
cp -rpv ../LICENSE ./LICENSE
|
|
||||||
cp -rpv ../ggml/scripts/gen-authors.sh ./scripts/gen-authors.sh
|
|
6
extra/sync-llama.sh
Executable file
6
extra/sync-llama.sh
Executable file
@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
cp -rpv ../llama.cpp/llama.h ./examples/talk-llama/llama.h
|
||||||
|
cp -rpv ../llama.cpp/llama.cpp ./examples/talk-llama/llama.cpp
|
||||||
|
cp -rpv ../llama.cpp/unicode.h ./examples/talk-llama/unicode.h
|
||||||
|
cp -rpv ../llama.cpp/unicode.cpp ./examples/talk-llama/unicode.cpp
|
10
ggml-alloc.c
10
ggml-alloc.c
@ -548,11 +548,7 @@ static void ggml_gallocr_alloc_graph_impl(ggml_gallocr_t galloc, struct ggml_cgr
|
|||||||
for (int i = 0; i < graph->n_nodes; i++) {
|
for (int i = 0; i < graph->n_nodes; i++) {
|
||||||
struct ggml_tensor * node = graph->nodes[i];
|
struct ggml_tensor * node = graph->nodes[i];
|
||||||
|
|
||||||
// TODO: better way to add external dependencies
|
if (ggml_is_view(node)) {
|
||||||
// GGML_OP_NONE does not appear normally in the graph nodes, but is used by ggml-backend to add dependencies to
|
|
||||||
// control when some tensors are allocated and freed. in this case, the dependencies are in `src`, but the node
|
|
||||||
// itself is never used and should not be considered a dependency
|
|
||||||
if (ggml_is_view(node) && node->op != GGML_OP_NONE) {
|
|
||||||
struct ggml_tensor * view_src = node->view_src;
|
struct ggml_tensor * view_src = node->view_src;
|
||||||
ggml_gallocr_hash_get(galloc, view_src)->n_views += 1;
|
ggml_gallocr_hash_get(galloc, view_src)->n_views += 1;
|
||||||
}
|
}
|
||||||
@ -569,8 +565,8 @@ static void ggml_gallocr_alloc_graph_impl(ggml_gallocr_t galloc, struct ggml_cgr
|
|||||||
|
|
||||||
ggml_gallocr_hash_get(galloc, src)->n_children += 1;
|
ggml_gallocr_hash_get(galloc, src)->n_children += 1;
|
||||||
|
|
||||||
// allocate explicit inputs
|
// allocate explicit inputs and leafs
|
||||||
if (src->flags & GGML_TENSOR_FLAG_INPUT) {
|
if (src->flags & GGML_TENSOR_FLAG_INPUT || src->op == GGML_OP_NONE) {
|
||||||
ggml_gallocr_allocate_node(galloc, src, get_node_buffer_id(node_buffer_ids, i));
|
ggml_gallocr_allocate_node(galloc, src, get_node_buffer_id(node_buffer_ids, i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -103,11 +103,6 @@ extern "C" {
|
|||||||
// check if the backend supports an operation
|
// check if the backend supports an operation
|
||||||
bool (*GGML_CALL supports_op)(ggml_backend_t backend, const struct ggml_tensor * op);
|
bool (*GGML_CALL supports_op)(ggml_backend_t backend, const struct ggml_tensor * op);
|
||||||
|
|
||||||
// check if the backend wants to run an operation, even if the weights are allocated in a CPU buffer
|
|
||||||
// these should be expensive operations with large batch sizes that may benefit from running on this backend
|
|
||||||
// even if the weight has to be copied from the CPU temporarily
|
|
||||||
bool (*GGML_CALL offload_op)(ggml_backend_t backend, const struct ggml_tensor * op);
|
|
||||||
|
|
||||||
// (optional) event synchronization
|
// (optional) event synchronization
|
||||||
ggml_backend_event_t (*GGML_CALL event_new) (ggml_backend_t backend);
|
ggml_backend_event_t (*GGML_CALL event_new) (ggml_backend_t backend);
|
||||||
void (*GGML_CALL event_free) (ggml_backend_event_t event);
|
void (*GGML_CALL event_free) (ggml_backend_event_t event);
|
||||||
|
278
ggml-backend.c
278
ggml-backend.c
@ -278,7 +278,7 @@ enum ggml_status ggml_backend_graph_compute(ggml_backend_t backend, struct ggml_
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
enum ggml_status ggml_backend_graph_compute_async(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
|
bool ggml_backend_graph_compute_async(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
|
||||||
return backend->iface.graph_compute(backend, cgraph);
|
return backend->iface.graph_compute(backend, cgraph);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -286,13 +286,6 @@ bool ggml_backend_supports_op(ggml_backend_t backend, const struct ggml_tensor *
|
|||||||
return backend->iface.supports_op(backend, op);
|
return backend->iface.supports_op(backend, op);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ggml_backend_offload_op(ggml_backend_t backend, const struct ggml_tensor * op) {
|
|
||||||
if (backend->iface.offload_op != NULL) {
|
|
||||||
return backend->iface.offload_op(backend, op);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// backend copy
|
// backend copy
|
||||||
|
|
||||||
static bool ggml_are_same_layout(const struct ggml_tensor * a, const struct ggml_tensor * b) {
|
static bool ggml_are_same_layout(const struct ggml_tensor * a, const struct ggml_tensor * b) {
|
||||||
@ -420,7 +413,7 @@ GGML_CALL static void ggml_backend_registry_init(void) {
|
|||||||
ggml_backend_register("CPU", ggml_backend_reg_cpu_init, ggml_backend_cpu_buffer_type(), NULL);
|
ggml_backend_register("CPU", ggml_backend_reg_cpu_init, ggml_backend_cpu_buffer_type(), NULL);
|
||||||
|
|
||||||
// add forward decls here to avoid including the backend headers
|
// add forward decls here to avoid including the backend headers
|
||||||
#ifdef GGML_USE_CUDA
|
#ifdef GGML_USE_CUBLAS
|
||||||
extern GGML_CALL void ggml_backend_cuda_reg_devices(void);
|
extern GGML_CALL void ggml_backend_cuda_reg_devices(void);
|
||||||
ggml_backend_cuda_reg_devices();
|
ggml_backend_cuda_reg_devices();
|
||||||
#endif
|
#endif
|
||||||
@ -768,10 +761,6 @@ GGML_CALL static ggml_backend_graph_plan_t ggml_backend_cpu_graph_plan_create(gg
|
|||||||
|
|
||||||
if (cpu_plan->cplan.work_size > 0) {
|
if (cpu_plan->cplan.work_size > 0) {
|
||||||
cpu_plan->cplan.work_data = malloc(cpu_plan->cplan.work_size);
|
cpu_plan->cplan.work_data = malloc(cpu_plan->cplan.work_size);
|
||||||
if (cpu_plan->cplan.work_data == NULL) {
|
|
||||||
free(cpu_plan);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cpu_plan->cplan.abort_callback = cpu_ctx->abort_callback;
|
cpu_plan->cplan.abort_callback = cpu_ctx->abort_callback;
|
||||||
@ -845,7 +834,6 @@ static struct ggml_backend_i cpu_backend_i = {
|
|||||||
/* .graph_plan_compute = */ ggml_backend_cpu_graph_plan_compute,
|
/* .graph_plan_compute = */ ggml_backend_cpu_graph_plan_compute,
|
||||||
/* .graph_compute = */ ggml_backend_cpu_graph_compute,
|
/* .graph_compute = */ ggml_backend_cpu_graph_compute,
|
||||||
/* .supports_op = */ ggml_backend_cpu_supports_op,
|
/* .supports_op = */ ggml_backend_cpu_supports_op,
|
||||||
/* .offload_op = */ NULL,
|
|
||||||
/* .event_new = */ NULL,
|
/* .event_new = */ NULL,
|
||||||
/* .event_free = */ NULL,
|
/* .event_free = */ NULL,
|
||||||
/* .event_record = */ NULL,
|
/* .event_record = */ NULL,
|
||||||
@ -1011,11 +999,11 @@ static bool ggml_is_view_op(enum ggml_op op) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef GGML_SCHED_MAX_SPLITS
|
#ifndef GGML_SCHED_MAX_SPLITS
|
||||||
#define GGML_SCHED_MAX_SPLITS 2048
|
#define GGML_SCHED_MAX_SPLITS 256
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef GGML_SCHED_MAX_SPLIT_INPUTS
|
#ifndef GGML_SCHED_MAX_SPLIT_INPUTS
|
||||||
#define GGML_SCHED_MAX_SPLIT_INPUTS GGML_MAX_SRC
|
#define GGML_SCHED_MAX_SPLIT_INPUTS 16
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef GGML_SCHED_MAX_COPIES
|
#ifndef GGML_SCHED_MAX_COPIES
|
||||||
@ -1055,9 +1043,8 @@ struct ggml_backend_sched {
|
|||||||
struct ggml_cgraph * graph;
|
struct ggml_cgraph * graph;
|
||||||
|
|
||||||
// graph splits
|
// graph splits
|
||||||
struct ggml_backend_sched_split * splits;
|
struct ggml_backend_sched_split splits[GGML_SCHED_MAX_SPLITS];
|
||||||
int n_splits;
|
int n_splits;
|
||||||
int splits_capacity;
|
|
||||||
|
|
||||||
// pipeline parallelism support
|
// pipeline parallelism support
|
||||||
int n_copies;
|
int n_copies;
|
||||||
@ -1127,48 +1114,40 @@ static int ggml_backend_sched_backend_id_from_cur(ggml_backend_sched_t sched, st
|
|||||||
// TODO: use supports_op to check if the backend supports the op
|
// TODO: use supports_op to check if the backend supports the op
|
||||||
|
|
||||||
// assign pre-allocated nodes to their backend
|
// assign pre-allocated nodes to their backend
|
||||||
int cur_backend_id = ggml_backend_sched_backend_from_buffer(sched, tensor);
|
// dst
|
||||||
if (cur_backend_id != -1) {
|
int cur_backend = ggml_backend_sched_backend_from_buffer(sched, tensor);
|
||||||
|
if (cur_backend != -1) {
|
||||||
SET_CAUSE(tensor, "1.dst");
|
SET_CAUSE(tensor, "1.dst");
|
||||||
return cur_backend_id;
|
return cur_backend;
|
||||||
}
|
}
|
||||||
|
|
||||||
// view_src
|
// view_src
|
||||||
if (tensor->view_src != NULL) {
|
if (tensor->view_src != NULL) {
|
||||||
cur_backend_id = ggml_backend_sched_backend_from_buffer(sched, tensor->view_src);
|
cur_backend = ggml_backend_sched_backend_from_buffer(sched, tensor->view_src);
|
||||||
if (cur_backend_id != -1) {
|
if (cur_backend != -1) {
|
||||||
SET_CAUSE(tensor, "1.vsrc");
|
SET_CAUSE(tensor, "1.vsrc");
|
||||||
return cur_backend_id;
|
return cur_backend;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// graph input
|
// input
|
||||||
if (tensor->flags & GGML_TENSOR_FLAG_INPUT) {
|
if (tensor->flags & GGML_TENSOR_FLAG_INPUT) {
|
||||||
cur_backend_id = sched->n_backends - 1; // last backend (assumed CPU)
|
cur_backend = sched->n_backends - 1; // last backend (assumed CPU)
|
||||||
SET_CAUSE(tensor, "1.inp");
|
SET_CAUSE(tensor, "1.inp");
|
||||||
return cur_backend_id;
|
return cur_backend;
|
||||||
}
|
}
|
||||||
|
|
||||||
// assign nodes that use weights to the backend of the weights
|
// assign nodes that use weights to the backend of the weights
|
||||||
// operations with weights are preferably run on the same backend as the weights
|
|
||||||
for (int i = 0; i < GGML_MAX_SRC; i++) {
|
for (int i = 0; i < GGML_MAX_SRC; i++) {
|
||||||
const struct ggml_tensor * src = tensor->src[i];
|
const struct ggml_tensor * src = tensor->src[i];
|
||||||
if (src == NULL) {
|
if (src == NULL) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (src->buffer != NULL && src->buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS) {
|
if (src->buffer != NULL && src->buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS) {
|
||||||
int src_backend_id = ggml_backend_sched_backend_from_buffer(sched, src);
|
int src_backend = ggml_backend_sched_backend_from_buffer(sched, src);
|
||||||
// check if a backend with higher prio wants to offload the op
|
// operations with weights are always run on the same backend as the weights
|
||||||
if (src_backend_id == sched->n_backends - 1) {
|
|
||||||
for (int b = 0; b < src_backend_id; b++) {
|
|
||||||
if (ggml_backend_offload_op(sched->backends[b], tensor)) {
|
|
||||||
SET_CAUSE(tensor, "1.off");
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
SET_CAUSE(tensor, "1.wgt%d", i);
|
SET_CAUSE(tensor, "1.wgt%d", i);
|
||||||
return src_backend_id;
|
return src_backend;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1248,31 +1227,28 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
|
|||||||
// pass 1: assign backends to ops with pre-allocated inputs
|
// pass 1: assign backends to ops with pre-allocated inputs
|
||||||
for (int i = 0; i < graph->n_leafs; i++) {
|
for (int i = 0; i < graph->n_leafs; i++) {
|
||||||
struct ggml_tensor * leaf = graph->leafs[i];
|
struct ggml_tensor * leaf = graph->leafs[i];
|
||||||
int * leaf_backend_id = &tensor_backend_id(leaf);
|
if (tensor_backend_id(leaf) != -1) {
|
||||||
if (*leaf_backend_id != -1) {
|
|
||||||
// do not overwrite user assignments
|
// do not overwrite user assignments
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
*leaf_backend_id = ggml_backend_sched_backend_id_from_cur(sched, leaf);
|
tensor_backend_id(leaf) = ggml_backend_sched_backend_id_from_cur(sched, leaf);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < graph->n_nodes; i++) {
|
for (int i = 0; i < graph->n_nodes; i++) {
|
||||||
struct ggml_tensor * node = graph->nodes[i];
|
struct ggml_tensor * node = graph->nodes[i];
|
||||||
int * node_backend_id = &tensor_backend_id(node);
|
if (tensor_backend_id(node) != -1) {
|
||||||
if (*node_backend_id != -1) {
|
|
||||||
// do not overwrite user assignments
|
// do not overwrite user assignments
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
*node_backend_id = ggml_backend_sched_backend_id_from_cur(sched, node);
|
tensor_backend_id(node) = ggml_backend_sched_backend_id_from_cur(sched, node);
|
||||||
// src
|
// src
|
||||||
for (int j = 0; j < GGML_MAX_SRC; j++) {
|
for (int j = 0; j < GGML_MAX_SRC; j++) {
|
||||||
struct ggml_tensor * src = node->src[j];
|
struct ggml_tensor * src = node->src[j];
|
||||||
if (src == NULL) {
|
if (src == NULL) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
int * src_backend_id = &tensor_backend_id(src);
|
if (tensor_backend_id(src) == -1) {
|
||||||
if (*src_backend_id == -1) {
|
tensor_backend_id(src) = ggml_backend_sched_backend_id_from_cur(sched, src);
|
||||||
*src_backend_id = ggml_backend_sched_backend_id_from_cur(sched, src);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1294,20 +1270,21 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
|
|||||||
if (ggml_is_view_op(node->op)) {
|
if (ggml_is_view_op(node->op)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
int * node_backend_id = &tensor_backend_id(node);
|
int tensor_backend_id = tensor_backend_id(node);
|
||||||
if (*node_backend_id != -1) {
|
if (tensor_backend_id != -1) {
|
||||||
if (*node_backend_id == sched->n_backends - 1) {
|
if (tensor_backend_id == sched->n_backends - 1) {
|
||||||
// skip cpu (lowest prio backend)
|
// skip cpu (lowest prio backend)
|
||||||
cur_backend_id = -1;
|
cur_backend_id = -1;
|
||||||
} else {
|
} else {
|
||||||
cur_backend_id = *node_backend_id;
|
cur_backend_id = tensor_backend_id;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
*node_backend_id = cur_backend_id;
|
tensor_backend_id(node) = cur_backend_id;
|
||||||
SET_CAUSE(node, "2.2");
|
SET_CAUSE(node, "2.2");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pass 2.1 expand gpu up
|
// pass 2.1 expand gpu up
|
||||||
{
|
{
|
||||||
int cur_backend_id = -1;
|
int cur_backend_id = -1;
|
||||||
@ -1316,20 +1293,22 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
|
|||||||
if (ggml_is_view_op(node->op)) {
|
if (ggml_is_view_op(node->op)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
int * node_backend_id = &tensor_backend_id(node);
|
int tensor_backend_id = tensor_backend_id(node);
|
||||||
if (*node_backend_id != -1) {
|
if (tensor_backend_id != -1) {
|
||||||
if (*node_backend_id == sched->n_backends - 1) {
|
if (tensor_backend_id == sched->n_backends - 1) {
|
||||||
// skip cpu (lowest prio backend)
|
// skip cpu (lowest prio backend)
|
||||||
cur_backend_id = -1;
|
cur_backend_id = -1;
|
||||||
} else {
|
} else {
|
||||||
cur_backend_id = *node_backend_id;
|
cur_backend_id = tensor_backend_id;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
*node_backend_id = cur_backend_id;
|
tensor_backend_id(node) = cur_backend_id;
|
||||||
SET_CAUSE(node, "2.1");
|
SET_CAUSE(node, "2.1");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// pass 2.4 expand rest down
|
// pass 2.4 expand rest down
|
||||||
{
|
{
|
||||||
int cur_backend_id = -1;
|
int cur_backend_id = -1;
|
||||||
@ -1338,11 +1317,11 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
|
|||||||
if (ggml_is_view_op(node->op)) {
|
if (ggml_is_view_op(node->op)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
int * node_backend_id = &tensor_backend_id(node);
|
int tensor_backend_id = tensor_backend_id(node);
|
||||||
if (*node_backend_id != -1) {
|
if (tensor_backend_id != -1) {
|
||||||
cur_backend_id = *node_backend_id;
|
cur_backend_id = tensor_backend_id;
|
||||||
} else {
|
} else {
|
||||||
*node_backend_id = cur_backend_id;
|
tensor_backend_id(node) = cur_backend_id;
|
||||||
SET_CAUSE(node, "2.4");
|
SET_CAUSE(node, "2.4");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1355,11 +1334,11 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
|
|||||||
if (ggml_is_view_op(node->op)) {
|
if (ggml_is_view_op(node->op)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
int * node_backend_id = &tensor_backend_id(node);
|
int tensor_backend_id = tensor_backend_id(node);
|
||||||
if (*node_backend_id != -1) {
|
if (tensor_backend_id != -1) {
|
||||||
cur_backend_id = *node_backend_id;
|
cur_backend_id = tensor_backend_id;
|
||||||
} else {
|
} else {
|
||||||
*node_backend_id = cur_backend_id;
|
tensor_backend_id(node) = cur_backend_id;
|
||||||
SET_CAUSE(node, "2.3");
|
SET_CAUSE(node, "2.3");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1372,9 +1351,9 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
|
|||||||
// pass 3: assign backends to remaining src from dst and view_src
|
// pass 3: assign backends to remaining src from dst and view_src
|
||||||
for (int i = 0; i < graph->n_nodes; i++) {
|
for (int i = 0; i < graph->n_nodes; i++) {
|
||||||
struct ggml_tensor * node = graph->nodes[i];
|
struct ggml_tensor * node = graph->nodes[i];
|
||||||
int * cur_backend_id = &tensor_backend_id(node);
|
int cur_backend_id = tensor_backend_id(node);
|
||||||
if (node->view_src != NULL && *cur_backend_id == -1) {
|
if (node->view_src != NULL && cur_backend_id == -1) {
|
||||||
*cur_backend_id = tensor_backend_id(node->view_src);
|
cur_backend_id = tensor_backend_id(node) = tensor_backend_id(node->view_src);
|
||||||
SET_CAUSE(node, "3.vsrc");
|
SET_CAUSE(node, "3.vsrc");
|
||||||
}
|
}
|
||||||
for (int j = 0; j < GGML_MAX_SRC; j++) {
|
for (int j = 0; j < GGML_MAX_SRC; j++) {
|
||||||
@ -1382,14 +1361,14 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
|
|||||||
if (src == NULL) {
|
if (src == NULL) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
int * src_backend_id = &tensor_backend_id(src);
|
int src_backend_id = tensor_backend_id(src);
|
||||||
if (*src_backend_id == -1) {
|
if (src_backend_id == -1) {
|
||||||
if (src->view_src != NULL) {
|
if (src->view_src != NULL) {
|
||||||
// views are always on the same backend as the source
|
// views are always on the same backend as the source
|
||||||
*src_backend_id = tensor_backend_id(src->view_src);
|
tensor_backend_id(src) = tensor_backend_id(src->view_src);
|
||||||
SET_CAUSE(src, "3.vsrc");
|
SET_CAUSE(src, "3.vsrc");
|
||||||
} else {
|
} else {
|
||||||
*src_backend_id = *cur_backend_id;
|
tensor_backend_id(src) = cur_backend_id;
|
||||||
SET_CAUSE(src, "3.cur");
|
SET_CAUSE(src, "3.cur");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1401,20 +1380,19 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
|
|||||||
|
|
||||||
// pass 4: split graph, find tensors that need to be copied
|
// pass 4: split graph, find tensors that need to be copied
|
||||||
{
|
{
|
||||||
int i_split = 0;
|
int cur_split = 0;
|
||||||
struct ggml_backend_sched_split * split = &sched->splits[0];
|
|
||||||
// find the backend of the first split, skipping view ops
|
// find the backend of the first split, skipping view ops
|
||||||
for (int i = 0; i < graph->n_nodes; i++) {
|
for (int i = 0; i < graph->n_nodes; i++) {
|
||||||
struct ggml_tensor * node = graph->nodes[i];
|
struct ggml_tensor * node = graph->nodes[i];
|
||||||
if (!ggml_is_view_op(node->op)) {
|
if (!ggml_is_view_op(node->op)) {
|
||||||
split->backend_id = tensor_backend_id(node);
|
sched->splits[0].backend_id = tensor_backend_id(node);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
split->i_start = 0;
|
sched->splits[0].i_start = 0;
|
||||||
split->n_inputs = 0;
|
sched->splits[0].n_inputs = 0;
|
||||||
memset(split->inputs, 0, sizeof(split->inputs)); //HACK
|
memset(sched->splits[0].inputs, 0, sizeof(sched->splits[0].inputs)); //HACK
|
||||||
int cur_backend_id = split->backend_id;
|
int cur_backend_id = sched->splits[0].backend_id;
|
||||||
for (int i = 0; i < graph->n_nodes; i++) {
|
for (int i = 0; i < graph->n_nodes; i++) {
|
||||||
struct ggml_tensor * node = graph->nodes[i];
|
struct ggml_tensor * node = graph->nodes[i];
|
||||||
|
|
||||||
@ -1422,54 +1400,18 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int node_backend_id = tensor_backend_id(node);
|
int tensor_backend_id = tensor_backend_id(node);
|
||||||
|
|
||||||
GGML_ASSERT(node_backend_id != -1); // all nodes should be assigned by now
|
GGML_ASSERT(tensor_backend_id != -1); // all nodes should be assigned by now
|
||||||
|
|
||||||
// check if we should start a new split based on the sources of the current node
|
if (tensor_backend_id != cur_backend_id) {
|
||||||
bool need_new_split = false;
|
sched->splits[cur_split].i_end = i;
|
||||||
if (node_backend_id == cur_backend_id && split->n_inputs > 0) {
|
cur_split++;
|
||||||
for (int j = 0; j < GGML_MAX_SRC; j++) {
|
GGML_ASSERT(cur_split < GGML_SCHED_MAX_SPLITS);
|
||||||
struct ggml_tensor * src = node->src[j];
|
sched->splits[cur_split].backend_id = tensor_backend_id;
|
||||||
if (src == NULL) {
|
sched->splits[cur_split].i_start = i;
|
||||||
continue;
|
sched->splits[cur_split].n_inputs = 0;
|
||||||
}
|
cur_backend_id = tensor_backend_id;
|
||||||
// check if a weight is on a different backend
|
|
||||||
// by starting a new split, the memory of the previously offloaded weights can be reused
|
|
||||||
if (src->buffer != NULL && src->buffer->usage == GGML_BACKEND_BUFFER_USAGE_WEIGHTS) {
|
|
||||||
int src_backend_id = tensor_backend_id(src);
|
|
||||||
if (src_backend_id != -1 && src_backend_id != cur_backend_id) {
|
|
||||||
need_new_split = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// check if the split has too many inputs
|
|
||||||
if (split->n_inputs == GGML_SCHED_MAX_SPLIT_INPUTS) {
|
|
||||||
const size_t id = hash_id(src);
|
|
||||||
int src_backend_id = sched->tensor_backend_id[id];
|
|
||||||
if (src_backend_id != cur_backend_id && sched->tensor_copies[hash_id(src)][cur_backend_id][0] == NULL) {
|
|
||||||
//printf("starting new split because of too many inputs: node %s, input %s\n", node->name, src->name);
|
|
||||||
need_new_split = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (node_backend_id != cur_backend_id || need_new_split) {
|
|
||||||
split->i_end = i;
|
|
||||||
i_split++;
|
|
||||||
if (i_split >= sched->splits_capacity) {
|
|
||||||
sched->splits_capacity *= 2;
|
|
||||||
sched->splits = realloc(sched->splits, sched->splits_capacity * sizeof(struct ggml_backend_sched_split));
|
|
||||||
GGML_ASSERT(sched->splits != NULL);
|
|
||||||
}
|
|
||||||
GGML_ASSERT(i_split < GGML_SCHED_MAX_SPLITS);
|
|
||||||
split = &sched->splits[i_split];
|
|
||||||
split->backend_id = node_backend_id;
|
|
||||||
split->i_start = i;
|
|
||||||
split->n_inputs = 0;
|
|
||||||
cur_backend_id = node_backend_id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// find inputs that are not on the same backend
|
// find inputs that are not on the same backend
|
||||||
@ -1479,10 +1421,10 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int src_backend_id = tensor_backend_id(src);
|
int src_backend_id = tensor_backend_id(src);
|
||||||
assert(src_backend_id != -1); // all inputs should be assigned by now
|
assert(src_backend_id != -1); // all inputs should be assigned by now
|
||||||
|
|
||||||
if (src->flags & GGML_TENSOR_FLAG_INPUT && sched->n_copies > 1) {
|
if (src->flags & GGML_TENSOR_FLAG_INPUT) {
|
||||||
size_t id = hash_id(src);
|
size_t id = hash_id(src);
|
||||||
if (sched->tensor_copies[id][src_backend_id][0] == NULL) {
|
if (sched->tensor_copies[id][src_backend_id][0] == NULL) {
|
||||||
ggml_backend_t backend = sched->backends[src_backend_id];
|
ggml_backend_t backend = sched->backends[src_backend_id];
|
||||||
@ -1499,6 +1441,7 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
|
|||||||
ggml_set_output(tensor_copy); // prevent ggml-alloc from overwriting the tensor
|
ggml_set_output(tensor_copy); // prevent ggml-alloc from overwriting the tensor
|
||||||
}
|
}
|
||||||
sched->tensor_copies[id][src_backend_id][c] = tensor_copy;
|
sched->tensor_copies[id][src_backend_id][c] = tensor_copy;
|
||||||
|
tensor_backend_id(tensor_copy) = src_backend_id;
|
||||||
SET_CAUSE(tensor_copy, "4.cpy");
|
SET_CAUSE(tensor_copy, "4.cpy");
|
||||||
}
|
}
|
||||||
int n_graph_inputs = sched->n_graph_inputs++;
|
int n_graph_inputs = sched->n_graph_inputs++;
|
||||||
@ -1507,9 +1450,9 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (src_backend_id != node_backend_id) {
|
if (src_backend_id != tensor_backend_id) {
|
||||||
// create a copy of the input in the split's backend
|
// create a copy of the input in the split's backend
|
||||||
const size_t id = hash_id(src);
|
size_t id = hash_id(src);
|
||||||
if (sched->tensor_copies[id][cur_backend_id][0] == NULL) {
|
if (sched->tensor_copies[id][cur_backend_id][0] == NULL) {
|
||||||
ggml_backend_t backend = sched->backends[cur_backend_id];
|
ggml_backend_t backend = sched->backends[cur_backend_id];
|
||||||
for (int c = 0; c < sched->n_copies; c++) {
|
for (int c = 0; c < sched->n_copies; c++) {
|
||||||
@ -1520,42 +1463,76 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
|
|||||||
ggml_set_output(tensor_copy); // prevent ggml-alloc from overwriting the tensor
|
ggml_set_output(tensor_copy); // prevent ggml-alloc from overwriting the tensor
|
||||||
}
|
}
|
||||||
sched->tensor_copies[id][cur_backend_id][c] = tensor_copy;
|
sched->tensor_copies[id][cur_backend_id][c] = tensor_copy;
|
||||||
|
tensor_backend_id(tensor_copy) = cur_backend_id;
|
||||||
SET_CAUSE(tensor_copy, "4.cpy");
|
SET_CAUSE(tensor_copy, "4.cpy");
|
||||||
}
|
}
|
||||||
int n_inputs = split->n_inputs++;
|
int n_inputs = sched->splits[cur_split].n_inputs++;
|
||||||
GGML_ASSERT(n_inputs < GGML_SCHED_MAX_SPLIT_INPUTS);
|
GGML_ASSERT(n_inputs < GGML_SCHED_MAX_SPLIT_INPUTS);
|
||||||
split->inputs[n_inputs] = src;
|
sched->splits[cur_split].inputs[n_inputs] = src;
|
||||||
}
|
}
|
||||||
node->src[j] = sched->tensor_copies[id][cur_backend_id][sched->cur_copy];
|
node->src[j] = sched->tensor_copies[id][cur_backend_id][sched->cur_copy];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
split->i_end = graph->n_nodes;
|
sched->splits[cur_split].i_end = graph->n_nodes;
|
||||||
sched->n_splits = i_split + 1;
|
sched->n_splits = cur_split + 1;
|
||||||
}
|
}
|
||||||
#ifdef DEBUG_PASS4
|
#ifdef DEBUG_PASS4
|
||||||
fprintf(stderr, "PASS 4 ASSIGNMENTS\n"); ggml_backend_sched_print_assignments(sched, graph);
|
fprintf(stderr, "PASS 4 ASSIGNMENTS\n"); ggml_backend_sched_print_assignments(sched, graph);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef NDEBUG
|
||||||
|
// sanity check: all sources should have the same backend as the node
|
||||||
|
for (int i = 0; i < graph->n_nodes; i++) {
|
||||||
|
struct ggml_tensor * node = graph->nodes[i];
|
||||||
|
ggml_backend_t tensor_backend = ggml_backend_sched_get_tensor_backend(sched, node);
|
||||||
|
if (tensor_backend == NULL) {
|
||||||
|
fprintf(stderr, "!!!!!!! %s has no backend\n", node->name);
|
||||||
|
}
|
||||||
|
if (node->view_src != NULL && tensor_backend != ggml_backend_sched_get_tensor_backend(sched, node->view_src)) {
|
||||||
|
fprintf(stderr, "!!!!!!! %s has backend %s, view_src %s has backend %s\n",
|
||||||
|
node->name, tensor_backend ? ggml_backend_name(tensor_backend) : "NULL",
|
||||||
|
node->view_src->name, ggml_backend_sched_get_tensor_backend(sched, node->view_src) ?
|
||||||
|
ggml_backend_name(ggml_backend_sched_get_tensor_backend(sched, node->view_src)) : "NULL");
|
||||||
|
}
|
||||||
|
for (int j = 0; j < GGML_MAX_SRC; j++) {
|
||||||
|
struct ggml_tensor * src = node->src[j];
|
||||||
|
if (src == NULL) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
ggml_backend_t src_backend = ggml_backend_sched_get_tensor_backend(sched, src);
|
||||||
|
if (src_backend != tensor_backend /* && src_backend != NULL */) {
|
||||||
|
fprintf(stderr, "!!!! %s has backend %s, src %d (%s) has backend %s\n",
|
||||||
|
node->name, tensor_backend ? ggml_backend_name(tensor_backend) : "NULL",
|
||||||
|
j, src->name, src_backend ? ggml_backend_name(src_backend) : "NULL");
|
||||||
|
}
|
||||||
|
if (src->view_src != NULL && src_backend != ggml_backend_sched_get_tensor_backend(sched, src->view_src)) {
|
||||||
|
fprintf(stderr, "!!!!!!! [src] %s has backend %s, view_src %s has backend %s\n",
|
||||||
|
src->name, src_backend ? ggml_backend_name(src_backend) : "NULL",
|
||||||
|
src->view_src->name, ggml_backend_sched_get_tensor_backend(sched, src->view_src) ?
|
||||||
|
ggml_backend_name(ggml_backend_sched_get_tensor_backend(sched, src->view_src)) : "NULL");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fflush(stderr);
|
||||||
|
#endif
|
||||||
|
|
||||||
// create copies of the graph for each split
|
// create copies of the graph for each split
|
||||||
// TODO: avoid this copy
|
// TODO: avoid this copy
|
||||||
struct ggml_cgraph * graph_copy = ggml_new_graph_custom(sched->ctx, graph->n_nodes + sched->n_splits*GGML_SCHED_MAX_SPLIT_INPUTS*2, false);
|
struct ggml_cgraph * graph_copy = ggml_new_graph_custom(sched->ctx, graph->n_nodes + sched->n_splits*GGML_SCHED_MAX_SPLIT_INPUTS, false);
|
||||||
for (int i = 0; i < sched->n_splits; i++) {
|
for (int i = 0; i < sched->n_splits; i++) {
|
||||||
struct ggml_backend_sched_split * split = &sched->splits[i];
|
struct ggml_backend_sched_split * split = &sched->splits[i];
|
||||||
split->graph = ggml_graph_view(graph, split->i_start, split->i_end);
|
split->graph = ggml_graph_view(graph, split->i_start, split->i_end);
|
||||||
|
|
||||||
// add inputs to the graph copy so that they are allocated by ggml-alloc at the start of the split
|
// add inputs to the graph copy so that they are allocated by ggml-alloc at the start of the split
|
||||||
for (int j = 0; j < split->n_inputs; j++) {
|
for (int j = 0; j < split->n_inputs; j++) {
|
||||||
assert(graph_copy->size > (graph_copy->n_nodes + 1));
|
|
||||||
|
|
||||||
struct ggml_tensor * input = split->inputs[j];
|
struct ggml_tensor * input = split->inputs[j];
|
||||||
const size_t input_id = hash_id(input);
|
struct ggml_tensor * input_cpy = sched->tensor_copies[hash_id(input)][split->backend_id][sched->cur_copy];
|
||||||
struct ggml_tensor * input_cpy = sched->tensor_copies[input_id][split->backend_id][sched->cur_copy];
|
|
||||||
|
|
||||||
// add a dependency to the input source so that it is not freed before the copy is done
|
// add a dependency to the input source so that it is not freed before the copy is done
|
||||||
struct ggml_tensor * input_dep = ggml_view_tensor(sched->ctx, input);
|
struct ggml_tensor * input_dep = ggml_view_tensor(sched->ctx, input);
|
||||||
input_dep->src[0] = input;
|
input_dep->src[0] = input;
|
||||||
sched->node_backend_ids[graph_copy->n_nodes] = sched->tensor_backend_id[input_id];
|
sched->node_backend_ids[graph_copy->n_nodes] = tensor_backend_id(input);
|
||||||
graph_copy->nodes[graph_copy->n_nodes++] = input_dep;
|
graph_copy->nodes[graph_copy->n_nodes++] = input_dep;
|
||||||
|
|
||||||
// add a dependency to the input copy so that it is allocated at the start of the split
|
// add a dependency to the input copy so that it is allocated at the start of the split
|
||||||
@ -1564,7 +1541,6 @@ static void ggml_backend_sched_split_graph(ggml_backend_sched_t sched, struct gg
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (int j = split->i_start; j < split->i_end; j++) {
|
for (int j = split->i_start; j < split->i_end; j++) {
|
||||||
assert(graph_copy->size > graph_copy->n_nodes);
|
|
||||||
sched->node_backend_ids[graph_copy->n_nodes] = tensor_backend_id(graph->nodes[j]);
|
sched->node_backend_ids[graph_copy->n_nodes] = tensor_backend_id(graph->nodes[j]);
|
||||||
graph_copy->nodes[graph_copy->n_nodes++] = graph->nodes[j];
|
graph_copy->nodes[graph_copy->n_nodes++] = graph->nodes[j];
|
||||||
}
|
}
|
||||||
@ -1649,12 +1625,13 @@ static enum ggml_status ggml_backend_sched_compute_splits(ggml_backend_sched_t s
|
|||||||
}
|
}
|
||||||
ggml_backend_tensor_copy(input, input_cpy);
|
ggml_backend_tensor_copy(input, input_cpy);
|
||||||
} else {
|
} else {
|
||||||
// wait for the split backend to finish using the input before overwriting it
|
|
||||||
if (sched->events[split_backend_id][sched->cur_copy] != NULL) {
|
if (sched->events[split_backend_id][sched->cur_copy] != NULL) {
|
||||||
ggml_backend_event_wait(split_backend, sched->events[split_backend_id][sched->cur_copy]);
|
ggml_backend_event_wait(split_backend, sched->events[split_backend_id][sched->cur_copy]);
|
||||||
} else {
|
} else {
|
||||||
ggml_backend_synchronize(split_backend);
|
ggml_backend_synchronize(split_backend);
|
||||||
|
ggml_backend_synchronize(input_backend);
|
||||||
}
|
}
|
||||||
|
|
||||||
ggml_backend_tensor_copy_async(input_backend, split_backend, input, input_cpy);
|
ggml_backend_tensor_copy_async(input_backend, split_backend, input, input_cpy);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1724,21 +1701,17 @@ ggml_backend_sched_t ggml_backend_sched_new(
|
|||||||
struct ggml_backend_sched * sched = calloc(sizeof(struct ggml_backend_sched), 1);
|
struct ggml_backend_sched * sched = calloc(sizeof(struct ggml_backend_sched), 1);
|
||||||
|
|
||||||
// initialize hash table
|
// initialize hash table
|
||||||
sched->hash_set = ggml_hash_set_new(graph_size);
|
sched->hash_set = ggml_hash_set_new(graph_size + GGML_SCHED_MAX_SPLITS*GGML_SCHED_MAX_SPLIT_INPUTS);
|
||||||
sched->tensor_backend_id = calloc(sizeof(sched->tensor_backend_id[0]), sched->hash_set.size);
|
sched->tensor_backend_id = calloc(sizeof(sched->tensor_backend_id[0]), sched->hash_set.size);
|
||||||
sched->tensor_copies = calloc(sizeof(sched->tensor_copies[0]), sched->hash_set.size);
|
sched->tensor_copies = calloc(sizeof(sched->tensor_copies[0]), sched->hash_set.size);
|
||||||
|
sched->node_backend_ids = calloc(sizeof(sched->node_backend_ids[0]), graph_size);
|
||||||
const size_t nodes_size = graph_size + GGML_SCHED_MAX_SPLITS*GGML_SCHED_MAX_SPLIT_INPUTS*2;
|
sched->leaf_backend_ids = calloc(sizeof(sched->leaf_backend_ids[0]), graph_size);
|
||||||
sched->node_backend_ids = calloc(sizeof(sched->node_backend_ids[0]), nodes_size);
|
|
||||||
sched->leaf_backend_ids = calloc(sizeof(sched->leaf_backend_ids[0]), nodes_size);
|
|
||||||
|
|
||||||
sched->n_backends = n_backends;
|
sched->n_backends = n_backends;
|
||||||
|
|
||||||
sched->n_copies = parallel ? GGML_SCHED_MAX_COPIES : 1;
|
sched->n_copies = parallel ? GGML_SCHED_MAX_COPIES : 1;
|
||||||
|
|
||||||
const int initial_splits_capacity = 16;
|
GGML_ASSERT(sched->n_copies <= GGML_SCHED_MAX_COPIES);
|
||||||
sched->splits = calloc(sizeof(sched->splits[0]), initial_splits_capacity);
|
|
||||||
sched->splits_capacity = initial_splits_capacity;
|
|
||||||
|
|
||||||
for (int b = 0; b < n_backends; b++) {
|
for (int b = 0; b < n_backends; b++) {
|
||||||
sched->backends[b] = backends[b];
|
sched->backends[b] = backends[b];
|
||||||
@ -1769,7 +1742,6 @@ void ggml_backend_sched_free(ggml_backend_sched_t sched) {
|
|||||||
}
|
}
|
||||||
ggml_gallocr_free(sched->galloc);
|
ggml_gallocr_free(sched->galloc);
|
||||||
ggml_free(sched->ctx);
|
ggml_free(sched->ctx);
|
||||||
free(sched->splits);
|
|
||||||
free(sched->hash_set.keys);
|
free(sched->hash_set.keys);
|
||||||
free(sched->tensor_backend_id);
|
free(sched->tensor_backend_id);
|
||||||
free(sched->tensor_copies);
|
free(sched->tensor_copies);
|
||||||
@ -1790,8 +1762,6 @@ void ggml_backend_sched_reset(ggml_backend_sched_t sched) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool ggml_backend_sched_reserve(ggml_backend_sched_t sched, struct ggml_cgraph * measure_graph) {
|
bool ggml_backend_sched_reserve(ggml_backend_sched_t sched, struct ggml_cgraph * measure_graph) {
|
||||||
GGML_ASSERT((int)sched->hash_set.size >= measure_graph->n_nodes);
|
|
||||||
|
|
||||||
ggml_backend_sched_split_graph(sched, measure_graph);
|
ggml_backend_sched_split_graph(sched, measure_graph);
|
||||||
|
|
||||||
// TODO: extract this to a separate function
|
// TODO: extract this to a separate function
|
||||||
@ -1806,7 +1776,7 @@ bool ggml_backend_sched_reserve(ggml_backend_sched_t sched, struct ggml_cgraph *
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool ggml_backend_sched_alloc_graph(ggml_backend_sched_t sched, struct ggml_cgraph * graph) {
|
bool ggml_backend_sched_alloc_graph(ggml_backend_sched_t sched, struct ggml_cgraph * graph) {
|
||||||
GGML_ASSERT((int)sched->hash_set.size >= graph->n_nodes);
|
GGML_ASSERT((int)sched->hash_set.size >= graph->n_nodes + GGML_SCHED_MAX_SPLITS*GGML_SCHED_MAX_SPLIT_INPUTS);
|
||||||
|
|
||||||
ggml_backend_sched_split_graph(sched, graph);
|
ggml_backend_sched_split_graph(sched, graph);
|
||||||
|
|
||||||
|
@ -72,9 +72,9 @@ extern "C" {
|
|||||||
|
|
||||||
GGML_API enum ggml_status ggml_backend_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan);
|
GGML_API enum ggml_status ggml_backend_graph_plan_compute(ggml_backend_t backend, ggml_backend_graph_plan_t plan);
|
||||||
GGML_API enum ggml_status ggml_backend_graph_compute (ggml_backend_t backend, struct ggml_cgraph * cgraph);
|
GGML_API enum ggml_status ggml_backend_graph_compute (ggml_backend_t backend, struct ggml_cgraph * cgraph);
|
||||||
GGML_API enum ggml_status ggml_backend_graph_compute_async(ggml_backend_t backend, struct ggml_cgraph * cgraph);
|
|
||||||
|
GGML_API bool ggml_backend_graph_compute_async(ggml_backend_t backend, struct ggml_cgraph * cgraph);
|
||||||
GGML_API bool ggml_backend_supports_op(ggml_backend_t backend, const struct ggml_tensor * op);
|
GGML_API bool ggml_backend_supports_op(ggml_backend_t backend, const struct ggml_tensor * op);
|
||||||
GGML_API bool ggml_backend_offload_op(ggml_backend_t backend, const struct ggml_tensor * op);
|
|
||||||
|
|
||||||
// tensor copy between different backends
|
// tensor copy between different backends
|
||||||
GGML_API void ggml_backend_tensor_copy(struct ggml_tensor * src, struct ggml_tensor * dst);
|
GGML_API void ggml_backend_tensor_copy(struct ggml_tensor * src, struct ggml_tensor * dst);
|
||||||
@ -137,7 +137,7 @@ extern "C" {
|
|||||||
/*
|
/*
|
||||||
Example usage:
|
Example usage:
|
||||||
|
|
||||||
// operations that use tensors allocated in a buffer with USAGE_WEIGHTS will be assigned
|
// operations that use tensors allocated in a buffer with USAGE_WEIGHTS will be asigned
|
||||||
// preferrably to run on the same backend as the buffer
|
// preferrably to run on the same backend as the buffer
|
||||||
ggml_backend_buffer_set_usage(buf_weights, GGML_BACKEND_BUFFER_USAGE_WEIGHTS);
|
ggml_backend_buffer_set_usage(buf_weights, GGML_BACKEND_BUFFER_USAGE_WEIGHTS);
|
||||||
|
|
||||||
|
@ -377,27 +377,6 @@ typedef struct {
|
|||||||
} block_iq1_s;
|
} block_iq1_s;
|
||||||
static_assert(sizeof(block_iq1_s) == sizeof(ggml_half) + QK_K/8 + QK_K/16, "wrong iq1_s block size/padding");
|
static_assert(sizeof(block_iq1_s) == sizeof(ggml_half) + QK_K/8 + QK_K/16, "wrong iq1_s block size/padding");
|
||||||
|
|
||||||
// 1.75 bpw
|
|
||||||
typedef struct {
|
|
||||||
uint8_t qs[QK_K/8]; // grid index, low 8 bits
|
|
||||||
uint8_t qh[QK_K/16]; // grid index, high 3 bits + grid shift bit (for two groups of 8)
|
|
||||||
#if QK_K == 64
|
|
||||||
ggml_half d;
|
|
||||||
#endif
|
|
||||||
uint8_t scales[QK_K/32]; // 3-bit block scales (4-bit if QK_K == 64)
|
|
||||||
} block_iq1_m;
|
|
||||||
#if QK_K == 64
|
|
||||||
static_assert(sizeof(block_iq1_m) == QK_K/8 + QK_K/16 + QK_K/32 + sizeof(ggml_half), "wrong iq1_m block size/padding");
|
|
||||||
#else
|
|
||||||
static_assert(sizeof(block_iq1_m) == QK_K/8 + QK_K/16 + QK_K/32, "wrong iq1_m block size/padding");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Used by IQ1_M quants
|
|
||||||
typedef union {
|
|
||||||
ggml_half f16;
|
|
||||||
uint16_t u16;
|
|
||||||
} iq1m_scale_t;
|
|
||||||
|
|
||||||
// Non-linear quants
|
// Non-linear quants
|
||||||
#define QK4_NL 32
|
#define QK4_NL 32
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -447,11 +426,10 @@ static_assert(sizeof(block_iq4_xs) == sizeof(ggml_half) + sizeof(uint16_t) + QK_
|
|||||||
|
|
||||||
#define GGML_COMMON_IMPL
|
#define GGML_COMMON_IMPL
|
||||||
#elif defined(GGML_COMMON_IMPL_SYCL)
|
#elif defined(GGML_COMMON_IMPL_SYCL)
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
#define GGML_TABLE_BEGIN(type, name, size) static const type name[size] = {
|
#define GGML_TABLE_BEGIN(type, name, size) static dpct::global_memory<const type, 1> name(sycl::range<1>(size), {
|
||||||
#define GGML_TABLE_END() };
|
#define GGML_TABLE_END() });
|
||||||
|
|
||||||
#define GGML_COMMON_IMPL
|
#define GGML_COMMON_IMPL
|
||||||
#endif
|
#endif
|
||||||
@ -1072,7 +1050,6 @@ GGML_TABLE_END()
|
|||||||
|
|
||||||
#define NGRID_IQ1S 2048
|
#define NGRID_IQ1S 2048
|
||||||
#define IQ1S_DELTA 0.125f
|
#define IQ1S_DELTA 0.125f
|
||||||
#define IQ1M_DELTA 0.125f
|
|
||||||
#if defined(GGML_COMMON_IMPL_C)
|
#if defined(GGML_COMMON_IMPL_C)
|
||||||
GGML_TABLE_BEGIN(uint64_t, iq1s_grid, NGRID_IQ1S)
|
GGML_TABLE_BEGIN(uint64_t, iq1s_grid, NGRID_IQ1S)
|
||||||
0xffffffffffffffff, 0xffffffffffffff01, 0xffffffffffff0000, 0xffffffffffff01ff,
|
0xffffffffffffffff, 0xffffffffffffff01, 0xffffffffffff0000, 0xffffffffffff01ff,
|
||||||
|
11601
ggml-cuda.cu
11601
ggml-cuda.cu
File diff suppressed because it is too large
Load Diff
21
ggml-cuda.h
21
ggml-cuda.h
@ -17,17 +17,29 @@ extern "C" {
|
|||||||
|
|
||||||
#define GGML_CUDA_MAX_DEVICES 16
|
#define GGML_CUDA_MAX_DEVICES 16
|
||||||
|
|
||||||
|
// Always success. To check if CUDA is actually loaded, use `ggml_cublas_loaded`.
|
||||||
|
GGML_API GGML_CALL void ggml_init_cublas(void);
|
||||||
|
|
||||||
|
// Returns `true` if there are available CUDA devices and cublas loads successfully; otherwise, it returns `false`.
|
||||||
|
GGML_API GGML_CALL bool ggml_cublas_loaded(void);
|
||||||
|
|
||||||
|
GGML_API GGML_CALL void * ggml_cuda_host_malloc(size_t size);
|
||||||
|
GGML_API GGML_CALL void ggml_cuda_host_free(void * ptr);
|
||||||
|
|
||||||
|
GGML_API GGML_CALL bool ggml_cuda_can_mul_mat(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst);
|
||||||
|
GGML_API GGML_CALL bool ggml_cuda_compute_forward(struct ggml_compute_params * params, struct ggml_tensor * tensor);
|
||||||
|
|
||||||
|
GGML_API GGML_CALL int ggml_cuda_get_device_count(void);
|
||||||
|
GGML_API GGML_CALL void ggml_cuda_get_device_description(int device, char * description, size_t description_size);
|
||||||
|
|
||||||
// backend API
|
// backend API
|
||||||
GGML_API GGML_CALL ggml_backend_t ggml_backend_cuda_init(int device);
|
GGML_API GGML_CALL ggml_backend_t ggml_backend_cuda_init(int device);
|
||||||
|
|
||||||
GGML_API GGML_CALL bool ggml_backend_is_cuda(ggml_backend_t backend);
|
GGML_API GGML_CALL bool ggml_backend_is_cuda(ggml_backend_t backend);
|
||||||
|
|
||||||
// device buffer
|
|
||||||
GGML_API GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device);
|
GGML_API GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device);
|
||||||
|
|
||||||
// split tensor buffer that splits matrices by rows across multiple devices
|
// split tensor buffer that splits matrices by rows across multiple devices
|
||||||
GGML_API GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_split_buffer_type(const float * tensor_split);
|
GGML_API GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_split_buffer_type(const float * tensor_split);
|
||||||
|
|
||||||
// pinned host buffer for use with the CPU backend for faster copies between CPU and GPU
|
// pinned host buffer for use with the CPU backend for faster copies between CPU and GPU
|
||||||
GGML_API GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type(void);
|
GGML_API GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type(void);
|
||||||
|
|
||||||
@ -35,9 +47,6 @@ GGML_API GGML_CALL int ggml_backend_cuda_get_device_count(void);
|
|||||||
GGML_API GGML_CALL void ggml_backend_cuda_get_device_description(int device, char * description, size_t description_size);
|
GGML_API GGML_CALL void ggml_backend_cuda_get_device_description(int device, char * description, size_t description_size);
|
||||||
GGML_API GGML_CALL void ggml_backend_cuda_get_device_memory(int device, size_t * free, size_t * total);
|
GGML_API GGML_CALL void ggml_backend_cuda_get_device_memory(int device, size_t * free, size_t * total);
|
||||||
|
|
||||||
GGML_API GGML_CALL bool ggml_backend_cuda_register_host_buffer(void * buffer, size_t size);
|
|
||||||
GGML_API GGML_CALL void ggml_backend_cuda_unregister_host_buffer(void * buffer);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,47 +0,0 @@
|
|||||||
#include "acc.cuh"
|
|
||||||
|
|
||||||
static __global__ void acc_f32(const float * x, const float * y, float * dst, const int ne,
|
|
||||||
const int ne10, const int ne11, const int ne12,
|
|
||||||
const int nb1, const int nb2, int offset) {
|
|
||||||
const int i = blockDim.x * blockIdx.x + threadIdx.x;
|
|
||||||
if (i >= ne) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int src1_idx = i - offset;
|
|
||||||
int oz = src1_idx / nb2;
|
|
||||||
int oy = (src1_idx - (oz * nb2)) / nb1;
|
|
||||||
int ox = src1_idx % nb1;
|
|
||||||
if (src1_idx >= 0 && ox < ne10 && oy < ne11 && oz < ne12) {
|
|
||||||
dst[i] = x[i] + y[ox + oy * ne10 + oz * ne10 * ne11];
|
|
||||||
} else {
|
|
||||||
dst[i] = x[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void acc_f32_cuda(const float * x, const float * y, float * dst, const int n_elements,
|
|
||||||
const int ne10, const int ne11, const int ne12,
|
|
||||||
const int nb1, const int nb2, const int offset, cudaStream_t stream) {
|
|
||||||
int num_blocks = (n_elements + CUDA_ACC_BLOCK_SIZE - 1) / CUDA_ACC_BLOCK_SIZE;
|
|
||||||
acc_f32<<<num_blocks, CUDA_ACC_BLOCK_SIZE, 0, stream>>>(x, y, dst, n_elements, ne10, ne11, ne12, nb1, nb2, offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_acc(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
const ggml_tensor * src1 = dst->src[1];
|
|
||||||
const float * src0_d = (const float *)src0->data;
|
|
||||||
const float * src1_d = (const float *)src1->data;
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
GGML_ASSERT(src0->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT(src1->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT( dst->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT(dst->ne[3] == 1); // just 3D tensors supported
|
|
||||||
|
|
||||||
int nb1 = dst->op_params[0] / 4; // 4 bytes of float32
|
|
||||||
int nb2 = dst->op_params[1] / 4; // 4 bytes of float32
|
|
||||||
// int nb3 = dst->op_params[2] / 4; // 4 bytes of float32 - unused
|
|
||||||
int offset = dst->op_params[3] / 4; // offset in bytes
|
|
||||||
|
|
||||||
acc_f32_cuda(src0_d, src1_d, dst_d, ggml_nelements(dst), src1->ne[0], src1->ne[1], src1->ne[2], nb1, nb2, offset, stream);
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
#define CUDA_ACC_BLOCK_SIZE 256
|
|
||||||
|
|
||||||
void ggml_cuda_op_acc(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
@ -1,63 +0,0 @@
|
|||||||
#include "alibi.cuh"
|
|
||||||
|
|
||||||
static __global__ void alibi_f32(const float * x, float * dst, const int ncols, const int k_rows,
|
|
||||||
const int n_heads_log2_floor, const float m0, const float m1) {
|
|
||||||
const int col = blockDim.x*blockIdx.x + threadIdx.x;
|
|
||||||
|
|
||||||
if (col >= ncols) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int row = blockDim.y*blockIdx.y + threadIdx.y;
|
|
||||||
const int i = row*ncols + col;
|
|
||||||
|
|
||||||
const int k = row/k_rows;
|
|
||||||
|
|
||||||
float m_k;
|
|
||||||
if (k < n_heads_log2_floor) {
|
|
||||||
m_k = powf(m0, k + 1);
|
|
||||||
} else {
|
|
||||||
m_k = powf(m1, 2 * (k - n_heads_log2_floor) + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
dst[i] = col * m_k + x[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
static void alibi_f32_cuda(const float * x, float * dst, const int ncols, const int nrows,
|
|
||||||
const int k_rows, const int n_heads_log2_floor, const float m0,
|
|
||||||
const float m1, cudaStream_t stream) {
|
|
||||||
const dim3 block_dims(CUDA_ALIBI_BLOCK_SIZE, 1, 1);
|
|
||||||
const int num_blocks_x = (ncols + CUDA_ALIBI_BLOCK_SIZE - 1) / (CUDA_ALIBI_BLOCK_SIZE);
|
|
||||||
const dim3 block_nums(num_blocks_x, nrows, 1);
|
|
||||||
alibi_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, k_rows, n_heads_log2_floor, m0, m1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_alibi(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
const float * src0_d = (const float *)src0->data;
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
GGML_ASSERT(src0->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT( dst->type == GGML_TYPE_F32);
|
|
||||||
|
|
||||||
const int64_t ne00 = src0->ne[0];
|
|
||||||
const int64_t ne01 = src0->ne[1];
|
|
||||||
const int64_t ne02 = src0->ne[2];
|
|
||||||
const int64_t nrows = ggml_nrows(src0);
|
|
||||||
|
|
||||||
//const int n_past = ((int32_t *) dst->op_params)[0];
|
|
||||||
const int n_head = ((int32_t *) dst->op_params)[1];
|
|
||||||
float max_bias;
|
|
||||||
memcpy(&max_bias, (int32_t *) dst->op_params + 2, sizeof(float));
|
|
||||||
|
|
||||||
//GGML_ASSERT(ne01 + n_past == ne00);
|
|
||||||
GGML_ASSERT(n_head == ne02);
|
|
||||||
|
|
||||||
const int n_heads_log2_floor = 1 << (int) floor(log2(n_head));
|
|
||||||
|
|
||||||
const float m0 = powf(2.0f, -(max_bias) / n_heads_log2_floor);
|
|
||||||
const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_heads_log2_floor);
|
|
||||||
|
|
||||||
alibi_f32_cuda(src0_d, dst_d, ne00, nrows, ne01, n_heads_log2_floor, m0, m1, stream);
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
#define CUDA_ALIBI_BLOCK_SIZE 32
|
|
||||||
|
|
||||||
void ggml_cuda_op_alibi(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
@ -1,34 +0,0 @@
|
|||||||
#include "arange.cuh"
|
|
||||||
|
|
||||||
static __global__ void arange_f32(float * dst, const int ne0, const float start, const float step) {
|
|
||||||
// blockIDx.x: idx of ne0 / BLOCK_SIZE
|
|
||||||
int nidx = threadIdx.x + blockIdx.x * blockDim.x;
|
|
||||||
if (nidx >= ne0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
dst[nidx] = start + step * nidx;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void arange_f32_cuda(float * dst, const int ne0, const float start, const float step, cudaStream_t stream) {
|
|
||||||
int num_blocks = (ne0 + CUDA_ARANGE_BLOCK_SIZE - 1) / CUDA_ARANGE_BLOCK_SIZE;
|
|
||||||
arange_f32<<<num_blocks, CUDA_ARANGE_BLOCK_SIZE, 0, stream>>>(dst, ne0, start, step);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_arange(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
GGML_ASSERT(dst->type == GGML_TYPE_F32);
|
|
||||||
|
|
||||||
float start;
|
|
||||||
float stop;
|
|
||||||
float step;
|
|
||||||
memcpy(&start, (float *)dst->op_params + 0, sizeof(float));
|
|
||||||
memcpy(&stop, (float *)dst->op_params + 1, sizeof(float));
|
|
||||||
memcpy(&step, (float *)dst->op_params + 2, sizeof(float));
|
|
||||||
|
|
||||||
int64_t steps = (int64_t)ceil((stop - start) / step);
|
|
||||||
GGML_ASSERT(ggml_nelements(dst) == steps);
|
|
||||||
|
|
||||||
arange_f32_cuda(dst_d, dst->ne[0], start, step, stream);
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
#define CUDA_ARANGE_BLOCK_SIZE 256
|
|
||||||
|
|
||||||
void ggml_cuda_op_arange(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
@ -1,103 +0,0 @@
|
|||||||
#include "argsort.cuh"
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
static inline __device__ void ggml_cuda_swap(T & a, T & b) {
|
|
||||||
T tmp = a;
|
|
||||||
a = b;
|
|
||||||
b = tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<ggml_sort_order order>
|
|
||||||
static __global__ void k_argsort_f32_i32(const float * x, int * dst, const int ncols, int ncols_pad) {
|
|
||||||
// bitonic sort
|
|
||||||
int col = threadIdx.x;
|
|
||||||
int row = blockIdx.y;
|
|
||||||
|
|
||||||
if (col >= ncols_pad) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const float * x_row = x + row * ncols;
|
|
||||||
extern __shared__ int dst_row[];
|
|
||||||
|
|
||||||
// initialize indices
|
|
||||||
dst_row[col] = col;
|
|
||||||
|
|
||||||
__syncthreads();
|
|
||||||
|
|
||||||
for (int k = 2; k <= ncols_pad; k *= 2) {
|
|
||||||
for (int j = k / 2; j > 0; j /= 2) {
|
|
||||||
int ixj = col ^ j;
|
|
||||||
if (ixj > col) {
|
|
||||||
if ((col & k) == 0) {
|
|
||||||
if (dst_row[col] >= ncols ||
|
|
||||||
(dst_row[ixj] < ncols && (order == GGML_SORT_ORDER_ASC ?
|
|
||||||
x_row[dst_row[col]] > x_row[dst_row[ixj]] :
|
|
||||||
x_row[dst_row[col]] < x_row[dst_row[ixj]]))
|
|
||||||
) {
|
|
||||||
ggml_cuda_swap(dst_row[col], dst_row[ixj]);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (dst_row[ixj] >= ncols ||
|
|
||||||
(dst_row[col] < ncols && (order == GGML_SORT_ORDER_ASC ?
|
|
||||||
x_row[dst_row[col]] < x_row[dst_row[ixj]] :
|
|
||||||
x_row[dst_row[col]] > x_row[dst_row[ixj]]))
|
|
||||||
) {
|
|
||||||
ggml_cuda_swap(dst_row[col], dst_row[ixj]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
__syncthreads();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// copy the result to dst without the padding
|
|
||||||
if (col < ncols) {
|
|
||||||
dst[row * ncols + col] = dst_row[col];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int next_power_of_2(int x) {
|
|
||||||
int n = 1;
|
|
||||||
while (n < x) {
|
|
||||||
n *= 2;
|
|
||||||
}
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void argsort_f32_i32_cuda(const float * x, int * dst, const int ncols, const int nrows, ggml_sort_order order, cudaStream_t stream) {
|
|
||||||
// bitonic sort requires ncols to be power of 2
|
|
||||||
const int ncols_pad = next_power_of_2(ncols);
|
|
||||||
|
|
||||||
const dim3 block_dims(ncols_pad, 1, 1);
|
|
||||||
const dim3 block_nums(1, nrows, 1);
|
|
||||||
const size_t shared_mem = ncols_pad * sizeof(int);
|
|
||||||
|
|
||||||
GGML_ASSERT(shared_mem <= ggml_cuda_info().devices[ggml_cuda_get_device()].smpb);
|
|
||||||
|
|
||||||
if (order == GGML_SORT_ORDER_ASC) {
|
|
||||||
k_argsort_f32_i32<GGML_SORT_ORDER_ASC><<<block_nums, block_dims, shared_mem, stream>>>(x, dst, ncols, ncols_pad);
|
|
||||||
} else if (order == GGML_SORT_ORDER_DESC) {
|
|
||||||
k_argsort_f32_i32<GGML_SORT_ORDER_DESC><<<block_nums, block_dims, shared_mem, stream>>>(x, dst, ncols, ncols_pad);
|
|
||||||
} else {
|
|
||||||
GGML_ASSERT(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_argsort(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
const float * src0_d = (const float *)src0->data;
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
GGML_ASSERT(src0->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT( dst->type == GGML_TYPE_I32);
|
|
||||||
GGML_ASSERT(ggml_is_contiguous(src0));
|
|
||||||
|
|
||||||
const int64_t ncols = src0->ne[0];
|
|
||||||
const int64_t nrows = ggml_nrows(src0);
|
|
||||||
|
|
||||||
enum ggml_sort_order order = (enum ggml_sort_order) dst->op_params[0];
|
|
||||||
|
|
||||||
argsort_f32_i32_cuda(src0_d, (int *)dst_d, ncols, nrows, order, stream);
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
void ggml_cuda_op_argsort(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
@ -1,236 +0,0 @@
|
|||||||
#include "binbcast.cuh"
|
|
||||||
|
|
||||||
static __device__ __forceinline__ float op_repeat(const float a, const float b) {
|
|
||||||
return b;
|
|
||||||
GGML_UNUSED(a);
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ __forceinline__ float op_add(const float a, const float b) {
|
|
||||||
return a + b;
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ __forceinline__ float op_mul(const float a, const float b) {
|
|
||||||
return a * b;
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ __forceinline__ float op_div(const float a, const float b) {
|
|
||||||
return a / b;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
|
|
||||||
static __global__ void k_bin_bcast(const src0_t * src0, const src1_t * src1, dst_t * dst,
|
|
||||||
int ne0, int ne1, int ne2, int ne3,
|
|
||||||
int ne10, int ne11, int ne12, int ne13,
|
|
||||||
/*int s0, */ int s1, int s2, int s3,
|
|
||||||
/*int s10,*/ int s11, int s12, int s13) {
|
|
||||||
const int i0s = blockDim.x*blockIdx.x + threadIdx.x;
|
|
||||||
const int i1 = (blockDim.y*blockIdx.y + threadIdx.y);
|
|
||||||
const int i2 = (blockDim.z*blockIdx.z + threadIdx.z) / ne3;
|
|
||||||
const int i3 = (blockDim.z*blockIdx.z + threadIdx.z) % ne3;
|
|
||||||
|
|
||||||
if (i0s >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int i11 = i1 % ne11;
|
|
||||||
const int i12 = i2 % ne12;
|
|
||||||
const int i13 = i3 % ne13;
|
|
||||||
|
|
||||||
const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
|
|
||||||
const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
|
|
||||||
const size_t i_dst = i_src0;
|
|
||||||
|
|
||||||
const src0_t * src0_row = src0 + i_src0;
|
|
||||||
const src1_t * src1_row = src1 + i_src1;
|
|
||||||
dst_t * dst_row = dst + i_dst;
|
|
||||||
|
|
||||||
for (int i0 = i0s; i0 < ne0; i0 += blockDim.x*gridDim.x) {
|
|
||||||
const int i10 = i0 % ne10;
|
|
||||||
dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<float (*bin_op)(const float, const float), typename src0_t, typename src1_t, typename dst_t>
|
|
||||||
static __global__ void k_bin_bcast_unravel(const src0_t * src0, const src1_t * src1, dst_t * dst,
|
|
||||||
int ne0, int ne1, int ne2, int ne3,
|
|
||||||
int ne10, int ne11, int ne12, int ne13,
|
|
||||||
/*int s0, */ int s1, int s2, int s3,
|
|
||||||
/*int s10,*/ int s11, int s12, int s13) {
|
|
||||||
|
|
||||||
const int i = blockDim.x*blockIdx.x + threadIdx.x;
|
|
||||||
|
|
||||||
const int i3 = i/(ne2*ne1*ne0);
|
|
||||||
const int i2 = (i/(ne1*ne0)) % ne2;
|
|
||||||
const int i1 = (i/ne0) % ne1;
|
|
||||||
const int i0 = i % ne0;
|
|
||||||
|
|
||||||
if (i0 >= ne0 || i1 >= ne1 || i2 >= ne2 || i3 >= ne3) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int i11 = i1 % ne11;
|
|
||||||
const int i12 = i2 % ne12;
|
|
||||||
const int i13 = i3 % ne13;
|
|
||||||
|
|
||||||
const size_t i_src0 = i3*s3 + i2*s2 + i1*s1;
|
|
||||||
const size_t i_src1 = i13*s13 + i12*s12 + i11*s11;
|
|
||||||
const size_t i_dst = i_src0;
|
|
||||||
|
|
||||||
const src0_t * src0_row = src0 + i_src0;
|
|
||||||
const src1_t * src1_row = src1 + i_src1;
|
|
||||||
dst_t * dst_row = dst + i_dst;
|
|
||||||
|
|
||||||
const int i10 = i0 % ne10;
|
|
||||||
dst_row[i0] = (dst_t)bin_op(src0 ? (float)src0_row[i0] : 0.0f, (float)src1_row[i10]);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<float (*bin_op)(const float, const float)>
|
|
||||||
struct bin_bcast_cuda {
|
|
||||||
template<typename src0_t, typename src1_t, typename dst_t>
|
|
||||||
void operator()(const struct ggml_tensor * src0, const struct ggml_tensor * src1, struct ggml_tensor * dst,
|
|
||||||
const src0_t * src0_dd, const src1_t * src1_dd, dst_t * dst_dd,
|
|
||||||
cudaStream_t stream) {
|
|
||||||
|
|
||||||
GGML_TENSOR_BINARY_OP_LOCALS
|
|
||||||
|
|
||||||
int nr0 = ne10/ne0;
|
|
||||||
int nr1 = ne11/ne1;
|
|
||||||
int nr2 = ne12/ne2;
|
|
||||||
int nr3 = ne13/ne3;
|
|
||||||
|
|
||||||
int nr[4] = { nr0, nr1, nr2, nr3 };
|
|
||||||
|
|
||||||
// collapse dimensions until first broadcast dimension
|
|
||||||
int64_t cne0[] = {ne0, ne1, ne2, ne3};
|
|
||||||
int64_t cne1[] = {ne10, ne11, ne12, ne13};
|
|
||||||
size_t cnb0[] = {nb0, nb1, nb2, nb3};
|
|
||||||
size_t cnb1[] = {nb10, nb11, nb12, nb13};
|
|
||||||
auto collapse = [](int64_t cne[]) {
|
|
||||||
cne[0] *= cne[1];
|
|
||||||
cne[1] = cne[2];
|
|
||||||
cne[2] = cne[3];
|
|
||||||
cne[3] = 1;
|
|
||||||
};
|
|
||||||
|
|
||||||
auto collapse_nb = [](size_t cnb[], const int64_t cne[]) {
|
|
||||||
cnb[1] *= cne[1];
|
|
||||||
cnb[2] *= cne[2];
|
|
||||||
cnb[3] *= cne[3];
|
|
||||||
};
|
|
||||||
|
|
||||||
for (int i = 0; i < 4; i++) {
|
|
||||||
if (nr[i] != 1) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (i > 0) {
|
|
||||||
collapse_nb(cnb0, cne0);
|
|
||||||
collapse_nb(cnb1, cne1);
|
|
||||||
collapse(cne0);
|
|
||||||
collapse(cne1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
{
|
|
||||||
int64_t ne0 = cne0[0];
|
|
||||||
int64_t ne1 = cne0[1];
|
|
||||||
int64_t ne2 = cne0[2];
|
|
||||||
int64_t ne3 = cne0[3];
|
|
||||||
|
|
||||||
int64_t ne10 = cne1[0];
|
|
||||||
int64_t ne11 = cne1[1];
|
|
||||||
int64_t ne12 = cne1[2];
|
|
||||||
int64_t ne13 = cne1[3];
|
|
||||||
|
|
||||||
size_t nb0 = cnb0[0];
|
|
||||||
size_t nb1 = cnb0[1];
|
|
||||||
size_t nb2 = cnb0[2];
|
|
||||||
size_t nb3 = cnb0[3];
|
|
||||||
|
|
||||||
size_t nb10 = cnb1[0];
|
|
||||||
size_t nb11 = cnb1[1];
|
|
||||||
size_t nb12 = cnb1[2];
|
|
||||||
size_t nb13 = cnb1[3];
|
|
||||||
|
|
||||||
size_t s0 = nb0 / sizeof(dst_t);
|
|
||||||
size_t s1 = nb1 / sizeof(dst_t);
|
|
||||||
size_t s2 = nb2 / sizeof(dst_t);
|
|
||||||
size_t s3 = nb3 / sizeof(dst_t);
|
|
||||||
|
|
||||||
size_t s10 = nb10 / sizeof(src1_t);
|
|
||||||
size_t s11 = nb11 / sizeof(src1_t);
|
|
||||||
size_t s12 = nb12 / sizeof(src1_t);
|
|
||||||
size_t s13 = nb13 / sizeof(src1_t);
|
|
||||||
|
|
||||||
GGML_ASSERT(s0 == 1);
|
|
||||||
GGML_ASSERT(s10 == 1);
|
|
||||||
|
|
||||||
const int block_size = 128;
|
|
||||||
|
|
||||||
int64_t hne0 = std::max(ne0/2LL, 1LL);
|
|
||||||
|
|
||||||
dim3 block_dims;
|
|
||||||
block_dims.x = std::min<unsigned int>(hne0, block_size);
|
|
||||||
block_dims.y = std::min<unsigned int>(ne1, block_size / block_dims.x);
|
|
||||||
block_dims.z = std::min(std::min<unsigned int>(ne2*ne3, block_size / block_dims.x / block_dims.y), 64U);
|
|
||||||
|
|
||||||
dim3 block_nums(
|
|
||||||
(hne0 + block_dims.x - 1) / block_dims.x,
|
|
||||||
(ne1 + block_dims.y - 1) / block_dims.y,
|
|
||||||
(ne2*ne3 + block_dims.z - 1) / block_dims.z
|
|
||||||
);
|
|
||||||
|
|
||||||
if (block_nums.z > 65535) {
|
|
||||||
// this is the maximum number of blocks in z direction, fallback to 1D grid kernel
|
|
||||||
int block_num = (ne0*ne1*ne2*ne3 + block_size - 1) / block_size;
|
|
||||||
k_bin_bcast_unravel<bin_op><<<block_num, block_size, 0, stream>>>(
|
|
||||||
src0_dd, src1_dd, dst_dd,
|
|
||||||
ne0, ne1, ne2, ne3,
|
|
||||||
ne10, ne11, ne12, ne13,
|
|
||||||
/* s0, */ s1, s2, s3,
|
|
||||||
/* s10, */ s11, s12, s13);
|
|
||||||
} else {
|
|
||||||
k_bin_bcast<bin_op><<<block_nums, block_dims, 0, stream>>>(
|
|
||||||
src0_dd, src1_dd, dst_dd,
|
|
||||||
ne0, ne1, ne2, ne3,
|
|
||||||
ne10, ne11, ne12, ne13,
|
|
||||||
/* s0, */ s1, s2, s3,
|
|
||||||
/* s10, */ s11, s12, s13);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<class op>
|
|
||||||
static void ggml_cuda_op_bin_bcast(
|
|
||||||
const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
|
|
||||||
const void * src0_dd, const void * src1_dd, void * dst_dd, cudaStream_t stream) {
|
|
||||||
|
|
||||||
GGML_ASSERT(src1->type == GGML_TYPE_F32);
|
|
||||||
|
|
||||||
if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
|
|
||||||
op()(src0, src1, dst, (const float *)src0_dd, (const float *)src1_dd, (float *)dst_dd, stream);
|
|
||||||
} else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F16) {
|
|
||||||
op()(src0, src1, dst, (const half *) src0_dd, (const float *)src1_dd, (half *) dst_dd, stream);
|
|
||||||
} else if (src0->type == GGML_TYPE_F16 && dst->type == GGML_TYPE_F32) {
|
|
||||||
op()(src0, src1, dst, (const half *) src0_dd, (const float *)src1_dd, (float *)dst_dd, stream);
|
|
||||||
} else {
|
|
||||||
fprintf(stderr, "%s: unsupported types: dst: %s, src0: %s, src1: %s\n", __func__,
|
|
||||||
ggml_type_name(dst->type), ggml_type_name(src0->type), ggml_type_name(src1->type));
|
|
||||||
GGML_ASSERT(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_repeat(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_repeat>>(dst, dst->src[0], dst, nullptr, dst->src[0]->data, dst->data, ctx.stream());
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_add(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_add>>(dst->src[0], dst->src[1], dst, dst->src[0]->data, dst->src[1]->data, dst->data, ctx.stream());
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_mul(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_mul>>(dst->src[0], dst->src[1], dst, dst->src[0]->data, dst->src[1]->data, dst->data, ctx.stream());
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_div(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
ggml_cuda_op_bin_bcast<bin_bcast_cuda<op_div>>(dst->src[0], dst->src[1], dst, dst->src[0]->data, dst->src[1]->data, dst->data, ctx.stream());
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
void ggml_cuda_op_repeat(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
||||||
void ggml_cuda_op_add(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
||||||
void ggml_cuda_op_mul(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
||||||
void ggml_cuda_op_div(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
@ -1,35 +0,0 @@
|
|||||||
#include "clamp.cuh"
|
|
||||||
|
|
||||||
static __global__ void clamp_f32(const float * x, float * dst, const float min, const float max, const int k) {
|
|
||||||
const int i = blockDim.x*blockIdx.x + threadIdx.x;
|
|
||||||
|
|
||||||
if (i >= k) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
dst[i] = x[i] < min ? min : (x[i] > max ? max : x[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void clamp_f32_cuda(const float * x, float * dst, const float min, const float max, const int k, cudaStream_t stream) {
|
|
||||||
const int num_blocks = (k + CUDA_CLAMP_BLOCK_SIZE - 1) / CUDA_CLAMP_BLOCK_SIZE;
|
|
||||||
clamp_f32<<<num_blocks, CUDA_CLAMP_BLOCK_SIZE, 0, stream>>>(x, dst, min, max, k);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void ggml_cuda_op_clamp(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
const float * src0_d = (const float *)src0->data;
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
GGML_ASSERT(src0->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT( dst->type == GGML_TYPE_F32);
|
|
||||||
|
|
||||||
float min;
|
|
||||||
float max;
|
|
||||||
memcpy(&min, dst->op_params, sizeof(float));
|
|
||||||
memcpy(&max, (float *) dst->op_params + 1, sizeof(float));
|
|
||||||
|
|
||||||
clamp_f32_cuda(src0_d, dst_d, min, max, ggml_nelements(src0), stream);
|
|
||||||
CUDA_CHECK(cudaGetLastError());
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
#define CUDA_CLAMP_BLOCK_SIZE 256
|
|
||||||
|
|
||||||
void ggml_cuda_op_clamp(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
@ -1,551 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include "ggml.h"
|
|
||||||
#include "ggml-cuda.h"
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#if defined(GGML_USE_HIPBLAS)
|
|
||||||
#define GGML_COMMON_DECL_HIP
|
|
||||||
#define GGML_COMMON_IMPL_HIP
|
|
||||||
#else
|
|
||||||
#define GGML_COMMON_DECL_CUDA
|
|
||||||
#define GGML_COMMON_IMPL_CUDA
|
|
||||||
#endif
|
|
||||||
#include "ggml-common.h"
|
|
||||||
|
|
||||||
#include <cstdio>
|
|
||||||
#include <array>
|
|
||||||
#include <cassert>
|
|
||||||
#include <cfloat>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#if defined(GGML_USE_HIPBLAS)
|
|
||||||
#include <hip/hip_runtime.h>
|
|
||||||
#include <hipblas/hipblas.h>
|
|
||||||
#include <hip/hip_fp16.h>
|
|
||||||
#ifdef __HIP_PLATFORM_AMD__
|
|
||||||
// for rocblas_initialize()
|
|
||||||
#include "rocblas/rocblas.h"
|
|
||||||
#endif // __HIP_PLATFORM_AMD__
|
|
||||||
#define CUBLAS_COMPUTE_16F HIPBLAS_R_16F
|
|
||||||
#define CUBLAS_COMPUTE_32F HIPBLAS_R_32F
|
|
||||||
#define CUBLAS_COMPUTE_32F_FAST_16F HIPBLAS_R_32F
|
|
||||||
#define CUBLAS_GEMM_DEFAULT HIPBLAS_GEMM_DEFAULT
|
|
||||||
#define CUBLAS_GEMM_DEFAULT_TENSOR_OP HIPBLAS_GEMM_DEFAULT
|
|
||||||
#define CUBLAS_OP_N HIPBLAS_OP_N
|
|
||||||
#define CUBLAS_OP_T HIPBLAS_OP_T
|
|
||||||
#define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
|
|
||||||
#define CUBLAS_TF32_TENSOR_OP_MATH 0
|
|
||||||
#define CUDA_R_16F HIPBLAS_R_16F
|
|
||||||
#define CUDA_R_32F HIPBLAS_R_32F
|
|
||||||
#define __shfl_xor_sync(mask, var, laneMask, width) __shfl_xor(var, laneMask, width)
|
|
||||||
#define cublasComputeType_t hipblasDatatype_t //deprecated, new hipblasComputeType_t not in 5.6
|
|
||||||
#define cublasCreate hipblasCreate
|
|
||||||
#define cublasDestroy hipblasDestroy
|
|
||||||
#define cublasGemmEx hipblasGemmEx
|
|
||||||
#define cublasGemmBatchedEx hipblasGemmBatchedEx
|
|
||||||
#define cublasGemmStridedBatchedEx hipblasGemmStridedBatchedEx
|
|
||||||
#define cublasHandle_t hipblasHandle_t
|
|
||||||
#define cublasSetMathMode(handle, mode) CUBLAS_STATUS_SUCCESS
|
|
||||||
#define cublasSetStream hipblasSetStream
|
|
||||||
#define cublasSgemm hipblasSgemm
|
|
||||||
#define cublasStatus_t hipblasStatus_t
|
|
||||||
#define cudaDataType_t hipblasDatatype_t //deprecated, new hipblasDatatype not in 5.6
|
|
||||||
#define cudaDeviceCanAccessPeer hipDeviceCanAccessPeer
|
|
||||||
#define cudaDeviceDisablePeerAccess hipDeviceDisablePeerAccess
|
|
||||||
#define cudaDeviceEnablePeerAccess hipDeviceEnablePeerAccess
|
|
||||||
#define cudaDeviceProp hipDeviceProp_t
|
|
||||||
#define cudaDeviceSynchronize hipDeviceSynchronize
|
|
||||||
#define cudaError_t hipError_t
|
|
||||||
#define cudaErrorPeerAccessAlreadyEnabled hipErrorPeerAccessAlreadyEnabled
|
|
||||||
#define cudaErrorPeerAccessNotEnabled hipErrorPeerAccessNotEnabled
|
|
||||||
#define cudaEventCreateWithFlags hipEventCreateWithFlags
|
|
||||||
#define cudaEventDisableTiming hipEventDisableTiming
|
|
||||||
#define cudaEventRecord hipEventRecord
|
|
||||||
#define cudaEventSynchronize hipEventSynchronize
|
|
||||||
#define cudaEvent_t hipEvent_t
|
|
||||||
#define cudaEventDestroy hipEventDestroy
|
|
||||||
#define cudaFree hipFree
|
|
||||||
#define cudaFreeHost hipHostFree
|
|
||||||
#define cudaGetDevice hipGetDevice
|
|
||||||
#define cudaGetDeviceCount hipGetDeviceCount
|
|
||||||
#define cudaGetDeviceProperties hipGetDeviceProperties
|
|
||||||
#define cudaGetErrorString hipGetErrorString
|
|
||||||
#define cudaGetLastError hipGetLastError
|
|
||||||
#define cudaHostRegister hipHostRegister
|
|
||||||
#define cudaHostRegisterPortable hipHostRegisterPortable
|
|
||||||
#define cudaHostRegisterReadOnly hipHostRegisterReadOnly
|
|
||||||
#define cudaHostUnregister hipHostUnregister
|
|
||||||
#define cudaLaunchHostFunc hipLaunchHostFunc
|
|
||||||
#ifdef GGML_HIP_UMA
|
|
||||||
#define cudaMalloc hipMallocManaged
|
|
||||||
#define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size)
|
|
||||||
#else
|
|
||||||
#define cudaMalloc hipMalloc
|
|
||||||
#define cudaMallocHost(ptr, size) hipHostMalloc(ptr, size, hipHostMallocDefault)
|
|
||||||
#endif
|
|
||||||
#define cudaMemcpy hipMemcpy
|
|
||||||
#define cudaMemcpyAsync hipMemcpyAsync
|
|
||||||
#define cudaMemcpyPeerAsync hipMemcpyPeerAsync
|
|
||||||
#define cudaMemcpy2DAsync hipMemcpy2DAsync
|
|
||||||
#define cudaMemcpyDeviceToDevice hipMemcpyDeviceToDevice
|
|
||||||
#define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost
|
|
||||||
#define cudaMemcpyHostToDevice hipMemcpyHostToDevice
|
|
||||||
#define cudaMemcpyKind hipMemcpyKind
|
|
||||||
#define cudaMemset hipMemset
|
|
||||||
#define cudaMemsetAsync hipMemsetAsync
|
|
||||||
#define cudaMemGetInfo hipMemGetInfo
|
|
||||||
#define cudaOccupancyMaxPotentialBlockSize hipOccupancyMaxPotentialBlockSize
|
|
||||||
#define cudaSetDevice hipSetDevice
|
|
||||||
#define cudaStreamCreateWithFlags hipStreamCreateWithFlags
|
|
||||||
#define cudaStreamDestroy hipStreamDestroy
|
|
||||||
#define cudaStreamFireAndForget hipStreamFireAndForget
|
|
||||||
#define cudaStreamNonBlocking hipStreamNonBlocking
|
|
||||||
#define cudaStreamPerThread hipStreamPerThread
|
|
||||||
#define cudaStreamSynchronize hipStreamSynchronize
|
|
||||||
#define cudaStreamWaitEvent(stream, event, flags) hipStreamWaitEvent(stream, event, flags)
|
|
||||||
#define cudaStream_t hipStream_t
|
|
||||||
#define cudaSuccess hipSuccess
|
|
||||||
#define __trap abort
|
|
||||||
#define CUBLAS_STATUS_SUCCESS HIPBLAS_STATUS_SUCCESS
|
|
||||||
#define CUBLAS_STATUS_NOT_INITIALIZED HIPBLAS_STATUS_NOT_INITIALIZED
|
|
||||||
#define CUBLAS_STATUS_ALLOC_FAILED HIPBLAS_STATUS_ALLOC_FAILED
|
|
||||||
#define CUBLAS_STATUS_INVALID_VALUE HIPBLAS_STATUS_INVALID_VALUE
|
|
||||||
#define CUBLAS_STATUS_ARCH_MISMATCH HIPBLAS_STATUS_ARCH_MISMATCH
|
|
||||||
#define CUBLAS_STATUS_MAPPING_ERROR HIPBLAS_STATUS_MAPPING_ERROR
|
|
||||||
#define CUBLAS_STATUS_EXECUTION_FAILED HIPBLAS_STATUS_EXECUTION_FAILED
|
|
||||||
#define CUBLAS_STATUS_INTERNAL_ERROR HIPBLAS_STATUS_INTERNAL_ERROR
|
|
||||||
#define CUBLAS_STATUS_NOT_SUPPORTED HIPBLAS_STATUS_NOT_SUPPORTED
|
|
||||||
#else
|
|
||||||
#include <cuda_runtime.h>
|
|
||||||
#include <cuda.h>
|
|
||||||
#include <cublas_v2.h>
|
|
||||||
#include <cuda_fp16.h>
|
|
||||||
|
|
||||||
#if CUDART_VERSION < 11020
|
|
||||||
#define CU_DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED CU_DEVICE_ATTRIBUTE_VIRTUAL_ADDRESS_MANAGEMENT_SUPPORTED
|
|
||||||
#define CUBLAS_TF32_TENSOR_OP_MATH CUBLAS_TENSOR_OP_MATH
|
|
||||||
#define CUBLAS_COMPUTE_16F CUDA_R_16F
|
|
||||||
#define CUBLAS_COMPUTE_32F CUDA_R_32F
|
|
||||||
#define cublasComputeType_t cudaDataType_t
|
|
||||||
#endif // CUDART_VERSION < 11020
|
|
||||||
|
|
||||||
#endif // defined(GGML_USE_HIPBLAS)
|
|
||||||
|
|
||||||
#define STRINGIZE_IMPL(...) #__VA_ARGS__
|
|
||||||
#define STRINGIZE(...) STRINGIZE_IMPL(__VA_ARGS__)
|
|
||||||
|
|
||||||
#define WARP_SIZE 32
|
|
||||||
#define CUDART_HMAX 11070 // CUDA 11.7, min. ver. for which __hmax and __hmax2 are known to work (may be higher than needed)
|
|
||||||
|
|
||||||
#define CC_PASCAL 600
|
|
||||||
#define MIN_CC_DP4A 610 // minimum compute capability for __dp4a, an intrinsic for byte-wise dot products
|
|
||||||
#define CC_VOLTA 700
|
|
||||||
#define CC_OFFSET_AMD 1000000
|
|
||||||
#define CC_RDNA1 (CC_OFFSET_AMD + 1010)
|
|
||||||
#define CC_RDNA2 (CC_OFFSET_AMD + 1030)
|
|
||||||
#define CC_RDNA3 (CC_OFFSET_AMD + 1100)
|
|
||||||
|
|
||||||
// define this if you want to always fallback to MMQ kernels and not use cuBLAS for matrix multiplication
|
|
||||||
// on modern hardware, using cuBLAS is recommended as it utilizes F16 tensor cores which are very performant
|
|
||||||
// for large computational tasks. the drawback is that this requires some extra amount of VRAM:
|
|
||||||
// - 7B quantum model: +100-200 MB
|
|
||||||
// - 13B quantum model: +200-400 MB
|
|
||||||
//
|
|
||||||
//#define GGML_CUDA_FORCE_MMQ
|
|
||||||
|
|
||||||
// TODO: improve this to be correct for more hardware
|
|
||||||
// for example, currently fails for GeForce GTX 1660 which is TURING arch (> VOLTA) but does not have tensor cores
|
|
||||||
#if !defined(GGML_CUDA_FORCE_MMQ)
|
|
||||||
#define CUDA_USE_TENSOR_CORES
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define MMVQ_MAX_BATCH_SIZE 8 // max batch size to use MMVQ kernels
|
|
||||||
#define MMQ_MAX_BATCH_SIZE 32 // max batch size to use MMQ kernels when tensor cores are available
|
|
||||||
|
|
||||||
#define MATRIX_ROW_PADDING 512 // last row of quant. matrices is a multiple of this to avoid out-of-bounds memory accesses
|
|
||||||
|
|
||||||
#if defined(_MSC_VER)
|
|
||||||
#pragma warning(disable: 4244 4267) // possible loss of data
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define GGML_CUDA_MAX_STREAMS 8
|
|
||||||
|
|
||||||
[[noreturn]]
|
|
||||||
void ggml_cuda_error(const char * stmt, const char * func, const char * file, int line, const char * msg);
|
|
||||||
|
|
||||||
#define CUDA_CHECK_GEN(err, success, error_fn) \
|
|
||||||
do { \
|
|
||||||
auto err_ = (err); \
|
|
||||||
if (err_ != (success)) { \
|
|
||||||
ggml_cuda_error(#err, __func__, __FILE__, __LINE__, error_fn(err_)); \
|
|
||||||
} \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define CUDA_CHECK(err) CUDA_CHECK_GEN(err, cudaSuccess, cudaGetErrorString)
|
|
||||||
|
|
||||||
#if CUDART_VERSION >= 12000
|
|
||||||
static const char * cublas_get_error_str(const cublasStatus_t err) {
|
|
||||||
return cublasGetStatusString(err);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
static const char * cublas_get_error_str(const cublasStatus_t err) {
|
|
||||||
switch (err) {
|
|
||||||
case CUBLAS_STATUS_SUCCESS: return "CUBLAS_STATUS_SUCCESS";
|
|
||||||
case CUBLAS_STATUS_NOT_INITIALIZED: return "CUBLAS_STATUS_NOT_INITIALIZED";
|
|
||||||
case CUBLAS_STATUS_ALLOC_FAILED: return "CUBLAS_STATUS_ALLOC_FAILED";
|
|
||||||
case CUBLAS_STATUS_INVALID_VALUE: return "CUBLAS_STATUS_INVALID_VALUE";
|
|
||||||
case CUBLAS_STATUS_ARCH_MISMATCH: return "CUBLAS_STATUS_ARCH_MISMATCH";
|
|
||||||
case CUBLAS_STATUS_MAPPING_ERROR: return "CUBLAS_STATUS_MAPPING_ERROR";
|
|
||||||
case CUBLAS_STATUS_EXECUTION_FAILED: return "CUBLAS_STATUS_EXECUTION_FAILED";
|
|
||||||
case CUBLAS_STATUS_INTERNAL_ERROR: return "CUBLAS_STATUS_INTERNAL_ERROR";
|
|
||||||
case CUBLAS_STATUS_NOT_SUPPORTED: return "CUBLAS_STATUS_NOT_SUPPORTED";
|
|
||||||
default: return "unknown error";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif // CUDART_VERSION >= 12000
|
|
||||||
|
|
||||||
#define CUBLAS_CHECK(err) CUDA_CHECK_GEN(err, CUBLAS_STATUS_SUCCESS, cublas_get_error_str)
|
|
||||||
|
|
||||||
#if !defined(GGML_USE_HIPBLAS)
|
|
||||||
static const char * cu_get_error_str(CUresult err) {
|
|
||||||
const char * err_str;
|
|
||||||
cuGetErrorString(err, &err_str);
|
|
||||||
return err_str;
|
|
||||||
}
|
|
||||||
#define CU_CHECK(err) CUDA_CHECK_GEN(err, CUDA_SUCCESS, cu_get_error_str)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if CUDART_VERSION >= 11100
|
|
||||||
#define GGML_CUDA_ASSUME(x) __builtin_assume(x)
|
|
||||||
#else
|
|
||||||
#define GGML_CUDA_ASSUME(x)
|
|
||||||
#endif // CUDART_VERSION >= 11100
|
|
||||||
|
|
||||||
#ifdef GGML_CUDA_F16
|
|
||||||
typedef half dfloat; // dequantize float
|
|
||||||
typedef half2 dfloat2;
|
|
||||||
#else
|
|
||||||
typedef float dfloat; // dequantize float
|
|
||||||
typedef float2 dfloat2;
|
|
||||||
#endif //GGML_CUDA_F16
|
|
||||||
|
|
||||||
[[noreturn]]
|
|
||||||
static __device__ void no_device_code(
|
|
||||||
const char * file_name, const int line, const char * function_name, const int arch, const char * arch_list) {
|
|
||||||
|
|
||||||
#if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
|
|
||||||
printf("%s:%d: ERROR: HIP kernel %s has no device code compatible with HIP arch %d.\n",
|
|
||||||
file_name, line, function_name, arch);
|
|
||||||
GGML_UNUSED(arch_list);
|
|
||||||
#else
|
|
||||||
printf("%s:%d: ERROR: CUDA kernel %s has no device code compatible with CUDA arch %d. ggml-cuda.cu was compiled for: %s\n",
|
|
||||||
file_name, line, function_name, arch, arch_list);
|
|
||||||
#endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)
|
|
||||||
__trap();
|
|
||||||
|
|
||||||
GGML_UNUSED(no_device_code); // suppress unused function warning
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef __CUDA_ARCH__
|
|
||||||
#define NO_DEVICE_CODE no_device_code(__FILE__, __LINE__, __FUNCTION__, __CUDA_ARCH__, STRINGIZE(__CUDA_ARCH_LIST__))
|
|
||||||
#else
|
|
||||||
#define NO_DEVICE_CODE //GGML_ASSERT(false && "NO_DEVICE_CODE not valid in host code.")
|
|
||||||
#endif // __CUDA_ARCH__
|
|
||||||
|
|
||||||
static __device__ __forceinline__ float warp_reduce_sum(float x) {
|
|
||||||
#pragma unroll
|
|
||||||
for (int mask = 16; mask > 0; mask >>= 1) {
|
|
||||||
x += __shfl_xor_sync(0xffffffff, x, mask, 32);
|
|
||||||
}
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ __forceinline__ float2 warp_reduce_sum(float2 a) {
|
|
||||||
#pragma unroll
|
|
||||||
for (int mask = 16; mask > 0; mask >>= 1) {
|
|
||||||
a.x += __shfl_xor_sync(0xffffffff, a.x, mask, 32);
|
|
||||||
a.y += __shfl_xor_sync(0xffffffff, a.y, mask, 32);
|
|
||||||
}
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef GGML_CUDA_F16
|
|
||||||
static __device__ __forceinline__ half2 warp_reduce_sum(half2 a) {
|
|
||||||
#if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
|
|
||||||
#pragma unroll
|
|
||||||
for (int mask = 16; mask > 0; mask >>= 1) {
|
|
||||||
a = __hadd2(a, __shfl_xor_sync(0xffffffff, a, mask, 32));
|
|
||||||
}
|
|
||||||
return a;
|
|
||||||
#else
|
|
||||||
GGML_UNUSED(a);
|
|
||||||
NO_DEVICE_CODE;
|
|
||||||
#endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL
|
|
||||||
}
|
|
||||||
#endif // GGML_CUDA_F16
|
|
||||||
|
|
||||||
static __device__ __forceinline__ float warp_reduce_max(float x) {
|
|
||||||
#pragma unroll
|
|
||||||
for (int mask = 16; mask > 0; mask >>= 1) {
|
|
||||||
x = fmaxf(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
|
|
||||||
}
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
//static __device__ __forceinline__ half2 warp_reduce_max(half2 x) {
|
|
||||||
//#if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
|
|
||||||
//#pragma unroll
|
|
||||||
// for (int mask = 16; mask > 0; mask >>= 1) {
|
|
||||||
// x = __hmax2(x, __shfl_xor_sync(0xffffffff, x, mask, 32));
|
|
||||||
// }
|
|
||||||
// return x;
|
|
||||||
//#else
|
|
||||||
// GGML_UNUSED(x);
|
|
||||||
// NO_DEVICE_CODE;
|
|
||||||
//#endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__)) && __CUDA_ARCH__ >= CC_PASCAL && CUDART_VERSION >= CUDART_HMAX
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(GGML_USE_HIPBLAS)
|
|
||||||
#define __CUDA_ARCH__ 1300
|
|
||||||
|
|
||||||
#if defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) || \
|
|
||||||
defined(__gfx1150__) || defined(__gfx1151__)
|
|
||||||
#define RDNA3
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(__gfx1030__) || defined(__gfx1031__) || defined(__gfx1032__) || defined(__gfx1033__) || \
|
|
||||||
defined(__gfx1034__) || defined(__gfx1035__) || defined(__gfx1036__) || defined(__gfx1037__)
|
|
||||||
#define RDNA2
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef __has_builtin
|
|
||||||
#define __has_builtin(x) 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef int8_t int8x4_t __attribute__((ext_vector_type(4)));
|
|
||||||
typedef uint8_t uint8x4_t __attribute__((ext_vector_type(4)));
|
|
||||||
static __device__ __forceinline__ int __vsubss4(const int a, const int b) {
|
|
||||||
const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
|
|
||||||
const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
|
|
||||||
#if __has_builtin(__builtin_elementwise_sub_sat)
|
|
||||||
const int8x4_t c = __builtin_elementwise_sub_sat(va, vb);
|
|
||||||
return reinterpret_cast<const int &>(c);
|
|
||||||
#else
|
|
||||||
int8x4_t c;
|
|
||||||
int16_t tmp;
|
|
||||||
#pragma unroll
|
|
||||||
for (int i = 0; i < 4; i++) {
|
|
||||||
tmp = va[i] - vb[i];
|
|
||||||
if(tmp > std::numeric_limits<int8_t>::max()) tmp = std::numeric_limits<int8_t>::max();
|
|
||||||
if(tmp < std::numeric_limits<int8_t>::min()) tmp = std::numeric_limits<int8_t>::min();
|
|
||||||
c[i] = tmp;
|
|
||||||
}
|
|
||||||
return reinterpret_cast<int &>(c);
|
|
||||||
#endif // __has_builtin(__builtin_elementwise_sub_sat)
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ __forceinline__ int __vsub4(const int a, const int b) {
|
|
||||||
return __vsubss4(a, b);
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ __forceinline__ unsigned int __vcmpeq4(unsigned int a, unsigned int b) {
|
|
||||||
const uint8x4_t& va = reinterpret_cast<const uint8x4_t&>(a);
|
|
||||||
const uint8x4_t& vb = reinterpret_cast<const uint8x4_t&>(b);
|
|
||||||
unsigned int c;
|
|
||||||
uint8x4_t& vc = reinterpret_cast<uint8x4_t&>(c);
|
|
||||||
#pragma unroll
|
|
||||||
for (int i = 0; i < 4; ++i) {
|
|
||||||
vc[i] = va[i] == vb[i] ? 0xff : 0x00;
|
|
||||||
}
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ __forceinline__ int __dp4a(const int a, const int b, int c) {
|
|
||||||
#if defined(__gfx906__) || defined(__gfx908__) || defined(__gfx90a__) || defined(__gfx1030__)
|
|
||||||
c = __builtin_amdgcn_sdot4(a, b, c, false);
|
|
||||||
#elif defined(RDNA3)
|
|
||||||
c = __builtin_amdgcn_sudot4( true, a, true, b, c, false);
|
|
||||||
#elif defined(__gfx1010__) || defined(__gfx900__)
|
|
||||||
int tmp1;
|
|
||||||
int tmp2;
|
|
||||||
asm("\n \
|
|
||||||
v_mul_i32_i24 %1, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_0 src1_sel:BYTE_0 \n \
|
|
||||||
v_mul_i32_i24 %2, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_1 src1_sel:BYTE_1 \n \
|
|
||||||
v_add3_u32 %0, %1, %2, %0 \n \
|
|
||||||
v_mul_i32_i24 %1, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_2 src1_sel:BYTE_2 \n \
|
|
||||||
v_mul_i32_i24 %2, sext(%3), sext(%4) dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_3 src1_sel:BYTE_3 \n \
|
|
||||||
v_add3_u32 %0, %1, %2, %0 \n \
|
|
||||||
"
|
|
||||||
: "+v"(c), "=&v"(tmp1), "=&v"(tmp2)
|
|
||||||
: "v"(a), "v"(b)
|
|
||||||
);
|
|
||||||
#else
|
|
||||||
const int8x4_t va = reinterpret_cast<const int8x4_t&>(a);
|
|
||||||
const int8x4_t vb = reinterpret_cast<const int8x4_t&>(b);
|
|
||||||
c += va[0] * vb[0] + va[1] * vb[1] + va[2] * vb[2] + va[3] * vb[3];
|
|
||||||
#endif
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
#endif // defined(GGML_USE_HIPBLAS)
|
|
||||||
|
|
||||||
// TODO: move to ggml-common.h
|
|
||||||
static const __device__ int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
|
|
||||||
|
|
||||||
typedef void (*dequantize_kernel_t)(const void * vx, const int64_t ib, const int iqs, dfloat2 & v);
|
|
||||||
|
|
||||||
|
|
||||||
//////////////////////
|
|
||||||
|
|
||||||
struct ggml_cuda_device_info {
|
|
||||||
int device_count;
|
|
||||||
|
|
||||||
struct cuda_device_info {
|
|
||||||
int cc; // compute capability
|
|
||||||
size_t smpb; // max. shared memory per block
|
|
||||||
bool vmm; // virtual memory support
|
|
||||||
size_t vmm_granularity; // granularity of virtual memory
|
|
||||||
size_t total_vram;
|
|
||||||
};
|
|
||||||
|
|
||||||
cuda_device_info devices[GGML_CUDA_MAX_DEVICES] = {};
|
|
||||||
|
|
||||||
std::array<float, GGML_CUDA_MAX_DEVICES> default_tensor_split = {};
|
|
||||||
};
|
|
||||||
|
|
||||||
const ggml_cuda_device_info & ggml_cuda_info();
|
|
||||||
|
|
||||||
void ggml_cuda_set_device(int device);
|
|
||||||
int ggml_cuda_get_device();
|
|
||||||
|
|
||||||
struct ggml_cuda_pool {
|
|
||||||
virtual ~ggml_cuda_pool() = default;
|
|
||||||
|
|
||||||
virtual void * alloc(size_t size, size_t * actual_size) = 0;
|
|
||||||
virtual void free(void * ptr, size_t size) = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
struct ggml_cuda_pool_alloc {
|
|
||||||
ggml_cuda_pool * pool = nullptr;
|
|
||||||
T * ptr = nullptr;
|
|
||||||
size_t actual_size = 0;
|
|
||||||
|
|
||||||
ggml_cuda_pool_alloc() = default;
|
|
||||||
|
|
||||||
explicit ggml_cuda_pool_alloc(ggml_cuda_pool & pool) : pool(&pool) {
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_cuda_pool_alloc(ggml_cuda_pool & pool, size_t size) : pool(&pool) {
|
|
||||||
alloc(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
~ggml_cuda_pool_alloc() {
|
|
||||||
if (ptr != nullptr) {
|
|
||||||
pool->free(ptr, actual_size);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// size is in number of elements
|
|
||||||
T * alloc(size_t size) {
|
|
||||||
GGML_ASSERT(pool != nullptr);
|
|
||||||
GGML_ASSERT(ptr == nullptr);
|
|
||||||
ptr = (T *) pool->alloc(size * sizeof(T), &this->actual_size);
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
T * alloc(ggml_cuda_pool & pool, size_t size) {
|
|
||||||
this->pool = &pool;
|
|
||||||
return alloc(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
T * get() {
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_cuda_pool_alloc(const ggml_cuda_pool_alloc &) = delete;
|
|
||||||
ggml_cuda_pool_alloc(ggml_cuda_pool_alloc &&) = delete;
|
|
||||||
ggml_cuda_pool_alloc& operator=(const ggml_cuda_pool_alloc &) = delete;
|
|
||||||
ggml_cuda_pool_alloc& operator=(ggml_cuda_pool_alloc &&) = delete;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// backend interface
|
|
||||||
|
|
||||||
struct ggml_tensor_extra_gpu {
|
|
||||||
void * data_device[GGML_CUDA_MAX_DEVICES]; // 1 pointer for each device for split tensors
|
|
||||||
cudaEvent_t events[GGML_CUDA_MAX_DEVICES][GGML_CUDA_MAX_STREAMS]; // events for synchronizing multiple GPUs
|
|
||||||
};
|
|
||||||
|
|
||||||
struct ggml_backend_cuda_context {
|
|
||||||
int device;
|
|
||||||
std::string name;
|
|
||||||
cudaEvent_t copy_event = nullptr;
|
|
||||||
|
|
||||||
cudaStream_t streams[GGML_CUDA_MAX_DEVICES][GGML_CUDA_MAX_STREAMS] = { { nullptr } };
|
|
||||||
cublasHandle_t cublas_handles[GGML_CUDA_MAX_DEVICES] = {nullptr};
|
|
||||||
|
|
||||||
explicit ggml_backend_cuda_context(int device) :
|
|
||||||
device(device),
|
|
||||||
name(GGML_CUDA_NAME + std::to_string(device)) {
|
|
||||||
}
|
|
||||||
|
|
||||||
~ggml_backend_cuda_context() {
|
|
||||||
if (copy_event != nullptr) {
|
|
||||||
CUDA_CHECK(cudaEventDestroy(copy_event));
|
|
||||||
}
|
|
||||||
for (int i = 0; i < GGML_CUDA_MAX_DEVICES; ++i) {
|
|
||||||
for (int j = 0; j < GGML_CUDA_MAX_STREAMS; ++j) {
|
|
||||||
if (streams[i][j] != nullptr) {
|
|
||||||
CUDA_CHECK(cudaStreamDestroy(streams[i][j]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (cublas_handles[i] != nullptr) {
|
|
||||||
CUBLAS_CHECK(cublasDestroy(cublas_handles[i]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cudaStream_t stream(int device, int stream) {
|
|
||||||
if (streams[device][stream] == nullptr) {
|
|
||||||
ggml_cuda_set_device(device);
|
|
||||||
CUDA_CHECK(cudaStreamCreateWithFlags(&streams[device][stream], cudaStreamNonBlocking));
|
|
||||||
}
|
|
||||||
return streams[device][stream];
|
|
||||||
}
|
|
||||||
|
|
||||||
cudaStream_t stream() {
|
|
||||||
return stream(device, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
cublasHandle_t cublas_handle(int device) {
|
|
||||||
if (cublas_handles[device] == nullptr) {
|
|
||||||
ggml_cuda_set_device(device);
|
|
||||||
CUBLAS_CHECK(cublasCreate(&cublas_handles[device]));
|
|
||||||
CUBLAS_CHECK(cublasSetMathMode(cublas_handles[device], CUBLAS_TF32_TENSOR_OP_MATH));
|
|
||||||
}
|
|
||||||
return cublas_handles[device];
|
|
||||||
}
|
|
||||||
|
|
||||||
cublasHandle_t cublas_handle() {
|
|
||||||
return cublas_handle(device);
|
|
||||||
}
|
|
||||||
|
|
||||||
// pool
|
|
||||||
std::unique_ptr<ggml_cuda_pool> pools[GGML_CUDA_MAX_DEVICES];
|
|
||||||
|
|
||||||
static std::unique_ptr<ggml_cuda_pool> new_pool_for_device(int device);
|
|
||||||
|
|
||||||
ggml_cuda_pool & pool(int device) {
|
|
||||||
if (pools[device] == nullptr) {
|
|
||||||
pools[device] = new_pool_for_device(device);
|
|
||||||
}
|
|
||||||
return *pools[device];
|
|
||||||
}
|
|
||||||
|
|
||||||
ggml_cuda_pool & pool() {
|
|
||||||
return pool(device);
|
|
||||||
}
|
|
||||||
};
|
|
@ -1,49 +0,0 @@
|
|||||||
#include "concat.cuh"
|
|
||||||
|
|
||||||
static __global__ void concat_f32(const float * x,const float * y, float * dst, const int ne0, const int ne02) {
|
|
||||||
int nidx = threadIdx.x + blockIdx.x * blockDim.x;
|
|
||||||
if (nidx >= ne0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// operation
|
|
||||||
int offset_dst =
|
|
||||||
nidx +
|
|
||||||
blockIdx.y * ne0 +
|
|
||||||
blockIdx.z * ne0 * gridDim.y;
|
|
||||||
if (blockIdx.z < ne02) { // src0
|
|
||||||
int offset_src =
|
|
||||||
nidx +
|
|
||||||
blockIdx.y * ne0 +
|
|
||||||
blockIdx.z * ne0 * gridDim.y;
|
|
||||||
dst[offset_dst] = x[offset_src];
|
|
||||||
} else {
|
|
||||||
int offset_src =
|
|
||||||
nidx +
|
|
||||||
blockIdx.y * ne0 +
|
|
||||||
(blockIdx.z - ne02) * ne0 * gridDim.y;
|
|
||||||
dst[offset_dst] = y[offset_src];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void concat_f32_cuda(const float * x, const float * y, float * dst, const int ne0, int ne1, int ne2, int ne02, cudaStream_t stream) {
|
|
||||||
int num_blocks = (ne0 + CUDA_CONCAT_BLOCK_SIZE - 1) / CUDA_CONCAT_BLOCK_SIZE;
|
|
||||||
dim3 gridDim(num_blocks, ne1, ne2);
|
|
||||||
concat_f32<<<gridDim, CUDA_CONCAT_BLOCK_SIZE, 0, stream>>>(x, y, dst, ne0, ne02);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_concat(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
const ggml_tensor * src1 = dst->src[1];
|
|
||||||
const float * src0_d = (const float *)src0->data;
|
|
||||||
const float * src1_d = (const float *)src1->data;
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
GGML_ASSERT(src0->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT(src1->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT(dst->type == GGML_TYPE_F32);
|
|
||||||
|
|
||||||
for (int i3 = 0; i3 < dst->ne[3]; i3++) {
|
|
||||||
concat_f32_cuda(src0_d + i3 * (src0->nb[3] / 4), src1_d + i3 * (src1->nb[3] / 4), dst_d + i3 * (dst->nb[3] / 4), dst->ne[0], dst->ne[1], dst->ne[2], src0->ne[2], stream);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
#define CUDA_CONCAT_BLOCK_SIZE 256
|
|
||||||
|
|
||||||
void ggml_cuda_op_concat(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
@ -1,824 +0,0 @@
|
|||||||
#include "convert.cuh"
|
|
||||||
#include "dequantize.cuh"
|
|
||||||
|
|
||||||
#define CUDA_Q8_0_NE_ALIGN 2048
|
|
||||||
|
|
||||||
template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
|
|
||||||
static __global__ void dequantize_block(const void * __restrict__ vx, dst_t * __restrict__ y, const int64_t k) {
|
|
||||||
const int64_t i = 2*(blockDim.x*blockIdx.x + threadIdx.x);
|
|
||||||
|
|
||||||
if (i >= k) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int64_t ib = i/qk; // block index
|
|
||||||
const int iqs = (i%qk)/qr; // quant index
|
|
||||||
const int iybs = i - i%qk; // y block start index
|
|
||||||
const int y_offset = qr == 1 ? 1 : qk/2;
|
|
||||||
|
|
||||||
// dequantize
|
|
||||||
dfloat2 v;
|
|
||||||
dequantize_kernel(vx, ib, iqs, v);
|
|
||||||
|
|
||||||
y[iybs + iqs + 0] = v.x;
|
|
||||||
y[iybs + iqs + y_offset] = v.y;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <bool need_check>
|
|
||||||
static __global__ void dequantize_block_q8_0_f16(const void * __restrict__ vx, half * __restrict__ y, const int64_t k) {
|
|
||||||
#if __CUDA_ARCH__ >= CC_PASCAL
|
|
||||||
constexpr int nint = CUDA_Q8_0_NE_ALIGN/sizeof(int) + WARP_SIZE;
|
|
||||||
|
|
||||||
const int i0 = CUDA_Q8_0_NE_ALIGN*blockIdx.x;
|
|
||||||
const int * x0 = ((int *) vx) + blockIdx.x * nint;
|
|
||||||
half2 * y2 = (half2 *) (y + i0);
|
|
||||||
|
|
||||||
__shared__ int vals[nint];
|
|
||||||
|
|
||||||
#pragma unroll
|
|
||||||
for (int ix0 = 0; ix0 < nint; ix0 += WARP_SIZE) {
|
|
||||||
if (need_check && i0*sizeof(block_q8_0)/QK8_0 + sizeof(int)*(ix0 + threadIdx.x) >= k*sizeof(block_q8_0)/QK8_0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int ix = ix0 + threadIdx.x;
|
|
||||||
vals[ix] = x0[ix];
|
|
||||||
}
|
|
||||||
|
|
||||||
#pragma unroll
|
|
||||||
for (int iy = 0; iy < CUDA_Q8_0_NE_ALIGN; iy += 2*WARP_SIZE) {
|
|
||||||
if (need_check && i0 + iy + 2*threadIdx.x >= k) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const half * b0 = ((const half *) vals) + (sizeof(block_q8_0)/sizeof(half)) * ((iy + 2*threadIdx.x)/QK8_0);
|
|
||||||
const half d = *b0;
|
|
||||||
const char2 qs = ((const char2 *) (b0 + 1))[threadIdx.x % (QK8_0/2)];
|
|
||||||
|
|
||||||
y2[iy/2 + threadIdx.x] = __hmul2(make_half2(qs.x, qs.y), __half2half2(d));
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
GGML_UNUSED(vx);
|
|
||||||
GGML_UNUSED(y);
|
|
||||||
GGML_UNUSED(k);
|
|
||||||
NO_DEVICE_CODE;
|
|
||||||
#endif // __CUDA_ARCH__ >= CC_PASCAL
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static __global__ void dequantize_block_q4_0(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
|
|
||||||
|
|
||||||
const int64_t i = blockIdx.x;
|
|
||||||
|
|
||||||
// assume 32 threads
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
const int il = tid/8;
|
|
||||||
const int ir = tid%8;
|
|
||||||
const int64_t ib = 8*i + ir;
|
|
||||||
if (ib >= nb32) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
dst_t * y = yy + 256*i + 32*ir + 4*il;
|
|
||||||
|
|
||||||
const block_q4_0 * x = (const block_q4_0 *)vx + ib;
|
|
||||||
const float d = __half2float(x->d);
|
|
||||||
const float dm = -8*d;
|
|
||||||
|
|
||||||
const uint8_t * q = x->qs + 4*il;
|
|
||||||
|
|
||||||
for (int l = 0; l < 4; ++l) {
|
|
||||||
y[l+ 0] = d * (q[l] & 0xF) + dm;
|
|
||||||
y[l+16] = d * (q[l] >> 4) + dm;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static __global__ void dequantize_block_q4_1(const void * __restrict__ vx, dst_t * __restrict__ yy, int nb32) {
|
|
||||||
|
|
||||||
const int64_t i = blockIdx.x;
|
|
||||||
|
|
||||||
// assume 32 threads
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
const int il = tid/8;
|
|
||||||
const int ir = tid%8;
|
|
||||||
const int64_t ib = 8*i + ir;
|
|
||||||
if (ib >= nb32) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
dst_t * y = yy + 256*i + 32*ir + 4*il;
|
|
||||||
|
|
||||||
const block_q4_1 * x = (const block_q4_1 *)vx + ib;
|
|
||||||
const float2 d = __half22float2(x->dm);
|
|
||||||
|
|
||||||
const uint8_t * q = x->qs + 4*il;
|
|
||||||
|
|
||||||
for (int l = 0; l < 4; ++l) {
|
|
||||||
y[l+ 0] = d.x * (q[l] & 0xF) + d.y;
|
|
||||||
y[l+16] = d.x * (q[l] >> 4) + d.y;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//================================== k-quants
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static __global__ void dequantize_block_q2_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
|
|
||||||
|
|
||||||
const int i = blockIdx.x;
|
|
||||||
const block_q2_K * x = (const block_q2_K *) vx;
|
|
||||||
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
#if QK_K == 256
|
|
||||||
const int n = tid/32;
|
|
||||||
const int l = tid - 32*n;
|
|
||||||
const int is = 8*n + l/16;
|
|
||||||
|
|
||||||
const uint8_t q = x[i].qs[32*n + l];
|
|
||||||
dst_t * y = yy + i*QK_K + 128*n;
|
|
||||||
|
|
||||||
float dall = __low2half(x[i].dm);
|
|
||||||
float dmin = __high2half(x[i].dm);
|
|
||||||
y[l+ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
|
|
||||||
y[l+32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 2) & 3) - dmin * (x[i].scales[is+2] >> 4);
|
|
||||||
y[l+64] = dall * (x[i].scales[is+4] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+4] >> 4);
|
|
||||||
y[l+96] = dall * (x[i].scales[is+6] & 0xF) * ((q >> 6) & 3) - dmin * (x[i].scales[is+6] >> 4);
|
|
||||||
#else
|
|
||||||
const int is = tid/16; // 0 or 1
|
|
||||||
const int il = tid%16; // 0...15
|
|
||||||
const uint8_t q = x[i].qs[il] >> (2*is);
|
|
||||||
dst_t * y = yy + i*QK_K + 16*is + il;
|
|
||||||
float dall = __low2half(x[i].dm);
|
|
||||||
float dmin = __high2half(x[i].dm);
|
|
||||||
y[ 0] = dall * (x[i].scales[is+0] & 0xF) * ((q >> 0) & 3) - dmin * (x[i].scales[is+0] >> 4);
|
|
||||||
y[32] = dall * (x[i].scales[is+2] & 0xF) * ((q >> 4) & 3) - dmin * (x[i].scales[is+2] >> 4);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static __global__ void dequantize_block_q3_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
|
|
||||||
|
|
||||||
const int i = blockIdx.x;
|
|
||||||
const block_q3_K * x = (const block_q3_K *) vx;
|
|
||||||
|
|
||||||
#if QK_K == 256
|
|
||||||
const int r = threadIdx.x/4;
|
|
||||||
const int tid = r/2;
|
|
||||||
const int is0 = r%2;
|
|
||||||
const int l0 = 16*is0 + 4*(threadIdx.x%4);
|
|
||||||
const int n = tid / 4;
|
|
||||||
const int j = tid - 4*n;
|
|
||||||
|
|
||||||
uint8_t m = 1 << (4*n + j);
|
|
||||||
int is = 8*n + 2*j + is0;
|
|
||||||
int shift = 2*j;
|
|
||||||
|
|
||||||
int8_t us = is < 4 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+8] >> 0) & 3) << 4) :
|
|
||||||
is < 8 ? (x[i].scales[is-0] & 0xF) | (((x[i].scales[is+4] >> 2) & 3) << 4) :
|
|
||||||
is < 12 ? (x[i].scales[is-8] >> 4) | (((x[i].scales[is+0] >> 4) & 3) << 4) :
|
|
||||||
(x[i].scales[is-8] >> 4) | (((x[i].scales[is-4] >> 6) & 3) << 4);
|
|
||||||
float d_all = x[i].d;
|
|
||||||
float dl = d_all * (us - 32);
|
|
||||||
|
|
||||||
dst_t * y = yy + i*QK_K + 128*n + 32*j;
|
|
||||||
const uint8_t * q = x[i].qs + 32*n;
|
|
||||||
const uint8_t * hm = x[i].hmask;
|
|
||||||
|
|
||||||
for (int l = l0; l < l0+4; ++l) y[l] = dl * ((int8_t)((q[l] >> shift) & 3) - ((hm[l] & m) ? 0 : 4));
|
|
||||||
#else
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
const int is = tid/16; // 0 or 1
|
|
||||||
const int il = tid%16; // 0...15
|
|
||||||
const int im = il/8; // 0...1
|
|
||||||
const int in = il%8; // 0...7
|
|
||||||
|
|
||||||
dst_t * y = yy + i*QK_K + 16*is + il;
|
|
||||||
|
|
||||||
const uint8_t q = x[i].qs[il] >> (2*is);
|
|
||||||
const uint8_t h = x[i].hmask[in] >> (2*is + im);
|
|
||||||
const float d = (float)x[i].d;
|
|
||||||
|
|
||||||
if (is == 0) {
|
|
||||||
y[ 0] = d * ((x[i].scales[0] & 0xF) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
|
|
||||||
y[32] = d * ((x[i].scales[1] & 0xF) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
|
|
||||||
} else {
|
|
||||||
y[ 0] = d * ((x[i].scales[0] >> 4) - 8) * ((int8_t)((q >> 0) & 3) - ((h >> 0) & 1 ? 0 : 4));
|
|
||||||
y[32] = d * ((x[i].scales[1] >> 4) - 8) * ((int8_t)((q >> 4) & 3) - ((h >> 4) & 1 ? 0 : 4));
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#if QK_K == 256
|
|
||||||
static inline __device__ void get_scale_min_k4(int j, const uint8_t * q, uint8_t & d, uint8_t & m) {
|
|
||||||
if (j < 4) {
|
|
||||||
d = q[j] & 63; m = q[j + 4] & 63;
|
|
||||||
} else {
|
|
||||||
d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
|
|
||||||
m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static __global__ void dequantize_block_q4_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
|
|
||||||
const block_q4_K * x = (const block_q4_K *) vx;
|
|
||||||
|
|
||||||
const int i = blockIdx.x;
|
|
||||||
|
|
||||||
#if QK_K == 256
|
|
||||||
// assume 32 threads
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
const int il = tid/8;
|
|
||||||
const int ir = tid%8;
|
|
||||||
const int is = 2*il;
|
|
||||||
const int n = 4;
|
|
||||||
|
|
||||||
dst_t * y = yy + i*QK_K + 64*il + n*ir;
|
|
||||||
|
|
||||||
const float dall = __low2half(x[i].dm);
|
|
||||||
const float dmin = __high2half(x[i].dm);
|
|
||||||
|
|
||||||
const uint8_t * q = x[i].qs + 32*il + n*ir;
|
|
||||||
|
|
||||||
uint8_t sc, m;
|
|
||||||
get_scale_min_k4(is + 0, x[i].scales, sc, m);
|
|
||||||
const float d1 = dall * sc; const float m1 = dmin * m;
|
|
||||||
get_scale_min_k4(is + 1, x[i].scales, sc, m);
|
|
||||||
const float d2 = dall * sc; const float m2 = dmin * m;
|
|
||||||
for (int l = 0; l < n; ++l) {
|
|
||||||
y[l + 0] = d1 * (q[l] & 0xF) - m1;
|
|
||||||
y[l +32] = d2 * (q[l] >> 4) - m2;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
const uint8_t * q = x[i].qs;
|
|
||||||
dst_t * y = yy + i*QK_K;
|
|
||||||
const float d = (float)x[i].dm[0];
|
|
||||||
const float m = (float)x[i].dm[1];
|
|
||||||
y[tid+ 0] = d * (x[i].scales[0] & 0xF) * (q[tid] & 0xF) - m * (x[i].scales[0] >> 4);
|
|
||||||
y[tid+32] = d * (x[i].scales[1] & 0xF) * (q[tid] >> 4) - m * (x[i].scales[1] >> 4);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static __global__ void dequantize_block_q5_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
|
|
||||||
const block_q5_K * x = (const block_q5_K *) vx;
|
|
||||||
|
|
||||||
const int i = blockIdx.x;
|
|
||||||
|
|
||||||
#if QK_K == 256
|
|
||||||
// assume 64 threads - this is very slightly better than the one below
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
const int il = tid/16; // il is in 0...3
|
|
||||||
const int ir = tid%16; // ir is in 0...15
|
|
||||||
const int is = 2*il; // is is in 0...6
|
|
||||||
|
|
||||||
dst_t * y = yy + i*QK_K + 64*il + 2*ir;
|
|
||||||
|
|
||||||
const float dall = __low2half(x[i].dm);
|
|
||||||
const float dmin = __high2half(x[i].dm);
|
|
||||||
|
|
||||||
const uint8_t * ql = x[i].qs + 32*il + 2*ir;
|
|
||||||
const uint8_t * qh = x[i].qh + 2*ir;
|
|
||||||
|
|
||||||
uint8_t sc, m;
|
|
||||||
get_scale_min_k4(is + 0, x[i].scales, sc, m);
|
|
||||||
const float d1 = dall * sc; const float m1 = dmin * m;
|
|
||||||
get_scale_min_k4(is + 1, x[i].scales, sc, m);
|
|
||||||
const float d2 = dall * sc; const float m2 = dmin * m;
|
|
||||||
|
|
||||||
uint8_t hm = 1 << (2*il);
|
|
||||||
y[ 0] = d1 * ((ql[ 0] & 0xF) + (qh[ 0] & hm ? 16 : 0)) - m1;
|
|
||||||
y[ 1] = d1 * ((ql[ 1] & 0xF) + (qh[ 1] & hm ? 16 : 0)) - m1;
|
|
||||||
hm <<= 1;
|
|
||||||
y[32] = d2 * ((ql[ 0] >> 4) + (qh[ 0] & hm ? 16 : 0)) - m2;
|
|
||||||
y[33] = d2 * ((ql[ 1] >> 4) + (qh[ 1] & hm ? 16 : 0)) - m2;
|
|
||||||
#else
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
const uint8_t q = x[i].qs[tid];
|
|
||||||
const int im = tid/8; // 0...3
|
|
||||||
const int in = tid%8; // 0...7
|
|
||||||
const int is = tid/16; // 0 or 1
|
|
||||||
const uint8_t h = x[i].qh[in] >> im;
|
|
||||||
const float d = x[i].d;
|
|
||||||
dst_t * y = yy + i*QK_K + tid;
|
|
||||||
y[ 0] = d * x[i].scales[is+0] * ((q & 0xF) - ((h >> 0) & 1 ? 0 : 16));
|
|
||||||
y[32] = d * x[i].scales[is+2] * ((q >> 4) - ((h >> 4) & 1 ? 0 : 16));
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static __global__ void dequantize_block_q6_K(const void * __restrict__ vx, dst_t * __restrict__ yy) {
|
|
||||||
const block_q6_K * x = (const block_q6_K *) vx;
|
|
||||||
|
|
||||||
const int64_t i = blockIdx.x;
|
|
||||||
#if QK_K == 256
|
|
||||||
|
|
||||||
// assume 64 threads - this is very slightly better than the one below
|
|
||||||
const int64_t tid = threadIdx.x;
|
|
||||||
const int64_t ip = tid/32; // ip is 0 or 1
|
|
||||||
const int64_t il = tid - 32*ip; // 0...32
|
|
||||||
const int64_t is = 8*ip + il/16;
|
|
||||||
|
|
||||||
dst_t * y = yy + i*QK_K + 128*ip + il;
|
|
||||||
|
|
||||||
const float d = x[i].d;
|
|
||||||
|
|
||||||
const uint8_t * ql = x[i].ql + 64*ip + il;
|
|
||||||
const uint8_t qh = x[i].qh[32*ip + il];
|
|
||||||
const int8_t * sc = x[i].scales + is;
|
|
||||||
|
|
||||||
y[ 0] = d * sc[0] * ((int8_t)((ql[ 0] & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
|
|
||||||
y[32] = d * sc[2] * ((int8_t)((ql[32] & 0xF) | (((qh >> 2) & 3) << 4)) - 32);
|
|
||||||
y[64] = d * sc[4] * ((int8_t)((ql[ 0] >> 4) | (((qh >> 4) & 3) << 4)) - 32);
|
|
||||||
y[96] = d * sc[6] * ((int8_t)((ql[32] >> 4) | (((qh >> 6) & 3) << 4)) - 32);
|
|
||||||
#else
|
|
||||||
|
|
||||||
// assume 32 threads
|
|
||||||
const int64_t tid = threadIdx.x;
|
|
||||||
const int64_t ip = tid/16; // 0 or 1
|
|
||||||
const int64_t il = tid - 16*ip; // 0...15
|
|
||||||
|
|
||||||
dst_t * y = yy + i*QK_K + 16*ip + il;
|
|
||||||
|
|
||||||
const float d = x[i].d;
|
|
||||||
|
|
||||||
const uint8_t ql = x[i].ql[16*ip + il];
|
|
||||||
const uint8_t qh = x[i].qh[il] >> (2*ip);
|
|
||||||
const int8_t * sc = x[i].scales;
|
|
||||||
|
|
||||||
y[ 0] = d * sc[ip+0] * ((int8_t)((ql & 0xF) | (((qh >> 0) & 3) << 4)) - 32);
|
|
||||||
y[32] = d * sc[ip+2] * ((int8_t)((ql >> 4) | (((qh >> 4) & 3) << 4)) - 32);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static __global__ void dequantize_block_iq2_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
|
|
||||||
|
|
||||||
const int i = blockIdx.x;
|
|
||||||
const block_iq2_xxs * x = (const block_iq2_xxs *) vx;
|
|
||||||
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
#if QK_K == 256
|
|
||||||
const int il = tid/8; // 0...3
|
|
||||||
const int ib = tid%8; // 0...7
|
|
||||||
dst_t * y = yy + i*QK_K + 32*ib + 8*il;
|
|
||||||
const uint16_t * q2 = x[i].qs + 4*ib;
|
|
||||||
const uint8_t * aux8 = (const uint8_t *)q2;
|
|
||||||
const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[il]);
|
|
||||||
const uint32_t aux32 = q2[2] | (q2[3] << 16);
|
|
||||||
const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.25f;
|
|
||||||
const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
|
|
||||||
for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
|
|
||||||
#else
|
|
||||||
NO_DEVICE_CODE;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static __global__ void dequantize_block_iq2_xs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
|
|
||||||
|
|
||||||
const int i = blockIdx.x;
|
|
||||||
const block_iq2_xs * x = (const block_iq2_xs *) vx;
|
|
||||||
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
#if QK_K == 256
|
|
||||||
const int il = tid/8; // 0...3
|
|
||||||
const int ib = tid%8; // 0...7
|
|
||||||
dst_t * y = yy + i*QK_K + 32*ib + 8*il;
|
|
||||||
const uint16_t * q2 = x[i].qs + 4*ib;
|
|
||||||
const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[il] & 511));
|
|
||||||
const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
|
|
||||||
const uint8_t signs = ksigns_iq2xs[q2[il] >> 9];
|
|
||||||
for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
|
|
||||||
#else
|
|
||||||
NO_DEVICE_CODE;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static __global__ void dequantize_block_iq2_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
|
|
||||||
|
|
||||||
const int i = blockIdx.x;
|
|
||||||
const block_iq2_s * x = (const block_iq2_s *) vx;
|
|
||||||
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
#if QK_K == 256
|
|
||||||
const int il = tid/8; // 0...3
|
|
||||||
const int ib = tid%8; // 0...7
|
|
||||||
dst_t * y = yy + i*QK_K + 32*ib + 8*il;
|
|
||||||
const uint8_t * grid = (const uint8_t *)(iq2s_grid + (x[i].qs[4*ib+il] | ((x[i].qh[ib] << (8-2*il)) & 0x300)));
|
|
||||||
const float d = (float)x[i].d * (0.5f + ((x[i].scales[ib] >> 4*(il/2)) & 0xf)) * 0.25f;
|
|
||||||
const uint8_t signs = x[i].qs[QK_K/8+4*ib+il];
|
|
||||||
for (int j = 0; j < 8; ++j) y[j] = d * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
|
|
||||||
#else
|
|
||||||
NO_DEVICE_CODE;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static __global__ void dequantize_block_iq3_xxs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
|
|
||||||
|
|
||||||
const int i = blockIdx.x;
|
|
||||||
const block_iq3_xxs * x = (const block_iq3_xxs *) vx;
|
|
||||||
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
#if QK_K == 256
|
|
||||||
const int il = tid/8; // 0...3
|
|
||||||
const int ib = tid%8; // 0...7
|
|
||||||
dst_t * y = yy + i*QK_K + 32*ib + 8*il;
|
|
||||||
const uint8_t * q3 = x[i].qs + 8*ib;
|
|
||||||
const uint16_t * gas = (const uint16_t *)(x[i].qs + QK_K/4) + 2*ib;
|
|
||||||
const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*il+0]);
|
|
||||||
const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*il+1]);
|
|
||||||
const uint32_t aux32 = gas[0] | (gas[1] << 16);
|
|
||||||
const float d = (float)x[i].d * (0.5f + (aux32 >> 28)) * 0.5f;
|
|
||||||
const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*il) & 127];
|
|
||||||
for (int j = 0; j < 4; ++j) {
|
|
||||||
y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
|
|
||||||
y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
NO_DEVICE_CODE;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static __global__ void dequantize_block_iq3_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
|
|
||||||
|
|
||||||
const int i = blockIdx.x;
|
|
||||||
const block_iq3_s * x = (const block_iq3_s *) vx;
|
|
||||||
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
#if QK_K == 256
|
|
||||||
const int il = tid/8; // 0...3
|
|
||||||
const int ib = tid%8; // 0...7
|
|
||||||
dst_t * y = yy + i*QK_K + 32*ib + 8*il;
|
|
||||||
const uint8_t * qs = x[i].qs + 8*ib;
|
|
||||||
const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*il+0] | ((x[i].qh[ib] << (8-2*il)) & 256)));
|
|
||||||
const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*il+1] | ((x[i].qh[ib] << (7-2*il)) & 256)));
|
|
||||||
const float d = (float)x[i].d * (1 + 2*((x[i].scales[ib/2] >> 4*(ib%2)) & 0xf));
|
|
||||||
const uint8_t signs = x[i].signs[4*ib + il];
|
|
||||||
for (int j = 0; j < 4; ++j) {
|
|
||||||
y[j+0] = d * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
|
|
||||||
y[j+4] = d * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
NO_DEVICE_CODE;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static __global__ void dequantize_block_iq1_s(const void * __restrict__ vx, dst_t * __restrict__ yy) {
|
|
||||||
|
|
||||||
const int i = blockIdx.x;
|
|
||||||
const block_iq1_s * x = (const block_iq1_s *) vx;
|
|
||||||
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
#if QK_K == 256
|
|
||||||
const int il = tid/8; // 0...3
|
|
||||||
const int ib = tid%8; // 0...7
|
|
||||||
dst_t * y = yy + i*QK_K + 32*ib + 8*il;
|
|
||||||
const float delta = x[i].qh[ib] & 0x8000 ? -1 - IQ1S_DELTA : -1 + IQ1S_DELTA;
|
|
||||||
const float d = (float)x[i].d * (2*((x[i].qh[ib] >> 12) & 7) + 1);
|
|
||||||
uint32_t grid32[2]; const int8_t * q = (const int8_t *)grid32;
|
|
||||||
grid32[0] = iq1s_grid_gpu[x[i].qs[4*ib+il] | (((x[i].qh[ib] >> 3*il) & 7) << 8)];
|
|
||||||
grid32[1] = (grid32[0] >> 4) & 0x0f0f0f0f;
|
|
||||||
grid32[0] &= 0x0f0f0f0f;
|
|
||||||
for (int j = 0; j < 8; ++j) {
|
|
||||||
y[j] = d * (q[j] + delta);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
NO_DEVICE_CODE;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static __global__ void dequantize_block_iq1_m(const void * __restrict__ vx, dst_t * __restrict__ yy) {
|
|
||||||
|
|
||||||
const int i = blockIdx.x;
|
|
||||||
const block_iq1_m * x = (const block_iq1_m *) vx;
|
|
||||||
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
#if QK_K == 256
|
|
||||||
const int il = tid/8; // 0...3
|
|
||||||
const int ib = tid%8; // 0...7
|
|
||||||
dst_t * y = yy + i*QK_K + 32*ib + 8*il;
|
|
||||||
const uint16_t * sc = (const uint16_t *)x[i].scales;
|
|
||||||
iq1m_scale_t scale;
|
|
||||||
scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
|
|
||||||
const int ib16 = 2*ib + il/2; // sc[ib16/4] >> 3*(ib16%4) -> sc[ib/2] >> 3*((2*ib+il/2)%4);
|
|
||||||
const float d = (float)scale.f16 * (2*((sc[ib16/4] >> 3*(ib16%4)) & 0x7) + 1);
|
|
||||||
const float delta = x[i].qh[2*ib+il/2] & (0x08 << 4*(il%2)) ? -1 - IQ1M_DELTA : -1 + IQ1M_DELTA;
|
|
||||||
uint32_t grid32[2]; const int8_t * q = (const int8_t *)grid32;
|
|
||||||
grid32[0] = iq1s_grid_gpu[x[i].qs[4*ib+il] | (((x[i].qh[2*ib+il/2] >> 4*(il%2)) & 7) << 8)];
|
|
||||||
grid32[1] = (grid32[0] >> 4) & 0x0f0f0f0f;
|
|
||||||
grid32[0] &= 0x0f0f0f0f;
|
|
||||||
for (int j = 0; j < 8; ++j) {
|
|
||||||
y[j] = d * (q[j] + delta);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
NO_DEVICE_CODE;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static __global__ void dequantize_block_iq4_nl(const void * __restrict__ vx, dst_t * __restrict__ yy) {
|
|
||||||
|
|
||||||
const int i = blockIdx.x;
|
|
||||||
const block_iq4_nl * x = (const block_iq4_nl *) vx + i*(QK_K/QK4_NL);
|
|
||||||
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
const int il = tid/8; // 0...3
|
|
||||||
const int ib = tid%8; // 0...7
|
|
||||||
dst_t * y = yy + i*QK_K + 32*ib + 4*il;
|
|
||||||
const uint8_t * q4 = x[ib].qs + 4*il;
|
|
||||||
const float d = (float)x[ib].d;
|
|
||||||
for (int j = 0; j < 4; ++j) {
|
|
||||||
y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
|
|
||||||
y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#if QK_K != 64
|
|
||||||
template<typename dst_t>
|
|
||||||
static __global__ void dequantize_block_iq4_xs(const void * __restrict__ vx, dst_t * __restrict__ yy) {
|
|
||||||
const int i = blockIdx.x;
|
|
||||||
const block_iq4_xs * x = (const block_iq4_xs *)vx;
|
|
||||||
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
const int il = tid/8; // 0...3
|
|
||||||
const int ib = tid%8; // 0...7
|
|
||||||
dst_t * y = yy + i*QK_K + 32*ib + 4*il;
|
|
||||||
const uint8_t * q4 = x[i].qs + 16*ib + 4*il;
|
|
||||||
const float d = (float)x[i].d * ((((x[i].scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((x[i].scales_h >> 2*ib) & 3) << 4)) - 32);
|
|
||||||
for (int j = 0; j < 4; ++j) {
|
|
||||||
y[j+ 0] = d * kvalues_iq4nl[q4[j] & 0xf];
|
|
||||||
y[j+16] = d * kvalues_iq4nl[q4[j] >> 4];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
template <int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
|
|
||||||
static void dequantize_block_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int num_blocks = (k + 2*CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / (2*CUDA_DEQUANTIZE_BLOCK_SIZE);
|
|
||||||
dequantize_block<qk, qr, dequantize_kernel><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void dequantize_block_q8_0_f16_cuda(const void * __restrict__ vx, half * __restrict__ y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int num_blocks = (k + CUDA_Q8_0_NE_ALIGN - 1) / CUDA_Q8_0_NE_ALIGN;
|
|
||||||
if (k % CUDA_Q8_0_NE_ALIGN == 0) {
|
|
||||||
const bool need_check = false;
|
|
||||||
dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
|
|
||||||
} else {
|
|
||||||
const bool need_check = true;
|
|
||||||
dequantize_block_q8_0_f16<need_check><<<num_blocks, WARP_SIZE, 0, stream>>>(vx, y, k);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static void dequantize_row_q2_K_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int nb = k / QK_K;
|
|
||||||
#if QK_K == 256
|
|
||||||
dequantize_block_q2_K<<<nb, 64, 0, stream>>>(vx, y);
|
|
||||||
#else
|
|
||||||
dequantize_block_q2_K<<<nb, 32, 0, stream>>>(vx, y);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static void dequantize_row_q3_K_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int nb = k / QK_K;
|
|
||||||
#if QK_K == 256
|
|
||||||
dequantize_block_q3_K<<<nb, 64, 0, stream>>>(vx, y);
|
|
||||||
#else
|
|
||||||
dequantize_block_q3_K<<<nb, 32, 0, stream>>>(vx, y);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static void dequantize_row_q4_0_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int nb32 = k / 32;
|
|
||||||
const int nb = (k + 255) / 256;
|
|
||||||
dequantize_block_q4_0<<<nb, 32, 0, stream>>>(vx, y, nb32);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static void dequantize_row_q4_1_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int nb32 = k / 32;
|
|
||||||
const int nb = (k + 255) / 256;
|
|
||||||
dequantize_block_q4_1<<<nb, 32, 0, stream>>>(vx, y, nb32);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static void dequantize_row_q4_K_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int nb = k / QK_K;
|
|
||||||
dequantize_block_q4_K<<<nb, 32, 0, stream>>>(vx, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static void dequantize_row_q5_K_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int nb = k / QK_K;
|
|
||||||
#if QK_K == 256
|
|
||||||
dequantize_block_q5_K<<<nb, 64, 0, stream>>>(vx, y);
|
|
||||||
#else
|
|
||||||
dequantize_block_q5_K<<<nb, 32, 0, stream>>>(vx, y);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static void dequantize_row_q6_K_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int nb = k / QK_K;
|
|
||||||
#if QK_K == 256
|
|
||||||
dequantize_block_q6_K<<<nb, 64, 0, stream>>>(vx, y);
|
|
||||||
#else
|
|
||||||
dequantize_block_q6_K<<<nb, 32, 0, stream>>>(vx, y);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static void dequantize_row_iq2_xxs_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int nb = k / QK_K;
|
|
||||||
dequantize_block_iq2_xxs<<<nb, 32, 0, stream>>>(vx, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static void dequantize_row_iq2_xs_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int nb = k / QK_K;
|
|
||||||
dequantize_block_iq2_xs<<<nb, 32, 0, stream>>>(vx, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static void dequantize_row_iq2_s_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int nb = k / QK_K;
|
|
||||||
dequantize_block_iq2_s<<<nb, 32, 0, stream>>>(vx, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static void dequantize_row_iq3_xxs_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int nb = k / QK_K;
|
|
||||||
dequantize_block_iq3_xxs<<<nb, 32, 0, stream>>>(vx, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static void dequantize_row_iq3_s_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int nb = k / QK_K;
|
|
||||||
dequantize_block_iq3_s<<<nb, 32, 0, stream>>>(vx, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static void dequantize_row_iq1_s_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int nb = k / QK_K;
|
|
||||||
dequantize_block_iq1_s<<<nb, 32, 0, stream>>>(vx, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static void dequantize_row_iq4_nl_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int nb = (k + QK_K - 1) / QK_K;
|
|
||||||
dequantize_block_iq4_nl<<<nb, 32, 0, stream>>>(vx, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static void dequantize_row_iq1_m_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int nb = k / QK_K;
|
|
||||||
dequantize_block_iq1_m<<<nb, 32, 0, stream>>>(vx, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename dst_t>
|
|
||||||
static void dequantize_row_iq4_xs_cuda(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int nb = (k + QK_K - 1) / QK_K;
|
|
||||||
#if QK_K == 64
|
|
||||||
dequantize_block_iq4_nl<<<nb, 32, 0, stream>>>(vx, y);
|
|
||||||
#else
|
|
||||||
dequantize_block_iq4_xs<<<nb, 32, 0, stream>>>(vx, y);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename src_t, typename dst_t>
|
|
||||||
static __global__ void convert_unary(const void * __restrict__ vx, dst_t * __restrict__ y, const int64_t k) {
|
|
||||||
const int64_t i = (int64_t)blockDim.x*blockIdx.x + threadIdx.x;
|
|
||||||
|
|
||||||
if (i >= k) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const src_t * x = (src_t *) vx;
|
|
||||||
|
|
||||||
y[i] = x[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename src_t, typename dst_t>
|
|
||||||
static void convert_unary_cuda(const void * __restrict__ vx, dst_t * __restrict__ y, const int64_t k, cudaStream_t stream) {
|
|
||||||
const int num_blocks = (k + CUDA_DEQUANTIZE_BLOCK_SIZE - 1) / CUDA_DEQUANTIZE_BLOCK_SIZE;
|
|
||||||
convert_unary<src_t><<<num_blocks, CUDA_DEQUANTIZE_BLOCK_SIZE, 0, stream>>>(vx, y, k);
|
|
||||||
}
|
|
||||||
|
|
||||||
to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
|
|
||||||
int id;
|
|
||||||
switch (type) {
|
|
||||||
case GGML_TYPE_Q4_0:
|
|
||||||
return dequantize_row_q4_0_cuda;
|
|
||||||
case GGML_TYPE_Q4_1:
|
|
||||||
return dequantize_row_q4_1_cuda;
|
|
||||||
case GGML_TYPE_Q5_0:
|
|
||||||
return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
|
|
||||||
case GGML_TYPE_Q5_1:
|
|
||||||
return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
|
|
||||||
case GGML_TYPE_Q8_0:
|
|
||||||
CUDA_CHECK(cudaGetDevice(&id));
|
|
||||||
if (ggml_cuda_info().devices[id].cc >= CC_PASCAL) {
|
|
||||||
return dequantize_block_q8_0_f16_cuda;
|
|
||||||
}
|
|
||||||
return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
|
|
||||||
case GGML_TYPE_Q2_K:
|
|
||||||
return dequantize_row_q2_K_cuda;
|
|
||||||
case GGML_TYPE_Q3_K:
|
|
||||||
return dequantize_row_q3_K_cuda;
|
|
||||||
case GGML_TYPE_Q4_K:
|
|
||||||
return dequantize_row_q4_K_cuda;
|
|
||||||
case GGML_TYPE_Q5_K:
|
|
||||||
return dequantize_row_q5_K_cuda;
|
|
||||||
case GGML_TYPE_Q6_K:
|
|
||||||
return dequantize_row_q6_K_cuda;
|
|
||||||
case GGML_TYPE_IQ2_XXS:
|
|
||||||
return dequantize_row_iq2_xxs_cuda;
|
|
||||||
case GGML_TYPE_IQ2_XS:
|
|
||||||
return dequantize_row_iq2_xs_cuda;
|
|
||||||
case GGML_TYPE_IQ2_S:
|
|
||||||
return dequantize_row_iq2_s_cuda;
|
|
||||||
case GGML_TYPE_IQ3_XXS:
|
|
||||||
return dequantize_row_iq3_xxs_cuda;
|
|
||||||
case GGML_TYPE_IQ1_S:
|
|
||||||
return dequantize_row_iq1_s_cuda;
|
|
||||||
case GGML_TYPE_IQ1_M:
|
|
||||||
return dequantize_row_iq1_m_cuda;
|
|
||||||
case GGML_TYPE_IQ4_NL:
|
|
||||||
return dequantize_row_iq4_nl_cuda;
|
|
||||||
case GGML_TYPE_IQ4_XS:
|
|
||||||
return dequantize_row_iq4_xs_cuda;
|
|
||||||
case GGML_TYPE_IQ3_S:
|
|
||||||
return dequantize_row_iq3_s_cuda;
|
|
||||||
case GGML_TYPE_F32:
|
|
||||||
return convert_unary_cuda<float>;
|
|
||||||
default:
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
|
|
||||||
switch (type) {
|
|
||||||
case GGML_TYPE_Q4_0:
|
|
||||||
return dequantize_row_q4_0_cuda;
|
|
||||||
case GGML_TYPE_Q4_1:
|
|
||||||
return dequantize_row_q4_1_cuda;
|
|
||||||
case GGML_TYPE_Q5_0:
|
|
||||||
return dequantize_block_cuda<QK5_0, QR5_0, dequantize_q5_0>;
|
|
||||||
case GGML_TYPE_Q5_1:
|
|
||||||
return dequantize_block_cuda<QK5_1, QR5_1, dequantize_q5_1>;
|
|
||||||
case GGML_TYPE_Q8_0:
|
|
||||||
return dequantize_block_cuda<QK8_0, QR8_0, dequantize_q8_0>;
|
|
||||||
case GGML_TYPE_Q2_K:
|
|
||||||
return dequantize_row_q2_K_cuda;
|
|
||||||
case GGML_TYPE_Q3_K:
|
|
||||||
return dequantize_row_q3_K_cuda;
|
|
||||||
case GGML_TYPE_Q4_K:
|
|
||||||
return dequantize_row_q4_K_cuda;
|
|
||||||
case GGML_TYPE_Q5_K:
|
|
||||||
return dequantize_row_q5_K_cuda;
|
|
||||||
case GGML_TYPE_Q6_K:
|
|
||||||
return dequantize_row_q6_K_cuda;
|
|
||||||
case GGML_TYPE_IQ2_XXS:
|
|
||||||
return dequantize_row_iq2_xxs_cuda;
|
|
||||||
case GGML_TYPE_IQ2_XS:
|
|
||||||
return dequantize_row_iq2_xs_cuda;
|
|
||||||
case GGML_TYPE_IQ2_S:
|
|
||||||
return dequantize_row_iq2_s_cuda;
|
|
||||||
case GGML_TYPE_IQ3_XXS:
|
|
||||||
return dequantize_row_iq3_xxs_cuda;
|
|
||||||
case GGML_TYPE_IQ1_S:
|
|
||||||
return dequantize_row_iq1_s_cuda;
|
|
||||||
case GGML_TYPE_IQ1_M:
|
|
||||||
return dequantize_row_iq1_m_cuda;
|
|
||||||
case GGML_TYPE_IQ4_NL:
|
|
||||||
return dequantize_row_iq4_nl_cuda;
|
|
||||||
case GGML_TYPE_IQ4_XS:
|
|
||||||
return dequantize_row_iq4_xs_cuda;
|
|
||||||
case GGML_TYPE_IQ3_S:
|
|
||||||
return dequantize_row_iq3_s_cuda;
|
|
||||||
case GGML_TYPE_F16:
|
|
||||||
return convert_unary_cuda<half>;
|
|
||||||
default:
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
#define CUDA_DEQUANTIZE_BLOCK_SIZE 256
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
using to_t_cuda_t = void (*)(const void * __restrict__ x, T * __restrict__ y, int64_t k, cudaStream_t stream);
|
|
||||||
|
|
||||||
typedef to_t_cuda_t<float> to_fp32_cuda_t;
|
|
||||||
typedef to_t_cuda_t<half> to_fp16_cuda_t;
|
|
||||||
|
|
||||||
to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type);
|
|
||||||
|
|
||||||
to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type);
|
|
461
ggml-cuda/cpy.cu
461
ggml-cuda/cpy.cu
@ -1,461 +0,0 @@
|
|||||||
#include "cpy.cuh"
|
|
||||||
|
|
||||||
typedef void (*cpy_kernel_t)(const char * cx, char * cdst);
|
|
||||||
|
|
||||||
static __device__ void cpy_1_f32_f32(const char * cxi, char * cdsti) {
|
|
||||||
const float * xi = (const float *) cxi;
|
|
||||||
float * dsti = (float *) cdsti;
|
|
||||||
|
|
||||||
*dsti = *xi;
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ void cpy_1_f32_f16(const char * cxi, char * cdsti) {
|
|
||||||
const float * xi = (const float *) cxi;
|
|
||||||
half * dsti = (half *) cdsti;
|
|
||||||
|
|
||||||
*dsti = __float2half(*xi);
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ void cpy_1_f16_f16(const char * cxi, char * cdsti) {
|
|
||||||
const half * xi = (const half *) cxi;
|
|
||||||
half * dsti = (half *) cdsti;
|
|
||||||
|
|
||||||
*dsti = *xi;
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ void cpy_1_f16_f32(const char * cxi, char * cdsti) {
|
|
||||||
const half * xi = (const half *) cxi;
|
|
||||||
float * dsti = (float *) cdsti;
|
|
||||||
|
|
||||||
*dsti = *xi;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <cpy_kernel_t cpy_1>
|
|
||||||
static __global__ void cpy_f32_f16(const char * cx, char * cdst, const int ne,
|
|
||||||
const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
|
|
||||||
const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
|
|
||||||
const int nb12, const int nb13) {
|
|
||||||
const int64_t i = blockDim.x*blockIdx.x + threadIdx.x;
|
|
||||||
|
|
||||||
if (i >= ne) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// determine indices i03/i13, i02/i12, i01/i11, i00/i10 as a function of index i of flattened tensor
|
|
||||||
// then combine those indices with the corresponding byte offsets to get the total offsets
|
|
||||||
const int64_t i03 = i/(ne00 * ne01 * ne02);
|
|
||||||
const int64_t i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
|
|
||||||
const int64_t i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
|
|
||||||
const int64_t i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
|
|
||||||
const int64_t x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
|
|
||||||
|
|
||||||
const int64_t i13 = i/(ne10 * ne11 * ne12);
|
|
||||||
const int64_t i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
|
|
||||||
const int64_t i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
|
|
||||||
const int64_t i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
|
|
||||||
const int64_t dst_offset = i10*nb10 + i11*nb11 + i12*nb12 + i13 * nb13;
|
|
||||||
|
|
||||||
cpy_1(cx + x_offset, cdst + dst_offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ void cpy_blck_f32_q8_0(const char * cxi, char * cdsti) {
|
|
||||||
const float * xi = (const float *) cxi;
|
|
||||||
block_q8_0 * dsti = (block_q8_0 *) cdsti;
|
|
||||||
|
|
||||||
float amax = 0.0f; // absolute max
|
|
||||||
|
|
||||||
for (int j = 0; j < QK8_0; j++) {
|
|
||||||
const float v = xi[j];
|
|
||||||
amax = fmaxf(amax, fabsf(v));
|
|
||||||
}
|
|
||||||
|
|
||||||
const float d = amax / ((1 << 7) - 1);
|
|
||||||
const float id = d ? 1.0f/d : 0.0f;
|
|
||||||
|
|
||||||
dsti->d = d;
|
|
||||||
|
|
||||||
for (int j = 0; j < QK8_0; ++j) {
|
|
||||||
const float x0 = xi[j]*id;
|
|
||||||
|
|
||||||
dsti->qs[j] = roundf(x0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ void cpy_blck_f32_q4_0(const char * cxi, char * cdsti) {
|
|
||||||
const float * xi = (const float *) cxi;
|
|
||||||
block_q4_0 * dsti = (block_q4_0 *) cdsti;
|
|
||||||
|
|
||||||
float amax = 0.0f;
|
|
||||||
float vmax = 0.0f;
|
|
||||||
|
|
||||||
for (int j = 0; j < QK4_0; ++j) {
|
|
||||||
const float v = xi[j];
|
|
||||||
if (amax < fabsf(v)) {
|
|
||||||
amax = fabsf(v);
|
|
||||||
vmax = v;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const float d = vmax / -8;
|
|
||||||
const float id = d ? 1.0f/d : 0.0f;
|
|
||||||
|
|
||||||
dsti->d = d;
|
|
||||||
|
|
||||||
for (int j = 0; j < QK4_0/2; ++j) {
|
|
||||||
const float x0 = xi[0 + j]*id;
|
|
||||||
const float x1 = xi[QK4_0/2 + j]*id;
|
|
||||||
|
|
||||||
const uint8_t xi0 = min(15, (int8_t)(x0 + 8.5f));
|
|
||||||
const uint8_t xi1 = min(15, (int8_t)(x1 + 8.5f));
|
|
||||||
|
|
||||||
dsti->qs[j] = xi0;
|
|
||||||
dsti->qs[j] |= xi1 << 4;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ void cpy_blck_f32_q4_1(const char * cxi, char * cdsti) {
|
|
||||||
const float * xi = (const float *) cxi;
|
|
||||||
block_q4_1 * dsti = (block_q4_1 *) cdsti;
|
|
||||||
|
|
||||||
float vmin = FLT_MAX;
|
|
||||||
float vmax = -FLT_MAX;
|
|
||||||
|
|
||||||
for (int j = 0; j < QK4_1; ++j) {
|
|
||||||
const float v = xi[j];
|
|
||||||
|
|
||||||
if (v < vmin) vmin = v;
|
|
||||||
if (v > vmax) vmax = v;
|
|
||||||
}
|
|
||||||
|
|
||||||
const float d = (vmax - vmin) / ((1 << 4) - 1);
|
|
||||||
const float id = d ? 1.0f/d : 0.0f;
|
|
||||||
|
|
||||||
dsti->dm.x = d;
|
|
||||||
dsti->dm.y = vmin;
|
|
||||||
|
|
||||||
for (int j = 0; j < QK4_1/2; ++j) {
|
|
||||||
const float x0 = (xi[0 + j] - vmin)*id;
|
|
||||||
const float x1 = (xi[QK4_1/2 + j] - vmin)*id;
|
|
||||||
|
|
||||||
const uint8_t xi0 = min(15, (int8_t)(x0 + 0.5f));
|
|
||||||
const uint8_t xi1 = min(15, (int8_t)(x1 + 0.5f));
|
|
||||||
|
|
||||||
dsti->qs[j] = xi0;
|
|
||||||
dsti->qs[j] |= xi1 << 4;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ void cpy_blck_f32_q5_0(const char * cxi, char * cdsti) {
|
|
||||||
const float * xi = (const float *) cxi;
|
|
||||||
block_q5_0 * dsti = (block_q5_0 *) cdsti;
|
|
||||||
|
|
||||||
float amax = 0.0f;
|
|
||||||
float vmax = 0.0f;
|
|
||||||
|
|
||||||
for (int j = 0; j < QK5_0; ++j) {
|
|
||||||
const float v = xi[j];
|
|
||||||
if (amax < fabsf(v)) {
|
|
||||||
amax = fabsf(v);
|
|
||||||
vmax = v;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const float d = vmax / -16;
|
|
||||||
const float id = d ? 1.0f/d : 0.0f;
|
|
||||||
|
|
||||||
dsti->d = d;
|
|
||||||
|
|
||||||
uint32_t qh = 0;
|
|
||||||
for (int j = 0; j < QK5_0/2; ++j) {
|
|
||||||
const float x0 = xi[0 + j]*id;
|
|
||||||
const float x1 = xi[QK5_0/2 + j]*id;
|
|
||||||
|
|
||||||
const uint8_t xi0 = min(31, (int8_t)(x0 + 16.5f));
|
|
||||||
const uint8_t xi1 = min(31, (int8_t)(x1 + 16.5f));
|
|
||||||
|
|
||||||
dsti->qs[j] = (xi0 & 0xf) | ((xi1 & 0xf) << 4);
|
|
||||||
qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
|
|
||||||
qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
|
|
||||||
}
|
|
||||||
memcpy(dsti->qh, &qh, sizeof(qh));
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ void cpy_blck_f32_q5_1(const char * cxi, char * cdsti) {
|
|
||||||
const float * xi = (const float *) cxi;
|
|
||||||
block_q5_1 * dsti = (block_q5_1 *) cdsti;
|
|
||||||
|
|
||||||
float min = xi[0];
|
|
||||||
float max = xi[0];
|
|
||||||
|
|
||||||
for (int j = 1; j < QK5_1; ++j) {
|
|
||||||
const float v = xi[j];
|
|
||||||
min = v < min ? v : min;
|
|
||||||
max = v > max ? v : max;
|
|
||||||
}
|
|
||||||
|
|
||||||
const float d = (max - min) / 31;
|
|
||||||
const float id = d ? 1.0f/d : 0.0f;
|
|
||||||
|
|
||||||
dsti->dm.x = d;
|
|
||||||
dsti->dm.y = min;
|
|
||||||
|
|
||||||
uint32_t qh = 0;
|
|
||||||
for (int j = 0; j < QK5_1/2; ++j) {
|
|
||||||
const float x0 = (xi[0 + j] - min)*id;
|
|
||||||
const float x1 = (xi[QK5_1/2 + j] - min)*id;
|
|
||||||
|
|
||||||
const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
|
|
||||||
const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
|
|
||||||
|
|
||||||
dsti->qs[j] = (xi0 & 0xf) | ((xi1 & 0xf) << 4);
|
|
||||||
qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
|
|
||||||
qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_1/2);
|
|
||||||
}
|
|
||||||
memcpy(dsti->qh, &qh, sizeof(qh));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static __device__ __forceinline__ int best_index_int8(int n, const int8_t * val, float x) {
|
|
||||||
if (x <= val[0]) return 0;
|
|
||||||
if (x >= val[n-1]) return n-1;
|
|
||||||
int ml = 0, mu = n-1;
|
|
||||||
while (mu-ml > 1) {
|
|
||||||
int mav = (ml+mu)/2;
|
|
||||||
if (x < val[mav]) mu = mav; else ml = mav;
|
|
||||||
}
|
|
||||||
return x - val[mu-1] < val[mu] - x ? mu-1 : mu;
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ void cpy_blck_f32_iq4_nl(const char * cxi, char * cdsti) {
|
|
||||||
const float * xi = (const float *) cxi;
|
|
||||||
block_iq4_nl * dsti = (block_iq4_nl *) cdsti;
|
|
||||||
|
|
||||||
float amax = 0.0f;
|
|
||||||
float vmax = 0.0f;
|
|
||||||
|
|
||||||
for (int j = 0; j < QK4_NL; ++j) {
|
|
||||||
const float v = xi[j];
|
|
||||||
if (amax < fabsf(v)) {
|
|
||||||
amax = fabsf(v);
|
|
||||||
vmax = v;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
float d = vmax / kvalues_iq4nl[0];
|
|
||||||
const float id = d ? 1.0f/d : 0.0f;
|
|
||||||
|
|
||||||
float sumqx = 0, sumq2 = 0;
|
|
||||||
for (int j = 0; j < QK4_NL/2; ++j) {
|
|
||||||
const float x0 = xi[0 + j]*id;
|
|
||||||
const float x1 = xi[QK4_NL/2 + j]*id;
|
|
||||||
const uint8_t xi0 = best_index_int8(16, kvalues_iq4nl, x0);
|
|
||||||
const uint8_t xi1 = best_index_int8(16, kvalues_iq4nl, x1);
|
|
||||||
dsti->qs[j] = xi0 | (xi1 << 4);
|
|
||||||
const float v0 = kvalues_iq4nl[xi0];
|
|
||||||
const float v1 = kvalues_iq4nl[xi1];
|
|
||||||
const float w0 = xi[0 + j]*xi[0 + j];
|
|
||||||
const float w1 = xi[QK4_NL/2 + j]*xi[QK4_NL/2 + j];
|
|
||||||
sumqx += w0*v0*xi[j] + w1*v1*xi[QK4_NL/2 + j];
|
|
||||||
sumq2 += w0*v0*v0 + w1*v1*v1;
|
|
||||||
}
|
|
||||||
|
|
||||||
dsti->d = sumq2 > 0 ? sumqx/sumq2 : d;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <cpy_kernel_t cpy_blck, int qk>
|
|
||||||
static __global__ void cpy_f32_q(const char * cx, char * cdst, const int ne,
|
|
||||||
const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
|
|
||||||
const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11,
|
|
||||||
const int nb12, const int nb13) {
|
|
||||||
const int i = (blockDim.x*blockIdx.x + threadIdx.x)*qk;
|
|
||||||
|
|
||||||
if (i >= ne) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int i03 = i/(ne00 * ne01 * ne02);
|
|
||||||
const int i02 = (i - i03*ne00*ne01*ne02 )/ (ne00*ne01);
|
|
||||||
const int i01 = (i - i03*ne00*ne01*ne02 - i02*ne01*ne00) / ne00;
|
|
||||||
const int i00 = i - i03*ne00*ne01*ne02 - i02*ne01*ne00 - i01*ne00;
|
|
||||||
const int x_offset = i00*nb00 + i01*nb01 + i02*nb02 + i03 * nb03;
|
|
||||||
|
|
||||||
const int i13 = i/(ne10 * ne11 * ne12);
|
|
||||||
const int i12 = (i - i13*ne10*ne11*ne12) / (ne10*ne11);
|
|
||||||
const int i11 = (i - i13*ne10*ne11*ne12 - i12*ne10*ne11) / ne10;
|
|
||||||
const int i10 = i - i13*ne10*ne11*ne12 - i12*ne10*ne11 - i11*ne10;
|
|
||||||
const int dst_offset = (i10/qk)*nb10 + i11*nb11 + i12*nb12 + i13*nb13;
|
|
||||||
|
|
||||||
cpy_blck(cx + x_offset, cdst + dst_offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ggml_cpy_f16_f32_cuda(
|
|
||||||
const char * cx, char * cdst, const int ne,
|
|
||||||
const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
|
|
||||||
const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
|
|
||||||
|
|
||||||
const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
|
|
||||||
cpy_f32_f16<cpy_1_f16_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
|
|
||||||
(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ggml_cpy_f32_f32_cuda(
|
|
||||||
const char * cx, char * cdst, const int ne,
|
|
||||||
const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
|
|
||||||
const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
|
|
||||||
|
|
||||||
const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
|
|
||||||
cpy_f32_f16<cpy_1_f32_f32><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
|
|
||||||
(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ggml_cpy_f32_f16_cuda(
|
|
||||||
const char * cx, char * cdst, const int ne,
|
|
||||||
const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
|
|
||||||
const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
|
|
||||||
|
|
||||||
const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
|
|
||||||
cpy_f32_f16<cpy_1_f32_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
|
|
||||||
(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ggml_cpy_f32_q8_0_cuda(
|
|
||||||
const char * cx, char * cdst, const int ne,
|
|
||||||
const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
|
|
||||||
const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
|
|
||||||
|
|
||||||
GGML_ASSERT(ne % QK8_0 == 0);
|
|
||||||
const int num_blocks = ne / QK8_0;
|
|
||||||
cpy_f32_q<cpy_blck_f32_q8_0, QK8_0><<<num_blocks, 1, 0, stream>>>
|
|
||||||
(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ggml_cpy_f32_q4_0_cuda(
|
|
||||||
const char * cx, char * cdst, const int ne,
|
|
||||||
const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
|
|
||||||
const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
|
|
||||||
|
|
||||||
GGML_ASSERT(ne % QK4_0 == 0);
|
|
||||||
const int num_blocks = ne / QK4_0;
|
|
||||||
cpy_f32_q<cpy_blck_f32_q4_0, QK4_0><<<num_blocks, 1, 0, stream>>>
|
|
||||||
(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ggml_cpy_f32_q4_1_cuda(
|
|
||||||
const char * cx, char * cdst, const int ne,
|
|
||||||
const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
|
|
||||||
const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
|
|
||||||
|
|
||||||
GGML_ASSERT(ne % QK4_1 == 0);
|
|
||||||
const int num_blocks = ne / QK4_1;
|
|
||||||
cpy_f32_q<cpy_blck_f32_q4_1, QK4_1><<<num_blocks, 1, 0, stream>>>
|
|
||||||
(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ggml_cpy_f32_q5_0_cuda(
|
|
||||||
const char * cx, char * cdst, const int ne,
|
|
||||||
const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
|
|
||||||
const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
|
|
||||||
|
|
||||||
GGML_ASSERT(ne % QK5_0 == 0);
|
|
||||||
const int num_blocks = ne / QK5_0;
|
|
||||||
cpy_f32_q<cpy_blck_f32_q5_0, QK5_0><<<num_blocks, 1, 0, stream>>>
|
|
||||||
(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ggml_cpy_f32_q5_1_cuda(
|
|
||||||
const char * cx, char * cdst, const int ne,
|
|
||||||
const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
|
|
||||||
const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
|
|
||||||
|
|
||||||
GGML_ASSERT(ne % QK5_1 == 0);
|
|
||||||
const int num_blocks = ne / QK5_1;
|
|
||||||
cpy_f32_q<cpy_blck_f32_q5_1, QK5_1><<<num_blocks, 1, 0, stream>>>
|
|
||||||
(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ggml_cpy_f32_iq4_nl_cuda(
|
|
||||||
const char * cx, char * cdst, const int ne,
|
|
||||||
const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
|
|
||||||
const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
|
|
||||||
|
|
||||||
GGML_ASSERT(ne % QK4_NL == 0);
|
|
||||||
const int num_blocks = ne / QK4_NL;
|
|
||||||
cpy_f32_q<cpy_blck_f32_iq4_nl, QK4_NL><<<num_blocks, 1, 0, stream>>>
|
|
||||||
(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ggml_cpy_f16_f16_cuda(
|
|
||||||
const char * cx, char * cdst, const int ne,
|
|
||||||
const int ne00, const int ne01, const int ne02, const int nb00, const int nb01, const int nb02,
|
|
||||||
const int nb03, const int ne10, const int ne11, const int ne12, const int nb10, const int nb11, const int nb12, const int nb13, cudaStream_t stream) {
|
|
||||||
|
|
||||||
const int num_blocks = (ne + CUDA_CPY_BLOCK_SIZE - 1) / CUDA_CPY_BLOCK_SIZE;
|
|
||||||
cpy_f32_f16<cpy_1_f16_f16><<<num_blocks, CUDA_CPY_BLOCK_SIZE, 0, stream>>>
|
|
||||||
(cx, cdst, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_cpy(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, ggml_tensor * src1) {
|
|
||||||
const int64_t ne = ggml_nelements(src0);
|
|
||||||
GGML_ASSERT(ne == ggml_nelements(src1));
|
|
||||||
|
|
||||||
GGML_ASSERT(ggml_nbytes(src0) <= INT_MAX);
|
|
||||||
GGML_ASSERT(ggml_nbytes(src1) <= INT_MAX);
|
|
||||||
|
|
||||||
const int64_t ne00 = src0->ne[0];
|
|
||||||
const int64_t ne01 = src0->ne[1];
|
|
||||||
const int64_t ne02 = src0->ne[2];
|
|
||||||
|
|
||||||
//GGML_ASSERT(src0->ne[3] == 1);
|
|
||||||
|
|
||||||
const int64_t nb00 = src0->nb[0];
|
|
||||||
const int64_t nb01 = src0->nb[1];
|
|
||||||
const int64_t nb02 = src0->nb[2];
|
|
||||||
const int64_t nb03 = src0->nb[3];
|
|
||||||
|
|
||||||
const int64_t ne10 = src1->ne[0];
|
|
||||||
const int64_t ne11 = src1->ne[1];
|
|
||||||
const int64_t ne12 = src1->ne[2];
|
|
||||||
|
|
||||||
//GGML_ASSERT(src1->ne[3] == 1);
|
|
||||||
|
|
||||||
const int64_t nb10 = src1->nb[0];
|
|
||||||
const int64_t nb11 = src1->nb[1];
|
|
||||||
const int64_t nb12 = src1->nb[2];
|
|
||||||
const int64_t nb13 = src1->nb[3];
|
|
||||||
|
|
||||||
cudaStream_t main_stream = ctx.stream();
|
|
||||||
|
|
||||||
char * src0_ddc = (char *) src0->data;
|
|
||||||
char * src1_ddc = (char *) src1->data;
|
|
||||||
|
|
||||||
if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F32) {
|
|
||||||
ggml_cpy_f32_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
|
|
||||||
} else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_F16) {
|
|
||||||
ggml_cpy_f32_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
|
|
||||||
} else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q8_0) {
|
|
||||||
ggml_cpy_f32_q8_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
|
|
||||||
} else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_0) {
|
|
||||||
ggml_cpy_f32_q4_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
|
|
||||||
} else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q4_1) {
|
|
||||||
ggml_cpy_f32_q4_1_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
|
|
||||||
} else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q5_0) {
|
|
||||||
ggml_cpy_f32_q5_0_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
|
|
||||||
} else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_IQ4_NL) {
|
|
||||||
ggml_cpy_f32_iq4_nl_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
|
|
||||||
} else if (src0->type == GGML_TYPE_F32 && src1->type == GGML_TYPE_Q5_1) {
|
|
||||||
ggml_cpy_f32_q5_1_cuda(src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
|
|
||||||
} else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F16) {
|
|
||||||
ggml_cpy_f16_f16_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
|
|
||||||
} else if (src0->type == GGML_TYPE_F16 && src1->type == GGML_TYPE_F32) {
|
|
||||||
ggml_cpy_f16_f32_cuda (src0_ddc, src1_ddc, ne, ne00, ne01, ne02, nb00, nb01, nb02, nb03, ne10, ne11, ne12, nb10, nb11, nb12, nb13, main_stream);
|
|
||||||
} else {
|
|
||||||
fprintf(stderr, "%s: unsupported type combination (%s to %s)\n", __func__,
|
|
||||||
ggml_type_name(src0->type), ggml_type_name(src1->type));
|
|
||||||
GGML_ASSERT(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_dup(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
ggml_cuda_cpy(ctx, src0, dst);
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
#define CUDA_CPY_BLOCK_SIZE 32
|
|
||||||
|
|
||||||
void ggml_cuda_cpy(ggml_backend_cuda_context & ctx, const ggml_tensor * src0, ggml_tensor * src1);
|
|
||||||
|
|
||||||
void ggml_cuda_dup(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
@ -1,103 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
static __device__ __forceinline__ void dequantize_q4_0(const void * vx, const int64_t ib, const int iqs, dfloat2 & v){
|
|
||||||
const block_q4_0 * x = (const block_q4_0 *) vx;
|
|
||||||
|
|
||||||
const dfloat d = x[ib].d;
|
|
||||||
|
|
||||||
const int vui = x[ib].qs[iqs];
|
|
||||||
|
|
||||||
v.x = vui & 0xF;
|
|
||||||
v.y = vui >> 4;
|
|
||||||
|
|
||||||
#ifdef GGML_CUDA_F16
|
|
||||||
v = __hsub2(v, {8.0f, 8.0f});
|
|
||||||
v = __hmul2(v, {d, d});
|
|
||||||
#else
|
|
||||||
v.x = (v.x - 8.0f) * d;
|
|
||||||
v.y = (v.y - 8.0f) * d;
|
|
||||||
#endif // GGML_CUDA_F16
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ __forceinline__ void dequantize_q4_1(const void * vx, const int64_t ib, const int iqs, dfloat2 & v){
|
|
||||||
const block_q4_1 * x = (const block_q4_1 *) vx;
|
|
||||||
|
|
||||||
const dfloat d = __low2half(x[ib].dm);
|
|
||||||
const dfloat m = __high2half(x[ib].dm);
|
|
||||||
|
|
||||||
const int vui = x[ib].qs[iqs];
|
|
||||||
|
|
||||||
v.x = vui & 0xF;
|
|
||||||
v.y = vui >> 4;
|
|
||||||
|
|
||||||
#ifdef GGML_CUDA_F16
|
|
||||||
v = __hmul2(v, {d, d});
|
|
||||||
v = __hadd2(v, {m, m});
|
|
||||||
#else
|
|
||||||
v.x = (v.x * d) + m;
|
|
||||||
v.y = (v.y * d) + m;
|
|
||||||
#endif // GGML_CUDA_F16
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ __forceinline__ void dequantize_q5_0(const void * vx, const int64_t ib, const int iqs, dfloat2 & v){
|
|
||||||
const block_q5_0 * x = (const block_q5_0 *) vx;
|
|
||||||
|
|
||||||
const dfloat d = x[ib].d;
|
|
||||||
|
|
||||||
uint32_t qh;
|
|
||||||
memcpy(&qh, x[ib].qh, sizeof(qh));
|
|
||||||
|
|
||||||
const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
|
|
||||||
const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
|
|
||||||
|
|
||||||
v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
|
|
||||||
v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
|
|
||||||
|
|
||||||
#ifdef GGML_CUDA_F16
|
|
||||||
v = __hsub2(v, {16.0f, 16.0f});
|
|
||||||
v = __hmul2(v, {d, d});
|
|
||||||
#else
|
|
||||||
v.x = (v.x - 16.0f) * d;
|
|
||||||
v.y = (v.y - 16.0f) * d;
|
|
||||||
#endif // GGML_CUDA_F16
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ __forceinline__ void dequantize_q5_1(const void * vx, const int64_t ib, const int iqs, dfloat2 & v){
|
|
||||||
const block_q5_1 * x = (const block_q5_1 *) vx;
|
|
||||||
|
|
||||||
const dfloat d = __low2half(x[ib].dm);
|
|
||||||
const dfloat m = __high2half(x[ib].dm);
|
|
||||||
|
|
||||||
uint32_t qh;
|
|
||||||
memcpy(&qh, x[ib].qh, sizeof(qh));
|
|
||||||
|
|
||||||
const int xh_0 = ((qh >> (iqs + 0)) << 4) & 0x10;
|
|
||||||
const int xh_1 = ((qh >> (iqs + 12)) ) & 0x10;
|
|
||||||
|
|
||||||
v.x = ((x[ib].qs[iqs] & 0xf) | xh_0);
|
|
||||||
v.y = ((x[ib].qs[iqs] >> 4) | xh_1);
|
|
||||||
|
|
||||||
#ifdef GGML_CUDA_F16
|
|
||||||
v = __hmul2(v, {d, d});
|
|
||||||
v = __hadd2(v, {m, m});
|
|
||||||
#else
|
|
||||||
v.x = (v.x * d) + m;
|
|
||||||
v.y = (v.y * d) + m;
|
|
||||||
#endif // GGML_CUDA_F16
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ __forceinline__ void dequantize_q8_0(const void * vx, const int64_t ib, const int iqs, dfloat2 & v){
|
|
||||||
const block_q8_0 * x = (const block_q8_0 *) vx;
|
|
||||||
|
|
||||||
const dfloat d = x[ib].d;
|
|
||||||
|
|
||||||
v.x = x[ib].qs[iqs + 0];
|
|
||||||
v.y = x[ib].qs[iqs + 1];
|
|
||||||
|
|
||||||
#ifdef GGML_CUDA_F16
|
|
||||||
v = __hmul2(v, {d, d});
|
|
||||||
#else
|
|
||||||
v.x *= d;
|
|
||||||
v.y *= d;
|
|
||||||
#endif // GGML_CUDA_F16
|
|
||||||
}
|
|
@ -1,40 +0,0 @@
|
|||||||
#include "diagmask.cuh"
|
|
||||||
|
|
||||||
static __global__ void diag_mask_inf_f32(const float * x, float * dst, const int ncols, const int rows_per_channel, const int n_past) {
|
|
||||||
const int col = blockDim.y*blockIdx.y + threadIdx.y;
|
|
||||||
const int row = blockDim.x*blockIdx.x + threadIdx.x;
|
|
||||||
|
|
||||||
if (col >= ncols) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int i = row*ncols + col;
|
|
||||||
//dst[i] = col > (n_past + row % rows_per_channel) ? -INFINITY : x[i];
|
|
||||||
//dst[i] = x[i] - (col > n_past + row % rows_per_channel) * INT_MAX; // equivalent within rounding error but slightly faster on GPU
|
|
||||||
dst[i] = x[i] - (col > n_past + row % rows_per_channel) * FLT_MAX;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void diag_mask_inf_f32_cuda(const float * x, float * dst, const int ncols_x, const int nrows_x, const int rows_per_channel, const int n_past, cudaStream_t stream) {
|
|
||||||
const dim3 block_dims(1, CUDA_DIAG_MASK_INF_BLOCK_SIZE, 1);
|
|
||||||
const int block_num_x = (ncols_x + CUDA_DIAG_MASK_INF_BLOCK_SIZE - 1) / CUDA_DIAG_MASK_INF_BLOCK_SIZE;
|
|
||||||
const dim3 block_nums(nrows_x, block_num_x, 1);
|
|
||||||
diag_mask_inf_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols_x, rows_per_channel, n_past);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_diag_mask_inf(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
const float * src0_d = (const float *)src0->data;
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
GGML_ASSERT(src0->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT( dst->type == GGML_TYPE_F32);
|
|
||||||
|
|
||||||
const int64_t ne00 = src0->ne[0];
|
|
||||||
const int64_t ne01 = src0->ne[1];
|
|
||||||
const int nrows0 = ggml_nrows(src0);
|
|
||||||
|
|
||||||
const int n_past = ((int32_t *) dst->op_params)[0];
|
|
||||||
|
|
||||||
diag_mask_inf_f32_cuda(src0_d, dst_d, ne00, nrows0, ne01, n_past, stream);
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
#define CUDA_DIAG_MASK_INF_BLOCK_SIZE 32
|
|
||||||
|
|
||||||
void ggml_cuda_op_diag_mask_inf(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
@ -1,813 +0,0 @@
|
|||||||
#include "dmmv.cuh"
|
|
||||||
#include "dequantize.cuh"
|
|
||||||
#include "convert.cuh"
|
|
||||||
|
|
||||||
#ifndef K_QUANTS_PER_ITERATION
|
|
||||||
#define K_QUANTS_PER_ITERATION 2
|
|
||||||
#else
|
|
||||||
static_assert(K_QUANTS_PER_ITERATION == 1 || K_QUANTS_PER_ITERATION == 2, "K_QUANTS_PER_ITERATION must be 1 or 2");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static __global__ void dequantize_mul_mat_vec_q2_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows) {
|
|
||||||
|
|
||||||
static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
|
|
||||||
|
|
||||||
const int row = blockIdx.x*blockDim.y + threadIdx.y;
|
|
||||||
if (row > nrows) return;
|
|
||||||
|
|
||||||
const int num_blocks_per_row = ncols / QK_K;
|
|
||||||
const int ib0 = row*num_blocks_per_row;
|
|
||||||
|
|
||||||
const block_q2_K * x = (const block_q2_K *)vx + ib0;
|
|
||||||
|
|
||||||
float tmp = 0; // partial sum for thread in warp
|
|
||||||
|
|
||||||
#if QK_K == 256
|
|
||||||
const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...15
|
|
||||||
const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
|
|
||||||
|
|
||||||
const int step = 16/K_QUANTS_PER_ITERATION;
|
|
||||||
|
|
||||||
const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
|
|
||||||
const int in = tid - step*im; // 0...15 or 0...7
|
|
||||||
|
|
||||||
const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15 or 0...14 in steps of 2
|
|
||||||
const int q_offset = 32*im + l0;
|
|
||||||
const int s_offset = 8*im;
|
|
||||||
const int y_offset = 128*im + l0;
|
|
||||||
|
|
||||||
uint32_t aux[4];
|
|
||||||
const uint8_t * d = (const uint8_t *)aux;
|
|
||||||
const uint8_t * m = (const uint8_t *)(aux + 2);
|
|
||||||
|
|
||||||
for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
|
|
||||||
|
|
||||||
const float * y = yy + i * QK_K + y_offset;
|
|
||||||
const uint8_t * q = x[i].qs + q_offset;
|
|
||||||
|
|
||||||
const float dall = __low2half(x[i].dm);
|
|
||||||
const float dmin = __high2half(x[i].dm);
|
|
||||||
|
|
||||||
const uint32_t * a = (const uint32_t *)(x[i].scales + s_offset);
|
|
||||||
aux[0] = a[0] & 0x0f0f0f0f;
|
|
||||||
aux[1] = a[1] & 0x0f0f0f0f;
|
|
||||||
aux[2] = (a[0] >> 4) & 0x0f0f0f0f;
|
|
||||||
aux[3] = (a[1] >> 4) & 0x0f0f0f0f;
|
|
||||||
|
|
||||||
float sum1 = 0, sum2 = 0;
|
|
||||||
for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
|
|
||||||
sum1 += y[l+ 0] * d[0] * ((q[l+ 0] >> 0) & 3)
|
|
||||||
+ y[l+32] * d[2] * ((q[l+ 0] >> 2) & 3)
|
|
||||||
+ y[l+64] * d[4] * ((q[l+ 0] >> 4) & 3)
|
|
||||||
+ y[l+96] * d[6] * ((q[l+ 0] >> 6) & 3)
|
|
||||||
+ y[l+16] * d[1] * ((q[l+16] >> 0) & 3)
|
|
||||||
+ y[l+48] * d[3] * ((q[l+16] >> 2) & 3)
|
|
||||||
+ y[l+80] * d[5] * ((q[l+16] >> 4) & 3)
|
|
||||||
+y[l+112] * d[7] * ((q[l+16] >> 6) & 3);
|
|
||||||
sum2 += y[l+ 0] * m[0] + y[l+32] * m[2] + y[l+64] * m[4] + y[ l+96] * m[6]
|
|
||||||
+ y[l+16] * m[1] + y[l+48] * m[3] + y[l+80] * m[5] + y[l+112] * m[7];
|
|
||||||
|
|
||||||
}
|
|
||||||
tmp += dall * sum1 - dmin * sum2;
|
|
||||||
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
|
|
||||||
const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
|
|
||||||
const int offset = tid * K_QUANTS_PER_ITERATION;
|
|
||||||
|
|
||||||
uint32_t uaux[2];
|
|
||||||
const uint8_t * d = (const uint8_t *)uaux;
|
|
||||||
|
|
||||||
for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
|
|
||||||
|
|
||||||
const float * y = yy + i * QK_K + offset;
|
|
||||||
const uint8_t * q = x[i].qs + offset;
|
|
||||||
const uint32_t * s = (const uint32_t *)x[i].scales;
|
|
||||||
|
|
||||||
uaux[0] = s[0] & 0x0f0f0f0f;
|
|
||||||
uaux[1] = (s[0] >> 4) & 0x0f0f0f0f;
|
|
||||||
|
|
||||||
const float2 dall = __half22float2(x[i].dm);
|
|
||||||
|
|
||||||
float sum1 = 0, sum2 = 0;
|
|
||||||
for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
|
|
||||||
const uint8_t ql = q[l];
|
|
||||||
sum1 += y[l+ 0] * d[0] * ((ql >> 0) & 3)
|
|
||||||
+ y[l+16] * d[1] * ((ql >> 2) & 3)
|
|
||||||
+ y[l+32] * d[2] * ((ql >> 4) & 3)
|
|
||||||
+ y[l+48] * d[3] * ((ql >> 6) & 3);
|
|
||||||
sum2 += y[l+0] * d[4] + y[l+16] * d[5] + y[l+32] * d[6] + y[l+48] * d[7];
|
|
||||||
}
|
|
||||||
tmp += dall.x * sum1 - dall.y * sum2;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// sum up partial sums and write back result
|
|
||||||
tmp = warp_reduce_sum(tmp);
|
|
||||||
|
|
||||||
if (threadIdx.x == 0) {
|
|
||||||
dst[row] = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static __global__ void dequantize_mul_mat_vec_q3_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows) {
|
|
||||||
|
|
||||||
const int row = blockIdx.x*blockDim.y + threadIdx.y;
|
|
||||||
if (row > nrows) return;
|
|
||||||
|
|
||||||
const int num_blocks_per_row = ncols / QK_K;
|
|
||||||
const int ib0 = row*num_blocks_per_row;
|
|
||||||
|
|
||||||
const block_q3_K * x = (const block_q3_K *)vx + ib0;
|
|
||||||
|
|
||||||
float tmp = 0; // partial sum for thread in warp
|
|
||||||
|
|
||||||
#if QK_K == 256
|
|
||||||
|
|
||||||
const uint16_t kmask1 = 0x0303;
|
|
||||||
const uint16_t kmask2 = 0x0f0f;
|
|
||||||
|
|
||||||
const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
|
|
||||||
const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
|
|
||||||
|
|
||||||
const int n = K_QUANTS_PER_ITERATION; // iterations in the inner loop
|
|
||||||
const int step = 16/K_QUANTS_PER_ITERATION;
|
|
||||||
const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
|
|
||||||
const int in = tid - step*im; // 0....15 or 0...7
|
|
||||||
|
|
||||||
const uint8_t m = 1 << (4*im);
|
|
||||||
|
|
||||||
const int l0 = n*in; // 0...15 or 0...14 in steps of 2
|
|
||||||
const int q_offset = 32*im + l0;
|
|
||||||
const int y_offset = 128*im + l0;
|
|
||||||
|
|
||||||
uint16_t utmp[4];
|
|
||||||
const int8_t * s = (const int8_t *)utmp;
|
|
||||||
|
|
||||||
const uint16_t s_shift = 4*im;
|
|
||||||
|
|
||||||
for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
|
|
||||||
|
|
||||||
const float * y = yy + i * QK_K + y_offset;
|
|
||||||
const uint8_t * q = x[i].qs + q_offset;
|
|
||||||
const uint8_t * h = x[i].hmask + l0;
|
|
||||||
|
|
||||||
const uint16_t * a = (const uint16_t *)x[i].scales;
|
|
||||||
utmp[0] = ((a[0] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 0)) & kmask1) << 4);
|
|
||||||
utmp[1] = ((a[1] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 0)) & kmask1) << 4);
|
|
||||||
utmp[2] = ((a[2] >> s_shift) & kmask2) | (((a[4] >> (s_shift + 2)) & kmask1) << 4);
|
|
||||||
utmp[3] = ((a[3] >> s_shift) & kmask2) | (((a[5] >> (s_shift + 2)) & kmask1) << 4);
|
|
||||||
|
|
||||||
const float d = x[i].d;
|
|
||||||
|
|
||||||
float sum = 0;
|
|
||||||
for (int l = 0; l < n; ++l) {
|
|
||||||
sum += y[l+ 0] * (s[0] - 32) * (((q[l] >> 0) & 3) - (h[l] & (m << 0) ? 0 : 4))
|
|
||||||
+ y[l+32] * (s[2] - 32) * (((q[l] >> 2) & 3) - (h[l] & (m << 1) ? 0 : 4))
|
|
||||||
+ y[l+64] * (s[4] - 32) * (((q[l] >> 4) & 3) - (h[l] & (m << 2) ? 0 : 4))
|
|
||||||
+ y[l+96] * (s[6] - 32) * (((q[l] >> 6) & 3) - (h[l] & (m << 3) ? 0 : 4));
|
|
||||||
sum += y[l+16] * (s[1] - 32) * (((q[l+16] >> 0) & 3) - (h[l+16] & (m << 0) ? 0 : 4))
|
|
||||||
+ y[l+48] * (s[3] - 32) * (((q[l+16] >> 2) & 3) - (h[l+16] & (m << 1) ? 0 : 4))
|
|
||||||
+ y[l+80] * (s[5] - 32) * (((q[l+16] >> 4) & 3) - (h[l+16] & (m << 2) ? 0 : 4))
|
|
||||||
+ y[l+112] * (s[7] - 32) * (((q[l+16] >> 6) & 3) - (h[l+16] & (m << 3) ? 0 : 4));
|
|
||||||
}
|
|
||||||
tmp += d * sum;
|
|
||||||
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
|
|
||||||
const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15 or 0...7
|
|
||||||
const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0....1 or 0...3
|
|
||||||
const int offset = tid * K_QUANTS_PER_ITERATION; // 0...15 or 0...14
|
|
||||||
const int in = offset/8; // 0 or 1
|
|
||||||
const int im = offset%8; // 0...7
|
|
||||||
|
|
||||||
for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
|
|
||||||
|
|
||||||
const float * y = yy + i * QK_K + offset;
|
|
||||||
const uint8_t * q = x[i].qs + offset;
|
|
||||||
const uint8_t * s = x[i].scales;
|
|
||||||
|
|
||||||
const float dall = (float)x[i].d;
|
|
||||||
|
|
||||||
float sum = 0;
|
|
||||||
for (int l = 0; l < K_QUANTS_PER_ITERATION; ++l) {
|
|
||||||
const uint8_t hl = x[i].hmask[im+l] >> in;
|
|
||||||
const uint8_t ql = q[l];
|
|
||||||
sum += y[l+ 0] * dall * ((s[0] & 0xF) - 8) * ((int8_t)((ql >> 0) & 3) - ((hl >> 0) & 1 ? 0 : 4))
|
|
||||||
+ y[l+16] * dall * ((s[0] >> 4) - 8) * ((int8_t)((ql >> 2) & 3) - ((hl >> 2) & 1 ? 0 : 4))
|
|
||||||
+ y[l+32] * dall * ((s[1] & 0xF) - 8) * ((int8_t)((ql >> 4) & 3) - ((hl >> 4) & 1 ? 0 : 4))
|
|
||||||
+ y[l+48] * dall * ((s[1] >> 4) - 8) * ((int8_t)((ql >> 6) & 3) - ((hl >> 6) & 1 ? 0 : 4));
|
|
||||||
}
|
|
||||||
tmp += sum;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// sum up partial sums and write back result
|
|
||||||
tmp = warp_reduce_sum(tmp);
|
|
||||||
|
|
||||||
if (threadIdx.x == 0) {
|
|
||||||
dst[row] = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static __global__ void dequantize_mul_mat_vec_q4_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows) {
|
|
||||||
|
|
||||||
const int row = blockIdx.x*blockDim.y + threadIdx.y;
|
|
||||||
if (row > nrows) return;
|
|
||||||
const int num_blocks_per_row = ncols / QK_K;
|
|
||||||
const int ib0 = row*num_blocks_per_row;
|
|
||||||
|
|
||||||
const block_q4_K * x = (const block_q4_K *)vx + ib0;
|
|
||||||
|
|
||||||
#if QK_K == 256
|
|
||||||
const uint16_t kmask1 = 0x3f3f;
|
|
||||||
const uint16_t kmask2 = 0x0f0f;
|
|
||||||
const uint16_t kmask3 = 0xc0c0;
|
|
||||||
|
|
||||||
const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
|
|
||||||
const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0,1
|
|
||||||
|
|
||||||
const int step = 8/K_QUANTS_PER_ITERATION; // 8 or 4
|
|
||||||
|
|
||||||
const int il = tid/step; // 0...3
|
|
||||||
const int ir = tid - step*il; // 0...7 or 0...3
|
|
||||||
const int n = 2 * K_QUANTS_PER_ITERATION; // 2 or 4
|
|
||||||
|
|
||||||
const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
|
|
||||||
const int in = il%2;
|
|
||||||
|
|
||||||
const int l0 = n*(2*ir + in);
|
|
||||||
const int q_offset = 32*im + l0;
|
|
||||||
const int y_offset = 64*im + l0;
|
|
||||||
|
|
||||||
uint16_t aux[4];
|
|
||||||
const uint8_t * sc = (const uint8_t *)aux;
|
|
||||||
|
|
||||||
#if K_QUANTS_PER_ITERATION == 2
|
|
||||||
uint32_t q32[4];
|
|
||||||
const uint8_t * q4 = (const uint8_t *)q32;
|
|
||||||
#else
|
|
||||||
uint16_t q16[4];
|
|
||||||
const uint8_t * q4 = (const uint8_t *)q16;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
float tmp = 0; // partial sum for thread in warp
|
|
||||||
|
|
||||||
for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
|
|
||||||
|
|
||||||
const float * y1 = yy + i*QK_K + y_offset;
|
|
||||||
const float * y2 = y1 + 128;
|
|
||||||
|
|
||||||
const float dall = __low2half(x[i].dm);
|
|
||||||
const float dmin = __high2half(x[i].dm);
|
|
||||||
|
|
||||||
const uint16_t * a = (const uint16_t *)x[i].scales;
|
|
||||||
aux[0] = a[im+0] & kmask1;
|
|
||||||
aux[1] = a[im+2] & kmask1;
|
|
||||||
aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
|
|
||||||
aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
|
|
||||||
|
|
||||||
#if K_QUANTS_PER_ITERATION == 2
|
|
||||||
const uint32_t * q1 = (const uint32_t *)(x[i].qs + q_offset);
|
|
||||||
const uint32_t * q2 = q1 + 16;
|
|
||||||
|
|
||||||
q32[0] = q1[0] & 0x0f0f0f0f;
|
|
||||||
q32[1] = q1[0] & 0xf0f0f0f0;
|
|
||||||
q32[2] = q2[0] & 0x0f0f0f0f;
|
|
||||||
q32[3] = q2[0] & 0xf0f0f0f0;
|
|
||||||
|
|
||||||
float4 s = {0.f, 0.f, 0.f, 0.f};
|
|
||||||
float smin = 0;
|
|
||||||
for (int l = 0; l < 4; ++l) {
|
|
||||||
s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+ 4];
|
|
||||||
s.z += y2[l] * q4[l+8]; s.w += y2[l+32] * q4[l+12];
|
|
||||||
smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
|
|
||||||
}
|
|
||||||
tmp += dall * (s.x * sc[0] + s.y * sc[1] * 1.f/16.f + s.z * sc[4] + s.w * sc[5] * 1.f/16.f) - dmin * smin;
|
|
||||||
#else
|
|
||||||
const uint16_t * q1 = (const uint16_t *)(x[i].qs + q_offset);
|
|
||||||
const uint16_t * q2 = q1 + 32;
|
|
||||||
|
|
||||||
q16[0] = q1[0] & 0x0f0f;
|
|
||||||
q16[1] = q1[0] & 0xf0f0;
|
|
||||||
q16[2] = q2[0] & 0x0f0f;
|
|
||||||
q16[3] = q2[0] & 0xf0f0;
|
|
||||||
|
|
||||||
float4 s = {0.f, 0.f, 0.f, 0.f};
|
|
||||||
float smin = 0;
|
|
||||||
for (int l = 0; l < 2; ++l) {
|
|
||||||
s.x += y1[l] * q4[l+0]; s.y += y1[l+32] * q4[l+2];
|
|
||||||
s.z += y2[l] * q4[l+4]; s.w += y2[l+32] * q4[l+6];
|
|
||||||
smin += y1[l] * sc[2] + y1[l+32] * sc[3] + y2[l] * sc[6] + y2[l+32] * sc[7];
|
|
||||||
}
|
|
||||||
tmp += dall * (s.x * sc[0] + s.y * sc[1] * 1.f/16.f + s.z * sc[4] + s.w * sc[5] * 1.f/16.f) - dmin * smin;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
|
|
||||||
const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
|
|
||||||
|
|
||||||
const int step = tid * K_QUANTS_PER_ITERATION;
|
|
||||||
|
|
||||||
uint16_t aux16[2];
|
|
||||||
const uint8_t * s = (const uint8_t *)aux16;
|
|
||||||
|
|
||||||
float tmp = 0;
|
|
||||||
|
|
||||||
for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
|
|
||||||
const uint8_t * q = x[i].qs + step;
|
|
||||||
const float * y = yy + i*QK_K + step;
|
|
||||||
const uint16_t * a = (const uint16_t *)x[i].scales;
|
|
||||||
aux16[0] = a[0] & 0x0f0f;
|
|
||||||
aux16[1] = (a[0] >> 4) & 0x0f0f;
|
|
||||||
const float d = (float)x[i].dm[0];
|
|
||||||
const float m = (float)x[i].dm[1];
|
|
||||||
float sum = 0.f;
|
|
||||||
for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
|
|
||||||
sum += y[j+ 0] * (d * s[0] * (q[j+ 0] & 0xF) - m * s[2])
|
|
||||||
+ y[j+16] * (d * s[0] * (q[j+16] & 0xF) - m * s[2])
|
|
||||||
+ y[j+32] * (d * s[1] * (q[j+ 0] >> 4) - m * s[3])
|
|
||||||
+ y[j+48] * (d * s[1] * (q[j+16] >> 4) - m * s[3]);
|
|
||||||
}
|
|
||||||
tmp += sum;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// sum up partial sums and write back result
|
|
||||||
tmp = warp_reduce_sum(tmp);
|
|
||||||
|
|
||||||
if (tid == 0) {
|
|
||||||
dst[row] = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static __global__ void dequantize_mul_mat_vec_q5_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols) {
|
|
||||||
|
|
||||||
const int row = blockIdx.x;
|
|
||||||
const int num_blocks_per_row = ncols / QK_K;
|
|
||||||
const int ib0 = row*num_blocks_per_row;
|
|
||||||
|
|
||||||
const block_q5_K * x = (const block_q5_K *)vx + ib0;
|
|
||||||
|
|
||||||
float tmp = 0; // partial sum for thread in warp
|
|
||||||
|
|
||||||
#if QK_K == 256
|
|
||||||
const uint16_t kmask1 = 0x3f3f;
|
|
||||||
const uint16_t kmask2 = 0x0f0f;
|
|
||||||
const uint16_t kmask3 = 0xc0c0;
|
|
||||||
|
|
||||||
const int tid = threadIdx.x/2; // 0...15
|
|
||||||
const int ix = threadIdx.x%2;
|
|
||||||
|
|
||||||
const int il = tid/4; // 0...3
|
|
||||||
const int ir = tid - 4*il;// 0...3
|
|
||||||
const int n = 2;
|
|
||||||
|
|
||||||
const int im = il/2; // 0 or 1. 0 computes 0,32 + 128,160, 1 computes 64,96 + 192,224
|
|
||||||
const int in = il%2;
|
|
||||||
|
|
||||||
const int l0 = n*(2*ir + in);
|
|
||||||
const int q_offset = 32*im + l0;
|
|
||||||
const int y_offset = 64*im + l0;
|
|
||||||
|
|
||||||
const uint8_t hm1 = 1 << (2*im);
|
|
||||||
const uint8_t hm2 = hm1 << 4;
|
|
||||||
|
|
||||||
uint16_t aux[4];
|
|
||||||
const uint8_t * sc = (const uint8_t *)aux;
|
|
||||||
|
|
||||||
uint16_t q16[8];
|
|
||||||
const uint8_t * q4 = (const uint8_t *)q16;
|
|
||||||
|
|
||||||
for (int i = ix; i < num_blocks_per_row; i += 2) {
|
|
||||||
|
|
||||||
const uint8_t * ql1 = x[i].qs + q_offset;
|
|
||||||
const uint8_t * qh = x[i].qh + l0;
|
|
||||||
const float * y1 = yy + i*QK_K + y_offset;
|
|
||||||
const float * y2 = y1 + 128;
|
|
||||||
|
|
||||||
const float dall = __low2half(x[i].dm);
|
|
||||||
const float dmin = __high2half(x[i].dm);
|
|
||||||
|
|
||||||
const uint16_t * a = (const uint16_t *)x[i].scales;
|
|
||||||
aux[0] = a[im+0] & kmask1;
|
|
||||||
aux[1] = a[im+2] & kmask1;
|
|
||||||
aux[2] = ((a[im+4] >> 0) & kmask2) | ((a[im+0] & kmask3) >> 2);
|
|
||||||
aux[3] = ((a[im+4] >> 4) & kmask2) | ((a[im+2] & kmask3) >> 2);
|
|
||||||
|
|
||||||
float4 sum = {0.f, 0.f, 0.f, 0.f};
|
|
||||||
float smin = 0;
|
|
||||||
const uint16_t * q1 = (const uint16_t *)ql1;
|
|
||||||
const uint16_t * q2 = q1 + 32;
|
|
||||||
q16[0] = q1[0] & 0x0f0f;
|
|
||||||
q16[1] = q1[8] & 0x0f0f;
|
|
||||||
q16[2] = (q1[0] >> 4) & 0x0f0f;
|
|
||||||
q16[3] = (q1[8] >> 4) & 0x0f0f;
|
|
||||||
q16[4] = q2[0] & 0x0f0f;
|
|
||||||
q16[5] = q2[8] & 0x0f0f;
|
|
||||||
q16[6] = (q2[0] >> 4) & 0x0f0f;
|
|
||||||
q16[7] = (q2[8] >> 4) & 0x0f0f;
|
|
||||||
for (int l = 0; l < n; ++l) {
|
|
||||||
sum.x += y1[l+ 0] * (q4[l +0] + (qh[l+ 0] & (hm1 << 0) ? 16 : 0))
|
|
||||||
+ y1[l+16] * (q4[l +2] + (qh[l+16] & (hm1 << 0) ? 16 : 0));
|
|
||||||
sum.y += y1[l+32] * (q4[l +4] + (qh[l+ 0] & (hm1 << 1) ? 16 : 0))
|
|
||||||
+ y1[l+48] * (q4[l +6] + (qh[l+16] & (hm1 << 1) ? 16 : 0));
|
|
||||||
sum.z += y2[l+ 0] * (q4[l +8] + (qh[l+ 0] & (hm2 << 0) ? 16 : 0))
|
|
||||||
+ y2[l+16] * (q4[l+10] + (qh[l+16] & (hm2 << 0) ? 16 : 0));
|
|
||||||
sum.w += y2[l+32] * (q4[l+12] + (qh[l+ 0] & (hm2 << 1) ? 16 : 0))
|
|
||||||
+ y2[l+48] * (q4[l+14] + (qh[l+16] & (hm2 << 1) ? 16 : 0));
|
|
||||||
smin += (y1[l] + y1[l+16]) * sc[2] + (y1[l+32] + y1[l+48]) * sc[3]
|
|
||||||
+ (y2[l] + y2[l+16]) * sc[6] + (y2[l+32] + y2[l+48]) * sc[7];
|
|
||||||
}
|
|
||||||
tmp += dall * (sum.x * sc[0] + sum.y * sc[1] + sum.z * sc[4] + sum.w * sc[5]) - dmin * smin;
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...15
|
|
||||||
const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION);
|
|
||||||
const int step = tid * K_QUANTS_PER_ITERATION;
|
|
||||||
const int im = step/8;
|
|
||||||
const int in = step%8;
|
|
||||||
|
|
||||||
for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
|
|
||||||
const uint8_t * q = x[i].qs + step;
|
|
||||||
const int8_t * s = x[i].scales;
|
|
||||||
const float * y = yy + i*QK_K + step;
|
|
||||||
const float d = x[i].d;
|
|
||||||
float sum = 0.f;
|
|
||||||
for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
|
|
||||||
const uint8_t h = x[i].qh[in+j] >> im;
|
|
||||||
sum += y[j+ 0] * d * s[0] * ((q[j+ 0] & 0xF) - ((h >> 0) & 1 ? 0 : 16))
|
|
||||||
+ y[j+16] * d * s[1] * ((q[j+16] & 0xF) - ((h >> 2) & 1 ? 0 : 16))
|
|
||||||
+ y[j+32] * d * s[2] * ((q[j+ 0] >> 4) - ((h >> 4) & 1 ? 0 : 16))
|
|
||||||
+ y[j+48] * d * s[3] * ((q[j+16] >> 4) - ((h >> 6) & 1 ? 0 : 16));
|
|
||||||
}
|
|
||||||
tmp += sum;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// sum up partial sums and write back result
|
|
||||||
tmp = warp_reduce_sum(tmp);
|
|
||||||
|
|
||||||
if (threadIdx.x == 0) {
|
|
||||||
dst[row] = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static __global__ void dequantize_mul_mat_vec_q6_k(const void * __restrict__ vx, const float * __restrict__ yy, float * __restrict__ dst, const int ncols, int nrows) {
|
|
||||||
|
|
||||||
static_assert(16%K_QUANTS_PER_ITERATION == 0, "16 must be divisible by K_QUANTS_PER_ITERATION");
|
|
||||||
|
|
||||||
const int row = blockIdx.x*blockDim.y + threadIdx.y;
|
|
||||||
if (row > nrows) return;
|
|
||||||
|
|
||||||
const int num_blocks_per_row = ncols / QK_K;
|
|
||||||
const int ib0 = row*num_blocks_per_row;
|
|
||||||
|
|
||||||
const block_q6_K * x = (const block_q6_K *)vx + ib0;
|
|
||||||
|
|
||||||
#if QK_K == 256
|
|
||||||
|
|
||||||
const int tid = threadIdx.x/K_QUANTS_PER_ITERATION; // 0...31 or 0...16
|
|
||||||
const int ix = threadIdx.x%K_QUANTS_PER_ITERATION; // 0 or 0, 1
|
|
||||||
|
|
||||||
const int step = 16/K_QUANTS_PER_ITERATION; // 16 or 8
|
|
||||||
|
|
||||||
const int im = tid/step; // 0 or 1. 0 computes 0..., 1 computes 128...
|
|
||||||
const int in = tid - step*im; // 0...15 or 0...7
|
|
||||||
|
|
||||||
#if K_QUANTS_PER_ITERATION == 1
|
|
||||||
const int l0 = K_QUANTS_PER_ITERATION*in; // 0...15
|
|
||||||
const int is = 0;
|
|
||||||
#else
|
|
||||||
const int l0 = 4 * in; // 0, 4, 8, ..., 28
|
|
||||||
const int is = in / 4;
|
|
||||||
#endif
|
|
||||||
const int ql_offset = 64*im + l0;
|
|
||||||
const int qh_offset = 32*im + l0;
|
|
||||||
const int s_offset = 8*im + is;
|
|
||||||
const int y_offset = 128*im + l0;
|
|
||||||
|
|
||||||
float tmp = 0; // partial sum for thread in warp
|
|
||||||
|
|
||||||
for (int i = ix; i < num_blocks_per_row; i += K_QUANTS_PER_ITERATION) {
|
|
||||||
|
|
||||||
const float * y = yy + i * QK_K + y_offset;
|
|
||||||
const uint8_t * ql = x[i].ql + ql_offset;
|
|
||||||
const uint8_t * qh = x[i].qh + qh_offset;
|
|
||||||
const int8_t * s = x[i].scales + s_offset;
|
|
||||||
|
|
||||||
const float d = x[i].d;
|
|
||||||
|
|
||||||
#if K_QUANTS_PER_ITERATION == 1
|
|
||||||
float sum = y[ 0] * s[0] * d * ((int8_t)((ql[ 0] & 0xF) | ((qh[ 0] & 0x03) << 4)) - 32)
|
|
||||||
+ y[16] * s[1] * d * ((int8_t)((ql[16] & 0xF) | ((qh[16] & 0x03) << 4)) - 32)
|
|
||||||
+ y[32] * s[2] * d * ((int8_t)((ql[32] & 0xF) | ((qh[ 0] & 0x0c) << 2)) - 32)
|
|
||||||
+ y[48] * s[3] * d * ((int8_t)((ql[48] & 0xF) | ((qh[16] & 0x0c) << 2)) - 32)
|
|
||||||
+ y[64] * s[4] * d * ((int8_t)((ql[ 0] >> 4) | ((qh[ 0] & 0x30) >> 0)) - 32)
|
|
||||||
+ y[80] * s[5] * d * ((int8_t)((ql[16] >> 4) | ((qh[16] & 0x30) >> 0)) - 32)
|
|
||||||
+ y[96] * s[6] * d * ((int8_t)((ql[32] >> 4) | ((qh[ 0] & 0xc0) >> 2)) - 32)
|
|
||||||
+y[112] * s[7] * d * ((int8_t)((ql[48] >> 4) | ((qh[16] & 0xc0) >> 2)) - 32);
|
|
||||||
tmp += sum;
|
|
||||||
#else
|
|
||||||
float sum = 0;
|
|
||||||
for (int l = 0; l < 4; ++l) {
|
|
||||||
sum += y[l+ 0] * s[0] * d * ((int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32)
|
|
||||||
+ y[l+32] * s[2] * d * ((int8_t)((ql[l+32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32)
|
|
||||||
+ y[l+64] * s[4] * d * ((int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32)
|
|
||||||
+ y[l+96] * s[6] * d * ((int8_t)((ql[l+32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32);
|
|
||||||
}
|
|
||||||
tmp += sum;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
const int tid = threadIdx.x/(2*K_QUANTS_PER_ITERATION); // 0...7
|
|
||||||
const int ix = threadIdx.x%(2*K_QUANTS_PER_ITERATION); // 0...3
|
|
||||||
|
|
||||||
const int step = tid * K_QUANTS_PER_ITERATION;
|
|
||||||
|
|
||||||
float tmp = 0; // partial sum for thread in warp
|
|
||||||
|
|
||||||
for (int i = ix; i < num_blocks_per_row; i += 2*K_QUANTS_PER_ITERATION) {
|
|
||||||
|
|
||||||
const float * y = yy + i * QK_K + step;
|
|
||||||
const uint8_t * ql = x[i].ql + step;
|
|
||||||
const uint8_t * qh = x[i].qh + step;
|
|
||||||
const int8_t * s = x[i].scales;
|
|
||||||
|
|
||||||
const float d = x[i+0].d;
|
|
||||||
|
|
||||||
float sum = 0;
|
|
||||||
for (int j = 0; j < K_QUANTS_PER_ITERATION; ++j) {
|
|
||||||
sum += y[j+ 0] * s[0] * d * ((int8_t)((ql[j+ 0] & 0xF) | ((qh[j] & 0x03) << 4)) - 32)
|
|
||||||
+ y[j+16] * s[1] * d * ((int8_t)((ql[j+16] & 0xF) | ((qh[j] & 0x0c) << 2)) - 32)
|
|
||||||
+ y[j+32] * s[2] * d * ((int8_t)((ql[j+ 0] >> 4) | ((qh[j] & 0x30) >> 0)) - 32)
|
|
||||||
+ y[j+48] * s[3] * d * ((int8_t)((ql[j+16] >> 4) | ((qh[j] & 0xc0) >> 2)) - 32);
|
|
||||||
}
|
|
||||||
tmp += sum;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// sum up partial sums and write back result
|
|
||||||
tmp = warp_reduce_sum(tmp);
|
|
||||||
|
|
||||||
if (tid == 0) {
|
|
||||||
dst[row] = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static __device__ void convert_f16(const void * vx, const int64_t ib, const int iqs, dfloat2 & v){
|
|
||||||
const half * x = (const half *) vx;
|
|
||||||
|
|
||||||
// automatic half -> float type cast if dfloat == float
|
|
||||||
v.x = x[ib + iqs + 0];
|
|
||||||
v.y = x[ib + iqs + 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
template <int qk, int qr, dequantize_kernel_t dequantize_kernel>
|
|
||||||
static __global__ void dequantize_mul_mat_vec(const void * __restrict__ vx, const dfloat * __restrict__ y, float * __restrict__ dst, const int ncols, const int nrows) {
|
|
||||||
// qk = quantized weights per x block
|
|
||||||
// qr = number of quantized weights per data value in x block
|
|
||||||
const int64_t row = (int64_t)blockIdx.x*blockDim.y + threadIdx.y;
|
|
||||||
|
|
||||||
if (row >= nrows) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
|
|
||||||
const int iter_stride = 2*GGML_CUDA_DMMV_X;
|
|
||||||
const int vals_per_iter = iter_stride / WARP_SIZE; // num quantized vals per thread and i iter
|
|
||||||
const int y_offset = qr == 1 ? 1 : qk/2;
|
|
||||||
|
|
||||||
// partial sum for each thread
|
|
||||||
#ifdef GGML_CUDA_F16
|
|
||||||
half2 tmp = {0.0f, 0.0f}; // two sums for f16 to take advantage of half2 intrinsics
|
|
||||||
#else
|
|
||||||
float tmp = 0.0f;
|
|
||||||
#endif // GGML_CUDA_F16
|
|
||||||
|
|
||||||
for (int i = 0; i < ncols; i += iter_stride) {
|
|
||||||
const int col = i + vals_per_iter*tid;
|
|
||||||
const int64_t ib = ((int64_t)row*ncols + col)/qk; // x block index
|
|
||||||
const int iqs = (col%qk)/qr; // x quant index
|
|
||||||
const int iybs = col - col%qk; // y block start index
|
|
||||||
|
|
||||||
// processing >2 values per i iter is faster for fast GPUs
|
|
||||||
#pragma unroll
|
|
||||||
for (int j = 0; j < vals_per_iter; j += 2) {
|
|
||||||
// process 2 vals per j iter
|
|
||||||
|
|
||||||
// dequantize
|
|
||||||
// for qr = 2 the iqs needs to increase by 1 per j iter because 2 weights per data val
|
|
||||||
dfloat2 v;
|
|
||||||
dequantize_kernel(vx, ib, iqs + j/qr, v);
|
|
||||||
|
|
||||||
// matrix multiplication
|
|
||||||
// for qr = 2 the y index needs to increase by 1 per j iter because of y_offset = qk/2
|
|
||||||
#ifdef GGML_CUDA_F16
|
|
||||||
tmp += __hmul2(v, {
|
|
||||||
y[iybs + iqs + j/qr + 0],
|
|
||||||
y[iybs + iqs + j/qr + y_offset]
|
|
||||||
});
|
|
||||||
#else
|
|
||||||
tmp += v.x * y[iybs + iqs + j/qr + 0];
|
|
||||||
tmp += v.y * y[iybs + iqs + j/qr + y_offset];
|
|
||||||
#endif // GGML_CUDA_F16
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// sum up partial sums and write back result
|
|
||||||
tmp = warp_reduce_sum(tmp);
|
|
||||||
|
|
||||||
if (tid == 0) {
|
|
||||||
#ifdef GGML_CUDA_F16
|
|
||||||
dst[row] = tmp.x + tmp.y;
|
|
||||||
#else
|
|
||||||
dst[row] = tmp;
|
|
||||||
#endif // GGML_CUDA_F16
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void dequantize_mul_mat_vec_q4_0_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
|
|
||||||
GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
|
|
||||||
const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
|
|
||||||
// the number of rows may exceed maximum grid size in the y or z dimensions, use the x dimension instead
|
|
||||||
const dim3 block_nums(block_num_y, 1, 1);
|
|
||||||
const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
|
|
||||||
dequantize_mul_mat_vec<QK4_0, QR4_0, dequantize_q4_0>
|
|
||||||
<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void dequantize_mul_mat_vec_q4_1_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
|
|
||||||
GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
|
|
||||||
const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
|
|
||||||
const dim3 block_nums(block_num_y, 1, 1);
|
|
||||||
const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
|
|
||||||
dequantize_mul_mat_vec<QK4_1, QR4_1, dequantize_q4_1>
|
|
||||||
<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void dequantize_mul_mat_vec_q5_0_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
|
|
||||||
GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
|
|
||||||
const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
|
|
||||||
const dim3 block_nums(block_num_y, 1, 1);
|
|
||||||
const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
|
|
||||||
dequantize_mul_mat_vec<QK5_0, QR5_0, dequantize_q5_0>
|
|
||||||
<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void dequantize_mul_mat_vec_q5_1_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
|
|
||||||
GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
|
|
||||||
const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
|
|
||||||
const dim3 block_nums(block_num_y, 1, 1);
|
|
||||||
const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
|
|
||||||
dequantize_mul_mat_vec<QK5_1, QR5_1, dequantize_q5_1>
|
|
||||||
<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void dequantize_mul_mat_vec_q8_0_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
|
|
||||||
GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
|
|
||||||
const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
|
|
||||||
const dim3 block_nums(block_num_y, 1, 1);
|
|
||||||
const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
|
|
||||||
dequantize_mul_mat_vec<QK8_0, QR8_0, dequantize_q8_0>
|
|
||||||
<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void dequantize_mul_mat_vec_q2_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
|
|
||||||
GGML_ASSERT(ncols % QK_K == 0);
|
|
||||||
const int ny = 2; // very slightly faster than 1 even when K_QUANTS_PER_ITERATION = 2
|
|
||||||
const int block_num_y = (nrows + ny - 1) / ny;
|
|
||||||
const dim3 block_nums(block_num_y, 1, 1);
|
|
||||||
const dim3 block_dims(32, ny, 1);
|
|
||||||
dequantize_mul_mat_vec_q2_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void dequantize_mul_mat_vec_q3_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
|
|
||||||
GGML_ASSERT(ncols % QK_K == 0);
|
|
||||||
const int ny = 2 / K_QUANTS_PER_ITERATION;
|
|
||||||
const int block_num_y = (nrows + ny - 1) / ny;
|
|
||||||
const dim3 block_nums(block_num_y, 1, 1);
|
|
||||||
const dim3 block_dims(32, ny, 1);
|
|
||||||
dequantize_mul_mat_vec_q3_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void dequantize_mul_mat_vec_q4_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
|
|
||||||
GGML_ASSERT(ncols % QK_K == 0);
|
|
||||||
const int ny = 2 / K_QUANTS_PER_ITERATION;
|
|
||||||
const int block_num_y = (nrows + ny - 1) / ny;
|
|
||||||
const dim3 block_nums(block_num_y, 1, 1);
|
|
||||||
const dim3 block_dims(32, ny, 1);
|
|
||||||
dequantize_mul_mat_vec_q4_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void dequantize_mul_mat_vec_q5_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
|
|
||||||
GGML_ASSERT(ncols % QK_K == 0);
|
|
||||||
const dim3 block_dims(32, 1, 1);
|
|
||||||
dequantize_mul_mat_vec_q5_k<<<nrows, block_dims, 0, stream>>>(vx, y, dst, ncols);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void dequantize_mul_mat_vec_q6_K_cuda(const void * vx, const float * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
|
|
||||||
GGML_ASSERT(ncols % QK_K == 0);
|
|
||||||
const int ny = 2 / K_QUANTS_PER_ITERATION;
|
|
||||||
const int block_num_y = (nrows + ny - 1) / ny;
|
|
||||||
const dim3 block_nums(block_num_y, 1, 1);
|
|
||||||
const dim3 block_dims(32, ny, 1);
|
|
||||||
dequantize_mul_mat_vec_q6_k<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void convert_mul_mat_vec_f16_cuda(const void * vx, const dfloat * y, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
|
|
||||||
GGML_ASSERT(ncols % GGML_CUDA_DMMV_X == 0);
|
|
||||||
const int block_num_y = (nrows + GGML_CUDA_MMV_Y - 1) / GGML_CUDA_MMV_Y;
|
|
||||||
const dim3 block_nums(block_num_y, 1, 1);
|
|
||||||
const dim3 block_dims(WARP_SIZE, GGML_CUDA_MMV_Y, 1);
|
|
||||||
dequantize_mul_mat_vec<1, 1, convert_f16>
|
|
||||||
<<<block_nums, block_dims, 0, stream>>>(vx, y, dst, ncols, nrows);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_dequantize_mul_mat_vec(
|
|
||||||
ggml_backend_cuda_context & ctx,
|
|
||||||
const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
|
|
||||||
const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
|
|
||||||
const int64_t src1_padded_row_size, cudaStream_t stream) {
|
|
||||||
GGML_UNUSED(ctx);
|
|
||||||
const int64_t ne00 = src0->ne[0];
|
|
||||||
const int64_t row_diff = row_high - row_low;
|
|
||||||
|
|
||||||
GGML_ASSERT(src1->type == GGML_TYPE_F32);
|
|
||||||
|
|
||||||
// on some GPUs it is faster to convert src1 to half and to use half precision intrinsics
|
|
||||||
#ifdef GGML_CUDA_F16
|
|
||||||
ggml_cuda_pool_alloc<half> src1_dfloat_a(ctx.pool());
|
|
||||||
half * src1_dfloat = nullptr; // dfloat == half
|
|
||||||
|
|
||||||
bool src1_convert_f16 =
|
|
||||||
src0->type == GGML_TYPE_Q4_0 || src0->type == GGML_TYPE_Q4_1 ||
|
|
||||||
src0->type == GGML_TYPE_Q5_0 || src0->type == GGML_TYPE_Q5_1 ||
|
|
||||||
src0->type == GGML_TYPE_Q8_0 || src0->type == GGML_TYPE_F16;
|
|
||||||
|
|
||||||
if (src1_convert_f16) {
|
|
||||||
src1_dfloat = src1_dfloat_a.alloc(ne00);
|
|
||||||
const to_fp16_cuda_t to_fp16_cuda = ggml_get_to_fp16_cuda(src1->type);
|
|
||||||
GGML_ASSERT(to_fp16_cuda != nullptr);
|
|
||||||
to_fp16_cuda(src1_ddf_i, src1_dfloat, ne00, stream);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
const dfloat * src1_dfloat = (const dfloat *) src1_ddf_i; // dfloat == float, no conversion
|
|
||||||
#endif // GGML_CUDA_F16
|
|
||||||
|
|
||||||
switch (src0->type) {
|
|
||||||
case GGML_TYPE_Q4_0:
|
|
||||||
dequantize_mul_mat_vec_q4_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q4_1:
|
|
||||||
dequantize_mul_mat_vec_q4_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q5_0:
|
|
||||||
dequantize_mul_mat_vec_q5_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q5_1:
|
|
||||||
dequantize_mul_mat_vec_q5_1_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q8_0:
|
|
||||||
dequantize_mul_mat_vec_q8_0_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q2_K:
|
|
||||||
dequantize_mul_mat_vec_q2_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q3_K:
|
|
||||||
dequantize_mul_mat_vec_q3_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q4_K:
|
|
||||||
dequantize_mul_mat_vec_q4_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q5_K:
|
|
||||||
dequantize_mul_mat_vec_q5_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q6_K:
|
|
||||||
dequantize_mul_mat_vec_q6_K_cuda(src0_dd_i, src1_ddf_i, dst_dd_i, ne00, row_diff, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_F16:
|
|
||||||
convert_mul_mat_vec_f16_cuda(src0_dd_i, src1_dfloat, dst_dd_i, ne00, row_diff, stream);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
GGML_ASSERT(false);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
GGML_UNUSED(src1);
|
|
||||||
GGML_UNUSED(dst);
|
|
||||||
GGML_UNUSED(src1_ddq_i);
|
|
||||||
GGML_UNUSED(src1_ncols);
|
|
||||||
GGML_UNUSED(src1_padded_row_size);
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
// dmmv = dequantize_mul_mat_vec
|
|
||||||
|
|
||||||
// TODO: remove this?
|
|
||||||
#ifndef GGML_CUDA_DMMV_X
|
|
||||||
#define GGML_CUDA_DMMV_X 32
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef GGML_CUDA_MMV_Y
|
|
||||||
#define GGML_CUDA_MMV_Y 1
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void ggml_cuda_op_dequantize_mul_mat_vec(
|
|
||||||
ggml_backend_cuda_context & ctx,
|
|
||||||
const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
|
|
||||||
const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
|
|
||||||
const int64_t src1_padded_row_size, cudaStream_t stream);
|
|
@ -1,178 +0,0 @@
|
|||||||
#include "getrows.cuh"
|
|
||||||
#include "dequantize.cuh"
|
|
||||||
|
|
||||||
template<int qk, int qr, dequantize_kernel_t dequantize_kernel, typename dst_t>
|
|
||||||
static __global__ void k_get_rows(
|
|
||||||
const void * src0, const int32_t * src1, dst_t * dst,
|
|
||||||
int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
|
|
||||||
/*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
|
|
||||||
/*size_t s0,*/ size_t s1, size_t s2, size_t s3,
|
|
||||||
/*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
|
|
||||||
size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
|
|
||||||
|
|
||||||
const int i00 = (blockIdx.x*blockDim.x + threadIdx.x)*2;
|
|
||||||
const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
|
|
||||||
const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
|
|
||||||
const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
|
|
||||||
|
|
||||||
if (i00 >= ne00) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
|
|
||||||
|
|
||||||
dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
|
|
||||||
const void * src0_row = (const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03;
|
|
||||||
|
|
||||||
const int ib = i00/qk; // block index
|
|
||||||
const int iqs = (i00%qk)/qr; // quant index
|
|
||||||
const int iybs = i00 - i00%qk; // dst block start index
|
|
||||||
const int y_offset = qr == 1 ? 1 : qk/2;
|
|
||||||
|
|
||||||
// dequantize
|
|
||||||
dfloat2 v;
|
|
||||||
dequantize_kernel(src0_row, ib, iqs, v);
|
|
||||||
|
|
||||||
dst_row[iybs + iqs + 0] = v.x;
|
|
||||||
dst_row[iybs + iqs + y_offset] = v.y;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename src0_t, typename dst_t>
|
|
||||||
static __global__ void k_get_rows_float(
|
|
||||||
const src0_t * src0, const int32_t * src1, dst_t * dst,
|
|
||||||
int64_t ne00, /*int64_t ne01, int64_t ne02, int64_t ne03,*/
|
|
||||||
/*int64_t ne10, int64_t ne11,*/ int64_t ne12, /*int64_t ne13,*/
|
|
||||||
/*size_t s0,*/ size_t s1, size_t s2, size_t s3,
|
|
||||||
/*size_t nb00,*/ size_t nb01, size_t nb02, size_t nb03,
|
|
||||||
size_t s10, size_t s11, size_t s12/*, size_t s13*/) {
|
|
||||||
|
|
||||||
const int i00 = blockIdx.x*blockDim.x + threadIdx.x;
|
|
||||||
const int i10 = blockDim.y*blockIdx.y + threadIdx.y;
|
|
||||||
const int i11 = (blockIdx.z*blockDim.z + threadIdx.z)/ne12;
|
|
||||||
const int i12 = (blockIdx.z*blockDim.z + threadIdx.z)%ne12;
|
|
||||||
|
|
||||||
if (i00 >= ne00) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int i01 = src1[i10*s10 + i11*s11 + i12*s12];
|
|
||||||
|
|
||||||
dst_t * dst_row = dst + i10*s1 + i11*s2 + i12*s3;
|
|
||||||
const src0_t * src0_row = (const src0_t *)((const char *)src0 + i01*nb01 + i11*nb02 + i12*nb03);
|
|
||||||
|
|
||||||
dst_row[i00] = src0_row[i00];
|
|
||||||
}
|
|
||||||
|
|
||||||
template<int qk, int qr, dequantize_kernel_t dq>
|
|
||||||
static void get_rows_cuda(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
|
|
||||||
const void * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
|
|
||||||
|
|
||||||
GGML_TENSOR_BINARY_OP_LOCALS
|
|
||||||
|
|
||||||
const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
|
|
||||||
const int block_num_x = (ne00 + 2*CUDA_GET_ROWS_BLOCK_SIZE - 1) / (2*CUDA_GET_ROWS_BLOCK_SIZE);
|
|
||||||
const dim3 block_nums(block_num_x, ne10, ne11*ne12);
|
|
||||||
|
|
||||||
// strides in elements
|
|
||||||
//const size_t s0 = nb0 / ggml_element_size(dst);
|
|
||||||
const size_t s1 = nb1 / ggml_element_size(dst);
|
|
||||||
const size_t s2 = nb2 / ggml_element_size(dst);
|
|
||||||
const size_t s3 = nb3 / ggml_element_size(dst);
|
|
||||||
|
|
||||||
const size_t s10 = nb10 / ggml_element_size(src1);
|
|
||||||
const size_t s11 = nb11 / ggml_element_size(src1);
|
|
||||||
const size_t s12 = nb12 / ggml_element_size(src1);
|
|
||||||
//const size_t s13 = nb13 / ggml_element_size(src1);
|
|
||||||
|
|
||||||
GGML_ASSERT(ne00 % 2 == 0);
|
|
||||||
|
|
||||||
k_get_rows<qk, qr, dq><<<block_nums, block_dims, 0, stream>>>(
|
|
||||||
src0_dd, src1_dd, dst_dd,
|
|
||||||
ne00, /*ne01, ne02, ne03,*/
|
|
||||||
/*ne10, ne11,*/ ne12, /*ne13,*/
|
|
||||||
/* s0,*/ s1, s2, s3,
|
|
||||||
/* nb00,*/ nb01, nb02, nb03,
|
|
||||||
s10, s11, s12/*, s13*/);
|
|
||||||
|
|
||||||
GGML_UNUSED(dst);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename src0_t>
|
|
||||||
static void get_rows_cuda_float(const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst,
|
|
||||||
const src0_t * src0_dd, const int32_t * src1_dd, float * dst_dd, cudaStream_t stream) {
|
|
||||||
|
|
||||||
GGML_TENSOR_BINARY_OP_LOCALS
|
|
||||||
|
|
||||||
const dim3 block_dims(CUDA_GET_ROWS_BLOCK_SIZE, 1, 1);
|
|
||||||
const int block_num_x = (ne00 + CUDA_GET_ROWS_BLOCK_SIZE - 1) / CUDA_GET_ROWS_BLOCK_SIZE;
|
|
||||||
const dim3 block_nums(block_num_x, ne10, ne11*ne12);
|
|
||||||
|
|
||||||
// strides in elements
|
|
||||||
//const size_t s0 = nb0 / ggml_element_size(dst);
|
|
||||||
const size_t s1 = nb1 / ggml_element_size(dst);
|
|
||||||
const size_t s2 = nb2 / ggml_element_size(dst);
|
|
||||||
const size_t s3 = nb3 / ggml_element_size(dst);
|
|
||||||
|
|
||||||
const size_t s10 = nb10 / ggml_element_size(src1);
|
|
||||||
const size_t s11 = nb11 / ggml_element_size(src1);
|
|
||||||
const size_t s12 = nb12 / ggml_element_size(src1);
|
|
||||||
//const size_t s13 = nb13 / ggml_element_size(src1);
|
|
||||||
|
|
||||||
k_get_rows_float<<<block_nums, block_dims, 0, stream>>>(
|
|
||||||
src0_dd, src1_dd, dst_dd,
|
|
||||||
ne00, /*ne01, ne02, ne03,*/
|
|
||||||
/*ne10, ne11,*/ ne12, /*ne13,*/
|
|
||||||
/* s0,*/ s1, s2, s3,
|
|
||||||
/* nb00,*/ nb01, nb02, nb03,
|
|
||||||
s10, s11, s12/*, s13*/);
|
|
||||||
|
|
||||||
GGML_UNUSED(dst);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_get_rows(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
const ggml_tensor * src1 = dst->src[1];
|
|
||||||
const float * src0_d = (const float *)src0->data;
|
|
||||||
const float * src1_d = (const float *)src1->data;
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
|
|
||||||
GGML_ASSERT(src1->type == GGML_TYPE_I32);
|
|
||||||
GGML_ASSERT(dst->type == GGML_TYPE_F32);
|
|
||||||
|
|
||||||
GGML_ASSERT(src0->nb[0] == ggml_type_size(src0->type));
|
|
||||||
GGML_ASSERT(src1->nb[0] == ggml_type_size(src1->type));
|
|
||||||
GGML_ASSERT(dst->nb[0] == ggml_type_size(dst->type));
|
|
||||||
|
|
||||||
const int32_t * src1_i32 = (const int32_t *) src1_d;
|
|
||||||
|
|
||||||
switch (src0->type) {
|
|
||||||
case GGML_TYPE_F16:
|
|
||||||
get_rows_cuda_float(src0, src1, dst, (const half *)src0_d, src1_i32, dst_d, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_F32:
|
|
||||||
get_rows_cuda_float(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q4_0:
|
|
||||||
get_rows_cuda<QK4_0, QR4_0, dequantize_q4_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q4_1:
|
|
||||||
get_rows_cuda<QK4_1, QR4_1, dequantize_q4_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q5_0:
|
|
||||||
get_rows_cuda<QK5_0, QR5_0, dequantize_q5_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q5_1:
|
|
||||||
get_rows_cuda<QK5_1, QR5_1, dequantize_q5_1>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q8_0:
|
|
||||||
get_rows_cuda<QK8_0, QR8_0, dequantize_q8_0>(src0, src1, dst, src0_d, src1_i32, dst_d, stream);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
// TODO: k-quants
|
|
||||||
fprintf(stderr, "%s: unsupported type: %s\n", __func__, ggml_type_name(src0->type));
|
|
||||||
GGML_ASSERT(false);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
#define CUDA_GET_ROWS_BLOCK_SIZE 256
|
|
||||||
|
|
||||||
void ggml_cuda_op_get_rows(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
@ -1,104 +0,0 @@
|
|||||||
#include "im2col.cuh"
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
static __global__ void im2col_kernel(
|
|
||||||
const float * x, T * dst, int64_t batch_offset,
|
|
||||||
int64_t offset_delta, int64_t IC, int64_t IW, int64_t IH, int64_t OH, int64_t OW, int64_t KW, int64_t KH, int64_t pelements, int64_t CHW,
|
|
||||||
int s0, int s1, int p0, int p1, int d0, int d1) {
|
|
||||||
const int64_t i = threadIdx.x + blockIdx.x * blockDim.x;
|
|
||||||
if (i >= pelements) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int64_t ksize = OW * (KH > 1 ? KW : 1);
|
|
||||||
const int64_t kx = i / ksize;
|
|
||||||
const int64_t kd = kx * ksize;
|
|
||||||
const int64_t ky = (i - kd) / OW;
|
|
||||||
const int64_t ix = i % OW;
|
|
||||||
|
|
||||||
const int64_t oh = blockIdx.y;
|
|
||||||
const int64_t batch = blockIdx.z / IC;
|
|
||||||
const int64_t ic = blockIdx.z % IC;
|
|
||||||
|
|
||||||
const int64_t iiw = ix * s0 + kx * d0 - p0;
|
|
||||||
const int64_t iih = oh * s1 + ky * d1 - p1;
|
|
||||||
|
|
||||||
const int64_t offset_dst =
|
|
||||||
((batch * OH + oh) * OW + ix) * CHW +
|
|
||||||
(ic * (KW * KH) + ky * KW + kx);
|
|
||||||
|
|
||||||
if (iih < 0 || iih >= IH || iiw < 0 || iiw >= IW) {
|
|
||||||
dst[offset_dst] = 0.0f;
|
|
||||||
} else {
|
|
||||||
const int64_t offset_src = ic * offset_delta + batch * batch_offset;
|
|
||||||
dst[offset_dst] = x[offset_src + iih * IW + iiw];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
static void im2col_cuda(const float * x, T* dst,
|
|
||||||
int64_t IW, int64_t IH, int64_t OW, int64_t OH, int64_t KW, int64_t KH, int64_t IC,
|
|
||||||
int64_t batch, int64_t batch_offset, int64_t offset_delta,
|
|
||||||
int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
|
|
||||||
const int parallel_elements = OW * KW * KH;
|
|
||||||
const int num_blocks = (parallel_elements + CUDA_IM2COL_BLOCK_SIZE - 1) / CUDA_IM2COL_BLOCK_SIZE;
|
|
||||||
dim3 block_nums(num_blocks, OH, batch * IC);
|
|
||||||
im2col_kernel<<<block_nums, CUDA_IM2COL_BLOCK_SIZE, 0, stream>>>(x, dst, batch_offset, offset_delta, IC, IW, IH, OH, OW, KW, KH, parallel_elements, (IC * KH * KW), s0, s1, p0, p1, d0, d1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void im2col_cuda_f16(const float * x, half * dst,
|
|
||||||
int64_t IW, int64_t IH, int64_t OW, int64_t OH, int64_t KW, int64_t KH, int64_t IC,
|
|
||||||
int64_t batch, int64_t batch_offset, int64_t offset_delta,
|
|
||||||
int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
|
|
||||||
|
|
||||||
im2col_cuda<half>(x, dst, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, offset_delta, s0, s1, p0, p1, d0, d1, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void im2col_cuda_f32(const float * x, float * dst,
|
|
||||||
int64_t IW, int64_t IH, int64_t OW, int64_t OH, int64_t KW, int64_t KH, int64_t IC,
|
|
||||||
int64_t batch, int64_t batch_offset, int64_t offset_delta,
|
|
||||||
int s0,int s1,int p0,int p1,int d0,int d1, cudaStream_t stream) {
|
|
||||||
|
|
||||||
im2col_cuda<float>(x, dst, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, offset_delta, s0, s1, p0, p1, d0, d1, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_im2col(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
const ggml_tensor * src1 = dst->src[1];
|
|
||||||
const float * src1_d = (const float *)src1->data;
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
GGML_ASSERT(src0->type == GGML_TYPE_F16);
|
|
||||||
GGML_ASSERT(src1->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT( dst->type == GGML_TYPE_F16 || dst->type == GGML_TYPE_F32);
|
|
||||||
|
|
||||||
const int32_t s0 = ((const int32_t*)(dst->op_params))[0];
|
|
||||||
const int32_t s1 = ((const int32_t*)(dst->op_params))[1];
|
|
||||||
const int32_t p0 = ((const int32_t*)(dst->op_params))[2];
|
|
||||||
const int32_t p1 = ((const int32_t*)(dst->op_params))[3];
|
|
||||||
const int32_t d0 = ((const int32_t*)(dst->op_params))[4];
|
|
||||||
const int32_t d1 = ((const int32_t*)(dst->op_params))[5];
|
|
||||||
|
|
||||||
const bool is_2D = ((const int32_t*)(dst->op_params))[6] == 1;
|
|
||||||
|
|
||||||
const int64_t IC = src1->ne[is_2D ? 2 : 1];
|
|
||||||
const int64_t IH = is_2D ? src1->ne[1] : 1;
|
|
||||||
const int64_t IW = src1->ne[0];
|
|
||||||
|
|
||||||
const int64_t KH = is_2D ? src0->ne[1] : 1;
|
|
||||||
const int64_t KW = src0->ne[0];
|
|
||||||
|
|
||||||
const int64_t OH = is_2D ? dst->ne[2] : 1;
|
|
||||||
const int64_t OW = dst->ne[1];
|
|
||||||
|
|
||||||
const size_t delta_offset = src1->nb[is_2D ? 2 : 1] / 4; // nb is byte offset, src is type float32
|
|
||||||
const int64_t batch = src1->ne[3];
|
|
||||||
const size_t batch_offset = src1->nb[3] / 4; // nb is byte offset, src is type float32
|
|
||||||
|
|
||||||
if(dst->type == GGML_TYPE_F16) {
|
|
||||||
im2col_cuda_f16(src1_d, (half *) dst_d, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, delta_offset, s0, s1, p0, p1, d0, d1, stream);
|
|
||||||
} else {
|
|
||||||
im2col_cuda_f32(src1_d, (float *) dst_d, IW, IH, OW, OH, KW, KH, IC, batch, batch_offset, delta_offset, s0, s1, p0, p1, d0, d1, stream);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
#define CUDA_IM2COL_BLOCK_SIZE 256
|
|
||||||
|
|
||||||
void ggml_cuda_op_im2col(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
2265
ggml-cuda/mmq.cu
2265
ggml-cuda/mmq.cu
File diff suppressed because it is too large
Load Diff
@ -1,9 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
void ggml_cuda_op_mul_mat_q(
|
|
||||||
ggml_backend_cuda_context & ctx,
|
|
||||||
const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
|
|
||||||
const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
|
|
||||||
const int64_t src1_padded_row_size, cudaStream_t stream);
|
|
||||||
|
|
||||||
bool ggml_cuda_supports_mmq(enum ggml_type type);
|
|
@ -1,406 +0,0 @@
|
|||||||
#include "mmvq.cuh"
|
|
||||||
#include "vecdotq.cuh"
|
|
||||||
|
|
||||||
typedef float (*vec_dot_q_cuda_t)(const void * __restrict__ vbq, const block_q8_1 * __restrict__ bq8_1, const int & iqs);
|
|
||||||
|
|
||||||
template <int ncols_y, int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot_q_cuda>
|
|
||||||
#if !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
|
|
||||||
// tell the compiler to use as many registers as it wants, see nwarps definition below
|
|
||||||
__launch_bounds__((ncols_y <= 4 ? 4 : 2)*WARP_SIZE, 1)
|
|
||||||
#endif // !(defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__))
|
|
||||||
static __global__ void mul_mat_vec_q(
|
|
||||||
const void * __restrict__ vx, const void * __restrict__ vy, float * __restrict__ dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int nrows_dst) {
|
|
||||||
|
|
||||||
#if defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && (defined(RDNA2) || defined(RDNA3))
|
|
||||||
constexpr int nwarps = 1;
|
|
||||||
constexpr int rows_per_cuda_block = 1;
|
|
||||||
#else
|
|
||||||
constexpr int nwarps = ncols_y <= 4 ? 4 : 2;
|
|
||||||
constexpr int rows_per_cuda_block = ncols_y == 1 ? 1 : 2;
|
|
||||||
#endif // defined(GGML_USE_HIPBLAS) && defined(__HIP_PLATFORM_AMD__) && !defined(RDNA2) && !defined(RDNA3)
|
|
||||||
|
|
||||||
const int tid = WARP_SIZE*threadIdx.y + threadIdx.x;
|
|
||||||
const int row0 = rows_per_cuda_block*blockIdx.x;
|
|
||||||
const int blocks_per_row_x = ncols_x / qk;
|
|
||||||
const int blocks_per_col_y = nrows_y / QK8_1;
|
|
||||||
constexpr int blocks_per_iter = vdr * nwarps*WARP_SIZE / qi;
|
|
||||||
|
|
||||||
// partial sum for each thread
|
|
||||||
float tmp[ncols_y][rows_per_cuda_block] = {0.0f};
|
|
||||||
|
|
||||||
const block_q_t * x = (const block_q_t *) vx;
|
|
||||||
const block_q8_1 * y = (const block_q8_1 *) vy;
|
|
||||||
|
|
||||||
for (int kbx = tid / (qi/vdr); kbx < blocks_per_row_x; kbx += blocks_per_iter) {
|
|
||||||
const int kby = kbx * (qk/QK8_1); // y block index that aligns with kbx
|
|
||||||
|
|
||||||
// x block quant index when casting the quants to int
|
|
||||||
const int kqs = vdr * (tid % (qi/vdr));
|
|
||||||
|
|
||||||
#pragma unroll
|
|
||||||
for (int j = 0; j < ncols_y; ++j) {
|
|
||||||
#pragma unroll
|
|
||||||
for (int i = 0; i < rows_per_cuda_block; ++i) {
|
|
||||||
tmp[j][i] += vec_dot_q_cuda(
|
|
||||||
&x[kbx + (row0 + i)*blocks_per_row_x], &y[j*blocks_per_col_y + kby], kqs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
__shared__ float tmp_shared[nwarps-1 > 0 ? nwarps-1 : 1][ncols_y][rows_per_cuda_block][WARP_SIZE];
|
|
||||||
if (threadIdx.y > 0) {
|
|
||||||
#pragma unroll
|
|
||||||
for (int j = 0; j < ncols_y; ++j) {
|
|
||||||
#pragma unroll
|
|
||||||
for (int i = 0; i < rows_per_cuda_block; ++i) {
|
|
||||||
tmp_shared[threadIdx.y-1][j][i][threadIdx.x] = tmp[j][i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
__syncthreads();
|
|
||||||
if (threadIdx.y > 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// sum up partial sums and write back result
|
|
||||||
#pragma unroll
|
|
||||||
for (int j = 0; j < ncols_y; ++j) {
|
|
||||||
#pragma unroll
|
|
||||||
for (int i = 0; i < rows_per_cuda_block; ++i) {
|
|
||||||
#pragma unroll
|
|
||||||
for (int l = 0; l < nwarps-1; ++l) {
|
|
||||||
tmp[j][i] += tmp_shared[l][j][i][threadIdx.x];
|
|
||||||
}
|
|
||||||
tmp[j][i] = warp_reduce_sum(tmp[j][i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (threadIdx.x < rows_per_cuda_block) {
|
|
||||||
dst[j*nrows_dst + row0 + threadIdx.x] = tmp[j][threadIdx.x];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <int qk, int qi, typename block_q_t, int vdr, vec_dot_q_cuda_t vec_dot>
|
|
||||||
static void mul_mat_vec_q_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
GGML_ASSERT(ncols_x % qk == 0);
|
|
||||||
GGML_ASSERT(ncols_y <= MMVQ_MAX_BATCH_SIZE);
|
|
||||||
|
|
||||||
int id;
|
|
||||||
CUDA_CHECK(cudaGetDevice(&id));
|
|
||||||
|
|
||||||
int64_t nwarps = 1;
|
|
||||||
int64_t rows_per_cuda_block = 1;
|
|
||||||
|
|
||||||
if (ggml_cuda_info().devices[id].cc < CC_RDNA2) { // NVIDIA and AMD older than RDNA2
|
|
||||||
switch(ncols_y) {
|
|
||||||
case 1:
|
|
||||||
nwarps = 4;
|
|
||||||
rows_per_cuda_block = 1;
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
case 3:
|
|
||||||
case 4:
|
|
||||||
nwarps = 4;
|
|
||||||
rows_per_cuda_block = 2;
|
|
||||||
break;
|
|
||||||
case 5:
|
|
||||||
case 6:
|
|
||||||
case 7:
|
|
||||||
case 8:
|
|
||||||
nwarps = 2;
|
|
||||||
rows_per_cuda_block = 2;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
GGML_ASSERT(false);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const int64_t nblocks = (nrows_x + rows_per_cuda_block - 1) / rows_per_cuda_block;
|
|
||||||
const dim3 block_nums(nblocks, 1, 1);
|
|
||||||
const dim3 block_dims(WARP_SIZE, nwarps, 1);
|
|
||||||
|
|
||||||
switch (ncols_y) {
|
|
||||||
case 1:
|
|
||||||
mul_mat_vec_q<1, qk, qi, block_q_t, vdr, vec_dot>
|
|
||||||
<<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
mul_mat_vec_q<2, qk, qi, block_q_t, vdr, vec_dot>
|
|
||||||
<<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
mul_mat_vec_q<3, qk, qi, block_q_t, vdr, vec_dot>
|
|
||||||
<<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
mul_mat_vec_q<4, qk, qi, block_q_t, vdr, vec_dot>
|
|
||||||
<<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
|
|
||||||
break;
|
|
||||||
case 5:
|
|
||||||
mul_mat_vec_q<5, qk, qi, block_q_t, vdr, vec_dot>
|
|
||||||
<<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
|
|
||||||
break;
|
|
||||||
case 6:
|
|
||||||
mul_mat_vec_q<6, qk, qi, block_q_t, vdr, vec_dot>
|
|
||||||
<<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
|
|
||||||
break;
|
|
||||||
case 7:
|
|
||||||
mul_mat_vec_q<7, qk, qi, block_q_t, vdr, vec_dot>
|
|
||||||
<<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
|
|
||||||
break;
|
|
||||||
case 8:
|
|
||||||
mul_mat_vec_q<8, qk, qi, block_q_t, vdr, vec_dot>
|
|
||||||
<<<block_nums, block_dims, 0, stream>>>(vx, vy, dst, ncols_x, nrows_x, nrows_y, nrows_dst);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
GGML_ASSERT(false);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_q4_0_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK4_0, QI4_0, block_q4_0, VDR_Q4_0_Q8_1_MMVQ, vec_dot_q4_0_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_q4_1_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK4_1, QI4_1, block_q4_1, VDR_Q4_1_Q8_1_MMVQ, vec_dot_q4_1_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_q5_0_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK5_0, QI5_0, block_q5_0, VDR_Q5_0_Q8_1_MMVQ, vec_dot_q5_0_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_q5_1_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK5_1, QI5_1, block_q5_1, VDR_Q5_1_Q8_1_MMVQ, vec_dot_q5_1_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_q8_0_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK8_0, QI8_0, block_q8_0, VDR_Q8_0_Q8_1_MMVQ, vec_dot_q8_0_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_q2_K_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK_K, QI2_K, block_q2_K, VDR_Q2_K_Q8_1_MMVQ, vec_dot_q2_K_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_q3_K_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK_K, QI3_K, block_q3_K, VDR_Q3_K_Q8_1_MMVQ, vec_dot_q3_K_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_q4_K_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK_K, QI4_K, block_q4_K, VDR_Q4_K_Q8_1_MMVQ, vec_dot_q4_K_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_q5_K_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK_K, QI5_K, block_q5_K, VDR_Q5_K_Q8_1_MMVQ, vec_dot_q5_K_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_q6_K_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK_K, QI6_K, block_q6_K, VDR_Q6_K_Q8_1_MMVQ, vec_dot_q6_K_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_iq2_xxs_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK_K, QI2_XXS, block_iq2_xxs, 1, vec_dot_iq2_xxs_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_iq2_xs_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK_K, QI2_XS, block_iq2_xs, 1, vec_dot_iq2_xs_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_iq2_s_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK_K, QI2_S, block_iq2_s, 1, vec_dot_iq2_s_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_iq3_xxs_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK_K, QI3_XXS, block_iq3_xxs, 1, vec_dot_iq3_xxs_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_iq1_s_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK_K, QI1_S, block_iq1_s, 1, vec_dot_iq1_s_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_iq1_m_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK_K, QI1_S, block_iq1_m, 1, vec_dot_iq1_m_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_iq4_nl_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK4_NL, QI4_NL, block_iq4_nl, VDR_Q4_0_Q8_1_MMVQ, vec_dot_iq4_nl_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_iq4_xs_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK_K, QI4_XS, block_iq4_xs, 1, vec_dot_iq4_xs_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mul_mat_vec_iq3_s_q8_1_cuda(
|
|
||||||
const void * vx, const void * vy, float * dst,
|
|
||||||
const int ncols_x, const int nrows_x, const int nrows_y, const int ncols_y, const int nrows_dst, cudaStream_t stream) {
|
|
||||||
|
|
||||||
mul_mat_vec_q_cuda<QK_K, QI3_XS, block_iq3_s, 1, vec_dot_iq3_s_q8_1>
|
|
||||||
(vx, vy, dst, ncols_x, nrows_x, nrows_y, ncols_y, nrows_dst, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_mul_mat_vec_q(
|
|
||||||
ggml_backend_cuda_context & ctx,
|
|
||||||
const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
|
|
||||||
const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
|
|
||||||
const int64_t src1_padded_row_size, cudaStream_t stream) {
|
|
||||||
|
|
||||||
const int64_t ne00 = src0->ne[0];
|
|
||||||
const int64_t row_diff = row_high - row_low;
|
|
||||||
|
|
||||||
const int64_t ne10 = src1->ne[0];
|
|
||||||
GGML_ASSERT(ne10 % QK8_1 == 0);
|
|
||||||
|
|
||||||
const int64_t ne0 = dst->ne[0];
|
|
||||||
|
|
||||||
int id;
|
|
||||||
CUDA_CHECK(cudaGetDevice(&id));
|
|
||||||
|
|
||||||
// the main device has a larger memory buffer to hold the results from all GPUs
|
|
||||||
// nrows_dst == nrows of the matrix that the kernel writes into
|
|
||||||
const int64_t nrows_dst = id == ctx.device ? ne0 : row_diff;
|
|
||||||
|
|
||||||
switch (src0->type) {
|
|
||||||
case GGML_TYPE_Q4_0:
|
|
||||||
mul_mat_vec_q4_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q4_1:
|
|
||||||
mul_mat_vec_q4_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q5_0:
|
|
||||||
mul_mat_vec_q5_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q5_1:
|
|
||||||
mul_mat_vec_q5_1_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q8_0:
|
|
||||||
mul_mat_vec_q8_0_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q2_K:
|
|
||||||
mul_mat_vec_q2_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q3_K:
|
|
||||||
mul_mat_vec_q3_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q4_K:
|
|
||||||
mul_mat_vec_q4_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q5_K:
|
|
||||||
mul_mat_vec_q5_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_Q6_K:
|
|
||||||
mul_mat_vec_q6_K_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_IQ2_XXS:
|
|
||||||
mul_mat_vec_iq2_xxs_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_IQ2_XS:
|
|
||||||
mul_mat_vec_iq2_xs_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_IQ2_S:
|
|
||||||
mul_mat_vec_iq2_s_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_IQ3_XXS:
|
|
||||||
mul_mat_vec_iq3_xxs_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_IQ1_S:
|
|
||||||
mul_mat_vec_iq1_s_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_IQ1_M:
|
|
||||||
mul_mat_vec_iq1_m_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_IQ4_NL:
|
|
||||||
mul_mat_vec_iq4_nl_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_IQ4_XS:
|
|
||||||
mul_mat_vec_iq4_xs_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
case GGML_TYPE_IQ3_S:
|
|
||||||
mul_mat_vec_iq3_s_q8_1_cuda(src0_dd_i, src1_ddq_i, dst_dd_i, ne00, row_diff, src1_padded_row_size, src1_ncols, nrows_dst, stream);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
GGML_ASSERT(false);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
GGML_UNUSED(src1);
|
|
||||||
GGML_UNUSED(dst);
|
|
||||||
GGML_UNUSED(src1_ddf_i);
|
|
||||||
GGML_UNUSED(src1_ncols);
|
|
||||||
GGML_UNUSED(src1_padded_row_size);
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
void ggml_cuda_op_mul_mat_vec_q(
|
|
||||||
ggml_backend_cuda_context & ctx,
|
|
||||||
const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst, const char * src0_dd_i, const float * src1_ddf_i,
|
|
||||||
const char * src1_ddq_i, float * dst_dd_i, const int64_t row_low, const int64_t row_high, const int64_t src1_ncols,
|
|
||||||
const int64_t src1_padded_row_size, cudaStream_t stream);
|
|
@ -1,215 +0,0 @@
|
|||||||
#include "norm.cuh"
|
|
||||||
|
|
||||||
template <int block_size>
|
|
||||||
static __global__ void norm_f32(const float * x, float * dst, const int ncols, const float eps) {
|
|
||||||
const int row = blockIdx.x*blockDim.y + threadIdx.y;
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
|
|
||||||
float2 mean_var = make_float2(0.f, 0.f);
|
|
||||||
|
|
||||||
for (int col = tid; col < ncols; col += block_size) {
|
|
||||||
const float xi = x[row*ncols + col];
|
|
||||||
mean_var.x += xi;
|
|
||||||
mean_var.y += xi * xi;
|
|
||||||
}
|
|
||||||
|
|
||||||
// sum up partial sums
|
|
||||||
mean_var = warp_reduce_sum(mean_var);
|
|
||||||
if (block_size > WARP_SIZE) {
|
|
||||||
__shared__ float2 s_sum[32];
|
|
||||||
int warp_id = threadIdx.x / WARP_SIZE;
|
|
||||||
int lane_id = threadIdx.x % WARP_SIZE;
|
|
||||||
if (lane_id == 0) {
|
|
||||||
s_sum[warp_id] = mean_var;
|
|
||||||
}
|
|
||||||
__syncthreads();
|
|
||||||
mean_var = s_sum[lane_id];
|
|
||||||
mean_var = warp_reduce_sum(mean_var);
|
|
||||||
}
|
|
||||||
|
|
||||||
const float mean = mean_var.x / ncols;
|
|
||||||
const float var = mean_var.y / ncols - mean * mean;
|
|
||||||
const float inv_std = rsqrtf(var + eps);
|
|
||||||
|
|
||||||
for (int col = tid; col < ncols; col += block_size) {
|
|
||||||
dst[row*ncols + col] = (x[row*ncols + col] - mean) * inv_std;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <int block_size>
|
|
||||||
static __global__ void group_norm_f32(const float * x, float * dst, const int group_size, const int ne_elements, const float eps) {
|
|
||||||
// blockIdx.x: num_groups idx
|
|
||||||
// threadIdx.x: block_size idx
|
|
||||||
int start = blockIdx.x * group_size;
|
|
||||||
int end = start + group_size;
|
|
||||||
|
|
||||||
start += threadIdx.x;
|
|
||||||
|
|
||||||
if (end >= ne_elements) {
|
|
||||||
end = ne_elements;
|
|
||||||
}
|
|
||||||
|
|
||||||
float tmp = 0.0f; // partial sum for thread in warp
|
|
||||||
|
|
||||||
for (int j = start; j < end; j += block_size) {
|
|
||||||
tmp += x[j];
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp = warp_reduce_sum(tmp);
|
|
||||||
if (block_size > WARP_SIZE) {
|
|
||||||
__shared__ float s_sum[32];
|
|
||||||
int warp_id = threadIdx.x / WARP_SIZE;
|
|
||||||
int lane_id = threadIdx.x % WARP_SIZE;
|
|
||||||
if (lane_id == 0) {
|
|
||||||
s_sum[warp_id] = tmp;
|
|
||||||
}
|
|
||||||
__syncthreads();
|
|
||||||
tmp = s_sum[lane_id];
|
|
||||||
tmp = warp_reduce_sum(tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
float mean = tmp / group_size;
|
|
||||||
tmp = 0.0f;
|
|
||||||
|
|
||||||
for (int j = start; j < end; j += block_size) {
|
|
||||||
float xi = x[j] - mean;
|
|
||||||
dst[j] = xi;
|
|
||||||
tmp += xi * xi;
|
|
||||||
}
|
|
||||||
|
|
||||||
tmp = warp_reduce_sum(tmp);
|
|
||||||
if (block_size > WARP_SIZE) {
|
|
||||||
__shared__ float s_sum[32];
|
|
||||||
int warp_id = threadIdx.x / WARP_SIZE;
|
|
||||||
int lane_id = threadIdx.x % WARP_SIZE;
|
|
||||||
if (lane_id == 0) {
|
|
||||||
s_sum[warp_id] = tmp;
|
|
||||||
}
|
|
||||||
__syncthreads();
|
|
||||||
tmp = s_sum[lane_id];
|
|
||||||
tmp = warp_reduce_sum(tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
float variance = tmp / group_size;
|
|
||||||
float scale = rsqrtf(variance + eps);
|
|
||||||
for (int j = start; j < end; j += block_size) {
|
|
||||||
dst[j] *= scale;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <int block_size>
|
|
||||||
static __global__ void rms_norm_f32(const float * x, float * dst, const int ncols, const float eps) {
|
|
||||||
const int row = blockIdx.x*blockDim.y + threadIdx.y;
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
|
|
||||||
float tmp = 0.0f; // partial sum for thread in warp
|
|
||||||
|
|
||||||
for (int col = tid; col < ncols; col += block_size) {
|
|
||||||
const float xi = x[row*ncols + col];
|
|
||||||
tmp += xi * xi;
|
|
||||||
}
|
|
||||||
|
|
||||||
// sum up partial sums
|
|
||||||
tmp = warp_reduce_sum(tmp);
|
|
||||||
if (block_size > WARP_SIZE) {
|
|
||||||
__shared__ float s_sum[32];
|
|
||||||
int warp_id = threadIdx.x / WARP_SIZE;
|
|
||||||
int lane_id = threadIdx.x % WARP_SIZE;
|
|
||||||
if (lane_id == 0) {
|
|
||||||
s_sum[warp_id] = tmp;
|
|
||||||
}
|
|
||||||
__syncthreads();
|
|
||||||
tmp = s_sum[lane_id];
|
|
||||||
tmp = warp_reduce_sum(tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
const float mean = tmp / ncols;
|
|
||||||
const float scale = rsqrtf(mean + eps);
|
|
||||||
|
|
||||||
for (int col = tid; col < ncols; col += block_size) {
|
|
||||||
dst[row*ncols + col] = scale * x[row*ncols + col];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
|
|
||||||
GGML_ASSERT(ncols % WARP_SIZE == 0);
|
|
||||||
if (ncols < 1024) {
|
|
||||||
const dim3 block_dims(WARP_SIZE, 1, 1);
|
|
||||||
norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
|
|
||||||
} else {
|
|
||||||
const dim3 block_dims(1024, 1, 1);
|
|
||||||
norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void group_norm_f32_cuda(const float * x, float * dst, const int num_groups, const int group_size, const int ne_elements, cudaStream_t stream) {
|
|
||||||
static const float eps = 1e-6f;
|
|
||||||
if (group_size < 1024) {
|
|
||||||
const dim3 block_dims(WARP_SIZE, 1, 1);
|
|
||||||
group_norm_f32<WARP_SIZE><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
|
|
||||||
} else {
|
|
||||||
const dim3 block_dims(1024, 1, 1);
|
|
||||||
group_norm_f32<1024><<<num_groups, block_dims, 0, stream>>>(x, dst, group_size, ne_elements, eps);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rms_norm_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, const float eps, cudaStream_t stream) {
|
|
||||||
GGML_ASSERT(ncols % WARP_SIZE == 0);
|
|
||||||
if (ncols < 1024) {
|
|
||||||
const dim3 block_dims(WARP_SIZE, 1, 1);
|
|
||||||
rms_norm_f32<WARP_SIZE><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
|
|
||||||
} else {
|
|
||||||
const dim3 block_dims(1024, 1, 1);
|
|
||||||
rms_norm_f32<1024><<<nrows, block_dims, 0, stream>>>(x, dst, ncols, eps);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_norm(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
const float * src0_d = (const float *)src0->data;
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
GGML_ASSERT(src0->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT( dst->type == GGML_TYPE_F32);
|
|
||||||
|
|
||||||
const int64_t ne00 = src0->ne[0];
|
|
||||||
const int64_t nrows = ggml_nrows(src0);
|
|
||||||
|
|
||||||
float eps;
|
|
||||||
memcpy(&eps, dst->op_params, sizeof(float));
|
|
||||||
|
|
||||||
norm_f32_cuda(src0_d, dst_d, ne00, nrows, eps, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_group_norm(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
const float * src0_d = (const float *)src0->data;
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
GGML_ASSERT(src0->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT( dst->type == GGML_TYPE_F32);
|
|
||||||
|
|
||||||
int num_groups = dst->op_params[0];
|
|
||||||
int group_size = src0->ne[0] * src0->ne[1] * ((src0->ne[2] + num_groups - 1) / num_groups);
|
|
||||||
group_norm_f32_cuda(src0_d, dst_d, num_groups * src0->ne[3], group_size, ggml_nelements(src0), stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_rms_norm(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
const float * src0_d = (const float *)src0->data;
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
GGML_ASSERT(src0->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT( dst->type == GGML_TYPE_F32);
|
|
||||||
|
|
||||||
const int64_t ne00 = src0->ne[0];
|
|
||||||
const int64_t nrows = ggml_nrows(src0);
|
|
||||||
|
|
||||||
float eps;
|
|
||||||
memcpy(&eps, dst->op_params, sizeof(float));
|
|
||||||
|
|
||||||
rms_norm_f32_cuda(src0_d, dst_d, ne00, nrows, eps, stream);
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
void ggml_cuda_op_norm(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
||||||
|
|
||||||
void ggml_cuda_op_group_norm(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
||||||
|
|
||||||
void ggml_cuda_op_rms_norm(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
@ -1,49 +0,0 @@
|
|||||||
#include "pad.cuh"
|
|
||||||
|
|
||||||
static __global__ void pad_f32(const float * x, float * dst, const int ne0, const int ne00, const int ne01, const int ne02, const int ne03) {
|
|
||||||
// blockIdx.z: idx of ne2*ne3, aka ne02*ne03
|
|
||||||
// blockIdx.y: idx of ne1
|
|
||||||
// blockIDx.x: idx of ne0 / BLOCK_SIZE
|
|
||||||
int nidx = threadIdx.x + blockIdx.x * blockDim.x;
|
|
||||||
if (nidx >= ne0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// operation
|
|
||||||
int offset_dst =
|
|
||||||
nidx +
|
|
||||||
blockIdx.y * ne0 +
|
|
||||||
blockIdx.z * ne0 * gridDim.y;
|
|
||||||
if (nidx < ne00 && blockIdx.y < ne01 && blockIdx.z < ne02*ne03) {
|
|
||||||
int offset_src =
|
|
||||||
nidx +
|
|
||||||
blockIdx.y * ne00 +
|
|
||||||
blockIdx.z * ne00 * ne01;
|
|
||||||
dst[offset_dst] = x[offset_src];
|
|
||||||
} else {
|
|
||||||
dst[offset_dst] = 0.0f;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void pad_f32_cuda(const float * x, float * dst,
|
|
||||||
const int ne00, const int ne01, const int ne02, const int ne03,
|
|
||||||
const int ne0, const int ne1, const int ne2, const int ne3, cudaStream_t stream) {
|
|
||||||
int num_blocks = (ne0 + CUDA_PAD_BLOCK_SIZE - 1) / CUDA_PAD_BLOCK_SIZE;
|
|
||||||
dim3 gridDim(num_blocks, ne1, ne2*ne3);
|
|
||||||
pad_f32<<<gridDim, CUDA_PAD_BLOCK_SIZE, 0, stream>>>(x, dst, ne0, ne00, ne01, ne02, ne03);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_pad(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
const float * src0_d = (const float *)src0->data;
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
GGML_ASSERT(src0->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT(dst->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT(src0->ne[3] == 1 && dst->ne[3] == 1); // just 3D tensors
|
|
||||||
|
|
||||||
pad_f32_cuda(src0_d, dst_d,
|
|
||||||
src0->ne[0], src0->ne[1], src0->ne[2], src0->ne[3],
|
|
||||||
dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3], stream);
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
#define CUDA_PAD_BLOCK_SIZE 256
|
|
||||||
|
|
||||||
void ggml_cuda_op_pad(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
@ -1,94 +0,0 @@
|
|||||||
#include "pool2d.cuh"
|
|
||||||
|
|
||||||
template <typename Ti, typename To>
|
|
||||||
static __global__ void pool2d_nchw_kernel(
|
|
||||||
const int ih, const int iw, const int oh, const int ow,
|
|
||||||
const int kh, const int kw, const int sh, const int sw,
|
|
||||||
const int ph, const int pw, const int parallel_elements,
|
|
||||||
const Ti* src, To* dst, const enum ggml_op_pool op) {
|
|
||||||
int idx = threadIdx.x + blockIdx.x * blockDim.x;
|
|
||||||
if (idx >= parallel_elements) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int I_HW = ih * iw;
|
|
||||||
const int O_HW = oh * ow;
|
|
||||||
const int nc = idx / O_HW;
|
|
||||||
const int cur_oh = idx % O_HW / ow;
|
|
||||||
const int cur_ow = idx % O_HW % ow;
|
|
||||||
const Ti* i_ptr = src + nc * I_HW;
|
|
||||||
To* o_ptr = dst + nc * O_HW;
|
|
||||||
const int start_h = cur_oh * sh - ph;
|
|
||||||
const int bh = max(0, start_h);
|
|
||||||
const int eh = min(ih, start_h + kh);
|
|
||||||
const int start_w = cur_ow * sw - pw;
|
|
||||||
const int bw = max(0, start_w);
|
|
||||||
const int ew = min(iw, start_w + kw);
|
|
||||||
const To scale = 1. / (kh * kw);
|
|
||||||
To res = 0;
|
|
||||||
|
|
||||||
switch (op) {
|
|
||||||
case GGML_OP_POOL_AVG: res = 0; break;
|
|
||||||
case GGML_OP_POOL_MAX: res = -FLT_MAX; break;
|
|
||||||
default: assert(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = bh; i < eh; i += 1) {
|
|
||||||
for (int j = bw; j < ew; j += 1) {
|
|
||||||
#if __CUDA_ARCH__ >= 350
|
|
||||||
Ti cur = __ldg(i_ptr + i * iw + j);
|
|
||||||
#else
|
|
||||||
Ti cur = i_ptr[i * iw + j];
|
|
||||||
#endif
|
|
||||||
switch (op) {
|
|
||||||
case GGML_OP_POOL_AVG: res += cur * scale; break;
|
|
||||||
case GGML_OP_POOL_MAX: res = max(res, (To)cur); break;
|
|
||||||
default: assert(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
o_ptr[cur_oh * ow + cur_ow] = res;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void pool2d_nchw_kernel_f32_f32_cuda(
|
|
||||||
const int ih, const int iw, const int oh, const int ow,
|
|
||||||
const int kh, const int kw, const int sh, const int sw,
|
|
||||||
const int ph, const int pw, const int parallel_elements,
|
|
||||||
const float * src, float * dst, const enum ggml_op_pool op,
|
|
||||||
cudaStream_t stream) {
|
|
||||||
|
|
||||||
const int num_blocks = (parallel_elements + CUDA_POOL2D_BLOCK_SIZE - 1) / CUDA_POOL2D_BLOCK_SIZE;
|
|
||||||
dim3 block_nums(num_blocks);
|
|
||||||
pool2d_nchw_kernel<<<block_nums, CUDA_POOL2D_BLOCK_SIZE, 0, stream>>>(ih, iw, oh, ow, kh, kw, sh, sw, ph, pw, parallel_elements, src, dst, op);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_pool2d(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
const float * src0_d = (const float *)src0->data;
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
GGML_ASSERT(src0->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT( dst->type == GGML_TYPE_F32);
|
|
||||||
|
|
||||||
const int32_t * opts = (const int32_t *)dst->op_params;
|
|
||||||
enum ggml_op_pool op = static_cast<ggml_op_pool>(opts[0]);
|
|
||||||
const int k0 = opts[1];
|
|
||||||
const int k1 = opts[2];
|
|
||||||
const int s0 = opts[3];
|
|
||||||
const int s1 = opts[4];
|
|
||||||
const int p0 = opts[5];
|
|
||||||
const int p1 = opts[6];
|
|
||||||
|
|
||||||
const int64_t IH = src0->ne[1];
|
|
||||||
const int64_t IW = src0->ne[0];
|
|
||||||
|
|
||||||
const int64_t N = dst->ne[3];
|
|
||||||
const int64_t OC = dst->ne[2];
|
|
||||||
const int64_t OH = dst->ne[1];
|
|
||||||
const int64_t OW = dst->ne[0];
|
|
||||||
|
|
||||||
const int parallel_elements = N * OC * OH * OW;
|
|
||||||
|
|
||||||
pool2d_nchw_kernel_f32_f32_cuda(IH, IW, OH, OW, k1, k0, s1, s0, p1, p0, parallel_elements, src0_d, dst_d, op, stream);
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
#define CUDA_POOL2D_BLOCK_SIZE 256
|
|
||||||
|
|
||||||
void ggml_cuda_op_pool2d(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
@ -1,45 +0,0 @@
|
|||||||
#include "quantize.cuh"
|
|
||||||
|
|
||||||
static __global__ void quantize_q8_1(const float * __restrict__ x, void * __restrict__ vy, const int64_t kx, const int64_t kx_padded) {
|
|
||||||
const int64_t ix = (int64_t)blockDim.x*blockIdx.x + threadIdx.x;
|
|
||||||
|
|
||||||
if (ix >= kx_padded) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int64_t iy = (int64_t)blockDim.y*blockIdx.y + threadIdx.y;
|
|
||||||
|
|
||||||
const int64_t i_padded = (int64_t)iy*kx_padded + ix;
|
|
||||||
|
|
||||||
block_q8_1 * y = (block_q8_1 *) vy;
|
|
||||||
|
|
||||||
const int64_t ib = i_padded / QK8_1; // block index
|
|
||||||
const int64_t iqs = i_padded % QK8_1; // quant index
|
|
||||||
|
|
||||||
const float xi = ix < kx ? x[iy*kx + ix] : 0.0f;
|
|
||||||
float amax = fabsf(xi);
|
|
||||||
float sum = xi;
|
|
||||||
|
|
||||||
amax = warp_reduce_max(amax);
|
|
||||||
sum = warp_reduce_sum(sum);
|
|
||||||
|
|
||||||
const float d = amax / 127;
|
|
||||||
const int8_t q = amax == 0.0f ? 0 : roundf(xi / d);
|
|
||||||
|
|
||||||
y[ib].qs[iqs] = q;
|
|
||||||
|
|
||||||
if (iqs > 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
reinterpret_cast<half&>(y[ib].ds.x) = d;
|
|
||||||
reinterpret_cast<half&>(y[ib].ds.y) = sum;
|
|
||||||
}
|
|
||||||
|
|
||||||
void quantize_row_q8_1_cuda(const float * x, void * vy, const int64_t kx, const int64_t ky, const int64_t kx_padded, cudaStream_t stream) {
|
|
||||||
const int64_t block_num_x = (kx_padded + CUDA_QUANTIZE_BLOCK_SIZE - 1) / CUDA_QUANTIZE_BLOCK_SIZE;
|
|
||||||
const dim3 num_blocks(block_num_x, ky, 1);
|
|
||||||
const dim3 block_size(CUDA_QUANTIZE_BLOCK_SIZE, 1, 1);
|
|
||||||
quantize_q8_1<<<num_blocks, block_size, 0, stream>>>(x, vy, kx, kx_padded);
|
|
||||||
}
|
|
||||||
|
|
@ -1,5 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
#define CUDA_QUANTIZE_BLOCK_SIZE 256
|
|
||||||
|
|
||||||
void quantize_row_q8_1_cuda(const float * x, void * vy, const int64_t kx, const int64_t ky, const int64_t kx_padded, cudaStream_t stream);
|
|
@ -1,308 +0,0 @@
|
|||||||
#include "rope.cuh"
|
|
||||||
|
|
||||||
struct rope_corr_dims {
|
|
||||||
float v[4];
|
|
||||||
};
|
|
||||||
|
|
||||||
static __device__ float rope_yarn_ramp(const float low, const float high, const int i0) {
|
|
||||||
const float y = (i0 / 2 - low) / max(0.001f, high - low);
|
|
||||||
return 1.0f - min(1.0f, max(0.0f, y));
|
|
||||||
}
|
|
||||||
|
|
||||||
// YaRN algorithm based on LlamaYaRNScaledRotaryEmbedding.py from https://github.com/jquesnelle/yarn
|
|
||||||
// MIT licensed. Copyright (c) 2023 Jeffrey Quesnelle and Bowen Peng.
|
|
||||||
static __device__ void rope_yarn(
|
|
||||||
float theta_extrap, float freq_scale, rope_corr_dims corr_dims, int64_t i0, float ext_factor, float mscale,
|
|
||||||
float * cos_theta, float * sin_theta
|
|
||||||
) {
|
|
||||||
// Get n-d rotational scaling corrected for extrapolation
|
|
||||||
float theta_interp = freq_scale * theta_extrap;
|
|
||||||
float theta = theta_interp;
|
|
||||||
if (ext_factor != 0.0f) {
|
|
||||||
float ramp_mix = rope_yarn_ramp(corr_dims.v[0], corr_dims.v[1], i0) * ext_factor;
|
|
||||||
theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
|
|
||||||
|
|
||||||
// Get n-d magnitude scaling corrected for interpolation
|
|
||||||
mscale *= 1.0f + 0.1f * logf(1.0f / freq_scale);
|
|
||||||
}
|
|
||||||
*cos_theta = cosf(theta) * mscale;
|
|
||||||
*sin_theta = sinf(theta) * mscale;
|
|
||||||
}
|
|
||||||
|
|
||||||
// rope == RoPE == rotary positional embedding
|
|
||||||
template<typename T, bool has_pos>
|
|
||||||
static __global__ void rope(
|
|
||||||
const T * x, T * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
|
|
||||||
float ext_factor, float attn_factor, rope_corr_dims corr_dims
|
|
||||||
) {
|
|
||||||
const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
|
|
||||||
|
|
||||||
if (col >= ncols) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int row = blockDim.x*blockIdx.x + threadIdx.x;
|
|
||||||
const int i = row*ncols + col;
|
|
||||||
const int i2 = row/p_delta_rows;
|
|
||||||
|
|
||||||
const int p = has_pos ? pos[i2] : 0;
|
|
||||||
const float theta_base = p*powf(freq_base, -float(col)/ncols);
|
|
||||||
|
|
||||||
float cos_theta, sin_theta;
|
|
||||||
rope_yarn(theta_base, freq_scale, corr_dims, col, ext_factor, attn_factor, &cos_theta, &sin_theta);
|
|
||||||
|
|
||||||
const float x0 = x[i + 0];
|
|
||||||
const float x1 = x[i + 1];
|
|
||||||
|
|
||||||
dst[i + 0] = x0*cos_theta - x1*sin_theta;
|
|
||||||
dst[i + 1] = x0*sin_theta + x1*cos_theta;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, bool has_pos>
|
|
||||||
static __global__ void rope_neox(
|
|
||||||
const T * x, T * dst, int ncols, int n_dims, const int32_t * pos, float freq_scale, int p_delta_rows,
|
|
||||||
float ext_factor, float attn_factor, rope_corr_dims corr_dims, float theta_scale, float inv_ndims
|
|
||||||
) {
|
|
||||||
const int col = 2*(blockDim.y*blockIdx.y + threadIdx.y);
|
|
||||||
|
|
||||||
if (col >= ncols) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int row = blockDim.x*blockIdx.x + threadIdx.x;
|
|
||||||
const int ib = col / n_dims;
|
|
||||||
const int ic = col % n_dims;
|
|
||||||
|
|
||||||
if (ib > 0) {
|
|
||||||
const int i = row*ncols + ib*n_dims + ic;
|
|
||||||
|
|
||||||
dst[i + 0] = x[i + 0];
|
|
||||||
dst[i + 1] = x[i + 1];
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int i = row*ncols + ib*n_dims + ic/2;
|
|
||||||
const int i2 = row/p_delta_rows;
|
|
||||||
|
|
||||||
float cur_rot = inv_ndims * ic - ib;
|
|
||||||
|
|
||||||
const int p = has_pos ? pos[i2] : 0;
|
|
||||||
const float theta_base = p*freq_scale*powf(theta_scale, col/2.0f);
|
|
||||||
|
|
||||||
float cos_theta, sin_theta;
|
|
||||||
rope_yarn(theta_base, freq_scale, corr_dims, cur_rot, ext_factor, attn_factor, &cos_theta, &sin_theta);
|
|
||||||
|
|
||||||
const float x0 = x[i + 0];
|
|
||||||
const float x1 = x[i + n_dims/2];
|
|
||||||
|
|
||||||
dst[i + 0] = x0*cos_theta - x1*sin_theta;
|
|
||||||
dst[i + n_dims/2] = x0*sin_theta + x1*cos_theta;
|
|
||||||
}
|
|
||||||
|
|
||||||
static __global__ void rope_glm_f32(
|
|
||||||
const float * x, float * dst, int ncols, const int32_t * pos, float freq_scale, int p_delta_rows, float freq_base,
|
|
||||||
int n_ctx
|
|
||||||
) {
|
|
||||||
const int col = blockDim.x*blockIdx.x + threadIdx.x;
|
|
||||||
const int half_n_dims = ncols/4;
|
|
||||||
|
|
||||||
if (col >= half_n_dims) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int row = blockDim.y*blockIdx.y + threadIdx.y;
|
|
||||||
const int i = row*ncols + col;
|
|
||||||
const int i2 = row/p_delta_rows;
|
|
||||||
|
|
||||||
const float col_theta_scale = powf(freq_base, -2.0f*col/ncols);
|
|
||||||
// FIXME: this is likely wrong
|
|
||||||
const int p = pos != nullptr ? pos[i2] : 0;
|
|
||||||
|
|
||||||
const float theta = min(p, n_ctx - 2)*freq_scale*col_theta_scale;
|
|
||||||
const float sin_theta = sinf(theta);
|
|
||||||
const float cos_theta = cosf(theta);
|
|
||||||
|
|
||||||
const float x0 = x[i + 0];
|
|
||||||
const float x1 = x[i + half_n_dims];
|
|
||||||
|
|
||||||
dst[i + 0] = x0*cos_theta - x1*sin_theta;
|
|
||||||
dst[i + half_n_dims] = x0*sin_theta + x1*cos_theta;
|
|
||||||
|
|
||||||
const float block_theta = ((float)max(p - n_ctx - 2, 0))*col_theta_scale;
|
|
||||||
const float sin_block_theta = sinf(block_theta);
|
|
||||||
const float cos_block_theta = cosf(block_theta);
|
|
||||||
|
|
||||||
const float x2 = x[i + half_n_dims * 2];
|
|
||||||
const float x3 = x[i + half_n_dims * 3];
|
|
||||||
|
|
||||||
dst[i + half_n_dims * 2] = x2*cos_block_theta - x3*sin_block_theta;
|
|
||||||
dst[i + half_n_dims * 3] = x2*sin_block_theta + x3*cos_block_theta;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
static void rope_cuda(
|
|
||||||
const T * x, T * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
|
|
||||||
float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
|
|
||||||
) {
|
|
||||||
GGML_ASSERT(ncols % 2 == 0);
|
|
||||||
const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
|
|
||||||
const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
|
|
||||||
const dim3 block_nums(nrows, num_blocks_x, 1);
|
|
||||||
if (pos == nullptr) {
|
|
||||||
rope<T, false><<<block_nums, block_dims, 0, stream>>>(
|
|
||||||
x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
rope<T, true><<<block_nums, block_dims, 0, stream>>>(
|
|
||||||
x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
static void rope_neox_cuda(
|
|
||||||
const T * x, T * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
|
|
||||||
float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
|
|
||||||
) {
|
|
||||||
GGML_ASSERT(ncols % 2 == 0);
|
|
||||||
const dim3 block_dims(1, CUDA_ROPE_BLOCK_SIZE, 1);
|
|
||||||
const int num_blocks_x = (ncols + 2*CUDA_ROPE_BLOCK_SIZE - 1) / (2*CUDA_ROPE_BLOCK_SIZE);
|
|
||||||
const dim3 block_nums(nrows, num_blocks_x, 1);
|
|
||||||
|
|
||||||
const float theta_scale = powf(freq_base, -2.0f/n_dims);
|
|
||||||
const float inv_ndims = -1.0f / n_dims;
|
|
||||||
|
|
||||||
if (pos == nullptr) {
|
|
||||||
rope_neox<T, false><<<block_nums, block_dims, 0, stream>>>(
|
|
||||||
x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
|
|
||||||
theta_scale, inv_ndims
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
rope_neox<T, true><<<block_nums, block_dims, 0, stream>>>(
|
|
||||||
x, dst, ncols, n_dims, pos, freq_scale, p_delta_rows, ext_factor, attn_factor, corr_dims,
|
|
||||||
theta_scale, inv_ndims
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rope_glm_f32_cuda(
|
|
||||||
const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
|
|
||||||
float freq_base, int n_ctx, cudaStream_t stream
|
|
||||||
) {
|
|
||||||
GGML_ASSERT(ncols % 4 == 0);
|
|
||||||
const dim3 block_dims(CUDA_ROPE_BLOCK_SIZE/4, 1, 1);
|
|
||||||
const int num_blocks_x = (ncols + CUDA_ROPE_BLOCK_SIZE - 1) / CUDA_ROPE_BLOCK_SIZE;
|
|
||||||
const dim3 block_nums(num_blocks_x, nrows, 1);
|
|
||||||
rope_glm_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols, pos, freq_scale, p_delta_rows, freq_base, n_ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rope_cuda_f16(
|
|
||||||
const half * x, half * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
|
|
||||||
float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream) {
|
|
||||||
|
|
||||||
rope_cuda<half>(x, dst, ncols, nrows, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rope_cuda_f32(
|
|
||||||
const float * x, float * dst, int ncols, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
|
|
||||||
float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream) {
|
|
||||||
|
|
||||||
rope_cuda<float>(x, dst, ncols, nrows, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rope_neox_cuda_f16(
|
|
||||||
const half * x, half * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
|
|
||||||
float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream) {
|
|
||||||
|
|
||||||
rope_neox_cuda<half>(x, dst, ncols, n_dims, nrows, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rope_neox_cuda_f32(
|
|
||||||
const float * x, float * dst, int ncols, int n_dims, int nrows, const int32_t * pos, float freq_scale, int p_delta_rows,
|
|
||||||
float freq_base, float ext_factor, float attn_factor, rope_corr_dims corr_dims, cudaStream_t stream
|
|
||||||
) {
|
|
||||||
|
|
||||||
rope_neox_cuda<float>(x, dst, ncols, n_dims, nrows, pos, freq_scale, p_delta_rows, freq_base, ext_factor, attn_factor, corr_dims, stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_rope(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
const ggml_tensor * src1 = dst->src[1];
|
|
||||||
const float * src0_d = (const float *)src0->data;
|
|
||||||
const float * src1_d = (const float *)src1->data;
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
GGML_ASSERT(src0->type == GGML_TYPE_F32 || src0->type == GGML_TYPE_F16);
|
|
||||||
GGML_ASSERT( dst->type == GGML_TYPE_F32 || dst->type == GGML_TYPE_F16);
|
|
||||||
GGML_ASSERT(src0->type == dst->type);
|
|
||||||
|
|
||||||
const int64_t ne00 = src0->ne[0];
|
|
||||||
const int64_t ne01 = src0->ne[1];
|
|
||||||
const int64_t ne2 = dst->ne[2];
|
|
||||||
const int64_t nrows = ggml_nrows(src0);
|
|
||||||
|
|
||||||
//const int n_past = ((int32_t *) dst->op_params)[0];
|
|
||||||
const int n_dims = ((int32_t *) dst->op_params)[1];
|
|
||||||
const int mode = ((int32_t *) dst->op_params)[2];
|
|
||||||
const int n_ctx = ((int32_t *) dst->op_params)[3];
|
|
||||||
const int n_orig_ctx = ((int32_t *) dst->op_params)[4];
|
|
||||||
|
|
||||||
// RoPE alteration for extended context
|
|
||||||
float freq_base, freq_scale, ext_factor, attn_factor, beta_fast, beta_slow;
|
|
||||||
memcpy(&freq_base, (int32_t *) dst->op_params + 5, sizeof(float));
|
|
||||||
memcpy(&freq_scale, (int32_t *) dst->op_params + 6, sizeof(float));
|
|
||||||
memcpy(&ext_factor, (int32_t *) dst->op_params + 7, sizeof(float));
|
|
||||||
memcpy(&attn_factor, (int32_t *) dst->op_params + 8, sizeof(float));
|
|
||||||
memcpy(&beta_fast, (int32_t *) dst->op_params + 9, sizeof(float));
|
|
||||||
memcpy(&beta_slow, (int32_t *) dst->op_params + 10, sizeof(float));
|
|
||||||
|
|
||||||
const int32_t * pos = nullptr;
|
|
||||||
if ((mode & 1) == 0) {
|
|
||||||
GGML_ASSERT(src1->type == GGML_TYPE_I32);
|
|
||||||
GGML_ASSERT(src1->ne[0] == ne2);
|
|
||||||
pos = (const int32_t *) src1_d;
|
|
||||||
}
|
|
||||||
|
|
||||||
const bool is_neox = mode & 2;
|
|
||||||
const bool is_glm = mode & 4;
|
|
||||||
|
|
||||||
rope_corr_dims corr_dims;
|
|
||||||
ggml_rope_yarn_corr_dims(n_dims, n_orig_ctx, freq_base, beta_fast, beta_slow, corr_dims.v);
|
|
||||||
|
|
||||||
// compute
|
|
||||||
if (is_glm) {
|
|
||||||
GGML_ASSERT(false);
|
|
||||||
rope_glm_f32_cuda(src0_d, dst_d, ne00, nrows, pos, freq_scale, ne01, freq_base, n_ctx, stream);
|
|
||||||
} else if (is_neox) {
|
|
||||||
if (src0->type == GGML_TYPE_F32) {
|
|
||||||
rope_neox_cuda_f32(
|
|
||||||
(const float *)src0_d, (float *)dst_d, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
|
|
||||||
attn_factor, corr_dims, stream
|
|
||||||
);
|
|
||||||
} else if (src0->type == GGML_TYPE_F16) {
|
|
||||||
rope_neox_cuda_f16(
|
|
||||||
(const half *)src0_d, (half *)dst_d, ne00, n_dims, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
|
|
||||||
attn_factor, corr_dims, stream
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
GGML_ASSERT(false);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (src0->type == GGML_TYPE_F32) {
|
|
||||||
rope_cuda_f32(
|
|
||||||
(const float *)src0_d, (float *)dst_d, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
|
|
||||||
attn_factor, corr_dims, stream
|
|
||||||
);
|
|
||||||
} else if (src0->type == GGML_TYPE_F16) {
|
|
||||||
rope_cuda_f16(
|
|
||||||
(const half *)src0_d, (half *)dst_d, ne00, nrows, pos, freq_scale, ne01, freq_base, ext_factor,
|
|
||||||
attn_factor, corr_dims, stream
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
GGML_ASSERT(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
#define CUDA_ROPE_BLOCK_SIZE 256
|
|
||||||
|
|
||||||
void ggml_cuda_op_rope(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
@ -1,32 +0,0 @@
|
|||||||
#include "scale.cuh"
|
|
||||||
|
|
||||||
static __global__ void scale_f32(const float * x, float * dst, const float scale, const int k) {
|
|
||||||
const int i = blockDim.x*blockIdx.x + threadIdx.x;
|
|
||||||
|
|
||||||
if (i >= k) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
dst[i] = scale * x[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
static void scale_f32_cuda(const float * x, float * dst, const float scale, const int k, cudaStream_t stream) {
|
|
||||||
const int num_blocks = (k + CUDA_SCALE_BLOCK_SIZE - 1) / CUDA_SCALE_BLOCK_SIZE;
|
|
||||||
scale_f32<<<num_blocks, CUDA_SCALE_BLOCK_SIZE, 0, stream>>>(x, dst, scale, k);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_scale(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
const float * src0_d = (const float *)src0->data;
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
GGML_ASSERT(src0->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT( dst->type == GGML_TYPE_F32);
|
|
||||||
|
|
||||||
float scale;
|
|
||||||
memcpy(&scale, dst->op_params, sizeof(float));
|
|
||||||
|
|
||||||
scale_f32_cuda(src0_d, dst_d, scale, ggml_nelements(src0), stream);
|
|
||||||
CUDA_CHECK(cudaGetLastError());
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
#define CUDA_SCALE_BLOCK_SIZE 256
|
|
||||||
|
|
||||||
void ggml_cuda_op_scale(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
@ -1,201 +0,0 @@
|
|||||||
#include "softmax.cuh"
|
|
||||||
|
|
||||||
template <bool vals_smem, int ncols_template, int block_size_template>
|
|
||||||
static __global__ void soft_max_f32(const float * x, const float * mask, const float * pos, float * dst, const int ncols_par, const int nrows_y, const float scale, const float max_bias, const float m0, const float m1, uint32_t n_head_log2) {
|
|
||||||
const int ncols = ncols_template == 0 ? ncols_par : ncols_template;
|
|
||||||
|
|
||||||
const int tid = threadIdx.x;
|
|
||||||
const int rowx = blockIdx.x;
|
|
||||||
const int rowy = rowx % nrows_y; // broadcast the mask in the row dimension
|
|
||||||
|
|
||||||
const int block_size = block_size_template == 0 ? blockDim.x : block_size_template;
|
|
||||||
|
|
||||||
const int warp_id = threadIdx.x / WARP_SIZE;
|
|
||||||
const int lane_id = threadIdx.x % WARP_SIZE;
|
|
||||||
|
|
||||||
float slope = 0.0f;
|
|
||||||
|
|
||||||
// ALiBi
|
|
||||||
if (max_bias > 0.0f) {
|
|
||||||
const int h = rowx/nrows_y; // head index
|
|
||||||
|
|
||||||
const float base = h < n_head_log2 ? m0 : m1;
|
|
||||||
const int exp = h < n_head_log2 ? h + 1 : 2*(h - n_head_log2) + 1;
|
|
||||||
|
|
||||||
slope = powf(base, exp);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern __shared__ float data_soft_max_f32[];
|
|
||||||
float * buf_iw = data_soft_max_f32; // shared memory buffer for inter-warp communication
|
|
||||||
// shared memory buffer to cache values between iterations:
|
|
||||||
float * vals = vals_smem ? buf_iw + WARP_SIZE : dst + rowx*ncols;
|
|
||||||
|
|
||||||
float max_val = -INFINITY;
|
|
||||||
|
|
||||||
#pragma unroll
|
|
||||||
for (int col0 = 0; col0 < ncols; col0 += block_size) {
|
|
||||||
const int col = col0 + tid;
|
|
||||||
|
|
||||||
if (ncols_template == 0 && col >= ncols) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int ix = rowx*ncols + col;
|
|
||||||
const int iy = rowy*ncols + col;
|
|
||||||
|
|
||||||
const float val = x[ix]*scale + (mask ? mask[iy] : 0.0f) + (pos ? slope*pos[col] : 0.0f);
|
|
||||||
|
|
||||||
vals[col] = val;
|
|
||||||
max_val = max(max_val, val);
|
|
||||||
}
|
|
||||||
|
|
||||||
// find the max value in the block
|
|
||||||
max_val = warp_reduce_max(max_val);
|
|
||||||
if (block_size > WARP_SIZE) {
|
|
||||||
if (warp_id == 0) {
|
|
||||||
buf_iw[lane_id] = -INFINITY;
|
|
||||||
}
|
|
||||||
__syncthreads();
|
|
||||||
|
|
||||||
if (lane_id == 0) {
|
|
||||||
buf_iw[warp_id] = max_val;
|
|
||||||
}
|
|
||||||
__syncthreads();
|
|
||||||
|
|
||||||
max_val = buf_iw[lane_id];
|
|
||||||
max_val = warp_reduce_max(max_val);
|
|
||||||
}
|
|
||||||
|
|
||||||
float tmp = 0.0f; // partial sum
|
|
||||||
|
|
||||||
#pragma unroll
|
|
||||||
for (int col0 = 0; col0 < ncols; col0 += block_size) {
|
|
||||||
const int col = col0 + tid;
|
|
||||||
|
|
||||||
if (ncols_template == 0 && col >= ncols) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
const float val = expf(vals[col] - max_val);
|
|
||||||
tmp += val;
|
|
||||||
vals[col] = val;
|
|
||||||
}
|
|
||||||
|
|
||||||
// find the sum of exps in the block
|
|
||||||
tmp = warp_reduce_sum(tmp);
|
|
||||||
if (block_size > WARP_SIZE) {
|
|
||||||
__syncthreads();
|
|
||||||
if (warp_id == 0) {
|
|
||||||
buf_iw[lane_id] = 0.0f;
|
|
||||||
}
|
|
||||||
__syncthreads();
|
|
||||||
|
|
||||||
if (lane_id == 0) {
|
|
||||||
buf_iw[warp_id] = tmp;
|
|
||||||
}
|
|
||||||
__syncthreads();
|
|
||||||
|
|
||||||
tmp = buf_iw[lane_id];
|
|
||||||
tmp = warp_reduce_sum(tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
const float inv_sum = 1.0f / tmp;
|
|
||||||
|
|
||||||
#pragma unroll
|
|
||||||
for (int col0 = 0; col0 < ncols; col0 += block_size) {
|
|
||||||
const int col = col0 + tid;
|
|
||||||
|
|
||||||
if (ncols_template == 0 && col >= ncols) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int idst = rowx*ncols + col;
|
|
||||||
dst[idst] = vals[col] * inv_sum;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void soft_max_f32_cuda(const float * x, const float * mask, const float * pos, float * dst, const int ncols_x, const int nrows_x, const int nrows_y, const float scale, const float max_bias, cudaStream_t stream) {
|
|
||||||
int nth = WARP_SIZE;
|
|
||||||
while (nth < ncols_x && nth < CUDA_SOFT_MAX_BLOCK_SIZE) nth *= 2;
|
|
||||||
const dim3 block_dims(nth, 1, 1);
|
|
||||||
const dim3 block_nums(nrows_x, 1, 1);
|
|
||||||
const size_t shmem = (GGML_PAD(ncols_x, WARP_SIZE) + WARP_SIZE)*sizeof(float);
|
|
||||||
static_assert(CUDA_SOFT_MAX_BLOCK_SIZE == 1024, "These values need to be adjusted.");
|
|
||||||
|
|
||||||
const uint32_t n_head_kv = nrows_x/nrows_y;
|
|
||||||
const uint32_t n_head_log2 = 1u << (uint32_t) floorf(log2f((float) n_head_kv));
|
|
||||||
|
|
||||||
const float m0 = powf(2.0f, -(max_bias ) / n_head_log2);
|
|
||||||
const float m1 = powf(2.0f, -(max_bias / 2.0f) / n_head_log2);
|
|
||||||
|
|
||||||
if (shmem < ggml_cuda_info().devices[ggml_cuda_get_device()].smpb) {
|
|
||||||
switch (ncols_x) {
|
|
||||||
case 32:
|
|
||||||
soft_max_f32<true, 32, 32><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
|
|
||||||
break;
|
|
||||||
case 64:
|
|
||||||
soft_max_f32<true, 64, 64><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
|
|
||||||
break;
|
|
||||||
case 128:
|
|
||||||
soft_max_f32<true, 128, 128><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
|
|
||||||
break;
|
|
||||||
case 256:
|
|
||||||
soft_max_f32<true, 256, 256><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
|
|
||||||
break;
|
|
||||||
case 512:
|
|
||||||
soft_max_f32<true, 512, 512><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
|
|
||||||
break;
|
|
||||||
case 1024:
|
|
||||||
soft_max_f32<true, 1024, 1024><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
|
|
||||||
break;
|
|
||||||
case 2048:
|
|
||||||
soft_max_f32<true, 2048, 1024><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
|
|
||||||
break;
|
|
||||||
case 4096:
|
|
||||||
soft_max_f32<true, 4096, 1024><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
soft_max_f32<true, 0, 0><<<block_nums, block_dims, shmem, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
const size_t shmem_low = WARP_SIZE*sizeof(float);
|
|
||||||
soft_max_f32<false, 0, 0><<<block_nums, block_dims, shmem_low, stream>>>(x, mask, pos, dst, ncols_x, nrows_y, scale, max_bias, m0, m1, n_head_log2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_soft_max(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
const ggml_tensor * src1 = dst->src[1];
|
|
||||||
const float * src0_d = (const float *)src0->data;
|
|
||||||
const float * src1_d = src1 ? (const float *)src1->data : nullptr;
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
GGML_ASSERT(src0->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT( dst->type == GGML_TYPE_F32);
|
|
||||||
|
|
||||||
GGML_ASSERT(!src1 || src1->type == GGML_TYPE_F32); // src1 contains mask and it is optional
|
|
||||||
|
|
||||||
const int64_t ne00 = src0->ne[0];
|
|
||||||
const int64_t nrows_x = ggml_nrows(src0);
|
|
||||||
const int64_t nrows_y = src0->ne[1];
|
|
||||||
|
|
||||||
float scale = 1.0f;
|
|
||||||
float max_bias = 0.0f;
|
|
||||||
|
|
||||||
memcpy(&scale, (float *) dst->op_params + 0, sizeof(float));
|
|
||||||
memcpy(&max_bias, (float *) dst->op_params + 1, sizeof(float));
|
|
||||||
|
|
||||||
// positions tensor
|
|
||||||
float * src2_dd = nullptr;
|
|
||||||
|
|
||||||
ggml_tensor * src2 = dst->src[2];
|
|
||||||
const bool use_src2 = src2 != nullptr;
|
|
||||||
|
|
||||||
if (use_src2) {
|
|
||||||
src2_dd = (float *)src2->data;
|
|
||||||
}
|
|
||||||
|
|
||||||
soft_max_f32_cuda(src0_d, src1_d, src2_dd, dst_d, ne00, nrows_x, nrows_y, scale, max_bias, stream);
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
#define CUDA_SOFT_MAX_BLOCK_SIZE 1024
|
|
||||||
|
|
||||||
void ggml_cuda_op_soft_max(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
@ -1,40 +0,0 @@
|
|||||||
#include "sumrows.cuh"
|
|
||||||
|
|
||||||
static __global__ void k_sum_rows_f32(const float * x, float * dst, const int ncols) {
|
|
||||||
const int row = blockIdx.x;
|
|
||||||
const int col = threadIdx.x;
|
|
||||||
|
|
||||||
float sum = 0.0f;
|
|
||||||
for (int i = col; i < ncols; i += blockDim.x) {
|
|
||||||
sum += x[row * ncols + i];
|
|
||||||
}
|
|
||||||
|
|
||||||
sum = warp_reduce_sum(sum);
|
|
||||||
|
|
||||||
if (col == 0) {
|
|
||||||
dst[row] = sum;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void sum_rows_f32_cuda(const float * x, float * dst, const int ncols, const int nrows, cudaStream_t stream) {
|
|
||||||
const dim3 block_dims(WARP_SIZE, 1, 1);
|
|
||||||
const dim3 block_nums(nrows, 1, 1);
|
|
||||||
k_sum_rows_f32<<<block_nums, block_dims, 0, stream>>>(x, dst, ncols);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_sum_rows(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
const float * src0_d = (const float *)src0->data;
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
GGML_ASSERT(src0->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT( dst->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT(ggml_is_contiguous(src0));
|
|
||||||
|
|
||||||
|
|
||||||
const int64_t ncols = src0->ne[0];
|
|
||||||
const int64_t nrows = ggml_nrows(src0);
|
|
||||||
|
|
||||||
sum_rows_f32_cuda(src0_d, dst_d, ncols, nrows, stream);
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
#include "common.cuh"
|
|
||||||
|
|
||||||
void ggml_cuda_op_sum_rows(ggml_backend_cuda_context & ctx, ggml_tensor * dst);
|
|
@ -1,47 +0,0 @@
|
|||||||
#include "tsembd.cuh"
|
|
||||||
|
|
||||||
static __global__ void timestep_embedding_f32(const float * timesteps, float * dst, const int nb1, const int dim, const int max_period) {
|
|
||||||
// blockIDx.y: idx of timesteps->ne[0]
|
|
||||||
// blockIDx.x: idx of ((dim + 1) / 2) / BLOCK_SIZE
|
|
||||||
int i = blockIdx.y;
|
|
||||||
int j = threadIdx.x + blockIdx.x * blockDim.x;
|
|
||||||
float * embed_data = (float *)((char *)dst + i*nb1);
|
|
||||||
|
|
||||||
if (dim % 2 != 0 && j == ((dim + 1) / 2)) {
|
|
||||||
embed_data[dim] = 0.f;
|
|
||||||
}
|
|
||||||
|
|
||||||
int half = dim / 2;
|
|
||||||
if (j >= half) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
float timestep = timesteps[i];
|
|
||||||
float freq = (float)expf(-logf(max_period) * j / half);
|
|
||||||
float arg = timestep * freq;
|
|
||||||
embed_data[j] = cosf(arg);
|
|
||||||
embed_data[j + half] = sinf(arg);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void timestep_embedding_f32_cuda(const float * x, float * dst, const int ne00, const int nb1,
|
|
||||||
const int dim, const int max_period, cudaStream_t stream) {
|
|
||||||
int half_ceil = (dim + 1) / 2;
|
|
||||||
int num_blocks = (half_ceil + CUDA_TIMESTEP_EMBEDDING_BLOCK_SIZE - 1) / CUDA_TIMESTEP_EMBEDDING_BLOCK_SIZE;
|
|
||||||
dim3 gridDim(num_blocks, ne00, 1);
|
|
||||||
timestep_embedding_f32<<<gridDim, CUDA_TIMESTEP_EMBEDDING_BLOCK_SIZE, 0, stream>>>(x, dst, nb1, dim, max_period);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ggml_cuda_op_timestep_embedding(ggml_backend_cuda_context & ctx, ggml_tensor * dst) {
|
|
||||||
const ggml_tensor * src0 = dst->src[0];
|
|
||||||
const float * src0_d = (const float *)src0->data;
|
|
||||||
float * dst_d = (float *)dst->data;
|
|
||||||
cudaStream_t stream = ctx.stream();
|
|
||||||
|
|
||||||
GGML_ASSERT(src0->type == GGML_TYPE_F32);
|
|
||||||
GGML_ASSERT(dst->type == GGML_TYPE_F32);
|
|
||||||
|
|
||||||
const int dim = dst->op_params[0];
|
|
||||||
const int max_period = dst->op_params[1];
|
|
||||||
|
|
||||||
timestep_embedding_f32_cuda(src0_d, dst_d, src0->ne[0], dst->nb[1], dim, max_period, stream);
|
|
||||||
}
|
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user