{ "schema": "qfs.upstream-code-audit.v1", "upstream": { "repository": "moonshotai/Kimi-K3", "revision": "f831ab66814297da540d832a5235f8e904f29d06" }, "executed_during_audit": false, "weights_downloaded": false, "files": [ { "file": "configuration_kimi_k3.py", "sha256": "735eb9ebe593e17d231e08e1df7f7be9b5ee0e079f511aa201f9572077b416ae", "bytes": 11343, "imports": [ { "line": 1, "statement": "from typing import Optional" }, { "line": 3, "statement": "from transformers.configuration_utils import PretrainedConfig" } ], "effect_related_calls": [], "top_level_nodes": [] }, { "file": "encoding_k3.py", "sha256": "49ff03305fdc4be26867972788d36150b67f8a9e852e62bb7959d87482223676", "bytes": 26304, "imports": [ { "line": 9, "statement": "from __future__ import annotations" }, { "line": 11, "statement": "import json" }, { "line": 12, "statement": "from dataclasses import dataclass" }, { "line": 13, "statement": "from typing import Any, Iterable, Optional" } ], "effect_related_calls": [ { "line": 498, "call": "_open_tag" }, { "line": 526, "call": "_open_tag" }, { "line": 577, "call": "_open_tag" }, { "line": 519, "call": "_open_tag" }, { "line": 532, "call": "_open_tag" }, { "line": 626, "call": "_internal_system_message" }, { "line": 706, "call": "_internal_system_message" }, { "line": 725, "call": "_internal_system_message" }, { "line": 746, "call": "_open_tag" }, { "line": 747, "call": "_open_tag" }, { "line": 536, "call": "_open_tag" }, { "line": 648, "call": "_open_tag" }, { "line": 714, "call": "_internal_system_message" }, { "line": 735, "call": "_internal_system_message" }, { "line": 541, "call": "_open_tag" }, { "line": 547, "call": "_open_tag" }, { "line": 658, "call": "_open_tag" }, { "line": 680, "call": "_open_tag" }, { "line": 694, "call": "_open_tag" } ], "top_level_nodes": [ { "line": 1, "code": "'Kimi K3 XTML encoding helpers.\\n\\nThis module keeps chat rendering in Python.\\nCallers that need token IDs should consume ``EncodeSegment`` objects directly:\\nstructural markers may be encoded as tiktoken special tokens, while user/tool\\ntext and attribute values are encoded as ordinary text.\\n'" }, { "line": 15, "code": "OPEN_TOKEN = '<|open|>'" }, { "line": 16, "code": "CLOSE_TOKEN = '<|close|>'" }, { "line": 17, "code": "SEP_TOKEN = '<|sep|>'" }, { "line": 18, "code": "END_OF_MSG_TOKEN = '<|end_of_msg|>'" }, { "line": 19, "code": "IMAGE_PLACEHOLDER = '<|kimi_image_placeholder|>'" }, { "line": 21, "code": "_VALID_THINKING_EFFORTS = {'low', 'high', 'max'}" }, { "line": 193, "code": "XtmlArgument = tuple[str, str, str]" } ] }, { "file": "kimi_k3_processor.py", "sha256": "ec9f7e86d2ab0eee07a8e7e7c037046e77ac3c25a710ad1298ec13be3b585b54", "bytes": 7660, "imports": [ { "line": 7, "statement": "from transformers.feature_extraction_utils import BatchFeature" }, { "line": 8, "statement": "from transformers.processing_utils import ProcessorMixin" }, { "line": 9, "statement": "from transformers.utils import logging" }, { "line": 11, "statement": "from .media_utils import ensure_media_type" } ], "effect_related_calls": [], "top_level_nodes": [ { "line": 1, "code": "\"Kimi-K3 processor: wraps vision processor + tokenizer into a single interface.\\n\\nChat rendering (including XTML tool-result ordering) is handled by the\\ntokenizer's Python encoder; this processor adds multimodal media preprocessing.\\n\"" }, { "line": 13, "code": "logger = logging.get_logger(__name__)" } ] }, { "file": "kimi_k3_vision_processing.py", "sha256": "d122b30bfd3a51a6f05d4bfcfda1e657827322b1353f7caefeebc2835d7736b5", "bytes": 6686, "imports": [ { "line": 4, "statement": "import json" }, { "line": 5, "statement": "from typing import Any, Dict, Optional, Union" }, { "line": 7, "statement": "import numpy as np" }, { "line": 8, "statement": "import torch" }, { "line": 9, "statement": "from PIL import Image" }, { "line": 10, "statement": "from transformers.image_processing_utils import BaseImageProcessor, BatchFeature" }, { "line": 12, "statement": "from transformers.utils import TensorType" }, { "line": 14, "statement": "from .media_utils import MediaInput, TransparentBgConfig, _to_tensor, ensure_media_type, image_to_np, navit_patchify, navit_resize_image, normalize" } ], "effect_related_calls": [ { "line": 170, "call": "config_dict.copy" } ], "top_level_nodes": [ { "line": 1, "code": "'Image processor class for Kimi-K3.\\n'" } ] }, { "file": "media_utils.py", "sha256": "78403540328f9847d6b7ebc5c44eb2e6a752863de0afb7d0710728bb161dc60d", "bytes": 13844, "imports": [ { "line": 1, "statement": "import base64" }, { "line": 2, "statement": "import functools" }, { "line": 3, "statement": "import io" }, { "line": 4, "statement": "import math" }, { "line": 5, "statement": "from dataclasses import dataclass" }, { "line": 6, "statement": "from typing import Literal, TypedDict" }, { "line": 8, "statement": "import numpy as np" }, { "line": 9, "statement": "from PIL import Image" }, { "line": 361, "statement": "import torch" } ], "effect_related_calls": [ { "line": 182, "call": "Image.open" }, { "line": 184, "call": "Image.open" }, { "line": 186, "call": "Image.open" } ], "top_level_nodes": [ { "line": 17, "code": "MediaInput = ImageInput" } ] }, { "file": "modeling_kimi_k3.py", "sha256": "b9171c96726eda55234c92ac8dfae7e24c512fda68968ae8f2c3782b42665ea2", "bytes": 53444, "imports": [ { "line": 25, "statement": "import math" }, { "line": 26, "statement": "from collections.abc import Sequence" }, { "line": 27, "statement": "from copy import deepcopy" }, { "line": 28, "statement": "from typing import Optional" }, { "line": 30, "statement": "import numpy as np" }, { "line": 31, "statement": "import torch" }, { "line": 32, "statement": "import torch.nn as nn" }, { "line": 33, "statement": "import torch.nn.functional as F" }, { "line": 34, "statement": "from transformers import activations" }, { "line": 42, "statement": "from transformers.activations import PytorchGELUTanh" }, { "line": 43, "statement": "from transformers.configuration_utils import PretrainedConfig" }, { "line": 44, "statement": "from transformers.modeling_utils import PreTrainedModel" }, { "line": 45, "statement": "from transformers.models.llava.modeling_llava import LlavaCausalLMOutputWithPast" }, { "line": 47, "statement": "from transformers.utils import is_flash_attn_2_available" }, { "line": 49, "statement": "from .configuration_kimi_k3 import KimiK3Config" }, { "line": 50, "statement": "from .modeling_kimi_linear import KimiLinearForCausalLM" }, { "line": 37, "statement": "from transformers.activations import PytorchGELUTanh" }, { "line": 54, "statement": "from flash_attn import flash_attn_varlen_func" }, { "line": 39, "statement": "from transformers.activations import GELUTanh" } ], "effect_related_calls": [ { "line": 658, "call": "deepcopy" } ], "top_level_nodes": [ { "line": 36, "code": "try:\n from transformers.activations import PytorchGELUTanh\nexcept ImportError:\n from transformers.activations import GELUTanh\n activations.PytorchGELUTanh = GELUTanh\n PytorchGELUTanh = GELUTanh" }, { "line": 53, "code": "if is_flash_attn_2_available():\n from flash_attn import flash_attn_varlen_func\nelse:\n flash_attn_varlen_func = None" }, { "line": 136, "code": "VL_VISION_ATTENTION_FUNCTIONS = {'flash_attention_2': multihead_attention, 'eager': eager_attention}" } ] }, { "file": "modeling_kimi_linear.py", "sha256": "9e3564c70ac21854ce5a090cc946c5dc76b70d1050ef50840449181a20fff44a", "bytes": 51506, "imports": [ { "line": 24, "statement": "import math" }, { "line": 25, "statement": "from collections.abc import Callable" }, { "line": 26, "statement": "from typing import Any" }, { "line": 28, "statement": "import torch" }, { "line": 29, "statement": "import torch.nn.functional as F" }, { "line": 30, "statement": "import transformers" }, { "line": 31, "statement": "from einops import rearrange" }, { "line": 32, "statement": "from packaging import version" }, { "line": 33, "statement": "from torch import nn" }, { "line": 34, "statement": "from transformers.activations import ACT2FN" }, { "line": 35, "statement": "from transformers.cache_utils import Cache" }, { "line": 36, "statement": "from transformers.generation import GenerationMixin" }, { "line": 37, "statement": "from transformers.masking_utils import create_causal_mask" }, { "line": 38, "statement": "from transformers.modeling_flash_attention_utils import FlashAttentionKwargs" }, { "line": 39, "statement": "from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast" }, { "line": 40, "statement": "from transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel" }, { "line": 41, "statement": "from transformers.processing_utils import Unpack" }, { "line": 42, "statement": "from transformers.pytorch_utils import ALL_LAYERNORM_LAYERS" }, { "line": 43, "statement": "from transformers.utils import TransformersKwargs, auto_docstring, can_return_tuple, logging" }, { "line": 44, "statement": "from transformers.utils.generic import OutputRecorder, check_model_inputs" }, { "line": 55, "statement": "from .configuration_kimi_k3 import KimiLinearConfig" }, { "line": 47, "statement": "from fla.modules import FusedRMSNormGated, ShortConvolution" }, { "line": 48, "statement": "from fla.ops.kda import chunk_kda, fused_recurrent_kda" }, { "line": 50, "statement": "from fla.ops.utils.index import prepare_cu_seqlens_from_mask, prepare_lens_from_mask" }, { "line": 51, "statement": "from fla.utils import tensor_cache" }, { "line": 699, "statement": "import torch.nn.init as init" } ], "effect_related_calls": [], "top_level_nodes": [ { "line": 46, "code": "try:\n from fla.modules import FusedRMSNormGated, ShortConvolution\n from fla.ops.kda import chunk_kda, fused_recurrent_kda\n from fla.ops.utils.index import prepare_cu_seqlens_from_mask, prepare_lens_from_mask\n from fla.utils import tensor_cache\nexcept ImportError:\n raise ImportError('Plese run `pip install -U fla-core`')" }, { "line": 57, "code": "assert version.parse(transformers.__version__) >= version.parse('4.56.0'), 'Please upgrade transformers to >= 4.56.0'" }, { "line": 60, "code": "logger = logging.get_logger(__name__)" }, { "line": 85, "code": "ACT2FN['situ'] = SituAndMul" }, { "line": 239, "code": "ALL_LAYERNORM_LAYERS.append(KimiRMSNorm)" } ] }, { "file": "tokenization_kimi.py", "sha256": "f28ea66e2d862a2a5814970b2ce40c2f7d8296ff09aed90a7e7def689b906944", "bytes": 16145, "imports": [ { "line": 1, "statement": "import os" }, { "line": 2, "statement": "from logging import getLogger" }, { "line": 3, "statement": "from pathlib import Path" }, { "line": 4, "statement": "from shutil import copyfile" }, { "line": 5, "statement": "from typing import Dict, Iterator, List, Optional, Tuple, Union, cast" }, { "line": 7, "statement": "import tiktoken" }, { "line": 8, "statement": "from tiktoken.load import load_tiktoken_bpe" }, { "line": 9, "statement": "from tokenizers import AddedToken" }, { "line": 10, "statement": "from transformers.convert_slow_tokenizer import bytes_to_unicode" }, { "line": 11, "statement": "from transformers.tokenization_utils import PreTrainedTokenizer" }, { "line": 14, "statement": "from .encoding_k3 import build_chat_segments, is_batched_conversation" }, { "line": 16, "statement": "from encoding_k3 import build_chat_segments, is_batched_conversation" } ], "effect_related_calls": [ { "line": 99, "call": "load_tiktoken_bpe" }, { "line": 353, "call": "copyfile" } ], "top_level_nodes": [ { "line": 13, "code": "try:\n from .encoding_k3 import build_chat_segments, is_batched_conversation\nexcept ImportError:\n from encoding_k3 import build_chat_segments, is_batched_conversation" }, { "line": 18, "code": "logger = getLogger(__name__)" }, { "line": 19, "code": "VOCAB_FILES_NAMES = {'vocab_file': 'tiktoken.model'}" } ] } ], "license": "Kimi K3 License for Moonshot code with Apache-2.0 adapted portions; FLA reference adaptations MIT. All three license texts retained.", "import_time_effects": [ "Imports Python/NumPy/Torch/Transformers; upstream K3 hard imports FLA and imports/registry-mutates ACT2FN and ALL_LAYERNORM_LAYERS.", "Upstream processor/tokenizer closure is not used by fixture: byte tokenizer is authored independently. tiktoken load_tiktoken_bpe can read local or remote vocabulary at tokenizer construction. media_utils reads image filenames/data on explicit invocation, not import.", "No subprocess, shell, eval/exec, socket, network request, or model-weight load found in upstream module import-time statements. Decorators and dependency imports execute library initialization. This is source review, not sandboxing." ], "runtime_scope": "Four-file patched full Kimi K3 wrapper closure plus installed torch/numpy/transformers. CPU reference math replaces FLA imports entirely; no lazy or eager GPU dependency imports from text runtime. Vision keeps conditional flash-attn import and executes original eager or true Torch SDPA attention on CPU, according to the actual loaded backend.", "network_policy": "No upstream model weights fetched. Custom capture explicitly permits pinned code metadata/download from the selected private model repo; local model weights. Not entirely offline capture. Comparison independent of code and GPU.", "patch_provenance": "runtime-patches.json; cpu_reference.py; reference/pin.json", "review_limitations": "Static source review is not a security guarantee. Dependency implementations belong to the pinned environment. Main must run all runtime checks before release.", "reference_sources": { "LICENSE": { "sha256": "41a83c8187efc1e3ccc21909e806a9e52338e69448554d9754706c3d1cd610e7", "bytes": 1096 }, "convolution.py": { "sha256": "2ab60fc508097457a2ad01dd1f6602ed7e1b47d8a2468949944402b7d5451b8a", "bytes": 1194 }, "fused_norm_gate.py": { "sha256": "109de719d6f9b4e692e3fd504eda4a6e897156c8dc9ac464a6f94193fa94d677", "bytes": 40532 }, "gate.py": { "sha256": "7ea46d62149b8e28e0827e070fee327576de783cb981e3b855182f2b8198c16e", "bytes": 15758 }, "l2norm.py": { "sha256": "5160cc4ff716c9975c655b81fd0e1bae700501dff3b4d6b045680a93185a1e99", "bytes": 7792 }, "naive.py": { "sha256": "60a32285d4b67068ff633b48bbe8ab31028066d24f00d27e12199a88fc73f016", "bytes": 6339 }, "pin.json": { "sha256": "b99a0f4427f07729fc1bde070e48a1814c395f413a8103ff6828cb36bd94ca47", "bytes": 111 }, "short_conv.py": { "sha256": "e2ee319f11b0421545ea5eaa233f12f91e6904ce3dba83cee04a4b25110e6afe", "bytes": 10389 } }, "patched_runtime_sources": { "__init__.py": { "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "bytes": 0 }, "configuration_kimi_k3.py": { "sha256": "735eb9ebe593e17d231e08e1df7f7be9b5ee0e079f511aa201f9572077b416ae", "bytes": 11343 }, "cpu_reference.py": { "sha256": "f5ab2904ea43b434bcc17fb61a104f6c2975e5f52a39cffbbcfa94b358350e4e", "bytes": 5976 }, "modeling_kimi_k3.py": { "sha256": "344de6dfed495bd6e66e1d16abac12ba6cfd222e872b15ad73003f38462f35af", "bytes": 54765 }, "modeling_kimi_linear.py": { "sha256": "94956f39cd28bf211acf70a3e38f469db5d24637b76a4f58b0b096527bb70df5", "bytes": 50914 } }, "generation_compatibility": "Root directly inherits GenerationMixin; keeps upstream prepare_inputs_for_generation slicing; migrates cached image mask and beam cache access to KimiDynamicCache.", "dtype_initialization": "Untouched FP32 initialization -> temporary FP32 checkpoint -> actual BF16 loader selecting FP32-retained parameters -> per-tensor original.to(target_dtype) equality -> final exact mixed-dtype save/reload. No pre-rounding of sensitive parameters.", "vision_backend": "Original eager/FlashAttention2 dispatch plus true Torch SDPA packed-clip path. Backend-specific floating-point equivalence is not claimed." }