nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/autodiff/samediff/SameDiff.java
public ByteBuffer asFlatBuffers(@NonNull ExecutorConfiguration configuration) {
        Nd4j.getExecutioner().commit();
        FlatBufferBuilder bufferBuilder = new FlatBufferBuilder(1024);
        val idCounter = new AtomicInteger(0);

        val flatVariables = new ArrayList<Integer>();
        val flatOffsets = new ArrayList<Integer>();
        val flatNodes = new ArrayList<Integer>();

        // first of all we build VariableSpace dump
        List<SDVariable> variableList = new ArrayList<>(variables());
        val reverseMap = new LinkedHashMap<String, Integer>();
        val forwardMap = new LinkedHashMap<String, Integer>();
        val framesMap = new LinkedHashMap<String, Integer>();

        int idx = 0;
        for (val variable : variables()) {
            log.debug("Exporting variable: [{}]", variable.getVarName());
            if (variable.getArr() == null || variable.getShape() == null) {
                //putArrayForVarName(variable.getVarName(), Nd4j.scalar(1.0));
                //addAsPlaceHolder(variable.getVarName());
                continue;
            }


            val pair = parseVariable(variable.getVarName());
            reverseMap.put(pair.getFirst(), idCounter.incrementAndGet());
            log.debug("Adding [{}] as [{}]", pair.getFirst(), idCounter.get());

            val arr = variable.getArr();

            int name = bufferBuilder.createString(variable.getVarName());
            int array = arr.toFlatArray(bufferBuilder);
            int id = IntPair.createIntPair(bufferBuilder, idCounter.get(), 0);


            int flatVariable = FlatVariable.createFlatVariable(bufferBuilder, id, name, 0, array, -1);
            flatVariables.add(flatVariable);
        }

        //add functions
        for (val func : functionInstancesById.values()) {
            flatNodes.add(asFlatNode(func, bufferBuilder, variableList, reverseMap, forwardMap, framesMap, idCounter));
        }

        // we're dumping scopes now
        for (val scope : sameDiffFunctionInstances.entrySet()) {
            flatNodes.add(asFlatNode(scope.getKey(), scope.getValue(), bufferBuilder));
            val currVarList = new ArrayList<SDVariable>(scope.getValue().variables());
            // converting all ops from node
            for (val node : scope.getValue().variables()) {
                INDArray arr = node.getArr();
                if (arr == null) {
                    //val otherArr = Nd4j.scalar(1.0);
                    //scope.getValue().putArrayForVarName(node.getVarName(), otherArr);
                    //log.warn("Adding placeholder for export for var name {}", node.getVarName());
                    //arr = otherArr;
                    continue;
                }

                int name = bufferBuilder.createString(node.getVarName());
                int array = arr.toFlatArray(bufferBuilder);
                int id = IntPair.createIntPair(bufferBuilder, ++idx, 0);

                val pair = parseVariable(node.getVarName());
                reverseMap.put(pair.getFirst(), idx);

                log.debug("Adding [{}] as [{}]", pair.getFirst(), idx);

                int flatVariable = FlatVariable.createFlatVariable(bufferBuilder, id, name, 0, array, -1);
                flatVariables.add(flatVariable);
            }

            //add functions
            for (val func : scope.getValue().functionInstancesById.values()) {
                flatNodes.add(asFlatNode(func, bufferBuilder, currVarList, reverseMap, forwardMap, framesMap, idCounter));
            }

        }

        int outputsOffset = FlatGraph.createVariablesVector(bufferBuilder, Ints.toArray(flatOffsets));
        int variablesOffset = FlatGraph.createVariablesVector(bufferBuilder, Ints.toArray(flatVariables));
        int nodesOffset = FlatGraph.createNodesVector(bufferBuilder, Ints.toArray(flatNodes));

        int fg = FlatGraph.createFlatGraph(bufferBuilder, 119, variablesOffset, nodesOffset, outputsOffset, configuration.getFlatConfiguration(bufferBuilder));
        bufferBuilder.finish(fg);

        return bufferBuilder.dataBuffer();
    }
public ByteBuffer asFlatBuffers(@NonNull ExecutorConfiguration configuration) {
        Nd4j.getExecutioner().commit();
        FlatBufferBuilder bufferBuilder = new FlatBufferBuilder(1024);
        val idCounter = new AtomicInteger(0);

        val flatVariables = new ArrayList<Integer>();
        val flatOffsets = new ArrayList<Integer>();
        val flatNodes = new ArrayList<Integer>();

        // first of all we build VariableSpace dump
        List<SDVariable> variableList = new ArrayList<>(variables());
        val reverseMap = new LinkedHashMap<String, Integer>();
        val forwardMap = new LinkedHashMap<String, Integer>();
        val framesMap = new LinkedHashMap<String, Integer>();

        int idx = 0;
        for (val variable : variables()) {
            log.debug("Exporting variable: [{}]", variable.getVarName());
            if (variable.getArr() == null || variable.getShape() == null) {
                //putArrayForVarName(variable.getVarName(), Nd4j.scalar(1.0));
                //addAsPlaceHolder(variable.getVarName());
                continue;
            }


            val pair = parseVariable(variable.getVarName());
            reverseMap.put(pair.getFirst(), idCounter.incrementAndGet());
            log.debug("Adding [{}] as [{}]", pair.getFirst(), idCounter.get());

            val arr = variable.getArr();

            int name = bufferBuilder.createString(variable.getVarName());
            int array = arr.toFlatArray(bufferBuilder);
            int id = IntPair.createIntPair(bufferBuilder, idCounter.get(), 0);


            int flatVariable = FlatVariable.createFlatVariable(bufferBuilder, id, name, 0, array, -1);
            flatVariables.add(flatVariable);
        }

        //add functions
        for (val func : functionInstancesById.values()) {
            flatNodes.add(asFlatNode(func, bufferBuilder, variableList, reverseMap, forwardMap, framesMap, idCounter));
        }

        // we're dumping scopes now
        for (val scope : sameDiffFunctionInstances.entrySet()) {
            flatNodes.add(asFlatNode(scope.getKey(), scope.getValue(), bufferBuilder));
            val currVarList = new ArrayList<SDVariable>(scope.getValue().variables());
            // converting all ops from node
            for (val node : scope.getValue().variables()) {
                INDArray arr = node.getArr();
                if (arr == null) {
                    //val otherArr = Nd4j.scalar(1.0);
                    //scope.getValue().putArrayForVarName(node.getVarName(), otherArr);
                    //log.warn("Adding placeholder for export for var name {}", node.getVarName());
                    //arr = otherArr;
                    continue;
                }

                int name = bufferBuilder.createString(node.getVarName());
                int array = arr.toFlatArray(bufferBuilder);
                int id = IntPair.createIntPair(bufferBuilder, ++idx, 0);

                val pair = parseVariable(node.getVarName());
                reverseMap.put(pair.getFirst(), idx);

                log.debug("Adding [{}] as [{}]", pair.getFirst(), idx);

                int flatVariable = FlatVariable.createFlatVariable(bufferBuilder, id, name, 0, array, -1);
                flatVariables.add(flatVariable);
            }

            //add functions
            for (val func : scope.getValue().functionInstancesById.values()) {
                flatNodes.add(asFlatNode(func, bufferBuilder, currVarList, reverseMap, forwardMap, framesMap, idCounter));
            }

        }

        int outputsOffset = FlatGraph.createVariablesVector(bufferBuilder, Ints.toArray(flatOffsets));
        int variablesOffset = FlatGraph.createVariablesVector(bufferBuilder, Ints.toArray(flatVariables));
        int nodesOffset = FlatGraph.createNodesVector(bufferBuilder, Ints.toArray(flatNodes));

        int fg = FlatGraph.createFlatGraph(bufferBuilder, 119, variablesOffset, nodesOffset, outputsOffset, configuration.getFlatConfiguration(bufferBuilder));
        bufferBuilder.finish(fg);

        return bufferBuilder.dataBuffer();
    }
public ByteBuffer asFlatBuffers ( @ NonNull ExecutorConfiguration configuration ) { Nd4j . getExecutioner ( ) . commit ( ) ; FlatBufferBuilder bufferBuilder = new FlatBufferBuilder ( 1024 ) ; val idCounter = new AtomicInteger ( 0 ) ; val flatVariables = new ArrayList < Integer > ( ) ; val flatOffsets = new ArrayList < Integer > ( ) ; val flatNodes = new ArrayList < Integer > ( ) ; // first of all we build VariableSpace dump List < SDVariable > variableList = new ArrayList <> ( variables ( ) ) ; val reverseMap = new LinkedHashMap < String , Integer > ( ) ; val forwardMap = new LinkedHashMap < String , Integer > ( ) ; val framesMap = new LinkedHashMap < String , Integer > ( ) ; int idx = 0 ; for ( val variable : variables ( ) ) { log . debug ( "Exporting variable: [{}]" , variable . getVarName ( ) ) ; if ( variable . getArr ( ) == null || variable . getShape ( ) == null ) { //putArrayForVarName(variable.getVarName(), Nd4j.scalar(1.0)); //addAsPlaceHolder(variable.getVarName()); continue ; } val pair = parseVariable ( variable . getVarName ( ) ) ; reverseMap . put ( pair . getFirst ( ) , idCounter . incrementAndGet ( ) ) ; log . debug ( "Adding [{}] as [{}]" , pair . getFirst ( ) , idCounter . get ( ) ) ; val arr = variable . getArr ( ) ; int name = bufferBuilder . createString ( variable . getVarName ( ) ) ; int array = arr . toFlatArray ( bufferBuilder ) ; int id = IntPair . createIntPair ( bufferBuilder , idCounter . get ( ) , 0 ) ; int flatVariable = FlatVariable . createFlatVariable ( bufferBuilder , id , name , 0 , array , - 1 ) ; flatVariables . add ( flatVariable ) ; } //add functions for ( val func : functionInstancesById . values ( ) ) { flatNodes . add ( asFlatNode ( func , bufferBuilder , variableList , reverseMap , forwardMap , framesMap , idCounter ) ) ; } // we're dumping scopes now for ( val scope : sameDiffFunctionInstances . entrySet ( ) ) { flatNodes . add ( asFlatNode ( scope . getKey ( ) , scope . getValue ( ) , bufferBuilder ) ) ; val currVarList = new ArrayList < SDVariable > ( scope . getValue ( ) . variables ( ) ) ; // converting all ops from node for ( val node : scope . getValue ( ) . variables ( ) ) { INDArray arr = node . getArr ( ) ; if ( arr == null ) { //val otherArr = Nd4j.scalar(1.0); //scope.getValue().putArrayForVarName(node.getVarName(), otherArr); //log.warn("Adding placeholder for export for var name {}", node.getVarName()); //arr = otherArr; continue ; } int name = bufferBuilder . createString ( node . getVarName ( ) ) ; int array = arr . toFlatArray ( bufferBuilder ) ; int id = IntPair . createIntPair ( bufferBuilder , ++ idx , 0 ) ; val pair = parseVariable ( node . getVarName ( ) ) ; reverseMap . put ( pair . getFirst ( ) , idx ) ; log . debug ( "Adding [{}] as [{}]" , pair . getFirst ( ) , idx ) ; int flatVariable = FlatVariable . createFlatVariable ( bufferBuilder , id , name , 0 , array , - 1 ) ; flatVariables . add ( flatVariable ) ; } //add functions for ( val func : scope . getValue ( ) . functionInstancesById . values ( ) ) { flatNodes . add ( asFlatNode ( func , bufferBuilder , currVarList , reverseMap , forwardMap , framesMap , idCounter ) ) ; } } int outputsOffset = FlatGraph . createVariablesVector ( bufferBuilder , Ints . toArray ( flatOffsets ) ) ; int variablesOffset = FlatGraph . createVariablesVector ( bufferBuilder , Ints . toArray ( flatVariables ) ) ; int nodesOffset = FlatGraph . createNodesVector ( bufferBuilder , Ints . toArray ( flatNodes ) ) ; int fg = FlatGraph . createFlatGraph ( bufferBuilder , 119 , variablesOffset , nodesOffset , outputsOffset , configuration . getFlatConfiguration ( bufferBuilder ) ) ; bufferBuilder . finish ( fg ) ; return bufferBuilder . dataBuffer ( ) ; }
This method exports given SameDiff instance into FlatBuffers

@param configuration - ExecutorConfiguration to be embedded into serialized graph
@return
This method exports given SameDiff instance into FlatBuffers