Coder Social home page Coder Social logo

llama.cpp.zig's Introduction

llama.cpp.zig

llama.cpp bindings and utilities for zig. Currently targeting zig 0.11.x, there is high chance nightly works as well (0.12.0-dev.1856+94c63f31f when I checked) (using same branch, only few places have needed patching where @hasDecl was enough to support both versions).

  • Implements llama.h for nicer interaction with zig.
    • Removes prefixes, changes naming for functions to camelCase. Group functions within most appropriete struct. etc.
    • Bindings still depend on translate-c as I tried to not rewrite struct definitions too much as those might change. But some were rewritten for ease of use. Has to be seen if others might benefit rewriting as well for nicer access / syntax.
  • Re-Implements some of the C++ that is not acessable through c api due to use of c++ std stuff:
  • Implements some utilities such as:
    • buffered Tokenizer & Detokenizer
    • prompt utility to simplify interaction with llama. (still wip) will support easly modifying prompt & regenerating it. Handling of out of context behaviour, etc. Possibly optional pagination/indexing of messages.
    • basic templated prompt generator, to try to manage different prompt formatting styles (chatML, alpaca, etc).

Example usage

Clone: git clone --recursive https://github.com/Deins/llama.cpp.zig.git

  1. Download llama.cpp supported model (usually *.gguf format). For example this one.
  2. build and run with:
zig build run-simple -Doptimize=ReleaseFast -- --model_path path_to/model.gguf --prompt "Hello! I am AI, and here are the 10 things I like to think about:"

See examples/simple.zig

CPP samples

Subset of llama cpp samples have been included in build scripts. Use -Dcpp_samples option to install them.
Or run them directly, for example: zig build run-cpp-main -Dclblast -Doptimize=ReleaseFast -- -m path/to/model.gguf -p "hello my name is"

CLBlast acceleration

Clblast is supported by building it from source with zig. At moment only OpenCl backend has been tested. Cuda backend is not finished as I don't have nvidia hardware, pull requests are welcome.

Build:

Ideally just zig build -Dclblast .... It should work out of the box if you have installed GPUOpen/ocl. For other configurations you will need to find where OpenCL headers/libs are and pass them in using arguments zig build -Dclblast -Dopencl_includes="/my/path" -Dopencl_libs="/my/path/" Auto detection might be improved in future - let me know what opencl sdk you use.

Selecting GPU

With opencl backend main_gpu parameter is ignored. Insted you can set ids of GGML_OPENCL_PLATFORM GGML_OPENCL_DEVICE system enviroment variables. There is zig build -Dclblast run-opencl_devices utility available to print all opencl devices detected.

llama.cpp.zig's People

Contributors

deins avatar adrid avatar

Stargazers

Brandt Cormorant avatar Adithya Awati avatar huaxk avatar Murat Genç avatar Jevgeni Tarassov avatar  avatar Brad Pillow avatar sooriya avatar Ethan M Lewis avatar Ethan Groat avatar Nate Todd avatar  avatar xieyao avatar Kentaro Iizuka avatar Doug Tangren avatar  avatar Benjamin Botwin avatar  avatar  avatar Cory Sabol avatar Kamil Shakirov avatar LI Yu avatar  avatar Scott Dunlop avatar Miguel GP avatar  avatar Jag Chadha avatar Narain  avatar Sandalots avatar Arseniy avatar  avatar ArtemisX64 avatar Martin H avatar Seth Vincent avatar hsnmkls avatar  avatar  avatar afrideva avatar

Watchers

 avatar  avatar

Forkers

adrid fivegrant

llama.cpp.zig's Issues

grammar from string

Regarding this comment:

grammar is not passed as string to be parsed, instead parsed llama_grammar* params, as at time of writing grammar_parser has no C api

Is there is any plan to support this? What do you think about creating simple cpp binding for this like that?

extern "C" llama_grammar * parseGrammar(const char * str) {
    grammar_parser::parse_state parsed_state = grammar_parser::parse(str);
    std::vector<const llama_grammar_element *> grammar_rules(parsed_state.c_rules());

    return llama_grammar_init(grammar_rules.data(), grammar_rules.size(), parsed_state.symbol_ids.at("root"));
}

I've tested this and after accept the token with grammar option it seems to work. I would prepare a draft if you think binding would be fine and we could check this in detail.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.