Yuchan commited on
Update Mo.py
Browse files123 unmodified lines12412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317412 unmodified lines187188189190191192193123 unmodified linesx_val, x_gate = tf.split(x_proj, 2, axis=-1)return self.out(x_val * tf.nn.silu(x_gate))class LoU(layers.Layer):def __init__(self, d_model, clip_value=5.0, eps=1e-6):super().__init__()self.d_model = d_modelself.clip_value = float(clip_value)self.eps = float(eps)# Q/K/V ๋ณํself.Q = layers.Dense(d_model, dtype='float32')self.K = layers.Dense(d_model, dtype='float32')self.V = layers.Dense(d_model, dtype='float32')# ์ ๊ทํself.norm = layers.LayerNormalization(epsilon=1e-5, dtype='float32')self.norm1 = layers.LayerNormalization(epsilon=1e-5, dtype='float32')# ๋น์ ํ ํํ๋ ฅself.glu = SwiGLU(d_model, 320)# ํ์ต ๊ฐ๋ฅํ ๊ณผ๊ฑฐ ํ ํฐ ๊ฐ์ค์นself.alpha = self.add_weight(shape=(d_model,), initializer='ones', trainable=True)def call(self, x):x_f32 = tf.cast(x, tf.float32)residual = x_f32x_f32 = self.norm1(x)q = self.Q(x_f32)k = self.K(x_f32)V = self.V(x_f32)g_q = (tf.nn.tanh(q) + 1.0) / 2.0g_k = (tf.nn.tanh(k) + 1.0) / 2.0# ๊ณผ๊ฑฐ ํ ํฐ ๊ฐ์ค์น ๋ฐ์ ์ ์score = g_q * g_k * self.alpha # element-wise scaling# ๋์ ํฉ ๋์ ๊ฐ์ค ํ๊ท# score_t = sum_{i=0}^{t} alpha_i * V_i / sum_{i=0}^{t} alpha_iscore_cum = tf.math.cumsum(score * V, axis=1)alpha_cum = tf.math.cumsum(score, axis=1)score_weighted = score_cum / tf.maximum(alpha_cum, self.eps)# ์ ๊ทํ + ํด๋ฆฌํscore_norm = tf.clip_by_value(score_weighted, -self.clip_value, self.clip_value)out = self.norm(score_norm + residual)out = self.glu(out)return tf.cast(out, x.dtype)12 unmodified linesclass Block(layers.Layer):def __init__(self, d_model):super().__init__()self.lou = LoU(d_model)self.lo = Lo(d_model)def call(self, x):123 unmodified lines12412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217312 unmodified lines186187188189190191192123 unmodified linesx_val, x_gate = tf.split(x_proj, 2, axis=-1)return self.out(x_val * tf.nn.silu(x_gate))class LoUScan(layers.Layer):def __init__(self, d_model, clip_value=5.0, eps=1e-6):super().__init__()self.d_model = d_modelself.clip_value = float(clip_value)self.eps = float(eps)self.Q = layers.Dense(d_model, dtype='float32')self.K = layers.Dense(d_model, dtype='float32')self.V = layers.Dense(d_model, dtype='float32')self.norm = layers.LayerNormalization(epsilon=1e-5, dtype='float32')self.norm1 = layers.LayerNormalization(epsilon=1e-5, dtype='float32')self.glu = SwiGLU(d_model, 3500)def call(self, x):x_f32 = tf.cast(x, tf.float32)residual = x_f32x_f32 = self.norm1(x_f32)q = self.Q(x_f32)k = self.K(x_f32)v = self.V(x_f32)g_q = (tf.nn.tanh(q) + 1.0) / 2.0g_k = (tf.nn.tanh(k) + 1.0) / 2.0score = g_q * g_k # gating# tf.scan์ผ๋ก ์์ฐจ ๋์ ํฉ (์ธ๊ณผ์ )def step(carry, inputs):prev_sum = carrys, v_t = inputsnew_sum = prev_sum + s * v_t# ์ ๊ทํout = new_sum / tf.maximum(tf.reduce_sum(score[:tf.shape(prev_sum)[0]], axis=0, keepdims=True), self.eps)return new_sum, out# ์ด๊ธฐ๊ฐinit = tf.zeros_like(v[0])_, outputs = tf.scan(step, (score, v), initializer=init, axis=0)# ์์ ํoutputs = tf.clip_by_value(outputs, -self.clip_value, self.clip_value)out = self.norm(outputs + residual)out = self.glu(out)return tf.cast(out, x.dtype)12 unmodified linesclass Block(layers.Layer):def __init__(self, d_model):super().__init__()self.lou = LoUScan(d_model)self.lo = Lo(d_model)def call(self, x):
Mo.py+23-24