larroy commented on a change in pull request #14130: Refine runtime feature 
discovery python API and add documentation to …
URL: https://github.com/apache/incubator-mxnet/pull/14130#discussion_r256275625
 
 

 ##########
 File path: python/mxnet/runtime.py
 ##########
 @@ -28,21 +29,48 @@ class LibFeature(ctypes.Structure):
     Compile time feature description
     """
     _fields_ = [
-        ("name", ctypes.c_char_p),
+        ("_name", ctypes.c_char_p),
         ("index", ctypes.c_uint32),
         ("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 libinfo_features():
     """
     Check the library for compile-time features. The list of features are 
maintained in libinfo.h and libinfo.cc
 
     Returns
     -------
-    A list of class LibFeature indicating which features are available and 
enabled
+    :return: list of class LibFeature indicating which features are available 
and enabled
     """
     lib_features = ctypes.POINTER(LibFeature)()
     lib_features_size = ctypes.c_size_t()
     check_call(_LIB.MXLibInfoFeatures(ctypes.byref(lib_features), 
ctypes.byref(lib_features_size)))
     feature_list = [lib_features[i] for i in range(lib_features_size.value)]
     return feature_list
+
+def is_enabled(tocheck):
+    """
+    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_dict = {f.name: f.enabled for f in libinfo_features()}
+    if tocheck not in feature_dict:
+        raise RuntimeError("Feature '{}' is unknown, known features are: 
{}".format(tocheck, list(feature_dict.keys())))
+    return feature_dict[tocheck]
 
 Review comment:
   I see, that looks nice. On the upside repeated calls are more efficient than 
my proposal. On the downside I think the primary case which is to check if a 
feature is enabled is less ergonomic due to the syntactic sugar. I think a 
simple function with a single arg is always clearer.  A good point is that it 
should be case insensitive. I'm a bit torn. I will refine it a bit more.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to