Skip to content

Commit 5c78b9d

Browse files
author
Taylor Robie
authored
catch cpuinfo ImportError (tensorflow#4138)
* catch cpuinfo ImportError * add psutil import catch * fix typo
1 parent 369d198 commit 5c78b9d

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

official/utils/logs/logger.py

+19-13
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,18 @@ def _collect_cpu_info(run_info):
213213

214214
cpu_info["num_cores"] = multiprocessing.cpu_count()
215215

216-
# Note: cpuinfo is not installed in the TensorFlow OSS tree.
217-
# It is installable via pip.
218-
import cpuinfo # pylint: disable=g-import-not-at-top
216+
try:
217+
# Note: cpuinfo is not installed in the TensorFlow OSS tree.
218+
# It is installable via pip.
219+
import cpuinfo # pylint: disable=g-import-not-at-top
219220

220-
info = cpuinfo.get_cpu_info()
221-
cpu_info["cpu_info"] = info["brand"]
222-
cpu_info["mhz_per_cpu"] = info["hz_advertised_raw"][0] / 1.0e6
221+
info = cpuinfo.get_cpu_info()
222+
cpu_info["cpu_info"] = info["brand"]
223+
cpu_info["mhz_per_cpu"] = info["hz_advertised_raw"][0] / 1.0e6
223224

224-
run_info["machine_config"]["cpu_info"] = cpu_info
225+
run_info["machine_config"]["cpu_info"] = cpu_info
226+
except ImportError:
227+
tf.logging.warn("'cpuinfo' not imported. CPU info will not be logged.")
225228

226229

227230
def _collect_gpu_info(run_info):
@@ -243,12 +246,15 @@ def _collect_gpu_info(run_info):
243246

244247

245248
def _collect_memory_info(run_info):
246-
# Note: psutil is not installed in the TensorFlow OSS tree.
247-
# It is installable via pip.
248-
import psutil # pylint: disable=g-import-not-at-top
249-
vmem = psutil.virtual_memory()
250-
run_info["machine_config"]["memory_total"] = vmem.total
251-
run_info["machine_config"]["memory_available"] = vmem.available
249+
try:
250+
# Note: psutil is not installed in the TensorFlow OSS tree.
251+
# It is installable via pip.
252+
import psutil # pylint: disable=g-import-not-at-top
253+
vmem = psutil.virtual_memory()
254+
run_info["machine_config"]["memory_total"] = vmem.total
255+
run_info["machine_config"]["memory_available"] = vmem.available
256+
except ImportError:
257+
tf.logging.warn("'psutil' not imported. Memory info will not be logged.")
252258

253259

254260
def _parse_gpu_model(physical_device_desc):

0 commit comments

Comments
 (0)