Skip to content

Conversation

@avik-pal
Copy link
Member

fixes #1527

@gemini-code-assist
Copy link

Summary of Changes

Hello @avik-pal, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant performance improvement for GPU-accelerated training workflows by integrating a caching allocator from the GPUArrays package. By adding a dedicated allocator_cache to the TrainState, the system can now intelligently reuse memory allocations, reducing overhead and potentially speeding up gradient computations and optimization steps on GPUs. This change directly addresses and fixes issue #1527, enhancing the efficiency of GPU-based operations.

Highlights

  • GPUArrays Integration: Introduced GPUArrays as a dependency and created LuxGPUArraysExt to enable caching allocator functionality for GPU workflows.
  • TrainState Enhancement: Added an allocator_cache field to the TrainState struct to manage memory allocations for GPU-compatible backends.
  • Caching Mechanism: Implemented wrapper functions (compute_gradients_impl_with_allocator_cache, apply_gradients_with_allocator_cache, single_train_step_impl_with_allocator_cache) that utilize GPUArrays.@cached macro for efficient memory reuse during training steps.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a caching allocator for GPUArrays workflows, aiming to improve performance by reducing memory allocations on the GPU. This is implemented by adding an allocator_cache field to TrainState and refactoring the core training functions to use a dispatch-based system. A new extension, LuxGPUArraysExt, provides the specific caching logic for GPUArrays.AllocCache.

The overall design is solid and uses a clean, non-invasive approach to extend the functionality. The refactoring in training.jl is well-executed.

I have a couple of comments on the new extension file, ext/LuxGPUArraysExt.jl. One is a critical fix for an UndefVarError due to a missing import, and the other is a minor suggestion to improve code cleanliness.

@avik-pal avik-pal force-pushed the ap/caching_allocator branch from 353e9e5 to bfd75a7 Compare November 13, 2025 06:04
@avik-pal avik-pal force-pushed the ap/caching_allocator branch from bfd75a7 to f99bab9 Compare November 13, 2025 06:04
@github-actions
Copy link
Contributor

github-actions bot commented Nov 13, 2025

Benchmark Results (Julia v1.11)

Time benchmarks
main 872ec0b... main / 872ec0b...
basics/MHA 3.59 ± 0.71 μs 3.46 ± 0.58 μs 1.04 ± 0.27
basics/MHA (first run) 3.98 ± 0.66 μs 3.62 ± 0.5 μs 1.1 ± 0.24
basics/MHA reactant 0.0618 ± 0.0067 ms 0.0656 ± 0.0039 ms 0.943 ± 0.12
basics/MHA reactant (comp + run) 0.157 ± 0.0032 s 0.159 ± 0.0071 s 0.991 ± 0.049
basics/conv 15.3 ± 11 μs 15.9 ± 11 μs 0.966 ± 0.96
basics/conv (first run) 13.1 ± 1.5 μs 13.2 ± 8.1 μs 0.993 ± 0.62
basics/conv reactant 0.0504 ± 0.0029 ms 0.0536 ± 0.0025 ms 0.941 ± 0.07
basics/conv reactant (comp + run) 0.112 ± 0.0037 s 0.115 ± 0.003 s 0.977 ± 0.041
basics/dense 0.13 ± 0.002 μs 0.131 ± 0.002 μs 0.992 ± 0.022
basics/dense (first run) 0.151 ± 0.021 μs 0.135 ± 0.021 μs 1.12 ± 0.23
basics/dense reactant 0.0495 ± 0.0022 ms 0.051 ± 0.0022 ms 0.971 ± 0.06
basics/dense reactant (comp + run) 0.0932 ± 0.0019 s 0.0929 ± 0.0015 s 1 ± 0.026
time_to_load 0.895 ± 0.0052 s 0.914 ± 0.0048 s 0.979 ± 0.0077
Memory benchmarks
main 872ec0b... main / 872ec0b...
basics/MHA 0.087 k allocs: 6.05 kB 0.087 k allocs: 6.05 kB 1
basics/MHA (first run) 0.087 k allocs: 6.05 kB 0.087 k allocs: 6.05 kB 1
basics/MHA reactant 19 allocs: 0.578 kB 19 allocs: 0.578 kB 1
basics/MHA reactant (comp + run) 18 k allocs: 1.28 MB 18 k allocs: 1.28 MB 1
basics/conv 0.038 k allocs: 5.12 kB 0.038 k allocs: 5.12 kB 1
basics/conv (first run) 0.038 k allocs: 5.12 kB 0.038 k allocs: 5.12 kB 1
basics/conv reactant 15 allocs: 0.438 kB 15 allocs: 0.438 kB 1
basics/conv reactant (comp + run) 6.16 k allocs: 0.722 MB 6.16 k allocs: 0.722 MB 1
basics/dense 2 allocs: 0.109 kB 2 allocs: 0.109 kB 1
basics/dense (first run) 2 allocs: 0.109 kB 2 allocs: 0.109 kB 1
basics/dense reactant 15 allocs: 0.422 kB 15 allocs: 0.422 kB 1
basics/dense reactant (comp + run) 5.9 k allocs: 0.703 MB 5.9 k allocs: 0.703 MB 1
time_to_load 0.159 k allocs: 11.2 kB 0.159 k allocs: 11.2 kB 1

@avik-pal avik-pal force-pushed the ap/caching_allocator branch from 1d06902 to 6ef2ad5 Compare November 13, 2025 07:03
@avik-pal avik-pal merged commit 654bd34 into main Nov 13, 2025
41 of 46 checks passed
@avik-pal avik-pal deleted the ap/caching_allocator branch November 13, 2025 15:32
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.

Automatically cache allocations for JuliaGPU workloads

2 participants