Text Generation
Transformers
Safetensors
English
matriochka
matryoshka
nested-models
speculative-decoding
model-suite
distillation
custom_code
Instructions to use nthngdy/matryoshka-3B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use nthngdy/matryoshka-3B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="nthngdy/matryoshka-3B", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("nthngdy/matryoshka-3B", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use nthngdy/matryoshka-3B with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "nthngdy/matryoshka-3B" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "nthngdy/matryoshka-3B", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/nthngdy/matryoshka-3B
- SGLang
How to use nthngdy/matryoshka-3B with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "nthngdy/matryoshka-3B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "nthngdy/matryoshka-3B", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "nthngdy/matryoshka-3B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "nthngdy/matryoshka-3B", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use nthngdy/matryoshka-3B with Docker Model Runner:
docker model run hf.co/nthngdy/matryoshka-3B
Upload Matriochka cascade up to main
Browse files- config.json +8 -7
- model.safetensors +3 -0
- modeling_matriochka.py +19 -9
config.json
CHANGED
|
@@ -9,6 +9,7 @@
|
|
| 9 |
"bos_token_id": 0,
|
| 10 |
"dtype": "float32",
|
| 11 |
"eos_token_id": 0,
|
|
|
|
| 12 |
"model_type": "matriochka",
|
| 13 |
"sub_model_configs": [
|
| 14 |
{
|
|
@@ -20,7 +21,7 @@
|
|
| 20 |
"num_hidden_layers": 24,
|
| 21 |
"num_key_value_heads": 16,
|
| 22 |
"rms_norm_eps": 1e-05,
|
| 23 |
-
"rope_theta": 100000,
|
| 24 |
"tie_word_embeddings": false,
|
| 25 |
"vocab_size": 49152
|
| 26 |
},
|
|
@@ -33,7 +34,7 @@
|
|
| 33 |
"num_hidden_layers": 10,
|
| 34 |
"num_key_value_heads": 24,
|
| 35 |
"rms_norm_eps": 1e-05,
|
| 36 |
-
"rope_theta": 100000,
|
| 37 |
"tie_word_embeddings": false,
|
| 38 |
"vocab_size": 49152
|
| 39 |
},
|
|
@@ -46,16 +47,16 @@
|
|
| 46 |
"num_hidden_layers": 5,
|
| 47 |
"num_key_value_heads": 34,
|
| 48 |
"rms_norm_eps": 1e-05,
|
| 49 |
-
"rope_theta": 100000,
|
| 50 |
"tie_word_embeddings": false,
|
| 51 |
"vocab_size": 49152
|
| 52 |
}
|
| 53 |
],
|
| 54 |
"sub_model_tags": [
|
| 55 |
-
"
|
| 56 |
-
"1-
|
| 57 |
-
"
|
| 58 |
],
|
| 59 |
-
"transformers_version": "
|
| 60 |
"vocab_size": 49152
|
| 61 |
}
|
|
|
|
| 9 |
"bos_token_id": 0,
|
| 10 |
"dtype": "float32",
|
| 11 |
"eos_token_id": 0,
|
| 12 |
+
"junction_type": "norm",
|
| 13 |
"model_type": "matriochka",
|
| 14 |
"sub_model_configs": [
|
| 15 |
{
|
|
|
|
| 21 |
"num_hidden_layers": 24,
|
| 22 |
"num_key_value_heads": 16,
|
| 23 |
"rms_norm_eps": 1e-05,
|
| 24 |
+
"rope_theta": 100000.0,
|
| 25 |
"tie_word_embeddings": false,
|
| 26 |
"vocab_size": 49152
|
| 27 |
},
|
|
|
|
| 34 |
"num_hidden_layers": 10,
|
| 35 |
"num_key_value_heads": 24,
|
| 36 |
"rms_norm_eps": 1e-05,
|
| 37 |
+
"rope_theta": 100000.0,
|
| 38 |
"tie_word_embeddings": false,
|
| 39 |
"vocab_size": 49152
|
| 40 |
},
|
|
|
|
| 47 |
"num_hidden_layers": 5,
|
| 48 |
"num_key_value_heads": 34,
|
| 49 |
"rms_norm_eps": 1e-05,
|
| 50 |
+
"rope_theta": 100000.0,
|
| 51 |
"tie_word_embeddings": false,
|
| 52 |
"vocab_size": 49152
|
| 53 |
}
|
| 54 |
],
|
| 55 |
"sub_model_tags": [
|
| 56 |
+
"500M_60B_cd",
|
| 57 |
+
"1-5B_60B_cd",
|
| 58 |
+
"3B_60B_cd"
|
| 59 |
],
|
| 60 |
+
"transformers_version": "5.6.2",
|
| 61 |
"vocab_size": 49152
|
| 62 |
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:856e925904135a66f0b60974c4dd153a9b1d6e1f1dc40d9a7abbadd94a8b0e70
|
| 3 |
+
size 13434995600
|
modeling_matriochka.py
CHANGED
|
@@ -75,12 +75,14 @@ class MatriochkaConfig(PretrainedConfig):
|
|
| 75 |
vocab_size: int = 49152,
|
| 76 |
bos_token_id: int = 1,
|
| 77 |
eos_token_id: int = 2,
|
|
|
|
| 78 |
**kwargs,
|
| 79 |
):
|
| 80 |
super().__init__(bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
|
| 81 |
self.sub_model_configs = sub_model_configs or []
|
| 82 |
self.sub_model_tags = sub_model_tags or []
|
| 83 |
self.vocab_size = vocab_size
|
|
|
|
| 84 |
|
| 85 |
@classmethod
|
| 86 |
def from_shape_list(
|
|
@@ -107,7 +109,7 @@ class MatriochkaConfig(PretrainedConfig):
|
|
| 107 |
"intermediate_size": 4 * hidden,
|
| 108 |
"vocab_size": base.vocab_size,
|
| 109 |
"max_position_embeddings": base.max_position_embeddings,
|
| 110 |
-
"rope_theta": getattr(base, "rope_theta",
|
| 111 |
"rms_norm_eps": base.rms_norm_eps,
|
| 112 |
"tie_word_embeddings": False,
|
| 113 |
})
|
|
@@ -120,6 +122,7 @@ class MatriochkaConfig(PretrainedConfig):
|
|
| 120 |
**kwargs,
|
| 121 |
)
|
| 122 |
|
|
|
|
| 123 |
def truncated(self, tag: str) -> "MatriochkaConfig":
|
| 124 |
"""Return a new config containing only sub-models up to and including `tag`."""
|
| 125 |
idx = self.sub_model_tags.index(tag)
|
|
@@ -129,6 +132,7 @@ class MatriochkaConfig(PretrainedConfig):
|
|
| 129 |
vocab_size=self.vocab_size,
|
| 130 |
bos_token_id=self.bos_token_id,
|
| 131 |
eos_token_id=self.eos_token_id,
|
|
|
|
| 132 |
)
|
| 133 |
|
| 134 |
|
|
@@ -147,9 +151,10 @@ class _MatriochkaSubModel(nn.Module):
|
|
| 147 |
prev_hidden_size=None signals index 0 (no predecessor).
|
| 148 |
"""
|
| 149 |
|
| 150 |
-
def __init__(self, llama_cfg: LlamaConfig, prev_hidden_size: Optional[int]):
|
| 151 |
super().__init__()
|
| 152 |
self.prev_hidden_size = prev_hidden_size
|
|
|
|
| 153 |
self.backbone = LlamaForCausalLM(llama_cfg)
|
| 154 |
|
| 155 |
if prev_hidden_size is not None:
|
|
@@ -175,13 +180,18 @@ class _MatriochkaSubModel(nn.Module):
|
|
| 175 |
# embed_tokens produces (own - prev) dims, or own dims for index 0
|
| 176 |
inputs_embeds = self.backbone.get_input_embeddings()(input_ids)
|
| 177 |
|
|
|
|
|
|
|
|
|
|
| 178 |
if self.prev_hidden_size is not None and prev_hidden_states is not None:
|
| 179 |
-
#
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
| 185 |
inputs_embeds = torch.cat([prev_hidden_states * factor, inputs_embeds], dim=-1)
|
| 186 |
|
| 187 |
return self.backbone(
|
|
@@ -227,7 +237,7 @@ class MatriochkaForCausalLM(PreTrainedModel):
|
|
| 227 |
prev_hidden: Optional[int] = None
|
| 228 |
for tag, sub_cfg_dict in zip(config.sub_model_tags, config.sub_model_configs):
|
| 229 |
llama_cfg = LlamaConfig(**sub_cfg_dict)
|
| 230 |
-
self.lm_model_dict[tag] = _MatriochkaSubModel(llama_cfg, prev_hidden)
|
| 231 |
prev_hidden = sub_cfg_dict["hidden_size"]
|
| 232 |
|
| 233 |
self.post_init()
|
|
|
|
| 75 |
vocab_size: int = 49152,
|
| 76 |
bos_token_id: int = 1,
|
| 77 |
eos_token_id: int = 2,
|
| 78 |
+
junction_type: str = "norm",
|
| 79 |
**kwargs,
|
| 80 |
):
|
| 81 |
super().__init__(bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
|
| 82 |
self.sub_model_configs = sub_model_configs or []
|
| 83 |
self.sub_model_tags = sub_model_tags or []
|
| 84 |
self.vocab_size = vocab_size
|
| 85 |
+
self.junction_type = junction_type
|
| 86 |
|
| 87 |
@classmethod
|
| 88 |
def from_shape_list(
|
|
|
|
| 109 |
"intermediate_size": 4 * hidden,
|
| 110 |
"vocab_size": base.vocab_size,
|
| 111 |
"max_position_embeddings": base.max_position_embeddings,
|
| 112 |
+
"rope_theta": getattr(base, "rope_theta", 100000.0),
|
| 113 |
"rms_norm_eps": base.rms_norm_eps,
|
| 114 |
"tie_word_embeddings": False,
|
| 115 |
})
|
|
|
|
| 122 |
**kwargs,
|
| 123 |
)
|
| 124 |
|
| 125 |
+
|
| 126 |
def truncated(self, tag: str) -> "MatriochkaConfig":
|
| 127 |
"""Return a new config containing only sub-models up to and including `tag`."""
|
| 128 |
idx = self.sub_model_tags.index(tag)
|
|
|
|
| 132 |
vocab_size=self.vocab_size,
|
| 133 |
bos_token_id=self.bos_token_id,
|
| 134 |
eos_token_id=self.eos_token_id,
|
| 135 |
+
junction_type=self.junction_type,
|
| 136 |
)
|
| 137 |
|
| 138 |
|
|
|
|
| 151 |
prev_hidden_size=None signals index 0 (no predecessor).
|
| 152 |
"""
|
| 153 |
|
| 154 |
+
def __init__(self, llama_cfg: LlamaConfig, prev_hidden_size: Optional[int], junction_type: str = "norm"):
|
| 155 |
super().__init__()
|
| 156 |
self.prev_hidden_size = prev_hidden_size
|
| 157 |
+
self.junction_type = junction_type
|
| 158 |
self.backbone = LlamaForCausalLM(llama_cfg)
|
| 159 |
|
| 160 |
if prev_hidden_size is not None:
|
|
|
|
| 180 |
# embed_tokens produces (own - prev) dims, or own dims for index 0
|
| 181 |
inputs_embeds = self.backbone.get_input_embeddings()(input_ids)
|
| 182 |
|
| 183 |
+
if self.junction_type == "zero" and self.prev_hidden_size is not None:
|
| 184 |
+
inputs_embeds = 0 * inputs_embeds
|
| 185 |
+
|
| 186 |
if self.prev_hidden_size is not None and prev_hidden_states is not None:
|
| 187 |
+
# Combine prev_hs with own embedding; result width: own_hidden.
|
| 188 |
+
if self.junction_type == "norm":
|
| 189 |
+
factor = (
|
| 190 |
+
inputs_embeds.pow(2).mean(-1, keepdim=True).sqrt()
|
| 191 |
+
/ (1e-9 + prev_hidden_states.pow(2).mean(-1, keepdim=True).sqrt())
|
| 192 |
+
)
|
| 193 |
+
else:
|
| 194 |
+
factor = 1.0
|
| 195 |
inputs_embeds = torch.cat([prev_hidden_states * factor, inputs_embeds], dim=-1)
|
| 196 |
|
| 197 |
return self.backbone(
|
|
|
|
| 237 |
prev_hidden: Optional[int] = None
|
| 238 |
for tag, sub_cfg_dict in zip(config.sub_model_tags, config.sub_model_configs):
|
| 239 |
llama_cfg = LlamaConfig(**sub_cfg_dict)
|
| 240 |
+
self.lm_model_dict[tag] = _MatriochkaSubModel(llama_cfg, prev_hidden, config.junction_type)
|
| 241 |
prev_hidden = sub_cfg_dict["hidden_size"]
|
| 242 |
|
| 243 |
self.post_init()
|