Back to Ray

Anti-pattern: Closure capturing large objects harms performance

doc/source/ray-core/patterns/closure-capture-large-objects.rst

1.13.11.6 KB
Original Source

Anti-pattern: Closure capturing large objects harms performance

TLDR: Avoid closure capturing large objects in remote functions or classes, use object store instead.

When you define a :func:ray.remote <ray.remote> function or class, it is easy to accidentally capture large (more than a few MB) objects implicitly in the definition. This can lead to slow performance or even OOM since Ray is not designed to handle serialized functions or classes that are very large.

For such large objects, there are two options to resolve this problem:

  • Use :func:ray.put() <ray.put> to put the large objects in the Ray object store, and then pass object references as arguments to the remote functions or classes ("better approach #1" below)
  • Create the large objects inside the remote functions or classes by passing a lambda method ("better approach #2"). This is also the only option for using unserializable objects.

Code example

Anti-pattern:

.. literalinclude:: ../doc_code/anti_pattern_closure_capture_large_objects.py :language: python :start-after: anti_pattern_start :end-before: anti_pattern_end

Better approach #1:

.. literalinclude:: ../doc_code/anti_pattern_closure_capture_large_objects.py :language: python :start-after: better_approach_1_start :end-before: better_approach_1_end

Better approach #2:

.. literalinclude:: ../doc_code/anti_pattern_closure_capture_large_objects.py :language: python :start-after: better_approach_2_start :end-before: better_approach_2_end