Skip to content

Commit

Permalink
add managers
Browse files Browse the repository at this point in the history
  • Loading branch information
laitassou committed Jun 5, 2020
1 parent 540b9c5 commit fcf65f8
Show file tree
Hide file tree
Showing 10 changed files with 689 additions and 82 deletions.
5 changes: 4 additions & 1 deletion service/examples/mpg/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
cmake_minimum_required(VERSION 3.10)
project(mpg)


set(CMAKE_BUILD_TYPE Debug)

set(CMAKE_CXX_STANDARD 17)
#include_directories(../../include)
add_executable(mpg main.cpp ../../src/Model.cpp ../../src/Tensor.cpp ../../src/ModelInfos.cpp )
add_executable(mpg main.cpp ../../src/Model.cpp ../../src/Tensor.cpp ../../src/ModelInfos.cpp ../../src/LoaderHarness.cpp )
target_include_directories(mpg PRIVATE ../../include)
target_link_libraries (mpg -ltensorflow)
71 changes: 69 additions & 2 deletions service/examples/mpg/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,35 @@
#include "../../include/Tensor.hpp"
#include "../../include/ModelInfos.hpp"

#include "../../include/ModelManager.hpp"
#include "../../include/Loader.hpp"
#include "../../include/LoaderHarness.hpp"

#include "../../include/TFLoader.hpp"
#include "../../include/BasicModelManager.hpp"


#include <algorithm>
#include <iterator>


using namespace std;
using namespace ML;


template <typename Enumeration>
auto as_integer(Enumeration const value)
-> typename std::underlying_type<Enumeration>::type
{
return static_cast<typename std::underlying_type<Enumeration>::type>(value);
}

int main() {


// Create model
Model m("../frozen_graph.pb");
//m.restore("../checkpoint/train.ckpt");
//Model m("../frozen_graph.pb");
////m.restore("../checkpoint/train.ckpt");

ModelInfos<float> mInfos("../ModelInfo.txt");

Expand All @@ -22,6 +39,54 @@ int main() {
mInfos.show();




//std::unique_ptr<Loader> p_loader;

//p_loader = make_unique<BasicLoader>("../frozen_graph.pb");

std::array<ModelId,1> mlist = { ModelId{"mpg",{1,0}}};

//auto p_loader_harness = make_unique<LoaderHarness>(model_id , std::move(p_loader));

//p_loader_harness->Load();

//cout << "state:" << as_integer(p_loader_harness->state())<<endl;

//p_loader_harness->Unload();
//p_loader->Unload();
//cout << "state:" << as_integer(p_loader_harness->state())<<endl;

std::unique_ptr<BasicModelManager> manager_;
BasicModelManager::Create(&manager_);

for (auto &el : mlist){
manager_->LoadModel(el,"../","frozen_graph.pb");
}

auto v = manager_->ListAvailableModelIds();

for( auto &elem: v)
{
cout << "name : " << elem.name << "version:" << elem.version.major <<endl;
}

//manager_->UnloadModel(mlist[0]);

//std::unique_ptr<UntypedModelHandle> untyped_handle ;

ModelHandle<int64_t> handle ;
manager_->GetModelHandle(mlist[0],&handle);


manager_->UnloadModel(mlist[0]);





#if 0

mInfos.compute_columns();

std::cout << "depth:" <<mInfos.get_depth() <<std::endl;
Expand Down Expand Up @@ -77,5 +142,7 @@ Origin 314.0 1.573248 0.800988 1.0 1.00 1.0 2.00
std::cout << std::endl;


#endif


}
32 changes: 0 additions & 32 deletions service/include/BasicLoader.hpp

This file was deleted.

174 changes: 174 additions & 0 deletions service/include/BasicModelManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#pragma once

#include "LoaderHarness.hpp"
#include "ModelManager.hpp"

namespace ML {



class SharedPtrHandle final : public UntypedModelHandle {
public:
~SharedPtrHandle() override = default;

explicit SharedPtrHandle(const ModelId& id, std::shared_ptr<Loader> loader)
: id_(id), loader_(std::move(loader)) {}

void * servable() override { return loader_->servable(); }

const ModelId& id() const override { return id_; }

private:
const ModelId id_;
std::shared_ptr<Loader> loader_;
};


class BasicModelManager : public ModelManager {
public:
using PreHook = std::function<void(const uint32_t&)>;

~BasicModelManager() override { cout << "BasicModelManager destructor "<<endl;};

std::vector<ModelId> ListAvailableModelIds() const override;


void LoadModel(const ModelId& id,std::string path, std::string name);
void UnloadModel(const ModelId& id);


static Status Create(std::unique_ptr<BasicModelManager>* manager);


private :

struct Compare
{

bool operator() (const ModelId& l,
const ModelId& r) const{
return l.name.compare(r.name) && l.version.major == r.version.major && l.version.minor == r.version.minor ;
}

};

using HandlesMap = std::map<ModelId,std::shared_ptr< LoaderHarness> ,Compare>;
using mutex_lock = std::lock_guard<std::mutex>;

HandlesMap handles_map_;


Status GetUntypedModelHandle(const ModelId& request, std::unique_ptr<UntypedModelHandle>* untyped_handle) override ;

std::map<ModelId, std::unique_ptr<UntypedModelHandle>>GetAvailableUntypedModelHandles() const override {};

mutable mutex mu;


};

std::vector<ModelId> BasicModelManager::ListAvailableModelIds() const {
std::vector<ModelId> ids;

HandlesMap handles_map = handles_map_;
for (auto iter = handles_map.begin(); iter != handles_map.end(); iter++) {
// We get the iterator where all the values for a particular key ends.

if (iter->first.version.major>=1) {
ids.push_back(iter->first);
}

}

return ids;
}




Status BasicModelManager::GetUntypedModelHandle(const ModelId& request,std::unique_ptr<UntypedModelHandle>* const untyped_handle) {
const auto found_it = handles_map_.find(request);
if (found_it == handles_map_.end()) {
return Status::ERROR;
}
//std::shared_ptr<const HandlesMap> handles_map = handles_map_.get();

const LoaderHarness& harness = *found_it->second;

untyped_handle->reset(new SharedPtrHandle(
harness.id(), std::shared_ptr<Loader>(harness.loader())));

return Status::OK;
}

/*
std::map<ModelId, std::unique_ptr<UntypedModelHandle>>BasicModelManager::GetAvailableUntypedModelHandles() const {
std::map<ModelId, std::unique_ptr<UntypedModelHandle>> result;
//std::shared_ptr<const HandlesMap> handles_map = handles_map_.get();
for (const auto& handle : *handles_map_) {
const ModelId& request = handle.first;
if (!request.version) {
continue;
}
const LoaderHarness& harness = *handle.second;
result.emplace(harness.id(),
std::unique_ptr<UntypedModelHandle>(new SharedPtrHandle(
harness.id(), std::shared_ptr<Loader>(handles_map_,harness.loader()))));
}
return result;
}
*/


void BasicModelManager::LoadModel(const ModelId& id,std::string path, std::string name) {
cout << " load servable " << id.name << " version:" <<id.version.major<<endl;
mutex_lock l(mu);
std::unique_ptr<Loader> p_loader;

p_loader = make_unique<BasicLoader>(path,name);

std::shared_ptr<LoaderHarness> p_loader_harness = make_shared<LoaderHarness>(id , std::move(p_loader));

Status status = p_loader_harness->Load();

if( status == Status::OK){
handles_map_.emplace(id,p_loader_harness);
cout << "size:" <<handles_map_.size()<<endl;
cout << " emplace model " << id.name << " version:" <<id.version.major<<endl;

}
else
{
cout << "failed to load model :"<< id.name << endl;
}
}


void BasicModelManager::UnloadModel(const ModelId& model) {
//cout << " unload model " << model.name << " version:" <<model.version.major<<"." <<model.version.minor<<endl;
const auto found_it = handles_map_.find(model);

mutex_lock l(mu);
if(found_it != handles_map_.end()){
handles_map_.erase(found_it);
cout << "UnloadModel size:" <<handles_map_.size()<<endl;

}
else
{
cout << "nout found" << endl;
}
}


Status BasicModelManager::Create(std::unique_ptr<BasicModelManager>* manager) {
manager->reset(new BasicModelManager());
return Status::OK;
}


}
9 changes: 0 additions & 9 deletions service/include/BasicModelManger.hpp

This file was deleted.

Loading

0 comments on commit fcf65f8

Please sign in to comment.