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:41:10 UTC

[GitHub] [systemds] SteveineiterTU opened a new pull request, #1584: [SYSTEMDS-3303] WIP: NN Builtin: Attention Layer

SteveineiterTU opened a new pull request, #1584:
URL: https://github.com/apache/systemds/pull/1584

   This is WIP. 
   
   This implements the attention layer. This allows to train RNNs with long term sequences easier using the attention mechanism. 
   
   A quick reference to what an attention layer is: https://analyticsindiamag.com/a-beginners-guide-to-using-attention-layer-in-neural-networks/
   
   
   
   Tasks:
   - [ ] Add attention.dml to scripts/nn/layers/ with the implementation of the layer.
   - [ ] Add an AttentionExample.dml file to scripts/nn/examples where the attention layer is used with an lstm-layer to demonstrate that an attention model can be trained and used for predictions.
   


-- 
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


[GitHub] [systemds] Baunsgaard commented on pull request #1584: [SYSTEMDS-3303] WIP: NN Builtin: Attention Layer

Posted by GitBox <gi...@apache.org>.
Baunsgaard commented on PR #1584:
URL: https://github.com/apache/systemds/pull/1584#issuecomment-1147372052

   @SteveineiterTU  and @A-Postl : Are we closing this PR because of the other one being more finished?


-- 
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


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

Posted by GitBox <gi...@apache.org>.
Baunsgaard commented on code in PR #1584:
URL: https://github.com/apache/systemds/pull/1584#discussion_r856099481


##########
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:
   Start with the simplest thing you can think of, then we can add arguments later that add the different parts.



-- 
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


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

Posted by GitBox <gi...@apache.org>.
Baunsgaard commented on code in PR #1584:
URL: https://github.com/apache/systemds/pull/1584#discussion_r856103388


##########
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:
   you choose, personally i would prefer a relevant/practical solution, that we can plug in that actually works nicely :)
   But if you find it to complicated you can start with a simpler model, and then make perhaps a boolean argument chose a simple or more complicated model.



-- 
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


[GitHub] [systemds] Baunsgaard closed pull request #1584: [SYSTEMDS-3303] WIP: NN Builtin: Attention Layer

Posted by GitBox <gi...@apache.org>.
Baunsgaard closed pull request #1584: [SYSTEMDS-3303] WIP: NN Builtin: Attention Layer
URL: https://github.com/apache/systemds/pull/1584


-- 
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


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

Posted by GitBox <gi...@apache.org>.
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