| | --- |
| | library_name: transformers |
| | base_model: |
| | - Zyphra/ZAYA1-reasoning-base |
| | --- |
| | |
| | This tiny model is intended for debugging. It is randomly initialized using the configuration adapted from [Zyphra/ZAYA1-reasoning-base](https://huggingface.co/Zyphra/ZAYA1-reasoning-base). |
| |
|
| | ### Example usage: |
| |
|
| | ```python |
| | from transformers import pipeline |
| | model_id = "tiny-random/zaya1" |
| | pipe = pipeline('text-generation', model=model_id, |
| | device='cuda', dtype="bfloat16") |
| | print(pipe('Hello World!')) |
| | ``` |
| |
|
| | ### Codes to create this repo: |
| |
|
| | ```python |
| | import json |
| | from pathlib import Path |
| | |
| | import accelerate |
| | import torch |
| | from huggingface_hub import file_exists, hf_hub_download |
| | from transformers import ( |
| | AutoConfig, |
| | AutoModelForCausalLM, |
| | AutoProcessor, |
| | AutoTokenizer, |
| | GenerationConfig, |
| | set_seed, |
| | ) |
| | |
| | source_model_id = "Zyphra/ZAYA1-reasoning-base" |
| | save_folder = "/tmp/tiny-random/zaya1" |
| | |
| | processor = AutoTokenizer.from_pretrained( |
| | source_model_id, trust_remote_code=True) |
| | processor.save_pretrained(save_folder) |
| | |
| | with open(hf_hub_download(source_model_id, filename='config.json', repo_type='model'), 'r', encoding='utf-8') as f: |
| | config_json = json.load(f) |
| | config_json['hidden_size'] = 512 |
| | config_json['num_attention_heads'] = 4 |
| | config_json['num_key_value_heads'] = 1 |
| | config_json['num_hidden_layers'] = 2 |
| | # bug. need to first set False and then hack |
| | config_json['tie_word_embeddings'] = False |
| | config_json['cca_num_q_heads'] = [2, 0] |
| | config_json['ffn_hidden_size_list'] = [0, 32] |
| | config_json['num_query_groups_list'] = [1, 0] |
| | config_json['zaya_layers'] = ['a', 16] |
| | config_json['zaya_mlp_expansion'] = [0, 8] |
| | |
| | with open(f"{save_folder}/config.json", "w", encoding='utf-8') as f: |
| | json.dump(config_json, f, indent=2) |
| | |
| | config = AutoConfig.from_pretrained( |
| | save_folder, |
| | trust_remote_code=True, |
| | ) |
| | print(config) |
| | torch.set_default_dtype(torch.bfloat16) |
| | model = AutoModelForCausalLM.from_config(config) |
| | model.lm_head = None |
| | torch.set_default_dtype(torch.float32) |
| | if file_exists(filename="generation_config.json", repo_id=source_model_id, repo_type='model'): |
| | model.generation_config = GenerationConfig.from_pretrained( |
| | source_model_id, trust_remote_code=True, |
| | ) |
| | set_seed(42) |
| | model = model.cpu() |
| | with torch.no_grad(): |
| | for name, p in sorted(model.named_parameters()): |
| | torch.nn.init.normal_(p, 0, 0.1) |
| | print(name, p.shape) |
| | model.save_pretrained(save_folder) |
| | with open(f"{save_folder}/config.json", 'r', encoding='utf-8') as f: |
| | config_json = json.load(f) |
| | config_json['tie_word_embeddings'] = True |
| | with open(f"{save_folder}/config.json", "w", encoding='utf-8') as f: |
| | json.dump(config_json, f, indent=2) |
| | ``` |
| |
|
| | ### Printing the model: |
| |
|
| | ```text |
| | ZayaForCausalLM( |
| | (model): ZayaModel( |
| | (embed_tokens): Embedding(262272, 512, padding_idx=0) |
| | (layers): ModuleList( |
| | (0): ZayaDecoderATTLayer( |
| | (self_attn): ZayaSdpaAttention( |
| | (o_proj): Linear(in_features=256, out_features=512, bias=False) |
| | (qkv): CCA( |
| | (linear_q): Linear(in_features=512, out_features=256, bias=False) |
| | (linear_k): Linear(in_features=512, out_features=128, bias=False) |
| | (val_proj1): Linear(in_features=512, out_features=64, bias=False) |
| | (val_proj2): Linear(in_features=512, out_features=64, bias=False) |
| | (conv_qk): Sequential( |
| | (0): Conv1d(384, 384, kernel_size=(2,), stride=(1,), groups=384) |
| | (1): Conv1d(384, 384, kernel_size=(2,), stride=(1,), groups=3) |
| | ) |
| | ) |
| | ) |
| | (input_norm): ZayaRMSNorm((512,), eps=1e-05) |
| | (res_scale): ResidualScaling() |
| | ) |
| | (1): ZayaDecoderMLPLayer( |
| | (zaya_block): ZayaBlock( |
| | (router): ZayaRouter( |
| | (down_proj): Linear(in_features=512, out_features=8, bias=True) |
| | (rmsnorm_eda): ZayaRMSNorm((8,), eps=1e-06) |
| | (non_linearity): GELU(approximate='none') |
| | (router_mlp): Sequential( |
| | (0): Linear(in_features=8, out_features=8, bias=True) |
| | (1): GELU(approximate='none') |
| | (2): Linear(in_features=8, out_features=8, bias=True) |
| | (3): GELU(approximate='none') |
| | (4): Linear(in_features=8, out_features=17, bias=False) |
| | ) |
| | ) |
| | (experts): SequentialMLP( |
| | (local_experts): ModuleList( |
| | (0-15): 16 x MLP( |
| | (linear_fc1): Linear(in_features=512, out_features=32, bias=False) |
| | (linear_fc2): Linear(in_features=16, out_features=512, bias=False) |
| | ) |
| | ) |
| | ) |
| | ) |
| | (input_norm): ZayaRMSNorm((512,), eps=1e-05) |
| | (res_scale): ResidualScaling() |
| | ) |
| | ) |
| | (res_scale): ResidualScaling() |
| | (final_norm): ZayaRMSNorm((512,), eps=1e-05) |
| | (rotary_emb): ZayaRotaryEmbedding() |
| | ) |
| | (lm_head): None |
| | ) |
| | ``` |