Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
llama.cppのバックエンドggmlを読もう
Search
dasoran
May 17, 2025
110
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
llama.cppのバックエンドggmlを読もう
VRCLT #18 で発表したスライドです
dasoran
May 17, 2025
More Decks by dasoran
See All by dasoran
Rift Sで沢山苦労した話といくつかの処方箋
dasoran
0
350
データ集計結果を提供するAPIとServerlessでの失敗談
dasoran
0
580
機械学習を支えるx86-64の拡張命令セットを読む会
dasoran
0
64
Featured
See All Featured
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
850
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
Amusing Abliteration
ianozsvald
1
300
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
Why Our Code Smells
bkeepers
PRO
340
58k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
700
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
370
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
320
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
50
10k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.6k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
3
4.3k
Transcript
llama.cppのバックエンド ggmlを読んでみた 読もう 2025/05/17 榊そら
自己紹介 - ソフトウェアエンジニア - 仕事はAWSやったり人工衛星つくったり - 最近はMLにハマっている - Local LLM
しようとしてebayでEPYCを買うも 2か月届かず.... - VRChatは2019年からゆるゆるやってます
ggmlとは - llama.cppの計算バックエンド - OllamaやLM Studioで使用されている - 事実上のLocal LLMにおけるデファクト実装 -
(おそらく) PyTorchとかより早い - 関連としてGGUFフォーマットがある - Ollamaとかでモデルを動かす際のフォーマット 作者の頭文字+mlでggml
ggmlを探る いきなりコードを読むのはハードルが高い - Introduction to ggml - https://huggingface.co/blog/introduction-to-ggml - ggml
の概要とggmlを使って簡単な計算をする記事 - Understanding how LLM inference works with llama.cpp - https://www.omrimallis.com/posts/understanding-how-llm-inference-works-with-llama-cpp/ - llama.cpp/ggml がなぜ早いのかを解説した記事 この辺を読むと、簡単な動かし方や概要がわかる
ggmlを読んでみる - 前提としてほぼC/C++で書かれています - 利用する際の基本的な処理の流れは以下の通り - バックエンドを初期化 - contextの初期化 -
contextへメタデータの設定 - contextへバックエンドバッファを割り当てる - バックエンドへデータをコピー - 計算グラフを作成、contextへ設定 - 計算 - 結果をバックエンドから取り出す
ディレクトリ構成 - … - examples/ - includes/ - src/ -
ggml-xxx/ - … - … - exapmles: - ggmlを使ったサンプルがある - コードを読む際の読み始める部分の参考に使える - 今回は examples/simple/simple-backend.cpp を参 考にした - includes: - 一般的なincludesディレクトリ - ggmlは実装がコンパクトなので、 includesを一通り眺 めると全体像が把握できる - src: - 直下にggml-xxx/という実行環境ごとのコードがある - 他はsrc直下にすべて実装がある
includes を眺める - ggml-alloc.h - ggml-backend.h - ggml-blas.h - ggml-cann.h
- ggml-cpp.h - ggml-cpu.h - ggml-cuda.h - ggml-kompute.h - ggml-metal.h - ggml-opencl.h - ggml-opt.h - ggml-rpc.h - ggml-sycl.h - ggml-vulkan.h - ggml.h - gguf.h
includes を眺める - ggml-alloc.h - ggml-backend.h - ggml-blas.h - ggml-cann.h
- ggml-cpp.h - ggml-cpu.h - ggml-cuda.h - ggml-kompute.h - ggml-metal.h - ggml-opencl.h - ggml-opt.h - ggml-rpc.h - ggml-sycl.h - ggml-vulkan.h - ggml.h - gguf.h - 実は大部分は実行環境ごとの内容 - 読む上では6つのheaderファイルを見れば概ね大丈夫 - さらに以下は部分的な内容 - ggml-alloc.h: graphやtensorの領域確保 - ggml-backend.h: 実行環境関係 - ggml-cpp.h: スマートポインタがちょっと書いてある - ggml-opt.h: 学習関係 - gguf.h: モデルファイル - 大部分はggml.hのみにまとまっている コンパクト!読めそう!
src を覗く - ggml-xxx/ - … - ggml-alloc.c - ggml-backend*.cpp
- ggml-quants.c - ggml-threading.cpp - ggml.c - gguf.cpp headerファイルを 除いた一覧 - ggml-xxx/ は実行環境ごとの実装 - ggml-cpu とか、1つ参照するとよさそう - quantsは量子化、ggufはファイル周り、threadingは実質6行 - とりあえず読むなら - ggml-alloc.c - ggml-backend*.cpp - ggml.c - この辺と適当な実行環境のコードを参照すればよさそう コンパクト!読めそう!
ど真ん中っぽいggml.cを見る ファイルの行数は.... 6504行.... やっぱりだめかも....
ど真ん中っぽいggml.cを見る - よく見ると処理が //////…//// で区切ら れている - 9つのセクションに分かれている - 定数定義
- printとかのutil - contextの処理(initとか) - contextにぶら下がるobject/tensor処理 - 計算 - graph処理 - 計算実行時のオプション - 量子化 - スレッド処理 - 1787-5121行目の3334行は計算 - addとか 仕組み周りは3000行ちょっと!
読めるかも!!
ご清聴ありがとうございました
参考資料: オブジェクトの構造 ctx ggml_init(param) ggml_object ggml_object ggml_object objects_begin objects_end buffer
buffer buffer ggml_backend_alloc_ctx_tensors(ctx, backend) ggml_backend_tensor_set(tensor, …) on backend