Skip to content

Commit

Permalink
Merge pull request #29 from probcomp/052924-srvasude-github-actions
Browse files Browse the repository at this point in the history
Add Github Actions for Format + Testing
  • Loading branch information
srvasude authored Jun 4, 2024
2 parents 9e4f222 + 0dc1a85 commit b4e3c7b
Show file tree
Hide file tree
Showing 21 changed files with 515 additions and 534 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This workflow will run tests.

name: Tests
on:
push:
branches: [ main ]
pull_request:
jobs:
run_tests:
name: Run tests
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Checking out repository
uses: actions/checkout@v4
- name: Install dependencies
run : |
sudo apt-get update && sudo apt-get install -yq clang clang-format
- name: Build everything
run: |
cd cxx && bazel build ...
- name: Test C++
run: |
cd cxx && bazel test --test_output=errors ...
30 changes: 0 additions & 30 deletions .github/workflows/python-package.yml

This file was deleted.

2 changes: 1 addition & 1 deletion check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ root=$(cd -- "$(dirname -- "$0")" && pwd)
if [ $# -eq 0 ]; then
# (Default) Run tests/
./pythenv.sh "$PYTHON" -m pytest --pyargs hirm
cd cxx && make tests
cd cxx && bazel test :all
elif [ ${1} = 'coverage' ]; then
# Generate coverage report.
./pythenv.sh coverage run --source=build/ -m pytest --pyargs hirm
Expand Down
8 changes: 8 additions & 0 deletions cxx/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Language: Cpp
BasedOnStyle: Google
DerivePointerAlignment: false
PointerAlignment: Left
BreakConstructorInitializers: BeforeColon
ConstructorInitializerIndentWidth: 4
PackConstructorInitializers: NextLine
SpaceBeforeCtorInitializerColon: true
14 changes: 6 additions & 8 deletions cxx/distributions/adapter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ class DistributionAdapter : Distribution<std::string> {

double logp_score() const { return d->logp_score(); }

std::string sample() {
SampleType s = d->sample();
return to_string(s);
}

void transition_hyperparameters() {
d->transition_hyperparameters();
}
std::string sample() {
SampleType s = d->sample();
return to_string(s);
}

void transition_hyperparameters() { d->transition_hyperparameters(); }

~DistributionAdapter() { delete d; }
};
118 changes: 59 additions & 59 deletions cxx/distributions/beta_bernoulli.hh
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,68 @@
#include "util_math.hh"

class BetaBernoulli : public Distribution<double> {
public:
double alpha = 1; // hyperparameter
double beta = 1; // hyperparameter
int s = 0; // sum of observed values
std::mt19937* prng;
public:
double alpha = 1; // hyperparameter
double beta = 1; // hyperparameter
int s = 0; // sum of observed values
std::mt19937* prng;

std::vector<double> alpha_grid;
std::vector<double> beta_grid;
std::vector<double> alpha_grid;
std::vector<double> beta_grid;

// BetaBernoulli does not take ownership of prng.
BetaBernoulli(std::mt19937 *prng) {
this->prng = prng;
alpha_grid = log_linspace(1e-4, 1e4, 10, true);
beta_grid = log_linspace(1e-4, 1e4, 10, true);
}
void incorporate(const double& x){
assert(x == 0 || x == 1);
++N;
s += x;
}
void unincorporate(const double& x) {
assert(x == 0 || x ==1);
--N;
s -= x;
assert(0 <= s);
assert(0 <= N);
}
double logp(const double& x) const {
assert(x == 0 || x == 1);
double log_denom = log(N + alpha + beta);
double log_numer = x ? log(s + alpha) : log(N - s + beta);
return log_numer - log_denom;
}
double logp_score() const {
double v1 = lbeta(s + alpha, N - s + beta);
double v2 = lbeta(alpha, beta);
return v1 - v2;
}
double sample() {
double p = exp(logp(1));
std::vector<int> items {0, 1};
std::vector<double> weights {1-p, p};
int idx = choice(weights, prng);
return items[idx];
}
void transition_hyperparameters() {
std::vector<double> logps;
std::vector<std::pair<double, double>> hypers;
// C++ doesn't yet allow range for-loops over existing variables. Sigh.
for (double alphat : alpha_grid) {
for (double betat : beta_grid) {
alpha = alphat;
beta = betat;
double lp = logp_score();
if (!std::isnan(lp)) {
logps.push_back(logp_score());
hypers.push_back(std::make_pair(alpha, beta));
}
// BetaBernoulli does not take ownership of prng.
BetaBernoulli(std::mt19937* prng) {
this->prng = prng;
alpha_grid = log_linspace(1e-4, 1e4, 10, true);
beta_grid = log_linspace(1e-4, 1e4, 10, true);
}
void incorporate(const double& x) {
assert(x == 0 || x == 1);
++N;
s += x;
}
void unincorporate(const double& x) {
assert(x == 0 || x == 1);
--N;
s -= x;
assert(0 <= s);
assert(0 <= N);
}
double logp(const double& x) const {
assert(x == 0 || x == 1);
double log_denom = log(N + alpha + beta);
double log_numer = x ? log(s + alpha) : log(N - s + beta);
return log_numer - log_denom;
}
double logp_score() const {
double v1 = lbeta(s + alpha, N - s + beta);
double v2 = lbeta(alpha, beta);
return v1 - v2;
}
double sample() {
double p = exp(logp(1));
std::vector<int> items{0, 1};
std::vector<double> weights{1 - p, p};
int idx = choice(weights, prng);
return items[idx];
}
void transition_hyperparameters() {
std::vector<double> logps;
std::vector<std::pair<double, double>> hypers;
// C++ doesn't yet allow range for-loops over existing variables. Sigh.
for (double alphat : alpha_grid) {
for (double betat : beta_grid) {
alpha = alphat;
beta = betat;
double lp = logp_score();
if (!std::isnan(lp)) {
logps.push_back(logp_score());
hypers.push_back(std::make_pair(alpha, beta));
}
}
int i = sample_from_logps(logps, prng);
alpha = hypers[i].first;
beta = hypers[i].second;
}
int i = sample_from_logps(logps, prng);
alpha = hypers[i].first;
beta = hypers[i].second;
}
};
24 changes: 12 additions & 12 deletions cxx/distributions/bigram.hh
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,18 @@ class Bigram : public Distribution<std::string> {
}

void transition_hyperparameters() {
std::vector<double> logps;
std::vector<double> alphas;
// C++ doesn't yet allow range for-loops over existing variables. Sigh.
for (double alphat : ALPHA_GRID) {
set_alpha(alphat);
double lp = logp_score();
if (!std::isnan(lp)) {
logps.push_back(logp_score());
alphas.push_back(alphat);
}
std::vector<double> logps;
std::vector<double> alphas;
// C++ doesn't yet allow range for-loops over existing variables. Sigh.
for (double alphat : ALPHA_GRID) {
set_alpha(alphat);
double lp = logp_score();
if (!std::isnan(lp)) {
logps.push_back(logp_score());
alphas.push_back(alphat);
}
int i = sample_from_logps(logps, prng);
set_alpha(alphas[i]);
}
int i = sample_from_logps(logps, prng);
set_alpha(alphas[i]);
}
};
129 changes: 62 additions & 67 deletions cxx/distributions/dirichlet_categorical.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,79 +5,74 @@
#include <algorithm>
#include <cassert>
#include <random>

#include "base.hh"
#include "util_math.hh"

#define ALPHA_GRID {1e-4, 1e-3, 1e-2, 1e-1, 1.0, 10.0, 100.0, 1000.0, 10000.0}
#define ALPHA_GRID \
{ 1e-4, 1e-3, 1e-2, 1e-1, 1.0, 10.0, 100.0, 1000.0, 10000.0 }

class DirichletCategorical : public Distribution<double> {
public:
double alpha = 1; // hyperparameter (applies to all categories)
std::vector<int> counts; // counts of observed categories
int n; // Total number of observations.
std::mt19937* prng;
public:
double alpha = 1; // hyperparameter (applies to all categories)
std::vector<int> counts; // counts of observed categories
int n; // Total number of observations.
std::mt19937* prng;

// DirichletCategorical does not take ownership of prng.
DirichletCategorical(std::mt19937 *prng, int k) { // k is number of categories
this->prng = prng;
counts = std::vector<int>(k, 0);
n = 0;
}
void incorporate(const double& x) {
assert(x >= 0 && x < counts.size());
counts[size_t(x)] += 1;
++n;
}
void unincorporate(const double& x) {
const size_t y = x;
assert(y < counts.size());
counts[y] -= 1;
--n;
assert(0 <= counts[y]);
assert(0 <= n);
}
double logp(const double& x) const {
assert(x >= 0 && x < counts.size());
const double numer = log(alpha + counts[size_t(x)]);
const double denom = log(n + alpha * counts.size());
return numer - denom;
}
double logp_score() const {
const size_t k = counts.size();
const double a = alpha * k;
const double lg = std::transform_reduce(
counts.cbegin(),
counts.cend(),
0,
std::plus{},
[&](size_t y) -> double {return lgamma(y + alpha); }
);
return lgamma(a) - lgamma(a + n) + lg - k * lgamma(alpha);
}
double sample() {
std::vector<double> weights(counts.size());
std::transform(
counts.begin(),
counts.end(),
weights.begin(),
[&](size_t y) -> double { return y + alpha; }
);
int idx = choice(weights, prng);
return double(idx);
}
void transition_hyperparameters() {
std::vector<double> logps;
std::vector<double> alphas;
// C++ doesn't yet allow range for-loops over existing variables. Sigh.
for (double alphat : ALPHA_GRID) {
alpha = alphat;
double lp = logp_score();
if (!std::isnan(lp)) {
logps.push_back(logp_score());
alphas.push_back(alpha);
}
// DirichletCategorical does not take ownership of prng.
DirichletCategorical(std::mt19937* prng,
int k) { // k is number of categories
this->prng = prng;
counts = std::vector<int>(k, 0);
n = 0;
}
void incorporate(const double& x) {
assert(x >= 0 && x < counts.size());
counts[size_t(x)] += 1;
++n;
}
void unincorporate(const double& x) {
const size_t y = x;
assert(y < counts.size());
counts[y] -= 1;
--n;
assert(0 <= counts[y]);
assert(0 <= n);
}
double logp(const double& x) const {
assert(x >= 0 && x < counts.size());
const double numer = log(alpha + counts[size_t(x)]);
const double denom = log(n + alpha * counts.size());
return numer - denom;
}
double logp_score() const {
const size_t k = counts.size();
const double a = alpha * k;
const double lg = std::transform_reduce(
counts.cbegin(), counts.cend(), 0, std::plus{},
[&](size_t y) -> double { return lgamma(y + alpha); });
return lgamma(a) - lgamma(a + n) + lg - k * lgamma(alpha);
}
double sample() {
std::vector<double> weights(counts.size());
std::transform(counts.begin(), counts.end(), weights.begin(),
[&](size_t y) -> double { return y + alpha; });
int idx = choice(weights, prng);
return double(idx);
}
void transition_hyperparameters() {
std::vector<double> logps;
std::vector<double> alphas;
// C++ doesn't yet allow range for-loops over existing variables. Sigh.
for (double alphat : ALPHA_GRID) {
alpha = alphat;
double lp = logp_score();
if (!std::isnan(lp)) {
logps.push_back(logp_score());
alphas.push_back(alpha);
}
int i = sample_from_logps(logps, prng);
alpha = alphas[i];
}
int i = sample_from_logps(logps, prng);
alpha = alphas[i];
}
};
Loading

0 comments on commit b4e3c7b

Please sign in to comment.