Skip to content

Commit

Permalink
Pack converter.
Browse files Browse the repository at this point in the history
  • Loading branch information
cryscan committed Dec 10, 2023
1 parent f685dd9 commit 783e1ca
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 31 deletions.
16 changes: 13 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ env:
LINUX_TARGET: x86_64-unknown-linux-gnu

# Space separated paths to include in the archive.
RELEASE_BINS: ai00_server converter
RELEASE_ADDS: README.md README_zh.md LICENSE assets

jobs:
Expand Down Expand Up @@ -82,20 +83,29 @@ jobs:
- name: Create tarball (Linux)
if: matrix.build == 'linux'
run: |
mv ./target/${{ env.LINUX_TARGET }}/release/${{ env.RELEASE_BIN }} ./dist/${{ env.RELEASE_BIN }}
for bin in ${{ env.RELEASE_BINS }}
do
mv ./target/${{ env.LINUX_TARGET }}/release/${bin} ./dist/${bin}
done
mv ${{ env.RELEASE_ADDS }} ./dist
7z a -tzip ./${{ env.RELEASE_DIR }}/${{ env.RELEASE_BIN }}-${{ steps.get_version.outputs.VERSION }}-${{ env.LINUX_TARGET }}.zip ./dist
- name: Create tarball (Windows)
if: matrix.build == 'windows'
shell: bash
run: |
mv ./target/release/${{ env.RELEASE_BIN }}.exe ./dist/${{ env.RELEASE_BIN }}.exe
for bin in ${{ env.RELEASE_BINS }}
do
mv ./target/release/${bin}.exe ./dist/${bin}.exe
done
mv ${{ env.RELEASE_ADDS }} ./dist
7z a -tzip ./${{ env.RELEASE_DIR }}/${{ env.RELEASE_BIN }}-${{ steps.get_version.outputs.VERSION }}-${{ env.WINDOWS_TARGET }}.zip ./dist
- name: Create tarball (MacOS)
if: matrix.build == 'macos'
run: |
mv ./target/release/${{ env.RELEASE_BIN }} ./dist/${{ env.RELEASE_BIN }}
for bin in ${{ env.RELEASE_BINS }}
do
mv ./target/release/${bin} ./dist/${bin}
done
mv ${{ env.RELEASE_ADDS }} ./dist
7z a -tzip ./${{ env.RELEASE_DIR }}/${{ env.RELEASE_BIN }}-${{ steps.get_version.outputs.VERSION }}-${{ env.MACOS_TARGET }}.zip ./dist
- name: Upload Zip
Expand Down
65 changes: 57 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ exclude = ["assets"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[[bin]]
name = "converter"

[dependencies]
tower = { version = "0.4.13", features = ["full"] }
tower-http = { version = "0.5.0", features = ["full"] }
tokio = { version = "1", features = ["full"] }
# web-rwkv = "0.4.3"
web-rwkv = { git = "https://github.com/cryscan/web-rwkv", tag = "v0.4.4" }
memmap = "0.7"
memmap2 = "0.7"
bytemuck = "1"
regex = "1.8"
clap = { version = "4.3", features = ["derive"] }
Expand All @@ -39,6 +40,11 @@ tempfile = "3.6"
toml = "0.8.6"
sha2 = "0.10.8"

[dependencies.web-rwkv]
git = "https://github.com/cryscan/web-rwkv"
tag = "v0.4.5"
features = ["converter"]

[dependencies.axum]
version = "0.7.1"
default-features = false
Expand Down
28 changes: 13 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,19 @@ It only supports Safetensors models with the `.st` extension now. Models saved w

1. [Download the `.pth` model](https://huggingface.co/BlinkDL)

2. Clone or download the [convert_safetensors.py](./convert_safetensors.py) from this repository and install the corresponding dependencies.

3. Run the above program, specifying the input and output paths.

```bash
$ python convert_safetensors.py --input ./filename.pth --output ./filename.st
```

4. If you don't want to have python or torch installed, you can go to [`web-rwkv`](https://github.com/cryscan/web-rwkv/releases) and download the dependency-less converter `web-rwkv-converter`.
```bash
$ ./web-rwkv-converter --input /path/to/model.pth
```
5. Just like the steps mentioned above, place the model in the `.st` model in the `assets/models/` path and modify the model path in [`assets/Config.toml`](./assets/Config.toml)
2. In the [Release](https://github.com/cgisky1980/ai00_rwkv_server/releases) you could find an executable called `converter`. Run

```bash
$ ./converter --input /path/to/model.pth
```

3. If you are building from source, run

```bash
$ cargo run --release --bin converter -- --input /path/to/model.pth
```

4. Just like the steps mentioned above, place the model in the `.st` model in the `assets/models/` path and modify the model path in [`assets/Config.toml`](./assets/Config.toml)


## 📝Supported Arguments
Expand Down
2 changes: 1 addition & 1 deletion src/api/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{

use anyhow::Result;
use axum::{extract::State, Json};
use memmap::Mmap;
use memmap2::Mmap;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

Expand Down
36 changes: 36 additions & 0 deletions src/bin/converter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::{fs::File, path::PathBuf};

use anyhow::Result;
use clap::Parser;
use memmap2::Mmap;
use web_rwkv::converter::{convert_safetensors, RENAME, TRANSPOSE};

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[arg(short, long, value_name = "FILE")]
input: PathBuf,
#[arg(short, long, value_name = "FILE")]
output: Option<PathBuf>,
}

fn main() -> Result<()> {
let cli = Cli::parse();

let file = File::open(&cli.input)?;
let map = unsafe { Mmap::map(&file)? };

let output = cli.output.unwrap_or_else(|| {
let path = cli
.input
.parent()
.map(|p| p.to_path_buf())
.unwrap_or_default();
let stem = cli.input.file_stem().expect("please name the file");
let name: PathBuf = [&stem.to_string_lossy(), "st"].join(".").into();
path.join(name)
});
convert_safetensors(cli.input, &map, output, RENAME, TRANSPOSE)?;

Ok(())
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use clap::Parser;
use config::{AdapterOption, Config};
use flume::{Receiver, Sender};
use itertools::Itertools;
use memmap::Mmap;
use memmap2::Mmap;
use run::RuntimeUntyped;
use serde::{Deserialize, Serialize};
use tokio::sync::{Mutex, RwLock};
Expand Down

0 comments on commit 783e1ca

Please sign in to comment.