How to speed up `import tensorflow`?

3,341 views
Skip to first unread message

peng...@gmail.com

unread,
Dec 10, 2017, 12:25:50 AM12/10/17
to Discuss
It runs slowly to import tensorflow. Is there a way to speed up the import? Thanks.

In [1]: %time  import tensorflow
CPU times: user 1.19 s, sys: 431 ms, total: 1.62 s
Wall time: 2.24 s

Jim Lloyd

unread,
Dec 10, 2017, 1:44:44 PM12/10/17
to peng...@gmail.com, Discuss
Which version of tensorflow are you using? Version 1.4 imports faster than prior versions.
--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+u...@tensorflow.org.
To post to this group, send email to dis...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/discuss/9f3e2d08-021f-4a63-96a6-6325a061537d%40tensorflow.org.

Peng Yu

unread,
Dec 10, 2017, 2:43:02 PM12/10/17
to Jim Lloyd, Discuss
In [2]: print tensorflow.__version__
1.4.0
--
Regards,
Peng

Yaroslav Bulatov

unread,
Dec 10, 2017, 4:44:54 PM12/10/17
to Peng Yu, Jim Lloyd, Discuss
Relevant commit: https://github.com/tensorflow/tensorflow/commit/3445dd0edf63f217822c4c920f85d0a3bab9152e
import time should be 400ms in TF 1.4 if the file cache is warm

On Sun, Dec 10, 2017 at 11:42 AM, Peng Yu <peng...@gmail.com> wrote:
In [2]: print tensorflow.__version__
1.4.0

On Sun, Dec 10, 2017 at 12:44 PM, Jim Lloyd <jim....@gmail.com> wrote:
> Which version of tensorflow are you using? Version 1.4 imports faster than
> prior versions.
> On Sat, Dec 9, 2017 at 9:25 PM <peng...@gmail.com> wrote:
>>
>> It runs slowly to import tensorflow. Is there a way to speed up the
>> import? Thanks.
>>
>> In [1]: %time  import tensorflow
>> CPU times: user 1.19 s, sys: 431 ms, total: 1.62 s
>> Wall time: 2.24 s
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Discuss" group.
>> To unsubscribe from this group and stop receiving emails from it, send an

>> To post to this group, send email to dis...@tensorflow.org.
>> To view this discussion on the web visit
>> https://groups.google.com/a/tensorflow.org/d/msgid/discuss/9f3e2d08-021f-4a63-96a6-6325a061537d%40tensorflow.org.



--
Regards,
Peng

--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+unsubscribe@tensorflow.org.

To post to this group, send email to dis...@tensorflow.org.

Dirk Toewe

unread,
Dec 10, 2017, 4:56:47 PM12/10/17
to dis...@tensorflow.org

Hi Pengyu,

as far as I understand it, Tensorflow is not going be optimized for a fast cold start. Usually training takes orders of magnitudes longer than 1.2 seconds, so it wouldn't make much sense. For deployment, there's AOT compilation (https://www.tensorflow.org/performance/xla/tfcompile) or the C/C++-API.

If You still need to stick to Python and You need a fast cold start, here's some possible solutions:

  • Install the Preload daemon under linux, it will try to cache frequently used files in RAM
  • You can create a RAM-Disk and copy Your Python environment to it, provided You have enough RAM
  • You can start a Python service and continually submit jobs to it. This could be done creating a tiny local webserver (e.g. with Flask).

Hope this helps
Dirk

Peng Yu

unread,
Dec 10, 2017, 8:14:41 PM12/10/17
to Yaroslav Bulatov, Jim Lloyd, Discuss
How to ensure the file cache is warm on Mac OS X?

On Sun, Dec 10, 2017 at 3:44 PM, Yaroslav Bulatov <yaros...@gmail.com> wrote:
> Relevant commit:
> https://github.com/tensorflow/tensorflow/commit/3445dd0edf63f217822c4c920f85d0a3bab9152e
> import time should be 400ms in TF 1.4 if the file cache is warm
>
> On Sun, Dec 10, 2017 at 11:42 AM, Peng Yu <peng...@gmail.com> wrote:
>>
>> In [2]: print tensorflow.__version__
>> 1.4.0
>>
>> On Sun, Dec 10, 2017 at 12:44 PM, Jim Lloyd <jim....@gmail.com> wrote:
>> > Which version of tensorflow are you using? Version 1.4 imports faster
>> > than
>> > prior versions.
>> > On Sat, Dec 9, 2017 at 9:25 PM <peng...@gmail.com> wrote:
>> >>
>> >> It runs slowly to import tensorflow. Is there a way to speed up the
>> >> import? Thanks.
>> >>
>> >> In [1]: %time import tensorflow
>> >> CPU times: user 1.19 s, sys: 431 ms, total: 1.62 s
>> >> Wall time: 2.24 s
>> >>
>> >> --
>> >> You received this message because you are subscribed to the Google
>> >> Groups
>> >> "Discuss" group.
>> >> To unsubscribe from this group and stop receiving emails from it, send
>> >> an
>> >> email to discuss+u...@tensorflow.org.
>> >> To post to this group, send email to dis...@tensorflow.org.
>> >> To view this discussion on the web visit
>> >>
>> >> https://groups.google.com/a/tensorflow.org/d/msgid/discuss/9f3e2d08-021f-4a63-96a6-6325a061537d%40tensorflow.org.
>>
>>
>>
>> --
>> Regards,
>> Peng
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Discuss" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to discuss+u...@tensorflow.org.
--
Regards,
Peng

Justine Tunney

unread,
Dec 10, 2017, 8:17:48 PM12/10/17
to Peng Yu, Yaroslav Bulatov, Jim Lloyd, Discuss
I seem to recall it being much slower on Mac than Linux. The only way we're going to make it go faster than the aforementioned commit is by putting all the .py and .pyc files in a zip and adding it to the Python path.


>> >> To post to this group, send email to dis...@tensorflow.org.
>> >> To view this discussion on the web visit
>> >>
>> >> https://groups.google.com/a/tensorflow.org/d/msgid/discuss/9f3e2d08-021f-4a63-96a6-6325a061537d%40tensorflow.org.
>>
>>
>>
>> --
>> Regards,
>> Peng
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Discuss" group.
>> To unsubscribe from this group and stop receiving emails from it, send an

>> To post to this group, send email to dis...@tensorflow.org.
>> To view this discussion on the web visit
>> https://groups.google.com/a/tensorflow.org/d/msgid/discuss/CABrM6wmNFL05kgFiyDnJzta7LrhK%2Bij-0cs3M3RZwKaJpztO9A%40mail.gmail.com.
>
>



--
Regards,
Peng

--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+unsubscribe@tensorflow.org.

To post to this group, send email to dis...@tensorflow.org.

Peng Yu

unread,
Dec 11, 2017, 12:19:37 AM12/11/17
to Justine Tunney, Yaroslav Bulatov, Jim Lloyd, Discuss
Could you show me how to do it? I got the following error.

~$ PYTHONPATH=/tmp/tensorflow.zip ./main.py
Traceback (most recent call last):
File "./main.py", line 4, in <module>
import tensorflow
File "/private/var/folders/r7/bvmh1vvx41d63snvgbdz7bl40000gr/T/pip-build-N4LEY0/tensorflow/tensorflow-1.4.0.data/purelib/tensorflow/__init__.py",
line 24, in <module>
File "/private/var/folders/r7/bvmh1vvx41d63snvgbdz7bl40000gr/T/pip-build-N4LEY0/tensorflow/tensorflow-1.4.0.data/purelib/tensorflow/python/__init__.py",
line 49, in <module>
File "/private/var/folders/r7/bvmh1vvx41d63snvgbdz7bl40000gr/T/pip-build-N4LEY0/tensorflow/tensorflow-1.4.0.data/purelib/tensorflow/python/pywrap_tensorflow.py",
line 72, in <module>
ImportError: Traceback (most recent call last):
File "/private/var/folders/r7/bvmh1vvx41d63snvgbdz7bl40000gr/T/pip-build-N4LEY0/tensorflow/tensorflow-1.4.0.data/purelib/tensorflow/python/pywrap_tensorflow.py",
line 58, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "/private/var/folders/r7/bvmh1vvx41d63snvgbdz7bl40000gr/T/pip-build-N4LEY0/tensorflow/tensorflow-1.4.0.data/purelib/tensorflow/python/pywrap_tensorflow_internal.py",
line 28, in <module>
File "/private/var/folders/r7/bvmh1vvx41d63snvgbdz7bl40000gr/T/pip-build-N4LEY0/tensorflow/tensorflow-1.4.0.data/purelib/tensorflow/python/pywrap_tensorflow_internal.py",
line 20, in swig_import_helper
ImportError: No module named _pywrap_tensorflow_internal


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/install_sources#common_installation_problems

for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.

~$ unzip -l /tmp/tensorflow.zip | head
Archive: /tmp/tensorflow.zip
Length Date Time Name
--------- ---------- ----- ----
0 11-18-2017 23:32 tensorflow/
1481 11-18-2017 23:31 tensorflow/__init__.py
630 11-18-2017 23:32 tensorflow/__init__.pyc
0 11-18-2017 23:32 tensorflow/contrib/
3666 11-18-2017 23:31 tensorflow/contrib/__init__.py
3316 11-18-2017 23:32 tensorflow/contrib/__init__.pyc
0 11-18-2017 23:32 tensorflow/contrib/all_reduce/
>> >> >> email to discuss+u...@tensorflow.org.
>> >> >> To post to this group, send email to dis...@tensorflow.org.
>> >> >> To view this discussion on the web visit
>> >> >>
>> >> >>
>> >> >> https://groups.google.com/a/tensorflow.org/d/msgid/discuss/9f3e2d08-021f-4a63-96a6-6325a061537d%40tensorflow.org.
>> >>
>> >>
>> >>
>> >> --
>> >> Regards,
>> >> Peng
>> >>
>> >> --
>> >> You received this message because you are subscribed to the Google
>> >> Groups
>> >> "Discuss" group.
>> >> To unsubscribe from this group and stop receiving emails from it, send
>> >> an
>> >> email to discuss+u...@tensorflow.org.
>> >> To post to this group, send email to dis...@tensorflow.org.
>> >> To view this discussion on the web visit
>> >>
>> >> https://groups.google.com/a/tensorflow.org/d/msgid/discuss/CABrM6wmNFL05kgFiyDnJzta7LrhK%2Bij-0cs3M3RZwKaJpztO9A%40mail.gmail.com.
>> >
>> >
>>
>>
>>
>> --
>> Regards,
>> Peng
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Discuss" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to discuss+u...@tensorflow.org.
--
Regards,
Peng

Justine Tunney

unread,
Dec 11, 2017, 12:21:39 AM12/11/17
to Peng Yu, Yaroslav Bulatov, Jim Lloyd, Discuss
Oh that one's probably going to be a real doodle to do. If it was simple I would've done it by now.


>> >> >> To post to this group, send email to dis...@tensorflow.org.
>> >> >> To view this discussion on the web visit
>> >> >>
>> >> >>
>> >> >> https://groups.google.com/a/tensorflow.org/d/msgid/discuss/9f3e2d08-021f-4a63-96a6-6325a061537d%40tensorflow.org.
>> >>
>> >>
>> >>
>> >> --
>> >> Regards,
>> >> Peng
>> >>
>> >> --
>> >> You received this message because you are subscribed to the Google
>> >> Groups
>> >> "Discuss" group.
>> >> To unsubscribe from this group and stop receiving emails from it, send
>> >> an

>> >> To post to this group, send email to dis...@tensorflow.org.
>> >> To view this discussion on the web visit
>> >>
>> >> https://groups.google.com/a/tensorflow.org/d/msgid/discuss/CABrM6wmNFL05kgFiyDnJzta7LrhK%2Bij-0cs3M3RZwKaJpztO9A%40mail.gmail.com.
>> >
>> >
>>
>>
>>
>> --
>> Regards,
>> Peng
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Discuss" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
--
Regards,
Peng

Jason

unread,
Dec 11, 2017, 3:36:36 PM12/11/17
to Discuss, peng...@gmail.com
I assume the mport is bevause you are using TF to execute forward propigation?

One crazy thing that I do, is I don't user tensorflow for model execution. I dump the network weights to JSON, then execute the same matrix operations in whatever I need to. Currently, it's python, but I could do JS (Node, brower)  just as easily. I only use TF for training. I don't find TF's binary format of save/restore very portable, but it does allow you to save and load graphs opaquely. 

Also, if you're serving results, go ahead and use a persistent server (Flask, Tornado, other) to do the import, be fed the model and inputs over HTTP and return results? 

Reply all
Reply to author
Forward
0 new messages