Code Monkey home page Code Monkey logo

onnxruntime-genai's Introduction

ONNX Runtime Generative AI

Run generative AI models with ONNX Runtime.

This library provides the generative AI loop for ONNX models, including inference with ONNX Runtime, logits processing, search and sampling, and KV cache management.

Users can call a high level generate() method, or run each iteration of the model in a loop.

  • Search techniques like greedy/beam search to generate token sequences
  • Built in scoring tools like repetition penalties
  • Easy custom scoring

Sample code for phi-2 in Python

Install onnxruntime-genai.

(Temporary) Build and install from source according to the instructions below.

import onnxruntime_genai as og

model=og.Model(f'models/microsoft/phi-2', device_type)

tokenizer = model.CreateTokenizer()

prompt = '''def print_prime(n):
    """
    Print all primes between 1 and n
    """'''

tokens = tokenizer.encode(prompt)

params=og.SearchParams(model)
params.max_length = 200
params.input_ids = tokens

output_tokens=model.Generate(params)

text = tokenizer.decode(output_tokens)

print("Output:")
print(text)

Features

  • Supported model architectures:
    • Phi-2
    • Llama
    • GPT
  • Supported targets:
    • CPU
    • GPU (CUDA)
  • Supported sampling features
    • Beam search
    • Greedy search
    • Top P/Top K
  • APIs
    • Python
    • C/C++

Coming very soon

  • Support for the Mistral and Whisper model architectures
  • C# API
  • Support for DirectML

Roadmap

  • Automatic model download and cache
  • More model architectures

Build from source

This step requires cmake to be installed.

  1. Clone this repo

    git clone https://github.com/microsoft/onnxruntime-genai
  2. Install ONNX Runtime

    mkdir -p ort/include
    cd ort/include
    wget https://raw.githubusercontent.com/microsoft/onnxruntime/v1.17.0/include/onnxruntime/core/session/onnxruntime_c_api.h
    
    cd ..
    mkdir -p ort/lib
    cd ort/lib
    wget https://github.com/microsoft/onnxruntime/releases/download/v1.17.0/onnxruntime-linux-x64-gpu-1.17.0.tgz
    tar xvzf onnxruntime-linux-x64-gpu-1.17.0.tgz 
    cp onnxruntime-linux-x64-gpu-1.17.0/lib/libonnxruntime*.so* .
  3. Build onnxruntime-genai

    bash build.sh

    Or build.bat on Windows

  4. Build wheel (temporary)

    cd build
    make PyPackageBuild
  5. Install Python wheel

    cd wheel
    pip install *.whl

Model download and export

ONNX models are run from a local folder, via a string supplied to the Model() method.

To source microsoft/phi-2 optimized for your target, download and run the following script:

wget https://github.com/microsoft/onnxruntime-genai/blob/kvaishnavi/models/models/export.py

Export int4 CPU version

python export.py python models/export.py --m microsoft/phi-2 -p int4 -e cpu -o phi2-int4-cpu.onnx

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.

onnxruntime-genai's People

Contributors

jchen351 avatar ryanunderhill avatar snnn avatar natke avatar baijumeswani avatar microsoftopensource avatar kunal-vaishnavi avatar aciddelgado avatar microsoft-github-operations[bot] avatar

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.