Intel/intel-optimized-model-for-embeddings-v1
012
1---2license: mit3---4# intel-optimized-model-for-embeddings-v15 6This is a text embedding model model: It maps sentences & paragraphs to a 512 dimensional dense vector space and can be used for tasks like clustering or semantic search. For sample code that uses this model in a torch serve container see [Intel-Optimized-Container-for-Embeddings](https://github.com/intel/Intel-Optimized-Container-for-Embeddings).7 8## Usage9 10Install the required packages:11```12pip install -U torch==2.3.1+cpu --extra-index-url https://download.pytorch.org/whl/cpu13pip install -U transformers==4.42.4 intel-extension-for-pytorch==2.3.10014```15 16Use the following example below to load the model with the transformers library, tokenize the text, run the model, and apply pooling to the output.17 18```19import torch20from transformers import AutoTokenizer, AutoModel21import intel_extension_for_pytorch as ipex22 23def mean_pooling(model_output, attention_mask):24 token_embeddings = model_output[0]25 input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()26 return torch.sum(token_embeddings * input_mask_expanded,27 1) / torch.clamp(input_mask_expanded.sum(1),28 min=1e-9)29 30# load model31tokenizer = AutoTokenizer.from_pretrained('Intel/intel-optimized-model-for-embeddings-v1')32model = AutoModel.from_pretrained('Intel/intel-optimized-model-for-embeddings-v1', 33 torchscript=True)34model.eval()35 36# do IPEX optimization37batch_size = 138seq_length=51239vocab_size = model.config.vocab_size40sample_input = {"input_ids": torch.randint(vocab_size, size=[batch_size, seq_length]),41 "token_type_ids": torch.zeros(size=[batch_size, seq_length],42 dtype=torch.int),43 "attention_mask": torch.randint(1, size=[batch_size, seq_length])}44text = "This is a test."45model = ipex.optimize(model, level="O1",auto_kernel_selection=True,46 conv_bn_folding=False, dtype=torch.bfloat16)47 48with torch.no_grad(), torch.cpu.amp.autocast(cache_enabled=False,49 dtype=torch.bfloat16):50 # Compile model51 model = torch.jit.trace(model, example_kwarg_inputs=sample_input,52 check_trace=False, strict=False)53 model = torch.jit.freeze(model)54 55 # Call model56 tokenized_text = tokenizer(text, padding=True, truncation=True, return_tensors='pt')57 model_output = model(**tokenized_text)58 sentence_embeddings = mean_pooling(model_output,tokenized_text['attention_mask'])59 embeddings = sentence_embeddings[0].tolist()60 61# Embeddings output62print(embeddings)63```64 65## Model Details66 67### Model Description68 69This model was fine-tuned using the [sentence-transformers](https://github.com/UKPLab/sentence-transformers) library 70based on the [BERT-Medium_L-8_H-512_A-8](https://huggingface.co/nreimers/BERT-Medium_L-8_H-512_A-8) model71 using [UAE-Large-V1](https://huggingface.co/WhereIsAI/UAE-Large-V1) as a teacher.72 73 74### Training Datasets75 76| Dataset | Description | License |77| ------------- |:-------------:| -----:|78| beir/dbpedia-entity | DBpedia-Entity is a standard test collection for entity search over the DBpedia knowledge base. | CC BY-SA 3.0 license |79| beir/nq | To help spur development in open-domain question answering, the Natural Questions (NQ) corpus has been created, along with a challenge website based on this data. | CC BY-SA 3.0 license |80| beir/scidocs | SciDocs is a new evaluation benchmark consisting of seven document-level tasks ranging from citation prediction, to document classification and recommendation. | CC-BY-SA-4.0 license |81| beir/trec-covid | TREC-COVID followed the TREC model for building IR test collections through community evaluations of search systems. | CC-BY-SA-4.0 license |82| beir/touche2020 | Given a question on a controversial topic, retrieve relevant arguments from a focused crawl of online debate portals. | CC BY 4.0 license |83| WikiAnswers | The WikiAnswers corpus contains clusters of questions tagged by WikiAnswers users as paraphrases. | MIT |84| Cohere/wikipedia-22-12-en-embeddings Dataset | The Cohere/Wikipedia dataset is a processed version of the wikipedia-22-12 dataset. It is English only, and the articles are broken up into paragraphs. | Apache 2.0 |85| MLNI | GLUE, the General Language Understanding Evaluation benchmark (https://gluebenchmark.com/) is a collection of resources for training, evaluating, and analyzing natural language understanding systems. | MIT |86 