учти вот это: "hiddenact": "silu", "hiddensize": 5120, "initializerrange": 0.02,…
- User
учти вот это:
"hidden_act": "silu", "hidden_size": 5120, "initializer_range": 0.02, "intermediate_size": 13824, "max_length": 4096, "max_position_embeddings": 4096, "model_type": "llama", "num_attention_heads": 40, "num_hidden_layers": 40, "num_key_value_heads": 40,
и это:
import tensorflow as tf
from tensorflow.keras.layers import Dense, LayerNormalization, Embedding, Dropout, Layer
from tensorflow.keras.models import Sequential
class LlamaAttention(Layer):
def init(self):
super().__init__() self.rotary_emb = LlamaRotaryEmbedding() self.k_proj = QuantLinear() self.o_proj = QuantLinear() self.q_proj = QuantLinear() self.v_proj = QuantLinear()def call(self, inputs):
# implement attentionclass LlamaMLP(Layer):
def init(self):
super().__init__() self.act_fn = tf.keras.layers.SiLU() self.down_proj = QuantLinear() self.gate_proj = QuantLinear() self.up_proj = QuantLinear()def call(self, inputs):
# implement MLPclass LlamaDecoderLayer(Layer):
def init(self):
super().__init__() self.self_attn = LlamaAttention() self.mlp = LlamaMLP() self.input_layernorm = LlamaRMSNorm() self.post_attention_layernorm = LlamaRMSNorm()def call(self, inputs):
# implement decoder layerclass LlamaModel(tf.keras.Model):
def init(self, vocab_size, d_model):
super().__init__() self.embed_tokens = Embedding(vocab_size, d_model) self.layers = [LlamaDecoderLayer() for _ in range(40)] self.norm = LlamaRMSNorm()def call(self, inputs):
x = self.embed_tokens(inputs) for layer in self.layers: x = layer(x) x = self.norm(x) return xmodel = LlamaForCausalLM(
model=LlamaModel(32000, 5120),
lm_head=Dense(32000)
)
и дай model architecture in tf.keras:
- Assistant
Вот модельная архитектура в tf.keras:
import tensorflow as tf from tensorflow.keras.layers import Dense, LayerNormalization, Embedding, Dropout, Layer from tensorflow.keras.models import Sequential class LlamaAttention(Layer): def __init__(self): super().__init__() self.rotary_emb = LlamaRotaryEmbedding() self.k_proj = QuantLinear() self.o_proj = QuantLinear() self.q_proj = QuantLinear() self.v_proj = QuantLinear() def call(self, inputs): # implement attention class LlamaMLP(Layer): def __init__(self): super().__init__() self.act_fn = tf.keras.layers.SiLU() self.down_proj = QuantLinear() self.gate_proj = QuantLinear() self.up_proj = QuantLinear() def call(self, inputs): # implement MLP class LlamaDecoderLayer(Layer): def __init__(self): super().__init__() self.self_attn = LlamaAttention() self.mlp = LlamaMLP() self.input_layernorm = LlamaRMSNorm() self.post_attention_layernorm = LlamaRMSNorm() def call(self, inputs): # implement decoder layer class LlamaModel(tf.keras.Model): def __init__(self, vocab_size, d_model): super().__init__() self.embed_tokens = Embedding(vocab_size, d_model) self.layers = [LlamaDecoderLayer() for _ in range(40)] self.norm = LlamaRMSNorm() def call(self, inputs): x = self.embed_tokens(inputs) for layer in self.layers: x = layer(x) x = self.norm(x) return x model = LlamaModel(32000, 5120) model.build(input_shape=(None, sequence_length)) model.summary()