# InternLM3

[InternLM3](https://huggingface.co/internlm/internlm3-8b-instruct) は、[Shanghai AI Laboratory](https://huggingface.co/internlm) によって開発された 8B パラメータの指示チューニング済み言語モデルです。そのアーキテクチャは [Llama](./llama) と同じです。設定ファイルでは、クエリ、キー、バリューの射影のバイアスを `qkv_bias`、アテンション出力の射影および MLP 層のバイアスを `bias` と呼びます。[InternLM3Config](/docs/transformers/v5.23.0/ja/model_doc/internlm3#transformers.InternLM3Config) はこれらを `attention_bias` および `mlp_bias` として読み込み、公開されているチェックポイントではこれらが等しいため、等しいことを要求します。

auto クラスは Llama の実装（[LlamaModel](/docs/transformers/v5.23.0/ja/model_doc/llama2#transformers.LlamaModel) と [LlamaForCausalLM](/docs/transformers/v5.23.0/ja/model_doc/llama2#transformers.LlamaForCausalLM)）で読み込むため、InternLM3 に固有なのは [InternLM3Config](/docs/transformers/v5.23.0/ja/model_doc/internlm3#transformers.InternLM3Config) のみです。

以下の例は、[AutoModelForCausalLM](/docs/transformers/v5.23.0/ja/model_doc/auto#transformers.AutoModelForCausalLM) クラスを使用してテキストを生成する方法を示しています。

```python
from transformers import AutoModelForCausalLM, AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("internlm/internlm3-8b-instruct")
model = AutoModelForCausalLM.from_pretrained("internlm/internlm3-8b-instruct", device_map="auto")

messages = [{"role": "user", "content": "Explain the Pythagorean theorem in one sentence."}]
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt", return_dict=True)
output = model.generate(**inputs.to(model.device), max_new_tokens=64)
print(tokenizer.decode(output[0][inputs["input_ids"].shape[1] :], skip_special_tokens=True))
```

## InternLM3Config[[transformers.InternLM3Config]]

#### transformers.InternLM3Config[[transformers.InternLM3Config]]

```python
transformers.InternLM3Config(transformers_version: str | None = None, architectures: list[str] | None = None, output_hidden_states: bool | None = False, return_dict: bool | None = True, dtype: str | torch.dtype | None = None, chunk_size_feed_forward: int = 0, is_encoder_decoder: bool = False, id2label: dict[int, str] | dict[str, str] | None = None, label2id: dict[str, int] | dict[str, str] | None = None, problem_type: Literal['regression', 'single_label_classification', 'multi_label_classification'] | None = None, vocab_size: int = 128512, hidden_size: int = 4096, intermediate_size: int = 10240, num_hidden_layers: int = 48, num_attention_heads: int = 32, num_key_value_heads: int | None = 2, hidden_act: str = 'silu', max_position_embeddings: int = 32768, initializer_range: float = 0.02, rms_norm_eps: float = 1e-05, use_cache: bool = True, pad_token_id: int | None = 2, bos_token_id: int | None = 1, eos_token_id: int | list[int] | None = 2, tie_word_embeddings: bool = False, rope_parameters: transformers.modeling_rope_utils.RopeParameters | dict | None = None, attention_bias: bool = False, attention_dropout: float | int = 0.0, mlp_bias: bool = False, head_dim: int | None = 128)
```

[ソース](https://github.com/huggingface/transformers/blob/v5.23.0/src/transformers/models/internlm3/configuration_internlm3.py#L30)

**パラメータ:**

vocab_size (`int`、*任意*、デフォルトは`128512`) : モデルの語彙サイズ。`input_ids`で表現できるトークンの種類数を定義します。

hidden_size (`int`、*任意*、デフォルトは`4096`) : 隠れ表現の次元数。

intermediate_size (`int`、*任意*、デフォルトは `10240`) : MLP 表現の次元数。

num_hidden_layers (`int`、*任意*、デフォルトは `48`) : Transformer デコーダーの隠れ層の数。

num_attention_heads (`int`、*任意*、デフォルトは `32`) : Transformer デコーダーの各アテンション層におけるアテンションヘッドの数。

num_key_value_heads (`int`、*任意*、デフォルトは `2`) : Grouped Query Attention を実装するために使用する key_value ヘッドの数です。`num_key_value_heads=num_attention_heads` の場合、モデルは Multi Head Attention (MHA) を使用し、`num_key_value_heads=1` の場合は Multi Query Attention (MQA) を使用し、それ以外の場合は GQA が使用されます。マルチヘッドのチェックポイントを GQA のチェックポイントに変換する際は、各グループの key ヘッドと value ヘッドは、そのグループ内の元のすべてのヘッドを平均プーリングして構築する必要があります。詳細については、[この論文](https://huggingface.co/papers/2305.13245)を参照してください。指定されない場合、デフォルトで `num_attention_heads` が使用されます。

hidden_act (`str`、*任意*、デフォルトは`silu`) : デコーダーで使用する非線形活性化関数（関数または文字列）。例えば、`"gelu"`、`"relu"`、`"silu"`など。

max_position_embeddings (`int`、*任意*、デフォルトは`32768`) : このモデルが使用される可能性のある最大シーケンス長です。

initializer_range (`float`、*任意*、デフォルトは`0.02`) : すべての重み行列を初期化するためのtruncated_normal_initializerの標準偏差です。

rms_norm_eps (`float`、*任意*、デフォルトは`1e-05`) : rms 正規化レイヤーで使用する epsilon。

use_cache (`bool`、*任意*、デフォルトは `True`) : モデルが最後のキー/バリューアテンションを返すかどうか（すべてのモデルで利用されるわけではありません）。`config.is_decoder=True` の場合、またはモデルがデコーダー専用の生成モデルである場合にのみ関連します。

pad_token_id (`int`、*任意*、デフォルトは `2`) : 語彙内でパディングに使用されるトークン ID。

bos_token_id (`int`, *任意*, デフォルトは `1`) : 語彙内でストリームの開始に使用されるトークン ID。

eos_token_id (`Union[int, list[int]]`、*任意*、デフォルトは`2`) : 語彙内でストリーム終了に使用されるトークン ID。

tie_word_embeddings (`bool`, *任意*, デフォルトは `False`) : モデルの `tied_weights_keys` マッピングに従って重み埋め込みを共有するかどうか。

rope_parameters (`Union[~modeling_rope_utils.RopeParameters, dict]`, *optional*) : RoPE 埋め込みの設定パラメータを含む辞書です。この辞書には `rope_theta` の値が含まれていなければならず、より長い `max_position_embeddings` で RoPE を使用する場合、スケーリングに使用されるパラメータが任意で含まれます。

attention_bias (`bool`、*任意*、デフォルトは`False`) : セルフアテンションにおけるクエリ、キー、バリュー、出力の投影層でバイアスを使用するかどうか。

attention_dropout (`Union[float, int]`、*任意*、デフォルトは`0.0`) : 注意確率のドロップアウト率。

mlp_bias (`bool`, *任意*, デフォルトは `False`) : MLP 層の up_proj、down_proj、gate_proj 層でバイアスを使用するかどうか。

head_dim (`int`、*任意*、デフォルトは`128`) : アテンションヘッドの次元です。None の場合、hidden_size // num_attention_heads がデフォルトとして使用されます。

これはLlamaModelの設定を保存するための設定クラスです。指定された引数に基づいてInternlm3モデルをインスタンス化し、モデルのアーキテクチャを定義するために使用されます。デフォルト値で設定をインスタンス化すると、[internlm/internlm3-8b-instruct](https://huggingface.co/internlm/internlm3-8b-instruct)と類似した設定が得られます。

設定オブジェクトは [PreTrainedConfig](/docs/transformers/v5.23.0/ja/main_classes/configuration#transformers.PreTrainedConfig) を継承し、モデルの出力を制御するために使用できます。詳細については、[PreTrainedConfig](/docs/transformers/v5.23.0/ja/main_classes/configuration#transformers.PreTrainedConfig) のドキュメントを参照してください。

```python
>>> from transformers import AutoModelForCausalLM, InternLM3Config

>>> # Initializing an InternLM3-8B style configuration
>>> configuration = InternLM3Config()

>>> # Initializing a model (which uses the Llama implementation) from that configuration
>>> model = AutoModelForCausalLM.from_config(configuration)

>>> # Accessing the model configuration
>>> configuration = model.config
```

