huggingface / huggingface/candle

Phi4 mini multiple runs

Open
#3,523 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
21k
Forks
1.8k
Avg merge
16h 42m
Merged PRs (30d)
25

Description

Model: Phi3 ("microsoft/Phi-4-mini-instruct")
Code adapted from: https://github.com/huggingface/candle/tree/main/candle-examples/examples/phi

When running the example code multiple times without reloading the model, it appears that something becomes corrupted, causing the output to become repetitive or sometimes even complete garbage. Before each run, the tokenizer is cleared and the KV caches are reset. The code has been tested on both CUDA and Metal, and the behavior is similar.

````
self.tokenizer.clear();
self.pipeline.clear_kv_cache();

let mut tokens = self
.tokenizer
.tokenizer()
.encode(prompt, true)
.map_err(anyhow::Error::msg)?
.get_ids()
.to_vec();

let mut generated_tokens = 0usize;
let start_gen = std::time::Instant::now();
let mut pos = 0;
let mut response = String::new();
for index in 0..MAX_TOKENS {
let context_size = if index > 0 { 1 } else { tokens.len() };
let ctxt = &tokens[tokens.len().saturating_sub(context_size)..];
let input = Tensor::new(ctxt, self.pipeline.device())?.unsqueeze(0)?;
let logits = self.pipeline.forward(&input, pos)?.i((.., 0, ..))?;
let logits = logits.squeeze(0)?.to_dtype(DType::F32)?;

let start_at = tokens.len().saturating_sub(64);
let logits = candle_transformers::utils::apply_repeat_penalty(
&logits,
1.1,
&tokens[start_at..],
)?;

let next_token = self.logits_processor.sample(&logits)?;
tokens.push(next_token);
generated_tokens += 1;
if next_token == self.eos_token {
break;
}
if let Some(t) = self.tokenizer.next_token(next_token)? {
response.push_str(&t);
}
pos += context_size;
}
if let Some(rest) = self.tokenizer.decode_rest().map_err(anyhow::Error::msg)? {
response.push_str(&rest);
}
````

Here is a sample output of the first run:
````
To set up the Rust programming language environment, follow these steps:

1. Install Homebrew:
- First, make sure you have Ruby and Xcode Command Line Tools installed since they provide necessary tools like `xcode-select --install` or using a package manager such as MacPorts.

2. Use Brew to install Rust's Cargo tool (which includes the compiler):
```shell
$ brew update && brew upgrade
```

3. Install Rust:
- Run this command in your terminal:

```bash
$ curl https://sh.rustup.ci.FOREAGENT.com//bin/rustc -- -y >> /dev/null
```

4. Add the current shell to your path, then install cargo by running these commands:
1. In a new terminal window:

```shell
$ echo 'export CARGO_HOME=$HOME/.cargo' | tee -a ~/.bash_profile # (for zsh use .zprofile)
```
2. Source this file in any other open terminals and check the Rust installation with `rustup --version`.
3. To ensure that you have installed everything correctly, run:

1. Check version of your rust compiler by running:
```shell
$ cargo --help # to see help on 'cargo'
```
```

4. Create a new project:
- create a folder in the main directory and add .rs file there using this command.

5. Run with these steps

- Compile code, build executable or run it if necessary by creating `Cargo.toml` for configuration.

6. Check your working setup of Rust development environment is now ready to use on macOS.
````

And here is the second run:
````
To set up your environment for developing with Rust on the Mac, follow these steps:

1. **Install Homebrew:**

If you do not have it already installed:

- Open Terminal.

- Paste this command and hit enter:
```sh

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

```

2. **Update your system with the latest version of Xcode:**

This will ensure you have all necessary build tools:

- Open Terminal.

- Run this command to install or update `xcode-select --install`.

3. **Install Rust using rustup (Rust's official installer):**

In the terminal, run:

```sh

curl https://sh.rustup.rs -sSf | sh -s -- -y

```

4. **Configure your current shell to use `rustc` and manage Cargo from a specific location:**

Rust uses these environment variables by default:

```
$ rustc --version
target triple = x86_64-unknown-linux-gnu

cargo check --locked -v1 (target)

name : the current project root.

set to ``root``. If you have a specific subfolder, then this can be found in 0/7 by default:
0xcode is not currently compiled using your compilers as it cannot use and therefore uses the first result of the results that were obtained with `cargo -v`.

target for compiling

: ``target``. (default)

For example, this will generate a file called "t \texttt on 0/7 by default is an output from /dev system which can be accessed via commands or manually.

if the first line in your current folder that you have just created with as shown above

`target` (default) to use for compiling and building of `` target.

- If a new project, then this would make it is also possible 0/7 by default on which will be compiled using ".

If not set the value in your current active environment variables are used directly with these two commands.

- To determine what `target` (default) to use for compiling and building of `` target. This can either a new project, or if you have an existing one already present that is being worked on from

You do not need any other than it only needs in the current environment variables which are set directly with these two commands:

- To determine what `target` (default) to use for compiling and building of `` target. This can either a new, or if you have an existing one already present that is being worked on from.

If not do so this default value then the current active environment variables will be set directly with these two commands:

- To determine what `target` (default) to use for compiling and building of `` target. This can either a new, or if you have an existing already present that is being worked on from.

If not do so by using this default value then the current active environment variables will be set directly with these two commands:

- To determine what `target` (default) to use for compiling and building of `` target. This can either a new, or if you have an existing already present that is being worked on from.

0/7

If this default value then the current active environment variables are set directly with these two commands:

- To determine what `target` (default) to use for compiling and building of `` target. This can either a new, or if you have an existing already present that is being worked on from.

0/7

If this default value then the current active environment variables will be set directly with these two commands:

- To determine what `target` (default) to use for compiling and building of `` target. This can either a new, or if you have an existing already present that is being worked on from.

0/7

If this default value then the current active environment variables will be set directly with these two commands:

- To determine what `target` (default) to use for compiling and building of `` target. This can either a new, or if you have an existing already present that is being worked on from.

0/7

If this default value then the current active environment variables will be set directly with these two commands:

- To determine what `target` (default) to use for compiling and building of `` target. This can either a new, or if you have an already existing present that is being worked on from.

0/7

If this default value then the current active environment variables will be set directly with these two commands:

- To determine what `target` (default) to use for compiling and building of `` target. This can either a new, or if you have an already existing present that is being worked on from.
````

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the Phi example at candle-examples/examples/phi and trace the repeated-run path around tokenizer.clear(), pipeline.clear_kv_cache(), and forward(). Reproduce the first and second generations on CUDA or Metal, then verify that repeated runs produce non-corrupted output without reloading the model.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.