from openmind import AutoTokenizer, AutoModel, is_torch_npu_available
from openmind_hub import snapshot_download
import torch
import argparse
import torch.nn.functional as F
# 均值池化 - 考虑注意力掩码以进行正确的平均
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0] # model_output的第一个元素包含所有token嵌入
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_name_or_path",
type=str,
help="Path to model",
default="../",
)
args = parser.parse_args()
return args
def main():
args = parse_args()
model_path = args.model_name_or_path
if is_torch_npu_available():
device = "npu:0"
else:
device = "cpu"
# 我们想要获取句子嵌入的句子
sentences = ['This is an example sentence', 'Each sentence is converted']
# 从openmind_hub加载模型
tokenizer = AutoTokenizer.from_pretrained('Rose/gemma-2B-inst-aipi')
model = AutoModel.from_pretrained('Rose/gemma-2B-inst-aipi')
# 对句子进行分词
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
# 计算token嵌入
with torch.no_grad():
model_output = model(**encoded_input)
# 执行池化
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
# 归一化嵌入
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
print("Sentence embeddings:")
print(sentence_embeddings)
if __name__ == "__main__":
main()This repository contains the gemma-2B model fine-tuned on the sail/symbolic-instruction-tuning dataset. The model is designed to interpret and execute symbolic instructions with improved accuracy and efficiency.
The gemma-2B model, originally known for its robust language understanding capabilities, has been fine-tuned to enhance its performance on symbolic instruction data. This involves retraining the model on the sail/symbolic-instruction-tuning dataset, which comprises a diverse range of instructional data that tests a model's ability to follow abstract and complex directives.
The motivation behind fine-tuning gemma-2B on this particular dataset is to bridge the gap between language understanding and execution in a symbolic context. This has wide applications in areas such as code generation, automated reasoning, and more sophisticated AI instruction following.
To use this model, you'll need to have an account on Hugging Face and the transformers library installed. You can install the library using pip:
pip install transformersOnce installed, you can use the following code to load and use the model:
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "your-huggingface-username/gemma-2B-fine-tuned"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Now you can use the model for inference
input_text = "Your symbolic instruction here"
input_ids = tokenizer.encode(input_text, return_tensors='pt')
# Generate the output
output = model.generate(input_ids)
print(tokenizer.decode(output[0], skip_special_tokens=True))The model was fine-tuned using the following process:
sail/symbolic-instruction-tuning dataset was preprocessed to conform with the input format required by gemma-2B.training_script.py file.