Run this model with HF Jobs
Run MemTensor/MemT-1.0-Flash on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs and TRL to fine-tune https://huggingface.co/MemTensor/MemT-1.0-Flash. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/MemTensor/MemT-1.0-Flash on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Evaluate on a benchmark
Use HF Jobs to evaluate https://huggingface.co/MemTensor/MemT-1.0-Flash. Ask me which benchmark to run, then save the scores and the per-example results to a new dataset repo. Run `hf jobs --help` first to learn the CLI.
Quantize for lower memory use
Use HF Jobs to create a 4-bit version of https://huggingface.co/MemTensor/MemT-1.0-Flash. Push it to a new model repo, then reload it from there and report its memory footprint. Run `hf jobs --help` first to learn the CLI.
Instructions to use MemTensor/MemT-1.0-Flash with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use MemTensor/MemT-1.0-Flash with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="MemTensor/MemT-1.0-Flash", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("MemTensor/MemT-1.0-Flash", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use MemTensor/MemT-1.0-Flash with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "MemTensor/MemT-1.0-Flash" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "MemTensor/MemT-1.0-Flash", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/MemTensor/MemT-1.0-Flash
- SGLang
How to use MemTensor/MemT-1.0-Flash 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 "MemTensor/MemT-1.0-Flash" \ --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": "MemTensor/MemT-1.0-Flash", "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 "MemTensor/MemT-1.0-Flash" \ --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": "MemTensor/MemT-1.0-Flash", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use MemTensor/MemT-1.0-Flash with Docker Model Runner:
docker model run hf.co/MemTensor/MemT-1.0-Flash
Commit ·
cb35498
0
Parent(s):
Upload model weights
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- .gitattributesAdded+42
- .gitignoreAdded+3
- README.mdAdded+195
- assets/MemT-1.0.pngAdded
- assets/application.pngAdded
- assets/badges/arxiv-coming-soon.svgAdded
- assets/badges/github.svgAdded
- assets/badges/huggingface-models.svgAdded
- assets/badges/tech-report.svgAdded
- assets/figure_performance.pngAdded
- assets/memt-arc.jpgAdded
- config.jsonAdded+231
- configuration_memt.pyAdded+87
- encoding_dsv4.pyAdded+760
- generation_config.jsonAdded+11
- memt-00001-of-00002.safetensorsAdded+3
- memt-00002-of-00002.safetensorsAdded+3
- model-00001-of-00043.safetensorsAdded+3
- model-00002-of-00043.safetensorsAdded+3
- model-00003-of-00043.safetensorsAdded+3
- model-00004-of-00043.safetensorsAdded+3
- model-00005-of-00043.safetensorsAdded+3
- model-00006-of-00043.safetensorsAdded+3
- model-00007-of-00043.safetensorsAdded+3
- model-00008-of-00043.safetensorsAdded+3
- model-00009-of-00043.safetensorsAdded+3
- model-00010-of-00043.safetensorsAdded+3
- model-00011-of-00043.safetensorsAdded+3
- model-00012-of-00043.safetensorsAdded+3
- model-00013-of-00043.safetensorsAdded+3
- model-00014-of-00043.safetensorsAdded+3
- model-00015-of-00043.safetensorsAdded+3
- model-00016-of-00043.safetensorsAdded+3
- model-00017-of-00043.safetensorsAdded+3
- model-00018-of-00043.safetensorsAdded+3
- model-00019-of-00043.safetensorsAdded+3
- model-00020-of-00043.safetensorsAdded+3
- model-00021-of-00043.safetensorsAdded+3
- model-00022-of-00043.safetensorsAdded+3
- model-00023-of-00043.safetensorsAdded+3
- model-00024-of-00043.safetensorsAdded+3
- model-00025-of-00043.safetensorsAdded+3
- model-00026-of-00043.safetensorsAdded+3
- model-00027-of-00043.safetensorsAdded+3
- model-00028-of-00043.safetensorsAdded+3
- model-00029-of-00043.safetensorsAdded+3
- model-00030-of-00043.safetensorsAdded+3
- model-00031-of-00043.safetensorsAdded+3
- model-00032-of-00043.safetensorsAdded+3
- model-00033-of-00043.safetensorsAdded+3
123456789101112131415161718192021222324252627282930313233343536373839404142*.7z filter=lfs diff=lfs merge=lfs -text*.arrow filter=lfs diff=lfs merge=lfs -text*.bin filter=lfs diff=lfs merge=lfs -text*.bz2 filter=lfs diff=lfs merge=lfs -text*.ckpt filter=lfs diff=lfs merge=lfs -text*.ftz filter=lfs diff=lfs merge=lfs -text*.gz filter=lfs diff=lfs merge=lfs -text*.h5 filter=lfs diff=lfs merge=lfs -text*.joblib filter=lfs diff=lfs merge=lfs -text*.lfs.* filter=lfs diff=lfs merge=lfs -text*.mlmodel filter=lfs diff=lfs merge=lfs -text*.model filter=lfs diff=lfs merge=lfs -text*.msgpack filter=lfs diff=lfs merge=lfs -text*.npy filter=lfs diff=lfs merge=lfs -text*.npz filter=lfs diff=lfs merge=lfs -text*.onnx filter=lfs diff=lfs merge=lfs -text*.ot filter=lfs diff=lfs merge=lfs -text*.parquet filter=lfs diff=lfs merge=lfs -text*.pb filter=lfs diff=lfs merge=lfs -text*.pickle filter=lfs diff=lfs merge=lfs -text*.pkl filter=lfs diff=lfs merge=lfs -text*.pt filter=lfs diff=lfs merge=lfs -text*.pth filter=lfs diff=lfs merge=lfs -text*.rar filter=lfs diff=lfs merge=lfs -text*.safetensors filter=lfs diff=lfs merge=lfs -textsaved_model/**/* filter=lfs diff=lfs merge=lfs -text*.tar.* filter=lfs diff=lfs merge=lfs -text*.tar filter=lfs diff=lfs merge=lfs -text*.tflite filter=lfs diff=lfs merge=lfs -text*.tgz filter=lfs diff=lfs merge=lfs -text*.wasm filter=lfs diff=lfs merge=lfs -text*.xz filter=lfs diff=lfs merge=lfs -text*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -textassets/MemT-1.0.png filter=lfs diff=lfs merge=lfs -textassets/memt-arc.jpg filter=lfs diff=lfs merge=lfs -textassets/application.jpg filter=lfs diff=lfs merge=lfs -textassets/figure_performance.jpg filter=lfs diff=lfs merge=lfs -textassets/figure_performance.png filter=lfs diff=lfs merge=lfs -texttech_report.pdf filter=lfs diff=lfs merge=lfs -textassets/application.png filter=lfs diff=lfs merge=lfs -text123*.incomplete__pycache__/*.pyc123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195---library_name: transformerspipeline_tag: text-generationtags: - custom_code - memt - text-generationbase_model: deepseek-ai/DeepSeek-V4-Flashlicense: mit---
# **MemT-1.0-Flash**

<p align="center" style="line-height: 0;"> <a href="https://huggingface.co/collections/MemTensor/memt-1" style="display: inline-block; height: 20px; line-height: 0; vertical-align: middle;"><img src="assets/badges/huggingface-models.svg" alt="Hugging Face Models" width="151" height="20" style="display: inline-block; height: 20px; vertical-align: middle; margin: 0;"></a> <a href="https://huggingface.co/MemTensor/MemT-1.0-Flash/resolve/main/tech_report.pdf" style="display: inline-block; height: 20px; line-height: 0; vertical-align: middle;"><img src="assets/badges/tech-report.svg" alt="Tech Report: PDF" width="140" height="20" style="display: inline-block; height: 20px; vertical-align: middle; margin: 0;"></a> <a href="https://github.com/MemTensor/MemT-1.0" style="display: inline-block; height: 20px; line-height: 0; vertical-align: middle;"><img src="assets/badges/github.svg" alt="GitHub: MemT-1.0" width="147" height="20" style="display: inline-block; height: 20px; vertical-align: middle; margin: 0;"></a></p>
## **Overview**
**MemT-1.0 is a new generation of memory foundation models that makes persistent, dynamically evolving memory an intrinsic part of model computation.**
MemT-1.0 integrates dynamic local memory and trainable hyper memory to retain and leverage historical information without accessing original context at inference. Its persistent state remains constant in size, reducing storage by approximately 66× versus full-context KV cache at 128K history length.
The project makes three main contributions:
* **Memory architecture**. Introduce MemT-1.0 with improved memory storage and utilization, and Mixture-of-Storage (MoS) for scalable memory capacity.
* **Training and evaluation**. Develop backbone preparation and a two-stage training pipeline, supported by 34B tokens of data and dedicated infrastructure. Evaluate memory and general capabilities.
* **Scaling and applications**. Study memory scaling and demonstrate native memory for experience-guided agentic coding.
### Model Family
| Model | Backbone | Total Parameters | Activated Parameters | Trainable Memory Parameters | Dynamic Memory State || --------------------------- | -------------------------- | ---------------- | -------------------- | --------------------------- | -------------------- || MemT-1.0-Nano | Qwen3.6-35B-A3B | 36B | 3B | 451M | 10M || MemT-1.0-Flash | DeepSeek-V4-Flash | 288B | 17B | 3.61B | 336M |
### Architecture
A **Native Memory Block** runs alongside the backbone attention and contains two components:
* The **Local Memory Block** maintains a dynamic, multi-channel memory matrix and evolves across interaction steps.
* The **Hyper Memory Block** contains trainable memory query, key, and value projections, erase, write, and forget gates, as well as an output gate.
During generation, memory attention reads from the current memory state, normalizes and gates the readout, and adds it to the backbone attention output. At the end of each turn or segment, a stable GDN-2 performs token-level memory updates the memory state for subsequent steps.

### Evaluation Results
Across 11 memory benchmarks, MemT-1.0-Flash and MemT-1.0-Nano rank first and second among the **evaluated parametric-memory methods**, respectively. Strong textual-memory systems remain ahead overall, particularly on long histories that require detailed evidence retrieval.

### Experience-Guided Agentic Coding
MemT-1.0 introduces an agentic framework for reusing procedural experience in long-horizon coding tasks. Retrieved experience is stored in native memory and accessed through memory attention during execution, improving task performance without adding experience text to the task context.

## **Quick Start**
The examples below use MemT-1.0 with Hugging Face Transformers.
### Installation
Use Python 3.11 or newer and install a CUDA 13 build of PyTorch 2.11.0 for your GPU, then:
```bashpip install transformers==5.12.1 accelerate==1.15.0 safetensors==0.8.0```
### Load the Model
```pythonimport torchfrom transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
model_id = "MemTensor/MemT-1.0-Flash" # Or a local model directory.tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)model = AutoModelForCausalLM.from_pretrained( model_id, trust_remote_code=True, dtype=torch.bfloat16, device_map="auto", attn_implementation="eager",)```
### A Conversation with Memory
```pythonsession = model.create_session(tokenizer)
# Set enable_memory=True to save past and current turns information to latent# local memory and clear the plaintext conversation context.session.chat( "I have a three-month-old kitten named memmy who loves eating " "small shrimp and is excellent at remembering things.", enable_memory=True, enable_thinking=False,)
# Answer using the latent local memory.answer = session.chat( "What are my kitten's name, age, favorite food, and special trait?", enable_thinking=False,)print(answer)# Based on the information you provided earlier:## * **Name:** Memmy# * **Age:** Three months old# * **Favorite Food:** Small shrimp# * **Special Trait:** An excellent memory```
### Save and Resume
```python# Save local memory, including any unsaved conversation turns.state_path = "memmy_memory.safetensors"session.save_state(state_path)session.close()
# Restore local memory without loading the plaintext conversation.restored = model.create_session(tokenizer, state=state_path)
print(len(restored.messages))# 0
answer = restored.chat( "How old is my kitten?", enable_thinking=False,)print(answer)# Your kitten is **3 months old**.
restored.close()```
### Standard Chat
```python# Use the standard Transformers pipeline for chat generation with plaintext context, without enable_memory.generator = pipeline("text-generation", model=model, tokenizer=tokenizer)messages = [{ "role": "user", "content": "Type 'I love MemT' exactly.",}]result = generator( messages, generation_config=model.generation_config, tokenizer_encode_kwargs={"enable_thinking": False},)print(result[0]["generated_text"][-1]["content"])# I love MemT```
## **Citation**
For the MemT-1.0 technical report:
```bibtex@misc{memtensor2026memt1, title = {MemT-1.0: Towards Memory-Native Evolution}, author = {{MemTensor Team}}, year = {2026}, howpublished = {Technical report}}```
## **License**
The MemT-1.0-Flash model weights are released under the**[MIT License](https://opensource.org/licenses/MIT)**.
We thank the DeepSeek team for the backbone model.The backbone and third-party components retain their original licenses andcopyright notices; the MemT license does not replace their terms.
Portions of the inference code are adapted from Hugging Face Transformersand retain their upstream attribution:Copyright 2026 The HuggingFace Inc. team. All rights reserved. Apache License 2.0.
## **Contact**
Correspondence: [lizy@memtensor.cn](mailto:lizy@memtensor.cn).123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231{ "_name_or_path": "", "architectures": [ "MemTDeepseekV4ForCausalLM" ], "attention_bias": false, "attention_dropout": 0.0, "auto_map": { "AutoConfig": "configuration_memt.MemTDeepseekV4Config", "AutoModelForCausalLM": "modeling_memt.MemTDeepseekV4ForCausalLM" }, "bos_token_id": 0, "chunk_size_feed_forward": 0, "compress_rates": { "compressed_sparse_attention": 4, "heavily_compressed_attention": 128 }, "compress_rope_theta": 160000, "dtype": "bfloat16", "eos_token_id": 1, "expert_dtype": "bf16", "hc_eps": 1e-06, "hc_mult": 4, "hc_sinkhorn_iters": 20, "head_dim": 512, "hidden_act": "silu", "hidden_size": 4096, "id2label": { "0": "LABEL_0", "1": "LABEL_1" }, "index_head_dim": 128, "index_n_heads": 64, "index_topk": 512, "initializer_range": 0.02, "is_encoder_decoder": false, "label2id": { "LABEL_0": 0, "LABEL_1": 1 }, "layer_types": [ "sliding_attention", "sliding_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention", "heavily_compressed_attention", "compressed_sparse_attention" ], "max_position_embeddings": 1048576, "memt": { "chunk_tokens": 2048, "gate_latent_norm": false, "gate_rank": 512, "gdn_version": "stable_gdn2", "head_dim": 512, "kv_groups": 8, "kv_rank": 512, "layers": [ 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42 ], "memory_head_mode": "mha", "memory_read_scale": "k_inv_sqrt", "memory_update_mode": "token", "memory_use_short_conv": false, "norm_eps": 1e-06, "num_kv_heads": 64, "num_memory_heads": 64, "output_gate_bias": true, "output_gate_softcap_enabled": true, "output_gate_softcap_limit": 2.0, "output_gate_type": "silu", "projection": "grouped_kv_low_rank", "q_rank": 1024, "stable_gdn2_sqrt_erase_gate": true, "tail_policy": "commit" }, "mlp_bias": false, "mlp_layer_types": [ "hash_moe", "hash_moe", "hash_moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe", "moe" ], "model_type": "memt_deepseek_v4", "moe_intermediate_size": 2048, "n_routed_experts": 256, "n_shared_experts": 1, "norm_topk_prob": true, "num_attention_heads": 64, "num_experts_per_tok": 6, "num_hidden_layers": 43, "num_key_value_heads": 1, "num_nextn_predict_layers": 0, "o_groups": 8, "o_lora_rank": 1024, "output_attentions": false, "output_hidden_states": false, "output_router_logits": false, "pad_token_id": 1, "partial_rotary_factor": 0.125, "problem_type": null, "q_lora_rank": 1024, "qk_rope_head_dim": 64, "return_dict": true, "rms_norm_eps": 1e-06, "rope_parameters": { "compress": { "attention_factor": 1.0, "beta_fast": 32, "beta_slow": 1, "factor": 16, "original_max_position_embeddings": 65536, "partial_rotary_factor": 0.125, "rope_theta": 160000, "rope_type": "yarn", "type": "yarn" }, "main": { "partial_rotary_factor": 0.125, "rope_theta": 10000, "rope_type": "default" } }, "rope_theta": 10000, "routed_scaling_factor": 1.5, "router_aux_loss_coef": 0.001, "router_jitter_noise": 0.0, "scoring_func": "sqrtsoftplus", "sliding_window": 128, "swiglu_limit": 10.0, "tie_word_embeddings": false, "topk_method": "noaux_tc", "transformers_version": "5.12.1", "use_cache": true, "vocab_size": 129280}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687"""Configuration for the complete MemT-1.0-Flash model (MIT)."""
import mathfrom copy import deepcopy
from transformers.models.deepseek_v4.configuration_deepseek_v4 import DeepseekV4Config
_DECLARED_SEMANTICS = { "memory_update_mode": "token", "memory_use_short_conv": False, "memory_head_mode": "mha", "memory_read_scale": "k_inv_sqrt", "tail_policy": "commit", "output_gate_type": "silu", "output_gate_softcap_enabled": True,}
class MemTDeepseekV4Config(DeepseekV4Config): model_type = "memt_deepseek_v4"
def __init__(self, memt=None, **kwargs): super().__init__(**kwargs) supplied = deepcopy(memt or {}) defaults = { **_DECLARED_SEMANTICS, "layers": [i for i, kind in enumerate(self.layer_types) if i >= 3 and kind == "compressed_sparse_attention"], "projection": "grouped_kv_low_rank", "num_memory_heads": self.num_attention_heads, "num_kv_heads": self.num_attention_heads, "head_dim": self.head_dim, "q_rank": self.q_lora_rank, "kv_groups": 8, "kv_rank": self.head_dim, "gate_rank": self.head_dim, "norm_eps": self.rms_norm_eps, "gate_latent_norm": False, "gdn_version": "stable_gdn2", "stable_gdn2_sqrt_erase_gate": True, "output_gate_bias": True, "output_gate_softcap_limit": 2.0, "chunk_tokens": 2048, } unknown = set(supplied) - set(defaults) if unknown: raise ValueError(f"Unknown MemT fields: {sorted(unknown)}") self.memt = defaults | supplied m = self.memt for name, expected in _DECLARED_SEMANTICS.items(): if type(m[name]) is not type(expected) or m[name] != expected: raise ValueError(f"Unsupported memt.{name}: {m[name]!r}") if (m["projection"] != "grouped_kv_low_rank" or m["gdn_version"] != "stable_gdn2" or m["stable_gdn2_sqrt_erase_gate"] is not True or m["gate_latent_norm"]): raise ValueError("This release implements the trained grouped-KV stable sqrt-b model") if type(m["output_gate_bias"]) is not bool: raise ValueError("memt.output_gate_bias must be bool") limit = m["output_gate_softcap_limit"] if isinstance(limit, bool) or not isinstance(limit, (int, float)) or not math.isfinite(limit) or limit <= 0: raise ValueError("memt.output_gate_softcap_limit must be positive and finite") for name in ("num_memory_heads", "num_kv_heads", "head_dim", "q_rank", "kv_groups", "kv_rank", "gate_rank", "chunk_tokens"): if type(m[name]) is not int or m[name] <= 0: raise ValueError(f"memt.{name} must be a positive integer") if m["num_memory_heads"] % m["kv_groups"]: raise ValueError("MemT heads must be divisible by the number of KV groups") if m["num_kv_heads"] != m["num_memory_heads"]: raise ValueError("MemT KV heads must match memory heads after grouped projection") if m["num_memory_heads"] != self.num_attention_heads or m["head_dim"] != self.head_dim: raise ValueError("MemT output geometry must match the host attention") if len(set(m["layers"])) != len(m["layers"]) or any( not 0 <= i < self.num_hidden_layers or self.layer_types[i] != "compressed_sparse_attention" for i in m["layers"] ): raise ValueError("MemT layers must be distinct CSA layers")
@classmethod def from_dict(cls, config_dict, **kwargs): result = super().from_dict(deepcopy(config_dict), **kwargs) config = result[0] if isinstance(result, tuple) else result # HF applies keyword overrides after __init__; validate those too. config.memt = cls(**config.to_dict()).memt return result
MemTDeepseekV4Config.register_for_auto_class()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760"""DeepSeek-V4 Encoding
A self-contained implementation for encoding/decoding DeepSeek-V4 chat messageswith tool calling, thinking mode, and quick instruction task support."""
from typing import Any, Dict, List, Union, Optional, Tupleimport copyimport jsonimport re
# ============================================================# Special Tokens# ============================================================
bos_token: str = "<|begin▁of▁sentence|>"eos_token: str = "<|end▁of▁sentence|>"thinking_start_token: str = "<think>"thinking_end_token: str = "</think>"dsml_token: str = "|DSML|"
USER_SP_TOKEN = "<|User|>"ASSISTANT_SP_TOKEN = "<|Assistant|>"LATEST_REMINDER_SP_TOKEN = "<|latest_reminder|>"
# Task special tokens for internal classification tasksDS_TASK_SP_TOKENS = { "action": "<|action|>", "query": "<|query|>", "authority": "<|authority|>", "domain": "<|domain|>", "title": "<|title|>", "read_url": "<|read_url|>",}VALID_TASKS = set(DS_TASK_SP_TOKENS.keys())
# ============================================================# Templates# ============================================================
system_msg_template: str = "{content}"user_msg_template: str = "{content}"latest_reminder_msg_template: str = "{content}"assistant_msg_template: str = "{reasoning}{content}{tool_calls}" + eos_tokenassistant_msg_wo_eos_template: str = "{reasoning}{content}{tool_calls}"thinking_template: str = "{reasoning_content}"
response_format_template: str = ( "## Response Format:\n\nYou MUST strictly adhere to the following schema to reply:\n{schema}")tool_call_template: str = ( "<{dsml_token}invoke name=\"{name}\">\n{arguments}\n</{dsml_token}invoke>")tool_calls_template = ( "<{dsml_token}{tc_block_name}>\n{tool_calls}\n</{dsml_token}{tc_block_name}>")tool_calls_block_name: str = "tool_calls"
tool_output_template: str = ( "<tool_result>{content}</tool_result>")
# Reasoning effort levels. In thinking mode, the prompt for the selected level is# prepended at the very beginning of the conversation. `low` is the default and# adds nothing.REASONING_EFFORT_PROMPTS: Dict[str, str] = { "low": "", "high": ( "Reasoning Effort: Absolute maximum with no shortcuts permitted.\n" "You MUST be very thorough in your thinking and comprehensively decompose the problem to resolve the root cause, rigorously stress-testing your logic against all potential paths, edge cases, and adversarial scenarios.\n" "Explicitly write out your entire deliberation process, documenting every intermediate step, considered alternative, and rejected hypothesis to ensure absolutely no assumption is left unchecked.\n\n" ), "max": ( "Reasoning Effort: Beyond maximum — exhaustive, relentless, and uncompromising.\n" "You MUST reason with the utmost depth and rigor, leaving absolutely nothing to chance: exhaustively decompose the problem into its most fundamental components, trace every causal chain to its root, and resolve the underlying cause rather than any surface symptom.\n" "Do not stop reasoning until you have independently verified the solution from multiple angles and are certain that no assumption remains unchecked and no error remains undiscovered.\n\n" ),}DEFAULT_REASONING_EFFORT = "low"
TOOLS_TEMPLATE = """## Tools
You have access to a set of tools to help answer the user's question. You can invoke tools by writing a "<{dsml_token}tool_calls>" block like the following:
<{dsml_token}tool_calls><{dsml_token}invoke name="$TOOL_NAME"><{dsml_token}parameter name="$PARAMETER_NAME" string="true|false">$PARAMETER_VALUE</{dsml_token}parameter>...</{dsml_token}invoke><{dsml_token}invoke name="$TOOL_NAME2">...</{dsml_token}invoke></{dsml_token}tool_calls>
String parameters should be specified as is and set `string="true"`. For all other types (numbers, booleans, arrays, objects), pass the value in JSON format and set `string="false"`.
If thinking_mode is enabled (triggered by {thinking_start_token}), you MUST output your complete reasoning inside {thinking_start_token}...{thinking_end_token} BEFORE any tool calls or final response.
Otherwise, output directly after {thinking_end_token} with tool calls or final response.
### Available Tool Schemas
{tool_schemas}
You MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls."""
# ============================================================# Utility Functions# ============================================================
def to_json(value: Any) -> str: """Serialize a value to JSON string.""" try: return json.dumps(value, ensure_ascii=False) except: return json.dumps(value, ensure_ascii=True)
def tools_from_openai_format(tools): """Extract function definitions from OpenAI-format tool list.""" return [tool["function"] for tool in tools]
def tool_calls_from_openai_format(tool_calls): """Convert OpenAI-format tool calls to internal format.""" return [ { "name": tool_call["function"]["name"], "arguments": tool_call["function"]["arguments"], } for tool_call in tool_calls ]
def tool_calls_to_openai_format(tool_calls): """Convert internal tool calls to OpenAI format.""" return [ { "type": "function", "function": { "name": tool_call["name"], "arguments": tool_call["arguments"], } } for tool_call in tool_calls ]
def encode_arguments_to_dsml(tool_call: Dict[str, str]) -> str: """ Encode tool call arguments into DSML parameter format.
Args: tool_call: Dict with "name" and "arguments" (JSON string) keys.
Returns: DSML-formatted parameter string. """ p_dsml_template = '<{dsml_token}parameter name="{key}" string="{is_str}">{value}</{dsml_token}parameter>' P_dsml_strs = []
try: arguments = json.loads(tool_call["arguments"]) except Exception as err: arguments = {"arguments": tool_call["arguments"]}
for k, v in arguments.items(): p_dsml_str = p_dsml_template.format( dsml_token=dsml_token, key=k, is_str="true" if isinstance(v, str) else "false", value=v if isinstance(v, str) else to_json(v), ) P_dsml_strs.append(p_dsml_str)
return "\n".join(P_dsml_strs)
def decode_dsml_to_arguments(tool_name: str, tool_args: Dict[str, Tuple[str, str]]) -> Dict[str, str]: """ Decode DSML parameters back to a tool call dict.
Args: tool_name: Name of the tool. tool_args: Dict mapping param_name -> (value, is_string_flag).
Returns: Dict with "name" and "arguments" (JSON string) keys. """ def _decode_value(key: str, value: str, string: str): if string == "true": value = to_json(value) return f"{to_json(key)}: {value}"
tool_args_json = "{" + ", ".join([_decode_value(k, v, string=is_str) for k, (v, is_str) in tool_args.items()]) + "}" return dict(name=tool_name, arguments=tool_args_json)
def render_tools(tools: List[Dict[str, Union[str, Dict[str, Any]]]]) -> str: """ Render tool schemas into the system prompt format.
Args: tools: List of tool schema dicts (each with name, description, parameters).
Returns: Formatted tools section string. """ tools_json = [to_json(t) for t in tools]
return TOOLS_TEMPLATE.format( tool_schemas="\n".join(tools_json), dsml_token=dsml_token, thinking_start_token=thinking_start_token, thinking_end_token=thinking_end_token, )
def find_last_user_index(messages: List[Dict[str, Any]]) -> int: """Find the index of the last user/developer message.""" last_user_index = -1 for idx in range(len(messages) - 1, -1, -1): if messages[idx].get("role") in ["user", "developer"]: last_user_index = idx break return last_user_index
# ============================================================# Message Rendering# ============================================================
def render_message(index: int, messages: List[Dict[str, Any]], thinking_mode: str, drop_thinking: bool = True, reasoning_effort: Optional[str] = None) -> str: """ Render a single message at the given index into its encoded string form.
This is the core function that converts each message in the conversation into the DeepSeek-V4 format.
Args: index: Index of the message to render. messages: Full list of messages in the conversation. thinking_mode: Either "chat" or "thinking". drop_thinking: Whether to drop reasoning content from earlier turns. reasoning_effort: Reasoning effort level, one of "low", "high", "max". None is treated as "low".
Returns: Encoded string for this message. """ assert 0 <= index < len(messages) assert thinking_mode in ["chat", "thinking"], f"Invalid thinking_mode `{thinking_mode}`"
prompt = "" msg = messages[index] last_user_idx = find_last_user_index(messages)
role = msg.get("role") content = msg.get("content") tools = msg.get("tools") response_format = msg.get("response_format") tool_calls = msg.get("tool_calls") reasoning_content = msg.get("reasoning_content") wo_eos = msg.get("wo_eos", False)
if tools: tools = tools_from_openai_format(tools) if tool_calls: tool_calls = tool_calls_from_openai_format(tool_calls)
# Reasoning effort prefix (only at index 0 in thinking mode; "low" adds nothing) reasoning_effort = reasoning_effort or DEFAULT_REASONING_EFFORT assert reasoning_effort in REASONING_EFFORT_PROMPTS, \ f"Invalid reasoning effort: {reasoning_effort}, expected one of {list(REASONING_EFFORT_PROMPTS)}" if index == 0 and thinking_mode == "thinking": prompt += REASONING_EFFORT_PROMPTS[reasoning_effort]
if role == "system": prompt += system_msg_template.format(content=content or "") if tools: prompt += "\n\n" + render_tools(tools) if response_format: prompt += "\n\n" + response_format_template.format(schema=to_json(response_format))
elif role == "developer": assert content, f"Invalid message for role `{role}`: {msg}"
content_developer = USER_SP_TOKEN content_developer += content
if tools: content_developer += "\n\n" + render_tools(tools) if response_format: content_developer += "\n\n" + response_format_template.format(schema=to_json(response_format))
prompt += user_msg_template.format(content=content_developer)
elif role == "user": prompt += USER_SP_TOKEN
# Handle content blocks (tool results mixed with text) content_blocks = msg.get("content_blocks") if content_blocks: parts = [] for block in content_blocks: block_type = block.get("type") if block_type == "text": parts.append(block.get("text", "")) elif block_type == "tool_result": tool_content = block.get("content", "") if isinstance(tool_content, list): text_parts = [] for b in tool_content: if b.get("type") == "text": text_parts.append(b.get("text", "")) else: text_parts.append(f"[Unsupported {b.get('type')}]") tool_content = "\n\n".join(text_parts) parts.append(tool_output_template.format(content=tool_content)) else: parts.append(f"[Unsupported {block_type}]") prompt += "\n\n".join(parts) else: prompt += content or ""
elif role == "latest_reminder": prompt += LATEST_REMINDER_SP_TOKEN + latest_reminder_msg_template.format(content=content)
elif role == "tool": raise NotImplementedError("deepseek_v4 merges tool messages into user; please preprocess with merge_tool_messages()")
elif role == "assistant": thinking_part = "" tc_content = ""
if tool_calls: tc_list = [ tool_call_template.format( dsml_token=dsml_token, name=tc.get("name"), arguments=encode_arguments_to_dsml(tc) ) for tc in tool_calls ] tc_content += '\n\n' + tool_calls_template.format( dsml_token=dsml_token, tool_calls="\n".join(tc_list), tc_block_name=tool_calls_block_name, )
summary_content = content or "" rc = reasoning_content or ""
# Check if previous message has a task - if so, this is a task output (no thinking) prev_has_task = index - 1 >= 0 and messages[index - 1].get("task") is not None
if thinking_mode == "thinking" and not prev_has_task: if not drop_thinking or index > last_user_idx: thinking_part = thinking_template.format(reasoning_content=rc) + thinking_end_token else: thinking_part = ""
if wo_eos: prompt += assistant_msg_wo_eos_template.format( reasoning=thinking_part, content=summary_content, tool_calls=tc_content, ) else: prompt += assistant_msg_template.format( reasoning=thinking_part, content=summary_content, tool_calls=tc_content, ) else: raise NotImplementedError(f"Unknown role: {role}")
# Append transition tokens based on what follows if index + 1 < len(messages) and messages[index + 1].get("role") not in ["assistant", "latest_reminder"]: return prompt
task = messages[index].get("task") if task is not None: # Task special token for internal classification tasks assert task in VALID_TASKS, f"Invalid task: '{task}'. Valid tasks are: {list(VALID_TASKS)}" task_sp_token = DS_TASK_SP_TOKENS[task]
if task != "action": # Non-action tasks: append task sp token directly after the message prompt += task_sp_token else: # Action task: append Assistant + thinking token + action sp token prompt += ASSISTANT_SP_TOKEN prompt += thinking_end_token if thinking_mode != "thinking" else thinking_start_token prompt += task_sp_token
elif messages[index].get("role") in ["user", "developer"]: # Normal generation: append Assistant + thinking token prompt += ASSISTANT_SP_TOKEN if not drop_thinking and thinking_mode == "thinking": prompt += thinking_start_token elif drop_thinking and thinking_mode == "thinking" and index >= last_user_idx: prompt += thinking_start_token else: prompt += thinking_end_token
return prompt
# ============================================================# Preprocessing# ============================================================
def merge_tool_messages(messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]: """ Merge tool messages into the preceding user message using content_blocks format.
DeepSeek-V4 does not have a standalone "tool" role; instead, tool results are encoded as <tool_result> blocks within user messages.
This function converts a standard OpenAI-format conversation (with separate "tool" role messages) into V4 format where tool results are merged into user messages.
Args: messages: List of message dicts in OpenAI format.
Returns: Processed message list with tool messages merged into user messages. """ merged: List[Dict[str, Any]] = []
for msg in messages: msg = copy.deepcopy(msg) role = msg.get("role")
if role == "tool": # Convert tool message to a user message with tool_result block tool_block = { "type": "tool_result", "tool_use_id": msg.get("tool_call_id", ""), "content": msg.get("content", ""), } # Merge into previous message if it's already a user (merged tool) if merged and merged[-1].get("role") == "user" and "content_blocks" in merged[-1]: merged[-1]["content_blocks"].append(tool_block) else: merged.append({ "role": "user", "content_blocks": [tool_block], }) elif role == "user": text_block = {"type": "text", "text": msg.get("content", "")} if merged and merged[-1].get("role") == "user" and "content_blocks" in merged[-1] and merged[-1].get("task") is None: merged[-1]["content_blocks"].append(text_block) else: new_msg = { "role": "user", "content": msg.get("content", ""), "content_blocks": [text_block], } # Preserve extra fields (task, wo_eos, mask, etc.) for key in ("task", "wo_eos", "mask"): if key in msg: new_msg[key] = msg[key] merged.append(new_msg) else: merged.append(msg)
return merged
def sort_tool_results_by_call_order(messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]: """ Sort tool_result blocks within user messages by the order of tool_calls in the preceding assistant message.
Args: messages: Preprocessed message list (after merge_tool_messages).
Returns: Message list with sorted tool result blocks. """ last_tool_call_order: Dict[str, int] = {}
for msg in messages: role = msg.get("role") if role == "assistant" and msg.get("tool_calls"): last_tool_call_order = {} for idx, tc in enumerate(msg["tool_calls"]): tc_id = tc.get("id") or tc.get("function", {}).get("id", "") if tc_id: last_tool_call_order[tc_id] = idx
elif role == "user" and msg.get("content_blocks"): tool_blocks = [b for b in msg["content_blocks"] if b.get("type") == "tool_result"] if len(tool_blocks) > 1 and last_tool_call_order: sorted_blocks = sorted( tool_blocks, key=lambda b: last_tool_call_order.get(b.get("tool_use_id", ""), 0) ) sorted_idx = 0 new_blocks = [] for block in msg["content_blocks"]: if block.get("type") == "tool_result": new_blocks.append(sorted_blocks[sorted_idx]) sorted_idx += 1 else: new_blocks.append(block) msg["content_blocks"] = new_blocks
return messages
# ============================================================# Main Encoding Function# ============================================================
def encode_messages( messages: List[Dict[str, Any]], thinking_mode: str, context: Optional[List[Dict[str, Any]]] = None, drop_thinking: bool = True, add_default_bos_token: bool = True, reasoning_effort: Optional[str] = None,) -> str: """ Encode a list of messages into the DeepSeek-V4 prompt format.
This is the main entry point for encoding conversations. It handles: - BOS token insertion - Thinking mode with optional reasoning content dropping - Tool message merging into user messages - Multi-turn conversation context
Args: messages: List of message dicts to encode. thinking_mode: Either "chat" or "thinking". context: Optional preceding context messages (already encoded prefix). drop_thinking: If True, drop reasoning_content from earlier assistant turns (only keep reasoning for messages after the last user message). add_default_bos_token: Whether to prepend BOS token at conversation start. reasoning_effort: Reasoning effort level, one of "low", "high", "max". Only takes effect in thinking mode. None is treated as "low".
Returns: The encoded prompt string. """ context = context if context else []
# Preprocess: merge tool messages and sort tool results messages = merge_tool_messages(messages) messages = sort_tool_results_by_call_order(context + messages)[len(context):] if context: context = merge_tool_messages(context) context = sort_tool_results_by_call_order(context)
full_messages = context + messages
prompt = bos_token if add_default_bos_token and len(context) == 0 else ""
# Resolve drop_thinking: if any message has tools defined, don't drop thinking effective_drop_thinking = drop_thinking if any(m.get("tools") for m in full_messages): effective_drop_thinking = False
if thinking_mode == "thinking" and effective_drop_thinking: full_messages = _drop_thinking_messages(full_messages) # After dropping, recalculate how many messages to render # (context may have shrunk too) num_to_render = len(full_messages) - len(_drop_thinking_messages(context)) context_len = len(full_messages) - num_to_render else: num_to_render = len(messages) context_len = len(context)
for idx in range(num_to_render): prompt += render_message( idx + context_len, full_messages, thinking_mode=thinking_mode, drop_thinking=effective_drop_thinking, reasoning_effort=reasoning_effort, )
return prompt
def _drop_thinking_messages(messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]: """ Drop reasoning_content and non-essential messages before the last user message.
Behavior: - Messages with role in ["user", "system", "tool", "latest_reminder"] are always kept. - Messages at or after the last user index are always kept. - Assistant messages before the last user get reasoning_content removed. - Developer messages before the last user are dropped entirely. """ last_user_idx = find_last_user_index(messages) result = [] keep_roles = {"user", "system", "tool", "latest_reminder", "direct_search_results"}
for idx, msg in enumerate(messages): role = msg.get("role") if role in keep_roles or idx >= last_user_idx: result.append(msg) elif role == "assistant": msg = copy.copy(msg) msg.pop("reasoning_content", None) result.append(msg) # developer and other roles before last_user_idx are dropped
return result
# ============================================================# Parsing (Decoding model output)# ============================================================
def _read_until_stop(index: int, text: str, stop: List[str]) -> Tuple[int, str, Optional[str]]: """ Read text from index until one of the stop strings is found.
Returns: Tuple of (new_index, content_before_stop, matched_stop_string_or_None). """ min_pos = len(text) matched_stop = None
for s in stop: pos = text.find(s, index) if pos != -1 and pos < min_pos: min_pos = pos matched_stop = s
if matched_stop: content = text[index:min_pos] return min_pos + len(matched_stop), content, matched_stop else: content = text[index:] return len(text), content, None
def parse_tool_calls(index: int, text: str) -> Tuple[int, Optional[str], List[Dict[str, str]]]: """ Parse DSML tool calls from text starting at the given index.
Args: index: Starting position in text. text: The full text to parse.
Returns: Tuple of (new_index, last_stop_token, list_of_tool_call_dicts). Each tool call dict has "name" and "arguments" keys. """ tool_calls: List[Dict[str, Any]] = [] stop_token = None tool_calls_end_token = f"</{dsml_token}{tool_calls_block_name}>"
while index < len(text): index, _, stop_token = _read_until_stop(index, text, [f"<{dsml_token}invoke", tool_calls_end_token]) if _ != ">\n": raise ValueError(f"Tool call format error: expected '>\\n' but got '{_}'")
if stop_token == tool_calls_end_token: break
if stop_token is None: raise ValueError("Missing special token in tool calls")
index, tool_name_content, stop_token = _read_until_stop(index, text, [f"<{dsml_token}parameter", f"</{dsml_token}invoke"])
p_tool_name = re.findall(r'^\s*name="(.*?)">\n$', tool_name_content, flags=re.DOTALL) if len(p_tool_name) != 1: raise ValueError(f"Tool name format error: '{tool_name_content}'") tool_name = p_tool_name[0]
tool_args: Dict[str, Tuple[str, str]] = {} while stop_token == f"<{dsml_token}parameter": index, param_content, stop_token = _read_until_stop(index, text, [f"/{dsml_token}parameter"])
param_kv = re.findall(r'^ name="(.*?)" string="(true|false)">(.*?)<$', param_content, flags=re.DOTALL) if len(param_kv) != 1: raise ValueError(f"Parameter format error: '{param_content}'") param_name, string, param_value = param_kv[0]
if param_name in tool_args: raise ValueError(f"Duplicate parameter name: '{param_name}'") tool_args[param_name] = (param_value, string)
index, content, stop_token = _read_until_stop(index, text, [f"<{dsml_token}parameter", f"</{dsml_token}invoke"]) if content != ">\n": raise ValueError(f"Parameter format error: expected '>\\n' but got '{content}'")
tool_call = decode_dsml_to_arguments(tool_name=tool_name, tool_args=tool_args) tool_calls.append(tool_call)
return index, stop_token, tool_calls
def parse_message_from_completion_text(text: str, thinking_mode: str) -> Dict[str, Any]: """ Parse a model completion text into a structured assistant message.
This function takes the raw text output from the model (a single assistant turn) and extracts: - reasoning_content (thinking block) - content (summary/response) - tool_calls (if any)
NOTE: This function is designed to parse only correctly formatted strings and will raise ValueError for malformed output.
Args: text: The raw completion text (including EOS token). thinking_mode: Either "chat" or "thinking".
Returns: Dict with keys: "role", "content", "reasoning_content", "tool_calls". tool_calls are in OpenAI format. """ summary_content, reasoning_content, tool_calls = "", "", [] index, stop_token = 0, None tool_calls_start_token = f"\n\n<{dsml_token}{tool_calls_block_name}"
is_thinking = thinking_mode == "thinking" is_tool_calling = False
if is_thinking: index, content_delta, stop_token = _read_until_stop(index, text, [thinking_end_token, tool_calls_start_token]) reasoning_content = content_delta assert stop_token == thinking_end_token, "Invalid thinking format: missing </think>"
index, content_delta, stop_token = _read_until_stop(index, text, [eos_token, tool_calls_start_token]) summary_content = content_delta if stop_token == tool_calls_start_token: is_tool_calling = True else: assert stop_token == eos_token, "Invalid format: missing EOS token"
if is_tool_calling: index, stop_token, tool_calls = parse_tool_calls(index, text)
index, tool_ends_text, stop_token = _read_until_stop(index, text, [eos_token]) assert not tool_ends_text, "Unexpected content after tool calls"
assert len(text) == index and stop_token in [eos_token, None], "Unexpected content at end"
for sp_token in [bos_token, eos_token, thinking_start_token, thinking_end_token, dsml_token]: assert sp_token not in summary_content and sp_token not in reasoning_content, \ f"Unexpected special token '{sp_token}' in content"
return { "role": "assistant", "content": summary_content, "reasoning_content": reasoning_content, "tool_calls": tool_calls_to_openai_format(tool_calls) }1234567891011{ "_from_model_config": true, "bos_token_id": 0, "do_sample": true, "eos_token_id": 1, "temperature": 1.0, "top_p": 1.0, "transformers_version": "5.12.1", "pad_token_id": 1, "max_new_tokens": 4096}123version https://git-lfs.github.com/spec/v1oid sha256:5d2a1b12b5c0888eef6d1709dc4235618c1287d2fdded099751b48bc87d41913size 3608625888123version https://git-lfs.github.com/spec/v1oid sha256:6ff18d09e235266d0bacf1a01be6076928622542848a4a10d72a7a7814604bfbsize 3608625936123version https://git-lfs.github.com/spec/v1oid sha256:5e905898639c65e9a58c6af8beed6a4075ef01d367bc2228a406119ef0ce2ecbsize 15279060516123version https://git-lfs.github.com/spec/v1oid sha256:fe9f01c0eb927b67348a1a76c9f4caa4b3c091aa0966ae08fe702d3f96787c15size 13157511232123version https://git-lfs.github.com/spec/v1oid sha256:e0dc7337489c7c9f65c8c5f587ae4664aece8c42701ca1a6407957857b7b3c7bsize 13195807368123version https://git-lfs.github.com/spec/v1oid sha256:54d44e795637fbd163ecd1a9e108331d8b8d2211d7dfd8c1c301b8fa73d9bcf2size 13163061840123version https://git-lfs.github.com/spec/v1oid sha256:9e55dd3c0cddbafafe3a01ba33e0c35e4637f4772f2c90c0acd6d4c64a9239a7size 13192705680123version https://git-lfs.github.com/spec/v1oid sha256:11856ff0daf7d92a62a06cfb0aca134ae3a96afde7643b0e0d0f79937e6dd243size 13163061840123version https://git-lfs.github.com/spec/v1oid sha256:aa14f16c3135ee884a277e34fe61b50b4bfe6d535e1aaba2687289baaaa51a79size 13192705680123version https://git-lfs.github.com/spec/v1oid sha256:13f7455a0867aaa8f6be2ac816abeb5fc1e07a99ee1d57369462e65b0c490b49size 13163061840123version https://git-lfs.github.com/spec/v1oid sha256:35ade895c38f334ae42ab8a8ef2b87574aaf8000f1f72c66fc49eb3af6ee7fafsize 13192705680123version https://git-lfs.github.com/spec/v1oid sha256:4467794f1b9229ff4b09bcdcb3704b17174999c405aae73d930ded0dedfe6183size 13163061848123version https://git-lfs.github.com/spec/v1oid sha256:8cf058654ade372df926e820623d268e1623eadaf3c17b5958818ef0fb518672size 13192705712123version https://git-lfs.github.com/spec/v1oid sha256:57db5c2e060ac6716fdc09d3b1a3baeb7908107cc76238c9fcf56b5947d8f485size 13163061864123version https://git-lfs.github.com/spec/v1oid sha256:2897178d7ab0fbcdbf57d74f8e154580525cd4cbd847da3d7edf5d8de3416ceasize 13192705712123version https://git-lfs.github.com/spec/v1oid sha256:8bfafcc7171ff999250617a31c921165e3b78959b850d154351b9cff0b46ecd9size 13163061864123version https://git-lfs.github.com/spec/v1oid sha256:156e8d3a3e398ac1023a9e523fca406452a71f41544d93431cbcc5f03b20cb0bsize 13192705712123version https://git-lfs.github.com/spec/v1oid sha256:6efbe35f98293d0da46d0033ff805c4fcdbd07b4dbc324d9aa15bd2d5dc66b18size 13163061864123version https://git-lfs.github.com/spec/v1oid sha256:a117e9db779aa12d047e46a3ebc53d5061d7bb99f98b78aababb255d92ecd139size 13192705712123version https://git-lfs.github.com/spec/v1oid sha256:6bfb29852933ffcee10f4b06a493f0329663ff2663c2664b2f6ebe8c4e0c4fb2size 13163061864123version https://git-lfs.github.com/spec/v1oid sha256:42c6f1f7a5bb1c0df407a311c3ccbc5b0f0354e6f619dfcdbb425eb67eb3e2f6size 13192705712123version https://git-lfs.github.com/spec/v1oid sha256:a654b802aeef4703144708f4eae56b5a17414a581c32607e0068d0a714eeff9fsize 13163061864123version https://git-lfs.github.com/spec/v1oid sha256:25715629394706158c83435b5571c8c21c06796667c626794f10e5193ec01308size 13192705712123version https://git-lfs.github.com/spec/v1oid sha256:ce180246bea50b6a7e2d8b0ed4106b615188024f1a9e90046bf2ca4e5c93cb65size 13163061864123version https://git-lfs.github.com/spec/v1oid sha256:af5b8d92d019a1ea55c46d704095b03cbecba969497dbaaa0af8784ac76a3cc4size 13192705712123version https://git-lfs.github.com/spec/v1oid sha256:4946c4936af708b07f81c6d5a7e1218c1497c560e0a5acabbc9d820baf165397size 13163061864123version https://git-lfs.github.com/spec/v1oid sha256:6a184a20faade0b22a48c83e9efd70ee9b888bbd6ccab3e00af393f54348b338size 13192705712123version https://git-lfs.github.com/spec/v1oid sha256:b030f2cd22ce19580d2a66416a8845b66389500573432cba0d847c8075325926size 13163061864123version https://git-lfs.github.com/spec/v1oid sha256:8f5f5ce2828af69e7d10f8fa967947946579d3755033217fc75612987a0b8df9size 13192705712123version https://git-lfs.github.com/spec/v1oid sha256:baea1f285f26522684896adef2ff37dc719bfd4e0b414d26b3400162ad72a752size 13163061864123version https://git-lfs.github.com/spec/v1oid sha256:6edc27f608d8b24653dfbc3239265107f611d65aae45c01602dd47afcf19022esize 13192705712123version https://git-lfs.github.com/spec/v1oid sha256:6d66bb6745c431c612619922ec6802c95b5b85b419db2a067e6ed99c925b42cbsize 13163061864123version https://git-lfs.github.com/spec/v1oid sha256:6cc5551e5491d7b0a1e0f46506c6868d07879bd4d57243dc2fb1b75469e47454size 13192705712123version https://git-lfs.github.com/spec/v1oid sha256:fea3e3b0fc2f35c7c9ffb1ba65ae0a151b2002113b2e59292a21e0e10b6e8e48size 13163061864123version https://git-lfs.github.com/spec/v1oid sha256:50b29f862e91786336fd559d633b88aa861563bc204ad3390b173de2b963df16size 13192705712.gitattributesAdded+42
.gitignoreAdded+3
README.mdAdded+195
assets/MemT-1.0.pngAdded

Large File Pointer Details
- SHA256: 1bc3e33992b36f65518c4831686382510a186980624b9612c868cde4cd786a02
- Pointer size: 132 Bytes
- Size of remote file: 1.29 MB
assets/application.pngAdded

Large File Pointer Details
- SHA256: a06481ed23d733bf4293a1211cad9ea7f80dfa4674e0c3599c0b077c282ae4bb
- Pointer size: 131 Bytes
- Size of remote file: 404 kB
assets/badges/arxiv-coming-soon.svgAdded
assets/badges/github.svgAdded
assets/badges/huggingface-models.svgAdded
assets/badges/tech-report.svgAdded
assets/figure_performance.pngAdded

Large File Pointer Details
- SHA256: 9de0e47c3ff33ce13d03fb525ed0700b1309d3e166d0f837d1885ec7aac33ea7
- Pointer size: 131 Bytes
- Size of remote file: 302 kB
assets/memt-arc.jpgAdded

Large File Pointer Details
- SHA256: 05c8ba71d29367e6aacd161f358fc500a9e42301a88bd7aa3e2fc45619c41529
- Pointer size: 132 Bytes
- Size of remote file: 1.59 MB
config.jsonAdded+231
configuration_memt.pyAdded+87
encoding_dsv4.pyAdded+760
generation_config.jsonAdded+11
memt-00001-of-00002.safetensorsAdded+3
memt-00002-of-00002.safetensorsAdded+3
model-00001-of-00043.safetensorsAdded+3
model-00002-of-00043.safetensorsAdded+3
model-00003-of-00043.safetensorsAdded+3
model-00004-of-00043.safetensorsAdded+3
model-00005-of-00043.safetensorsAdded+3
model-00006-of-00043.safetensorsAdded+3
model-00007-of-00043.safetensorsAdded+3
model-00008-of-00043.safetensorsAdded+3
model-00009-of-00043.safetensorsAdded+3
model-00010-of-00043.safetensorsAdded+3
model-00011-of-00043.safetensorsAdded+3
model-00012-of-00043.safetensorsAdded+3
model-00013-of-00043.safetensorsAdded+3
model-00014-of-00043.safetensorsAdded+3
model-00015-of-00043.safetensorsAdded+3
model-00016-of-00043.safetensorsAdded+3
model-00017-of-00043.safetensorsAdded+3
model-00018-of-00043.safetensorsAdded+3
model-00019-of-00043.safetensorsAdded+3
model-00020-of-00043.safetensorsAdded+3
model-00021-of-00043.safetensorsAdded+3
model-00022-of-00043.safetensorsAdded+3
model-00023-of-00043.safetensorsAdded+3
model-00024-of-00043.safetensorsAdded+3
model-00025-of-00043.safetensorsAdded+3
model-00026-of-00043.safetensorsAdded+3
model-00027-of-00043.safetensorsAdded+3
model-00028-of-00043.safetensorsAdded+3
model-00029-of-00043.safetensorsAdded+3
model-00030-of-00043.safetensorsAdded+3
model-00031-of-00043.safetensorsAdded+3
model-00032-of-00043.safetensorsAdded+3
model-00033-of-00043.safetensorsAdded+3