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] Add functionality to define kernel and bias initializers for RLModules. #42137

Merged

Conversation

simonsays1980
Copy link
Collaborator

@simonsays1980 simonsays1980 commented Dec 31, 2023

Why are these changes needed?

The new stack does not offer users to define the initializers for network weights. Following literature (e.g. https://arxiv.org/abs/2006.05990) initialization of network weights can have signficant influence on policy performance.

This PR adds the funcitonality of network initialization to the new stack and enables thereby users to

  1. Define the initialization method for the kernel by passing in a kernel initializer defined by the framework (for torch and tensorflow)
  2. Define their custom initializers by deriving from the frameworks' initializers.

Related issue number

Closes #40259

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 :(

…etworks. This commit implements it for framework 'tf2'.

Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
… the initial dense layer of 'TransposeCNNHead'.

Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
@sven1977 sven1977 changed the title Add functionality to define kernel initializers for RLModules. [RLlib] Add functionality to define kernel initializers for RLModules. Jan 5, 2024
rllib/models/utils.py Outdated Show resolved Hide resolved
rllib/models/utils.py Outdated Show resolved Hide resolved

# Initialize, if necessary.
if cnn_transpose_initializer:
if cnn_transpose_initializer_config:
Copy link
Contributor

Choose a reason for hiding this comment

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

Simplify to:

if cnn_transpose_initializer_config is not None:
    cnn_transpose_initializer(
        layer.weight, **(cnn_transpose_initializer_config or {})
    )

Gets rid of one entire if-else-block.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Great catch! Thanks @sven1977 !

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think we do not need the or {} there

bias=cnn_transpose_use_bias or is_final_layer,
),

layer = nn.ConvTranspose2d(
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

)
if cnn_initializer:
if cnn_initializer_config:
Copy link
Contributor

Choose a reason for hiding this comment

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

Same simplification as described below (see Conv Transpose).

output_initializer(layer.weight)
else:
if hidden_initializer:
if hidden_layer_initializer_config:
Copy link
Contributor

Choose a reason for hiding this comment

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

Same simplification as described below (see Conv Transpose).


if initial_dense_initializer:
if config.initial_dense_initializer_config:
initial_dense_initializer(
Copy link
Contributor

Choose a reason for hiding this comment

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

Same simplification as described below (see Conv Transpose).

…review from @sven1977.

Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
…does not allow like the others None for the initializer.

Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Sven Mika <sven@anyscale.io>
Copy link
Contributor

@sven1977 sven1977 left a comment

Choose a reason for hiding this comment

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

LGTM. Pending test cases for merge ... Thanks @simonsays1980 !

@sven1977
Copy link
Contributor

sven1977 commented Jan 9, 2024

Hey @simonsays1980 , could you check the tests? I think 2 are failing due to the new changes.

@sven1977 sven1977 self-assigned this Jan 9, 2024
… were not yet named right. Also added docs for these.

Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
@simonsays1980
Copy link
Collaborator Author

Hey @simonsays1980 , could you check the tests? I think 2 are failing due to the new changes.

@sven1977 There were a couple of minor bugs yesterday - the ones you saw after my last push. Hopefully with my commit today tests should all pass.

@simonsays1980 simonsays1980 changed the title [RLlib] Add functionality to define kernel initializers for RLModules. [RLlib] Add functionality to define kernel and bias initializers for RLModules. Jan 10, 2024
Signed-off-by: Simon Zehnder <simon.zehnder@gmail.com>
@sven1977 sven1977 merged commit 1aec3f1 into ray-project:master Jan 10, 2024
9 checks passed
vickytsang pushed a commit to ROCm/ray that referenced this pull request Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RLlib] RLModule API support for custom init of WnB
2 participants