All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nd4j.autodiff.validation.listeners.NonInplaceValidationListener Maven / Gradle / Ivy

There is a newer version: 1.0.0-M2.1
Show newest version
package org.nd4j.autodiff.validation.listeners;

import lombok.Getter;
import org.nd4j.autodiff.listeners.At;
import org.nd4j.autodiff.listeners.BaseListener;
import org.nd4j.autodiff.listeners.Operation;
import org.nd4j.autodiff.samediff.SameDiff;
import org.nd4j.autodiff.samediff.internal.SameDiffOp;
import org.nd4j.base.Preconditions;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.api.ops.DynamicCustomOp;
import org.nd4j.linalg.api.ops.Op;

import java.security.MessageDigest;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
import org.nd4j.linalg.dataset.api.MultiDataSet;

public class NonInplaceValidationListener extends BaseListener {
    @Getter
    private static AtomicInteger useCounter = new AtomicInteger();
    @Getter
    private static AtomicInteger passCounter = new AtomicInteger();
    @Getter
    private static AtomicInteger failCounter = new AtomicInteger();

    protected INDArray[] opInputs;
    protected INDArray[] opInputsOrig;

    public NonInplaceValidationListener(){
        useCounter.getAndIncrement();
    }

    @Override
    public void preOpExecution(SameDiff sd, At at, SameDiffOp op) {
        if(op.getOp().isInPlace()){
            //Don't check inplace op
            return;
        }
        if(op.getOp() instanceof Op){
            Op o = (Op)op.getOp();
            if(o.x() == null){
                //No input op
                return;
            } else if(o.y() == null){
                opInputsOrig = new INDArray[]{o.x()};
                opInputs = new INDArray[]{o.x().dup()};
            } else {
                opInputsOrig = new INDArray[]{o.x(), o.y()};
                opInputs = new INDArray[]{o.x().dup(), o.y().dup()};
            }
        } else if(op.getOp() instanceof DynamicCustomOp){
            INDArray[] arr = ((DynamicCustomOp) op.getOp()).inputArguments();
            opInputs = new INDArray[arr.length];
            opInputsOrig = new INDArray[arr.length];
            for( int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy