Views
No views yet
pip install hf-hub-ctranslate2>=2.12.0 ctranslate2>=3.17.11# from transformers import AutoTokenizer
2model_name = "michaelfeil/ct2fast-all-MiniLM-L12-v2"
3model_name_orig="sentence-transformers/all-MiniLM-L12-v2"
4
5from hf_hub_ctranslate2 import EncoderCT2fromHfHub
6model = EncoderCT2fromHfHub(
7 # load in int8 on CUDA
8 model_name_or_path=model_name,
9 device="cuda",
10 compute_type="int8_float16"
11)
12outputs = model.generate(
13 text=["I like soccer", "I like tennis", "The eiffel tower is in Paris"],
14 max_length=64,
15) # perform downstream tasks on outputs
16outputs["pooler_output"]
17outputs["last_hidden_state"]
18outputs["attention_mask"]
19
20# alternative, use SentenceTransformer Mix-In
21# for end-to-end Sentence embeddings generation
22# (not pulling from this CT2fast-HF repo)
23
24from hf_hub_ctranslate2 import CT2SentenceTransformer
25model = CT2SentenceTransformer(
26 model_name_orig, compute_type="int8_float16", device="cuda"
27)
28embeddings = model.encode(
29 ["I like soccer", "I like tennis", "The eiffel tower is in Paris"],
30 batch_size=32,
31 convert_to_numpy=True,
32 normalize_embeddings=True,
33)
34print(embeddings.shape, embeddings)
35scores = (embeddings @ embeddings.T) * 100
36
37# Hint: you can also host this code via REST API and
38# via github.com/michaelfeil/infinity
39
40compute_type=int8_float16 for device="cuda"compute_type=int8 for device="cpu"LLama-2 -> removed <pad> token.