char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Xuan-Son Nguyencommon: add `subproc.h` wrapper, disabled on android/ios (#26102)d2a818231

char/aliceai-t5-moe
2.8 KiB143 linesraw
1#include "subproc.h"
2
3bool common_subproc::is_supported() {
4#ifdef LLAMA_SUBPROCESS
5    return true;
6#else
7    return false;
8#endif
9}
10
11#ifdef LLAMA_SUBPROCESS
12
13static std::vector<char *> to_cstr_vec(const std::vector<std::string> & v) {
14    std::vector<char *> r;
15    r.reserve(v.size() + 1);
16    for (const auto & s : v) {
17        r.push_back(const_cast<char *>(s.c_str()));
18    }
19    r.push_back(nullptr);
20    return r;
21}
22
23common_subproc::~common_subproc() {
24    if (is_created) {
25        subprocess_destroy(&proc);
26        is_created = false;
27    }
28}
29
30bool common_subproc::create(
31        const std::vector<std::string> & args,
32        int options,
33        const std::vector<std::string> & env,
34        const char * cwd) {
35    auto argv = to_cstr_vec(args);
36
37    int result;
38    if (env.empty() && cwd == nullptr) {
39        result = subprocess_create(argv.data(), options, &proc);
40    } else {
41        auto envp = to_cstr_vec(env);
42        result = subprocess_create_ex(argv.data(), options, env.empty() ? nullptr : envp.data(), cwd, &proc);
43    }
44
45    is_created = result == 0;
46    return is_created;
47}
48
49bool common_subproc::has_handle() const {
50    if (!is_created) {
51        return false;
52    }
53#if defined(_WIN32)
54    return proc.hProcess != nullptr;
55#else
56    return proc.child > 0;
57#endif
58}
59
60bool common_subproc::alive() {
61    return is_created && subprocess_alive(&proc);
62}
63
64FILE * common_subproc::stdin_file() {
65    return is_created ? subprocess_stdin(&proc) : nullptr;
66}
67
68FILE * common_subproc::stdout_file() {
69    return is_created ? subprocess_stdout(&proc) : nullptr;
70}
71
72FILE * common_subproc::stderr_file() {
73    return is_created ? subprocess_stderr(&proc) : nullptr;
74}
75
76void common_subproc::close_stdin() {
77    if (is_created && proc.stdin_file) {
78        fclose(proc.stdin_file);
79        proc.stdin_file = nullptr;
80    }
81}
82
83void common_subproc::terminate() {
84    if (has_handle()) {
85        subprocess_terminate(&proc);
86    }
87}
88
89int common_subproc::join() {
90    int exit_code = -1;
91    if (is_created) {
92        subprocess_join(&proc, &exit_code);
93        subprocess_destroy(&proc);
94        is_created = false;
95    }
96    return exit_code;
97}
98
99#else // !LLAMA_SUBPROCESS
100
101common_subproc::~common_subproc() = default;
102
103bool common_subproc::create(
104        const std::vector<std::string> &,
105        int,
106        const std::vector<std::string> &,
107        const char *) {
108    (void)(proc);
109    (void)(is_created);
110    return false;
111}
112
113bool common_subproc::has_handle() const {
114    return false;
115}
116
117bool common_subproc::alive() {
118    return false;
119}
120
121FILE * common_subproc::stdin_file() {
122    return nullptr;
123}
124
125FILE * common_subproc::stdout_file() {
126    return nullptr;
127}
128
129FILE * common_subproc::stderr_file() {
130    return nullptr;
131}
132
133void common_subproc::close_stdin() {
134}
135
136void common_subproc::terminate() {
137}
138
139int common_subproc::join() {
140    return -1;
141}
142
143#endif // LLAMA_SUBPROCESS