-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
[ROCm] Implement RNN support #25755
Open
Ruturaj4
wants to merge
1
commit into
jax-ml:main
Choose a base branch
from
ROCm:ci_rnn_final-upstream
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+425
−125
Open
[ROCm] Implement RNN support #25755
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -175,6 +175,31 @@ def init_lstm_weight(rng: PRNGKeyArray, input_size: int, hidden_size: int, | |
return jax.random.uniform( | ||
rng, shape=(param_count,), dtype=jnp.float32, minval=-k, maxval=k) | ||
|
||
def swap_lstm_gates(weights, input_size, hidden_size, num_layers, bidirectional): | ||
"""Swaps the weights for the input and output gates for an LSTM model.""" | ||
weights = jnp.asarray(weights) # Ensure weights are JAX arrays | ||
flat_shapes = _get_params_shapes_in_lstm(input_size, hidden_size, num_layers, bidirectional) | ||
num_directions = 2 if bidirectional else 1 | ||
|
||
w_offsets = 0 | ||
for l in range(num_layers): | ||
for direction in range(num_directions): | ||
# Iterate through all weight and bias gate names to swap gates in both weights and biases | ||
for gate_name in ["W_ih", "W_hh", "b_ih", "b_hh"]: | ||
shape = flat_shapes.pop(0) # Get the current shape and remove it from the list | ||
num_elems = math.prod(shape) | ||
matrix = weights[w_offsets:w_offsets + num_elems].reshape(shape) | ||
|
||
# Swap between the input and output gates (third and fourth gates) | ||
gates = jnp.split(matrix, 4, axis=0) | ||
swapped_matrix = jnp.concatenate([gates[0], gates[1], gates[3], gates[2]], axis=0) | ||
|
||
# Update the weights with swapped matrix | ||
weights = weights.at[w_offsets:w_offsets + num_elems].set(swapped_matrix.flatten()) | ||
w_offsets += num_elems | ||
|
||
return weights | ||
|
||
|
||
def unpack_lstm_weights( | ||
weights: Array, input_size: int, hidden_size: int, num_layers: int, | ||
|
@@ -437,7 +462,8 @@ def _gpu_lowering_strip_tf32(fn, *args, cudnn_allow_tf32, **kw): | |
rnn_fwd_p.def_impl(partial(xla.apply_primitive, rnn_fwd_p)) | ||
rnn_fwd_p.def_abstract_eval(rnn_abstract_eval) | ||
if gpu_rnn: | ||
mlir.register_lowering(rnn_fwd_p, gpu_rnn.cudnn_rnn_lowering, platform='cuda') | ||
mlir.register_lowering(rnn_fwd_p, gpu_rnn.cudnn_rnn_fwd_lowering, platform='cuda') | ||
mlir.register_lowering(rnn_fwd_p, gpu_rnn.miopen_rnn_fwd_lowering, platform='rocm') | ||
|
||
|
||
def lstm_bwd(input_size: int, hidden_size: int, num_layers: int, dropout: float, | ||
|
@@ -481,5 +507,7 @@ def rnn_bwd_abstract_eval(dy_aval, dhn_aval, dcn_aval, x_aval, h0_aval, c0_aval, | |
if gpu_rnn: | ||
mlir.register_lowering( | ||
rnn_bwd_p, gpu_rnn.cudnn_rnn_bwd_lowering, platform='cuda') | ||
mlir.register_lowering( | ||
rnn_bwd_p, gpu_rnn.miopen_rnn_bwd_lowering, platform='rocm') | ||
Comment on lines
+510
to
+511
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, this needs to be protected against old version of jaxlib. |
||
|
||
lstm.defvjp(lstm_fwd, lstm_bwd) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
gpu_rnn
is in jaxlib, these changes will cause problems with version skew. JAX always needs to work with the most recent stable release of jaxlib. Perhaps you could protect this usinghasattr(gpu_rnn, "miopen_rnn_fwd_lowering")
?