Skip to content

refactor: move device utilities to platform classes and io_struct#757

Merged
nuzant merged 3 commits intomainfrom
fw/device
Dec 23, 2025
Merged

refactor: move device utilities to platform classes and io_struct#757
nuzant merged 3 commits intomainfrom
fw/device

Conversation

@garrett4wade
Copy link
Collaborator

Description

In the single-controller mode, the log_gpu_stats function runs on the CPU node and it is a bug. This PR attaches the device logging method into the engine to avoid this issue.

  • Add DeviceRuntimeInfo dataclass to io_struct.py for runtime memory stats
  • Add clear_memory() method to all platform classes (cuda, npu, cpu, unknown)
  • Add get_device_stats() abstract method to TrainEngine API
  • Implement get_device_stats() in FSDPEngine and MegatronEngine
  • Update all usages from log_gpu_stats() to engine.get_device_stats().log()
  • Delete areal/utils/device.py as functionality moved to platform/engine

🤖 Generated with Claude Code

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not
    work as expected)
  • Documentation update
  • Code refactoring (no functional changes)
  • Performance improvement
  • Test coverage improvement

Checklist

  • I have read the Contributing Guide
  • I have run formatting tools (pre-commit or manual)
  • I have run relevant unit tests and they pass
  • I have added tests for new functionality
  • I have updated documentation if needed
  • My branch is up to date with main
  • This PR introduces breaking changes (if yes, fill out details below)
  • If this PR changes documentation, I have built and previewed it locally with
    jb build docs
  • No critical issues raised by AI reviewers (/gemini review)

- Add DeviceRuntimeInfo dataclass to io_struct.py for runtime memory stats
- Add clear_memory() method to all platform classes (cuda, npu, cpu, unknown)
- Add get_device_stats() abstract method to TrainEngine API
- Implement get_device_stats() in FSDPEngine and MegatronEngine
- Update all usages from log_gpu_stats() to engine.get_device_stats().log()
- Delete areal/utils/device.py as functionality moved to platform/engine

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @garrett4wade, 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 significantly refactors the handling of device-specific operations, particularly memory management and logging. The core intent is to resolve an issue where GPU statistics were erroneously collected on the CPU in single-controller mode, and to establish a more robust, platform-aware mechanism for managing device resources. By integrating device utilities directly into the engine and platform classes, the changes enhance the reliability and consistency of device interaction across the codebase.

Highlights

  • Centralized Device Utilities: Device utility functions, previously in areal/utils/device.py, have been refactored and moved into platform-specific classes and the io_struct module for better organization and correctness.
  • Bug Fix for log_gpu_stats: Addresses a bug where log_gpu_stats was incorrectly running on the CPU node in single-controller mode by attaching device logging methods directly to the engine.
  • New DeviceRuntimeInfo Dataclass: A new DeviceRuntimeInfo dataclass has been introduced in areal/api/io_struct.py to encapsulate runtime memory statistics, providing a standardized way to report device memory usage.
  • Standardized clear_memory Method: A clear_memory() method has been added to all platform classes (CUDA, NPU, CPU, Unknown) and the base Platform class, ensuring a consistent interface for clearing device memory across different hardware.
  • Engine API Integration for Device Stats: An abstract get_device_stats() method has been added to the TrainEngine API and implemented in FSDPEngine and MegatronEngine, allowing engines to directly provide their device runtime information.
  • Updated Usage of Device Logging: All previous calls to the standalone log_gpu_stats() function have been updated to use the new engine.get_device_stats().log() pattern, ensuring device stats are logged through the engine's context.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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.

@garrett4wade garrett4wade added the safe-to-test Ready to run unit-tests in a PR. label Dec 23, 2025
Copy link
Contributor

@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 is a solid refactoring that moves device-specific utilities like memory logging and clearing into platform-specific classes, fixing a bug where log_gpu_stats was running on the wrong node. The introduction of DeviceRuntimeInfo and the clear_memory method in platform classes is a good architectural improvement. The changes are well-executed across the codebase. I've identified a couple of areas for improvement: one to make the new DeviceRuntimeInfo class more robust against incorrect usage, and a potential bug in the UnknownPlatform where the clear_memory implementation seems to be missing.

@garrett4wade garrett4wade added safe-to-test Ready to run unit-tests in a PR. and removed safe-to-test Ready to run unit-tests in a PR. labels Dec 23, 2025
Copy link
Collaborator

@nuzant nuzant left a comment

Choose a reason for hiding this comment

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

LGTM.

@nuzant nuzant merged commit d5093d7 into main Dec 23, 2025
11 of 13 checks passed
@nuzant nuzant deleted the fw/device branch December 23, 2025 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

safe-to-test Ready to run unit-tests in a PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants