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

Very slow inference in tensorflow #6

Open
jianlong-yuan opened this issue May 17, 2018 · 14 comments
Open

Very slow inference in tensorflow #6

jianlong-yuan opened this issue May 17, 2018 · 14 comments

Comments

@jianlong-yuan
Copy link

before i use your loss function, 2.5sec/step
after i use your loss function, 32.0sec/step

i use tensorflow 1.6.0

@bermanmaxim
Copy link
Owner

That slowdown seems quite drastic, do you have e.g. many categories or images to evaluate in one step? I suspect that dedicated CUDA kernel would speed up the implementation a lot (better than the large succession of the masking/selecting/sort operations). However I don't plan to tackle this in the near future - contributions in this direction are welcome.

@bermanmaxim bermanmaxim added the enhancement New feature or request label May 17, 2018
@jianlong-yuan
Copy link
Author

I just use it on cityscapes dataset. I use distributed computing. model is deeplab v3+

@bermanmaxim
Copy link
Owner

bermanmaxim commented May 19, 2018

I did some profiling. It seems in tensorflow the tf.cumsum operation is extremely slow on GPU, and takes a huge amount of time (~99% of the total time).

In Pytorch, as expected, the sort operation is the one that takes the most time, cumsum is virtually instant on GPU.

I will investigate a bit more, it might mandate an issue report for tensorflow.

@bermanmaxim bermanmaxim added tensorflow and removed enhancement New feature or request labels May 19, 2018
@bermanmaxim
Copy link
Owner

bermanmaxim commented May 19, 2018

This python notebook summarizes the problem: tensorflow/profile_ops.ipynb
The cumsum operation is ~4000x slower in tensorflow vs pytorch for typical number of pixels/batch.
tensorflow/tensorflow#813 mention that current implementations of cumsum in Eigen (tensorflow) is naïve. A solution would be to write a custom cuda op for this operation. Pointers for cumsum on gpu are given on https://stackoverflow.com/a/25251434/805502.

@bermanmaxim bermanmaxim changed the title training is very slow Very slow inference in tensorflow May 19, 2018
@bermanmaxim
Copy link
Owner

bermanmaxim commented May 24, 2018

After looking more into it, it seems the easiest way is to create a custom tensorflow op using cub exclusive sum instead of the native tf.Cumsum operation. Note that there are already operators defined in tensorflow using cub, e.g. topK, so it shouldn't be too difficult to implement this.

I will not implement this for now as I'm mainly using pytorch - I might do it one day but in the meantime I'll tag this as contributions welcome.

@ekelsen
Copy link

ekelsen commented Nov 17, 2018

The speed of cumsum has been improved significantly; I'm going to close this. Feel free to re-open if you feel it still isn't fast enough.

@ben2789
Copy link

ben2789 commented Nov 18, 2018

@ekelsen Which version of tensor flow has these improvements?

@ekelsen
Copy link

ekelsen commented Nov 18, 2018

Currently just HEAD: tensorflow/tensorflow@73e3215

@bermanmaxim
Copy link
Owner

Thanks for the pointer @ekelsen. Closing this issue

@stillwaterman
Copy link

stillwaterman commented Jan 15, 2019

@ekelsen , hello, I'm using Keras(backend: tensorflow 1.12) and cuda9.0, but the train speed is still slow with this loss function. Can you give me advice? My GPU is GTX 1080Ti

@ben2789
Copy link

ben2789 commented Jan 15, 2019

@stillwaterman I expect the build of Tensorflow you are using, was made before the changes to cumsum were implemented. Building Tensorflow from source, might be a reasonable option to expedite training.

@Z-Ianthe
Copy link

@jianlong-yuan hi~I want to use Lovász-Softmax loss in deeplab v3+ but failed. Could you give me some reference or demos? Thanks.

@jianlong-yuan
Copy link
Author

@Z-Ianthe How to solve the abouve problems, i put here https://github.com/jianlong-yuan/LovaszSoftmax_tf/tree/master

@bermanmaxim bermanmaxim reopened this Apr 9, 2019
@bermanmaxim
Copy link
Owner

I don't have time to investivate into tensorflow issues for now but I am at least reopening the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants