You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@systemds.apache.org by GitBox <gi...@apache.org> on 2022/04/13 14:50:48 UTC

[GitHub] [systemds] SteveineiterTU commented on a diff in pull request #1584: [SYSTEMDS-3303] WIP: NN Builtin: Attention Layer

SteveineiterTU commented on code in PR #1584:
URL: https://github.com/apache/systemds/pull/1584#discussion_r849578085


##########
scripts/nn/layers/attention.dml:
##########
@@ -0,0 +1,64 @@
+#-------------------------------------------------------------
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+#-------------------------------------------------------------
+
+source("nn/layers/softmax.dml") as softmax
+
+# 1. Create "calculate_score" function, for the beginning without additional parameters -> calculate with dot product.
+
+calculate_scores = function(matrix[double] query, matrix[double] key)
+    return (matrix[double] scores) {
+    /*
+     * dim(query) = AxB
+     * dim(key) = CxB
+     * dim(scores) = AxC
+     */
+    scores = query %*% t(key)
+}

Review Comment:
   Here we are unsure how many parameters / choices a user should have. 
   
   E.g: 
   - masking
   - scaling
   - different calculation variants (or is dot enough)



##########
scripts/nn/examples/attention_layer.dml:
##########
@@ -0,0 +1,82 @@
+#-------------------------------------------------------------
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+#-------------------------------------------------------------
+
+
+source("nn/layers/attention.dml") as attention
+
+# Create training data
+q = matrix("2 0 2 2 0 0 4 0 2 2 1 2", rows=4, cols=3)
+k = matrix("2 2 2 0 2 1 2 4 3 0 1 1", rows=4, cols=3)
+v = matrix("1 1 0 0 1 1 1 2 1 0 0 0", rows=4, cols=3)
+
+print(toString(q))
+print(toString(k))
+print(toString(v))
+print("Before attention calculation")
+attention = attention::forward(q, k, v)
+
+
+# ============== Simple test case =============================
+# Create training data
+q = matrix(1.1, rows=1, cols=1)
+k = matrix(1.6, rows=1, cols=1)
+v = matrix(1, rows=1, cols=1)
+
+calculated = attention::forward(q, k, v)
+print(toString(calculated))
+
+# expected = q * k = 1.1 * 1.6 = 1.76
+expected = matrix(1.76, rows=1, cols=1)
+assert(toString(calculated) == toString(expected))
+
+# ============== Example ======================================
+# 1. input eg text data
+# 2. embedding layer
+# 3. lstm layer
+# 4. attention layer
+# 5. dense layer

Review Comment:
   We are unsure if it is fine to use simple examples as in lines 25-34 or 38-48, or if we should construct something more relevant/practical for example a text classification with an architecture similar as described in lines 51-55? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@systemds.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org