|
| 1 | +//*************************************************************************** |
| 2 | +// Copyright 2025 Intel Corporation |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +//***************************************************************************** |
| 16 | +#include "hf_pull_model_module.hpp" |
| 17 | + |
| 18 | +#include <string> |
| 19 | +#include <utility> |
| 20 | +#include <variant> |
| 21 | + |
| 22 | +#include "../config.hpp" |
| 23 | +#include "libgit2.hpp" |
| 24 | +#include "../graph_export/graph_export.hpp" |
| 25 | +#include "../logging.hpp" |
| 26 | +#include "../module_names.hpp" |
| 27 | +#include "../status.hpp" |
| 28 | +#include "../stringutils.hpp" |
| 29 | + |
| 30 | +namespace ovms { |
| 31 | +const std::string DEFAULT_EMPTY_ENV_VALUE{""}; |
| 32 | + |
| 33 | +const std::string HfPullModelModule::GIT_SERVER_CONNECT_TIMEOUT_ENV{"GIT_SERVER_CONNECT_TIMEOUT_MS"}; |
| 34 | +const std::string HfPullModelModule::GIT_SERVER_TIMEOUT_ENV{"GIT_SERVER_TIMEOUT_MS"}; |
| 35 | +// GIT_OPT_SET_SERVER_TIMEOUT |
| 36 | + |
| 37 | +static std::string getEnvReturnOrDefaultIfNotSet(const std::string& envName, const std::string& defaultValue = DEFAULT_EMPTY_ENV_VALUE) { |
| 38 | + std::string value = defaultValue; |
| 39 | + const char* envValue = std::getenv(envName.c_str()); |
| 40 | + if (envValue) { |
| 41 | + value = std::string(envValue); |
| 42 | + SPDLOG_DEBUG("{} environment variable set. Using value: {};", envName, value); |
| 43 | + } else { |
| 44 | + SPDLOG_DEBUG("{} environment variable not set. Using default value: {};", envName, defaultValue); |
| 45 | + } |
| 46 | + return value; |
| 47 | +} |
| 48 | + |
| 49 | +HfPullModelModule::HfPullModelModule() {} |
| 50 | + |
| 51 | +#define RETURN_IF_ERROR(StatusOr) \ |
| 52 | + do { \ |
| 53 | + if (std::holds_alternative<Status>(StatusOr)) { \ |
| 54 | + return std::get<Status>(StatusOr); \ |
| 55 | + } \ |
| 56 | + } while (0) |
| 57 | + |
| 58 | +static std::variant<ovms::Status, Libgit2Options> prepareLibgit2Opts() { |
| 59 | + Libgit2Options opts; |
| 60 | + std::string timeoutString = getEnvReturnOrDefaultIfNotSet(HfPullModelModule::GIT_SERVER_CONNECT_TIMEOUT_ENV, "1000"); |
| 61 | + auto timeoutOpt = ovms::stoi32(timeoutString); |
| 62 | + if (!timeoutOpt.has_value()) { |
| 63 | + SPDLOG_ERROR("Set invalid value for libgit2 server connection timeout:{}", timeoutString); |
| 64 | + return StatusCode::HF_FAILED_TO_INIT_LIBGIT2; |
| 65 | + } |
| 66 | + bool isHttpsProxyUsed = !getEnvReturnOrDefaultIfNotSet("https_proxy").empty(); |
| 67 | + if (isHttpsProxyUsed) { |
| 68 | + if (timeoutOpt.value() != 0) |
| 69 | + SPDLOG_WARN("We are not able to set connection timeout when proxy is used"); |
| 70 | + } else { |
| 71 | + opts.serverConnectTimeoutMs = timeoutOpt.value(); |
| 72 | + } |
| 73 | + timeoutString = getEnvReturnOrDefaultIfNotSet(HfPullModelModule::GIT_SERVER_TIMEOUT_ENV, "0"); |
| 74 | + timeoutOpt = ovms::stoi32(timeoutString); |
| 75 | + if (!timeoutOpt.has_value()) { |
| 76 | + SPDLOG_ERROR("Set invalid value for libgit2 server timeout:{}", timeoutString); |
| 77 | + return StatusCode::HF_FAILED_TO_INIT_LIBGIT2; |
| 78 | + } |
| 79 | + opts.serverTimeoutMs = timeoutOpt.value(); |
| 80 | + return opts; |
| 81 | +} |
| 82 | + |
| 83 | +std::variant<ovms::Status, std::unique_ptr<Libgt2InitGuard>> createGuard() { |
| 84 | + auto optsOrError = prepareLibgit2Opts(); |
| 85 | + RETURN_IF_ERROR(optsOrError); |
| 86 | + auto initGuard = std::make_unique<Libgt2InitGuard>(std::get<Libgit2Options>(optsOrError)); |
| 87 | + if (initGuard->status < 0) { |
| 88 | + SPDLOG_ERROR("Failed to init libgit2: {}", initGuard->errMsg.c_str()); |
| 89 | + return StatusCode::HF_FAILED_TO_INIT_LIBGIT2; |
| 90 | + } |
| 91 | + return std::move(initGuard); |
| 92 | +} |
| 93 | + |
| 94 | +Status HfPullModelModule::start(const ovms::Config& config) { |
| 95 | + state = ModuleState::STARTED_INITIALIZE; |
| 96 | + SPDLOG_INFO("{} starting", HF_MODEL_PULL_MODULE_NAME); |
| 97 | + auto guardOrError = createGuard(); |
| 98 | + RETURN_IF_ERROR(guardOrError); |
| 99 | + this->hfSettings = config.getServerSettings().hfSettings; |
| 100 | + state = ModuleState::INITIALIZED; |
| 101 | + SPDLOG_INFO("{} started", HF_MODEL_PULL_MODULE_NAME); |
| 102 | + return StatusCode::OK; |
| 103 | +} |
| 104 | + |
| 105 | +Status HfPullModelModule::clone() const { |
| 106 | + auto guardOrError = createGuard(); |
| 107 | + if (std::holds_alternative<Status>(guardOrError)) { |
| 108 | + return std::get<Status>(guardOrError); |
| 109 | + } |
| 110 | + HfDownloader hfDownloader(this->hfSettings.sourceModel, this->hfSettings.downloadPath, this->GetHfEndpoint(), this->GetHfToken(), this->GetProxy()); |
| 111 | + auto status = hfDownloader.cloneRepository(); |
| 112 | + if (!status.ok()) { |
| 113 | + return status; |
| 114 | + } |
| 115 | + GraphExport graphExporter; |
| 116 | + status = graphExporter.createGraphFile(this->hfSettings.downloadPath, this->hfSettings.graphSettings); |
| 117 | + if (!status.ok()) { |
| 118 | + return status; |
| 119 | + } |
| 120 | + return StatusCode::OK; |
| 121 | +} |
| 122 | + |
| 123 | +const std::string HfPullModelModule::GetProxy() const { |
| 124 | + return getEnvReturnOrDefaultIfNotSet("https_proxy"); |
| 125 | +} |
| 126 | + |
| 127 | +const std::string HfPullModelModule::GetHfToken() const { |
| 128 | + return getEnvReturnOrDefaultIfNotSet("HF_TOKEN"); |
| 129 | +} |
| 130 | + |
| 131 | +const std::string HfPullModelModule::GetHfEndpoint() const { |
| 132 | + std::string hfEndpoint = getEnvReturnOrDefaultIfNotSet("HF_ENDPOINT", "huggingface.co"); |
| 133 | + if (!endsWith(hfEndpoint, "/")) { |
| 134 | + hfEndpoint.append("/"); |
| 135 | + } |
| 136 | + return hfEndpoint; |
| 137 | +} |
| 138 | + |
| 139 | +void HfPullModelModule::shutdown() { |
| 140 | + if (state == ModuleState::SHUTDOWN) |
| 141 | + return; |
| 142 | + state = ModuleState::STARTED_SHUTDOWN; |
| 143 | + SPDLOG_INFO("{} shutting down", HF_MODEL_PULL_MODULE_NAME); |
| 144 | + state = ModuleState::SHUTDOWN; |
| 145 | + SPDLOG_INFO("{} shutdown", HF_MODEL_PULL_MODULE_NAME); |
| 146 | +} |
| 147 | + |
| 148 | +HfPullModelModule::~HfPullModelModule() { |
| 149 | + this->shutdown(); |
| 150 | +} |
| 151 | + |
| 152 | +} // namespace ovms |
0 commit comments