• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Contains GPU utility functions."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21import collections
22import re
23
24
25# Matches the DeviceAttributes.physical_device_desc field.
26_PHYSICAL_DEVICE_DESCRIPTION_REGEX = re.compile(
27    r'name: ([^,]*), (?:.*compute capability: (\d+)\.(\d+))?')
28
29
30# compute_capability is a (major version, minor version) pair, or None if this
31# is not an Nvidia GPU.
32GpuInfo = collections.namedtuple('gpu_info', ['name', 'compute_capability'])
33
34
35def compute_capability_from_device_desc(device_attrs):
36  """Returns the GpuInfo given a DeviceAttributes proto.
37
38  Args:
39    device_attrs: A DeviceAttributes proto.
40
41  Returns
42    A gpu_info tuple. Both fields are None if `device_attrs` does not have a
43    valid physical_device_desc field.
44  """
45  # TODO(jingyue): The device description generator has to be in sync with
46  # this file. Another option is to put compute capability in
47  # DeviceAttributes, but I avoided that to keep DeviceAttributes
48  # target-independent. Reconsider this option when we have more things like
49  # this to keep in sync.
50  # LINT.IfChange
51  match = _PHYSICAL_DEVICE_DESCRIPTION_REGEX.search(
52      device_attrs.physical_device_desc)
53  # LINT.ThenChange(//tensorflow/core/common_runtime/gpu/gpu_device.cc)
54  if not match:
55    return GpuInfo(None, None)
56  cc = (int(match.group(2)), int(match.group(3))) if match.group(2) else None
57  return GpuInfo(match.group(1), cc)
58