- Notifications
You must be signed in to change notification settings - Fork288
AddDense
layer in2_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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Conversation
Apparently, `candle` expects the tensors to be contiguous on Metal whenperforming 2D matrix multiplication
Required for some models as e.g.https://huggingface.co/sentence-transformers/LaBSE
@alvarobartt Hi! Just for your reference, you may already be aware, Stella v5 model uses |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Looks good, I think we can simplify a bit the parsing part.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
If `--dense-path` was not allowed, that would prevent users from usingother `Dense` layers when available as per e.g.https://huggingface.co/NovaSearch/stella_en_400M_v5, that containsdifferent directories for different `Dense` layers with different outputvector dimensionality as `2_Dense_<dims>/`.
Dense
,DenseLayer
andDenseConfig
to handle2_Dense/
Dense
layer in2_Dense/
modules
Uh oh!
There was an error while loading.Please reload this page.
What does this PR do?
This PR adds support for
2_Dense/
modules, since some models as e.g.https://huggingface.co/sentence-transformers/LaBSE require the extraDense
module i.e., an extraLinear
layer on top of the pooled embeddings, when generating the embeddings.So on, this PR introduces the
DenseLayer
trait, impl forDense
and addsDenseConfig
, which are models with basically a singleLinear
layer, pulling the configuration from2_Dense/config.json
and the model weights from2_Dense/model.safetensors
.Note
The
2_Dense/
is only required when generating embeddings, meaning that it will only apply to theEmbedding
model type, whereas theReranker
andClassifier
are not affected by this addition, so on, neither therank
orpredict
methods for the given backend.This PR solves the issue recently reported athttps://discuss.huggingface.co/t/inference-result-not-aligned-with-local-version-of-same-model-and-revision/160514.
Additionally, this PR also fixes a shape mismatch issue produced when performing matrix multiplication of 2D tensors on Metal devices due to the
candle
Metal kernels expecting the tensors to be contiguous. It seems that the error only arises on Metal for 2D tensors, where as for e.g. 3D tensors it seems to be working just fine without having to use.contiguous()
(which is expensive as it needs to clone the tensor).Reproduce
To ensure that the implementation was working fine and producing successful results i.e.,
allclose
like checks are true, and the cosine similarity is 1.0 (or as close as possible), the following test has been run:torch
,transformers
,sentence-transformers
,accelerate
andnumpy
):It should produce the following on any combination of device (CPU, MPS, CUDA) and dtype (float32, float16):
Before submitting
Who can review?
@Narsil