Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RLlib] RLModule: InferenceOnlyAPI. #47572

Merged
merged 6 commits into from
Sep 11, 2024

Conversation

sven1977
Copy link
Contributor

@sven1977 sven1977 commented Sep 9, 2024

Introduce the RLModule InferenceOnlyAPI to simplify and generalize the concept of defining an inference-only version of ones (custom) RLModule.

Users that want to save space on the EnvRunners (on which all RLModule are constructed with the inference_only flag set to True), can now override a single method in their RLModule code (get_non_inference_only_attributes) that returns those attribute names (possibly nested) that point to sub-components of the model NOT needed for action computations.

RLlib will then automatically:
a) remove those parts in inference-only mode.
b) adjust the state returned by get_state(inference_only=True) from an inference_only=False RLModule which does implement the InferenceOnlyAPI (capable of having an inference_only version). This way, when getting the weights from a Learner (inference_only=False) for an EnvRunner (inference_only=True), the weight matrix is reduced already on the Learner saving network traffic.

Why are these changes needed?

Related issue number

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

Signed-off-by: sven1977 <[email protected]>
Signed-off-by: sven1977 <[email protected]>
Copy link
Collaborator

@simonsays1980 simonsays1980 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Some questions here and there. Awesome PR that saves us a lot of code 👍

"""An API to be implemented by RLModules that have an inference-only mode.

Only the `get_non_inference_attributes` method needs to get implemented for
a RLModule to have the following functionality:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit "a" -> "an RLModule" ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


Only the `get_non_inference_attributes` method needs to get implemented for
a RLModule to have the following functionality:
- On EnvRunners (or when self.config.inference_only=True), RLlib will remove
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we indent these? It makes it easier to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it'll pass the doctests, but let's try ...


@abc.abstractmethod
def get_non_inference_attributes(self) -> List[str]:
"""Returns a list of names (str) of attributes of inference-only components.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is confusing. The method is named get_NON_inference_attributes and the docstring tells that this returns attributes of inference-only attributes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "Returns a list of names (str) of attributes not used in inference mode."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed and clarified


from ray.rllib.core.rl_module.rl_module import RLModuleSpec

spec = RLModuleSpec(module_class=..., inference_only=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice example!!

# If an inference-only class AND self.config.inference_only is True,
# remove all attributes that are returned by
# `self.get_non_inference_attributes()`.
if self.config.inference_only and isinstance(self, InferenceOnlyAPI):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test!

continue
target = getattr(self, parts[0])
# Traverse from the next part on (if nested).
for part in parts[1:]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This stops after the first nesting of modules. Can't we just use a recursive implementation that calls itself again?

Also, this first builds all the components (could be many) and then deletes them again. This could induce some initialization cost which hurts performance in cases where workers need to be rebuild often.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true, BUT users can always decide to not even build these components in the first place inside their setup(). In this case, the above functionality will simply ignore the (already) missing attributes (which it would have deleted otherwise).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, the logic is actually fine. It does not stop after the first level, but goes all the way to the end of the specified attribute string, e.g:

"a.b.c.d.e".

  • If any sub-attribute above does not exist, break and del nothing (attribute already missing).
  • If "a.b.c.d.e" can be found -> del only the "e" sub-attribute.

if (
inference_only
and not self.config.inference_only
and isinstance(self, InferenceOnlyAPI)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this form of testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, me, too. This is what Learners should do: Check, whether the given RLModules are actually compatible with them (or expose certain known APIs to work with).
This way, we can eliminate the need to have algo-specific RLModule classes, freeing the user to think about these beforehand and making it easier to switch between algos later.

@@ -85,6 +79,10 @@ def make_target_networks(self) -> None:
if self.uses_dueling:
self._target_vf = make_target_network(self.vf)

@override(InferenceOnlyAPI)
def get_non_inference_attributes(self) -> List[str]:
return ["_target_encoder", "_target_af", "_target_vf"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return ["_target_encoder", "_target_af"] + ["_target_vf"] if self.uses_dueling else []

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

ret = ["vf"]
if hasattr(self.encoder, "critic_encoder"):
ret += ["encoder.critic_encoder"]
return ret
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return ["vf"] + [] if self.config.model_config_dict["vf_share_layers"] else ["encoder.critic_encoder"]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@override(InferenceOnlyAPI)
def get_non_inference_attributes(self) -> List[str]:
ret = ["qf", "target_qf", "qf_encoder", "target_qf_encoder"]
if self.twin_q:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return ["qf", "target_qf", "qf_encoder", "target_qf_encoder"] + [ "qf_twin", "target_qf_twin", "qf_twin_encoder", "target_qf_twin_encoder", ] if self.twin_q else []

@sven1977 sven1977 enabled auto-merge (squash) September 10, 2024 12:27
@github-actions github-actions bot added the go add ONLY when ready to merge, run all tests label Sep 10, 2024
Signed-off-by: sven1977 <[email protected]>
@sven1977 sven1977 enabled auto-merge (squash) September 10, 2024 12:31
Signed-off-by: sven1977 <[email protected]>
Signed-off-by: sven1977 <[email protected]>
Signed-off-by: sven1977 <[email protected]>
@sven1977 sven1977 requested review from maxpumperla and a team as code owners September 10, 2024 16:56
@sven1977 sven1977 merged commit 7cb95ce into ray-project:master Sep 11, 2024
5 checks passed
@sven1977 sven1977 deleted the rl_module_api_inference_only branch September 11, 2024 07:55
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go add ONLY when ready to merge, run all tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants