Files
Pedro Larroy 3a6fe22b66 Refine runtime feature discovery python API and add documentation to … (#14130)
* Refine runtime feature discovery python API and add documentation to Python API docs

* Fix lint

* Provide is_enabled method to check if feature is present from string

* Refine docs, add is_enabled

* Fix encoding

* Fix doc

* Address CR suggestions

* remove index as per CR suggestion

* Fix lint

* runtime

* Fix doc

* Add license
2019-02-18 11:29:47 -08:00

88 lines
2.8 KiB
Python

# coding: utf-8
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
# pylint: disable=not-an-iterable
"""runtime querying of compile time features in the native library"""
import ctypes
import collections
from .base import _LIB, check_call
class Feature(ctypes.Structure):
"""
Compile time feature description
"""
_fields_ = [
("_name", ctypes.c_char_p),
("enabled", ctypes.c_bool)
]
@property
def name(self):
return self._name.decode()
def __repr__(self):
if self.enabled:
return "✔ {}".format(self.name)
else:
return "✖ {}".format(self.name)
def feature_list():
"""
Check the library for compile-time features. The list of features are maintained in libinfo.h and libinfo.cc
Returns
-------
:return: list of class LibFeature indicating which features are available and enabled
"""
lib_features_c_array = ctypes.POINTER(Feature)()
lib_features_size = ctypes.c_size_t()
check_call(_LIB.MXLibInfoFeatures(ctypes.byref(lib_features_c_array), ctypes.byref(lib_features_size)))
features = [lib_features_c_array[i] for i in range(lib_features_size.value)]
return features
class Features(collections.OrderedDict):
"""
OrderedDict of name to Feature
"""
def __init__(self):
super(Features, self).__init__([(f.name, f) for f in feature_list()])
def __repr__(self):
return str(list(self.values()))
def is_enabled(self, feature_name):
"""
Check for a particular feature by name
Parameters
----------
:param x: str The name of a valid feature as string for example 'CUDA'
Returns
-------
:return: bool True if it's enabled, False if it's disabled, RuntimeError if the feature is not known
"""
feature_name = feature_name.upper()
if feature_name not in self:
raise RuntimeError("Feature '{}' is unknown, known features are: {}".format(
feature_name, list(self.keys())))
return self[feature_name].enabled