Skip to content

Commit

Permalink
Make the whisper model cloneable (huggingface#1200)
Browse files Browse the repository at this point in the history
* Add a quantized variant of llama2.c

* Clippy fixes.

* Make the whisper model cloneable.
  • Loading branch information
LaurentMazare authored Oct 27, 2023
1 parent b318145 commit 85bea43
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion candle-transformers/src/models/whisper/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn embedding(vocab_size: usize, hidden_size: usize, vb: VarBuilder) -> Result<Em
//
// We wrap the `Linear` layer here to add some tracing so that it's easier to profile the resulting
// model.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Linear {
inner: candle_nn::Linear,
span: tracing::Span,
Expand Down Expand Up @@ -53,6 +53,7 @@ fn layer_norm(size: usize, vb: VarBuilder) -> Result<LayerNorm> {
}

// https://github.com/openai/whisper/blob/f572f2161ba831bae131364c3bffdead7af6d210/whisper/model.py#L62
#[derive(Debug, Clone)]
struct MultiHeadAttention {
query: Linear,
key: Linear,
Expand Down Expand Up @@ -162,6 +163,7 @@ impl MultiHeadAttention {
}

// https://github.com/openai/whisper/blob/f572f2161ba831bae131364c3bffdead7af6d210/whisper/model.py#L111
#[derive(Debug, Clone)]
struct ResidualAttentionBlock {
attn: MultiHeadAttention,
attn_ln: LayerNorm,
Expand Down Expand Up @@ -241,6 +243,7 @@ fn sinusoids(length: usize, channels: usize) -> Result<Tensor> {
}

// https://github.com/openai/whisper/blob/f572f2161ba831bae131364c3bffdead7af6d210/whisper/model.py#L143
#[derive(Debug, Clone)]
pub struct AudioEncoder {
conv1: Conv1d,
conv2: Conv1d,
Expand Down Expand Up @@ -316,6 +319,7 @@ impl AudioEncoder {
}

// https://github.com/openai/whisper/blob/f572f2161ba831bae131364c3bffdead7af6d210/whisper/model.py#L176
#[derive(Debug, Clone)]
pub struct TextDecoder {
token_embedding: Embedding,
positional_embedding: Tensor,
Expand Down Expand Up @@ -380,6 +384,7 @@ impl TextDecoder {
}

// https://github.com/openai/whisper/blob/f572f2161ba831bae131364c3bffdead7af6d210/whisper/model.py#L221
#[derive(Debug, Clone)]
pub struct Whisper {
pub encoder: AudioEncoder,
pub decoder: TextDecoder,
Expand Down
5 changes: 5 additions & 0 deletions candle-transformers/src/models/whisper/quantized_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn conv1d(
}

// https://github.com/openai/whisper/blob/f572f2161ba831bae131364c3bffdead7af6d210/whisper/model.py#L62
#[derive(Debug, Clone)]
struct MultiHeadAttention {
query: Linear,
key: Linear,
Expand Down Expand Up @@ -128,6 +129,7 @@ impl MultiHeadAttention {
}

// https://github.com/openai/whisper/blob/f572f2161ba831bae131364c3bffdead7af6d210/whisper/model.py#L111
#[derive(Debug, Clone)]
struct ResidualAttentionBlock {
attn: MultiHeadAttention,
attn_ln: LayerNorm,
Expand Down Expand Up @@ -206,6 +208,7 @@ fn sinusoids(length: usize, channels: usize) -> Result<Tensor> {
}

// https://github.com/openai/whisper/blob/f572f2161ba831bae131364c3bffdead7af6d210/whisper/model.py#L143
#[derive(Debug, Clone)]
pub struct AudioEncoder {
conv1: Conv1d,
conv2: Conv1d,
Expand Down Expand Up @@ -281,6 +284,7 @@ impl AudioEncoder {
}

// https://github.com/openai/whisper/blob/f572f2161ba831bae131364c3bffdead7af6d210/whisper/model.py#L176
#[derive(Debug, Clone)]
pub struct TextDecoder {
token_embedding: Embedding,
positional_embedding: Tensor,
Expand Down Expand Up @@ -347,6 +351,7 @@ impl TextDecoder {
}

// https://github.com/openai/whisper/blob/f572f2161ba831bae131364c3bffdead7af6d210/whisper/model.py#L221
#[derive(Debug, Clone)]
pub struct Whisper {
pub encoder: AudioEncoder,
pub decoder: TextDecoder,
Expand Down

0 comments on commit 85bea43

Please sign in to comment.