Skip to content

Commit

Permalink
Add screenshots.
Browse files Browse the repository at this point in the history
  • Loading branch information
cryscan committed Sep 5, 2023
1 parent 3a6afc4 commit c92bcdd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ This is an inference engine for the [language model of RWKV](https://github.com/
- Int8 quantization.
- Very fast.

![chat](screenshots/chat.gif)
![batch](screenshots/batch.gif)

## Compile and Run
1. [Install Rust](https://rustup.rs/).
2. Run `cargo run --release --example gen` to generate 100 tokens and measure the time cost.
Expand Down Expand Up @@ -49,5 +52,16 @@ You may download the official RWKV World series models from [HuggingFace](https:

An already-converted 0.4B model can be found under [`assets/models`](assets/models/RWKV-4-World-0.4B-v1-20230529-ctx4096.st).

## Troubleshoot
- "thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: HeaderTooLarge'"

Your model is broken, mainly because you cloned the repo but did not set up git-lfs.Please download the model manually and overwrite that one in `assets/models`.

- "thread 'main' panicked at 'wgpu error: Validation Error"

It's most likely that you are using the D3D backend.
Please use Vulkan backend instead.


## Credits
- Tokenizer is implemented by [@koute](https://github.com/koute/rwkv_tokenizer).
18 changes: 15 additions & 3 deletions examples/batch.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use anyhow::Result;
use clap::Parser;
#[cfg(not(debug_assertions))]
use crossterm::terminal::{
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
};
#[cfg(not(debug_assertions))]
use dialoguer::{theme::ColorfulTheme, Select};
use itertools::Itertools;
use memmap2::Mmap;
#[cfg(not(debug_assertions))]
use ratatui::{
prelude::{Constraint, CrosstermBackend, Direction, Layout},
style::{Color, Modifier, Style, Stylize},
Expand All @@ -16,7 +18,7 @@ use ratatui::{
};
use std::{
fs::File,
io::{BufReader, Read, Stdout},
io::{BufReader, Read},
path::PathBuf,
str::FromStr,
};
Expand Down Expand Up @@ -96,21 +98,24 @@ fn load_model(context: &Context, model: PathBuf, quant: Option<u64>) -> Result<M
Ok(model)
}

fn setup_terminal() -> Result<Terminal<CrosstermBackend<Stdout>>> {
#[cfg(not(debug_assertions))]
fn setup_terminal() -> Result<Terminal<CrosstermBackend<std::io::Stdout>>> {
let mut stdout = std::io::stdout();
enable_raw_mode()?;
crossterm::execute!(stdout, EnterAlternateScreen)?;
Ok(Terminal::new(CrosstermBackend::new(stdout))?)
}

fn restore_terminal(terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> Result<()> {
#[cfg(not(debug_assertions))]
fn restore_terminal(terminal: &mut Terminal<CrosstermBackend<std::io::Stdout>>) -> Result<()> {
disable_raw_mode()?;
crossterm::execute!(terminal.backend_mut(), LeaveAlternateScreen,)?;
Ok(terminal.show_cursor()?)
}

async fn run(cli: Cli) -> Result<()> {
let context = create_context().await?;
#[cfg(not(debug_assertions))]
let mut terminal = setup_terminal()?;

let tokenizer = load_tokenizer()?;
Expand Down Expand Up @@ -146,6 +151,7 @@ async fn run(cli: Cli) -> Result<()> {
.repeat((cli.batch + prompts.len() - 1) / prompts.len())[..cli.batch]
.to_vec();
loop {
#[cfg(not(debug_assertions))]
terminal.draw(|frame| {
let size = frame.size();

Expand Down Expand Up @@ -193,6 +199,11 @@ async fn run(cli: Cli) -> Result<()> {
}
})?;

#[cfg(debug_assertions)]
for (index, prompt) in prompts.iter().enumerate() {
println!("{index}: {prompt}");
}

let logits = model.run(&mut tokens, &state)?;
let probs = model.softmax(logits)?;
for (index, probs) in probs.into_iter().enumerate().filter(|(_, v)| !v.is_empty()) {
Expand All @@ -212,6 +223,7 @@ async fn run(cli: Cli) -> Result<()> {
}
}

#[cfg(not(debug_assertions))]
restore_terminal(&mut terminal)?;
Ok(())
}
Expand Down
Binary file added screenshots/batch.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/chat.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c92bcdd

Please sign in to comment.