11

Related: Import TensorFlow contrib module is slow in TensorFlow 1.2.1 also: What can cause the TensorFlow import to be so slow?

I am using an ssd and importing TensorFlow. I have 4 ghz 8 core pc with 16 gb ram (Processor AMD FX(tm)-8350 Eight-Core Processor, 4000 Mhz, 4 Core(s), 8 Logical Processor(s)). TensorFlow takes 10-12 seconds to import.

Is there any way to selectively import parts of TensorFlow?

Would a RAM disk help?

Is there any more work being done on stuff like this or: Slow to import tensorflow.contrib with Python 3 because inspect.stack is slow #11829?

Edit: Python 3.6.8 :: Anaconda, Inc. on windoze 8.1. Dos box, cygwin bash are slow at 12 seconds. Vs code bash/power shell is fastest at 8 seconds. Importing in a .py file like: import tensorflow as tf. Not sure what environments are.

Edit 2:

PS D:\ray\dev\ml\ss1> conda info --envs
# conda environments:
#
base                  *  D:\Anaconda3
first                    D:\Anaconda3\envs\first
                         d:\Anaconda3
first                    d:\Anaconda3\envs\first

Edit 3: Using the code below, i get 9-10 seconds in a command prompt:

(tf2) D:\ray\dev\ml\ss1>python timeimport.py
 import tensorflow: 1 units, 9.796 seconds. 0.1 units/second.
version: 2.0.0

(tf2) D:\ray\dev\ml\ss1>python timeimport.py
 import tensorflow: 1 units, 9.448 seconds. 0.11 units/second.
version: 2.0.0

(tf2) D:\ray\dev\ml\ss1>python timeimport.py
 import tensorflow: 1 units, 9.421 seconds. 0.11 units/second.
version: 2.0.0


from __future__ import absolute_import, division, print_function, unicode_literals
from contextlib import contextmanager
from timeit import default_timer as timer
@contextmanager
def timing(description: str,units=1,title="",before="") -> None:
    if before!="":
        print(before,flush=True)
    start = timer()
    yield
    dt = timer() - start
    frequency=0 if units is None else (units/dt)
    if units is None:
        if title is None: print(f"{description}: {dt} seconds.",flush=True)
        else: print(f"{title} {description}: {dt} seconds.",flush=True)
    else: 
        #"{0:.2f}".format(a)
        dt=round(dt,3)
        frequency=round(frequency,2)
        print(f"{title} {description}: {str(units)} units, {dt} seconds. {str(frequency)} units/second.",flush=True) 
    return dt
with timing("import tensorflow",1):
    import tensorflow as tf
print("version:",tf.__version__)

Edit 4: turning of windows degender, i get 8-9 seconds instead of 9-10 seconds.

Edit 5: i found a workaround:

make a notebook with:

import tensorflow as tf
print(tf.__version__)
import tensorflow_datasets as tfds
import code.py

then in your code.py:

print("enter imported code")
import tensorflow as tf
print(tf.__version__)

# !pip install -q tensorflow-datasets
import tensorflow_datasets as tfds
import matplotlib.pyplot as plt
import numpy as np

#tfds.list_builders()
ds = tfds.load('mnist', split='train', shuffle_files=True)
...

so you run the notebook once and it takes 10 seconds. next time it goes like the wind.

Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
  • 1
    For my linux machine, first time import is always slow, but any successive imports (i.e. across different Python CLI instances) is immediate. Is similar behavior observed on your end too? – Rahul Bharadwaj Mar 20 '20 at 12:50
  • 1
    i have windoze. please see edit 3. – Ray Tayek Mar 20 '20 at 13:10
  • Can you try to disable windows defender and any other antivirus and benchmark again? Also, are you using the windows susbsistem for linux (WSL)? – BlackBear Mar 26 '20 at 13:28
  • win 8.1 - afaik, i am not using any windows subsystem. – Ray Tayek Mar 26 '20 at 14:12
  • i found a workaround, please see edit 5. – Ray Tayek Sep 05 '20 at 01:37
  • @RahulBharadwaj Yes, I observed same behavior. Very first import is slow, subsequent imports are fast. I see this on Container Optimized OS instances running in GCP `c2-standard-8`. Did you find workaround? – Gillespie Nov 05 '22 at 04:00

2 Answers2

0

I want to start off by saying that I'm using a 3 Ghz quad core and it does not take me any where near ten seconds to import TensorFlow in Python. Could you elaborate on what environment you're having issues importing it with (i.e. Windows/Mac/Linux in terminal/console/command prompt/Anaconda etc.)? You didn't specify how you are trying to import Tensorflow, but considering you tagged it with python-3.x I'm assuming you are importing Tensorflow with Python. I'm sure this won't be a popular answer but maybe consider using Tensorflow with a compiled language like c++. It is well known that interpreted languages such as Python are considerably slower than compiled languages and if speed is paramount then it would seem obvious to use TensorFlow in its native language.

siditious
  • 71
  • 8
-1

When I import tensorflow from a Thinkpad T560(i7 6600U with integrated GPU) in my office, it takes over 10 secs(about 15 secs). But recently I got a new Laptop Lenovo Y7000 at home(i7 10750H and GTX 1650 Card, SAMSUNG SSD), and I installed CUDA dependencies and able to run TensorFlow with CUDA successfully.

I would say now I feel like I switch from an old carriage to a bullet train.

Harrison Wang
  • 146
  • 1
  • 5
  • nice cpu, yours is about two times better than mine: https://www.cpubenchmark.net/compare/AMD-FX-8350-Eight-Core-vs-Intel-i7-10750H/1780vs3657 - i recently managed to get tf 2 installed again (2.1.0), but it's still takes 10 seconds to import https://stackoverflow.com/questions/62376660/installing-tensorflow-2-gets-a-dll-failed-to-load-in-pywrap-tensorflow-py – Ray Tayek Jun 17 '20 at 20:30