-
Notifications
You must be signed in to change notification settings - Fork 285
Add Dense
layer in 2_Dense/
modules
#660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alvarobartt
wants to merge
15
commits into
main
Choose a base branch
from
add-dense
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
73935bc
Fix `forward` pass on `Linear` for Metal devices
alvarobartt 1a59eaf
Add `Dense`, `DenseLayer` and `DenseConfig` to handle `2_Dense/`
alvarobartt b666d0e
Fix linting and update code-comment
alvarobartt 27adbb6
Run `pre-commit run --all-files`
alvarobartt 578ea3a
Merge branch 'main' into add-dense
alvarobartt 1f38d86
Add `DenseActivation` and handle `tanh` and `identity`
alvarobartt dcb3ee8
Run `pre-commit run --all-files`
alvarobartt c9cddf2
Add `--dense-path` argument (to be used within `CandleBackend`)
alvarobartt 070ef02
Fix warn/error messages on `2_Dense` downloads
alvarobartt 5ff72a6
Update `download_artifacts` in `candle/tests` to include `dense_path`
alvarobartt 1618559
Add `backends/candle/tests/test_dense.rs`
alvarobartt 144aec1
Rename with `serde` in `DenseActivation`
alvarobartt 0d1b698
Add comments in `DenseActivation`
alvarobartt a368460
Add missing `None` in `run` for `dense_path`
alvarobartt 4c855bc
Merge branch 'main' into add-dense
alvarobartt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use crate::layers::Linear; | ||
use candle::{Result, Tensor}; | ||
use candle_nn::VarBuilder; | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum DenseActivation { | ||
Tanh, | ||
Identity, | ||
} | ||
|
||
impl DenseActivation { | ||
pub fn forward(&self, x: &Tensor) -> Result<Tensor> { | ||
match self { | ||
Self::Tanh => x.tanh(), | ||
Self::Identity => Ok(x.clone()), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Deserialize)] | ||
pub struct DenseConfig { | ||
in_features: usize, | ||
out_features: usize, | ||
bias: bool, | ||
activation_function: Option<String>, | ||
} | ||
|
||
pub trait DenseLayer { | ||
fn forward(&self, hidden_states: &Tensor) -> Result<Tensor>; | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Dense { | ||
linear: Linear, | ||
activation: DenseActivation, | ||
span: tracing::Span, | ||
} | ||
|
||
impl Dense { | ||
pub fn load(vb: VarBuilder, config: &DenseConfig) -> Result<Self> { | ||
let weight = vb.get((config.out_features, config.in_features), "linear.weight")?; | ||
let bias = if config.bias { | ||
Some(vb.get(config.out_features, "linear.bias")?) | ||
} else { | ||
None | ||
}; | ||
|
||
// Here we cannot leverage HiddenAct, since the activation functions for the | ||
// 2_Dense/config.json are defined as PyTorch imports instead, so the deserialization would | ||
// be different, as well as the range of commonly used activation functions (mainly tanh | ||
// and identity) | ||
let activation = match config.activation_function { | ||
// e.g. https://huggingface.co/sentence-transformers/LaBSE/blob/main/2_Dense/config.json | ||
Some(ref act) if act == "torch.nn.modules.activation.Tanh" => DenseActivation::Tanh, | ||
// e.g. https://huggingface.co/NovaSearch/stella_en_400M_v5/blob/main/2_Dense/config.json | ||
Some(ref act) if act == "torch.nn.modules.linear.Identity" => DenseActivation::Identity, | ||
alvarobartt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_ => DenseActivation::Identity, | ||
}; | ||
|
||
let linear = Linear::new(weight, bias, None); | ||
|
||
Ok(Self { | ||
linear, | ||
activation, | ||
span: tracing::span!(tracing::Level::TRACE, "dense"), | ||
}) | ||
} | ||
} | ||
|
||
impl DenseLayer for Dense { | ||
fn forward(&self, hidden_states: &Tensor) -> Result<Tensor> { | ||
let _enter = self.span.enter(); | ||
|
||
let hidden_states = self.linear.forward(hidden_states)?; | ||
self.activation.forward(&hidden_states) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -405,6 +405,7 @@ fn get_backend_model_type( | |
} | ||
} | ||
}; | ||
|
||
Ok(text_embeddings_backend::ModelType::Embedding(pool)) | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.