|
| 1 | +#------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | +# |
| 20 | +#------------------------------------------------------------- |
| 21 | + |
| 22 | +source("nn/layers/affine.dml") as affine |
| 23 | +source("nn/layers/multi_attention.dml") as attention |
| 24 | +source("nn/layers/dropout.dml") as dropout |
| 25 | +source("nn/layers/batch_norm1d.dml") as batch_norm |
| 26 | +source("nn/layers/tanh.dml") as tanh |
| 27 | +source("nn/layers/gelu.dml") as gelu |
| 28 | + |
| 29 | +linear_tensor_forward = function(matrix[double] X, matrix[double] W, matrix[double] b, int B, int C) |
| 30 | + return (matrix[double] out) { |
| 31 | + /* |
| 32 | + * Helper function for computing linear layer with tensor input, of shape (A, B*C) |
| 33 | + */ |
| 34 | + A = nrow(X) |
| 35 | + C_new = ncol(W) |
| 36 | + out = affine::forward(matrix(X, rows=A*B, cols=C), W, b) |
| 37 | + out = matrix(out, rows=A, cols=B*C_new) |
| 38 | +} |
| 39 | + |
| 40 | +layer_norm_forward = function(matrix[double] X, matrix[double] gamma, matrix[double] beta, double epsilon, int B, int C) |
| 41 | + return (matrix[double] out, matrix[double] cache_mean, matrix[double] cache_var, matrix[double] cache_norm) { |
| 42 | + /* |
| 43 | + * Helper function for computing layer norm via 1D batch norm with tensor input, of shpae (A, B*C) |
| 44 | + */ |
| 45 | + A = nrow(X) |
| 46 | + batch_norm_input = t(matrix(X, rows=A*B, cols=C)) |
| 47 | + # EMA matrices are unused and thus empty matrices will be provided |
| 48 | + emas_mat = matrix(0, rows=1, cols=A*B) |
| 49 | + [batch_norm_out, unused1, unused2, cache_mean, cache_var, cache_norm] = batch_norm::forward( |
| 50 | + batch_norm_input, t(gamma), t(beta), "train", emas_mat, emas_mat, 0.0, epsilon) |
| 51 | + out = matrix(t(batch_norm_out), rows=A, cols=B*C) |
| 52 | +} |
| 53 | + |
| 54 | +forward = function(matrix[double] states, |
| 55 | + int H, int T, int d, int I, |
| 56 | + matrix[double] W_Q, matrix[double] b_Q, |
| 57 | + matrix[double] W_K, matrix[double] b_K, |
| 58 | + matrix[double] W_V, matrix[double] b_V, |
| 59 | + matrix[double] W_context, matrix[double] b_context, |
| 60 | + matrix[double] W_intermediate, matrix[double] b_intermediate, |
| 61 | + matrix[double] W_out, matrix[double] b_out, |
| 62 | + double dropout_p_attention, |
| 63 | + double dropout_p_output, |
| 64 | + double epsilon_ln, |
| 65 | + matrix[double] gamma_ln1, matrix[double] beta_ln1, |
| 66 | + matrix[double] gamma_ln2, matrix[double] beta_ln2, |
| 67 | + string activation) |
| 68 | + return (matrix[double] out_states, matrix[double] attention, |
| 69 | + list[unknown] outputs, |
| 70 | + matrix[double] dropout_mask_attention, |
| 71 | + matrix[double] dropout_mask_output_1, |
| 72 | + matrix[double] dropout_mask_output_2, |
| 73 | + matrix[double] cache_mean_ln1, matrix[double] cache_var_ln1, matrix[double] cache_norm_ln1, |
| 74 | + matrix[double] cache_mean_ln2, matrix[double] cache_var_ln2, matrix[double] cache_norm_ln2) { |
| 75 | + /* |
| 76 | + * Computes the forward pass for a layer of the BERT transformer architecture. |
| 77 | + * |
| 78 | + * Inputs (B: Batch size, T: Sequence length, D: Embedding length, H: Heads): |
| 79 | + * - states: Hidden states, of shape (B, T*D). |
| 80 | + * - H: Head count. |
| 81 | + * - T: Sequence length. |
| 82 | + * - d: Embedding length of single token per head with d*H = D. |
| 83 | + * - I: Intemediate embedding length. |
| 84 | + * - W_Q: Weights for linear query layer, of shape (D, D). |
| 85 | + * - b_Q: Biases for linear query layer, of shape (1, D). |
| 86 | + * - W_K: Weights for linear key layer, of shape (D, D). |
| 87 | + * - b_K: Biases for linear key layer, of shape (1, D). |
| 88 | + * - W_V: Weights for linear value layer, of shape (D, D). |
| 89 | + * - b_V: Biases for linear value layer, of shape (1, D). |
| 90 | + * - W_context: Weights for linear output layer on context, of shape (D, D). |
| 91 | + * - b_context: Biases for linear output layer on context, of shape (1, D). |
| 92 | + * - W_intermediate: Weights for intermediate linear layer, of shape (D, I). |
| 93 | + * - b_intermediate: Biases for intermediate linear layer, of shape (1, I). |
| 94 | + * - W_out: Weights for last linear output layer, of shape (D, D). |
| 95 | + * - b_out: Biases for last linear output layer, of shape (1, D). |
| 96 | + * - dropout_p_attention: Probability for dropout on attention. |
| 97 | + * - dropout_p_output: Probability for dropout on output. |
| 98 | + * - epsilon_ln: Epsilon value for layer norm. |
| 99 | + * - gamma_ln1: Gamma params for layer norm 1, of shape (1, D). |
| 100 | + * - beta_ln1: Beta params for layer norm 1, of shape (1, D). |
| 101 | + * - gamma_ln2: Gamma params for layer norm 2, of shape (1, D). |
| 102 | + * - beta_ln2: Beta params for layer norm 2, of shape (1, D). |
| 103 | + * - activation: String specifying type of activation to use. |
| 104 | + * Can be tanh or gelu. |
| 105 | + * |
| 106 | + * Outputs: |
| 107 | + * - out_states: Token output states, of shape (B, T*D) |
| 108 | + * - attention: Attention values for keys & querys, of shape (B, H*T*T) |
| 109 | + * - outputs: List of relevant outputs for backward pass with following |
| 110 | + * order/content: |
| 111 | + * -> 1: Output of linear query layer, of shape (B, T*D). |
| 112 | + * -> 2: Output of linear key layer, of shape (B, T*D). |
| 113 | + * -> 3: Output of linear value layer, of shape (B, T*D). |
| 114 | + * -> 4: Output context of attention layer, of shape (B, T*D). |
| 115 | + * -> 5: Output attention of attention layer, of shape (B, T*D). |
| 116 | + * -> 6: Output of residual pass 1, of shape (B, T*D). |
| 117 | + * -> 7: Output of layer norm 1, of shape (B, T*D). |
| 118 | + * -> 8: Output of intermediate linear layer, of shape (B, T*I). |
| 119 | + * -> 9: Output of activation layer, of shape (B, T*I). |
| 120 | + * -> 10: Output of residual pass 2, of shape (B, T*D). |
| 121 | + * - dropout_mask_attention: Dropout mask used on attention, of shape (B, H*T*T) |
| 122 | + * - dropout_mask_output_1: Dropout mask used on attention output, of shape (B, T*D) |
| 123 | + * - dropout_mask_output_2: Dropout mask used on attention output, of shape (B, T*D) |
| 124 | + * - cache_mean_ln1: Cached mean from layer norm 1, of shape (1, B*T) |
| 125 | + * - cache_var_ln1: Cached mean from layer norm 1, of shape (1, B*T) |
| 126 | + * - cache_norm_ln1: Cached mean from layer norm 1, of shape (1, B*T) |
| 127 | + * - cache_mean_ln2: Cached mean from layer norm 2, of shape (1, B*T) |
| 128 | + * - cache_var_ln2: Cached mean from layer norm 2, of shape (1, B*T) |
| 129 | + * - cache_norm_ln2: Cached mean from layer norm 2, of shape (1, B*T) |
| 130 | + */ |
| 131 | + # Embedding dim |
| 132 | + D = d * H |
| 133 | + |
| 134 | + # Linear layers for Q, K, V |
| 135 | + Q = linear_tensor_forward(states, W_Q, b_Q, T, D) # Shape (B, T*D) |
| 136 | + K = linear_tensor_forward(states, W_K, b_K, T, D) # Shape (B, T*D) |
| 137 | + V = linear_tensor_forward(states, W_V, b_V, T, D) # Shape (B, T*D) |
| 138 | + |
| 139 | + # Multi-head self attention |
| 140 | + [context, attention, dropout_mask_attention] = attention::forward(Q, K, V, H, T, d, dropout_p_attention) |
| 141 | + # Shapes (B, T*D), (B, H*T*T), (B, H*T*T) |
| 142 | + outputs = list(Q, K, V, context, attention) |
| 143 | + |
| 144 | + # Linear layer on attention output (output layer) |
| 145 | + out_states = linear_tensor_forward(context, W_context, b_context, T, D) # Shape (B, T*D) |
| 146 | + # Dropout on output 1 |
| 147 | + dropout_mask_output_1 = matrix(0, 1, 1) |
| 148 | + if (dropout_p_output > 0.0) { |
| 149 | + [out_states, dropout_mask_output_1] = dropout::forward(out_states, dropout_p_output, -1) |
| 150 | + } |
| 151 | + |
| 152 | + # Residual pass 1 |
| 153 | + out_states = out_states + states # Shapes (B, T*D). |
| 154 | + outputs = append(outputs, out_states) |
| 155 | + # Layer norm 1 for each token |
| 156 | + [out_states, cache_mean_ln1, cache_var_ln1, cache_norm_ln1] = layer_norm_forward( |
| 157 | + out_states, gamma_ln1, beta_ln1, epsilon_ln, T, D) |
| 158 | + outputs = append(outputs, out_states) |
| 159 | + |
| 160 | + # Save out_states for residual pass |
| 161 | + out_states_identity = out_states |
| 162 | + # Linear layer of intermediate part |
| 163 | + out_states = linear_tensor_forward(out_states, W_intermediate, b_intermediate, T, D) # Shape (B, T*I) |
| 164 | + outputs = append(outputs, out_states) |
| 165 | + # Activation |
| 166 | + if (activation == "gelu") { |
| 167 | + out_states = gelu::forward(out_states) |
| 168 | + } else if (activation == "tanh") { |
| 169 | + out_states = tanh::forward(out_states) |
| 170 | + } |
| 171 | + outputs = append(outputs, out_states) |
| 172 | + |
| 173 | + # Final linear output layer |
| 174 | + out_states = linear_tensor_forward(out_states, W_out, b_out, T, I) # Shape (B, T*D) |
| 175 | + # Dropout on output 2 |
| 176 | + dropout_mask_output_2 = matrix(0, 1, 1) |
| 177 | + if (dropout_p_output > 0.0) { |
| 178 | + [out_states, dropout_mask_output_2] = dropout::forward(out_states, dropout_p_output, -1) |
| 179 | + } |
| 180 | + # Residual pass 2 |
| 181 | + out_states = out_states + out_states_identity |
| 182 | + outputs = append(outputs, out_states) |
| 183 | + # Layer norm 2 for each token |
| 184 | + [out_states, cache_mean_ln2, cache_var_ln2, cache_norm_ln2] = layer_norm_forward( |
| 185 | + out_states, gamma_ln2, beta_ln2, epsilon_ln, T, D) |
| 186 | +} |
0 commit comments