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
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
---
|
| 3 |
+
|
| 4 |
+
library_name: transformers
|
| 5 |
+
pipeline_tag: text-generation
|
| 6 |
+
inference: true
|
| 7 |
+
widget:
|
| 8 |
+
- text: Hello!
|
| 9 |
+
example_title: Hello world
|
| 10 |
+
group: Python
|
| 11 |
+
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
[](https://hf.co/QuantFactory)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# QuantFactory/deepseek-v3-tiny-random-GGUF
|
| 18 |
+
This is quantized version of [yujiepan/deepseek-v3-tiny-random](https://huggingface.co/yujiepan/deepseek-v3-tiny-random) created using llama.cpp
|
| 19 |
+
|
| 20 |
+
# Original Model Card
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
This model is for debugging. It is randomly initialized with the config from [deepseek-ai/DeepSeek-V3](https://huggingface.co/deepseek-ai/DeepSeek-V3) but is of smaller size.
|
| 24 |
+
|
| 25 |
+
**⚠️Note: At this moment, this repo does not contain the Multi-Token Prediction (MTP) module as explained [here](https://huggingface.co/deepseek-ai/DeepSeek-V3/blob/main/README_WEIGHTS.md).**
|
| 26 |
+
|
| 27 |
+
Usage:
|
| 28 |
+
```python
|
| 29 |
+
import torch
|
| 30 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 31 |
+
|
| 32 |
+
model_id = "yujiepan/deepseek-v3-tiny-random"
|
| 33 |
+
device = torch.device("cuda")
|
| 34 |
+
|
| 35 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 36 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 37 |
+
model_id, trust_remote_code=True,
|
| 38 |
+
).eval().to(device)
|
| 39 |
+
|
| 40 |
+
prompt = 'Hello!'
|
| 41 |
+
messages = [
|
| 42 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 43 |
+
{"role": "user", "content": prompt}
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
inputs = tokenizer.apply_chat_template(
|
| 47 |
+
messages, tokenize=True, add_generation_prompt=True, return_tensors="pt"
|
| 48 |
+
).to(device)
|
| 49 |
+
|
| 50 |
+
with torch.inference_mode():
|
| 51 |
+
outputs = model.generate(
|
| 52 |
+
inputs,
|
| 53 |
+
max_new_tokens=16,
|
| 54 |
+
do_sample=False,
|
| 55 |
+
use_cache=True,
|
| 56 |
+
)
|
| 57 |
+
string = tokenizer.decode(outputs[0])
|
| 58 |
+
print(string)
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
Codes:
|
| 62 |
+
```python
|
| 63 |
+
import os
|
| 64 |
+
from pathlib import Path
|
| 65 |
+
|
| 66 |
+
import torch
|
| 67 |
+
import transformers
|
| 68 |
+
from huggingface_hub import create_repo, upload_folder
|
| 69 |
+
from transformers import (AutoConfig, AutoModelForCausalLM, AutoTokenizer,
|
| 70 |
+
GenerationConfig, enable_full_determinism, pipeline,
|
| 71 |
+
set_seed)
|
| 72 |
+
|
| 73 |
+
model_id = "deepseek-ai/DeepSeek-V3"
|
| 74 |
+
repo_id = "yujiepan/deepseek-v3-tiny-random"
|
| 75 |
+
save_path = f"/tmp/{repo_id}"
|
| 76 |
+
os.system(f"rm -rf {save_path}")
|
| 77 |
+
|
| 78 |
+
config = AutoConfig.from_pretrained(model_id, trust_remote_code=True)
|
| 79 |
+
config.num_hidden_layers = 2
|
| 80 |
+
config.first_k_dense_replace = 1
|
| 81 |
+
config.hidden_size = 16
|
| 82 |
+
config.intermediate_size = 32
|
| 83 |
+
config.moe_intermediate_size = 16
|
| 84 |
+
config.q_lora_rank = 16
|
| 85 |
+
config.kv_lora_rank = 16
|
| 86 |
+
config.qk_rope_head_dim = 16
|
| 87 |
+
config.qk_nope_head_dim = 16
|
| 88 |
+
config.v_head_dim = 16
|
| 89 |
+
config.num_attention_heads = 2
|
| 90 |
+
config.num_key_value_heads = 2
|
| 91 |
+
# transformers has not supported the customized quantization config
|
| 92 |
+
del config.quantization_config
|
| 93 |
+
|
| 94 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 95 |
+
tokenizer.save_pretrained(save_path)
|
| 96 |
+
|
| 97 |
+
enable_full_determinism(seed=42)
|
| 98 |
+
model = AutoModelForCausalLM.from_config(
|
| 99 |
+
config, torch_dtype=torch.bfloat16, trust_remote_code=True,
|
| 100 |
+
).eval()
|
| 101 |
+
|
| 102 |
+
try:
|
| 103 |
+
model.generation_config = GenerationConfig.from_pretrained(
|
| 104 |
+
model_id, trust_remote_code=True)
|
| 105 |
+
except:
|
| 106 |
+
print("No generation config found")
|
| 107 |
+
|
| 108 |
+
num_params = 0
|
| 109 |
+
with torch.no_grad():
|
| 110 |
+
for name, p in sorted(model.named_parameters()):
|
| 111 |
+
if 'experts' in name and 'experts.0.' not in name: # avoid printing too much
|
| 112 |
+
pass
|
| 113 |
+
else:
|
| 114 |
+
print(name, p.shape)
|
| 115 |
+
# torch.nn.init.uniform_(p, -0.2, 0.2)
|
| 116 |
+
num_params += p.numel()
|
| 117 |
+
print(f"Number of parameters: {num_params / 1e6:.2f}M")
|
| 118 |
+
model.save_pretrained(save_path)
|
| 119 |
+
|
| 120 |
+
# patch to use official modeling codes
|
| 121 |
+
auto_map = config.auto_map
|
| 122 |
+
import json
|
| 123 |
+
with open(f"{save_path}/config.json", "r") as f:
|
| 124 |
+
config = json.load(f)
|
| 125 |
+
config['auto_map'] = auto_map
|
| 126 |
+
with open(f"{save_path}/config.json", "w") as f:
|
| 127 |
+
json.dump(config, f, indent=2)
|
| 128 |
+
|
| 129 |
+
! cat {save_path}/config.json
|
| 130 |
+
|
| 131 |
+
del model
|
| 132 |
+
del tokenizer
|
| 133 |
+
for p in Path(save_path).glob("*.py"):
|
| 134 |
+
os.remove(p)
|
| 135 |
+
|
| 136 |
+
os.system(f"ls -alh {save_path}")
|
| 137 |
+
torch.use_deterministic_algorithms(False)
|
| 138 |
+
tokenizer = AutoTokenizer.from_pretrained(save_path)
|
| 139 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 140 |
+
save_path, trust_remote_code=True).eval()
|
| 141 |
+
prompt = 'Hello!'
|
| 142 |
+
messages = [
|
| 143 |
+
{"role": "system", "content": "You are a helpful assistant."}
|
| 144 |
+
]
|
| 145 |
+
messages.append({"role": "user", "content": prompt})
|
| 146 |
+
tokenized_chat = tokenizer.apply_chat_template(
|
| 147 |
+
messages, tokenize=True, add_generation_prompt=True, return_tensors="pt")
|
| 148 |
+
|
| 149 |
+
device = torch.device("cuda")
|
| 150 |
+
outputs = model.to(device).generate(
|
| 151 |
+
tokenized_chat.to(device),
|
| 152 |
+
max_new_tokens=16,
|
| 153 |
+
do_sample=False,
|
| 154 |
+
use_cache=True,
|
| 155 |
+
)
|
| 156 |
+
tokens = tokenizer.convert_ids_to_tokens(outputs[0])
|
| 157 |
+
string = tokenizer.decode(outputs[0])
|
| 158 |
+
print(tokens)
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
# create_repo(repo_id, exist_ok=True)
|
| 162 |
+
# upload_folder(repo_id=repo_id, folder_path=save_path)
|
| 163 |
+
```
|
| 164 |
+
|