-
Notifications
You must be signed in to change notification settings - Fork 302
Add Dense
layer for 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
Conversation
Apparently, `candle` expects the tensors to be contiguous on Metal when performing 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.
If `--dense-path` was not allowed, that would prevent users from using other `Dense` layers when available as per e.g. https://huggingface.co/NovaSearch/stella_en_400M_v5, that contains different directories for different `Dense` layers with different output vector dimensionality as `2_Dense_<dims>/`.
Dense
, DenseLayer
and DenseConfig
to handle 2_Dense/
Dense
layer in 2_Dense/
modules
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Dense
layer in 2_Dense/
modulesDense
layer for 2_Dense/
modules
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 at https://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