Skip to main content

An open source implementation of the Kimi K2 Reasoning model architecture in pure PyTorch

Project description

Open-Kimi-K2-Thinking

Architecture kimi k2

This repository is a straightforward attempt to implement the base Kimi K2 Reasoning model architecture in pure PyTorch as simply as possible.

Link

Install

pip3 install -U open-kimi

Example

from open_kimi.model import KimiK2
import torch

if __name__ == "__main__":
    model = KimiK2(
        dim=512,
        depth=2,
        attention_heads=8,
        experts=16,
        experts_per_token=4,
        seq_len=1024,
        lite_verison=True,
        vocab_size=10000,
    )

    x = torch.randint(0, 10000, (2, 1024))
    out = model(x)
    print(out)

Full Example

from open_kimi.model import KimiK2
import torch

if __name__ == "__main__":
    model = KimiK2(
        dim=7168,
        depth=61,
        attention_heads=64,
        experts=384,
        experts_per_token=8,
        seq_len=1024,
        lite_verison=False,
        vocab_size=160000,
    )

    x = torch.randint(0, 10000, (2, 7168))
    out = model(x)
    print(out)

Kimi Linear

Kimi Linear Architecture

Kimi Linear is a hybrid linear attention architecture that outperforms full attention under fair comparisons across various scenarios, including short-context, long-context, and reinforcement learning scaling regimes. At its core is Kimi Delta Attention (KDA), an expressive linear attention module that extends Gated DeltaNet with a finer-grained gating mechanism, enabling more effective use of limited finite-state RNN memory. Paper Link: Kimi Linear: An Expressive, Efficient Attention Architecture (arXiv:2510.26692)

Usage Example

import torch
from open_kimi.kimi_linear import KimiLinear

if __name__ == "__main__":
    model = KimiLinear(
        dim=512,
        num_heads=8,
        head_dim=64,
        chunk_size=64,
        n_experts=16,
        n_activated=4,
        kda_layers=2,
        depth=2,
        vocab_size=10000,
        seq_len=1024,
    )

    x = torch.randint(0, 10000, (2, 1024))
    out = model(x)
    print(out)
    print(out.shape)

Post Training

On the model huggingface page, they mention they use Native INT4 Quantization in the post training phase. So I would say a good post training recipe would include:

  • Native INT4 Quantization
  • MUON Optimizer
  • GRPO

Citation

@misc{moonshot-kimi-k2,
  title={Kimi K2 Thinking},
  author={Moonshot AI},
  year={2024},
  howpublished={\url{https://huggingface.co/moonshotai/Kimi-K2-Thinking}}
}

Acknowledgments

This implementation is based on the architecture specifications published by Moonshot AI for the Kimi K2 Thinking model. Special thanks to the Moonshot AI team for making the model architecture details publicly available.

Contact

For questions, issues, or contributions, please open an issue on the repository or contact the maintainers.


Note: This is an independent implementation based on publicly available specifications. It is not affiliated with or endorsed by Moonshot AI. For production use, please refer to the official model repository and weights.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

open_kimi-0.1.2.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

open_kimi-0.1.2-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file open_kimi-0.1.2.tar.gz.

File metadata

  • Download URL: open_kimi-0.1.2.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Darwin/24.5.0

File hashes

Hashes for open_kimi-0.1.2.tar.gz
Algorithm Hash digest
SHA256 88aac253fe9a65e7f3056ba312ccb4d897b9bf247440cb4755af18b2461619f5
MD5 e14116ce8010c1dfb00d17da7ec0918f
BLAKE2b-256 c3ee4d8b7be16ce86b926e6f4333a69ff2aa3eb9987701c7de0f46b60bba37dc

See more details on using hashes here.

File details

Details for the file open_kimi-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: open_kimi-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Darwin/24.5.0

File hashes

Hashes for open_kimi-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8b5a7f498b398c79e7e04dfc189e027caf017db13823da185fea64d20702db6c
MD5 468d4843aeca42d90a876af6e53b1d55
BLAKE2b-256 59e7cc255ff9ef47a28397e84c1c9e052af5ea2749f50ad4cbafd12d06dc804d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page