Skip to content
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

Rename linear projection #37

Merged
merged 6 commits into from
Aug 20, 2024
Merged

Rename linear projection #37

merged 6 commits into from
Aug 20, 2024

Conversation

NohTow
Copy link
Collaborator

@NohTow NohTow commented Aug 20, 2024

This PR refactors the old LinearProjection module to better extend the existing sentence transformer layer.

This allows to unify the names so the conversion from one lib checkpoint to another is easier (as the only difference here is in the activation function and the forward function being called on all the tokens instead of just the pooled one).

I changed a bit the behavior of the model creation:

  • If there is no dense layer at all, create one (with the provided target size, default at 128)
  • If there is one dense layer (from ST or giga-cherche), if a target size is provided, check that it matches it, if not, create a new layer with the target. If no target size is provided or it corresponds, we plainly load the layer (including all of its config). If it's a layer from ST, we translate it to a giga-cherche dense layer to override the activation function and the forward behavior.

model = Dense(**config)
if os.path.exists(os.path.join(input_path, "model.safetensors")):
load_safetensors_model(model, os.path.join(input_path, "model.safetensors"))
else:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove the else and return the model in the if after load_safetensors_model
It will enable to designed model_load_state_dict

@@ -294,6 +316,18 @@ def __init__(
def load(input_path) -> "ColBERT":
return ColBERT(model_name_or_path=input_path)

def num_modules(self) -> int:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can rename this function by

def __len__(self) -> int: 
    return len(_self.modules)

then you can call len(self) to get the actual number of modules

and self[1].get_sentence_embedding_dimension() != embedding_size
):
logger.warning(
f"The checkpoint contains a dense layer but with incorrect dimension. Replacing it with a Dense layer with output dimensions ({hidden_size}, {embedding_size})"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to print the actual embedding dimension you found self[1].get_sentence_embedding_dimension()

def num_modules(self) -> int:
return len(self._modules)

def convert_dense_layer_from_sentence_transformer(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a docstring here ?

return features

@staticmethod
def from_sentence_transformers(dense_st: DenseSentenceTransformer):
Copy link
Collaborator

@raphaelsty raphaelsty Aug 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth to add a docstring here to explain that the Sentence Transformer has an activation function and we don't want one

@raphaelsty raphaelsty merged commit c572111 into main Aug 20, 2024
1 of 2 checks passed
@raphaelsty raphaelsty deleted the rename_linear_projection branch August 22, 2024 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants