Skip to content

Suboptimal graph structure when read-writing a parquet #8445

Closed

Description

@davidhao3300

What happened:

to-parquet ops don't appear to "fuse" together with the rest of the linear chain. Since fusing chains is the current workaround for dask/distributed#3974, this results in larger-than-necessary memory usage due to workers picking up work from other partitions earlier than they should.

What you expected to happen:

to-parquet ops should be merged into the linear chain like the other ops do. See code below for more explanation.

Minimal Complete Verifiable Example:

import pandas as pd
import dask.dataframe as dd
import numpy as np

df = pd.DataFrame(np.random.randn(100,100), columns=[str(x) for x in range(100)])

ddf = dd.from_pandas(df, npartitions=2)
ddf.to_parquet("input")

ddf = dd.read_parquet("input")
ddf = ddf.to_parquet('test', compute=False)
ddf.visualize(optimize_graph=True)

This is the unoptimized graph:

Screen Shot 2021-12-02 at 3 23 53 PM

This is the optimized graph:

Screen Shot 2021-12-02 at 3 24 01 PM

Ideally the to-parquet op would be part of the fused chain. That way workers have no chance to prematurely switch to working on a different chain. I'm not aware of any reason that the to-parquet op should remain unfused.

Anything else we need to know?:

Environment:

  • Dask version: 2021.11.2
  • Python version: 3.8.x
  • Operating System:
  • Install method (conda, pip, source):

Activity

  1. gjoseph92 commented on Dec 2, 2021

    @gjoseph92
    Collaborator

    @davidhao3300 thanks for raising this. You're totally right. As a workaround right now, I'd recommend:

    with dask.config.set({"optimization.fuse.active": True}):
        ddf.visualize(...)
        # or
        ddf.compute()
        # or
        ddf.persist()

    @rjzamora I guess I'd thought with #7620 that the O side of I/O was Blockwise'd as well, but reading through to_parquet, it's not:

    for d, filename in enumerate(filenames):
    dsk[(name, d)] = (
    apply,
    engine.write_partition,
    [
    (df._name, d),
    path,
    fs,
    filename,
    partition_on,
    write_metadata_file,
    ],
    toolz.merge(kwargs_pass, {"head": True}) if d == 0 else kwargs_pass,
    )
    part_tasks.append((name, d))
    final_name = "metadata-" + name
    # Collect metadata and write _metadata
    if write_metadata_file:
    dsk[(final_name, 0)] = (
    apply,
    engine.write_metadata,
    [
    part_tasks,
    meta,
    fs,
    path,
    ],
    {"append": append, "compression": compression},
    )
    else:
    dsk[(final_name, 0)] = (lambda x: None, part_tasks)

    Looks to me like this would be pretty easy to convert to a map_partitions once my changes in #8310 are merged (letting you pass BlockwiseDeps as arguments to map_partitions).

    More broadly, I'm starting to become concerned that #7620 was premature; going to open a separate issue about that (EDIT: it's #8447).

    cc @rjzamora, @ian-r-rose.

  2. davidhao3300 commented on Dec 3, 2021

    @davidhao3300
    Author

    Thank you for the fast response, I confirm that setting optimization.fuse.active to True fuses to-parquet into the chain.

    Are there any cons to enabling this globally? I guess all task graphs will have fusions that reduce opportunities for workers to share work?

  3. gjoseph92 commented on Dec 3, 2021

    @gjoseph92
    Collaborator

    Great to confirm, thanks.

    If you're not using set_index or groupby.apply, I think setting it is reasonable.

    The main con is just that you'll be converting all high-level graphs to low-level graphs, which are slower to transfer to the scheduler. Additionally, it appears that this may adversely affect set_index with the distributed scheduler, see #8451 (comment). If you're using set_index, this could be a significant problem; @rjzamora might be able to say more.

    I guess all task graphs will have fusions that reduce opportunities for workers to share work?

    Not particularly; the fusions that it makes should be pretty reasonable.

  4. rjzamora commented on Dec 3, 2021

    @rjzamora
    Member

    If you're using set_index, this could be a significant problem; @rjzamora might be able to say more.

    Right - @madsbk discovered a while back (see #6051) that shuffling (set_index) performance can be significantly improved by modifying the priority of certain tasks. This optimization was eventually implemented in the form of HLG annotations. This seemed "nice" at the time, because we were no longer adding a shuffle-specific hack in dask-core code. However, the optimization completely depends on the ShuffleLayer not being materialized on the client.

    The current "priority" solution is clearly a problem if your shuffle-based workflow happens to require low-level task fusion, for non-Blockwise layers. This is because Blockwise is the only Layer type where fusion at the HLG level is implemented. At the moment, any other form of fusion requires graph materialization, and it is hard to imagine a simple way to enable Blockwise fusion with other Layer types.

  5. rjzamora commented on Dec 3, 2021

    @rjzamora
    Member

    Note that I should be able to implement a to_parquet HLG layer soon. I suspect that breaking it into a Blockwise + Reduction (new), we should be able to utilize Blockwise fusion for the particular case motivating this issue.

  6. gjoseph92 commented on Dec 3, 2021

    @gjoseph92
    Collaborator

    @rjzamora what's Reduction? Naively reading the graph, I think it could be done easily with just Blockwise + BlockwiseDepDict to pass in the filenames to tasks (see #8310 (comment)). But Reduction sounds cool :)

  7. rjzamora commented on Dec 3, 2021

    @rjzamora
    Member

    @rjzamora what's Reduction? Naively reading the graph, I think it could be done easily with just Blockwise + BlockwiseDepDict to pass in the filenames to tasks (see #8310 (comment)). But Reduction sounds cool :)

    I think it makes sense to add a general Reduction Layer (or maybe TreeReduction) to reduce a Blockwise Layer down to a single output task. This would be used in to_parquet to collect the parquet metadata from every write task, and produce a single _metadata file.

  8. gjoseph92 commented on Dec 3, 2021

    @gjoseph92
    Collaborator

    TreeReduction would be a great one! That's probably the most common pattern that's not high-level right now.

  9. rjzamora commented on Dec 8, 2021

    @rjzamora
    Member

    TreeReduction would be a great one! That's probably the most common pattern that's not high-level right now.

    I took a first stab at this in #8468 - I'm hoping we can use the same reduction layer for the metadata stage in to_parquet

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions