|
| 1 | +# cython: language_level=3 |
| 2 | +# distutils: language = c++ |
| 3 | +# -*- coding: utf-8 -*- |
| 4 | +# ***************************************************************************** |
| 5 | +# Copyright (c) 2023, Intel Corporation |
| 6 | +# All rights reserved. |
| 7 | +# |
| 8 | +# Redistribution and use in source and binary forms, with or without |
| 9 | +# modification, are permitted provided that the following conditions are met: |
| 10 | +# - Redistributions of source code must retain the above copyright notice, |
| 11 | +# this list of conditions and the following disclaimer. |
| 12 | +# - Redistributions in binary form must reproduce the above copyright notice, |
| 13 | +# this list of conditions and the following disclaimer in the documentation |
| 14 | +# and/or other materials provided with the distribution. |
| 15 | +# |
| 16 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 26 | +# THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | +# ***************************************************************************** |
| 28 | + |
| 29 | + |
| 30 | +import dpnp |
| 31 | +from dpnp.dpnp_array import dpnp_array |
| 32 | +from dpnp.dpnp_utils import ( |
| 33 | + get_usm_allocations |
| 34 | +) |
| 35 | + |
| 36 | +import dpctl |
| 37 | +import dpctl.tensor as dpt |
| 38 | +import dpctl.tensor._tensor_impl as ti |
| 39 | + |
| 40 | + |
| 41 | +__all__ = [ |
| 42 | + "dpnp_cov" |
| 43 | +] |
| 44 | + |
| 45 | +def dpnp_cov(m, y=None, rowvar=True, dtype=None): |
| 46 | + """ |
| 47 | + Estimate a covariance matrix based on passed data. |
| 48 | + No support for given weights is provided now. |
| 49 | +
|
| 50 | + The implementation is done through existing dpnp and dpctl methods |
| 51 | + instead of separate function call of dpnp backend. |
| 52 | +
|
| 53 | + """ |
| 54 | + |
| 55 | + def _get_2dmin_array(x, dtype): |
| 56 | + """ |
| 57 | + Transform an input array to a form required for building a covariance matrix. |
| 58 | +
|
| 59 | + If applicable, it reshapes the input array to have 2 dimensions or greater. |
| 60 | + If applicable, it transposes the input array when 'rowvar' is False. |
| 61 | + It casts to another dtype, if the input array differs from requested one. |
| 62 | +
|
| 63 | + """ |
| 64 | + |
| 65 | + if x.ndim == 0: |
| 66 | + x = x.reshape((1, 1)) |
| 67 | + elif x.ndim == 1: |
| 68 | + x = x[dpnp.newaxis, :] |
| 69 | + |
| 70 | + if not rowvar and x.shape[0] != 1: |
| 71 | + x = x.T |
| 72 | + |
| 73 | + if x.dtype != dtype: |
| 74 | + x = dpnp.astype(x, dtype) |
| 75 | + return x |
| 76 | + |
| 77 | + |
| 78 | + # input arrays must follow CFD paradigm |
| 79 | + usm_type, queue = get_usm_allocations((m, ) if y is None else (m, y)) |
| 80 | + |
| 81 | + # calculate a type of result array if not passed explicitly |
| 82 | + if dtype is None: |
| 83 | + dtypes = [m.dtype, dpnp.default_float_type(sycl_queue=queue)] |
| 84 | + if y is not None: |
| 85 | + dtypes.append(y.dtype) |
| 86 | + dtype = dpt.result_type(*dtypes) |
| 87 | + |
| 88 | + X = _get_2dmin_array(m, dtype) |
| 89 | + if y is not None: |
| 90 | + y = _get_2dmin_array(y, dtype) |
| 91 | + |
| 92 | + # TODO: replace with dpnp.concatenate((X, y), axis=0) once dpctl implementation is ready |
| 93 | + if X.ndim != y.ndim: |
| 94 | + raise ValueError("all the input arrays must have same number of dimensions") |
| 95 | + |
| 96 | + if X.shape[1:] != y.shape[1:]: |
| 97 | + raise ValueError("all the input array dimensions for the concatenation axis must match exactly") |
| 98 | + |
| 99 | + res_shape = tuple(X.shape[i] if i > 0 else (X.shape[i] + y.shape[i]) for i in range(X.ndim)) |
| 100 | + res_usm = dpt.empty(res_shape, dtype=dtype, usm_type=usm_type, sycl_queue=queue) |
| 101 | + |
| 102 | + # concatenate input arrays 'm' and 'y' into single array among 0-axis |
| 103 | + hev1, _ = ti._copy_usm_ndarray_into_usm_ndarray(src=X.get_array(), dst=res_usm[:X.shape[0]], sycl_queue=queue) |
| 104 | + hev2, _ = ti._copy_usm_ndarray_into_usm_ndarray(src=y.get_array(), dst=res_usm[X.shape[0]:], sycl_queue=queue) |
| 105 | + dpctl.SyclEvent.wait_for([hev1, hev2]) |
| 106 | + |
| 107 | + X = dpnp_array._create_from_usm_ndarray(res_usm) |
| 108 | + |
| 109 | + avg = X.mean(axis=1) |
| 110 | + |
| 111 | + fact = X.shape[1] - 1 |
| 112 | + X -= avg[:, None] |
| 113 | + |
| 114 | + c = dpnp.dot(X, X.T.conj()) |
| 115 | + c *= 1 / fact if fact != 0 else dpnp.nan |
| 116 | + |
| 117 | + return dpnp.squeeze(c) |
0 commit comments