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