You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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__
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.
@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.
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.
@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
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!
FWIW I'm in support of entry-points. They're not perfect, but I think that
they're the best way to robustly provide extensibility like this. I think
that they're a feature that we should consider more often.
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
Some folks have non-open source software that they want to make work with
Dask. Submitting code to Dask or another public package isn't an option
for them.
On Sat, May 15, 2021 at 5:43 PM jakirkham ***@***.***> wrote:
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
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#7647 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACKZTFPM2ZOQHFB4G5OCJLTN32HJANCNFSM44YC6N4Q>
.
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)
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.
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.
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.
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.
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) forxinset_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.
@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.
Activity
[-]Sizeof entry-hook[/-][+]Sizeof entry-points hook[/+][-]Sizeof entry-points hook[/-][+]Feature: add sizeof entry-points hook[/+]jsignell commentedon May 14, 2021
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__agoose77 commentedon May 14, 2021
I was thinking that this would be just an extension to the existing mechanism - inverting the relationship so that Dask requests
sizeofplugins 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.
jrbourbeau commentedon May 14, 2021
Thanks for raising this issue @agoose77. Improved integration between Dask and Awkward Arrays would be great to see.
Could you elaborate a little more on why this approach doesn't work well for Awkward Arrays?
agoose77 commentedon May 14, 2021
@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-hookthat 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?
jrbourbeau commentedon May 14, 2021
Thanks for the further explanation, that makes sense. My initial thoughts about adding
sizeofentrypoint support are:sizeofimplementations and are feeling a similar pain around import side effects, then maybe entrypoints make sense.__sizeof__or register a custom function withdask.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).entrypointsas a new dependency for this? Can we stick withpkg_resourcesor 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
sizeofsupport forcupyandrmmdirectly into Dask?Additionally, I'm curious how complex it would be to integrate support for
sizeofentrypoints with our currentsizeofdispatching mechanism.agoose77 commentedon May 15, 2021
@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
And then a library wishing to register a sizeof implementation can just expose a plugin
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
.pthfile 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!mrocklin commentedon May 15, 2021
jakirkham commentedon May 15, 2021
I wonder if just turning
dask.sizeofinto a package instead of a module and letting users put their own things indask/sizeof(likedask.sizeof.awakward_arrayor similar) would be sufficient?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
mrocklin commentedon May 16, 2021
jrbourbeau commentedon May 17, 2021
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
sizeofimplementations instead of us including code within Dask (though we should keep the existing third-partysizeofimplementations 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)jrbourbeau commentedon May 21, 2021
cc'ing @nsmith- for visibility due to the awkward array connection
nsmith- commentedon May 21, 2021
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_lazyapproach? It places code specific to users of awkward in the dask library, but does not create any import requirement for either party.jrbourbeau commentedon May 21, 2021
There's nothing wrong from a technical perspective with adding a
register_lazyimplementation for awkward directly into Dask. We've done this with other libraries likecupy,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
sizeofimplementations 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_lazyimplementation 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.jpivarski commentedon May 21, 2021
On the original point of what
__sizeof__is supposed to do, perhapsnbyteswould help?Unlike
__sizeof__,nbytesalways 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.Currently, Awkward Array's
__sizeof__is just the object, no matter what, andnbytesis the size of the data it points to.Unlike a NumPy array, an Awkward Array can partially share its data with another Awkward Array. Calling
nbyteson both would naturally double-count the shared parts because each call doesn't know about the other.but any shared data within an Awkward Array is not double-counted.
So the total memory used by a set of arrays, without double-counting, can be determined by zipping them all and calling
nbytes:But now that I've thought through that, the NumPy idiom would be different:
(Awkward's
__sizeof__can't be easily changed to know about exclusive ownership while its data are still owned bystd::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.agoose77 commentedon May 21, 2021
@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 asnbytes, I would think. Of course, it's been a while - I can't recall if the serializer maintains an identity map for this reason.