Skip to content

Commit

Permalink
Feature model load (#8)
Browse files Browse the repository at this point in the history
* Ability to specify a model for Apollo to load when it encounters a new loop.

* Ability to specify a model for Apollo to load when it encounters a new loop.

* Added declaration of the model-loading constructor for DecisionTree.

* Initial success loading models for re-use.

* Added ability to instance Regions and pass in a path to a YAML file for the model you want to attach to that region.

Co-authored-by: Chad Wood <wood67@llnl.gov>
  • Loading branch information
cdwdirect and Chad Wood committed Aug 11, 2020
1 parent 6f3b244 commit 42dd440
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ sosd.*.db
sosd.*.key
*.swp
.gdb_history
.vscode/
examples/caliper-examples
CMakeCache.txt
CMakeFiles/
Expand Down
73 changes: 38 additions & 35 deletions COMPILATION_GUIDE.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

## Dependencies

Apollo requires the following dependencies to build:
* SOSflow
* Python 3.7+ (w/CFFI module)
Apollo has the following build dependencies:
* [Callpath](https://github.com/llnl/callpath)
* [OpenCV](https://github.com/opencv/opencv)

Apollo is currently built and tested with **GCC 4.9.3** or **Intel/19**
in a **TOSS3** environment on **CZQuartz**. Make sure to load
or swap in the correct compiler module before initializing this build process.
Notes:
* MPI support is optional, and typically provided by the system.
* Most of the scripts are preconfigured to look for projects in $HOME/src
* Check out the paths in the CMake + Make script: `go.hard`

Most of the scripts are preconfigured to look for projects in $HOME/src

## Basic Installation Process

Expand All @@ -20,28 +20,16 @@ Most of the scripts are preconfigured to look for projects in $HOME/src
# Execute this sequence of commands from the location where you want to
# base your installation of Apollo, eg. $HOME/src
#
#####
#
#
# NOTE: EVPath is no longer required by SOS. If you wish you run
# the version of SOS that uses EVPath, see the EVPath install
# instructinos at the bottom of this document, and install
# it before installing SOS.
#
#
# -- SOS
#
cd $HOME/src
git clone https://github.com/cdwdirect/sos_flow.git
cd sos_flow
./go.hard
######
#
# -- Callpath
#
cd $HOME/src
git clone https://github.com/LLNL/callpath.git
cd callpath
rm -rf ./install
rm -rf ./build/$SYS_TYPE
mkdir ./install
mkdir ./build/$SYS_TYPE && cd ./build/$SYS_TYPE
cmake
-D CMAKE_INSTALL_PREFIX=$HOME/src/callpath/install \
Expand All @@ -61,26 +49,41 @@ cd apollo
# You made it!
#
# Now, take a look at $APOLLO_DIR/jobs/job.blank.sh for a template
# that sets up the full environment, LD_LIBRARY_PATH and PYTHONPATH,
# that sets up the full environment, LD_LIBRARY_PATH, optional PYTHONPATH,
# experiment (Cleverleaf) binaries, etc. These are tested to work on CZQuartz
# with TOSS3.
#
# -- At this point, you are DONE and ready to go build examples!
#
##### #############################
##### Done! #############################
##### #############################

```



```bash
#####
#
# (Notes RE: Older experimental build w/SOS and Python+SKL)
#
Experimental async Apollo requires the following dependencies to build:
* SOSflow
* Python 3.7+ (w/CFFI module)

# --------------
Apollo is currently built and tested with **GCC 4.9.3** or **Intel/19**
in a **TOSS3** environment on **CZQuartz**. Make sure to load
or swap in the correct compiler module before initializing this build process.

#
# -- SOS
#
cd $HOME/src
git clone https://github.com/cdwdirect/sos_flow.git
cd sos_flow
./go.hard
#
## OPTIONAL: EVPath installation steps (before SOS, if using EVPath variant):
##
## -- EVPath (https://www.cc.gatech.edu/systems/projects/EVPath/)
##
##cd $HOME/src
##mkdir evpath
##cd evpath
##mkdir install
##wget http://www.cc.gatech.edu/systems/projects/EVPath/chaos_bootstrap.pl
##perl ./chaos_bootstrap.pl stable `pwd`/install
##perl ./chaos_build.pl

-->
```
1 change: 1 addition & 0 deletions include/apollo/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Config {
static int APOLLO_TRACE_ALLGATHER;
static int APOLLO_TRACE_BEST_POLICIES;
static std::string APOLLO_INIT_MODEL;
static std::string APOLLO_LOAD_MODEL;

private:
Config();
Expand Down
2 changes: 2 additions & 0 deletions include/apollo/ModelFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class ModelFactory {
static std::unique_ptr<PolicyModel> createRandom(int num_policies);
static std::unique_ptr<PolicyModel> createRoundRobin(int num_policies);

static std::unique_ptr<PolicyModel> loadDecisionTree(int num_policies,
std::string path);
static std::unique_ptr<PolicyModel> createDecisionTree(int num_policies,
std::vector< std::vector<float> > &features,
std::vector<int> &responses );
Expand Down
5 changes: 5 additions & 0 deletions include/apollo/Region.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class Apollo::Region {
const int num_features,
const char *regionName,
int numAvailablePolicies);
Region(
const int num_features,
const char *regionName,
int numAvailablePolicies,
std::string loadModelFromThisYamlFile);
~Region();

typedef struct Measure {
Expand Down
2 changes: 2 additions & 0 deletions include/apollo/models/DecisionTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ class DecisionTree : public PolicyModel {

public:
DecisionTree(int num_policies, std::vector< std::vector<float> > &features, std::vector<int> &responses);
DecisionTree(int num_policies, std::string path);

~DecisionTree();

int getIndex(void);
int getIndex(std::vector<float> &features);
void store(const std::string &filename);
void load(const std::string &filename);

private:
//Ptr<DTrees> dtree;
Expand Down
5 changes: 4 additions & 1 deletion src/Apollo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ Apollo::Apollo()
numThreads = ompDefaultNumThreads;

// Initialize config with defaults
Config::APOLLO_INIT_MODEL = safe_getenv( "APOLLO_INIT_MODEL", "Static,0" );
Config::APOLLO_INIT_MODEL = safe_getenv( "APOLLO_INIT_MODEL", "Static,0" ); // Create this kind of model.
Config::APOLLO_LOAD_MODEL = safe_getenv( "APOLLO_LOAD_MODEL", "" ); // Load a model from this file.

//std::cout << "init model " << Config::APOLLO_INIT_MODEL << std::endl;
Config::APOLLO_COLLECTIVE_TRAINING = std::stoi( safe_getenv( "APOLLO_COLLECTIVE_TRAINING", "1" ) );
//std::cout << "collective " << Config::APOLLO_COLLECTIVE_TRAINING << std::endl;
Expand Down Expand Up @@ -523,6 +525,7 @@ Apollo::flushAllRegionMeasurements(int step)
fout.close();
}

// TODO(cdw): Load prior decisiontree...
reg->model = ModelFactory::createDecisionTree(
num_policies,
train_features,
Expand Down
2 changes: 1 addition & 1 deletion src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ int Config::APOLLO_TRACE_RETRAIN;
int Config::APOLLO_TRACE_ALLGATHER;
int Config::APOLLO_TRACE_BEST_POLICIES;
std::string Config::APOLLO_INIT_MODEL;

std::string Config::APOLLO_LOAD_MODEL;

6 changes: 6 additions & 0 deletions src/ModelFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ std::unique_ptr<PolicyModel> ModelFactory::createRoundRobin(int num_policies) {
return std::make_unique<RoundRobin>( num_policies );
}


std::unique_ptr<PolicyModel> ModelFactory::loadDecisionTree(int num_policies,
std::string path) {
return std::make_unique<DecisionTree>( num_policies, path );
}
std::unique_ptr<PolicyModel> ModelFactory::createDecisionTree(int num_policies,
std::vector< std::vector<float> > &features,
std::vector<int> &responses ) {
return std::make_unique<DecisionTree>( num_policies, features, responses );
}


std::unique_ptr<TimingModel> ModelFactory::createRegressionTree(
std::vector< std::vector<float> > &features,
std::vector<float> &responses ) {
Expand Down
70 changes: 52 additions & 18 deletions src/Region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,31 +127,65 @@ Apollo::Region::Region(
current_policy = -1;
currently_inside_region = false;

// TODO use best_policies to train a model for new region for which there's training data
size_t pos = Config::APOLLO_INIT_MODEL.find(",");
std::string model_str = Config::APOLLO_INIT_MODEL.substr(0, pos);
if( "Static" == model_str ) {
int policy_choice = std::stoi( Config::APOLLO_INIT_MODEL.substr( pos+1 ) );
if( policy_choice < 0 || policy_choice >= numAvailablePolicies ) {
std::cerr << "Invalid policy_choice " << policy_choice << std::endl;

if( Config::APOLLO_LOAD_MODEL != "" ) {
model = ModelFactory::loadDecisionTree( apollo->num_policies, Config::APOLLO_LOAD_MODEL );
} else {
// TODO use best_policies to train a model for new region for which there's training data
size_t pos = Config::APOLLO_INIT_MODEL.find(",");
std::string model_str = Config::APOLLO_INIT_MODEL.substr(0, pos);
if( "Static" == model_str ) {
int policy_choice = std::stoi( Config::APOLLO_INIT_MODEL.substr( pos+1 ) );
if( policy_choice < 0 || policy_choice >= numAvailablePolicies ) {
std::cerr << "Invalid policy_choice " << policy_choice << std::endl;
abort();
}
model = ModelFactory::createStatic( apollo->num_policies, policy_choice );
//std::cout << "Model Static policy " << policy_choice << std::endl;
}
else if( "Random" == model_str ) {
model = ModelFactory::createRandom( apollo->num_policies );
//std::cout << "Model Random" << std::endl;
}
else if( "RoundRobin" == model_str ) {
model = ModelFactory::createRoundRobin( apollo->num_policies );
//std::cout << "Model RoundRobin" << std::endl;
}
else {
std::cerr << "Invalid model env var: " + Config::APOLLO_INIT_MODEL << std::endl;
abort();
}
model = ModelFactory::createStatic( apollo->num_policies, policy_choice );
//std::cout << "Model Static policy " << policy_choice << std::endl;
}
else if( "Random" == model_str ) {
model = ModelFactory::createRandom( apollo->num_policies );
//std::cout << "Model Random" << std::endl;
}
else if( "RoundRobin" == model_str ) {
model = ModelFactory::createRoundRobin( apollo->num_policies );
//std::cout << "Model RoundRobin" << std::endl;
//std::cout << "Insert region " << name << " ptr " << this << std::endl;
const auto ret = apollo->regions.insert( { name, this } );

return;
}

Apollo::Region::Region(
const int num_features,
const char *regionName,
int numAvailablePolicies,
std::string loadModelFromThisYamlFile)
:
num_features(num_features)
{
apollo = Apollo::instance();
if( Config::APOLLO_NUM_POLICIES ) {
apollo->num_policies = Config::APOLLO_NUM_POLICIES;
}
else {
std::cerr << "Invalid model env var: " + Config::APOLLO_INIT_MODEL << std::endl;
abort();
apollo->num_policies = numAvailablePolicies;
}

strncpy(name, regionName, sizeof(name)-1 );
name[ sizeof(name)-1 ] = '\0';

current_policy = -1;
currently_inside_region = false;


model = ModelFactory::loadDecisionTree( apollo->num_policies, loadModelFromThisYamlFile );
//std::cout << "Insert region " << name << " ptr " << this << std::endl;
const auto ret = apollo->regions.insert( { name, this } );

Expand Down
24 changes: 24 additions & 0 deletions src/models/DecisionTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#include <vector>
#include <algorithm>

#include <sys/stat.h>

#include "apollo/models/DecisionTree.h"
#include <opencv2/core/types.hpp>

Expand All @@ -48,6 +50,28 @@

using namespace std;

static inline bool fileExists(std::string path) {
struct stat stbuf;
return (stat(path.c_str(), &stbuf) == 0);
}

DecisionTree::DecisionTree(int num_policies, std::string path)
: PolicyModel(num_policies, "DecisionTree", false)
{
if (not fileExists(path)) {
std::cerr << "== APOLLO: Cannot access the DecisionTree model requested:\n" \
<< "== APOLLO: " << path << "\n" \
<< "== APOLLO: Exiting.\n";
exit(EXIT_FAILURE);
} else {
// The file at least exists... attempt to load a model from it!
std::cout << "== APOLLO: Loading the requested DecisionTree:\n" \
<< "== APOLLO: " << path << "\n";
dtree = RTrees::load(path.c_str());
}
return;
}

DecisionTree::DecisionTree(int num_policies, std::vector< std::vector<float> > &features, std::vector<int> &responses)
: PolicyModel(num_policies, "DecisionTree", false)
{
Expand Down

0 comments on commit 42dd440

Please sign in to comment.