Skip to content

Add Array shuffle / general take using tasks #11257

Closed

Description

@phofl

Implement a shuffle for arrays that can be used as a general take as well.

This is motivated by a groupby like pattern / general take that uses almost random indexes into a single dimension. This currently is done through slicing, which blows up the number of chunks and makes the result mostly unusable. Additionally, it completely overloads the scheduler if you operate a properly random indexer during the actual slicing operation.

Leveraging a shuffle like strategy gives us the option to keep the number of chunks consistent and re-order the array in a way that elements in a dimension are close together if needed.

The API would like (naming tbd):

def shuffle(arr, indexer: list[list], axis, n_chunks=None) -> arr:
    ...

Every inner list specifies a single group. We could take liberties and squash multiple groups into a single chunk without changing the order. Groupby like patterns could then do a shuffle - map_blocks for transformers or high-cardinality groupby ops.

As a first step, we will implement this using the task-based approach.

pydata/xarray#9220

cc @dcherian for feedback

Activity

  1. self-assigned this
    on Jul 29, 2024
  2. added
    needs triage
    on Jul 29, 2024
  3. changed the title [-]Array shuffle / general take using tasks[/-] [+]Add Array shuffle / general take using tasks[/+] on Jul 29, 2024
  4. dcherian commented on Jul 29, 2024

    @dcherian
    Collaborator

    see #11076

  5. phofl commented on Jul 29, 2024

    @phofl
    CollaboratorAuthor

    (precursor: I might be missing something).

    I don't think that take_along_axis solves the shuffle problem the way we would want to. We might want to put values from different chunks into the same chunk and potentially reorder between chunks, which won't work with a trivial blockwise based implementation of take_along_axis

  6. dcherian commented on Jul 29, 2024

    @dcherian
    Collaborator

    Yes I was wrong, we need to optimize take instead of take_along_axis. See pydata/xarray#9220 (comment)

  7. phofl commented on Jul 29, 2024

    @phofl
    CollaboratorAuthor

    Yeah I think take would call this new API under the hood in cases where it makes sense (a sorted take indexer is better suited to just use the current implementation for example). take is also a bit more general (you can select rows into more than one chunk for example), but the application is very very similar

  8. phofl commented on Aug 6, 2024

    @phofl
    CollaboratorAuthor

    shuffle and take have both been merged. shuffle probably needs a follow up, but closing here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

needs triage

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions