Skip to content

Commit

Permalink
fix: copy neccessary files to executable
Browse files Browse the repository at this point in the history
  • Loading branch information
namchuai committed Aug 30, 2024
1 parent f74fd1b commit 44eefd6
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 31 deletions.
52 changes: 43 additions & 9 deletions engine/commands/engine_init_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
// clang-format on
#include "utils/cuda_toolkit_utils.h"
#include "utils/engine_matcher_utils.h"
#if defined(_WIN32) || defined(__linux__)
#include "utils/file_manager_utils.h"
#endif

namespace commands {

Expand Down Expand Up @@ -106,17 +109,46 @@ bool EngineInitCmd::Exec() const {
}}};

DownloadService download_service;
download_service.AddDownloadTask(downloadTask, [](const std::string&
absolute_path,
bool unused) {
download_service.AddDownloadTask(downloadTask, [this](
const std::string&
absolute_path,
bool unused) {
// try to unzip the downloaded file
std::filesystem::path downloadedEnginePath{absolute_path};
LOG_INFO << "Downloaded engine path: "
<< downloadedEnginePath.string();

archive_utils::ExtractArchive(
downloadedEnginePath.string(),
downloadedEnginePath.parent_path().parent_path().string());
std::filesystem::path extract_path =
downloadedEnginePath.parent_path().parent_path();

archive_utils::ExtractArchive(downloadedEnginePath.string(),
extract_path.string());
#if defined(_WIN32) || defined(__linux__)
// FIXME: hacky try to copy the file. Remove this when we are able to set the library path
auto engine_path = extract_path / engineName_;
LOG_INFO << "Source path: " << engine_path.string();
auto executable_path =
file_manager_utils::GetExecutableFolderContainerPath();
for (const auto& entry :
std::filesystem::recursive_directory_iterator(engine_path)) {
if (entry.is_regular_file() &&
entry.path().extension() != ".gz") {
std::filesystem::path relative_path =
std::filesystem::relative(entry.path(), engine_path);
std::filesystem::path destFile =
executable_path / relative_path;

std::filesystem::create_directories(destFile.parent_path());
std::filesystem::copy_file(
entry.path(), destFile,
std::filesystem::copy_options::overwrite_existing);

std::cout << "Copied: " << entry.path().filename().string()
<< " to " << destFile.string() << std::endl;
}
}
std::cout << "DLL copying completed successfully." << std::endl;
#endif

// remove the downloaded file
// TODO(any) Could not delete file on Windows because it is currently hold by httplib(?)
Expand All @@ -138,7 +170,7 @@ bool EngineInitCmd::Exec() const {
const std::string cuda_toolkit_file_name = "cuda.tar.gz";
const std::string download_id = "cuda";

// TODO: we don't have API to retrieve list of cuda toolkit dependencies atm
// TODO: we don't have API to retrieve list of cuda toolkit dependencies atm because we hosting it at jan
// will have better logic after https://github.com/janhq/cortex/issues/1046 finished
// for now, assume that we have only 11.7 and 12.4
auto suitable_toolkit_version = "";
Expand All @@ -147,9 +179,11 @@ bool EngineInitCmd::Exec() const {
suitable_toolkit_version = "12.4";
} else {
// llamacpp
if (cuda_driver_version.starts_with("11.")) {
auto cuda_driver_semver =
semantic_version_utils::SplitVersion(cuda_driver_version);
if (cuda_driver_semver.major == 11) {
suitable_toolkit_version = "11.7";
} else if (cuda_driver_version.starts_with("12.")) {
} else if (cuda_driver_semver.major == 12) {
suitable_toolkit_version = "12.4";
}
}
Expand Down
16 changes: 13 additions & 3 deletions engine/utils/engine_matcher_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,19 @@ inline std::string GetSuitableCudaVariant(
bestMatchMinor = variantMinor;
}
}
} else if (cuda_version.empty() && selectedVariant.empty()) {
// If no CUDA version is provided, select the variant without any CUDA in the name
selectedVariant = variant;
}
}

// If no CUDA version is provided, select the variant without any CUDA in the name
if (selectedVariant.empty()) {
LOG_WARN
<< "No suitable CUDA variant found, selecting a variant without CUDA";
for (const auto& variant : variants) {
if (variant.find("cuda") == std::string::npos) {
selectedVariant = variant;
LOG_INFO << "Found variant without CUDA: " << selectedVariant << "\n";
break;
}
}
}

Expand Down
65 changes: 46 additions & 19 deletions engine/utils/semantic_version_utils.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,61 @@
#include <trantor/utils/Logger.h>
#include <sstream>
#include <vector>

namespace semantic_version_utils {
inline std::vector<int> SplitVersion(const std::string& version) {
std::vector<int> parts;
std::stringstream ss(version);
std::string part;
struct SemVer {
int major;
int minor;
int patch;
};

while (std::getline(ss, part, '.')) {
parts.push_back(std::stoi(part));
inline SemVer SplitVersion(const std::string& version) {
if (version.empty()) {
LOG_WARN << "Passed in version is empty!";
}
SemVer semVer = {0, 0, 0}; // default value
std::stringstream ss(version);
std::string part;

while (parts.size() < 3) {
parts.push_back(0);
int index = 0;
while (std::getline(ss, part, '.') && index < 3) {
int value = std::stoi(part);
switch (index) {
case 0:
semVer.major = value;
break;
case 1:
semVer.minor = value;
break;
case 2:
semVer.patch = value;
break;
}
++index;
}

return parts;
return semVer;
}

inline int CompareSemanticVersion(const std::string& version1,
const std::string& version2) {
std::vector<int> v1 = SplitVersion(version1);
std::vector<int> v2 = SplitVersion(version2);

for (size_t i = 0; i < 3; ++i) {
if (v1[i] < v2[i])
return -1;
if (v1[i] > v2[i])
return 1;
}
SemVer v1 = SplitVersion(version1);
SemVer v2 = SplitVersion(version2);

if (v1.major < v2.major)
return -1;
if (v1.major > v2.major)
return 1;

if (v1.minor < v2.minor)
return -1;
if (v1.minor > v2.minor)
return 1;

if (v1.patch < v2.patch)
return -1;
if (v1.patch > v2.patch)
return 1;

return 0;
}

Expand Down

0 comments on commit 44eefd6

Please sign in to comment.