<commit_before>#include <assert.h>
#include <cmath>
#include "test_utils.h"
#include "test_cnn.h"
#include "atom_constants.h"
#include "cnn_scorer.h"
#include <cuda_runtime.h>
#include "quaternion.h"
#include "caffe/proto/caffe.pb.h"
#include "caffe/layers/flex_lstm_layer.hpp"
#include "caffe/util/rng.hpp"
#include "caffe/util/device_alternate.hpp"
#include <boost/multi_array/multi_array_ref.hpp>
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include <boost/test/floating_point_comparison.hpp>
#define TOL 0.0001f

extern parsed_args p_args;

using namespace caffe;
using namespace std;

void test_set_atom_gradients() {
  Caffe::set_mode(Caffe::GPU);
  // randomly generate gridpoint gradients, accumulate for atoms, and then compare
  p_args.log << "CNN Set Atom Gradients Test \n";
  p_args.log << "Using random seed: " << p_args.seed << '\n';
  p_args.log << "Iteration " << p_args.iter_count << '\n';
  std::mt19937 engine(p_args.seed);
  std::normal_distribution<> diff_dist(0, 5);
  auto gen = std::bind(diff_dist, engine);

  std::vector<atom_params> mol_atoms;
  std::vector<smt> mol_types;
  make_mol(mol_atoms, mol_types, engine, 0, 1, 5000, 11.5, 11.5, 11.5);
  cnn_options cnnopts;
  cnnopts.cnn_scoring = CNNall;
  model m;
  CNNScorer cnn_scorer(cnnopts);
  typedef CNNScorer::Dtype Dtype;
  MolGridDataLayer<Dtype>* mgrid = cnn_scorer.get_mgrid();
  assert(mgrid);
  int ntypes = mgrid->getNumChannels();
  int dim = mgrid->getGridDims().x;
  mgrid->enableLigandGradients();

  set_cnn_grids(mgrid, mol_atoms, mol_types);

  vector<gfloat3> cpugrad, gpugrad;
  //randomly intialize gridpoint gradients
  Blob<Dtype> diff({1,ntypes,dim,dim,dim});
  Dtype* diffcpu = diff.mutable_cpu_diff();
  generate(diffcpu, diffcpu+diff.count(), gen);

  vector<Blob<Dtype>*> top{&diff};
  vector<Blob<Dtype>*> bottom; //not used

  //set up and calculate CPU atom gradients
  mgrid->backward(top, bottom, false);
  //store
  mgrid->getLigandGradient(0, cpugrad);

  //calculate GPU atom gradients
  mgrid->backward(top, bottom, true);
  mgrid->getLigandGradient(0, gpugrad);

  p_args.log << "NUMATOMS " << mol_atoms.size() << "\n";
  //compare results
  double sum = 0;
  for (size_t i = 0; i < mol_atoms.size(); ++i) {
    for (size_t j = 0; j < 3; ++j) {
      p_args.log << i <<"," << j << "  CPU " << cpugrad[i][j] << " GPU " << gpugrad[i][j] << "  " << mol_types[i] << " " << mol_atoms[i].coords << "\n";
      BOOST_REQUIRE_SMALL(cpugrad[i][j] - gpugrad[i][j], (float )0.01);
      sum += cpugrad[i].x+cpugrad[i].y+cpugrad[i].z;
    }
  }

  BOOST_REQUIRE_NE(sum, 0); //make sure something was actually calculated
}

void test_vanilla_grids() {
  //randomly generate mol, check that CPU and GPU grids match
  p_args.log << "CNN Vanilla Grids Test \n";
  p_args.log << "Using random seed: " << p_args.seed << '\n';
  p_args.log << "Iteration " << p_args.iter_count << '\n';
  std::mt19937 engine(p_args.seed);
  Caffe::set_mode(Caffe::GPU);

  //honestly just trying to replicate libmolgrid test to check whether it fails
  //here too, so being less stringent than I was for the other tests in terms
  //of randomization
  std::vector<atom_params> mol_atoms;
  std::vector<smt> mol_types;
  make_mol(mol_atoms, mol_types, engine);

  cnn_options cnnopts;
  cnnopts.cnn_scoring = CNNall;
  model m;
  CNNScorer cnn_scorer(cnnopts);
  typedef CNNScorer::Dtype Dtype;

  MolGridDataLayer<Dtype>* mgrid = cnn_scorer.get_mgrid();
  assert(mgrid);
  int ntypes = mgrid->getNumChannels();
  int dim = mgrid->getGridDims().x;

  set_cnn_grids(mgrid, mol_atoms, mol_types);

  vector<Blob<Dtype> > topblobs(mgrid->ExactNumTopBlobs());
  topblobs[0].Reshape({1,ntypes,dim,dim,dim});

  vector<Blob<Dtype>*> bottom;
  vector<Blob<Dtype>*> top;
<commit_msg>Use smaller model in tests<commit_after>#include <assert.h>
#include <cmath>
#include "test_utils.h"
#include "test_cnn.h"
#include "atom_constants.h"
#include "cnn_scorer.h"
#include <cuda_runtime.h>
#include "quaternion.h"
#include "caffe/proto/caffe.pb.h"
#include "caffe/layers/flex_lstm_layer.hpp"
#include "caffe/util/rng.hpp"
#include "caffe/util/device_alternate.hpp"
#include <boost/multi_array/multi_array_ref.hpp>
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include <boost/test/floating_point_comparison.hpp>
#define TOL 0.0001f

extern parsed_args p_args;

using namespace caffe;
using namespace std;

void test_set_atom_gradients() {
  // randomly generate gridpoint gradients, accumulate for atoms, and then compare
  p_args.log << "CNN Set Atom Gradients Test \n";
  p_args.log << "Using random seed: " << p_args.seed << '\n';
  p_args.log << "Iteration " << p_args.iter_count << '\n';

  Caffe::set_mode(Caffe::GPU);

  std::mt19937 engine(p_args.seed);
  std::normal_distribution<> diff_dist(0, 5);
  auto gen = std::bind(diff_dist, engine);

  std::vector<atom_params> mol_atoms;
  std::vector<smt> mol_types;
  make_mol(mol_atoms, mol_types, engine, 0, 1, 5000, 11.5, 11.5, 11.5);
  cnn_options cnnopts;
  cnnopts.cnn_scoring = CNNall;
  cnnopts.cnn_model_names.push_back("crossdock_default2018");

  model m;
  CNNScorer cnn_scorer(cnnopts);
  typedef CNNScorer::Dtype Dtype;
  MolGridDataLayer<Dtype>* mgrid = cnn_scorer.get_mgrid();
  assert(mgrid);
  int ntypes = mgrid->getNumChannels();
  int dim = mgrid->getGridDims().x;
  mgrid->enableLigandGradients();

  set_cnn_grids(mgrid, mol_atoms, mol_types);

  vector<gfloat3> cpugrad, gpugrad;
  //randomly intialize gridpoint gradients
  Blob<Dtype> diff({1,ntypes,dim,dim,dim});
  Dtype* diffcpu = diff.mutable_cpu_diff();
  generate(diffcpu, diffcpu+diff.count(), gen);

  vector<Blob<Dtype>*> top{&diff};
  vector<Blob<Dtype>*> bottom; //not used

  //set up and calculate CPU atom gradients
  mgrid->backward(top, bottom, false);
  //store
  mgrid->getLigandGradient(0, cpugrad);

  //calculate GPU atom gradients
  mgrid->backward(top, bottom, true);
  mgrid->getLigandGradient(0, gpugrad);

  p_args.log << "NUMATOMS " << mol_atoms.size() << "\n";
  //compare results
  double sum = 0;
  for (size_t i = 0; i < mol_atoms.size(); ++i) {
    for (size_t j = 0; j < 3; ++j) {
      p_args.log << i <<"," << j << "  CPU " << cpugrad[i][j] << " GPU " << gpugrad[i][j] << "  " << mol_types[i] << " " << mol_atoms[i].coords << "\n";
      BOOST_REQUIRE_SMALL(cpugrad[i][j] - gpugrad[i][j], (float )0.01);
      sum += cpugrad[i].x+cpugrad[i].y+cpugrad[i].z;
    }
  }

  BOOST_REQUIRE_NE(sum, 0); //make sure something was actually calculated
}

void test_vanilla_grids() {
  //randomly generate mol, check that CPU and GPU grids match
  p_args.log << "CNN Vanilla Grids Test \n";
  p_args.log << "Using random seed: " << p_args.seed << '\n';
  p_args.log << "Iteration " << p_args.iter_count << '\n';
  std::mt19937 engine(p_args.seed);
  Caffe::set_mode(Caffe::GPU);

  //honestly just trying to replicate libmolgrid test to check whether it fails
  //here too, so being less stringent than I was for the other tests in terms
  //of randomization
  std::vector<atom_params> mol_atoms;
  std::vector<smt> mol_types;
  make_mol(mol_atoms, mol_types, engine);

  cnn_options cnnopts;
  cnnopts.cnn_scoring = CNNall;
  cnnopts.cnn_model_names.push_back("crossdock_default2018");

  model m;
  CNNScorer cnn_scorer(cnnopts);
  typedef CNNScorer::Dtype Dtype;

  MolGridDataLayer<Dtype>* mgrid = cnn_scorer.get_mgrid();
  assert(mgrid);
  int ntypes = mgrid->getNumChannels();
  int dim = mgrid->getGridDims().x;

  set_cnn_grids(mgrid, mol_atoms, mol_types);

  vector<Blob<Dtype> > topblobs(mgrid->ExactNumTopBlobs());
  topblobs[0].Reshape({1,ntypes,dim,dim,dim});

  vector<Blob<Dtype>*> bottom;
  vector<Blob<Dtype>*> top;

{'fraction_non_alphanumeric': 0.15494605485506305, 'fraction_numerical': 0.012998830105290524, 'mean_word_length': 4.703484062268347, 'pattern_counts': {'":': 0, '<': 173, '<?xml version=': 0, '>': 73, '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': '6924895', 'n_tokens_mistral': 3137, 'n_tokens_neox': 2974, 'n_words': 624}