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

Add new_with_tokenizer for SentenceEmbeddingsModel #407

Merged
merged 5 commits into from
Aug 3, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add option to create sentence embeddings model with custom tokenizer
  • Loading branch information
guillaume-be committed Jul 31, 2023
commit 43878841112e9dcb4ca588015bfd63c9072b4c4b
66 changes: 45 additions & 21 deletions src/pipelines/sentence_embeddings/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,32 +177,18 @@ impl SentenceEmbeddingsModel {
///
/// * `config` - `SentenceEmbeddingsConfig` object containing the resource references (model, vocabulary, configuration) and device placement (CPU/GPU)
pub fn new(config: SentenceEmbeddingsConfig) -> Result<Self, RustBertError> {
let SentenceEmbeddingsConfig {
modules_config_resource,
sentence_bert_config_resource,
tokenizer_config_resource,
tokenizer_vocab_resource,
tokenizer_merges_resource,
transformer_type,
transformer_config_resource,
transformer_weights_resource,
pooling_config_resource,
dense_config_resource,
dense_weights_resource,
device,
} = config;

let modules =
SentenceEmbeddingsModulesConfig::from_file(modules_config_resource.get_local_path()?)
.validate()?;

// Setup tokenizer
let transformer_type = config.transformer_type;
let tokenizer_vocab_resource = &config.tokenizer_vocab_resource;
let tokenizer_merges_resource = &config.tokenizer_merges_resource;
let tokenizer_config_resource = &config.tokenizer_config_resource;
let sentence_bert_config_resource = &config.tokenizer_config_resource;
let tokenizer_config = SentenceEmbeddingsTokenizerConfig::from_file(
tokenizer_config_resource.get_local_path()?,
);
let sentence_bert_config = SentenceEmbeddingsSentenceBertConfig::from_file(
sentence_bert_config_resource.get_local_path()?,
&sentence_bert_config_resource.get_local_path()?,
);

let tokenizer = TokenizerOption::from_file(
transformer_type,
tokenizer_vocab_resource
Expand All @@ -222,6 +208,44 @@ impl SentenceEmbeddingsModel {
tokenizer_config.add_prefix_space,
)?;

Self::new_with_tokenizer(config, tokenizer)
}

/// Build a new `ONNXCausalGenerator` from a `GenerateConfig` and `TokenizerOption`.
///
/// A tokenizer must be provided by the user and can be customized to use non-default settings.
///
/// # Arguments
///
/// * `config` - `SentenceEmbeddingsConfig` object containing the resource references (model, vocabulary, configuration) and device placement (CPU/GPU)
/// * `tokenizer` - `TokenizerOption` tokenizer to use for question answering.
pub fn new_with_tokenizer(
config: SentenceEmbeddingsConfig,
tokenizer: TokenizerOption,
) -> Result<Self, RustBertError> {
let SentenceEmbeddingsConfig {
modules_config_resource,
sentence_bert_config_resource,
tokenizer_config_resource: _,
tokenizer_vocab_resource: _,
tokenizer_merges_resource: _,
transformer_type,
transformer_config_resource,
transformer_weights_resource,
pooling_config_resource,
dense_config_resource,
dense_weights_resource,
device,
} = config;

let modules =
SentenceEmbeddingsModulesConfig::from_file(modules_config_resource.get_local_path()?)
.validate()?;

let sentence_bert_config = SentenceEmbeddingsSentenceBertConfig::from_file(
sentence_bert_config_resource.get_local_path()?,
);

// Setup transformer
let mut var_store = nn::VarStore::new(device);
let transformer_config = ConfigOption::from_file(
Expand Down