-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New parsing logic and model definition data types (#35)
- Loading branch information
1 parent
d9d513b
commit 7672805
Showing
25 changed files
with
590 additions
and
448 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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,50 @@ | ||
pub(crate) mod model; | ||
pub(crate) mod parse; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
const BERT_CONFIG_PATH: &str = "tests/fixtures/all-MiniLM-L6-v2"; | ||
const JINABERT_CONFIG_PATH: &str = "tests/fixtures/jina-embeddings-v2-base-en"; | ||
const DISTILBERT_CONFIG_PATH: &str = "tests/fixtures/multi-qa-distilbert-dot-v1"; | ||
|
||
use std::path::Path; | ||
|
||
use super::*; | ||
use crate::config::parse::parse_config; | ||
use crate::pooling::PoolingStrategy; | ||
use crate::Result; | ||
|
||
fn test_parse_config_helper(config_path: &str, expected_type: model::ModelType) -> Result<()> { | ||
let path = Path::new(config_path); | ||
|
||
let config = parse_config(path, None)?; | ||
|
||
assert_eq!(config.model_type, expected_type); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_parse_config_bert() -> Result<()> { | ||
test_parse_config_helper( | ||
BERT_CONFIG_PATH, | ||
crate::config::model::ModelType::Embedding(PoolingStrategy::Mean), | ||
) | ||
} | ||
|
||
#[test] | ||
fn test_parse_config_jinabert() -> Result<()> { | ||
test_parse_config_helper( | ||
JINABERT_CONFIG_PATH, | ||
crate::config::model::ModelType::Embedding(PoolingStrategy::Mean), | ||
) | ||
} | ||
|
||
#[test] | ||
fn test_parse_config_distilbert() -> Result<()> { | ||
test_parse_config_helper( | ||
DISTILBERT_CONFIG_PATH, | ||
crate::config::model::ModelType::Embedding(PoolingStrategy::Cls), | ||
) | ||
} | ||
} |
This file contains 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,65 @@ | ||
//! Embedding model configuration data model | ||
//! | ||
//! An embedder is defined by its model configuration (defined in the `config.json` in the root | ||
//! of a Hugging Face model repository) the model type, and the pooling strategy (optionally | ||
//! defined in a `1_Pooling/config.json` file in the model repository). | ||
use candle_transformers::models::bert::Config as _BertConfig; | ||
use candle_transformers::models::distilbert::Config as DistilBertConfig; | ||
use candle_transformers::models::jina_bert::Config as _JinaBertConfig; | ||
use serde::Deserialize; | ||
use std::collections::HashMap; | ||
|
||
use crate::pooling::PoolingStrategy; | ||
|
||
/// The base HF embedding model configuration. | ||
/// | ||
/// This represents the base fields present in a `config.json` for an embedding model. | ||
#[derive(Debug, Deserialize)] | ||
#[allow(dead_code)] | ||
pub(crate) struct HFConfig { | ||
pub architectures: Vec<String>, | ||
pub model_type: String, | ||
#[serde(alias = "n_positions")] | ||
pub max_position_embeddings: usize, | ||
#[serde(default)] | ||
pub pad_token_id: usize, | ||
pub id2label: Option<HashMap<String, String>>, | ||
pub label2id: Option<HashMap<String, usize>>, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[serde(untagged)] | ||
pub(crate) enum BertConfig { | ||
Bert(_BertConfig), | ||
JinaBert(_JinaBertConfig), | ||
} | ||
|
||
/// The given model type. | ||
/// | ||
/// Based on the `model_type` key in the `config.json`, the given variant enables the parser | ||
/// to know what specific model configuration data model to use when deserializing the non-base | ||
/// keys. | ||
#[derive(Deserialize)] | ||
#[serde(tag = "model_type", rename_all = "kebab-case")] | ||
pub(crate) enum ModelConfig { | ||
Bert(BertConfig), | ||
// XlmRoberta(BertConfig), | ||
// Camembert(BertConfig), | ||
// Roberta(BertConfig), | ||
#[serde(rename(deserialize = "distilbert"))] | ||
DistilBert(DistilBertConfig), | ||
} | ||
|
||
/// The embedding strategy used by a given model. | ||
#[derive(Debug, PartialEq, Clone)] | ||
pub enum ModelType { | ||
Classifier, | ||
Embedding(PoolingStrategy), | ||
} | ||
|
||
/// The model definition | ||
pub(crate) struct ModelDefinition { | ||
pub(crate) model_config: ModelConfig, | ||
pub(crate) model_type: ModelType, | ||
} |
Oops, something went wrong.