Skip to content
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
21ac639
working draft for LongCat
molbap Sep 5, 2025
c939eb2
BC changes to deepseek_v3 for modular
molbap Sep 5, 2025
2535c28
format
molbap Sep 8, 2025
bac973f
Merge branch 'main' into new_moe
molbap Sep 8, 2025
cddaba5
various modularities
molbap Sep 8, 2025
67943a4
better tp plan
molbap Sep 8, 2025
d765b18
better init
molbap Sep 8, 2025
eebb41c
minor changes
molbap Sep 8, 2025
414ba61
make modular better
molbap Sep 8, 2025
7586dd7
clean up patterns
molbap Sep 8, 2025
b4584ad
Revert a couple of modular commits, because we won't convert in the end
molbap Sep 9, 2025
76e4555
make things explicit.
molbap Sep 9, 2025
c7c5a3d
draft test
molbap Sep 9, 2025
6e58487
toctree, tests and imports
molbap Sep 9, 2025
8bb172d
drop
molbap Sep 9, 2025
726828d
woops
molbap Sep 9, 2025
df11c0e
make better things
molbap Sep 9, 2025
fa3aacf
update test
molbap Sep 9, 2025
07af563
update
molbap Sep 9, 2025
927a55e
fixes
molbap Sep 9, 2025
36c3dbb
style and CI
molbap Sep 9, 2025
d85c3e3
convert stuff
molbap Sep 9, 2025
8cb4dc2
up
molbap Sep 9, 2025
1343b65
ah, yes, that
molbap Sep 9, 2025
275374a
enable gen tests
molbap Sep 10, 2025
f9d35c5
fix cache shape in test (sum of 2 things)
molbap Sep 10, 2025
74d2728
fix tests
molbap Sep 10, 2025
1c9b49f
comments
molbap Sep 10, 2025
967259a
re-Identitise
molbap Sep 10, 2025
da61426
minimize changes
molbap Sep 11, 2025
9ff6f95
better defaults
molbap Sep 11, 2025
d75311c
modular betterment
molbap Sep 11, 2025
87b5687
fix configuration, add documentation
molbap Sep 11, 2025
e39779d
fix init
molbap Sep 11, 2025
c85a7ea
add integration tests
molbap Sep 12, 2025
3846289
add info
molbap Sep 12, 2025
1ec96f4
simplify
molbap Sep 12, 2025
6778512
update slow tests
molbap Sep 12, 2025
88e3114
fix
molbap Sep 12, 2025
563f9e0
conflicted
molbap Sep 12, 2025
67fd0d1
style
molbap Sep 12, 2025
ae5fcbc
Merge branch 'main' into new_moe
molbap Sep 12, 2025
c85afdd
Merge branch 'new_moe' of github.com:huggingface/transformers into ne…
molbap Sep 12, 2025
f208aa4
some additional long tests
molbap Sep 12, 2025
a3be847
cpu-only long test
molbap Sep 12, 2025
cf09a0b
Merge branch 'main' into new_moe
molbap Sep 12, 2025
c0f965f
fix last tests?
molbap Sep 12, 2025
2a76079
Merge branch 'new_moe' of github.com:huggingface/transformers into ne…
molbap Sep 12, 2025
7dafc04
urg
molbap Sep 12, 2025
7910e57
cleaner tests why not
molbap Sep 15, 2025
0666611
fix
molbap Sep 15, 2025
fd6df4f
Merge branch 'main' into new_moe
molbap Sep 15, 2025
a9b040e
improve slow tests, no skip
molbap Sep 16, 2025
b95af0a
style
molbap Sep 16, 2025
f0dfec7
don't upcast
molbap Sep 16, 2025
8463c5b
Merge branch 'main' into new_moe
molbap Sep 16, 2025
8cd2bb4
one skip
molbap Sep 16, 2025
68943ca
Merge branch 'new_moe' of github.com:huggingface/transformers into ne…
molbap Sep 16, 2025
f0eb7af
Merge branch 'main' into new_moe
molbap Sep 16, 2025
c85b064
finally fix parallelism
molbap Sep 16, 2025
f385373
Merge branch 'new_moe' of github.com:huggingface/transformers into ne…
molbap Sep 16, 2025
66b414a
Merge branch 'main' into new_moe
molbap Sep 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/en/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@
title: Llama2
- local: model_doc/llama3
title: Llama3
- local: model_doc/longcat_flash
title: LongCatFlash
- local: model_doc/longformer
title: Longformer
- local: model_doc/longt5
Expand Down
128 changes: 128 additions & 0 deletions docs/source/en/model_doc/longcat_flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<!--Copyright 2025 the HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer.

-->
*This model was released on 2025-09-01 and added to Hugging Face Transformers on 2025-09-05.*


# LongCatFlash

## Overview

The LongCatFlash model was proposed in [LongCat-Flash Technical Report](https://huggingface.co/papers/2509.01322) by the Meituan LongCat Team.
LongCat-Flash is a 560B parameter Mixture-of-Experts (MoE) model that activates 18.6B-31.3B parameters dynamically (average ~27B). The model features a shortcut-connected architecture enabling high inference speed (>100 tokens/second) and advanced reasoning capabilities.

The abstract from the paper is the following:

*We present LongCat-Flash, a 560 billion parameter Mixture-of-Experts (MoE) language model featuring a dynamic computation mechanism that activates 18.6B-31.3B parameters based on context (average ~27B). The model incorporates a shortcut-connected architecture enabling high inference speed (>100 tokens/second) and demonstrates strong performance across multiple benchmarks including 89.71% accuracy on MMLU and exceptional agentic tool use capabilities.*

Tips:

- LongCat-Flash uses a unique shortcut-connected MoE architecture that enables faster inference compared to traditional MoE models
- The model supports up to 128k context length for long-form tasks
- Dynamic parameter activation makes it computationally efficient while maintaining high performance
- Best suited for applications requiring strong reasoning, coding, and tool-calling capabilities
- The MoE architecture includes zero experts (nn.Identity modules) which act as skip connections, allowing tokens to bypass expert computation when appropriate

This model was contributed by [Molbap](https://huggingface.co/Molbap).
The original code can be found [here](https://huggingface.co/meituan-longcat/LongCat-Flash-Chat).

## Usage examples

The model is large: you will need 2x8 H100 to run inference.
```python
# launch_longcat.py
from transformers import LongcatFlashForCausalLM, AutoTokenizer
import torch

model_id = "meituan-longcat/LongCat-Flash-Chat"

tokenizer = AutoTokenizer.from_pretrained(model_id)

chat = [
{"role": "user", "content": "Hello! What is the capital of France? What can you tell me about it?"},
]

model = LongcatFlashForCausalLM.from_pretrained(
model_id,
tp_plan="auto",
dtype=torch.bfloat16,
)

inputs = tokenizer.apply_chat_template(
chat, tokenize=True, add_generation_prompt=True, return_tensors="pt").to(model.device)

outputs = model.generate(inputs, max_new_tokens=30)
print(tokenizer.batch_decode(outputs))
```

To run with TP, you will need torchrun:

```bash
torchrun --nproc_per_node=8 --nnodes=2 --node_rank=0 | 1 --rdzv-id <an_id> --rdzv-backend c10d --rdzv-endpoint $NODE_ID:$NODE_PORT --log-dir ./logs_longcat launch_longcat.py
```

And you'll get a nice generation:
```json
[Round 0] USER:Hello! What is the capital of France? What can you tell me about it? ASSISTANT:Hello! 😊 The capital of France is Paris, one of the most famous and beloved cities in the world. Here’s a quick overview of what makes Paris special:
1. Iconic Landmarks

Eiffel Tower – The global symbol of France, built in 1889 for the World's Fair.
Notre-Dame Cathedral – A masterpiece of Gothic architecture (currently under restoration after the 2019 fire).
Louvre Museum – The world’s largest art museum, home to the Mona Lisa and Venus de Milo.
Sacré-Cœur Basilica – A stunning white church atop Montmartre with panoramic views.
Arc de Triomphe – Honors French military victories, with the Tomb of the Unknown Soldier beneath it.
Champs-Élysées – A glamorous avenue leading to the Arc de Triomphe, lined with shops and cafés.

2. Culture & Arts

Paris is the "City of Light" (La Ville Lumière), a nickname from its early adoption of street lighting and its role as a center of enlightenment.
It’s a global hub for fashion (haute couture, Paris Fashion Week) and art (Impressionism, Picasso, Dali).
Famous literary figures like Hemingway, Fitzgerald, and Sartre lived and wrote here.

3. Food & Cuisine

Croissants, baguettes, macarons, and crème brûlée are just a few of its culinary delights.
Paris has over 100 Michelin-starred restaurants and countless cozy bistros.
The Marché d’Aligre and Rue Mouffetard are great for fresh produce and local flavors.

4. History & Politics

Founded in the 3rd century BC by the Parisii tribe, it became a major European city under the Romans.
The French Revolution (1789–1799) began here, leading to the fall of the monarchy.
Today, it’s the political and economic heart of France, housing the French President’s residence (Élysée Palace) and the National Assembly.

**
```

## LongcatFlashConfig

[[autodoc]] LongcatFlashConfig

## LongcatFlashPreTrainedModel

[[autodoc]] LongcatFlashPreTrainedModel
- forward

## LongcatFlashModel

[[autodoc]] LongcatFlashModel
- forward

## LongcatFlashForCausalLM

[[autodoc]] LongcatFlashForCausalLM
1 change: 1 addition & 0 deletions src/transformers/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
from .llava_next import *
from .llava_next_video import *
from .llava_onevision import *
from .longcat_flash import *
from .longformer import *
from .longt5 import *
from .luke import *
Expand Down
2 changes: 2 additions & 0 deletions src/transformers/models/auto/configuration_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@
("llava_next", "LlavaNextConfig"),
("llava_next_video", "LlavaNextVideoConfig"),
("llava_onevision", "LlavaOnevisionConfig"),
("longcat_flash", "LongcatFlashConfig"),
("longformer", "LongformerConfig"),
("longt5", "LongT5Config"),
("luke", "LukeConfig"),
Expand Down Expand Up @@ -657,6 +658,7 @@
("llava_next", "LLaVA-NeXT"),
("llava_next_video", "LLaVa-NeXT-Video"),
("llava_onevision", "LLaVA-Onevision"),
("longcat_flash", "LongCatFlash"),
("longformer", "Longformer"),
("longt5", "LongT5"),
("luke", "LUKE"),
Expand Down
2 changes: 2 additions & 0 deletions src/transformers/models/auto/modeling_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ class _BaseModelWithGenerate(PreTrainedModel, GenerationMixin):
("llava_next", "LlavaNextModel"),
("llava_next_video", "LlavaNextVideoModel"),
("llava_onevision", "LlavaOnevisionModel"),
("longcat_flash", "LongcatFlashModel"),
("longformer", "LongformerModel"),
("longt5", "LongT5Model"),
("luke", "LukeModel"),
Expand Down Expand Up @@ -677,6 +678,7 @@ class _BaseModelWithGenerate(PreTrainedModel, GenerationMixin):
("llama", "LlamaForCausalLM"),
("llama4", "Llama4ForCausalLM"),
("llama4_text", "Llama4ForCausalLM"),
("longcat_flash", "LongcatFlashForCausalLM"),
("mamba", "MambaForCausalLM"),
("mamba2", "Mamba2ForCausalLM"),
("marian", "MarianForCausalLM"),
Expand Down
29 changes: 29 additions & 0 deletions src/transformers/models/longcat_flash/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# coding=utf-8
# Copyright 2025 Meituan and the HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import TYPE_CHECKING

from ...utils import _LazyModule
from ...utils.import_utils import define_import_structure


if TYPE_CHECKING:
from .configuration_longcat_flash import *
from .modeling_longcat_flash import *
else:
import sys

_file = globals()["__file__"]
sys.modules[__name__] = _LazyModule(__name__, _file, define_import_structure(_file), module_spec=__spec__)
Loading