diff --git a/.vscode/launch.json b/.vscode/launch.json index 7b1fe24a8..3184c5abe 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -108,6 +108,46 @@ "/home/bts/spark/": "${workspaceRoot}" } }, + { + "name": "TFTest ", + "type": "cppdbg", + "request": "launch", + "program": "/home/bts/spark/build/ninja_debug/sdk/examples/tf_test", + "args": [ + ], + "stopAtEntry": false, + "cwd": "/home/bts/spark/build/ninja_debug/sdk/examples/", + "environment": [], + "externalConsole": true, + "MIMode": "gdb", + "preLaunchTask": "", + "logging": { + "engineLogging": false + }, + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "enable pretty-printer", + "ignoreFailures": true + }, + { + "description": "Skip STL", + "text": "skip -gfi /usr/include/c++/7/bits/*.h", + "ignoreFailures": true + } + ], + "pipeTransport": { + "pipeCwd": "${workspaceRoot}", + "pipeProgram": "c:\\program files\\git\\usr\\bin\\ssh.exe", + "pipeArgs": [ + "bts@sparkvm" + ], + "debuggerPath": "/usr/bin/gdb" + }, + "sourceFileMap": { + "/home/bts/spark/": "${workspaceRoot}" + } + }, { "name": "Spreader Tests", "type": "cppdbg", diff --git a/CMakeLists.txt b/CMakeLists.txt index f40f0dcf1..a81d0138d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,7 +34,7 @@ if (WIN32) else () # so that sparkd looks for libspark.so in its directory set (CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) - set (CMAKE_INSTALL_RPATH ".") + set (CMAKE_INSTALL_RPATH ".:./lib") set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} \ -fPIC -Wall -pedantic -Wextra -Werror \ @@ -58,6 +58,9 @@ else () COMMAND mkdir ARGS -p ${CMAKE_BINARY_DIR}/server/plugins COMMAND ln ARGS -sf ${CMAKE_BINARY_DIR}/sdk/examples/order_plugin_example.so ${CMAKE_BINARY_DIR}/server/plugins/ COMMAND ln ARGS -sf ${CMAKE_BINARY_DIR}/sdk/examples/ai_order_plugin_example.so ${CMAKE_BINARY_DIR}/server/plugins/ + COMMAND mkdir -p ${CMAKE_BINARY_DIR}/server/lib + COMMAND ln ARGS -sf ${EXTERNALS_ROOT}/lib/libtensorflow.so ${CMAKE_BINARY_DIR}/server/lib/ + COMMAND ln ARGS -sf ${EXTERNALS_ROOT}/lib/libtensorflow_framework.so ${CMAKE_BINARY_DIR}/server/lib/ COMMENT "Creating server runtime layout" DEPENDS sparkd data_files_dir) diff --git a/externals b/externals index c0486ec0f..d026c5ed2 160000 --- a/externals +++ b/externals @@ -1 +1 @@ -Subproject commit c0486ec0f5aaabcf7e01a9f5490a52005a6fdcc8 +Subproject commit d026c5ed2ecbd5c3367add8906fdef82fd3ece67 diff --git a/src/sdk/examples/build_examples.sh b/src/sdk/examples/build_examples.sh index cedaa18ab..f998d32a7 100755 --- a/src/sdk/examples/build_examples.sh +++ b/src/sdk/examples/build_examples.sh @@ -55,15 +55,16 @@ g++ -isystem ../externals/include \ tf_test.cpp \ ../tensorflow/tfexecute.cpp \ ../tensorflow/tfloader.cpp \ + ../tensorflow/tfstatus.cpp \ -Wl,-rpath,. \ -Wl,-rpath,.. \ -Wl,-rpath,../lib \ -L ../lib \ -l spark \ -L ../externals/lib \ - -l tensorflow \ - -l tensorflow_framework \ + -l tensorflow-core \ -l nsync \ -lpthread \ -llttng-ust \ -ldl + diff --git a/src/sdk/examples/tf_test.cpp b/src/sdk/examples/tf_test.cpp index 10878fa48..97dfc6537 100644 --- a/src/sdk/examples/tf_test.cpp +++ b/src/sdk/examples/tf_test.cpp @@ -1,18 +1,18 @@ -#include "tensorflow/core/platform/env.h" -#include "tensorflow/core/public/session.h" +#include "../tensorflow/tfstatus.h" +#include "../tensorflow/tfloader.h" #include - using namespace std; using namespace tensorflow; +using namespace tensorflow::c; int main() { - Session* session; - Status status = NewSession(SessionOptions(), &session); - if (!status.ok()) { - cout << status.ToString() << "\n"; - return 1; - } - cout << "Session successfully created.\n"; + TF_Session* session = nullptr; + TFStatus status = bts::ml::create_tf_session(session); + if (!status.ok()) { + cout << status.code << "\n"; + return 1; + } + cout << "Session successfully created.\n"; } \ No newline at end of file diff --git a/src/sdk/tensorflow/saved_model_bundle_factory.h b/src/sdk/tensorflow/saved_model_bundle_factory.h deleted file mode 100644 index 14b947874..000000000 --- a/src/sdk/tensorflow/saved_model_bundle_factory.h +++ /dev/null @@ -1,93 +0,0 @@ -/* Copyright 2016 Google Inc. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_SERVING_SERVABLES_TENSORFLOW_SAVED_MODEL_BUNDLE_FACTORY_H_ -#define TENSORFLOW_SERVING_SERVABLES_TENSORFLOW_SAVED_MODEL_BUNDLE_FACTORY_H_ - -#include "tensorflow/cc/saved_model/loader.h" -#include "tensorflow/core/lib/core/status.h" -#include "tensorflow/core/platform/macros.h" -#include "tensorflow_serving/servables/tensorflow/session_bundle_config.pb.h" - -namespace tensorflow { -namespace serving { - -/// A factory that creates SavedModelBundles from SavedModel or SessionBundle -/// export paths. -/// -/// The emitted sessions only support Run(), and although not enforced it is -/// expected that the client will only make non-mutating Run() calls. (If this -/// restriction, which we've added as a safety measure, is problematic for your -/// use-case please contact the TensorFlow Serving team to discuss disabling -/// it.) -/// -/// If the config calls for batching, the emitted sessions automatically batch -/// Run() calls behind the scenes, using a SharedBatchScheduler owned by the -/// factory. The 'config.num_batch_threads' threads are shared across all -/// session instances created by this factory. However, each session has its own -/// dedicated queue of size 'config.max_enqueued_batches'. -/// -/// The factory can also estimate the resource (e.g. RAM) requirements of a -/// SavedModelBundle based on the SavedModel (i.e. prior to loading the -/// session). -/// -/// This class is thread-safe. -class SavedModelBundleFactory { - public: - /// Instantiates a SavedModelBundleFactory using a config. - /// - /// @param config Config with initialization options. - /// @param factory Newly created factory if the returned Status is OK. - static Status Create(const SessionBundleConfig& config, - std::unique_ptr* factory); - - /// Instantiates a bundle from a given export or SavedModel path. - /// - /// @param path Path to the model. - /// @param bundle Newly created SavedModelBundle if the returned Status is - /// OK. - Status CreateSavedModelBundle(const string& path, - std::unique_ptr* bundle); - - /// Estimates the resources a SavedModel bundle will use once loaded, from its - /// export path. - /// - /// @param path Path to the model. - /// @param estimate Output resource usage estimates. Different kinds of - /// resources (e.g. CPU, RAM, etc.) may get populated. -// Status EstimateResourceRequirement(const string& path, -// ResourceAllocation* estimate) const; - -// const SessionBundleConfig& config() const { return config_; } - - private: -// using Batcher = SharedBatchScheduler; - -// SavedModelBundleFactory(const SessionBundleConfig& config, -// std::shared_ptr batch_scheduler); - -// const SessionBundleConfig config_; - -// // A shared batch scheduler. One queue is used for each session this factory -// // emits. If batching is not configured, this remains null. -// std::shared_ptr batch_scheduler_; - - TF_DISALLOW_COPY_AND_ASSIGN(SavedModelBundleFactory); -}; - -} // namespace serving -} // namespace tensorflow - -#endif // TENSORFLOW_SERVING_SERVABLES_TENSORFLOW_SAVED_MODEL_BUNDLE_FACTORY_H_ diff --git a/src/sdk/tensorflow/tfapi.h b/src/sdk/tensorflow/tfapi.h new file mode 100644 index 000000000..bc5cca7fb --- /dev/null +++ b/src/sdk/tensorflow/tfapi.h @@ -0,0 +1,10 @@ + +#pragma once + +namespace tensorflow +{ + namespace c + { + #include "tensorflow/c/c_api.h" + } +} \ No newline at end of file diff --git a/src/sdk/tensorflow/tfexecute.cpp b/src/sdk/tensorflow/tfexecute.cpp index 03525a047..fa5c9a48c 100644 --- a/src/sdk/tensorflow/tfexecute.cpp +++ b/src/sdk/tensorflow/tfexecute.cpp @@ -1,8 +1,5 @@ #include "tfexecute.h" -#include "tensorflow/core/public/session.h" -#include "tensorflow/cc/saved_model/loader.h" - using namespace tensorflow; using std::vector; @@ -11,17 +8,17 @@ namespace bts namespace ml { -tensorflow::Status execute(const tensorflow::SavedModelBundle& bundle, - const vector >& inputs, - const vector& output_tensor_names, - const vector& target_node_names, - vector* outputs) -{ - tensorflow::RunMetadata* run_metadata = nullptr; - tensorflow::RunOptions run_options; - Status status = bundle.session->Run(run_options, inputs, output_tensor_names, target_node_names, outputs, run_metadata); - return status; -} +// tensorflow::Status execute(const tensorflow::SavedModelBundle& bundle, +// const vector >& inputs, +// const vector& output_tensor_names, +// const vector& target_node_names, +// vector* outputs) +// { +// tensorflow::RunMetadata* run_metadata = nullptr; +// tensorflow::RunOptions run_options; +// Status status = bundle.session->Run(run_options, inputs, output_tensor_names, target_node_names, outputs, run_metadata); +// return status; +// } } diff --git a/src/sdk/tensorflow/tfexecute.h b/src/sdk/tensorflow/tfexecute.h index 43eeb0c77..0f1101fa4 100644 --- a/src/sdk/tensorflow/tfexecute.h +++ b/src/sdk/tensorflow/tfexecute.h @@ -1,8 +1,9 @@ #pragma once +#include "tfstatus.h" #include #include -#include "tensorflow/core/platform/env.h" + namespace tensorflow { @@ -15,10 +16,10 @@ namespace bts namespace ml { - tensorflow::Status execute(const tensorflow::SavedModelBundle& bundle, - const std::vector >& inputs, - const std::vector& output_tensor_names, - const std::vector& target_node_names, - std::vector* outputs); + // tensorflow::c::TFStatus execute(const tensorflow::c::TF_Session& session, + // const std::vector >& inputs, + // const std::vector& output_tensor_names, + // const std::vector& target_node_names, + // std::vector* outputs); } } diff --git a/src/sdk/tensorflow/tfloader.cpp b/src/sdk/tensorflow/tfloader.cpp index 6acd6dfac..5872e2b11 100644 --- a/src/sdk/tensorflow/tfloader.cpp +++ b/src/sdk/tensorflow/tfloader.cpp @@ -1,12 +1,10 @@ #include "tfloader.h" #include "lib/utils/helper_macros.h" +#include - -//no c++ yet +//no c++ api yet //https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/label_image/main.cc -#include "tensorflow/cc/saved_model/loader.h" -using namespace tensorflow; using namespace tensorflow::c; using std::string; using std::vector; @@ -15,7 +13,44 @@ namespace bts { namespace ml { + +tensorflow::c::TFStatus create_tf_session(TF_Session*& session) +{ + TF_Graph* graph = TF_NewGraph(); + TF_SessionOptions* options = TF_NewSessionOptions(); + TF_Status* status = TF_NewStatus(); + session = TF_NewSession(graph, options, status); + TFStatus stat(*status); + TF_DeleteStatus(status); + return stat; +} + +tensorflow::c::TFStatus load_tf_model(const std::string& export_dir, const std::unordered_set& tags, + TF_Session*& session) +{ + vector ctags; + for(auto& t : tags) + ctags.push_back(const_cast(t.c_str())); + + TF_SessionOptions* session_options = TF_NewSessionOptions(); + TF_Buffer* run_options = nullptr; + TF_Buffer* meta_graph_def = TF_NewBuffer(); //TODO get the protos here + TF_Graph* graph = TF_NewGraph(); + TF_Status* status = TF_NewStatus(); + session = TF_LoadSessionFromSavedModel(session_options, run_options, export_dir.c_str(), &ctags[0], ctags.size(), + graph, meta_graph_def, status); + TFStatus stat(*status); + + TF_DeleteSessionOptions(session_options); + TF_DeleteBuffer(run_options); + TF_DeleteBuffer(meta_graph_def); + TF_DeleteStatus(status); + return stat; +} +} +} + // tensorflow::Status load_tf_model(const std::string& export_dir, const std::unordered_set& tags, SavedModelBundle* const bundle) // { // tensorflow::SessionOptions session_options; //defaults @@ -42,34 +77,4 @@ namespace ml // stat = factory->CreateSavedModelBundle(export_dir, bundle); // } // return stat; -// } - -tensorflow::c::TFStatus load_tf_model(const std::string& export_dir, const std::unordered_set& tags, - TF_Session** session) -{ - vector ctags; - for(auto& t : tags) - ctags.push_back(const_cast(t.c_str())); - - TF_SessionOptions* session_options = TF_NewSessionOptions(); - TF_Buffer* run_options = nullptr; - TF_Buffer* meta_graph_def = TF_NewBuffer(); //TODO get the protos here - TF_Graph* graph = TF_NewGraph(); - TF_Status* status = TF_NewStatus(); - *session = TF_LoadSessionFromSavedModel(session_options, run_options, export_dir.c_str(), &ctags[0], ctags.size(), - graph, meta_graph_def, status); - std::string msg(TF_Message(status)); - TF_Code code = TF_GetCode(status); - TFStatus stat; - stat.error = msg; - stat.code = code; - - TF_DeleteSessionOptions(session_options); - TF_DeleteBuffer(run_options); - TF_DeleteBuffer(meta_graph_def); - TF_DeleteStatus(status); - return stat; -} - -} -} \ No newline at end of file +// } \ No newline at end of file diff --git a/src/sdk/tensorflow/tfloader.h b/src/sdk/tensorflow/tfloader.h index cf8a17284..9a25fe1ff 100644 --- a/src/sdk/tensorflow/tfloader.h +++ b/src/sdk/tensorflow/tfloader.h @@ -1,38 +1,17 @@ #pragma once -#include "tensorflow/core/platform/env.h" +#include "tfstatus.h" #include #include -#include - - -namespace tensorflow -{ - struct SavedModelBundle; - namespace c - { - #include "tensorflow/c/c_api.h" - struct TFStatus - { - std::string error; - TF_Code code; - bool ok() { return (int)code == 0; } - }; - } -} namespace bts { namespace ml { - ///call Status.ok() for a boolean - // tensorflow::Status load_tf_model(const std::string& export_dir, const std::unordered_set& tags, - // tensorflow::SavedModelBundle* const bundle); - // tensorflow::Status load_tf_model(const std::string& export_dir, const std::unordered_set& tags, - // std::unique_ptr* bundle); - + + tensorflow::c::TFStatus create_tf_session(tensorflow::c::TF_Session*& session); tensorflow::c::TFStatus load_tf_model(const std::string& export_dir, const std::unordered_set& tags, - tensorflow::c::TF_Session** session); + tensorflow::c::TF_Session*& session); } } diff --git a/src/sdk/tensorflow/tfstatus.cpp b/src/sdk/tensorflow/tfstatus.cpp new file mode 100644 index 000000000..c9de66e07 --- /dev/null +++ b/src/sdk/tensorflow/tfstatus.cpp @@ -0,0 +1,21 @@ +#include "tfstatus.h" + +namespace tensorflow +{ +namespace c +{ + +TFStatus::TFStatus(TF_Status& stat) +{ + std::string msg(TF_Message(&stat)); + this->error = msg; + this->code = TF_GetCode(&stat); +} + +bool TFStatus::ok() const +{ + return (int)code == 0; +} + + +}} \ No newline at end of file diff --git a/src/sdk/tensorflow/tfstatus.h b/src/sdk/tensorflow/tfstatus.h new file mode 100644 index 000000000..a1d4c57eb --- /dev/null +++ b/src/sdk/tensorflow/tfstatus.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include "tfapi.h" + + +namespace tensorflow +{ + namespace c + { + struct TFStatus + { + TFStatus(TF_Status&); + bool ok() const; + + std::string error; + TF_Code code; + }; + } +}