<commit_before>package org.deeplearning4j.optimize.listeners;

import org.deeplearning4j.nn.api.Model;
import org.deeplearning4j.optimize.api.IterationListener;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.api.shape.Shape;
import org.nd4j.linalg.util.ArrayUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicLong;

/**
 * Simple IterationListener that tracks time spend on training per iteration.
 *
 * @author <EMAIL>
 */
public class PerformanceListener implements IterationListener {
    private final int frequency;
    private static final Logger logger = LoggerFactory.getLogger(PerformanceListener.class);
    private double samplesPerSec = 0.0f;
    private double batchesPerSec = 0.0f;
    private long lastTime;
    private AtomicLong iterationCount = new AtomicLong(0);

    private boolean reportScore;
    private boolean reportSample = true;
    private boolean reportBatch = true;
    private boolean reportIteration = true;
    private boolean reportTime = true;



    public PerformanceListener(int frequency) {
        this(frequency, false);
    }

    public PerformanceListener(int frequency, boolean reportScore) {
        this.frequency = frequency;
        this.reportScore = reportScore;
        this.lastTime = System.currentTimeMillis();
    }

    @Override
    public boolean invoked() {
        return false;
    }

    @Override
    public void invoke() {

    }

    @Override
    public void iterationDone(Model model, int iteration) {
        // we update lastTime on every iteration
        // just to simplify things


        if (iterationCount.getAndIncrement() % frequency == 0) {
            long currentTime = System.currentTimeMillis();

            long timeSpent = currentTime - lastTime ;
            float timeSec = timeSpent / 1000f;

            INDArray input = model.input();
            long tadLength = Shape.getTADLength(input.shape(), ArrayUtil.range(1,input.rank()));

            long numSamples = input.lengthLong() / tadLength;

            samplesPerSec = numSamples / timeSec;
            batchesPerSec = 1 / timeSec;

            StringBuilder builder = new StringBuilder();

            if (reportIteration)
                builder.append("iteration ").append(iterationCount.get()).append("; ");<commit_msg>perf listener fix for cg + multidataset<commit_after>package org.deeplearning4j.optimize.listeners;

import org.deeplearning4j.nn.api.Model;
import org.deeplearning4j.nn.graph.ComputationGraph;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.optimize.api.IterationListener;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.api.shape.Shape;
import org.nd4j.linalg.util.ArrayUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicLong;

/**
 * Simple IterationListener that tracks time spend on training per iteration.
 *
 * @author <EMAIL>
 */
public class PerformanceListener implements IterationListener {
    private final int frequency;
    private static final Logger logger = LoggerFactory.getLogger(PerformanceListener.class);
    private double samplesPerSec = 0.0f;
    private double batchesPerSec = 0.0f;
    private long lastTime;
    private AtomicLong iterationCount = new AtomicLong(0);

    private boolean reportScore;
    private boolean reportSample = true;
    private boolean reportBatch = true;
    private boolean reportIteration = true;
    private boolean reportTime = true;



    public PerformanceListener(int frequency) {
        this(frequency, false);
    }

    public PerformanceListener(int frequency, boolean reportScore) {
        this.frequency = frequency;
        this.reportScore = reportScore;
        this.lastTime = System.currentTimeMillis();
    }

    @Override
    public boolean invoked() {
        return false;
    }

    @Override
    public void invoke() {

    }

    @Override
    public void iterationDone(Model model, int iteration) {
        // we update lastTime on every iteration
        // just to simplify things


        if (iterationCount.getAndIncrement() % frequency == 0) {
            long currentTime = System.currentTimeMillis();

            long timeSpent = currentTime - lastTime ;
            float timeSec = timeSpent / 1000f;

            INDArray input;
            if (model instanceof ComputationGraph) {
                // for comp graph (with multidataset
                ComputationGraph cg = (ComputationGraph) model;
                INDArray[] inputs = cg.getInputs();

                if (inputs != null && inputs.length > 0)
                    input = inputs[0];
                else
                    input = model.input();
            } else {
                input = model.input();
            }

            long tadLength = Shape.getTADLength(input.shape(), ArrayUtil.range(1, input.rank()));

            long numSamples = input.lengthLong() / tadLength;

            samplesPerSec = numSamples / timeSec;
            batchesPerSec = 1 / timeSec;


            StringBuilder builder = new StringBuilder();

            if (reportIteration)
                builder.append("iteration ").append(iterationCount.get()).append("; ");
{'fraction_non_alphanumeric': 0.08778915434205536, 'fraction_numerical': 0.008342813803564657, 'mean_word_length': 2.714788732394366, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4945289', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1377, 'n_words': 364}