Skip to content

Commit

Permalink
Update C, C++, and C# examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kunal-vaishnavi committed Dec 20, 2024
1 parent ebc2bc8 commit b64079e
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 22 deletions.
44 changes: 36 additions & 8 deletions examples/c/src/phi3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <iomanip>
#include <iostream>
#include <string>
#include <cstring>
#include "ort_genai.h"
#include <thread>
#include <csignal>
Expand Down Expand Up @@ -119,9 +120,22 @@ void signalHandlerWrapper(int signum) {
catch_terminate.signalHandler(signum);
}

void CXX_API(const char* model_path) {
void CXX_API(const char* model_path, const char* execution_provider) {
std::cout << "Creating config..." << std::endl;
auto config = OgaConfig::Create(model_path);

config->ClearProviders();
std::string provider(execution_provider);
if (provider.compare("cpu") != 0) {
config->AppendProvider(execution_provider);
if (provider.compare("cuda") == 0) {
config->SetProviderOption(execution_provider, "enable_cuda_graph", "0");
}
}

std::cout << "Creating model..." << std::endl;
auto model = OgaModel::Create(model_path);
auto model = OgaModel::Create(*config);

std::cout << "Creating tokenizer..." << std::endl;
auto tokenizer = OgaTokenizer::Create(*model);
auto tokenizer_stream = OgaTokenizerStream::Create(*tokenizer);
Expand Down Expand Up @@ -209,10 +223,22 @@ bool CheckIfSessionTerminated(OgaResult* result, OgaGenerator* generator) {
return false;
}

void C_API(const char* model_path) {
void C_API(const char* model_path, const char* execution_provider) {
OgaConfig* config;
std::cout << "Creating config..." << std::endl;
CheckResult(OgaCreateConfig(model_path, &config));

CheckResult(OgaConfigClearProviders(&config));
if (strcmp(execution_provider, "cpu") != 0) {
CheckResult(OgaConfigAppendProvider(&config, execution_provider));
if (strcmp(execution_provider, "cuda") == 0) {
CheckResult(OgaConfigSetProviderOption(&config, execution_provider, "enable_cuda_graph", "0"));
}
}

OgaModel* model;
std::cout << "Creating model..." << std::endl;
OgaCreateModel(model_path, &model);
CheckResult(OgaCreateModelFromConfig(&config, &model));

OgaTokenizer* tokenizer;
std::cout << "Creating tokenizer..." << std::endl;
Expand Down Expand Up @@ -293,11 +319,13 @@ void C_API(const char* model_path) {
}

static void print_usage(int /*argc*/, char** argv) {
std::cerr << "usage: " << argv[0] << " model_path" << std::endl;
std::cerr << "usage: " << argv[0] << std::endl;
std::cerr << "model_path = " << argv[1] << std::endl;
std::cerr << "execution_provider = " << argv[2] << std::endl;
}

int main(int argc, char** argv) {
if (argc != 2) {
if (argc != 3) {
print_usage(argc, argv);
return -1;
}
Expand All @@ -311,10 +339,10 @@ int main(int argc, char** argv) {

#ifdef USE_CXX
std::cout << "C++ API" << std::endl;
CXX_API(argv[1]);
CXX_API(argv[1], argv[2]);
#else
std::cout << "C API" << std::endl;
C_API(argv[1]);
C_API(argv[1], argv[2]);
#endif

return 0;
Expand Down
43 changes: 35 additions & 8 deletions examples/c/src/phi3v.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@ std::string trim(const std::string& str) {

// C++ API Example

void CXX_API(const char* model_path) {
void CXX_API(const char* model_path, const char* execution_provider) {
std::cout << "Creating config..." << std::endl;
auto config = OgaConfig::Create(model_path);

config->ClearProviders();
std::string provider(execution_provider);
if (provider.compare("cpu") != 0) {
config->AppendProvider(execution_provider);
if (provider.compare("cuda") == 0) {
config->SetProviderOption(execution_provider, "enable_cuda_graph", "0");
}
}

std::cout << "Creating model..." << std::endl;
auto model = OgaModel::Create(model_path);
auto model = OgaModel::Create(*config);

std::cout << "Creating multimodal processor..." << std::endl;
auto processor = OgaMultiModalProcessor::Create(*model);

Expand Down Expand Up @@ -98,10 +111,22 @@ void CheckResult(OgaResult* result) {
}
}

void C_API(const char* model_path) {
void C_API(const char* model_path, const char* execution_provider) {
OgaConfig* config;
std::cout << "Creating config..." << std::endl;
CheckResult(OgaCreateConfig(model_path, &config));

CheckResult(OgaConfigClearProviders(&config));
if (strcmp(execution_provider, "cpu") != 0) {
CheckResult(OgaConfigAppendProvider(&config, execution_provider));
if (strcmp(execution_provider, "cuda") == 0) {
CheckResult(OgaConfigSetProviderOption(&config, execution_provider, "enable_cuda_graph", "0"));
}
}

OgaModel* model;
std::cout << "Creating model..." << std::endl;
CheckResult(OgaCreateModel(model_path, &model));
CheckResult(OgaCreateModelFromConfig(&config, &model));

OgaMultiModalProcessor* processor;
std::cout << "Creating multimodal processor..." << std::endl;
Expand Down Expand Up @@ -184,11 +209,13 @@ void C_API(const char* model_path) {
}

static void print_usage(int /*argc*/, char** argv) {
std::cerr << "usage: " << argv[0] << " <model_path>" << std::endl;
std::cerr << "usage: " << argv[0] << std::endl;
std::cerr << "model_path = " << argv[1] << std::endl;
std::cerr << "execution_provider = " << argv[2] << std::endl;
}

int main(int argc, char** argv) {
if (argc != 2) {
if (argc != 3) {
print_usage(argc, argv);
return -1;
}
Expand All @@ -199,10 +226,10 @@ int main(int argc, char** argv) {

#ifdef USE_CXX
std::cout << "C++ API" << std::endl;
CXX_API(argv[1]);
CXX_API(argv[1], argv[2]);
#else
std::cout << "C API" << std::endl;
C_API(argv[1]);
C_API(argv[1], argv[2]);
#endif

return 0;
Expand Down
25 changes: 24 additions & 1 deletion examples/csharp/HelloPhi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ void PrintUsage()
Console.WriteLine("Usage:");
Console.WriteLine(" -m model_path");
Console.WriteLine("\t\t\t\tPath to the model");
Console.WriteLine(" -e execution_provider");
Console.WriteLine("\t\t\t\tExecution provider to run the model");
Console.WriteLine(" --non-interactive (optional)");
Console.WriteLine("\t\t\t\tInteractive mode");
}
Expand All @@ -22,6 +24,7 @@ void PrintUsage()

bool interactive = true;
string modelPath = string.Empty;
string executionProvider = string.Empty;

uint i = 0;
while (i < args.Length)
Expand All @@ -38,22 +41,42 @@ void PrintUsage()
modelPath = Path.Combine(args[i+1]);
}
}
else if (arg == "-e")
{
if (i + 1 < args.Length)
{
executionProvider = Path.Combine(args[i+1]);
}
}
i++;
}

if (string.IsNullOrEmpty(modelPath))
{
throw new Exception("Model path must be specified");
}
if (string.IsNullOrEmpty(executionProvider))
{
throw new Exception("Execution provider must be specified");
}

Console.WriteLine("-------------");
Console.WriteLine("Hello, Phi!");
Console.WriteLine("-------------");

Console.WriteLine("Model path: " + modelPath);
Console.WriteLine("Execution provider: " + executionProvider);
Console.WriteLine("Interactive: " + interactive);

using Model model = new Model(modelPath);
using Config config = new Config(modelPath);
config.ClearProviders();
if (!config.equals("cpu")) {
config.AppendProvider(executionProvider);
if (executionProvider.equals("cuda")) {
config.SetProviderOption(executionProvider, "enable_cuda_graph", "0");
}
}
using Model model = new Model(config);
using Tokenizer tokenizer = new Tokenizer(model);

var option = 2;
Expand Down
12 changes: 8 additions & 4 deletions examples/csharp/HelloPhi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

## Obtain a model

You can download a published model from HuggingFace. For example, this is Phi-3 mini optimized for CPU and mobile. You can find other models here:
You can download a published model from Hugging Face. For example, this is Phi-3.5 mini optimized for CPU and mobile. You can find other models here:

```script
huggingface-cli download microsoft/Phi-3-mini-4k-instruct-onnx --include cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4/* --local-dir models
huggingface-cli download microsoft/Phi-3.5-mini-instruct-onnx --include cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4/* --local-dir models
move models\cpu_and_mobile\cpu-int4-rtn-block-32-acc-level-4 models\phi-3
```

Alternatively you can build a model yourself using the model builder tool. See [Generate models using Model Builder](https://onnxruntime.ai/docs/genai/howto/build-model.html) for more details.
Alternatively you can build a model yourself using the model builder. See [here](https://github.com/microsoft/onnxruntime-genai/blob/main/src/python/py/models/README.md) for more details.


## Run the model

Open [HelloPhi.sln](HelloPhi.sln) and run the console application.

Note that this application does not add a template to the prompt that you enter. If your model needs a template (e.g. `<|user|>\n{input} <|end|>\n<|assistant|>` for Phi-3) then please add this to your prompt.
Notes:

1. The `executionProvider` must be one of the following: `cpu`, `cuda`, or `dml`.

2. This application does not add a template to the prompt that you enter. If your model needs a template (e.g. `<|user|>\n{input} <|end|>\n<|assistant|>` for Phi-3.5) then please add this to your prompt.
30 changes: 29 additions & 1 deletion examples/csharp/HelloPhi3V/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ void PrintUsage()
Console.WriteLine("Usage:");
Console.WriteLine(" -m model_path");
Console.WriteLine("\t\t\t\tPath to the model");
Console.WriteLine(" -e execution_provider");
Console.WriteLine("\t\t\t\tExecution provider for the model");
Console.WriteLine(" --image_paths");
Console.WriteLine("\t\t\t\tPath to the images");
Console.WriteLine(" --non-interactive (optional), mainly for CI usage");
Expand All @@ -49,6 +51,7 @@ void PrintUsage()

bool interactive = true;
string modelPath = string.Empty;
string executionProvider = string.Empty;
List<string> imagePaths = new List<string>();

uint i_arg = 0;
Expand All @@ -66,6 +69,13 @@ void PrintUsage()
modelPath = Path.Combine(args[i_arg+1]);
}
}
else if (arg == "-e")
{
if (i_arg + 1 < args.Length)
{
executionProvider = Path.Combine(args[i_arg+1]);
}
}
else if (arg == "--image_paths")
{
if (i_arg + 1 < args.Length)
Expand All @@ -76,14 +86,32 @@ void PrintUsage()
i_arg++;
}

if (string.IsNullOrEmpty(modelPath))
{
throw new Exception("Model path must be specified");
}
if (string.IsNullOrEmpty(executionProvider))
{
throw new Exception("Execution provider must be specified");
}

Console.WriteLine("--------------------");
Console.WriteLine("Hello, Phi-3-Vision!");
Console.WriteLine("--------------------");

Console.WriteLine("Model path: " + modelPath);
Console.WriteLine("Execution provider: " + executionProvider);
Console.WriteLine("Interactive: " + interactive);

using Model model = new Model(modelPath);
using Config config = new Config(modelPath);
config.ClearProviders();
if (!config.equals("cpu")) {
config.AppendProvider(executionProvider);
if (executionProvider.equals("cuda")) {
config.SetProviderOption(executionProvider, "enable_cuda_graph", "0");
}
}
using Model model = new Model(config);
using MultiModalProcessor processor = new MultiModalProcessor(model);
using var tokenizerStream = processor.CreateStream();

Expand Down

0 comments on commit b64079e

Please sign in to comment.