Skip to content

Feature: add sizeof entry-points hook #7647

Closed

Description

@agoose77

I was recently thinking about how to add sizeof support to Dask for a particular library (Awkward Array). Currently, either Dask needs to be extended to support the library in question, or the library itself needs to be manually invoked to register the custom sizeof implementation.

I know that Dask can default to __sizeof__, but I'm not sure whether that is the best decision here. __sizeof__ should be the in-memory size of (just) the object, but in this particular case, an Awkward Array might not even own the data it refers to. I wouldn't expect __sizeof__ to report that memory. From the sys.getsizeof documentation,

Only the memory consumption directly attributed to the object is accounted for, not the memory consumption of objects it refers to.

I think that this would be a good candidate for entry_points. On startup, Dask can look for any registered entry-points that declare custom size-of functions, such that the user never needs to import the library explicitly on the worker, and the core of Dask can remain light-weight.

There are some performance concerns with entry_points, but projects like entrypoints claim to ameliorate the issue.

Activity

  1. changed the title [-]Sizeof entry-hook[/-] [+]Sizeof entry-points hook[/+] on May 12, 2021
  2. changed the title [-]Sizeof entry-points hook[/-] [+]Feature: add sizeof entry-points hook[/+] on May 12, 2021
  3. jsignell commented on May 14, 2021

    @jsignell
    Member

    I agree that entry_points would have been a good choice, but I doubt there is much appetite for changing how registers work in dask. That said I don't totally understand what the issue is with using __sizeof__

  4. agoose77 commented on May 14, 2021

    @agoose77
    ContributorAuthor

    I was thinking that this would be just an extension to the existing mechanism - inverting the relationship so that Dask requests sizeof plugins rather than those plugins having to register with Dask explicitly.

    Do you think that if a PR were written this would have a chance of being supported?

    RE __sizeof__, my understanding is that it's not a well-defined property. Clearly, if there's nothing better then Dask should use it because it's the only information about memory usage that Python exposes on a per-object basis. In my case I don't want __sizeof__ to return the size of the object itself, I want to use it to return the effective size, including any non-owned array data. It feels to me like this is abusing (albeit necessarily as a fallback) the purpose of __sizeof__.

    To clarify, I believe Dask is doing the right thing by utilising this property; I just think it should not be used as a primary API.

  5. jrbourbeau commented on May 14, 2021

    @jrbourbeau
    Member

    Thanks for raising this issue @agoose77. Improved integration between Dask and Awkward Arrays would be great to see.

    or the library itself needs to be manually invoked to register the custom sizeof implementation

    Could you elaborate a little more on why this approach doesn't work well for Awkward Arrays?

  6. agoose77 commented on May 14, 2021

    @agoose77
    ContributorAuthor

    @jrbourbeau actually, the current hook system does work, and I wouldn't propose changing it. It's just a matter of convenience; either Awkward needs to be patched to support Dask (which would work, but introduces a maintenance burden for a library that is not really related directly to Awkward), or a third-party package could be created (e.g. awkward-dask-hook) which would then need to be imported on the workers. I saw that users of CuPy at one point wanted something similar, but ultimately CuPy upstreamed the change.

    By adding support for entry-points, I could release a package awkward-dask-hook that exposes this entry point, and then simply by having the package installed I would have support for Awkward in Dask. So, it's not really a breaking feature, just a matter of separating the concerns of the packages.

    Does that make sense?

  7. jrbourbeau commented on May 14, 2021

    @jrbourbeau
    Member

    Thanks for the further explanation, that makes sense. My initial thoughts about adding sizeof entrypoint support are:

    • This moves that extra complexity burden from, in this case Awkward, into Dask. That said, if there are lots of third-party packages who want to register their own custom sizeof implementations and are feeling a similar pain around import side effects, then maybe entrypoints make sense.
    • Today there are two ways to tell Dask how to get the size of an object (i.e. override __sizeof__ or register a custom function with dask.sizeof). Generally it'd be good to minimize the number of ways to do the same thing. Though as you've pointed out, there are some shortcomings with the current mechanisms (in particular for library authors).
    • Is it worth adding entrypoints as a new dependency for this? Can we stick with pkg_resources or does that lead to a large import-time slow down for Dask?

    Overall there are certainly pros and cons to adding entrypoint support. I personally would be more excited about this if I knew of other groups who would also benefit from Dask adding this kind of support.

    @jakirkham @pentschev @madsbk would this make life easier for RAPIDS folks? Or would it have prior to us adding sizeof support for cupy and rmm directly into Dask?

    Additionally, I'm curious how complex it would be to integrate support for sizeof entrypoints with our current sizeof dispatching mechanism.

  8. agoose77 commented on May 15, 2021

    @agoose77
    ContributorAuthor

    @jrbourbeau a fair point. I would not expect the entry-point to add a huge amount of code (I picture something <100 lines), but indeed it's then a new API surface to maintain.

    Although this is a new mechanism to add size hints to Dask, it would just be a slightly different interface to the same system; i.e. it might effectively do something like

    # Dask init
    for name, plugin in entrypoints.items():
        plugin(sizeof.register)

    And then a library wishing to register a sizeof implementation can just expose a plugin

    def register_awkward(register):
        @register(awkward.highlevel.Array)
        def sizeof_ak_generic(obj):
            return obj.nbytes

    Evidently, I haven't developed a full proposal here. My first thought is that really this doesn't feel too Dask specific. This could be implemented separately to Dask by installing a .pth file which runs at Python start time. Perhaps that's a better solution here; it is hacky, but it's not adding anything to Dask. I'm still giving this some thought mysefl!

  9. mrocklin commented on May 15, 2021

    @mrocklin
    Member
  10. jakirkham commented on May 15, 2021

    @jakirkham
    Member

    I wonder if just turning dask.sizeof into a package instead of a module and letting users put their own things in dask/sizeof (like dask.sizeof.awakward_array or similar) would be sufficient?

    would this make life easier for RAPIDS folks? Or would it have prior to us adding sizeof support for cupy and rmm directly into Dask?

    Honestly it's been pretty easy to just add those lines to dask.sizeof. The code is simple and a PR works well.

    Wouldn't be opposed to Awkward Arrays or others adding a couple things to that file. There don't seem to be that many people making this ask, which is why just including what they want seems fine. Maybe there is a scalability limit, but I don't think we have run into that yet

  11. mrocklin commented on May 16, 2021

    @mrocklin
    Member
  12. jrbourbeau commented on May 17, 2021

    @jrbourbeau
    Member

    Thanks for the additional information @agoose77. Thinking about this more, I'm also in favor of adding entrypoint support. I think it would be more convenient for library authors and nice from Dask's perspective as library authors would have ownership over registering their own sizeof implementations instead of us including code within Dask (though we should keep the existing third-party sizeof implementations we current have).

    Also, Matt brings up a good point about non-open source projects. I hadn't considered that case.

    FWIW there are other packages in the ecosystem (including distributed) which use entrypoints (e.g. Xarray, Intake, and Distributed)

  13. jrbourbeau commented on May 21, 2021

    @jrbourbeau
    Member

    cc'ing @nsmith- for visibility due to the awkward array connection

  14. nsmith- commented on May 21, 2021

    @nsmith-

    I'll +1 entrypoints, as to the best of my knowledge this optional library cross-dependency is one of the motivating use cases.
    Though, for completeness, is there any technical issue with using the existing register_lazy approach? It places code specific to users of awkward in the dask library, but does not create any import requirement for either party.

  15. jrbourbeau commented on May 21, 2021

    @jrbourbeau
    Member

    There's nothing wrong from a technical perspective with adding a register_lazy implementation for awkward directly into Dask. We've done this with other libraries like cupy, rmm, etc.

    However, this pattern does however introduce a maintenance burden on Dask maintainers whenever a new library author shows up and wants to include code related to their project directly in Dask. Entrypoints would enable third-party library authors to keep their sizeof implementations inside their own code base, which would be simpler from a Dask maintenance perspective (and probably from a third-party library maintenance perspective too).

    Additionally, opening up a PR into Dask to add a register_lazy implementation isn't an option for some groups who can't contribute to open source projects for various (legal) reasons. Entrypoints would give these groups a convenient hook they can use.

  16. jpivarski commented on May 21, 2021

    @jpivarski

    On the original point of what __sizeof__ is supposed to do, perhaps nbytes would help?

    Unlike __sizeof__, nbytes always refers to the size of a large data buffer. Apparently NumPy includes the buffer in its __sizeof__ only if that NumPy array owns the data.

    >>> import numpy as np
    >>> import awkward as ak
    >>> np.__version__
    '1.20.3'
    >>> ak.__version__
    '1.2.2'
    >>> array = np.arange(1000000)
    >>> array.__sizeof__()
    8000104
    >>> array.nbytes
    8000000
    >>> # but...
    >>> derived = array[1:]
    >>> derived.__sizeof__()
    104
    >>> derived.nbytes
    7999992

    Currently, Awkward Array's __sizeof__ is just the object, no matter what, and nbytes is the size of the data it points to.

    >>> array = ak.Array(np.arange(1000000))
    >>> array.__sizeof__()
    32
    >>> array.nbytes
    8000000

    Unlike a NumPy array, an Awkward Array can partially share its data with another Awkward Array. Calling nbytes on both would naturally double-count the shared parts because each call doesn't know about the other.

    >>> x = ak.Array(np.arange(1000000))
    >>> z = ak.zip({"x": x})
    >>> x.nbytes
    8000000
    >>> z.nbytes
    8000000

    but any shared data within an Awkward Array is not double-counted.

    >>> z = ak.zip({"x": x, "y": x})
    >>> z.nbytes
    8000000
    >>> y = ak.Array(np.arange(1000000))
    >>> z = ak.zip({"x": x, "y": y})
    >>> z.nbytes
    16000000

    So the total memory used by a set of arrays, without double-counting, can be determined by zipping them all and calling nbytes:

    total = ak.zip(set_of_arrays, depth_limit=1).nbytes

    But now that I've thought through that, the NumPy idiom would be different:

    total = sum(sys.sizeof(x) for x in set_of_arrays)

    (Awkward's __sizeof__ can't be easily changed to know about exclusive ownership while its data are still owned by std::shared_ptrs, since the chain of references can thread back and forth through C++ and Python. If we manage the refactoring-to-Python that I talked about, this would be much easier.)

    So I guess having to do different things for NumPy arrays and Awkward Arrays is a ball of complexity that needs to be isolated somewhere. It would really help if the arguments to that entry point function take the complete set of arrays that you want to quantify. The same would apply if you ever want to quantify the size of Python objects, since their __sizeof__ methods are shallow and shared references are common.

  17. agoose77 commented on May 21, 2021

    @agoose77
    ContributorAuthor

    @jpivarski right, but if you serialise two awkward arrays that share data, they wouldn't be reconstructed at the other side in such a way as to maintain that relationship, right? You'd have two independent copies of the data? If that were the case, then we would want sizeof() to behave as nbytes, I would think. Of course, it's been a while - I can't recall if the serializer maintains an identity map for this reason.

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