This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose runtime feature detection in the public C API and in the Pytho…
…n API
- Loading branch information
Showing
4 changed files
with
150 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# 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. | ||
|
||
# coding: utf-8 | ||
# pylint: disable=invalid-name, no-member, trailing-comma-tuple, bad-mcs-classmethod-argument | ||
|
||
"""runtime detection of compile time features in the native library""" | ||
|
||
import ctypes | ||
import enum | ||
from .base import _LIB, check_call, mx_uint | ||
|
||
feature_names = [ | ||
"CUDA", | ||
"CUDNN", | ||
"NCCL", | ||
"CUDA_RTC", | ||
"TENSORRT", | ||
"CPU_SSE", | ||
"CPU_SSE2", | ||
"CPU_SSE3", | ||
"CPU_SSE4_1", | ||
"CPU_SSE4_2", | ||
"CPU_SSE4A", | ||
"CPU_AVX", | ||
"CPU_AVX2", | ||
"OPENMP", | ||
"SSE", | ||
"F16C", | ||
"JEMALLOC", | ||
"BLAS_OPEN", | ||
"BLAS_ATLAS", | ||
"BLAS_MKL", | ||
"BLAS_APPLE", | ||
"LAPACK", | ||
"MKLDNN", | ||
"OPENCV", | ||
"CAFFE", | ||
"PROFILER", | ||
"DIST_KVSTORE", | ||
"CXX14", | ||
"SIGNAL_HANDLER", | ||
"DEBUG" | ||
] | ||
|
||
|
||
Feature = enum.Enum('Feature', {name: index for index, name in enumerate(feature_names)}) | ||
|
||
|
||
def has_feature(feature): | ||
""" | ||
Check the library for compile-time feature at runtime | ||
Parameters | ||
---------- | ||
feature : int | ||
An integer representing the feature to check | ||
Returns | ||
------- | ||
boolean | ||
True if the feature is enabled, false otherwise | ||
""" | ||
res = ctypes.c_bool() | ||
check_call(_LIB.MXHasFeature(mx_uint(feature), ctypes.byref(res))) | ||
return res.value | ||
|
||
|
||
def features_enabled(): | ||
res = [] | ||
for f in Feature: | ||
if has_feature(f.value): | ||
res.append(f) | ||
return res | ||
|
||
def features_enabled_str(sep=', '): | ||
return sep.join(map(lambda x: x.name, features_enabled())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# 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. | ||
|
||
import mxnet as mx | ||
from mxnet.mxfeatures import * | ||
from nose.tools import * | ||
|
||
def test_runtime_features(): | ||
for f in Feature: | ||
res = has_feature(f.value) | ||
ok_(type(res) is bool) | ||
for f in features_enabled(): | ||
ok_(type(f) is Feature) | ||
ok_(type(features_enabled_str()) is str) | ||
print("Features enabled: {}".format(features_enabled_str())) | ||
|
||
|
||
if __name__ == "__main__": | ||
import nose | ||
nose.runmodule() |