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

Add multi OS binary portability support via Cosmopolitan libc toolchain #32

Closed
wants to merge 10 commits into from

Conversation

trholding
Copy link
Contributor

It is possible to compile once and run the binary on multiple OS via Cosmopolitan libc toolchain. I have added the necessary pre processor directives to enable that. Please have a look.

These are the features that come nearly free:

  • Executable that runs on

    • GNU/Systemd
    • FreeBSD
    • OpenBSD
    • NetBSD
    • XNU's Not UNIX
    • Bare Metal (-D COSMO_METAL) [Not fully functional]
    • Windows
  • Runs on

    • ARM64 via Blink x86-64 emulation (-D COSMO_BLINK)
    • x86_64
  • Standalone

    • Embedded model in executable (-D COSMO_ZIP)

Instructions

Get and build the comopolitan libc toolchain:

Follow instructions at https://github.com/jart/cosmopolitan

Or do:

sudo mkdir -p /opt
sudo chmod 1777 /opt
git clone https://github.com/jart/cosmopolitan /opt/cosmo
cd /opt/cosmo
make -j8 toolchain
mkdir -p /opt/cosmos/bin
export PATH="/opt/cosmos/bin:$PATH"
echo 'PATH="/opt/cosmos/bin:$PATH"' >>~/.profile
sudo ln -sf /opt/cosmo/tool/scripts/cosmocc /opt/cosmos/bin/cosmocc
sudo ln -sf /opt/cosmo/tool/scripts/cosmoc++ /opt/cosmos/bin/cosmoc++

Example build to generate a Actually Portable Executable (APE):

$ cosmocc -O3 -Ofast -funsafe-math-optimizations -ffast-math -D COSMO_BLINK \
-D COSMO_METAL -D COSMO_ZIP -o run.com run.c -lm

Add model.bin and tokenizer.bin to executable:
$ zip run.com out/model.bin
$ zip run.com tokenizer.bin

Run or copy to any supported system and run:

If model is embedded:

$ ./run.com

Else

$ ./run.com model.bin

Added multi OS binary portability support via Cosmopolitan libc toolchain

These are the features:

+ Executable that runs on
  + GNU/Systemd
  + FreeBSD
  + OpenBSD
  + NetBSD
  + XNU's Not UNIX
  + Bare Metal (-D COSMO_METAL) [Not fully functional]
  + Windows

+ Runs on 
  + ARM64 via Blink x86-64 emulation (-D COSMO_BLINK)
  + x86_64

+ Standalone
  + Embedded model in executable (-D COSMO_ZIP)

Instructions

Get and build the comopolitan libc toolchain:

Follow instructions at https://github.com/jart/cosmopolitan

Or do:

```
sudo mkdir -p /opt
sudo chmod 1777 /opt
git clone https://github.com/jart/cosmopolitan /opt/cosmo
cd /opt/cosmo
make -j8 toolchain
mkdir -p /opt/cosmos/bin
export PATH="/opt/cosmos/bin:$PATH"
echo 'PATH="/opt/cosmos/bin:$PATH"' >>~/.profile
sudo ln -sf /opt/cosmo/tool/scripts/cosmocc /opt/cosmos/bin/cosmocc
sudo ln -sf /opt/cosmo/tool/scripts/cosmoc++ /opt/cosmos/bin/cosmoc++
```

Example build to generate a Actually Portable Executable (APE):

```
$ cosmocc -O3 -Ofast -funsafe-math-optimizations -ffast-math -D COSMO_BLINK \
-D COSMO_METAL -D COSMO_ZIP -o run.com run.c -lm

Add model.bin and tokenizer.bin to executable:
$ zip run.com out/model.bin
$ zip run.com tokenizer.bin
```

Run or copy to any supported system and run:

```
If model is embedded:

$ ./run.com

Else

$ ./run.com model.bin

```
Added instructions on how to enable binary portability with Cosmopolitan libc toolchain.
@trholding
Copy link
Contributor Author

On a low end x86_64 machine:
runx86

On a aarch64 (runs very slow due to emulation)
runAarch64

@karpathy
Copy link
Owner

There's a lot going on here, I won't merge sorry. I am happy to link to your fork from the Readme though, if you'd like (see bottom of the Readme and the contributing section).

@karpathy karpathy closed this Jul 27, 2023
@trholding
Copy link
Contributor Author

trholding commented Jul 27, 2023

There's a lot going on here, I won't merge sorry. I am happy to link to your fork from the Readme though, if you'd like (see bottom of the Readme and the contributing section).

I agree that it would destroy the simplicity / elegance of llama2.c. I am continuing a friendly fork which will sync from yours but also will keep adding experimental features there with the final goal of booting a system directly into llama2.

Here is the link to the fork: https://github.com/trholding/llama2.c

I'd be honored if you would list it in the Readme section in the hope that some folks may find it useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants