Length Generalization of Causal Transformers without Position Encoding
Paper • 2404.12224 • Published • 1
How to use AntNLP/TinyLlama-NoPE-1.1B with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-generation", model="AntNLP/TinyLlama-NoPE-1.1B") # Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("AntNLP/TinyLlama-NoPE-1.1B")
model = AutoModelForCausalLM.from_pretrained("AntNLP/TinyLlama-NoPE-1.1B", device_map="auto")How to use AntNLP/TinyLlama-NoPE-1.1B with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "AntNLP/TinyLlama-NoPE-1.1B"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "AntNLP/TinyLlama-NoPE-1.1B",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'docker model run hf.co/AntNLP/TinyLlama-NoPE-1.1B
How to use AntNLP/TinyLlama-NoPE-1.1B with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "AntNLP/TinyLlama-NoPE-1.1B" \
--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": "AntNLP/TinyLlama-NoPE-1.1B",
"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 "AntNLP/TinyLlama-NoPE-1.1B" \
--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": "AntNLP/TinyLlama-NoPE-1.1B",
"prompt": "Once upon a time,",
"max_tokens": 512,
"temperature": 0.5
}'How to use AntNLP/TinyLlama-NoPE-1.1B with Docker Model Runner:
docker model run hf.co/AntNLP/TinyLlama-NoPE-1.1B
NoPE is a transformer model without positional encoding.
The model is trained following TinyLlama code base (https://github.com/jzhang38/TinyLlama)
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.models.llama import modeling_llama
def nope_monkey_patch(q, k, cos, sin, position_ids, unsqueeze_dim=1):
return q, k
modeling_llama.apply_rotary_pos_emb = nope_monkey_patch
model_path = "AntNLP/TinyLlama-NoPE-1.1B"
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(model_path).cuda()
input_ids = tokenizer("Hello, TinyLlama-NoPE", return_tensors="pt").input_ids.cuda()
output = model.generate(input_ids, do_sample=True, max_length=50)
print(tokenizer.decode(output[0], skip_special_tokens=True))
@misc{wang2024length,
title={Length Generalization of Causal Transformers without Position Encoding},
author={Jie Wang and Tao Ji and Yuanbin Wu and Hang Yan and Tao Gui and Qi Zhang and Xuanjing Huang and Xiaoling Wang},
year={2024},
eprint={2404.12224},
archivePrefix={arXiv},
primaryClass={cs.CL}
}