-
Notifications
You must be signed in to change notification settings - Fork 608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
scale function(_get_mean_var) updated for dense array, speedup upto ~4.65x #3099
Changes from 13 commits
65e7951
6d1f278
37ef6ac
34da6fb
f080c7f
e7a4662
b6b6139
06c0968
d244924
53d40dd
63404f2
9254a97
357bfbc
7a1a62e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -33,14 +33,55 @@ | |||||
def _get_mean_var( | ||||||
X: _SupportedArray, *, axis: Literal[0, 1] = 0 | ||||||
) -> tuple[NDArray[np.float64], NDArray[np.float64]]: | ||||||
if isinstance(X, sparse.spmatrix): | ||||||
mean, var = sparse_mean_variance_axis(X, axis=axis) | ||||||
if isinstance(X, np.ndarray): | ||||||
n_threads = numba.get_num_threads() | ||||||
mean, var = _compute_mean_var(X, axis=axis, n_threads=n_threads) | ||||||
else: | ||||||
mean = axis_mean(X, axis=axis, dtype=np.float64) | ||||||
mean_sq = axis_mean(elem_mul(X, X), axis=axis, dtype=np.float64) | ||||||
var = mean_sq - mean**2 | ||||||
# enforce R convention (unbiased estimator) for variance | ||||||
var *= X.shape[axis] / (X.shape[axis] - 1) | ||||||
if isinstance(X, sparse.spmatrix): | ||||||
mean, var = sparse_mean_variance_axis(X, axis=axis) | ||||||
else: | ||||||
mean = axis_mean(X, axis=axis, dtype=np.float64) | ||||||
mean_sq = axis_mean(elem_mul(X, X), axis=axis, dtype=np.float64) | ||||||
var = mean_sq - mean**2 | ||||||
# enforce R convention (unbiased estimator) for variance | ||||||
var *= X.shape[axis] / (X.shape[axis] - 1) | ||||||
return mean, var | ||||||
|
||||||
|
||||||
@numba.njit(cache=True, parallel=True) | ||||||
def _compute_mean_var( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can rename the kernel |
||||||
X: _SupportedArray, axis: Literal[0, 1] = 0, n_threads=1 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think |
||||||
) -> tuple[NDArray[np.float64], NDArray[np.float64]]: | ||||||
if axis == 0: | ||||||
axis_i = 1 | ||||||
sums = np.zeros((n_threads, X.shape[axis_i]), dtype=np.float64) | ||||||
sums_squared = np.zeros((n_threads, X.shape[axis_i]), dtype=np.float64) | ||||||
mean = np.zeros(X.shape[axis_i], dtype=np.float64) | ||||||
var = np.zeros(X.shape[axis_i], dtype=np.float64) | ||||||
n = X.shape[axis] | ||||||
for i in numba.prange(n_threads): | ||||||
for r in range(i, n, n_threads): | ||||||
for c in range(X.shape[axis_i]): | ||||||
value = np.float64(X[r, c]) | ||||||
ashish615 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
sums[i, c] += value | ||||||
sums_squared[i, c] += value * value | ||||||
ashish615 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
for c in numba.prange(X.shape[axis_i]): | ||||||
sum_ = sums[:, c].sum() | ||||||
mean[c] = sum_ / n | ||||||
var[c] = (sums_squared[:, c].sum() - sum_ * sum_ / n) / (n - 1) | ||||||
else: | ||||||
axis_i = 0 | ||||||
mean = np.zeros(X.shape[axis_i], dtype=np.float64) | ||||||
var = np.zeros(X.shape[axis_i], dtype=np.float64) | ||||||
for r in numba.prange(X.shape[0]): | ||||||
for c in range(X.shape[1]): | ||||||
value = np.float64(X[r, c]) | ||||||
ashish615 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
mean[r] += value | ||||||
var[r] += value * value | ||||||
for c in numba.prange(X.shape[0]): | ||||||
mean[c] = mean[c] / X.shape[1] | ||||||
var[c] = (var[c] - mean[c] ** 2) / (X.shape[1] - 1) | ||||||
|
||||||
return mean, var | ||||||
|
||||||
|
||||||
|
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.
Before your change, this line ran unconditionally, now it only runs for the
not isinstance(X, np.ndarray)
case. Is that intentional? Then you should mention that in_compute_mean_var
’s docstring.