-
Notifications
You must be signed in to change notification settings - Fork 6.8k
[RFC] MXNet Multithreaded Inference Interface #16431
Comments
Hey, this is the MXNet Label Bot. |
Great proposal! Few questions from my end:
|
Thanks @marcoabreu !
The issue I found with C API thread safety especially with the cached op use case was the ThreadLocalStore. If we fix this issue then C APIs related to CreateCachedOp and InvokeCachedOp should be threadsafe.
This should still support the single GPU use-case for 1.6. Multi GPU inference use case requires more verification at the cached op level .
I don't think we have such a strict split between inference and training APIs at the C API level. For example for gluon cached op we call InvokeCachedOp for both training and Inference. But if I rephrase your question to: |
@mxnet-label-bot , add [Feature] |
Hi @anirudh2290, what is the status of this proposal? When do you think changes will be ready? |
@ptrendx I am trying to open a PR by Friday. On the status : the two prereqs issues dmlc/dmlc-core#573 and #16434 have been better understood and fixed/worked around. I have made C API and backend changes and currently still testing it. Because of time and resource constraints I won't be able to add the CPP frontend changes (which has been mentioned in this PR as targeted for 1.6) in this proposal but only C API changes, backend changes and tests/verification. |
@anirudh2290 Just see this RFC. Let me share what I've done in multithreaded infererce, I think it's the only viable way now in mxnet. I've deployed many models with scala API, and run them in multiple threads. The whole system has run smoothly in production environment for more than 2 months. The backend of inference is graph executor, which is created for each thread with shared model parameters. The executors can be dynamically reshaped in each thread independently according to the shape of the data input. Like what's mentioned above, the dependency engine is not thread safe, so if you run it in threaded engine, dead lock and core dump will happen. Therefore, naive engine is the only option left. Without the dependency scheduling, any write dependency on model parameters is likely to be executed simultaneously and mess the internal data. If mkldnn is used to accelerate inference, you will get non-deterministic results per inference because mxnet stealthily reorder the data in ndarray (write dependency involved) for mkldnn operators. I've used a temporary method to address this issue which is not suitable for an official PR. Multithreaded inference should be used with caution. Sharing model parameters can reduce the memory footprint in your program, but a lot of memory usage is consumed by global resources (temporary workspace, random number generator, ...) or op cache for mkldnn which are stored in static thread_local variables. So thread number is the most important factor for memory footprint, any thread involving mxnet operation, be it any trivial imperative invoking of operators, will incur memory overhead by creating its own set of thread_local variables. I've spent so much time tracking down memory leak and the best solution is to limit thread number. A new method to do multithreaded inference by threaded engine is much welcomed here. It will solve the above issues automatically and ensure result correctness by enforcing dependency checking. |
Thanks for the thoughtful and valuable comments @arcadiaphy.
Yes, if I am not mistaken this is very similar to how the C Predict API supports multi threaded inference today.
This is a very useful point. In my proposal, I was concentrating mostly on ThreadedEngine and not NaiveEngine. Though, recently I added tests for NaiveEngine in my PR and everything seemed to be working fine. Till now I have not been able to reproduce the correctness issue that you mention with MKLDNN (hidden write) and NaiveEngine, but it could be because the Reorder doesnt happen in the spawned thread. Here is my test: https://github.com/apache/incubator-mxnet/pull/16654/files#diff-1335fbaf3930b1438d9be18edb07a1a6R1384 . Not sure, if something changed with MKLDNN 1.0 or my test doesnt catch that use case, will dig more into this.
Yes, the earlier approach which has one graph executor per thread, may have a lot of memory consumption for global resources. Sharing the cached op will alleviate the pain. As you know, we still have a lot of customers using graph executor as the backend. Would be a great add, if you are interested to contribute towards making graph executor also thread safe for inference use cases. |
@anirudh2290 hi, I use mutithreading C++ API and find that the change between NDArrayHandle* to NDArray cost too much time(about 50ms). My prvious pred time about the model just cost 20-30ms. Is it normal? |
though there exsists async, it really cost too much that against my will to use multithread |
I think is there any method can be replaced with data output, like vector<mx_float>. |
ok, I have found float* format output width method MxPred... works. |
Thanks to @nswamy for his inputs and design discussions related to this project and @frankfliu for explaining the requirements and the use case from customer perspective.
Problem Statement
One of the big un-catered for use cases in MXNet is loading a model and being able to run parallel inference on the model from multiple threads while sharing the parameters. There are multiple user requests for the same [1]. There also has been a lot of confusion around the current state of MXNet with respect to thread safety.
This doc attempts to address three things :
Current State of MXNet Thread Safety
MXNet Dependency Engine Thread Safety
Examining MXNet dependency engine code, it looks like it was designed to be thread safe. Tried to push Convolution op from multiple threads into MXNet Engine, to see if there are any issues with thread safety. Used CPP Package for the same. The script is provided here : https://github.com/anirudh2290/mxnet/tree/multithreaded_inference_poc/cpp-package/example/multithreading_engine_push_mxnet_op.cpp
The script pushes Convolution op to the engine from multiple threads. You can verify the correctness of the op with this script :
https://github.com/anirudh2290/mxnet/tree/multithreaded_inference_poc/test_cached_op_ts_check.py
MXNet Graph Executor Thread Safety
Removed NaiveEngine only restriction for C Predict API and tried to run multi threaded inference with C Predict API using ThreadedEngine by commenting the check : https://github.com/anirudh2290/mxnet/tree/multithreaded_inference_poc/src/c_api/c_predict_api.cc
When running this example the program core dumps with memory leaks in Graph Executor Bind. This shows that graph executor is not thread safe.
Cached Op (Gluon Backend) Thread Safety
Try to create cached op in the main thread and spawn multiple threads to invoke the same cached op inside each of the threads. Here is the script which does the same : https://github.com/anirudh2290/mxnet/tree/multithreaded_inference_poc/cpp-package/example/multithreading_engine_push_cached_op.cpp
Multiple failures seen when I run this: one is in the dmlc ThreadLocalStore [2], other is in MXPlanMemory, retrieving forward_ref_count attribute. These errors are because of race condition w.r.t reading and writing of shared states in CachedOp.
Proposed Solution
Additions (Prioritized for 1.6)
Proposing to add a minimal thread safe cached op for inference which will be the following :
C API Changes (Prioritized for 1.6)
Adding a new thread_safe flag for MXCreateCachedOpEx. When set to true this should create a thread_safe cached op instead of a cached op.
Add similar thread_safe flag flags to Invoke and Free to invoke thread safe cached op versions instead of the default versions.
Please see the PoC here for details:
Use Cases Tested:
CPP Frontend Changes (Priority for 1.6)
@access2rohit will be helping me with the CPP API changes.
Python Frontend Changes (Lower Priority, Post 1.6)
Existing Issues
Expected Benefits
One big benefit is being able to run inference on the same model with shared params from multiple threads. Current approach is to use multiprocessing library and import mxnet in each process. This saves a lot of memory footprint and improves the throughput for inference on a single machine. To obtain some numbers I wrote a multiprocessing script in python to load model and run inference from multiple processes.
Please see here for the python script : https://github.com/anirudh2290/mxnet/tree/multithreaded_inference_poc/test_symbolblock_cached_op_ts.py
This runs out of memory with 12 parallel inferences.
When running the same model inference on CPP, please see example here : https://github.com/anirudh2290/mxnet/tree/multithreaded_inference_poc/cpp-package/example/multithreading_engine_push_cached_op_full_model.cpp
This is able to run more than 960 parallel inferences though there is an increased latency with higher number of parallel inferences.
Model Coverage
This is a work in progress list and more models will be added to this list.
What will not be supported for 1.6 ?
Since, this is a new interface where many things can go wrong, we are starting small here and will incrementally add support. Lot of these features may just work but requires some effort with verification and won't be feasible for 1.6.
References
The text was updated successfully, but these errors were encountered: