project updated

This commit is contained in:
Paulo Coutinho 2023-04-15 21:32:10 -03:00
parent 74f5899df4
commit eea154c9c8
6 changed files with 1107 additions and 5 deletions

13
MOBILE.md Normal file
View file

@ -0,0 +1,13 @@
# Build for macOS
To build for macOS execute on terminal:
```
./scripts/build-macos.sh
```
# Local tests
```
./scripts/build-macos.sh && cp build/examples/main/Release/libmain.dylib ~/Developer/workspaces/flutter/aichat/macos/Vendor/libmain.dylib
```

1028
cmake/ios.toolchain.cmake Normal file

File diff suppressed because it is too large Load diff

View file

@ -177,7 +177,7 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
params.n_parts = std::stoi(argv[i]);
} else if (arg == "-h" || arg == "--help") {
gpt_print_usage(argc, argv, default_params);
exit(0);
return true;
} else if (arg == "--random-prompt") {
params.random_prompt = true;
} else if (arg == "--in-prefix") {
@ -189,13 +189,13 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
} else {
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
gpt_print_usage(argc, argv, default_params);
exit(1);
return false;
}
}
if (invalid_param) {
fprintf(stderr, "error: invalid parameter for argument: %s\n", arg.c_str());
gpt_print_usage(argc, argv, default_params);
exit(1);
return false;
}
return true;

View file

@ -1,4 +1,4 @@
set(TARGET main)
add_executable(${TARGET} main.cpp)
add_library(${TARGET} SHARED main.cpp)
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
target_compile_features(${TARGET} PRIVATE cxx_std_11)

View file

@ -16,6 +16,11 @@
#include <string>
#include <vector>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <unistd.h>
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
#include <signal.h>
#include <unistd.h>
@ -41,7 +46,57 @@ void sigint_handler(int signo) {
}
#endif
int main(int argc, char ** argv) {
#if _WIN32
#define LLAMA_EXPORT extern "C" __declspec(dllexport)
#else
#define LLAMA_EXPORT extern "C" __attribute__((visibility("default"))) __attribute__((used))
#endif
bool connected = false;
LLAMA_EXPORT
int llama_main(int argc, char ** argv) {
if (!connected) {
connected = true;
// params
printf("argc = %d\n", argc);
for (int i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
fflush(stdout);
// configure the sockaddr_in structure
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); // IP address of the host (in this case, localhost)
addr.sin_port = htons(5567); // socket port
// create the socket
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
return 1;
}
// connect the socket to the sockaddr_in structure
if (connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
return 1;
}
// redirect the output of printf to the socket
if (dup2(sockfd, STDOUT_FILENO) == -1) {
return 1;
}
// redirect the output of printf to the socket
if (dup2(sockfd, STDERR_FILENO) == -1) {
return 1;
}
}
gpt_params params;
params.model = "models/llama-7B/ggml-model.bin";

6
scripts/build-macos.sh Executable file
View file

@ -0,0 +1,6 @@
rm -rf build
mkdir -p build
cmake -S . -B build -GXcode -DCMAKE_TOOLCHAIN_FILE=cmake/ios.toolchain.cmake \
-DPLATFORM=MAC_UNIVERSAL \
-DBUILD_SHARED_LIBS=YES
cmake --build build --config Release