Elastic models are the models produced by TheStage AI ANNA: Automated Neural Networks Accelerator. ANNA allows you to control model size, latency and quality with a simple slider movement. For each model, ANNA produces a series of optimized models:
-
XL: Mathematically equivalent neural network, optimized with our DNN compiler.
-
L: Near lossless model, with less than 1% degradation obtained on corresponding benchmarks.
-
M: Faster model, with accuracy degradation less than 1.5%.
-
S: The fastest model, with accuracy degradation less than 2%.
1import torch
2from transformers import AutoTokenizer
3from elastic_models.transformers import AutoModelForCausalLM
4
5# Currently we require to have your HF token
6# as we use original weights for part of layers and
7# model confugaration as well
8model_name = "deepseek-ai/DeepSeek-R1-Distill-Qwen-14B"
9hf_token = ''
10device = torch.device("cuda")
11
12# Create mode
13tokenizer = AutoTokenizer.from_pretrained(
14 model_name, token=hf_token
15)
16model = AutoModelForCausalLM.from_pretrained(
17 model_name,
18 token=hf_token,
19 torch_dtype=torch.bfloat16,
20 attn_implementation="sdpa",
21 mode='S'
22).to(device)
23model.generation_config.pad_token_id = tokenizer.eos_token_id
24
25# Inference simple as transformers library
26prompt = "Describe basics of DNNs quantization."
27messages = [
28 {
29 "role": "system",
30 "content": "You are a search bot, answer on user text queries."
31 },
32 {
33 "role": "user",
34 "content": prompt
35 }
36]
37
38chat_prompt = tokenizer.apply_chat_template(
39 messages, add_generation_prompt=True, tokenize=False
40)
41
42inputs = tokenizer(chat_prompt, return_tensors="pt")
43inputs.to(device)
44
45with torch.inference_mode():
46 generate_ids = model.generate(**inputs, max_length=500)
47
48input_len = inputs['input_ids'].shape[1]
49generate_ids = generate_ids[:, input_len:]
50output = tokenizer.batch_decode(
51 generate_ids,
52 skip_special_tokens=True,
53 clean_up_tokenization_spaces=False
54)[0]
55
56# Validate answer
57print(f"# Q:\n{prompt}\n")
58print(f"# A:\n{output}\n")
1pip install thestage
2pip install 'thestage-elastic-models[nvidia]' --extra-index-url https://thestage.jfrog.io/artifactory/api/pypi/pypi-thestage-ai-production/simple
3pip install flash_attn==2.7.3 --no-build-isolation
4pip uninstall apex
Then go to
app.thestage.ai, login and generate API token from your profile page. Set up API token as follows:
Benchmarking is one of the most important procedures during model acceleration. We aim to provide clear performance metrics for models using our algorithms. The W8A8, int8 column indicates that we applied W8A8 quantization with int8 data type to all linear layers and used the same calibration data as for ANNA. The S model achieves practically identical speed but much higher quality, as ANNA knows how to improve quantization quality on sensitive layers!