|
| 1 | +""" |
| 2 | +Generate a tiny FP8 Qwen3 model for testing Bumblebee's FP8 support. |
| 3 | +
|
| 4 | +This creates a minimal model with: |
| 5 | +- FP8 E4M3FN weights for linear layers |
| 6 | +- Corresponding weight_scale_inv tensors (128x128 block scaling) |
| 7 | +- Saved in safetensors format |
| 8 | +
|
| 9 | +Usage: |
| 10 | + python generate_fp8_qwen3.py |
| 11 | + # Then upload to HuggingFace: huggingface-cli upload bumblebee-testing/tiny-random-Qwen3ForCausalLM-FP8 ./tiny-fp8-qwen3 |
| 12 | +""" |
| 13 | + |
| 14 | +import torch |
| 15 | +import json |
| 16 | +import os |
| 17 | +from safetensors.torch import save_file |
| 18 | + |
| 19 | +# Tiny model config matching existing tiny-random-Qwen3ForCausalLM |
| 20 | +CONFIG = { |
| 21 | + "architectures": ["Qwen3ForCausalLM"], |
| 22 | + "hidden_size": 32, |
| 23 | + "intermediate_size": 64, |
| 24 | + "num_attention_heads": 4, |
| 25 | + "num_hidden_layers": 2, |
| 26 | + "num_key_value_heads": 2, |
| 27 | + "vocab_size": 1024, |
| 28 | + "head_dim": 8, # hidden_size / num_attention_heads |
| 29 | + "rms_norm_eps": 1e-6, |
| 30 | + "rope_theta": 1000000.0, |
| 31 | + "max_position_embeddings": 512, |
| 32 | + "torch_dtype": "float8_e4m3fn", |
| 33 | + "model_type": "qwen3", |
| 34 | + "use_qk_norm": True, |
| 35 | + "tie_word_embeddings": True, |
| 36 | + "quantization_config": { |
| 37 | + "quant_method": "fp8", |
| 38 | + "weight_block_size": [128, 128] |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +BLOCK_SIZE = 128 |
| 43 | + |
| 44 | + |
| 45 | +def create_fp8_weight(shape, seed=42): |
| 46 | + """Create a random FP8 E4M3FN weight tensor.""" |
| 47 | + torch.manual_seed(seed) |
| 48 | + # Create random values in valid FP8 E4M3FN range (-448 to 448) |
| 49 | + weight_f32 = torch.randn(shape) * 0.1 |
| 50 | + weight_fp8 = weight_f32.to(torch.float8_e4m3fn) |
| 51 | + return weight_fp8 |
| 52 | + |
| 53 | + |
| 54 | +def create_scale_inv(weight_shape): |
| 55 | + """Create scale_inv tensor for block-wise dequantization. |
| 56 | +
|
| 57 | + Shape: [ceil(out_features/128), ceil(in_features/128)] |
| 58 | + For testing, use scale of 1.0 (identity) so dequantized = original. |
| 59 | + """ |
| 60 | + out_features, in_features = weight_shape |
| 61 | + out_blocks = (out_features + BLOCK_SIZE - 1) // BLOCK_SIZE |
| 62 | + in_blocks = (in_features + BLOCK_SIZE - 1) // BLOCK_SIZE |
| 63 | + # Use 1.0 for identity scaling (easier to verify in tests) |
| 64 | + return torch.ones(out_blocks, in_blocks, dtype=torch.float32) |
| 65 | + |
| 66 | + |
| 67 | +def generate_model(): |
| 68 | + hidden_size = CONFIG["hidden_size"] |
| 69 | + intermediate_size = CONFIG["intermediate_size"] |
| 70 | + num_heads = CONFIG["num_attention_heads"] |
| 71 | + num_kv_heads = CONFIG["num_key_value_heads"] |
| 72 | + head_dim = CONFIG["head_dim"] |
| 73 | + vocab_size = CONFIG["vocab_size"] |
| 74 | + num_layers = CONFIG["num_hidden_layers"] |
| 75 | + |
| 76 | + tensors = {} |
| 77 | + seed = 0 |
| 78 | + |
| 79 | + # Embedding (not quantized) |
| 80 | + tensors["model.embed_tokens.weight"] = torch.randn(vocab_size, hidden_size) |
| 81 | + |
| 82 | + for layer_idx in range(num_layers): |
| 83 | + prefix = f"model.layers.{layer_idx}" |
| 84 | + |
| 85 | + # Self-attention projections (FP8 quantized) |
| 86 | + q_size = num_heads * head_dim |
| 87 | + kv_size = num_kv_heads * head_dim |
| 88 | + |
| 89 | + # Q projection |
| 90 | + tensors[f"{prefix}.self_attn.q_proj.weight"] = create_fp8_weight((q_size, hidden_size), seed) |
| 91 | + seed += 1 |
| 92 | + tensors[f"{prefix}.self_attn.q_proj.weight_scale_inv"] = create_scale_inv((q_size, hidden_size)) |
| 93 | + |
| 94 | + # K projection |
| 95 | + tensors[f"{prefix}.self_attn.k_proj.weight"] = create_fp8_weight((kv_size, hidden_size), seed) |
| 96 | + seed += 1 |
| 97 | + tensors[f"{prefix}.self_attn.k_proj.weight_scale_inv"] = create_scale_inv((kv_size, hidden_size)) |
| 98 | + |
| 99 | + # V projection |
| 100 | + tensors[f"{prefix}.self_attn.v_proj.weight"] = create_fp8_weight((kv_size, hidden_size), seed) |
| 101 | + seed += 1 |
| 102 | + tensors[f"{prefix}.self_attn.v_proj.weight_scale_inv"] = create_scale_inv((kv_size, hidden_size)) |
| 103 | + |
| 104 | + # O projection |
| 105 | + tensors[f"{prefix}.self_attn.o_proj.weight"] = create_fp8_weight((hidden_size, q_size), seed) |
| 106 | + seed += 1 |
| 107 | + tensors[f"{prefix}.self_attn.o_proj.weight_scale_inv"] = create_scale_inv((hidden_size, q_size)) |
| 108 | + |
| 109 | + # QK norms (not quantized) |
| 110 | + tensors[f"{prefix}.self_attn.q_norm.weight"] = torch.ones(head_dim) |
| 111 | + tensors[f"{prefix}.self_attn.k_norm.weight"] = torch.ones(head_dim) |
| 112 | + |
| 113 | + # MLP (FP8 quantized) |
| 114 | + tensors[f"{prefix}.mlp.gate_proj.weight"] = create_fp8_weight((intermediate_size, hidden_size), seed) |
| 115 | + seed += 1 |
| 116 | + tensors[f"{prefix}.mlp.gate_proj.weight_scale_inv"] = create_scale_inv((intermediate_size, hidden_size)) |
| 117 | + |
| 118 | + tensors[f"{prefix}.mlp.up_proj.weight"] = create_fp8_weight((intermediate_size, hidden_size), seed) |
| 119 | + seed += 1 |
| 120 | + tensors[f"{prefix}.mlp.up_proj.weight_scale_inv"] = create_scale_inv((intermediate_size, hidden_size)) |
| 121 | + |
| 122 | + tensors[f"{prefix}.mlp.down_proj.weight"] = create_fp8_weight((hidden_size, intermediate_size), seed) |
| 123 | + seed += 1 |
| 124 | + tensors[f"{prefix}.mlp.down_proj.weight_scale_inv"] = create_scale_inv((hidden_size, intermediate_size)) |
| 125 | + |
| 126 | + # Layer norms (not quantized) |
| 127 | + tensors[f"{prefix}.input_layernorm.weight"] = torch.ones(hidden_size) |
| 128 | + tensors[f"{prefix}.post_attention_layernorm.weight"] = torch.ones(hidden_size) |
| 129 | + |
| 130 | + # Final norm (not quantized) |
| 131 | + tensors["model.norm.weight"] = torch.ones(hidden_size) |
| 132 | + |
| 133 | + # LM head (can be tied to embeddings, but we include it for completeness) |
| 134 | + # Not quantized since it shares with embeddings |
| 135 | + |
| 136 | + return tensors |
| 137 | + |
| 138 | + |
| 139 | +def main(): |
| 140 | + output_dir = "tiny-fp8-qwen3" |
| 141 | + os.makedirs(output_dir, exist_ok=True) |
| 142 | + |
| 143 | + # Generate model tensors |
| 144 | + tensors = generate_model() |
| 145 | + |
| 146 | + # Save as safetensors |
| 147 | + save_file(tensors, os.path.join(output_dir, "model.safetensors")) |
| 148 | + |
| 149 | + # Save config |
| 150 | + with open(os.path.join(output_dir, "config.json"), "w") as f: |
| 151 | + json.dump(CONFIG, f, indent=2) |
| 152 | + |
| 153 | + print(f"Model saved to {output_dir}/") |
| 154 | + print(f"Total tensors: {len(tensors)}") |
| 155 | + print("\nTo upload to HuggingFace:") |
| 156 | + print(f" huggingface-cli upload bumblebee-testing/tiny-random-Qwen3ForCausalLM-FP8 {output_dir}") |
| 157 | + |
| 158 | + |
| 159 | +if __name__ == "__main__": |
| 160 | + main() |
0 commit comments