How to debug Cpp codes in GGML development?

How to debug Cpp codes in GGML development?

Tags
VSCode
Deep Learning
Setup
GGML
Published
Author
  1. Build ggml from root directory
    1. cmake -B build-cpu cmake --build build-cpu --config Release -j 24
      or with cuda backend
      cmake -B build-cuda -DGGML_CUDA=ON cmake --build build-cuda --config Release -j 24
  1. Configurate debugging and running via tasks.json and launch.json under .vscode folder
    1. // tasks.json { "version": "2.0.0", "tasks": [ { "label": "build-with-ggml", "type": "shell", "command": "g++", "args": [ "-g", "${file}", "-o", "${fileDirname}/out/${fileBasenameNoExtension}", "-I", "${workspaceFolder}/src", "-I", "${workspaceFolder}/include", "-L", "${workspaceFolder}/build/src", // replace `build` with `build-cuda` if useing CUDA backend "-lggml", // "-DGGML_USE_CUDA" // uncomment this line if useing CUDA backend ], "group": "build", "problemMatcher": [ "$gcc" ], "dependsOn": "create-out-dir" }, { "label": "create-out-dir", "type": "shell", "command": "mkdir", "args": [ "-p", "${fileDirname}/out" ], "problemMatcher": [] }, ] }
      In tasks.json:
    2. we configured a task named build-with-ggml, which will be used in the launch named C++ Debug with ggml via preLaunchTask
    3. "-I", "${workspaceFolder}/src" and "-I", "${workspaceFolder}/include" are used to include necessary source files and header files of ggml library
    4. "-L", "${workspaceFolder}/build/src" is used to indicate where the ggml shared library locates (which we have built in step1), and "-lggml" to actually link the ggml shared library (libggml.so in linux)
    5. // launch.json { "version": "0.2.0", "configurations": [ { "name": "C++ Debug with ggml", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/out/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [ { "name": "LD_LIBRARY_PATH", "value": "${workspaceFolder}/build/src:${env:LD_LIBRARY_PATH}" // replace `build` with `build-cuda` if useing CUDA backend } ], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build-with-ggml", "miDebuggerPath": "/usr/bin/gdb", "logging": { // "trace": true, // "traceResponse": true, // "engineLogging": true } } ] }
  1. (Optional) Install gdb if haven't installed
    1. sudo apt-get update sudo apt-get install gdb
Use which gdb to check if gdb is successfully installed and the path matches with miDebuggerPath in launch.json
  1. Debug / Run without debugging
    1. notion image
Β 
VS Code extension: Graphviz Interactive Preview
notion image
notion image
With this extension, we don't have to bother converting .dot to .png for visualization
Β 
How to print a GGML tensor prettily?