lstm efficiency
It seems that small operations in LSTM cost a lot of time, we profile the kernel time using the cuda computeprof, the phenomenon is the same as described by https://devblogs.nvidia.com/parallelforall/optimizing-recurrent-neural-networks-cudnn-5/
Nvidia fusing these small operations into a big kernel. Is there any other way to solve this problem? @mli
The easiest way is to support cudnn lstm. It's on the schedule but no one is working on it yet. If you are interested, the cudnn_*-inl.h layers are good starting points.
Thanks,but this will reduce the flexibility. Is there any way to keep both: flexibility and efficiency ? By the way, I find setting environment var MXNET_GPU_WORKER_NTHREADS from 1 to 3, affects little on speed. My network has 3 layers. It should improve the speed by setting MXNET_GPU_WORKER_NTHREADS to a higher value since MxNet will auto parallel the operations in different layers. @piiswrong
No sequential layers cannot be parallelized because each one relies on the previous ones output. There is no way to be both flexible and efficient without runtime code generation, which is a huge project.
I did't describe the MXNET_GPU_WORKER_NTHREADS problem clearly. The network has 3 lstm layers, the seq_len is 16, some operators in different layers can be parallelized in theory. Just as the following pic shows, operators in the same back-diagonal can be executed in parallel. I think MxNet's engine can handle this case.

that's right. increasing MXNET_GPU_WORKER_NTHREADS might improve the performance since it's able to parallel the computations within a single card. however, the gain depends on the workload and hardware (e.g k40 is better than gtx 970).
as far as i know, we implemented Combined GEMMs, GEMM Streaming, Overlapping layers mentioned in the paper (table 1) http://arxiv.org/pdf/1604.01946v1.pdf
On Mon, May 23, 2016 at 12:10 AM, feiyulv [email protected] wrote:
I did't describe the MXNET_GPU_WORKER_NTHREADS problem clearly. The network has 3 lstm layers, the seq_len is 16, some operators in different layers can be parallelized in theory. Just as the following pic shows, operators in the same back-diagonal can be executed in parallel. I think MxNet's engine can handle this case. [image: image06] https://cloud.githubusercontent.com/assets/18651286/15462803/aa6ec5f8-20f7-11e6-8211-7898a346fc38.png
— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/dmlc/mxnet/issues/2210#issuecomment-220903018
Discussion
This is an interesting topic to discuss, and related to what @mavenlin and I discussed a while ago. Think about efficiency in executions, there are three causes:
- The memory latency and total save load cost of kernel
- The cost of kernel launching between consecutive kernel calls
- The cost of the kernel itself.
Currently mxnet parallelizes the workload within single card, which meas it can likely hide the kernel launching problem by a bit. But in our experience, it is likely to be around 25% gain. There are several potential cause here:
- The scheduler is not yet very optimized so there is still gap between kernel launching(likely to see gaps between kernel executions)
- The kernel is optimized for quickly execution and occupy all resources, but when there are small kernels, they still occupy all resources to prevent other workload to occupy GPU
- The fused kernel reduces memory fetchs and increases speed overall
Directions
Only the last direction will involve a NTC or specially optimized code. But I do think there might still be something to gain by simply sharing workloads and possibly using customized kernels that aimed to take partial resources. It is very interesting to see if it is possible to do so without loss of flexiblity.
@tqchen A simple idea comes to my mind is to restrict the resources used by small kernels, such as reducing the block numbers and block size, increasing the computations of each thread.
@mli The hardware I use is M40. I see no difference in speed when I changes the value of MXNET_GPU_WORKER_NTHREADS from 1 to 3
From the profiling result that @feiyulv showed, the small kernels are executed sequentially because the MXNET_GPU_WORKER_NTHREADS is set to 1. It could be helpful to also see the same graph when this value is set to 3.
I think the following parts are optimizable.
Multistream execution
The current engine would not determine which stream to use based on the dependency. The streams are used in a pulling way, whichever finishes first will pull the next kernel for execution. Therefore it is not guaranteed that the red blocks would be in different streams. But the work load would be balanced in this way.
In the multiple branch case, the efficiency of the engine can be further improved when the streams are used based on the dependency. For example, kernel that depends on other kernels should be scheduled to any of the streams of its depending kernels. A stream can be dynamically allocated(or get from the pool) when there is a bifurcation, and send back to the pool for reuse when the bifurcation merges back.
As @tqchen mentioned, whether paralleling with stream is beneficial is also decided by the resources that the kernel uses. In most of the cases, there is space for improvement because the resources are not used up. In the case each kernel is optimized to use all the resources it might even hamper the performance (but also depends on how well NV people have optimized the context switching of streams).
Gap between Kernel Launch
From the profiling result, I believe this is the major cause of the bad performance. Because the gaps are as long as if not longer than the kernels. This is caused by the current scheduling method of the engine. Although cuda provides an asynchronous interface, current engine is not using it. Instead, after a kernel is launched the engine will immediately call cudaStreamSynchronize. It is until the current kernel is finished, the callback function will launch the next one.
User -(async)-> Engine -(sync)-> Cuda
Prescheduling the kernels
Combining the above two points, a prescheduling engine can be developed.
- By prescheduling, the gap between kernel launches can be hidden because engine to cuda is now async.
User -(async)-> Engine -(async)-> Cuda
- This is also beneficial for embeded torch module (or other functions in script but calls cuda), because as long as the last step is async, we can preserve the performance.
User -(async)-> Engine -(sync)-> Lua -(async)-> Cuda
- Streams are balanced when they actively pulls from a task queue, it is hard to achieve this balance when we do prescheduling which means pushing. It is then necessary to schedule the kernels based on the dependencies.
Instead, after a kernel is launched the engine will immediately call cudaStreamSynchronize
This is a major issue for small kernels. We should fix this. @tqchen Can you work on a new engine?
@mavenlin
set MXNET_GPU_WORKER_NTHREADS to 2, speed: 1050 samples/s
set MXNET_GPU_WORKER_NTHREADS to 3, speed: 1070 samples/s
ps: the previous profiling pic is from the blog https://devblogs.nvidia.com/parallelforall/optimizing-recurrent-neural-networks-cudnn-5/
I find that the RNN api in CuDNN-V5 can only support standard recurrent neural networks (LSTM, RNN, GRU). However, we sometimes still need to define our own RNN structures. Specific RNN related optimizations in the engine side will be extremely helpful in this case.
This issue is closed due to lack of activity in the last 90 days. Feel free to reopen if this is still an active issue. Thanks!