#-------------------------------------------------------------
#
# 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/gelu.dml") as gelu
source("src/test/scripts/applications/nn/util.dml") as test_util

gelu_test1 = function() {
    print("Testing GELU, test 1")

    X = matrix("1.  -0.5
                0.   2.", rows=2, cols=2)
    dout = matrix("1 1
                1 1", rows=2, cols=2)
    out_expected = matrix("0.841192  -0.154286
                        0.         1.9545977", rows=2, cols=2)
    gradient_expected = matrix("1.0829641  0.13263011
                                0.5        1.0860993", rows=2, cols=2)

    out = gelu::forward(X)

    test_util::check_all_close(out, out_expected, 0.00001)

    gradient = gelu::backward(dout, X)
    test_util::check_all_close(gradient, gradient_expected, 0.00001)
}

gelu_test2 = function() {
    print("Testing GELU, test 2")

    X = matrix("0.5 -1.5
                1.  -2.", rows=2, cols=2)
    dout = matrix("1 1
                1 1", rows=2, cols=2)
    out_expected = matrix("0.345714   -0.10042843
                        0.841192   -0.04540229", rows=2, cols=2)
    gradient_expected = matrix("0.8673699  -0.1277108
                                1.0829641  -0.08609922", rows=2, cols=2)

    out = gelu::forward(X)

    test_util::check_all_close(out, out_expected, 0.00001)

    gradient = gelu::backward(dout, X)
    test_util::check_all_close(gradient, gradient_expected, 0.00001)
}

gelu_test1()
gelu_test2()