Skip to content

[MRG] ENH: Added block_size parameter for lesser memory consumption - #7979

Closed
dalmia wants to merge 39 commits into
scikit-learn:masterfrom
dalmia:7287
Closed

dalmia wants to merge 39 commits into
scikit-learn:masterfrom
dalmia:7287

Conversation

@dalmia

@dalmia dalmia commented Dec 5, 2016 •

Copy link
Copy Markdown
Contributor

Reference Issue

Fixes #7287

What does this implement/fix? Explain your changes.

This intends to add a function pairwise_distances_blockwise with an additional block_size parameter to avoid storing all the O(n^2) pairs' distances. This would then be used by neighbors, silhouette_score and pairwise_distances_argmin_min.

Any other comments?

  • Stacking within pairwise_distances_reduce
  • Revert changes to sklearn/neighbors
  • Docstring for pairwise_distances_reduce
  • Tests for pairwise_distances_reduce

@jnothman

jnothman commented Dec 5, 2016

Copy link
Copy Markdown
Member

I'd think the main thing needing work in the docstring is the return value: we can't return a monolithic matrix.

@dalmia

dalmia commented Dec 6, 2016

Copy link
Copy Markdown
Contributor Author

Yes, that needs to be changed. But since it's depending on the block_size, I am unable to assign a specific value to it. Any suggestion that you may have?
The work-around I could think of was to assign n_samples_x and n_samples_y which depend on the block_size parameter?

@jnothman

jnothman commented Dec 6, 2016 via email

Copy link
Copy Markdown
Member

@dalmia dalmia changed the title [WIP] ENH: Added template for pairwise_distances_blockwise with docstring c… [WIP] ENH: Added block_size parameter for lesser memory consumption Dec 6, 2016
@dalmia

dalmia commented Dec 6, 2016

Copy link
Copy Markdown
Contributor Author

@jnothman I currently don't have any experience with multithreading(in practise) but am able to understand most of the parts of your changes in #7177. So, do you suggest I follow up from your changes to get started and then try to fix if something breaks?

@jnothman

jnothman commented Dec 6, 2016 via email

Copy link
Copy Markdown
Member

@dalmia

dalmia commented Dec 7, 2016

Copy link
Copy Markdown
Contributor Author

@jnothman I added the code for the generator and checked it by running an example. It seemed to work fine. I could not add tests/ check with nosetests because of the following error I am getting on running nosetests:

======================================================================
ERROR: Failure: ValueError (numpy.dtype has the wrong size, try recompiling. Expected 88, got 96)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/nose/loader.py", line 414, in loadTestsFromName
    addr.filename, addr.module)
  File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/media/aman/BE66ECBA66EC7515/Open Source/scikit-learn/sklearn/__init__.py", line 57, in <module>
    from .base import clone
  File "/media/aman/BE66ECBA66EC7515/Open Source/scikit-learn/sklearn/base.py", line 12, in <module>
    from .utils.fixes import signature
  File "/media/aman/BE66ECBA66EC7515/Open Source/scikit-learn/sklearn/utils/__init__.py", line 10, in <module>
    from .murmurhash import murmurhash3_32
  File "__init__.pxd", line 155, in init sklearn.utils.murmurhash (sklearn/utils/murmurhash.c:6319)
ValueError: numpy.dtype has the wrong size, try recompiling. Expected 88, got 96

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)

Please help me with this error.

@jnothman

jnothman commented Dec 7, 2016 via email

Copy link
Copy Markdown
Member

@dalmia

dalmia commented Dec 8, 2016 •

Copy link
Copy Markdown
Contributor Author

I still get the same error. I get this while compiling:

 #warning "Using deprecated NumPy API, disable it by " \

Any suggestions? @jnothman

@dalmia

dalmia commented Dec 8, 2016

Copy link
Copy Markdown
Contributor Author

It's fixed now.

@jnothman

jnothman commented Dec 8, 2016

Copy link
Copy Markdown
Member

flake8 errors

@dalmia

dalmia commented Dec 8, 2016 •

Copy link
Copy Markdown
Contributor Author

Yes, I had noticed those errors before committing. But I didn't change it as I had just copied the docstring from the pairwise_distances function. Is there someway of overriding the flake8 errors? Because the same line in pairwise_distances function doesn't give this error:

    Y : array [n_samples_b, n_features], optional
        An optional second feature array. Only allowed if metric != "precomputed".

@jnothman

jnothman commented Dec 8, 2016

Copy link
Copy Markdown
Member

flake8 is run in diff mode so that we only encourage you to fix errors you have introduced to the code (although it doesn't catch everything). You are still required to fix those issues.

@dalmia dalmia changed the title [WIP] ENH: Added block_size parameter for lesser memory consumption [MRG] ENH: Added block_size parameter for lesser memory consumption Dec 8, 2016
@dalmia

dalmia commented Dec 8, 2016

Copy link
Copy Markdown
Contributor Author

I have added the tests for pairwise_distances_blockwise. I couldn't think of a better way of testing for invalid block size in a generator. Please review.
Thanks.

@dalmia

dalmia commented Dec 10, 2016

Copy link
Copy Markdown
Contributor Author

ping @jnothman @amueller @lesteve

Comment thread sklearn/metrics/pairwise.py Outdated

pairwise_distances(X, y, metric, n_jobs)

but uses much less memory.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"but may use less memory"

Comment thread sklearn/metrics/pairwise.py Outdated

def pairwise_distances_blockwise(X, Y=None, metric='euclidean', n_jobs=1,
block_size=DEFAULT_BLOCK_SIZE, **kwds):
""" Compute the distance matrix from a vector array X and optional Y.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove space between """ and text (PEP257)

Comment thread sklearn/metrics/pairwise.py Outdated

Returns
-------
D : generator of blocks based on the ``block_size`` parameter. The blocks,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first line is reserved for type description

Comment thread sklearn/metrics/pairwise.py Outdated

Parameters
----------
X : array [n_samples_a, n_samples_a] if metric == "precomputed", or, \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove , after "or"

Comment thread sklearn/metrics/pairwise.py Outdated

This method takes either a vector array or a distance matrix, and returns
a distance matrix. If the input is a vector array, the distances are
computed. If the input is a distances matrix, it is returned instead.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"returned in blocks"

Comment thread sklearn/metrics/pairwise.py Outdated
block_size=DEFAULT_BLOCK_SIZE, **kwds):
""" Compute the distance matrix from a vector array X and optional Y.

This method takes either a vector array or a distance matrix, and returns

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"returns" -> "generates blocks of"


def test_pairwise_distances_blockwise_invalid_block_size():
rng = np.random.RandomState(0)
X = rng.random_sample((400, 4))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might as well be empty or zeros

Comment thread sklearn/metrics/tests/test_pairwise.py Outdated
rng = np.random.RandomState(0)
X = rng.random_sample((400, 4))
y = rng.random_sample((200, 4))
gen = pairwise_distances_blockwise(X, y, block_size=0, metric='euclidean')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather see a test where block_size=1 or greater

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was testing this for invalid block size hence used 0.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but 1 is invalid too if X has many samples

Comment thread sklearn/metrics/tests/test_pairwise.py Outdated
X = rng.random_sample((400, 4))
y = rng.random_sample((200, 4))
gen = pairwise_distances_blockwise(X, y, block_size=0, metric='euclidean')
assert_raise_message(ValueError, 'block_size should be at least n_samples '

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather this error be raised upon calling pairwise_distances_blockwise. That means you need to not use a generator function immediately, but use a secondary function call.

Comment thread sklearn/metrics/pairwise.py Outdated
for start in range(0, n_samples, block_n_rows):
# get distances from block to every other sample
stop = min(start + block_n_rows, X.shape[0])
yield pairwise_distances(X[start:stop], Y, metric, n_jobs, **kwds)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will use at most block_size memory overall, not per job. I'm not sure this is the best way to do parallelism here, though. I.e. ideally we would use a parallel generator, but I don't think joblib.parallel supports that atm.

Comment thread sklearn/metrics/tests/test_pairwise.py Outdated
X = rng.random_sample((400, 4))
gen = pairwise_distances_blockwise(X, block_size=1, metric="euclidean")
S = np.empty((0, X.shape[0]))
for row in gen:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just use np.vstack(list(gen))

Comment thread sklearn/metrics/tests/test_pairwise.py Outdated
gen = pairwise_distances_blockwise(X, block_size=1, metric="euclidean")
S = np.empty((0, X.shape[0]))
for row in gen:
S = np.vstack((S, row))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Firstly, please check that block_size is upheld for each block.

Secondly, instead of repeated vstack, add to a list, then vstack the list.

Comment thread sklearn/metrics/tests/test_pairwise.py Outdated
np.testing.assert_almost_equal(dist_orig_val, dist_chunked_val, decimal=7)


def check_invalid_block_size_generator(generator):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this the same as next?

Comment thread sklearn/metrics/tests/test_pairwise.py Outdated
rng = np.random.RandomState(0)
# Euclidean distance should be equivalent to calling the function.
X = rng.random_sample((400, 4))
gen = pairwise_distances_blockwise(X, block_size=1, metric="euclidean")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should be testing at least one other metric. I think you would be best with a helper function like check_pairwise_distances_blockwise, then call it for different sets of parameters inclduing metric, Y None or otherwise, and block_size

@dalmia

dalmia commented Dec 12, 2016

Copy link
Copy Markdown
Contributor Author

@jnothman Thanks for the review. I've mostly implemented the changes you mentioned. However, the part you mentioned about using a parallel generator was a bit unclear to me. Since you're telling that it's not supported atm, should I be adding anything else there?

@jnothman

Copy link
Copy Markdown
Member

In terms of parallelism. The current approach to parallelism means that n_jobs jobs are started for every block. This is a lot of overhead. It may be possible to use this kind of within-block parallelism only if we reuse the same jobs (threads or processes) from block to block. I think this is possible but would require passing a Parallel instance into pairwise_distances

The parallelism used in #7177 was performed across blocks, but on the basis that each block was reduced to a negligible memory overhead before returning. For the sake of parallelism or otherwise, a more useful interface may be something like:

def pairwise_distances_reduce(X, Y, reduce_func, metric, n_jobs, block_size):
    ...

wherein reduce_func is applied to each block calculated in parallel processes. This could be used in silhouette_samplesor in a rewrite of pairwise_distances_argmin_min

@dalmia

dalmia commented Dec 15, 2016

Copy link
Copy Markdown
Contributor Author

@jnothman Thanks for the detailed explanation. I mostly got hold of what you are explaining. However, looking at #7177 I couldn't understand where in the code has each block been reduced to a negligible memory overhead. Could you please direct me to it so that I can understand better and implement the same?

@jnothman

jnothman commented Dec 15, 2016 via email

Copy link
Copy Markdown
Member

@dalmia

dalmia commented Dec 15, 2016

Copy link
Copy Markdown
Contributor Author

I understood it now. So, do you have any such reduce_func in mind for pairwise_distances or should I try incorporating it for silhouette_samples directly?

@jnothman

jnothman commented Dec 18, 2016 via email

Copy link
Copy Markdown
Member

@dalmia

dalmia commented Dec 18, 2016 •

Copy link
Copy Markdown
Contributor Author

@jnothman Oh! So, should I get started on incorporating it for silhouette_samples or pairwise_distances_argmin_min via pairwise_distances_reduce?

@jnothman

jnothman commented Dec 20, 2016 via email

Copy link
Copy Markdown
Member

@dalmia

dalmia commented Dec 22, 2016

Copy link
Copy Markdown
Contributor Author

@jnothman I'd been trying to think what to do next here, but am a bit confused(as your PR for silhouette_samples is not yet merged). I assume you are saying that I replace pairwise_distances in nearest neighbors to pairwise_distances_blockwise since we don't have a reduce_func for pairwise_distances. Could you please brief me as to what should I be doing next in this PR?

@jnothman

jnothman commented Dec 22, 2016 via email

Copy link
Copy Markdown
Member

@jnothman

jnothman commented Jan 5, 2017 via email

Copy link
Copy Markdown
Member

@dalmia

dalmia commented Feb 20, 2017 •

Copy link
Copy Markdown
Contributor Author

Seems great, working on them. Have a doubt though:

If we are to keep it, it needs tests.

From tests, do you mean apart from those in the gist linked above? (which I have added in utils/test_utils.py)

@jnothman

Copy link
Copy Markdown
Member

From tests, do you mean apart from those in the gist linked above? (which I have added in utils/test_utils.py)

Ha I'd forgotten I actually wrote a gist with unit tests :P

@jnothman jnothman changed the title [MRG] ENH: Added block_size parameter for lesser memory consumption [WIP] ENH: Added block_size parameter for lesser memory consumption Feb 23, 2017

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing to WIP while you make those substantial additions to the PR.

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, and sorry for the slow review. There continues to be interest in this kind of feature, with issues raised wrt silhouette_score and in the implementation of large margin nearest neighbors.

I don't think you need to integrate #7177 here; I think we can mark it MRG again.


if batch_size is not None:
warnings.warn("'batch_size' was deprecated in version 0.19 and will "
"be removed in version 0.21.", DeprecationWarning)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should set block_size according to batch_size, i.e. block_size = int(ceil(batch_size ** 2 / BYTES_PER_FLOAT / 1024 / 1024)) (I think).

sklearn.metrics.pairwise_distances
sklearn.metrics.pairwise_distances_argmin_min
"""
if batch_size is not None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just forward the parameters as provided onto argmin_min and let it do the warning and conversions.

return distances if squared else np.sqrt(distances, out=distances)


def _argmin_min_reduce_min(dist):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just be _argmin_min_reduce?

Returns
-------
D : array-like or sparse matrix or tuple
A distance matrix D such that D_{i, j} is the distance between the

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not accurate. The only thing we can say about the output here is that its first axis (or the first axis of each entry in a tuple) corresponds to rows in X.

are computed. If the input is a distances matrix, it is returned in blocks
instead.

This is equivalent to calling:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"vstacking the chunks generated by this function is equivalent to calling:"


pairwise_distances(X, y, metric, n_jobs)

but may use less memory.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but avoids storing the entire distance matrix in memory


def _generate_pairwise_distances_blockwise(X, Y=None, metric='euclidean',
n_jobs=1,
block_size=DEFAULT_BLOCK_SIZE,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

block_size isn't used here

D : generator of blocks based on the ``block_size`` parameter.

"""
if metric != 'precomputed' and Y is None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd do this in pairwise_distances_blockwise

n_samples = X.shape[0]
for start in range(0, n_samples, block_n_rows):
# get distances from block to every other sample
stop = min(start + block_n_rows, X.shape[0])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does just using start + block_n_rows break anything? Usually slice notation does not raise an error if stop is greater than the length

'sokalsneath', 'sqeuclidean', 'yule', "wminkowski"]


def _generate_pairwise_distances_blockwise(X, Y=None, metric='euclidean',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should inline this function in pairwise_distances_blockwise.

@jnothman jnothman changed the title [WIP] ENH: Added block_size parameter for lesser memory consumption [MRG] ENH: Added block_size parameter for lesser memory consumption May 24, 2017
@jnothman jnothman added the Sprint label Jun 3, 2017
@jnothman

jnothman commented Jun 3, 2017

Copy link
Copy Markdown
Member

@dalmia if this is considered for attention during an upcoming sprint, would you like to help finish it, or let someone else take it on?

@jnothman

jnothman commented Jun 8, 2017 •

Copy link
Copy Markdown
Member

@dalmia, I will mark this for someone else to finish.

@dalmia

dalmia commented Jun 8, 2017

Copy link
Copy Markdown
Contributor Author

@jnothman please do the same. I won't be able to continue work on this

@jnothman jnothman mentioned this pull request Jun 12, 2017
3 of 4 tasks
@jnothman jnothman added this to the 0.19 milestone Jun 13, 2017
@jnothman jnothman removed the Sprint label Jun 19, 2017
@jnothman jnothman modified the milestones: 0.19, 0.20 Jun 27, 2017
@jnothman

Copy link
Copy Markdown
Member

As noted at #8602 (comment), this continues to be an in-demand feature, but I've received next-to-no feedback from other core devs on whether I've led this PR in the right direction. I've also wondered whether we should make the batch size a global option (e.g. sklearn.set_config(chunk_memory='64M')) and then use it wherever operations can be chunked. Your thoughts on whether this is a priority; its design (at lesat in terms of the magical flexible_vstack I introduced); and whether configurations should be global are very welcome, @amueller, @lesteve, @GaelVaroquaux, ...

@jnothman

jnothman commented Feb 6, 2018

Copy link
Copy Markdown
Member

Closing as superseded by #10280

@jnothman jnothman closed this Feb 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Batch pairwise_distances in neighbors to reduce memory consumption

4 participants