From 1b2aa950cbf5f28dfbe3e76705534df57196cce2 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Sat, 11 Mar 2023 18:05:41 +0100 Subject: [PATCH] C++ CLI app, added arguments to select a GPU to use --- Examples/main/main.cpp | 6 ++++-- Examples/main/params.cpp | 24 ++++++++++++++++++++++++ Examples/main/params.h | 1 + 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Examples/main/main.cpp b/Examples/main/main.cpp index 29b92fc..677381e 100644 --- a/Examples/main/main.cpp +++ b/Examples/main/main.cpp @@ -10,11 +10,13 @@ using namespace Whisper; #define STREAM_AUDIO 1 -static HRESULT loadWhisperModel( const wchar_t* path, iModel** pp ) +static HRESULT loadWhisperModel( const wchar_t* path, const std::wstring& gpu, iModel** pp ) { using namespace Whisper; sModelSetup setup; setup.impl = eModelImplementation::GPU; + if( !gpu.empty() ) + setup.adapter = gpu.c_str(); return Whisper::loadModel( path, setup, nullptr, pp ); } @@ -199,7 +201,7 @@ int wmain( int argc, wchar_t* argv[] ) } ComLight::CComPtr model; - HRESULT hr = loadWhisperModel( params.model.c_str(), &model ); + HRESULT hr = loadWhisperModel( params.model.c_str(), params.gpu, &model ); if( FAILED( hr ) ) { printError( "failed to load the model", hr ); diff --git a/Examples/main/params.cpp b/Examples/main/params.cpp index ff1cfdd..d6d6ae9 100644 --- a/Examples/main/params.cpp +++ b/Examples/main/params.cpp @@ -2,6 +2,7 @@ #include #include #include "miscUtils.h" +#include "../../Whisper/API/iContext.cl.h" whisper_params::whisper_params() { @@ -27,6 +28,8 @@ void whisper_print_usage( int argc, wchar_t** argv, const whisper_params& params fprintf( stderr, "\n" ); fprintf( stderr, "options:\n" ); fprintf( stderr, " -h, --help [default] show this help message and exit\n" ); + fprintf( stderr, " -la, --list-adapters List graphic adapters and exit\n" ); + fprintf( stderr, " -gpu, --use-gpu The graphic adapter to use for inference\n" ); fprintf( stderr, " -t N, --threads N [%-7d] number of threads to use during computation\n", params.n_threads ); fprintf( stderr, " -p N, --processors N [%-7d] number of processors to use during computation\n", params.n_processors ); fprintf( stderr, " -ot N, --offset-t N [%-7d] time offset in milliseconds\n", params.offset_t_ms ); @@ -51,6 +54,20 @@ void whisper_print_usage( int argc, wchar_t** argv, const whisper_params& params fprintf( stderr, "\n" ); } +static void __stdcall pfnListAdapter( const wchar_t* name, void* ) +{ + wprintf( L"\"%s\"\n", name ); +} + +static void listGpus() +{ + printf( " Available graphic adapters:\n" ); + HRESULT hr = Whisper::listGPUs( &pfnListAdapter, nullptr ); + if( SUCCEEDED( hr ) ) + return; + printError( "Unable to enumerate GPUs", hr ); +} + bool whisper_params::parse( int argc, wchar_t* argv[] ) { for( int i = 1; i < argc; i++ ) @@ -69,6 +86,12 @@ bool whisper_params::parse( int argc, wchar_t* argv[] ) return false; } + if( arg == L"-la" || arg == L"--list-adapters" ) + { + listGpus(); + return false; + } + else if( arg == L"-t" || arg == L"--threads" ) { n_threads = std::stoul( argv[ ++i ] ); } else if( arg == L"-p" || arg == L"--processors" ) { n_processors = std::stoul( argv[ ++i ] ); } else if( arg == L"-ot" || arg == L"--offset-t" ) { offset_t_ms = std::stoul( argv[ ++i ] ); } @@ -90,6 +113,7 @@ bool whisper_params::parse( int argc, wchar_t* argv[] ) else if( arg == L"-l" || arg == L"--language" ) { language = utf8( argv[ ++i ] ); } else if( arg == L"-m" || arg == L"--model" ) { model = argv[ ++i ]; } else if( arg == L"-f" || arg == L"--file" ) { fname_inp.push_back( argv[ ++i ] ); } + else if( arg == L"-gpu" || arg == L"--use-gpu" ) { gpu = argv[ ++i ]; } else { fprintf( stderr, "error: unknown argument: %S\n", arg.c_str() ); diff --git a/Examples/main/params.h b/Examples/main/params.h index 9eb2b04..d8c2dcc 100644 --- a/Examples/main/params.h +++ b/Examples/main/params.h @@ -28,6 +28,7 @@ struct whisper_params std::string language = "en"; std::wstring model = L"models/ggml-base.en.bin"; + std::wstring gpu; std::vector fname_inp; whisper_params();