Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

project updated #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions MOBILE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Build for macOS

To build for macOS execute on terminal:

```
./scripts/build-macos.sh
```
1,028 changes: 1,028 additions & 0 deletions cmake/ios.toolchain.cmake

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions examples/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
57 changes: 56 additions & 1 deletion examples/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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";

Expand Down
6 changes: 6 additions & 0 deletions scripts/build-macos.sh
Original file line number Diff line number Diff line change
@@ -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