KomeijiForce/Inbedder-Pretrain-Data
Viewer • Updated • 198k • 69 • 1
How to use BrandonZYW/opt-1.3b-InBedder with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-generation", model="BrandonZYW/opt-1.3b-InBedder") # Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("BrandonZYW/opt-1.3b-InBedder")
model = AutoModelForCausalLM.from_pretrained("BrandonZYW/opt-1.3b-InBedder")How to use BrandonZYW/opt-1.3b-InBedder with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "BrandonZYW/opt-1.3b-InBedder"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "BrandonZYW/opt-1.3b-InBedder",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'docker model run hf.co/BrandonZYW/opt-1.3b-InBedder
How to use BrandonZYW/opt-1.3b-InBedder with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "BrandonZYW/opt-1.3b-InBedder" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "BrandonZYW/opt-1.3b-InBedder",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'docker run --gpus all \
--shm-size 32g \
-p 30000:30000 \
-v ~/.cache/huggingface:/root/.cache/huggingface \
--env "HF_TOKEN=<secret>" \
--ipc=host \
lmsysorg/sglang:latest \
python3 -m sglang.launch_server \
--model-path "BrandonZYW/opt-1.3b-InBedder" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "BrandonZYW/opt-1.3b-InBedder",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'How to use BrandonZYW/opt-1.3b-InBedder with Docker Model Runner:
docker model run hf.co/BrandonZYW/opt-1.3b-InBedder
InBedder🛌 is a text embedder that is designed to follow instructions. Instruction-following text embedder can capture characteristics of texts specified by user instructions. InBedder offers a novel viewpoint that treats the instruction as a question about the input text and encodes the expected answers to obtain the representation accordingly. We show that InBedder is aware of instructions with different evaluation tasks.
The following is a use case from https://github.com/zhang-yu-wei/InBedder/blob/main/UseCase.ipynb
import torch
from torch import nn
from torch.nn.functional import gelu, cosine_similarity
from transformers import AutoTokenizer, AutoModel, AutoModelForMaskedLM
import numpy as np
class InBedder():
def __init__(self, path='KomeijiForce/inbedder-roberta-large', device='cuda:0'):
model = AutoModelForMaskedLM.from_pretrained(path)
self.tokenizer = AutoTokenizer.from_pretrained(path)
self.model = model.roberta
self.dense = model.lm_head.dense
self.layer_norm = model.lm_head.layer_norm
self.device = torch.device(device)
self.model = self.model.to(self.device)
self.dense = self.dense.to(self.device)
self.layer_norm = self.layer_norm.to(self.device)
self.vocab = self.tokenizer.get_vocab()
self.vocab = {self.vocab[key]:key for key in self.vocab}
def encode(self, input_texts, instruction, n_mask):
if type(instruction) == str:
prompts = [instruction + self.tokenizer.mask_token*n_mask for input_text in input_texts]
elif type(instruction) == list:
prompts = [inst + self.tokenizer.mask_token*n_mask for inst in instruction]
inputs = self.tokenizer(input_texts, prompts, padding=True, truncation=True, return_tensors='pt').to(self.device)
mask = inputs.input_ids.eq(self.tokenizer.mask_token_id)
outputs = self.model(**inputs)
logits = outputs.last_hidden_state[mask]
logits = self.layer_norm(gelu(self.dense(logits)))
logits = logits.reshape(len(input_texts), n_mask, -1)
logits = logits.mean(1)
logits = (logits - logits.mean(1, keepdim=True)) / logits.std(1, keepdim=True)
return logits
inbedder = InBedder(path='KomeijiForce/inbedder-roberta-large', device='cpu')
texts = ["I love cat!", "I love dog!", "I dislike cat!"]
instruction = "What is the animal mentioned here?"
embeddings = inbedder.encode(texts, instruction, 3)
cosine_similarity(embeddings[:1], embeddings[1:], dim=1)
# tensor([0.9374, 0.9917], grad_fn=<SumBackward1>)
texts = ["I love cat!", "I love dog!", "I dislike cat!"]
instruction = "What is emotion expressed here?"
embeddings = inbedder.encode(texts, instruction, 3)
cosine_similarity(embeddings[:1], embeddings[1:], dim=1)
# tensor([0.9859, 0.8537], grad_fn=<SumBackward1>)