This repository was archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
Scale quantile via DAAL #644
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
//***************************************************************************** | ||
// Copyright (c) 2020, Intel Corporation All rights reserved. | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are met: | ||
// | ||
// Redistributions of source code must retain the above copyright notice, | ||
// this list of conditions and the following disclaimer. | ||
// | ||
// Redistributions in binary form must reproduce the above copyright notice, | ||
// this list of conditions and the following disclaimer in the documentation | ||
// and/or other materials provided with the distribution. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | ||
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | ||
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
//***************************************************************************** | ||
|
||
#include <Python.h> | ||
#include <daal.h> | ||
|
||
|
||
extern "C" | ||
{ | ||
|
||
int test(int x) | ||
{ | ||
return x + 42; | ||
} | ||
|
||
double sum(double *p, int c) | ||
{ | ||
double result = 0.0; | ||
for (int i = 0; i < c; ++i) | ||
{ | ||
result += p[i]; | ||
} | ||
return result; | ||
} | ||
|
||
double quantile(int c, double *p, double q) | ||
{ | ||
using namespace daal; | ||
using namespace daal::algorithms; | ||
using namespace daal::data_management; | ||
|
||
quantiles::Batch<> algorithm; | ||
|
||
auto in_table = HomogenNumericTable<double>::create(p, 1, c); | ||
algorithm.input.set(quantiles::data, in_table); | ||
|
||
algorithm.parameter.quantileOrders->assign(q); | ||
|
||
algorithm.compute(); | ||
|
||
auto out_table = algorithm.getResult()->get(quantiles::quantiles); | ||
return out_table->getValue<double>(0, 0); | ||
} | ||
|
||
PyMODINIT_FUNC PyInit_daal() | ||
{ | ||
static struct PyModuleDef moduledef = { | ||
PyModuleDef_HEAD_INIT, | ||
"daal", | ||
"No docs", | ||
-1, | ||
NULL, | ||
}; | ||
PyObject* m = PyModule_Create(&moduledef); | ||
if (m == NULL) | ||
{ | ||
return NULL; | ||
} | ||
|
||
#define REGISTER(func) PyObject_SetAttrString(m, #func, PyLong_FromVoidPtr((void*)(&func))); | ||
REGISTER(test) | ||
REGISTER(sum) | ||
REGISTER(quantile) | ||
#undef REGISTER | ||
return m; | ||
} | ||
|
||
} // extern "C" |
This file contains hidden or 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,69 @@ | ||
# ***************************************************************************** | ||
# Copyright (c) 2020, Intel Corporation All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# | ||
# Redistributions of source code must retain the above copyright notice, | ||
# this list of conditions and the following disclaimer. | ||
# | ||
# Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | ||
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | ||
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
# ***************************************************************************** | ||
|
||
import numba | ||
|
||
from numba import types | ||
from numba.extending import overload | ||
|
||
# from numba import typing, generated_jit | ||
# from numba.extending import models, register_model | ||
# from numba.extending import lower_builtin, overload_method, intrinsic | ||
|
||
# from llvmlite import ir as lir | ||
import llvmlite.binding as ll | ||
|
||
from . import daal | ||
|
||
|
||
ll.add_symbol('test', daal.test) | ||
ll.add_symbol('sum', daal.sum) | ||
|
||
|
||
_test = types.ExternalFunction("test", types.int_(types.int_)) | ||
_sum = types.ExternalFunction("sum", types.float64(types.voidptr, types.int_)) | ||
|
||
|
||
def test(x): | ||
pass | ||
|
||
|
||
@overload(test) | ||
def test_overload(x): | ||
return lambda x: _test(x) | ||
|
||
|
||
import ctypes | ||
|
||
functype_test = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int) | ||
ctypes_test = functype_test(daal.test) | ||
|
||
# functype_sum = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.POINTER(ctypes.c_double), ctypes.c_int) | ||
functype_sum = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_void_p, ctypes.c_int) | ||
ctypes_sum = functype_sum(daal.sum) | ||
|
||
|
||
quantile = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_int, ctypes.c_void_p, ctypes.c_double)(daal.quantile) |
This file contains hidden or 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 hidden or 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 hidden or 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,77 @@ | ||
# ***************************************************************************** | ||
# Copyright (c) 2020, Intel Corporation All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# | ||
# Redistributions of source code must retain the above copyright notice, | ||
# this list of conditions and the following disclaimer. | ||
# | ||
# Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | ||
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | ||
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
# ***************************************************************************** | ||
|
||
import numpy as np | ||
import ctypes | ||
|
||
from sdc.tests.test_base import TestCase | ||
|
||
from sdc.daal_overloads import test, ctypes_test, ctypes_sum, quantile | ||
|
||
|
||
class TestDaal(TestCase): | ||
|
||
def test_test(self): | ||
def pyfunc(): | ||
return test(10) | ||
|
||
def ctypes_pyfunc(): | ||
return ctypes_test(10) | ||
|
||
cfunc = self.jit(pyfunc) | ||
ctypes_cfunc = self.jit(ctypes_pyfunc) | ||
# self.assertEqual(cfunc(), pyfunc()) | ||
self.assertEqual(cfunc(), ctypes_pyfunc()) | ||
self.assertEqual(ctypes_cfunc(), ctypes_pyfunc()) | ||
|
||
def test_sum(self): | ||
def pyfunc(arr): | ||
# return ctypes_sum(arr.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), len(arr)) | ||
return ctypes_sum(arr.ctypes, len(arr)) | ||
cfunc = self.jit(pyfunc) | ||
|
||
arr = np.arange(10, dtype=np.float64) | ||
expected = np.sum(arr) | ||
|
||
# print(ctypes_sum.argtypes) | ||
|
||
self.assertEqual(pyfunc(arr), expected) | ||
self.assertEqual(cfunc(arr), expected) | ||
|
||
def test_quantile(self): | ||
def pyfunc(arr, q): | ||
return quantile(len(arr), arr.ctypes, q) | ||
cfunc = self.jit(pyfunc) | ||
|
||
arr = np.arange(10, dtype=np.float64) | ||
|
||
# print(ctypes_sum.argtypes) | ||
|
||
for q in [0., 0.25, 0.5, 0.75, 1.]: | ||
with self.subTest(q=q): | ||
expected = np.quantile(arr, q) | ||
self.assertEqual(pyfunc(arr, q), expected) | ||
self.assertEqual(cfunc(arr, q), expected) |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should specify the data type of algorithm as a template parameter and provide data with the same type. Default type is used now but it is not clear what this type is. If you will provide the double data to the float algorithm then time will be spent on converting and copying.