Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix vs support #2

Merged
merged 2 commits into from
Oct 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,15 @@ file(GLOB SOURCES
)

if (USE_PROTO)
if(MSVC)
message(FATAL_ERROR "Cannot use proto with MSVC.")
endif(MSVC)
find_package(Protobuf REQUIRED)
PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS proto/model.proto)
include_directories(${PROTOBUF_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
ADD_DEFINITIONS(-DUSE_PROTO)
SET(PROTO_FILES src/proto/gbdt_model_proto.cpp ${PROTO_HDRS} ${PROTO_SRCS})
else()
include_directories(src/proto/not_implemented)
SET(PROTO_FILES src/proto/not_implemented/gbdt_model_proto.cpp)
endif(USE_PROTO)

add_executable(lightgbm src/main.cpp ${SOURCES} ${PROTO_FILES})
Expand Down
5 changes: 5 additions & 0 deletions include/LightGBM/boosting.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

#include <LightGBM/meta.h>
#include <LightGBM/config.h>

#ifdef USE_PROTO
#include "model.pb.h"
#endif // USE_PROTO

#include <vector>
#include <string>
Expand Down Expand Up @@ -188,6 +191,7 @@ class LIGHTGBM_EXPORT Boosting {
*/
virtual bool LoadModelFromString(const std::string& model_str) = 0;

#ifdef USE_PROTO
/*!
* \brief Save model with protobuf
* \param num_iterations Number of model that want to save, -1 means save all
Expand All @@ -201,6 +205,7 @@ class LIGHTGBM_EXPORT Boosting {
* \return true if succeeded
*/
virtual bool LoadModelFromProto(const char* filename) = 0;
#endif // USE_PROTO

/*!
* \brief Calculate feature importances
Expand Down
9 changes: 7 additions & 2 deletions include/LightGBM/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

#include <LightGBM/meta.h>
#include <LightGBM/dataset.h>
#ifdef USE_PROTO
#include "model.pb.h"
#endif // USE_PROTO

#include <string>
#include <vector>
Expand Down Expand Up @@ -31,12 +33,13 @@ class Tree {
* \param str Model string
*/
explicit Tree(const std::string& str);

#ifdef USE_PROTO
/*!
* \brief Construtor, from a protobuf object
* \param model_tree Model protobuf object
*/
explicit Tree(const LightGBM::Model_Tree& model_tree);
explicit Tree(const Model_Tree& model_tree);
#endif // USE_PROTO

~Tree();

Expand Down Expand Up @@ -172,8 +175,10 @@ class Tree {
/*! \brief Serialize this object to if-else statement*/
std::string ToIfElse(int index, bool is_predict_leaf_index) const;

#ifdef USE_PROTO
/*! \brief Serialize this object to protobuf object*/
void ToProto(Model_Tree& model_tree) const;
#endif // USE_PROTO

inline static bool IsZero(double fval) {
if (fval > -kZeroAsMissingValueRange && fval <= kZeroAsMissingValueRange) {
Expand Down
4 changes: 4 additions & 0 deletions src/application/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,11 @@ void Application::Train() {
if (model_format == std::string("text")) {
boosting_->SaveModelToFile(-1, save_file_name.c_str());
} else if (model_format == std::string("proto")) {
#ifdef USE_PROTO
boosting_->SaveModelToProto(-1, save_file_name.c_str());
#else
Log::Fatal("Please cmake with -DUSE_PROTO=ON to use protobuf.");
#endif // USE_PROTO
} else {
Log::Fatal("Unknown model format during saving: %s", model_format.c_str());
}
Expand Down
4 changes: 4 additions & 0 deletions src/boosting/boosting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ bool Boosting::LoadFileToBoosting(Boosting* boosting, const std::string& format,
return false;
}
} else if (format == std::string("proto")) {
#ifdef USE_PROTO
if (!boosting->LoadModelFromProto(filename)) {
return false;
}
#else
Log::Fatal("Please cmake with -DUSE_PROTO=ON to use protobuf.");
#endif // USE_PROTO
} else {
Log::Fatal("Unknown model format during loading: %s", format.c_str());
}
Expand Down
2 changes: 2 additions & 0 deletions src/boosting/gbdt.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ class GBDT: public GBDTBase {
*/
bool LoadModelFromString(const std::string& model_str) override;

#ifdef USE_PROTO
/*!
* \brief Save model with protobuf
* \param num_iterations Number of model that want to save, -1 means save all
Expand All @@ -249,6 +250,7 @@ class GBDT: public GBDTBase {
* \return true if succeeded
*/
bool LoadModelFromProto(const char* filename) override;
#endif // USE_PROTO

/*!
* \brief Calculate feature importances
Expand Down
22 changes: 0 additions & 22 deletions src/proto/not_implemented/gbdt_model_proto.cpp

This file was deleted.

9 changes: 0 additions & 9 deletions src/proto/not_implemented/model.pb.h

This file was deleted.

3 changes: 0 additions & 3 deletions windows/LightGBM.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,12 @@
<ClInclude Include="..\src\treelearner\parallel_tree_learner.h" />
<ClInclude Include="..\src\treelearner\serial_tree_learner.h" />
<ClInclude Include="..\src\treelearner\split_info.hpp" />
<ClInclude Include="..\src\proto\not_implemented\model.pb.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\application\application.cpp" />
<ClCompile Include="..\src\boosting\boosting.cpp" />
<ClCompile Include="..\src\boosting\gbdt.cpp" />
<ClCompile Include="..\src\boosting\gbdt_model_text.cpp" />
<ClCompile Include="..\src\proto\gbdt_model_proto.cpp" />
<ClCompile Include="..\src\proto\not_implemented\gbdt_model_proto.cpp" />
<ClCompile Include="..\src\boosting\gbdt_prediction.cpp" />
<ClCompile Include="..\src\boosting\prediction_early_stop.cpp" />
<ClCompile Include="..\src\c_api.cpp" />
Expand Down
9 changes: 0 additions & 9 deletions windows/LightGBM.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,6 @@
<ClInclude Include="..\src\boosting\rf.hpp">
<Filter>src\boosting</Filter>
</ClInclude>
<ClInclude Include="..\src\proto\not_implemented\model.pb.h">
<Filter>src\proto\not_implemented</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\application\application.cpp">
Expand Down Expand Up @@ -284,11 +281,5 @@
<ClCompile Include="..\src\boosting\gbdt_model_text.cpp">
<Filter>src\boosting</Filter>
</ClCompile>
<ClCompile Include="..\src\proto\gbdt_model_proto.cpp">
<Filter>src\proto</Filter>
</ClCompile>
<ClCompile Include="..\src\proto\not_implemented\gbdt_model_proto.cpp">
<Filter>src\proto\not_implemented</Filter>
</ClCompile>
</ItemGroup>
</Project>