|
| 1 | +# -*- coding:utf-8 -*- |
| 2 | +"""Custom layer for the WuKong model: one stackable interaction layer made of a |
| 3 | +Factorization-Machine Block (FMB) and a Linear Compression Block (LCB), joined |
| 4 | +by a residual connection. |
| 5 | +
|
| 6 | +Reference: |
| 7 | + Zhang B, et al. Wukong: Towards a Scaling Law for Large-Scale Recommendation. |
| 8 | + ICML 2024. (https://arxiv.org/abs/2403.02545) |
| 9 | +""" |
| 10 | + |
| 11 | +import tensorflow as tf |
| 12 | +from tensorflow.keras.layers import Layer, Dropout |
| 13 | + |
| 14 | +from .normalization import LayerNormalization |
| 15 | + |
| 16 | + |
| 17 | +class WuKongLayer(Layer): |
| 18 | + """One WuKong interaction layer over a token matrix ``X`` of shape (B, k, d). |
| 19 | +
|
| 20 | + * FMB: explicit pairwise interactions ``X X^T`` (B, k, k) → MLP → reshaped to |
| 21 | + ``(B, n_fmb, d)`` and layer-normed. |
| 22 | + * LCB: a learned linear recombination of the input embeddings → ``(B, n_lcb, d)``. |
| 23 | + * Output: ``concat([FMB, LCB], axis=1) + X`` (residual), shape ``(B, k, d)``, |
| 24 | + so layers stack uniformly. |
| 25 | +
|
| 26 | + Input / output shape: ``(batch_size, k, d)``. |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__(self, num_fmb_tokens=None, fmb_hidden_dim=128, dropout_rate=0.0, |
| 30 | + seed=1024, **kwargs): |
| 31 | + super(WuKongLayer, self).__init__(**kwargs) |
| 32 | + self.num_fmb_tokens = num_fmb_tokens |
| 33 | + self.fmb_hidden_dim = fmb_hidden_dim |
| 34 | + self.dropout_rate = dropout_rate |
| 35 | + self.seed = seed |
| 36 | + self.dropout = Dropout(dropout_rate, seed=seed) |
| 37 | + self.ln = LayerNormalization() |
| 38 | + |
| 39 | + def build(self, input_shape): |
| 40 | + self.k = int(input_shape[1]) |
| 41 | + self.d = int(input_shape[2]) |
| 42 | + # split output tokens so n_fmb + n_lcb == k (keeps the layer stackable) |
| 43 | + self.n_fmb = self.num_fmb_tokens if self.num_fmb_tokens else max(1, self.k // 2) |
| 44 | + self.n_fmb = min(self.n_fmb, self.k) |
| 45 | + self.n_lcb = self.k - self.n_fmb |
| 46 | + |
| 47 | + # FMB: flatten(X X^T) (k*k) -> hidden -> n_fmb*d |
| 48 | + self.fmb_w1 = self.add_weight(name="fmb_w1", shape=(self.k * self.k, self.fmb_hidden_dim), |
| 49 | + initializer="glorot_uniform") |
| 50 | + self.fmb_b1 = self.add_weight(name="fmb_b1", shape=(self.fmb_hidden_dim,), |
| 51 | + initializer="zeros") |
| 52 | + self.fmb_w2 = self.add_weight(name="fmb_w2", shape=(self.fmb_hidden_dim, self.n_fmb * self.d), |
| 53 | + initializer="glorot_uniform") |
| 54 | + self.fmb_b2 = self.add_weight(name="fmb_b2", shape=(self.n_fmb * self.d,), |
| 55 | + initializer="zeros") |
| 56 | + # LCB: (n_lcb, k) linear recombination of input embeddings |
| 57 | + if self.n_lcb > 0: |
| 58 | + self.lcb_w = self.add_weight(name="lcb_w", shape=(self.n_lcb, self.k), |
| 59 | + initializer="glorot_uniform") |
| 60 | + super(WuKongLayer, self).build(input_shape) |
| 61 | + |
| 62 | + def call(self, inputs, training=None, **kwargs): |
| 63 | + x = inputs # (B, k, d) |
| 64 | + # ── FMB ── |
| 65 | + inter = tf.matmul(x, x, transpose_b=True) # (B, k, k) |
| 66 | + flat = tf.reshape(inter, [-1, self.k * self.k]) # (B, k*k) |
| 67 | + h = tf.nn.relu(tf.matmul(flat, self.fmb_w1) + self.fmb_b1) |
| 68 | + h = self.dropout(h, training=training) |
| 69 | + h = tf.matmul(h, self.fmb_w2) + self.fmb_b2 # (B, n_fmb*d) |
| 70 | + fmb = tf.reshape(h, [-1, self.n_fmb, self.d]) # (B, n_fmb, d) |
| 71 | + fmb = self.ln(fmb) |
| 72 | + |
| 73 | + # ── LCB ── |
| 74 | + if self.n_lcb > 0: |
| 75 | + lcb = tf.einsum("lk,bkd->bld", self.lcb_w, x) # (B, n_lcb, d) |
| 76 | + out = tf.concat([fmb, lcb], axis=1) # (B, k, d) |
| 77 | + else: |
| 78 | + out = fmb |
| 79 | + |
| 80 | + # ── residual ── |
| 81 | + return out + x |
| 82 | + |
| 83 | + def compute_output_shape(self, input_shape): |
| 84 | + return input_shape |
| 85 | + |
| 86 | + def get_config(self): |
| 87 | + config = {"num_fmb_tokens": self.num_fmb_tokens, "fmb_hidden_dim": self.fmb_hidden_dim, |
| 88 | + "dropout_rate": self.dropout_rate, "seed": self.seed} |
| 89 | + base = super(WuKongLayer, self).get_config() |
| 90 | + base.update(config) |
| 91 | + return base |
0 commit comments