Exemple #1
0
def _main():
    import argparse

    # Adding custom extend action for support all python versions
    class ExtendAction(argparse.Action):
        def __call__(self, parser, namespace, values, option_string=None):
            items = getattr(namespace, self.dest) or []
            items.extend(values)
            setattr(namespace, self.dest, items)

    parser = argparse.ArgumentParser(
        prog="python -m sklearnex.glob",
        description="""
            Patch all your Scikit-learn applications using Intel(R) Extension for
            scikit-learn.""",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.register('action', 'extend', ExtendAction)
    parser.add_argument('action', choices=["patch_sklearn", "unpatch_sklearn"],
                        help="Enable or Disable patching")
    parser.add_argument('--no-verbose', '-nv', action='store_false',
                        help="Disable additional information about enabling patching")
    parser.add_argument('--algorithm', '-a', action='extend', type=str, nargs="+",
                        help="The name of an algorithm to be patched globally")
    args = parser.parse_args()

    if args.action == "patch_sklearn":
        patch_sklearn(name=args.algorithm, verbose=args.no_verbose, global_patch=True)
    elif args.action == "unpatch_sklearn":
        unpatch_sklearn(global_unpatch=True)
    else:
        raise RuntimeError("Invalid choice for the action attribute."
                           " Expected: patch_sklearn or unpatch_sklearn."
                           f" Got {args.action}")
Exemple #2
0
def test_unpatch_by_list_many_estimators():
    sklearnex.patch_sklearn()

    from sklearn.ensemble import RandomForestRegressor
    from sklearn.neighbors import KNeighborsRegressor
    from sklearn.linear_model import LogisticRegression
    from sklearn.svm import SVC

    assert RandomForestRegressor.__module__.startswith('daal4py')
    assert KNeighborsRegressor.__module__.startswith('daal4py')
    assert LogisticRegression.__module__.startswith('daal4py')
    assert SVC.__module__.startswith('daal4py') or SVC.__module__.startswith(
        'sklearnex')

    sklearnex.unpatch_sklearn(["KNeighborsRegressor", "RandomForestRegressor"])

    from sklearn.ensemble import RandomForestRegressor
    from sklearn.neighbors import KNeighborsRegressor
    from sklearn.linear_model import LogisticRegression
    from sklearn.svm import SVC

    assert RandomForestRegressor.__module__.startswith('sklearn')
    assert KNeighborsRegressor.__module__.startswith('sklearn')
    assert LogisticRegression.__module__.startswith('daal4py')
    assert SVC.__module__.startswith('daal4py') or SVC.__module__.startswith(
        'sklearnex')
Exemple #3
0
def test_monkey_patching():
    _tokens = sklearnex.get_patch_names()
    _values = sklearnex.get_patch_map().values()
    _classes = list()

    for v in _values:
        for c in v:
            _classes.append(c[0])

    sklearnex.patch_sklearn()

    for i, _ in enumerate(_tokens):
        t = _tokens[i]
        p = _classes[i][0]
        n = _classes[i][1]

        class_module = getattr(p, n).__module__
        assert \
            class_module.startswith('daal4py') or class_module.startswith('sklearnex'), \
            "Patching has completed with error."

    for i, _ in enumerate(_tokens):
        t = _tokens[i]
        p = _classes[i][0]
        n = _classes[i][1]

        sklearnex.unpatch_sklearn(t)
        class_module = getattr(p, n).__module__
        assert class_module.startswith('sklearn'), \
            "Unpatching has completed with error."

    sklearnex.unpatch_sklearn()

    for i, _ in enumerate(_tokens):
        t = _tokens[i]
        p = _classes[i][0]
        n = _classes[i][1]

        class_module = getattr(p, n).__module__
        assert class_module.startswith('sklearn'), \
            "Unpatching has completed with error."

    sklearnex.unpatch_sklearn()

    for i, _ in enumerate(_tokens):
        t = _tokens[i]
        p = _classes[i][0]
        n = _classes[i][1]

        sklearnex.patch_sklearn(t)

        class_module = getattr(p, n).__module__
        assert \
            class_module.startswith('daal4py') or class_module.startswith('sklearnex'), \
            "Patching has completed with error."

    sklearnex.unpatch_sklearn()
Exemple #4
0
def test_monkey_patching():
    _tokens = sklearnex.get_patch_names()
    _values = sklearn_patch_map().values()
    _classes = list()
    for v in _values:
        _classes.append(v[0][0])

    assert len(_tokens) == len(_classes)
    assert isinstance(_tokens, list) and len(_tokens) > 0, \
        "Internal Error: list of patched names has unexcepable format."

    sklearnex.patch_sklearn()

    for i, _ in enumerate(_tokens):
        t = _tokens[i]
        p = _classes[i][0]
        n = _classes[i][1]

        class_module = getattr(p, n).__module__
        assert class_module.startswith('daal4py'), \
            "Patching has completed with error."

        sklearnex.unpatch_sklearn(t)
        print(p, n)
        class_module = getattr(p, n).__module__
        assert class_module.startswith('sklearn'), \
            "Unpatching has completed with error."

    sklearnex.unpatch_sklearn()

    for i, _ in enumerate(_tokens):
        t = _tokens[i]
        p = _classes[i][0]
        n = _classes[i][1]

        class_module = getattr(p, n).__module__
        assert class_module.startswith('sklearn'), \
            "Unpatching has completed with error."

        sklearnex.patch_sklearn(t)

        class_module = getattr(p, n).__module__
        assert class_module.startswith('daal4py'), \
            "Patching has completed with error."

    sklearnex.unpatch_sklearn()
err_code = subprocess.call(
    [sys.executable, "-m", "sklearnex.glob", "patch_sklearn", "-a", "svc"])
assert not err_code
from sklearn.svm import SVC, SVR
assert SVC.__module__.startswith('daal4py') or SVC.__module__.startswith(
    'sklearnex')
assert not SVR.__module__.startswith('daal4py') and \
       not SVR.__module__.startswith('sklearnex')

from sklearnex import patch_sklearn, unpatch_sklearn

# test unpatching from command line
err_code = subprocess.call(
    [sys.executable, "-m", "sklearnex.glob", "unpatch_sklearn"])
assert not err_code
unpatch_sklearn()
from sklearn.svm import SVC, SVR
assert not SVR.__module__.startswith('daal4py') and \
       not SVR.__module__.startswith('sklearnex')
assert not SVR.__module__.startswith('daal4py') and \
       not SVR.__module__.startswith('sklearnex')

# test patching from function
patch_sklearn(name=['svc'], global_patch=True)
from sklearn.svm import SVC, SVR
assert SVC.__module__.startswith('daal4py') or \
       SVC.__module__.startswith('sklearnex')
assert not SVR.__module__.startswith('daal4py') and \
       not SVR.__module__.startswith('sklearnex')

# test unpatching from function
Exemple #6
0
print(
    f"Execution time with Intel(R) Extension for Scikit-learn: {(time() - start):.2f} s"
)

# Make predictions with SVC classifier and print a report of the main classification metrics:

predicted = classifier.predict(x_test)
report = metrics.classification_report(y_test, predicted)
print(
    f"Classification report for SVC trained with Intel(R) extension for Scikit-learn:\n{report}\n"
)

# To cancel optimizations, use *unpatch_sklearn* and reimport the SVC class.

from sklearnex import unpatch_sklearn
unpatch_sklearn()

# "Train SVC algorithm with original scikit-learn:"

start = time()
from sklearn.svm import SVC
classifier = SVC(**params).fit(x_train, y_train)
print(
    f"Execution time with the original Scikit-learn: {(time() - start):.2f} s")

# Make predictions with SVC classifier and print a report of the main classification metrics:

predicted = classifier.predict(x_test)
report = metrics.classification_report(y_test, predicted)
print(
    f"Classification report for SVC trained with the original scikit-learn:\n{report}\n"