def test_nnet_compute(self):
    gen_config = NnetGenerationOptions()
    test_collapse_model = random.choice([True, False])
    configs = generate_config_sequence(gen_config)
    nnet = Nnet()
    for (j, config) in enumerate(configs):
        istrm = istringstream.from_str(config)
        nnet.read_config(istrm)
    request = ComputationRequest()
    inputs = compute_example_computation_request_simple(nnet, request)
    if test_collapse_model:
        set_batchnorm_test_mode(True, nnet)
        set_dropout_test_mode(True, nnet)
    compiler = Compiler(request, nnet)
    opts = CompilerOptions()
    computation = compiler.create_computation(opts)
    nnet_collapsed = Nnet.from_other(nnet)
    if test_collapse_model:
        collapse_config = CollapseModelConfig()
        collapse_model(collapse_config, nnet_collapsed)
        compiler_collapsed = Compiler(request, nnet_collapsed)
        computation_collapsed = compiler_collapsed.create_computation(opts)
        computation_collapsed.compute_cuda_indexes()
    ostrm = ostringstream()
    computation.print_computation(ostrm, nnet)
    check_config = CheckComputationOptions()
    check_config.check_rewrite = True
    checker = ComputationChecker(check_config, nnet, computation)
    checker.check()
    if random.choice([True, False]):
        opt_config = NnetOptimizeOptions()
        optimize(opt_config, nnet, max_output_time_in_request(request), computation)
        ostrm = ostringstream()
        computation.print_computation(ostrm, nnet)
    compute_opts = NnetComputeOptions()
    compute_opts.debug = random.choice([True, False])
    computation.compute_cuda_indexes()
    computer = NnetComputer(compute_opts, computation, nnet, nnet)
    for (i, ispec) in enumerate(request.inputs):
        temp = CuMatrix.from_matrix(inputs[i])
        print('Input sum:', temp.sum())
        computer.accept_input(ispec.name, temp)
    computer.run()
    output = computer.get_output_destructive('output')
    print('Output sum:', output.sum())
    if test_collapse_model:
        computer_collapsed = NnetComputer(compute_opts, computation_collapsed, nnet_collapsed, nnet_collapsed)
        for (i, ispec) in enumerate(request.inputs):
            temp = CuMatrix.from_matrix(inputs[i])
            computer_collapsed.accept_input(ispec.name, temp)
        computer_collapsed.run()
        output_collapsed = computer_collapsed.get_output_destructive('output')
        print('Output sum [collapsed]:', output_collapsed.sum())
        self.assertTrue(approx_equal_cu_matrix(output, output_collapsed), 'Regular and collapsed computation outputs differ.')
    output_deriv = CuMatrix.from_size(output.num_rows(), output.num_cols())
    output_deriv.set_randn()
    if request.outputs[0].has_deriv:
        computer.accept_input('output', output_deriv)
        computer.run()
        for (i, ispec) in enumerate(request.inputs):
            if ispec.has_deriv:
                in_deriv = computer.get_output_destructive(ispec.name)
                print('Input-deriv sum for input {} is:'.format(ispec.name), in_deriv.sum())
def test_nnet_compute(self):
    gen_config = NnetGenerationOptions()
    test_collapse_model = random.choice([True, False])
    configs = generate_config_sequence(gen_config)
    nnet = Nnet()
    for (j, config) in enumerate(configs):
        istrm = istringstream.from_str(config)
        nnet.read_config(istrm)
    request = ComputationRequest()
    inputs = compute_example_computation_request_simple(nnet, request)
    if test_collapse_model:
        set_batchnorm_test_mode(True, nnet)
        set_dropout_test_mode(True, nnet)
    compiler = Compiler(request, nnet)
    opts = CompilerOptions()
    computation = compiler.create_computation(opts)
    nnet_collapsed = Nnet.from_other(nnet)
    if test_collapse_model:
        collapse_config = CollapseModelConfig()
        collapse_model(collapse_config, nnet_collapsed)
        compiler_collapsed = Compiler(request, nnet_collapsed)
        computation_collapsed = compiler_collapsed.create_computation(opts)
        computation_collapsed.compute_cuda_indexes()
    ostrm = ostringstream()
    computation.print_computation(ostrm, nnet)
    check_config = CheckComputationOptions()
    check_config.check_rewrite = True
    checker = ComputationChecker(check_config, nnet, computation)
    checker.check()
    if random.choice([True, False]):
        opt_config = NnetOptimizeOptions()
        optimize(opt_config, nnet, max_output_time_in_request(request), computation)
        ostrm = ostringstream()
        computation.print_computation(ostrm, nnet)
    compute_opts = NnetComputeOptions()
    compute_opts.debug = random.choice([True, False])
    computation.compute_cuda_indexes()
    computer = NnetComputer(compute_opts, computation, nnet, nnet)
    for (i, ispec) in enumerate(request.inputs):
        temp = CuMatrix.from_matrix(inputs[i])
        print('Input sum:', temp.sum())
        computer.accept_input(ispec.name, temp)
    computer.run()
    output = computer.get_output_destructive('output')
    print('Output sum:', output.sum())
    if test_collapse_model:
        computer_collapsed = NnetComputer(compute_opts, computation_collapsed, nnet_collapsed, nnet_collapsed)
        for (i, ispec) in enumerate(request.inputs):
            temp = CuMatrix.from_matrix(inputs[i])
            computer_collapsed.accept_input(ispec.name, temp)
        computer_collapsed.run()
        output_collapsed = computer_collapsed.get_output_destructive('output')
        print('Output sum [collapsed]:', output_collapsed.sum())
        self.assertTrue(approx_equal_cu_matrix(output, output_collapsed), 'Regular and collapsed computation outputs differ.')
    output_deriv = CuMatrix.from_size(output.num_rows(), output.num_cols())
    output_deriv.set_randn()
    if request.outputs[0].has_deriv:
        computer.accept_input('output', output_deriv)
        computer.run()
        for (i, ispec) in enumerate(request.inputs):
            if ispec.has_deriv:
                in_deriv = computer.get_output_destructive(ispec.name)
                print('Input-deriv sum for input {} is:'.format(ispec.name), in_deriv.sum())