Deploying multiple models
What would be the best way to load and serve multiple models into the system?
Looking at the code that exists today, I couldn't see anything directly usable. If there's nothing that does this today, what would be a good set of things to look at to implement something like the following,
Some thoughts,
- Lets assume each model resided in a directory called /mnt/modelid/0000001
- The current
FileSystemStoragePathSourceonly looks under a given path for the aspired version. So ideally, there are some methods somewhere that can instead help look for modelid rather than within it. - The output of this should be a hashmap of
servableIdtype objects, and I ought to be able to do aGet()from such an object to get the rightmodelId, and then runclassify()on it.
Lastly - do you guys have use cases where there are multiple models spread out across multiple machines, and using some sort of a DHT implementation to access them?
Hi there,
The simplest approach is to deploy N instances of FileSystemStoragePathSource. The upstream modules that "catch" the aspired-versions calls are thread-safe (although I don't think we have good testing around that -- perhaps something you could contribute?).
If N gets large that starts to get ridiculous. Also, if you don't know the set of model names (a.k.a. servable names) in advance it wouldn't work because each FileSystemStoragePathSource requires to be configured with a model name. It would be great to have an alternative or generalized file-system source that would look for model_name/version_number, as you say. A contribution on that front would be welcome!
Regarding models on multiple machines -- we don't have anything that explicitly helps with that scenario at the moment. Perhaps if you can elaborate on your requirements and/or proposed architecture we can discuss further. It might be worth moving that to a different thread -- it's a distinct topic. Thanks.
Chris
Thanks for the answer, @chrisolston.
What I've ended up doing is create my own version of a FileSystemStoragePathSource that allows me to discover models within a filesystem, which are keyed by ID. I've also made a new version of the AspiredVersionsManager which allows me to load/unload these according to an eager policy. This gives me flexibility of either having a model with the same graph loaded with different datasets (versions), or different model graphs + datasets keyed by IDs.
Will start another thread around the multiple model serving, thanks!
Cool. Please consider contributing your code back to the project :). We are open to pull requests.
@chrisolston Absolutely :) - let me put these through the paces first.
One issue that would be good to get more info on is https://github.com/tensorflow/serving/issues/46.
Also, I'm still figuring out how to deploy this code in prod environments (https://github.com/tensorflow/serving/issues/44). Any pointers there would also help!
@viksit FYI this recent commit goes part way toward addressing this issue: https://github.com/tensorflow/serving/commit/a77b9a78af8d746fbd247b222128a0b64735f6fc
@kirilg @chrisolston thank you for the fyi - reviewing it now!
Any documentation on how to use TensorFlow serving to deploy multiple models ( not multiple versions of same model ) in production ?
I found this comment in main.cc of the server that seems to suggest this is already possible with a config file: https://github.com/tensorflow/serving/blob/master/tensorflow_serving/model_servers/main.cc#L20
// gRPC server implementation of
// tensorflow_serving/apis/prediction_service.proto.
//
// It bring up a standard server to serve a single TensorFlow model using
// command line flags, or multiple models via config file.
However, all the examples (MNIST & inception) seem to only use the command line flags. Is there any documentation on the config file?
As of this time, serving multiple models is fully supported:
- If you are using TF-Serving libraries (not the ModelServer binary) you can set up a FileSystemStoragePathSourceConfig to serve multiple models.
- If you are using the binary you can use ModelServerConfig to serve multiple models.
-Chris
@chrisolston Is there any documentation / example on using ModelServerConfig?
I don't think so. It should be pretty straightforward. You just give a list of models (via ModelConfigList). For each model you specify:
- Model name
- Model base path
- Model platform (use "tensorflow" unless you are doing something exotic)
@chrisolston could your please explain a little further.
- by 'binary', do you mean this 'bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server' ?
- how to give a list of models? code it in main.cc and compile or run the binary with a config file?
In main.cc, it says 'ModelServer does not yet support custom model config.'. It confuses me. Thanks in advance.
Same problem as @kinhunt . Any help? Thanks so much!
Same problem as @wangbin83-gmail-com - Thank you!
Ok, so not elegant, I made a simple hack / edit, but yes I had to edit the main.cc
that checks if the model argument has a comma, i.e. multiple models, if so it adds multiple models, appending the model name to the model path.
seems to work. this was a this morning hack. so not tried and tested,
my c++ is rusty, and I didn't want to add too many extra imports to do directory checking, to see if the directories in the model path are numeric (assume versions) and therefore only one model, or characters therefore multi-model
hope this helps
I've also gone down a similar path first I added the following imports:
#include <google/protobuf/text_format.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <fcntl.h>
Then created the following method:
ModelServerConfig BuildConfigFromFile(
const string& config_file_path) {
ModelServerConfig config;
LOG(INFO) << "Building from config file: "
<< config_file_path;
ModelServerConfig model_config;
int fd = open(config_file_path.c_str(), O_RDONLY);
google::protobuf::io::FileInputStream fstream(fd);
google::protobuf::TextFormat::Parse(&fstream, &model_config);
return model_config;
}
Then updated the main function by commenting out the model_base_path and model_name parameters and adding my own config_file parameter
int main(int argc, char** argv) {
tensorflow::int32 port = 8500;
bool enable_batching = false;
// tensorflow::string model_name = "default";
tensorflow::int32 file_system_poll_wait_seconds = 1;
// tensorflow::string model_base_path;
bool use_saved_model = false;
tensorflow::string config_file;
tensorflow::string model_version_policy =
FileSystemStoragePathSourceConfig_VersionPolicy_Name(
FileSystemStoragePathSourceConfig::LATEST_VERSION);
std::vector<tensorflow::Flag> flag_list = {
tensorflow::Flag("config_file", &config_file, "config file"),
tensorflow::Flag("port", &port, "port to listen on"),
tensorflow::Flag("enable_batching", &enable_batching, "enable batching"),
// tensorflow::Flag("model_name", &model_name, "name of model"),
// tensorflow::Flag(
// "model_version_policy", &model_version_policy,
// "The version policy which determines the number of model versions to "
// "be served at the same time. The default value is LATEST_VERSION, "
// "which will serve only the latest version. See "
// "file_system_storage_path_source.proto for the list of possible "
// "VersionPolicy."),
tensorflow::Flag("file_system_poll_wait_seconds",
&file_system_poll_wait_seconds,
"interval in seconds between each poll of the file "
"system for new model version"),
// tensorflow::Flag("model_base_path", &model_base_path,
// "path to export (required)"),
tensorflow::Flag("use_saved_model", &use_saved_model,
"If true, use SavedModel in the server; otherwise, use "
"SessionBundle. It is used by tensorflow serving team "
"to control the rollout of SavedModel and is not "
"expected to be set by users directly.")};
string usage = tensorflow::Flags::Usage(argv[0], flag_list);
const bool parse_result = tensorflow::Flags::Parse(&argc, argv, flag_list);
if (!parse_result || config_file.empty()) {
std::cout << usage;
return -1;
}
tensorflow::port::InitMain(argv[0], &argc, &argv);
if (argc != 1) {
std::cout << "unknown argument: " << argv[1] << "\n" << usage;
}
...
and also by commenting updating the creation of the server options:
// For ServerCore Options, we leave servable_state_monitor_creator unspecified
// so the default servable_state_monitor_creator will be used.
ServerCore::Options options;
//options.model_server_config = BuildSingleModelConfig(
// model_name, model_base_path, parsed_version_policy);
options.model_server_config = BuildConfigFromFile(config_file);
finally I can load in a text config file in the format, e.g.:
model_config_list: {
config: {
name: "bob",
base_path: "/tmp/model",
model_platform: "tensorflow"
},
config: {
name: "bob2",
base_path: "/tmp/model2",
model_platform: "tensorflow"
}
}
Finally running it as such:
./bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server --config_file=./tfserv.conf
I get output:
I tensorflow_serving/model_servers/main.cc:166] Building from config file: ./tfserv.conf
I tensorflow_serving/model_servers/server_core.cc:261] Adding/updating models.
I tensorflow_serving/model_servers/server_core.cc:298] (Re-)adding model: bob
I tensorflow_serving/model_servers/server_core.cc:298] (Re-)adding model: bob2
Maybe there's an easier way... hope this helps!
@perdasilva your c++ seems less rusty than mine. nice. might be worth a commit, maybe an override on preference check, config_file then single model.
Thanks
@sendit2me I've already forked the repo - I'm working on the patch now then I'll submit a PR The last time I touched C++ was at university =S thank god for google and stackoverflow hehehe
My apologies. I was mistaken when I claimed earlier that the binary (model_servers/main.cc) supports multiple models via ModelServerConfig. I am reviewing PR 294, which adds that feature, now. Thanks for your patience.
Any news on this? I'm looking for an option to deploy multiple models without having to change the C++ code.