yujiepan commited on
Commit
41f7583
1 Parent(s): 5dbd012

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +73 -0
README.md ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 with the config from [nvidia/Hymba-1.5B-Instruct](https://huggingface.co/nvidia/Hymba-1.5B-Instruct) but is of smaller size.
12
+
13
+ Codes:
14
+ ```python
15
+ from huggingface_hub import create_repo, upload_folder
16
+ import os
17
+
18
+ import torch
19
+ import transformers
20
+ from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer, GenerationConfig, pipeline, set_seed
21
+
22
+ model_id = "nvidia/Hymba-1.5B-Instruct"
23
+ repo_id = "yujiepan/hymba-tiny-random"
24
+ save_path = f"/tmp/{repo_id}"
25
+
26
+ config = AutoConfig.from_pretrained(model_id, trust_remote_code=True)
27
+ config.conv_dim = {str(i): 32 for i in range(3)}
28
+ config.hidden_size = 16
29
+ config.intermediate_size = 32
30
+ config.num_attention_heads = 2
31
+ config.num_key_value_heads = 1
32
+ config.v_head_dim = 8
33
+ config.num_hidden_layers = 3
34
+
35
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
36
+ tokenizer.save_pretrained(save_path)
37
+
38
+ model = AutoModelForCausalLM.from_config(
39
+ config, torch_dtype=torch.bfloat16, trust_remote_code=True,
40
+ )
41
+ model.generation_config = GenerationConfig.from_pretrained(
42
+ model_id, trust_remote_code=True)
43
+
44
+ set_seed(42)
45
+ with torch.no_grad():
46
+ for _, p in sorted(model.named_parameters()):
47
+ torch.nn.init.uniform_(p, -0.2, 0.2)
48
+
49
+ model.save_pretrained(save_path)
50
+
51
+ prompt = 'Hello!'
52
+ messages = [
53
+ {"role": "system", "content": "You are a helpful assistant."}
54
+ ]
55
+ messages.append({"role": "user", "content": prompt})
56
+ tokenized_chat = tokenizer.apply_chat_template(
57
+ messages, tokenize=True, add_generation_prompt=True, return_tensors="pt").to('cuda')
58
+ outputs = model.cuda().generate(
59
+ tokenized_chat,
60
+ max_new_tokens=16,
61
+ do_sample=False,
62
+ temperature=0.7,
63
+ use_cache=True,
64
+ )
65
+ input_length = tokenized_chat.shape[1]
66
+ response = tokenizer.decode(
67
+ outputs[0][input_length:], skip_special_tokens=True)
68
+ print(f"Model response: {response}")
69
+
70
+ os.system(f"ls -alh {save_path}")
71
+ create_repo(repo_id, exist_ok=True)
72
+ upload_folder(repo_id=repo_id, folder_path=save_path)
73
+ ```