From 783e1ca091b17329c2e62a384315fd68eb290ffb Mon Sep 17 00:00:00 2001 From: cryscan Date: Sun, 10 Dec 2023 14:04:28 +0800 Subject: [PATCH] Pack converter. --- .github/workflows/release.yml | 16 +++++++-- Cargo.lock | 65 ++++++++++++++++++++++++++++++----- Cargo.toml | 12 +++++-- README.md | 28 +++++++-------- src/api/file.rs | 2 +- src/bin/converter.rs | 36 +++++++++++++++++++ src/main.rs | 2 +- 7 files changed, 130 insertions(+), 31 deletions(-) create mode 100644 src/bin/converter.rs diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b0287ae7..5fb68bb0 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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: @@ -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 diff --git a/Cargo.lock b/Cargo.lock index 70ff55b3..dade399b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -63,7 +63,7 @@ dependencies = [ "futures-util", "itertools", "log", - "memmap", + "memmap2", "qp-trie", "rayon", "regex", @@ -1291,13 +1291,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" [[package]] -name = "memmap" -version = "0.7.0" +name = "memmap2" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6585fd95e7bb50d6cc31e20d4cf9afb4e2ba16c5846fc76793f11218da9c475b" +checksum = "f49388d20533534cd19360ad3d6a7dadc885944aa802ba3995040c5ec11288c6" dependencies = [ "libc", - "winapi", ] [[package]] @@ -1340,6 +1339,12 @@ dependencies = [ "unicase", ] +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.7.1" @@ -1395,6 +1400,37 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "num-bigint" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.17" @@ -1711,6 +1747,18 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "216080ab382b992234dda86873c18d4c48358f5cfcb70fd693d7f6f2131b628b" +[[package]] +name = "repugnant-pickle" +version = "0.0.1" +source = "git+https://github.com/KerfuffleV2/repugnant-pickle?tag=v0.0.1#803fb57f25eeceb1e882297d2f479be744f4aa4b" +dependencies = [ + "anyhow", + "nom", + "num-bigint", + "once_cell", + "zip", +] + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -2351,8 +2399,8 @@ checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f" [[package]] name = "web-rwkv" -version = "0.4.4" -source = "git+https://github.com/cryscan/web-rwkv?tag=v0.4.4#ac0b4506e94c174c042ae81af3677e3cd48cd9b7" +version = "0.4.5" +source = "git+https://github.com/cryscan/web-rwkv?tag=v0.4.5#7586824b27ea4ba940ceb4284f9ec8aa07ad920c" dependencies = [ "ahash", "anyhow", @@ -2366,6 +2414,7 @@ dependencies = [ "lazy_static", "log", "regex", + "repugnant-pickle", "safetensors", "serde", "serde_json", @@ -2377,7 +2426,7 @@ dependencies = [ [[package]] name = "web-rwkv-derive" version = "0.2.0" -source = "git+https://github.com/cryscan/web-rwkv?tag=v0.4.4#ac0b4506e94c174c042ae81af3677e3cd48cd9b7" +source = "git+https://github.com/cryscan/web-rwkv?tag=v0.4.5#7586824b27ea4ba940ceb4284f9ec8aa07ad920c" dependencies = [ "quote", "syn 2.0.39", diff --git a/Cargo.toml b/Cargo.toml index 2553424b..9ade3eff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } @@ -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 diff --git a/README.md b/README.md index dd2101d7..97251ab6 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/api/file.rs b/src/api/file.rs index 9e81efb3..970103a0 100644 --- a/src/api/file.rs +++ b/src/api/file.rs @@ -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}; diff --git a/src/bin/converter.rs b/src/bin/converter.rs new file mode 100644 index 00000000..20564f63 --- /dev/null +++ b/src/bin/converter.rs @@ -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, +} + +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(()) +} diff --git a/src/main.rs b/src/main.rs index 59603d7b..f8ae52c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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};