STT-tensorflow/tensorflow/python/framework/gpu_util.py
Deven Desai e8360cd9b2 [ROCm] Fix for the broken ROCm CSB - 191223
The following commit breaks the ROCm CSB

b32c69cb54

It leads to the following runtime error in many tests

```
Traceback (most recent call last):
  File "/home/jenkins/workspace/tensorflow-rocm-nightly/bazel-ci_build-cache/.cache/bazel/_bazel_jenkins/eab0d61a99b6696edb3d2aff87b585e8/execroot/org_tensorflow/bazel-out/k8-opt/bin/tensorflow/python/config_test_gpu.runfiles/org_tensorflow/tensorflow/python/framework/test_util.py", line 1377, in decorated
    if not is_gpu_available():
  File "/home/jenkins/workspace/tensorflow-rocm-nightly/bazel-ci_build-cache/.cache/bazel/_bazel_jenkins/eab0d61a99b6696edb3d2aff87b585e8/execroot/org_tensorflow/bazel-out/k8-opt/bin/tensorflow/python/config_test_gpu.runfiles/org_tensorflow/tensorflow/python/util/deprecation.py", line 324, in new_func
    return func(*args, **kwargs)
  File "/home/jenkins/workspace/tensorflow-rocm-nightly/bazel-ci_build-cache/.cache/bazel/_bazel_jenkins/eab0d61a99b6696edb3d2aff87b585e8/execroot/org_tensorflow/bazel-out/k8-opt/bin/tensorflow/python/config_test_gpu.runfiles/org_tensorflow/tensorflow/python/framework/test_util.py", line 1494, in is_gpu_available
    gpu_info = gpu_util.compute_capability_from_device_desc(local_device)
  File "/home/jenkins/workspace/tensorflow-rocm-nightly/bazel-ci_build-cache/.cache/bazel/_bazel_jenkins/eab0d61a99b6696edb3d2aff87b585e8/execroot/org_tensorflow/bazel-out/k8-opt/bin/tensorflow/python/config_test_gpu.runfiles/org_tensorflow/tensorflow/python/framework/gpu_util.py", line 56, in compute_capability_from_device_desc
    cc = int(match.group(2)), int(match.group(3)) if match.group(2) else None
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

```

This is because, the way it is written, the `if` check in file `gpu_util.py:56`
applies only to `int(match.group(3))` instead of to the tuple
`int(match.group(2)), int(match.group(3))`, which I believe is the intent here.

This results in runtime failures on ROCm platform where `match.group(2)` is indeed `None`

The fix is to make the tuple explicit to ensure the if condition is applied as intended
2019-12-23 16:22:25 +00:00

58 lines
2.1 KiB
Python

# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Contains GPU utility functions."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import collections
import re
# Matches the DeviceAttributes.physical_device_desc field.
_PHYSICAL_DEVICE_DESCRIPTION_REGEX = re.compile(
r'name: ([^,]*), (?:.*compute capability: (\d+)\.(\d+))?')
# compute_capability is a (major version, minor version) pair, or None if this
# is not an Nvidia GPU.
GpuInfo = collections.namedtuple('gpu_info', ['name', 'compute_capability'])
def compute_capability_from_device_desc(device_attrs):
"""Returns the GpuInfo given a DeviceAttributes proto.
Args:
device_attrs: A DeviceAttributes proto.
Returns
A gpu_info tuple. Both fields are None if `device_attrs` does not have a
valid physical_device_desc field.
"""
# TODO(jingyue): The device description generator has to be in sync with
# this file. Another option is to put compute capability in
# DeviceAttributes, but I avoided that to keep DeviceAttributes
# target-independent. Reconsider this option when we have more things like
# this to keep in sync.
# LINT.IfChange
match = _PHYSICAL_DEVICE_DESCRIPTION_REGEX.search(
device_attrs.physical_device_desc)
# LINT.ThenChange(//tensorflow/core/common_runtime/gpu/gpu_device.cc)
if not match:
return GpuInfo(None, None)
cc = (int(match.group(2)), int(match.group(3))) if match.group(2) else None
return GpuInfo(match.group(1), cc)