-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathcli_parser.cpp
More file actions
867 lines (808 loc) · 43.7 KB
/
cli_parser.cpp
File metadata and controls
867 lines (808 loc) · 43.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
//*****************************************************************************
// Copyright 2025 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#include "cli_parser.hpp"
#include <filesystem>
#include <iostream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#include <variant>
#include "capi_frontend/server_settings.hpp"
#include "graph_export/graph_cli_parser.hpp"
#include "graph_export/rerank_graph_cli_parser.hpp"
#include "graph_export/embeddings_graph_cli_parser.hpp"
#include "graph_export/t2s_graph_cli_parser.hpp"
#include "graph_export/s2t_graph_cli_parser.hpp"
#include "graph_export/image_generation_graph_cli_parser.hpp"
#include "ovms_exit_codes.hpp"
#include "filesystem/filesystem.hpp"
#include "filesystem/localfilesystem.hpp"
#include "stringutils.hpp"
#include "version.hpp"
namespace ovms {
constexpr const char* CONFIG_MANAGEMENT_HELP_GROUP{"config management"};
constexpr const char* API_KEY_ENV_VAR{"API_KEY"};
std::string getConfigPath(const std::string& configPath) {
bool isDir = false;
auto status = LocalFileSystem::isDir(configPath, &isDir);
if (!status.ok()) {
throw std::logic_error("Invalid path for the config: " + configPath);
}
if (isDir) {
return FileSystem::joinPath({configPath, "config.json"});
}
return configPath;
}
std::variant<bool, std::pair<int, std::string>> CLIParser::parse(int argc, char** argv) {
std::stringstream ss;
try {
options = std::make_unique<cxxopts::Options>(argv[0], "OpenVINO Model Server");
auto configOptions = std::make_unique<cxxopts::Options>("ovms --model_name <MODEL_NAME> --add_to_config --config_path <CONFIG_PATH> --model_repository_path <MODEL_REPO_PATH> \n ovms --model_path <MODEL_PATH> --model_name <MODEL_NAME> --add_to_config --config_path <CONFIG_PATH> \n ovms --remove_from_config --config_path <CONFIG_PATH> --model_name <MODEL_NAME>", "config management commands:");
// Adding this option to parse unrecognised options in another parser
options->allow_unrecognised_options();
// clang-format off
options->add_options()
("h, help",
"Show this help message and exit")
("version",
"Show binary version")
("port",
"gRPC server port",
cxxopts::value<uint32_t>()->default_value("0"),
"PORT")
("grpc_bind_address",
"Network interface address to bind to for the gRPC API",
cxxopts::value<std::string>()->default_value("0.0.0.0"),
"GRPC_BIND_ADDRESS")
("rest_port",
"REST server port, the REST server will not be started if rest_port is blank or set to 0",
cxxopts::value<uint32_t>()->default_value("0"),
"REST_PORT")
("rest_bind_address",
"Network interface address to bind to for the REST API",
cxxopts::value<std::string>()->default_value("0.0.0.0"),
"REST_BIND_ADDRESS")
("grpc_workers",
"Number of gRPC servers. Default 1. Increase for multi client, high throughput scenarios",
cxxopts::value<uint32_t>()->default_value("1"),
"GRPC_WORKERS")
("grpc_max_threads",
"Maximum number of threads which can be used by the gRPC server. Default value depends on number of CPUs.",
cxxopts::value<uint32_t>(),
"GRPC_MAX_THREADS")
("grpc_memory_quota",
"GRPC server buffer memory quota. Default value set to 2147483648 (2GB).",
cxxopts::value<size_t>(),
"GRPC_MEMORY_QUOTA")
("rest_workers",
"Number of worker threads in REST server - has no effect if rest_port is not set. Default value depends on number of CPUs. ",
cxxopts::value<uint32_t>(),
"REST_WORKERS")
("log_level",
"serving log level - one of TRACE, DEBUG, INFO, WARNING, ERROR",
cxxopts::value<std::string>()->default_value("INFO"), "LOG_LEVEL")
("log_path",
"Optional path to the log file",
cxxopts::value<std::string>(), "LOG_PATH")
#ifdef MTR_ENABLED
("trace_path",
"Path to the trace file",
cxxopts::value<std::string>(), "TRACE_PATH")
#endif
("grpc_channel_arguments",
"A comma separated list of arguments to be passed to the gRPC server. (e.g. grpc.max_connection_age_ms=2000)",
cxxopts::value<std::string>(), "GRPC_CHANNEL_ARGUMENTS")
("file_system_poll_wait_seconds",
"Time interval between config and model versions changes detection. Default is 1. Zero or negative value disables changes monitoring.",
cxxopts::value<uint32_t>()->default_value("1"),
"FILE_SYSTEM_POLL_WAIT_SECONDS")
("sequence_cleaner_poll_wait_minutes",
"Time interval between two consecutive sequence cleanup scans. Default is 5. Zero value disables sequence cleaner. It also sets the schedule for releasing free memory from the heap.",
cxxopts::value<uint32_t>()->default_value("5"),
"SEQUENCE_CLEANER_POLL_WAIT_MINUTES")
("custom_node_resources_cleaner_interval_seconds",
"Time interval between two consecutive resources cleanup scans. Default is 300. Zero value disables resources cleaner.",
cxxopts::value<uint32_t>()->default_value("300"),
"CUSTOM_NODE_RESOURCES_CLEANER_INTERVAL_SECONDS")
("cache_dir",
"Overrides model cache directory. By default cache files are saved into"
#ifdef __linux__
"/opt/cache"
#elif _WIN32
"c:\\Intel\\openvino_cache"
#endif
" if the directory is present. When enabled, first model load will produce cache files.",
cxxopts::value<std::string>(),
"CACHE_DIR")
("metrics_enable",
"Flag enabling metrics endpoint on rest_port.",
cxxopts::value<bool>()->default_value("false"),
"METRICS")
("metrics_list",
"Comma separated list of metrics. If unset, only default metrics will be enabled. Default metrics: ovms_requests_success, ovms_requests_fail, ovms_request_time_us, ovms_streams, ovms_inference_time_us, ovms_wait_for_infer_req_time_us. When set, only the listed metrics will be enabled. Optional metrics: ovms_infer_req_queue_size, ovms_infer_req_active.",
cxxopts::value<std::string>()->default_value(""),
"METRICS_LIST")
("cpu_extension",
"A path to shared library containing custom CPU layer implementation. Default: empty.",
cxxopts::value<std::string>()->default_value(""),
"CPU_EXTENSION")
("allowed_media_domains",
"Comma separated list of media domains from which URLs can be used as input for LLMs. Set to \"all\" to disable this restriction.",
cxxopts::value<std::vector<std::string>>(),
"ALLOWED_MEDIA_DOMAINS")
("allowed_local_media_path",
"Path to directory that contains multimedia files that can be used as input for LLMs.",
cxxopts::value<std::string>(),
"ALLOWED_LOCAL_MEDIA_PATH")
("allow_credentials",
"Flag enabling credentials on the API.",
cxxopts::value<bool>()->default_value("false"),
"ALLOW_CREDENTIALS")
("allowed_origins",
"Comma separated list of origins that are allowed to access the API. Default: *.",
cxxopts::value<std::string>()->default_value("*"),
"ALLOWED_ORIGINS")
("allowed_methods",
"Comma separated list of methods that are allowed to access the API. Default: *.",
cxxopts::value<std::string>()->default_value("*"),
"ALLOWED_METHODS")
("allowed_headers",
"Comma separated list of headers that are allowed to access the API. Default: *.",
cxxopts::value<std::string>()->default_value("*"),
"ALLOWED_HEADERS")
("api_key_file",
"path to the text file containing API key for authentication for generative endpoints. If not set, authentication is disabled.",
cxxopts::value<std::string>()->default_value(""),
"API_KEY");
options->add_options("multi model")
("config_path",
"Absolute path to json configuration file",
cxxopts::value<std::string>(), "CONFIG_PATH");
options->add_options(CONFIG_MANAGEMENT_HELP_GROUP)
("list_models",
"Directive to show available servables in models repository",
cxxopts::value<bool>()->default_value("false"),
"LIST_MODELS")
("add_to_config",
"Directive to add a model to configuration file. This parameter should be executed with --model_name, --config_path and either with --model_path or --model_repository_path.",
cxxopts::value<bool>()->default_value("false"),
"ADD_TO_CONFIG")
("remove_from_config",
"Directive to remove a model from configuration file. This parameter should be executed with --config_path and --model_name to specify which model to remove.",
cxxopts::value<bool>()->default_value("false"),
"REMOVE_FROM_CONFIG");
// Set default value for model_repository_path from environment variable if it exists and is not empty
std::string defaultModelRepoPath = "";
std::string defaultConfigPath = "";
const char* envModelRepoPath = std::getenv("OVMS_MODEL_REPOSITORY_PATH");
if (envModelRepoPath != nullptr && std::string(envModelRepoPath).length() > 0) {
defaultModelRepoPath = envModelRepoPath;
defaultConfigPath = std::string(envModelRepoPath) + "/config.json";
}
options->add_options("pull hf model")
("pull",
"Pull model from HF. Uses optional environment variables: HF_TOKEN - when set used for authentication, HF_ENDPOINT - when set replaces huggingface.co for model download.",
cxxopts::value<bool>()->default_value("false"),
"PULL_HF")
("source_model",
"HF source model path",
cxxopts::value<std::string>(),
"HF_SOURCE")
("gguf_filename",
"Name of the GGUF file",
cxxopts::value<std::string>(),
"GGUF_FILENAME")
("overwrite_models",
"Overwrite the model if it already exists in the models repository",
cxxopts::value<bool>()->default_value("false"),
"OVERWRITE_MODELS")
("model_repository_path",
"HF model destination download path",
cxxopts::value<std::string>()->default_value(defaultModelRepoPath),
"MODEL_REPOSITORY_PATH")
("task",
"Choose type of model export: text_generation - chat and completion endpoints, embeddings - embeddings endpoint, rerank - rerank endpoint, image_generation - image generation/edit/inpainting endpoints, text2speech - audio/speech endpoint, speech2text - audio/transcriptions endpoint.",
cxxopts::value<std::string>(),
"TASK")
("weight-format",
"Model precision used in optimum-cli export with conversion",
cxxopts::value<std::string>()->default_value("int8"),
"WEIGHT_FORMAT")
("extra_quantization_params",
"Model quantization parameters used in optimum-cli export with conversion for text generation models",
cxxopts::value<std::string>(),
"EXTRA_QUANTIZATION_PARAMS")
("vocoder",
"The vocoder model to use for text2speech. For example microsoft/speecht5_hifigan",
cxxopts::value<std::string>(),
"VOCODER");
options->add_options("single model")
("model_name",
"Name of the model",
cxxopts::value<std::string>(),
"MODEL_NAME")
("model_path",
"Folder with AI model versions or with mediapipe graph",
cxxopts::value<std::string>(),
"MODEL_PATH")
("batch_size",
"Resets models batchsize, int value or auto. This parameter will be ignored if shape is set",
cxxopts::value<std::string>(),
"BATCH_SIZE")
("shape",
"Resets models shape (model must support reshaping). If set, batch_size parameter is ignored",
cxxopts::value<std::string>(),
"SHAPE")
("layout",
"Resets model layout. It should be in format <TARGET_LAYOUT>:<SOURCE_LAYOUT> e.g. NCHW:NHWC",
cxxopts::value<std::string>(),
"LAYOUT")
("mean",
"Resets model mean.",
cxxopts::value<std::string>(),
"MEAN")
("scale",
"Resets model scale.",
cxxopts::value<std::string>(),
"SCALE")
("color_format",
"Resets model color format. It should be in format <TARGET_COLOR_FORMAT>:<SOURCE_COLOR_FORMAT> e.g. BGR:RGB",
cxxopts::value<std::string>(),
"COLOR_FORMAT")
("precision",
"Resets model precision.",
cxxopts::value<std::string>(),
"PRECISION")
("model_version_policy",
"Model version policy",
cxxopts::value<std::string>(),
"MODEL_VERSION_POLICY")
("nireq",
"Size of inference request queue for model executions. Recommended to be >= parallel executions. Default value calculated by OpenVINO based on available resources. Request for 0 is treated as request for default value",
cxxopts::value<uint32_t>(),
"NIREQ")
("target_device",
"Target device to run the inference",
cxxopts::value<std::string>()->default_value("CPU"),
"TARGET_DEVICE")
("plugin_config",
"A dictionary of plugin configuration keys and their values, eg \"{\\\"NUM_STREAMS\\\": \\\"1\\\"}\". Default number of streams is optimized to optimal latency with low concurrency.",
cxxopts::value<std::string>(),
"PLUGIN_CONFIG")
("stateful",
"Flag indicating model is stateful",
cxxopts::value<bool>()->default_value("false"),
"STATEFUL")
("idle_sequence_cleanup",
"Flag indicating if model is subject to sequence cleaner scans",
cxxopts::value<bool>()->default_value("true"),
"IDLE_SEQUENCE_CLEANUP")
("low_latency_transformation",
"Flag indicating that Model Server should perform low latency transformation on that model",
cxxopts::value<bool>()->default_value("false"),
"LOW_LATENCY_TRANSFORMATION")
("max_sequence_number",
"Determines how many sequences can be processed concurrently by one model instance. When that value is reached, attempt to start a new sequence will result in error.",
cxxopts::value<uint32_t>(),
"MAX_SEQUENCE_NUMBER");
configOptions->custom_help("");
configOptions->add_options(CONFIG_MANAGEMENT_HELP_GROUP)
("list_models",
"Directive to show available servables in models repository",
cxxopts::value<bool>()->default_value("false"),
"LIST_MODELS")
("add_to_config",
"Directive to add a model to configuration file. This parameter should be executed with --model_name, --config_path and either with --model_path or --model_repository_path.",
cxxopts::value<bool>()->default_value("false"),
"ADD_TO_CONFIG")
("remove_from_config",
"Directive to remove a model from configuration file. This parameter should be executed with --config_path and --model_name to specify which model to remove.",
cxxopts::value<bool>()->default_value("false"),
"REMOVE_FROM_CONFIG")
("model_repository_path",
"Absolute or relative path from the config directory to the model repository",
cxxopts::value<std::string>()->default_value(defaultModelRepoPath),
"MODEL_REPOSITORY_PATH")
("model_path",
"Absolute or relative path from the config directory to the model. By default is a combination of the model_repository_path and model_name.",
cxxopts::value<std::string>(),
"MODEL_PATH")
("model_name",
"Name of the model",
cxxopts::value<std::string>(),
"MODEL_NAME")
("config_path",
"Path to json configuration file",
cxxopts::value<std::string>()->default_value(defaultConfigPath),
"CONFIG_PATH");
result = std::make_unique<cxxopts::ParseResult>(options->parse(argc, argv));
// HF pull mode or pull and start mode
if (isHFPullOrPullAndStart(this->result)) {
std::vector<std::string> unmatchedOptions;
GraphExportType task;
if (result->count("task")) {
task = stringToEnum(result->operator[]("task").as<std::string>());
switch (task) {
case TEXT_GENERATION_GRAPH: {
GraphCLIParser cliParser;
unmatchedOptions = cliParser.parse(result->unmatched());
this->graphOptionsParser = std::move(cliParser);
break;
}
case EMBEDDINGS_GRAPH: {
EmbeddingsGraphCLIParser cliParser;
unmatchedOptions = cliParser.parse(result->unmatched());
this->graphOptionsParser = std::move(cliParser);
break;
}
case RERANK_GRAPH: {
RerankGraphCLIParser cliParser;
unmatchedOptions = cliParser.parse(result->unmatched());
this->graphOptionsParser = std::move(cliParser);
break;
}
case IMAGE_GENERATION_GRAPH: {
ImageGenerationGraphCLIParser cliParser;
unmatchedOptions = cliParser.parse(result->unmatched());
this->graphOptionsParser = std::move(cliParser);
break;
}
case TEXT_TO_SPEECH_GRAPH: {
TextToSpeechGraphCLIParser cliParser;
unmatchedOptions = cliParser.parse(result->unmatched());
this->graphOptionsParser = std::move(cliParser);
break;
}
case SPEECH_TO_TEXT_GRAPH: {
SpeechToTextGraphCLIParser cliParser;
unmatchedOptions = cliParser.parse(result->unmatched());
this->graphOptionsParser = std::move(cliParser);
break;
}
case UNKNOWN_GRAPH: {
ss << "error parsing options - --task parameter unsupported value: " + result->operator[]("task").as<std::string>();
return std::make_pair(OVMS_EX_USAGE, ss.str());
}
}
} else {
ss << "error parsing options - --task parameter wasn't passed";
return std::make_pair(OVMS_EX_USAGE, ss.str());
}
if (unmatchedOptions.size()) {
ss << "task: " << enumToString(task) << " - error parsing options - unmatched arguments : ";
for (auto& argument : unmatchedOptions) {
ss << argument << ", ";
}
ss << std::endl;
return std::make_pair(OVMS_EX_USAGE, ss.str());
}
} else if (result->unmatched().size()){
ss << "error parsing options - unmatched arguments: ";
for (auto& argument : result->unmatched()) {
ss << argument << ", ";
}
ss << std::endl;
return std::make_pair(OVMS_EX_USAGE, ss.str());
}
if (isHFPullOrPullAndStart(this->result) && result->count("list_models")) {
ss << "error parsing options - --list_models cannot be used with --pull or --task" << std::endl;
return std::make_pair(OVMS_EX_USAGE, ss.str());
}
if (isHFPullOrPullAndStart(this->result) && result->count("remove_from_config")) {
ss << "error parsing options - --remove_from_config cannot be used with --pull or --task" << std::endl;
return std::make_pair(OVMS_EX_USAGE, ss.str());
}
if (isHFPullOrPullAndStart(this->result) && result->count("add_to_config")) {
ss << "error parsing options - --add_to_config cannot be used with --pull or --task" << std::endl;
return std::make_pair(OVMS_EX_USAGE, ss.str());
}
if (result->count("add_to_config") && result->count("list_models")) {
ss << "error parsing options - --list_models cannot be used with --add_to_config" << std::endl;
return std::make_pair(OVMS_EX_USAGE, ss.str());
}
if (result->count("remove_from_config") && result->count("list_models")) {
ss << "error parsing options - --list_models cannot be used with --remove_from_config" << std::endl;
return std::make_pair(OVMS_EX_USAGE, ss.str());
}
if (result->count("remove_from_config") && result->count("model_path")) {
ss << "error parsing options - --model_path cannot be used with --remove_from_config" << std::endl;
return std::make_pair(OVMS_EX_USAGE, ss.str());
}
#pragma warning(push)
#pragma warning(disable : 4129)
if (result->count("version")) {
std::string project_name(PROJECT_NAME);
std::string project_version(PROJECT_VERSION);
ss << project_name + " " + project_version << std::endl;
ss << "OpenVINO backend " << ovms::getOpenVINOVersion() << std::endl;
const char* genaiVersion = ovms::getGenAIVersion();
if (genaiVersion[0] != '\0') {
ss << "OpenVINO GenAI backend " << genaiVersion << std::endl;
}
ss << "Bazel build flags: " << BAZEL_BUILD_FLAGS << std::endl;
#pragma warning(pop)
return std::make_pair(OVMS_EX_OK, ss.str());
}
if (result->count("help") || result->arguments().size() == 0) {
ss << options->help({"", "multi model", "single model", "pull hf model"}) << std::endl;
ss << configOptions->help({CONFIG_MANAGEMENT_HELP_GROUP}) << std::endl;
GraphCLIParser parser1;
RerankGraphCLIParser parser2;
EmbeddingsGraphCLIParser parser3;
ImageGenerationGraphCLIParser imageGenParser;
TextToSpeechGraphCLIParser ttsParser;
SpeechToTextGraphCLIParser sttParser;
parser1.printHelp();
parser2.printHelp();
parser3.printHelp();
imageGenParser.printHelp();
return std::make_pair(OVMS_EX_OK, ss.str());
}
return true;
} catch (const std::exception& e) {
ss << "error parsing options: " << e.what() << std::endl;
return std::make_pair(OVMS_EX_USAGE, ss.str());
}
}
void CLIParser::prepareServer(ServerSettingsImpl& serverSettings) {
// Server settings
serverSettings.startedWithCLI = true;
// list models mode
if (result->count("list_models")) {
serverSettings.serverMode = LIST_MODELS_MODE;
std::cout << "Listing models in repository..." << std::endl;
serverSettings.hfSettings.downloadPath = result->operator[]("model_repository_path").as<std::string>();
std::cout << "Model repository path: " << serverSettings.hfSettings.downloadPath << std::endl;
return;
}
std::string defaultConfigPath = "";
const char* envModelRepoPath = std::getenv("OVMS_MODEL_REPOSITORY_PATH");
if (envModelRepoPath != nullptr && std::string(envModelRepoPath).length() > 0) {
defaultConfigPath = std::string(envModelRepoPath) + "/config.json";
}
if (result->count("add_to_config")) {
serverSettings.serverMode = MODIFY_CONFIG_MODE;
serverSettings.exportConfigType = ENABLE_MODEL;
}
if (result->count("remove_from_config")) {
serverSettings.serverMode = MODIFY_CONFIG_MODE;
serverSettings.exportConfigType = DISABLE_MODEL;
}
serverSettings.grpcPort = result->operator[]("port").as<uint32_t>();
serverSettings.restPort = result->operator[]("rest_port").as<uint32_t>();
serverSettings.metricsEnabled = result->operator[]("metrics_enable").as<bool>();
serverSettings.metricsList = result->operator[]("metrics_list").as<std::string>();
serverSettings.filesystemPollWaitMilliseconds = result->operator[]("file_system_poll_wait_seconds").as<uint32_t>() * 1000;
serverSettings.sequenceCleanerPollWaitMinutes = result->operator[]("sequence_cleaner_poll_wait_minutes").as<uint32_t>();
serverSettings.resourcesCleanerPollWaitSeconds = result->operator[]("custom_node_resources_cleaner_interval_seconds").as<uint32_t>();
serverSettings.grpcWorkers = result->operator[]("grpc_workers").as<uint32_t>();
if (result->count("log_level"))
serverSettings.logLevel = result->operator[]("log_level").as<std::string>();
if (result->count("log_path"))
serverSettings.logPath = result->operator[]("log_path").as<std::string>();
if (result->count("grpc_channel_arguments"))
serverSettings.grpcChannelArguments = result->operator[]("grpc_channel_arguments").as<std::string>();
if (result != nullptr && result->count("cache_dir")) {
serverSettings.cacheDir = result->operator[]("cache_dir").as<std::string>();
}
if (result->count("cpu_extension")) {
serverSettings.cpuExtensionLibraryPath = result->operator[]("cpu_extension").as<std::string>();
}
if (result->count("allowed_media_domains")) {
serverSettings.allowedMediaDomains = result->operator[]("allowed_media_domains").as<std::vector<std::string>>();
}
if (result->count("allowed_local_media_path")) {
serverSettings.allowedLocalMediaPath = result->operator[]("allowed_local_media_path").as<std::string>();
}
if (result->count("grpc_bind_address"))
serverSettings.grpcBindAddress = result->operator[]("grpc_bind_address").as<std::string>();
if (result->count("rest_bind_address"))
serverSettings.restBindAddress = result->operator[]("rest_bind_address").as<std::string>();
if (result->count("grpc_max_threads"))
serverSettings.grpcMaxThreads = result->operator[]("grpc_max_threads").as<uint32_t>();
if (result->count("grpc_memory_quota"))
serverSettings.grpcMemoryQuota = result->operator[]("grpc_memory_quota").as<size_t>();
if (result->count("rest_workers"))
serverSettings.restWorkers = result->operator[]("rest_workers").as<uint32_t>();
#if (PYTHON_DISABLE == 0)
serverSettings.withPython = true;
#endif
#ifdef MTR_ENABLED
if (result->count("trace_path"))
serverSettings.tracePath = result->operator[]("trace_path").as<std::string>();
#endif
serverSettings.allowCredentials = result->operator[]("allow_credentials").as<bool>();
serverSettings.allowedOrigins = result->operator[]("allowed_origins").as<std::string>();
serverSettings.allowedMethods = result->operator[]("allowed_methods").as<std::string>();
serverSettings.allowedHeaders = result->operator[]("allowed_headers").as<std::string>();
std::filesystem::path apiKeyFile = result->operator[]("api_key_file").as<std::string>();
serverSettings.apiKey = "";
if (!apiKeyFile.empty()) {
std::ifstream file(apiKeyFile);
if (file.is_open()) {
std::getline(file, serverSettings.apiKey);
// Use first line and trim whitespace characters from both ends
size_t endpos = serverSettings.apiKey.find_last_not_of(" \n\r\t");
if (endpos != std::string::npos) {
serverSettings.apiKey = serverSettings.apiKey.substr(0, endpos + 1);
}
file.close();
} else {
throw std::filesystem::filesystem_error("Error reading API key file: Unable to open file ", apiKeyFile, std::error_code(2, std::generic_category()));
}
} else {
const char* envApiKey = std::getenv(API_KEY_ENV_VAR);
if (envApiKey != nullptr) {
serverSettings.apiKey = envApiKey;
}
}
}
void CLIParser::prepareModel(ModelsSettingsImpl& modelsSettings, HFSettingsImpl& hfSettings) {
// Model settings
if (result->count("model_name")) {
modelsSettings.modelName = result->operator[]("model_name").as<std::string>();
modelsSettings.userSetSingleModelArguments.push_back("model_name");
}
if (result->count("model_path")) {
modelsSettings.modelPath = result->operator[]("model_path").as<std::string>();
modelsSettings.userSetSingleModelArguments.push_back("model_path");
}
if (result->count("max_sequence_number")) {
modelsSettings.maxSequenceNumber = result->operator[]("max_sequence_number").as<uint32_t>();
modelsSettings.userSetSingleModelArguments.push_back("max_sequence_number");
}
if (result->count("batch_size")) {
modelsSettings.batchSize = result->operator[]("batch_size").as<std::string>();
modelsSettings.userSetSingleModelArguments.push_back("batch_size");
}
if (result->count("shape")) {
modelsSettings.shape = result->operator[]("shape").as<std::string>();
modelsSettings.userSetSingleModelArguments.push_back("shape");
}
if (result->count("layout")) {
modelsSettings.layout = result->operator[]("layout").as<std::string>();
modelsSettings.userSetSingleModelArguments.push_back("layout");
}
if (result->count("mean")) {
if (modelsSettings.layout.empty()) {
throw std::logic_error("error parsing options - --mean parameter requires --layout to be set");
}
modelsSettings.mean = result->operator[]("mean").as<std::string>();
modelsSettings.userSetSingleModelArguments.push_back("mean");
}
if (result->count("scale")) {
if (modelsSettings.layout.empty()) {
throw std::logic_error("error parsing options - --scale parameter requires --layout to be set");
}
modelsSettings.scale = result->operator[]("scale").as<std::string>();
modelsSettings.userSetSingleModelArguments.push_back("scale");
}
if (result->count("color_format")) {
if (modelsSettings.layout.empty()) {
throw std::logic_error("error parsing options - --color_format parameter requires --layout to be set");
}
modelsSettings.colorFormat = result->operator[]("color_format").as<std::string>();
modelsSettings.userSetSingleModelArguments.push_back("color_format");
}
if (result->count("precision")) {
if (modelsSettings.layout.empty()) {
throw std::logic_error("error parsing options - --precision parameter requires --layout to be set");
}
modelsSettings.precision = result->operator[]("precision").as<std::string>();
modelsSettings.userSetSingleModelArguments.push_back("precision");
}
if (result->count("model_version_policy")) {
modelsSettings.modelVersionPolicy = result->operator[]("model_version_policy").as<std::string>();
modelsSettings.userSetSingleModelArguments.push_back("model_version_policy");
}
if (result->count("nireq")) {
modelsSettings.nireq = result->operator[]("nireq").as<uint32_t>();
modelsSettings.userSetSingleModelArguments.push_back("nireq");
}
if (result->count("target_device")) {
modelsSettings.targetDevice = result->operator[]("target_device").as<std::string>();
if (isHFPullOrPullAndStart(this->result)) {
hfSettings.exportSettings.targetDevice = modelsSettings.targetDevice;
} else {
modelsSettings.userSetSingleModelArguments.push_back("target_device");
}
}
if (result->count("plugin_config")) {
modelsSettings.pluginConfig = result->operator[]("plugin_config").as<std::string>();
hfSettings.exportSettings.pluginConfig.manualString = modelsSettings.pluginConfig;
modelsSettings.userSetSingleModelArguments.push_back("plugin_config");
}
if (result->count("stateful")) {
modelsSettings.stateful = result->operator[]("stateful").as<bool>();
modelsSettings.userSetSingleModelArguments.push_back("stateful");
}
if (result->count("idle_sequence_cleanup")) {
modelsSettings.idleSequenceCleanup = result->operator[]("idle_sequence_cleanup").as<bool>();
modelsSettings.userSetSingleModelArguments.push_back("idle_sequence_cleanup");
}
if (result->count("low_latency_transformation")) {
modelsSettings.lowLatencyTransformation = result->operator[]("low_latency_transformation").as<bool>();
modelsSettings.userSetSingleModelArguments.push_back("low_latency_transformation");
}
if (result->count("config_path")) {
modelsSettings.configPath = result->operator[]("config_path").as<std::string>();
modelsSettings.userSetSingleModelArguments.push_back("config_path");
}
}
bool CLIParser::isHFPullOrPullAndStart(const std::unique_ptr<cxxopts::ParseResult>& result) {
return (result->count("pull") || result->count("task"));
}
void CLIParser::prepareGraph(ServerSettingsImpl& serverSettings, HFSettingsImpl& hfSettings, const std::string& modelName) {
// Ovms Pull models mode || pull and start models mode
if (isHFPullOrPullAndStart(this->result)) {
if (result->count("pull")) {
serverSettings.serverMode = HF_PULL_MODE;
} else {
serverSettings.serverMode = HF_PULL_AND_START_MODE;
}
if (result->count("gguf_filename")) {
hfSettings.ggufFilename = result->operator[]("gguf_filename").as<std::string>();
hfSettings.downloadType = GGUF_DOWNLOAD;
}
if (result->count("overwrite_models")) {
hfSettings.overwriteModels = result->operator[]("overwrite_models").as<bool>();
}
if (result->count("source_model")) {
hfSettings.sourceModel = result->operator[]("source_model").as<std::string>();
} else if (result->count("model_name")) {
hfSettings.sourceModel = result->operator[]("model_name").as<std::string>();
}
if ((result->count("weight-format") || result->count("extra_quantization_params")) && isOptimumCliDownload(hfSettings.sourceModel, hfSettings.ggufFilename)) {
hfSettings.downloadType = OPTIMUM_CLI_DOWNLOAD;
}
if (result->count("weight-format") && hfSettings.downloadType == GIT_CLONE_DOWNLOAD) {
throw std::logic_error("--weight-format parameter unsupported for OpenVINO models.");
}
if (result->count("extra_quantization_params") && hfSettings.downloadType == GIT_CLONE_DOWNLOAD) {
throw std::logic_error("--extra_quantization_params parameter unsupported for OpenVINO models.");
}
if (result->count("weight-format"))
hfSettings.exportSettings.precision = result->operator[]("weight-format").as<std::string>();
if (result->count("extra_quantization_params"))
hfSettings.exportSettings.extraQuantizationParams = result->operator[]("extra_quantization_params").as<std::string>();
if (result->count("vocoder"))
hfSettings.exportSettings.vocoder = result->operator[]("vocoder").as<std::string>();
hfSettings.downloadPath = result->operator[]("model_repository_path").as<std::string>();
if (result->count("task")) {
hfSettings.task = stringToEnum(result->operator[]("task").as<std::string>());
switch (hfSettings.task) {
case TEXT_GENERATION_GRAPH: {
if (std::holds_alternative<GraphCLIParser>(this->graphOptionsParser)) {
std::get<GraphCLIParser>(this->graphOptionsParser).prepare(serverSettings.serverMode, hfSettings, modelName);
} else {
throw std::logic_error("Tried to prepare graph settings without graph parser initialization");
}
break;
}
case EMBEDDINGS_GRAPH: {
if (std::holds_alternative<EmbeddingsGraphCLIParser>(this->graphOptionsParser)) {
std::get<EmbeddingsGraphCLIParser>(this->graphOptionsParser).prepare(serverSettings.serverMode, hfSettings, modelName);
} else {
throw std::logic_error("Tried to prepare graph settings without graph parser initialization");
}
break;
}
case RERANK_GRAPH: {
if (std::holds_alternative<RerankGraphCLIParser>(this->graphOptionsParser)) {
std::get<RerankGraphCLIParser>(this->graphOptionsParser).prepare(serverSettings.serverMode, hfSettings, modelName);
} else {
throw std::logic_error("Tried to prepare graph settings without graph parser initialization");
}
break;
}
case IMAGE_GENERATION_GRAPH: {
if (std::holds_alternative<ImageGenerationGraphCLIParser>(this->graphOptionsParser)) {
std::get<ImageGenerationGraphCLIParser>(this->graphOptionsParser).prepare(serverSettings, hfSettings, modelName);
} else {
throw std::logic_error("Tried to prepare graph settings without graph parser initialization");
}
break;
}
case TEXT_TO_SPEECH_GRAPH: {
if (std::holds_alternative<TextToSpeechGraphCLIParser>(this->graphOptionsParser)) {
std::get<TextToSpeechGraphCLIParser>(this->graphOptionsParser).prepare(serverSettings.serverMode, hfSettings, modelName);
} else {
throw std::logic_error("Tried to prepare graph settings without graph parser initialization");
}
break;
}
case SPEECH_TO_TEXT_GRAPH: {
if (std::holds_alternative<SpeechToTextGraphCLIParser>(this->graphOptionsParser)) {
std::get<SpeechToTextGraphCLIParser>(this->graphOptionsParser).prepare(serverSettings.serverMode, hfSettings, modelName);
} else {
throw std::logic_error("Tried to prepare graph settings without graph parser initialization");
}
break;
}
case UNKNOWN_GRAPH: {
throw std::logic_error("Error: --task parameter unsupported value: " + result->operator[]("task").as<std::string>());
break;
}
}
} else {
if (std::holds_alternative<GraphCLIParser>(this->graphOptionsParser)) {
std::get<GraphCLIParser>(this->graphOptionsParser).prepare(serverSettings.serverMode, hfSettings, modelName);
} else {
throw std::logic_error("Tried to prepare graph settings without graph parser initialization");
}
}
if (!serverSettings.cacheDir.empty()) {
hfSettings.exportSettings.pluginConfig.cacheDir = serverSettings.cacheDir;
}
// No pull nor pull and start mode
} else {
if (result->count("weight-format")) {
throw std::logic_error("--weight-format parameter unsupported for Openvino huggingface organization models.");
}
if (result->count("extra_quantization_params")) {
throw std::logic_error("--extra_quantization_params parameter unsupported for Openvino huggingface organization models.");
}
}
}
void CLIParser::prepareConfigExport(ModelsSettingsImpl& modelsSettings) {
// Export config.json mode
if (result->count("model_name")) {
modelsSettings.modelName = result->operator[]("model_name").as<std::string>();
}
if (result->count("model_path")) {
modelsSettings.modelPath = result->operator[]("model_path").as<std::string>();
} else if (!result->operator[]("model_repository_path").as<std::string>().empty() && result->count("model_name")) {
modelsSettings.modelPath = FileSystem::joinPath({result->operator[]("model_repository_path").as<std::string>(), modelsSettings.modelName});
}
std::string defaultConfigPath = "";
const char* envModelRepoPath = std::getenv("OVMS_MODEL_REPOSITORY_PATH");
if (envModelRepoPath != nullptr && std::string(envModelRepoPath).length() > 0) {
defaultConfigPath = FileSystem::joinPath({std::string(envModelRepoPath), "config.json"});
}
if (result->count("add_to_config") || result->count("remove_from_config")) {
if (result->count("config_path")) {
modelsSettings.configPath = result->operator[]("config_path").as<std::string>();
modelsSettings.userSetSingleModelArguments.push_back("config_path");
} else {
modelsSettings.configPath = defaultConfigPath;
std::cout << "Using default config path: " << modelsSettings.configPath << std::endl;
}
}
}
void CLIParser::prepareGraphStart(HFSettingsImpl& hfSettings, ModelsSettingsImpl& modelsSettings) {
// Ovms pull and start models mode
// Model settings
if (result->count("model_name")) {
modelsSettings.modelName = result->operator[]("model_name").as<std::string>();
} else {
modelsSettings.modelName = hfSettings.sourceModel;
}
modelsSettings.modelPath = FileSystem::joinPath({hfSettings.downloadPath, hfSettings.sourceModel});
}
void CLIParser::prepare(ServerSettingsImpl* serverSettings, ModelsSettingsImpl* modelsSettings) {
if (nullptr == result) {
throw std::logic_error("Tried to prepare server and model settings without parse result");
}
this->prepareServer(*serverSettings);
this->prepareModel(*modelsSettings, serverSettings->hfSettings);
this->prepareGraph(*serverSettings, serverSettings->hfSettings, modelsSettings->modelName);
if (serverSettings->serverMode == HF_PULL_AND_START_MODE)
this->prepareGraphStart(serverSettings->hfSettings, *modelsSettings);
if (serverSettings->exportConfigType != UNKNOWN_MODEL)
this->prepareConfigExport(*modelsSettings);
}
} // namespace ovms