Upload folder using huggingface_hub
Browse files
README.md
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
library_name: transformers
|
3 |
+
pipeline_tag: text-generation
|
4 |
+
inference: true
|
5 |
+
widget:
|
6 |
+
- text: Hello!
|
7 |
+
example_title: Hello world
|
8 |
+
group: Python
|
9 |
+
---
|
10 |
+
|
11 |
+
This model is for debugging. It is randomly initialized using the config from [mistralai/Mamba-Codestral-7B-v0.1](https://huggingface.co/mistralai/Mamba-Codestral-7B-v0.1) but with smaller size.
|
12 |
+
|
13 |
+
Codes:
|
14 |
+
```python
|
15 |
+
import os
|
16 |
+
|
17 |
+
import torch
|
18 |
+
|
19 |
+
from huggingface_hub import create_repo, upload_folder
|
20 |
+
from transformers import (
|
21 |
+
AutoModelForCausalLM,
|
22 |
+
AutoTokenizer,
|
23 |
+
GenerationConfig,
|
24 |
+
Mamba2Config,
|
25 |
+
pipeline,
|
26 |
+
set_seed,
|
27 |
+
)
|
28 |
+
|
29 |
+
model_id = "mistralai/Mamba-Codestral-7B-v0.1"
|
30 |
+
repo_id = "yujiepan/mamba2-tiny-random"
|
31 |
+
save_path = f"/tmp/{repo_id}"
|
32 |
+
|
33 |
+
os.system(f'rm -rf {save_path}')
|
34 |
+
|
35 |
+
config = Mamba2Config.from_pretrained(model_id)
|
36 |
+
config.use_cache = True
|
37 |
+
config.num_hidden_layers = 2
|
38 |
+
config.num_heads = 8
|
39 |
+
config.head_dim = 4
|
40 |
+
config.hidden_size = 8
|
41 |
+
config.expand = 4
|
42 |
+
config.intermediate_size = 32
|
43 |
+
config.state_size = 8
|
44 |
+
config.n_groups = 2
|
45 |
+
|
46 |
+
assert config.intermediate_size == \
|
47 |
+
config.hidden_size * config.expand == config.num_heads * config.head_dim
|
48 |
+
assert config.num_heads // config.n_groups > 0
|
49 |
+
assert config.num_heads % 8 == 0
|
50 |
+
|
51 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
52 |
+
tokenizer.save_pretrained(save_path)
|
53 |
+
|
54 |
+
model = AutoModelForCausalLM.from_config(
|
55 |
+
config, torch_dtype=torch.bfloat16,
|
56 |
+
trust_remote_code=True,
|
57 |
+
)
|
58 |
+
model.generation_config = GenerationConfig.from_pretrained(
|
59 |
+
model_id,
|
60 |
+
trust_remote_code=True,
|
61 |
+
)
|
62 |
+
|
63 |
+
set_seed(42)
|
64 |
+
with torch.no_grad():
|
65 |
+
for name, p in sorted(model.named_parameters()):
|
66 |
+
print(name, p.shape)
|
67 |
+
torch.nn.init.uniform_(p, -0.5, 0.5)
|
68 |
+
|
69 |
+
model.save_pretrained(save_path)
|
70 |
+
|
71 |
+
pipe = pipeline(
|
72 |
+
"text-generation",
|
73 |
+
model=save_path,
|
74 |
+
device="cuda",
|
75 |
+
trust_remote_code=True,
|
76 |
+
max_new_tokens=20,
|
77 |
+
)
|
78 |
+
print(pipe("Hello World!"))
|
79 |
+
|
80 |
+
create_repo(repo_id, exist_ok=True)
|
81 |
+
upload_folder(repo_id=repo_id, folder_path=save_path, repo_type='model')
|
82 |
+
```
|