protected void associateSameDiffWithOpsAndVariables(){
        for(SDVariable var : variableMap().values()){
            var.setSameDiff(this);
        }
//        for(DifferentialFunction df : functionInstancesById.values()){
        for(SameDiffOp op : ops.values()){
            DifferentialFunction df = op.getOp();
            df.setSameDiff(this);

            //TODO: This is ugly but seemingly necessary
            //Finally, also set the SDVariable for each op
            //Otherwise: could have an op pointing to this SameDiff instance, but op's SDVariable's sameDiff field pointing
            // to another SameDiff instance. At which point, they could fetch shapes and arrays from some other instance
            // (i.e., not from this one that is currently executing)
            SDVariable[] args = df.args();
            if(args != null){
                for(SDVariable arg : args){
                    arg.setSameDiff(this);
                }
            }

            SDVariable[] outputs = df.outputVariables();
            if(outputs != null){
                for(SDVariable out : outputs){
                    out.setSameDiff(this);
                }
            }
        }
    }
protected void associateSameDiffWithOpsAndVariables ( ) { for ( SDVariable var : variableMap ( ) . values ( ) ) { var . setSameDiff ( this ) ; } for ( SameDiffOp op : ops . values ( ) ) { DifferentialFunction df = op . getOp ( ) ; df . setSameDiff ( this ) ; SDVariable [ ] args = df . args ( ) ; if ( args != null ) { for ( SDVariable arg : args ) { arg . setSameDiff ( this ) ; } } SDVariable [ ] outputs = df . outputVariables ( ) ; if ( outputs != null ) { for ( SDVariable out : outputs ) { out . setSameDiff ( this ) ; } } } }
Associate the current SameDiff instance with all ops and variables.
This is necessary to ensure that when dealing with shared state (usually with a SameDiff function such
as "grad" - the backward function) we have the correct SameDiff instance set for all ops/SDVariables.<br>
If this is not done, arrays and shapes could be fetched from the incorrect SameDiff instance for some methods