Instructions to use nthngdy/matryoshka-200M with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use nthngdy/matryoshka-200M with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="nthngdy/matryoshka-200M", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("nthngdy/matryoshka-200M", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use nthngdy/matryoshka-200M with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "nthngdy/matryoshka-200M" # 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-200M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/nthngdy/matryoshka-200M
- SGLang
How to use nthngdy/matryoshka-200M 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-200M" \ --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-200M", "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-200M" \ --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-200M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use nthngdy/matryoshka-200M with Docker Model Runner:
docker model run hf.co/nthngdy/matryoshka-200M
Upload Matriochka cascade up to main
Browse files- config.json +1 -0
- modeling_matriochka.py +18 -8
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 |
{
|
|
|
|
| 9 |
"bos_token_id": 0,
|
| 10 |
"dtype": "float32",
|
| 11 |
"eos_token_id": 0,
|
| 12 |
+
"junction_type": "none",
|
| 13 |
"model_type": "matriochka",
|
| 14 |
"sub_model_configs": [
|
| 15 |
{
|
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(
|
|
@@ -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(
|
|
|
|
| 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()
|