Repository navigation
RMSNormVJP backward writes a full {n_rows, D} gw_temp intermediate
#4293
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,11 +109,9 @@ void RMSNormVJP::eval_gpu( | |
| array x_copy = contiguous_copy_gpu(x, s); | ||
| return {x_copy, true}; | ||
| }; | ||
| bool donate_g = inputs[2].is_donatable(); | ||
| auto [x, copied] = check_input(inputs[0]); | ||
| const array& w = inputs[1]; | ||
| auto [g, g_copied] = check_input(inputs[2]); | ||
| donate_g |= g_copied; | ||
| array& gx = outputs[0]; | ||
| array& gw = outputs[1]; | ||
|
|
||
|
|
@@ -137,17 +135,22 @@ void RMSNormVJP::eval_gpu( | |
| auto axis_size = static_cast<uint32_t>(x.shape().back()); | ||
| int n_rows = x.data_size() / axis_size; | ||
|
|
||
| const int target_groups = 512; | ||
| uint32_t rows_per_group = 1; | ||
| int n_groups = n_rows; | ||
| if (axis_size <= RMS_LOOPED_LIMIT) { | ||
| rows_per_group = (n_rows + target_groups - 1) / target_groups; | ||
| n_groups = (n_rows + rows_per_group - 1) / rows_per_group; | ||
| } | ||
|
|
||
| // Allocate the gradient accumulator gw and a temporary to store the | ||
| // gradients before they are accumulated. | ||
| array gw_temp = | ||
| (has_w) ? array({n_rows, x.shape().back()}, gw.dtype(), nullptr, {}) : w; | ||
| array gw_temp = (has_w) | ||
| ? array({n_groups, x.shape().back()}, gw.dtype(), nullptr, {}) | ||
| : w; | ||
| if (has_w) { | ||
| if (!g_in_gx && donate_g) { | ||
|
Collaborator
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. Nice catch. Based on my understanding
Contributor
Author
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. thanks |
||
| gw_temp.copy_shared_buffer(g); | ||
| } else { | ||
| gw_temp.set_data(allocator::malloc(gw_temp.nbytes())); | ||
| compute_encoder.add_temporary(gw_temp); | ||
| } | ||
| gw_temp.set_data(allocator::malloc(gw_temp.nbytes())); | ||
| compute_encoder.add_temporary(gw_temp); | ||
| } | ||
| gw.set_data(allocator::malloc(gw.nbytes())); | ||
|
|
||
|
|
@@ -174,7 +177,7 @@ void RMSNormVJP::eval_gpu( | |
| size_t simds_needed = (threadgroup_needed + simd_size - 1) / simd_size; | ||
| size_t threadgroup_size = simd_size * simds_needed; | ||
| assert(threadgroup_size <= kernel->maxTotalThreadsPerThreadgroup()); | ||
| size_t n_threads = n_rows * threadgroup_size; | ||
| size_t n_threads = n_groups * threadgroup_size; | ||
| grid_dims = MTL::Size(n_threads, 1, 1); | ||
| group_dims = MTL::Size(threadgroup_size, 1, 1); | ||
| } else { | ||
|
|
@@ -194,12 +197,16 @@ void RMSNormVJP::eval_gpu( | |
| compute_encoder.set_bytes(eps_, 5); | ||
| compute_encoder.set_bytes(axis_size, 6); | ||
| compute_encoder.set_bytes(w_stride, 7); | ||
| if (axis_size <= looped_limit) { | ||
| compute_encoder.set_bytes(static_cast<uint32_t>(n_rows), 8); | ||
| compute_encoder.set_bytes(rows_per_group, 9); | ||
| } | ||
| compute_encoder.dispatch_threads(grid_dims, group_dims); | ||
| } | ||
|
|
||
| if (has_w) { | ||
| ReductionPlan plan( | ||
| ReductionOpType::ContiguousStridedReduce, {n_rows}, {axis_size}); | ||
| ReductionOpType::ContiguousStridedReduce, {n_groups}, {axis_size}); | ||
| strided_reduce_general_dispatch( | ||
| gw_temp, gw, "sum", plan, {0}, compute_encoder, d, s); | ||
| } | ||
|
|
||
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.
Just curious why 512?
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.
I tried all two the power till 4096 and 512 perform the best from the bench.