/*
 *  ******************************************************************************
 *  *
 *  *
 *  * This program and the accompanying materials are made available under the
 *  * terms of the Apache License, Version 2.0 which is available at
 *  * https://www.apache.org/licenses/LICENSE-2.0.
 *  *
 *  *  See the NOTICE file distributed with this work for additional
 *  *  information regarding copyright ownership.
 *  * 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.
 *  *
 *  * SPDX-License-Identifier: Apache-2.0
 *  *****************************************************************************
 */

package org.eclipse.deeplearning4j.dl4jcore.nn.layers.samediff.testlayers;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.deeplearning4j.nn.conf.graph.GraphVertex;
import org.deeplearning4j.nn.conf.layers.samediff.SDVertexParams;
import org.deeplearning4j.nn.conf.layers.samediff.SameDiffVertex;
import org.deeplearning4j.nn.params.DefaultParamInitializer;
import org.deeplearning4j.nn.weights.WeightInit;
import org.deeplearning4j.nn.weights.WeightInitUtil;
import org.nd4j.autodiff.samediff.SDVariable;
import org.nd4j.autodiff.samediff.SameDiff;
import org.nd4j.linalg.activations.Activation;
import org.nd4j.linalg.api.ndarray.INDArray;

import java.util.Map;

@NoArgsConstructor
@Data
public class SameDiffDenseVertex extends SameDiffVertex {

    private int nIn;
    private int nOut;
    private Activation activation;
    private WeightInit weightInit;

    public SameDiffDenseVertex(int nIn, int nOut, Activation activation, WeightInit weightInit){
        this.nIn = nIn;
        this.nOut = nOut;
        this.activation = activation;
        this.weightInit = weightInit;
    }

    @Override
    public SDVariable defineVertex(SameDiff sameDiff, Map<String, SDVariable> layerInput, Map<String, SDVariable> paramTable, Map<String, SDVariable> maskVars) {
        SDVariable weights = paramTable.get(DefaultParamInitializer.WEIGHT_KEY);
        SDVariable bias = paramTable.get(DefaultParamInitializer.BIAS_KEY);

        SDVariable mmul = sameDiff.mmul("mmul", layerInput.get("in"), weights);
        SDVariable z = mmul.add("z", bias);
        return activation.asSameDiff("out", sameDiff, z);
    }

    @Override
    public void defineParametersAndInputs(SDVertexParams params) {
        params.defineInputs("in");
        params.addWeightParam("W", nIn, nOut);
        params.addBiasParam("b", 1, nOut);
    }

    @Override
    public void initializeParameters(Map<String, INDArray> params) {
        //Normally use 'c' order, but use 'f' for direct comparison to DL4J DenseLayer
        WeightInitUtil.initWeights(nIn, nOut, new long[]{nIn, nOut}, weightInit, null, 'f', params.get("W"));
        params.get("b").assign(0.0);
    }

    @Override
    public char paramReshapeOrder(String paramName){
        return 'f';     //To match DL4J DenseLayer - for easy comparison
    }

    @Override
    public GraphVertex clone() {
        return new SameDiffDenseVertex(nIn, nOut, activation, weightInit);
    }
}
