<commit_before>package edu.jhu.pacaya.autodiff;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.Assert;

import edu.jhu.hlt.optimize.function.DifferentiableFunction;
import edu.jhu.hlt.optimize.function.ValueGradient;
import edu.jhu.pacaya.util.dist.Gaussian;
import edu.jhu.prim.Primitives;
import edu.jhu.prim.vector.IntDoubleDenseVector;
import edu.jhu.prim.vector.IntDoubleVector;

public class ModuleTestUtils {

    private ModuleTestUtils() { }
        
    /**
     * Used to test modules by treating them as a differentiable function.
     * @author mgormley
     */
    public static class ModuleFn implements DifferentiableFunction {

        private Module<?> vecFn;
        int outIdx;
        
        public ModuleFn(Module<?> vecFn, int outIdx) {
            this.vecFn = vecFn;
            this.outIdx = outIdx;
        }
    
        @Override
        public int getNumDimensions() {
            return getOutputSize(vecFn.getInputs());
        }
        
        @Override
        public double getValue(IntDoubleVector point) {
            setInputsFromIdv(vecFn.getInputs(), point);
            MVec out = vecFn.forward();
            return out.getValue(outIdx);
        }
    
        @Override
        public IntDoubleVector getGradient(IntDoubleVector point) {
            setInputsFromIdv(vecFn.getInputs(), point);
            vecFn.forward();
            vecFn.zeroOutputAdj();
            for (Module<?> x : vecFn.getInputs()) {
                x.zeroOutputAdj();
            }
            MVec outAdj = vecFn.getOutputAdj();
            outAdj.fill(0.0);
            outAdj.setValue(outIdx, 1.0);
            vecFn.backward();
            return getInputsAsIdv(vecFn.getInputs());
        }
    
        @Override
        public ValueGradient getValueGradient(IntDoubleVector point) {
            return new ValueGradient(getValue(point), getGradient(point));
        }
        
        /**
         * Treats all the input modules as if they were concatenated into a long vector, and
         * computes the size of that vector.
         */
        public static int getOutputSize(List<? extends Module<? extends MVec>> mods) {
            int totInDimension = 0;
            for (Module<?> mod : mods) {<commit_msg>Bug fix: fresh type compile error<commit_after>package edu.jhu.pacaya.autodiff;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.Assert;

import edu.jhu.hlt.optimize.function.DifferentiableFunction;
import edu.jhu.hlt.optimize.function.ValueGradient;
import edu.jhu.pacaya.util.dist.Gaussian;
import edu.jhu.prim.Primitives;
import edu.jhu.prim.vector.IntDoubleDenseVector;
import edu.jhu.prim.vector.IntDoubleVector;

public class ModuleTestUtils {

    private ModuleTestUtils() { }
        
    /**
     * Used to test modules by treating them as a differentiable function.
     * @author mgormley
     */
    public static class ModuleFn implements DifferentiableFunction {

        private Module<?> vecFn;
        int outIdx;
        
        public ModuleFn(Module<?> vecFn, int outIdx) {
            this.vecFn = vecFn;
            this.outIdx = outIdx;
        }
    
        @Override
        public int getNumDimensions() {
            return getOutputSize(vecFn.getInputs());
        }
        
        @Override
        public double getValue(IntDoubleVector point) {
            setInputsFromIdv(vecFn.getInputs(), point);
            MVec out = vecFn.forward();
            return out.getValue(outIdx);
        }
    
        @Override
        public IntDoubleVector getGradient(IntDoubleVector point) {
            setInputsFromIdv(vecFn.getInputs(), point);
            vecFn.forward();
            vecFn.zeroOutputAdj();
            for (Module<?> x : vecFn.getInputs()) {
                x.zeroOutputAdj();
            }
            MVec outAdj = vecFn.getOutputAdj();
            outAdj.fill(0.0);
            outAdj.setValue(outIdx, 1.0);
            vecFn.backward();
            return getInputsAsIdv(vecFn.getInputs());
        }
    
        @Override
        public ValueGradient getValueGradient(IntDoubleVector point) {
            return new ValueGradient(getValue(point), getGradient(point));
        }
        
        /**
         * Treats all the input modules as if they were concatenated into a long vector, and
         * computes the size of that vector.
         */
        public static int getOutputSize(List<? extends Module<?>> mods) {
            int totInDimension = 0;
            for (Module<?> mod : mods) {
{'fraction_non_alphanumeric': 0.09456213146975322, 'fraction_numerical': 0.0021838829438742082, 'mean_word_length': 2.0945945945945947, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '5901126', 'n_tokens_mistral': 1311, 'n_tokens_neox': 1299, 'n_words': 295}