Skip to content

add nccl env intercept example code - #269

Merged
MC952-arch merged 2 commits into
flagos-ai:mainfrom
yzhang35:main
Oct 16, 2025
Merged

add nccl env intercept example code#269
MC952-arch merged 2 commits into
flagos-ai:mainfrom
yzhang35:main

Conversation

@yzhang35

Copy link
Copy Markdown
Collaborator

No description provided.

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @yzhang35, 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 robust system for intercepting and controlling NCCL environment variables, enhancing the flexibility of NCCL parameter configuration. It includes the core C++ logic for environment variable interception, an example for a specific NCCL parameter, and integrates this new capability into the existing flagcx tuning framework by updating relevant configuration types. This change enables more dynamic and fine-grained control over NCCL behavior during application execution.

Highlights

  • New NCCL Environment Variable Interception: Introduced a new mechanism to intercept and dynamically manage NCCL environment variables, allowing for runtime configuration of NCCL parameters.
  • Example Implementation for NCCL_P2P_NVL_CHUNKSIZE: Provided an example implementation in nccl_param.cc for intercepting the NCCL_P2P_NVL_CHUNKSIZE environment variable, complete with a default value and error handling.
  • Updated flagcx Tuner Configuration: Modified flagcx_tuner.cc to update the type of NCCL_P2P_NVL_CHUNKSIZE within the flagcxEnvConfig from FLAGCX_ENV_TYPE_COLL to FLAGCX_ENV_TYPE_CREATION, aligning with the new interception approach.
  • Build System for Interception Library: Added a new Makefile to build libnccl_param.so, a shared library responsible for the NCCL environment variable interception logic.
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.

@yzhang35

Copy link
Copy Markdown
Collaborator Author

env_verify.log
cat env_verify.log | grep -e "P2P Chunksize set" -e 'Profiling data for' -e 'best CommId='

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

Copy link
Copy Markdown

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 adds an example of how to intercept NCCL environment variables by preloading a shared library. The changes include a new Makefile, the C++ source for the shared library, and a fix in flagcx_tuner.cc to correctly classify an environment variable type.

My review focuses on the new C++ code. I've suggested several improvements to nccl_param.cc to make it more robust, idiomatic with modern C++, and easier to maintain. This includes using std::mutex for thread safety, simplifying logic, and cleaning up headers. I also pointed out a typo in a comment that could be confusing for developers using this example.

@@ -0,0 +1,33 @@
// This is a example of intercept NCCL environment getter functions.
#include <stdio.h>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The <stdio.h> header is not used in this file and can be removed to keep the includes clean. Additionally, for better C++ code style, consider using the C++-style headers <cstdlib> and <cstring> instead of <stdlib.h> and <cstring>.

Comment on lines +9 to +22
static void ncclLoadParam(char const* env, int64_t deftVal, int64_t* value) {
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&mutex);
const char* str = getenv(env);
*value = deftVal;
if (str && strlen(str) > 0) {
try {
*value = std::stoll(str);
} catch (const std::exception& e) {
*value = deftVal;
}
}
pthread_mutex_unlock(&mutex);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

This function can be improved for better C++11 style and clarity:

  1. Consider using std::mutex and std::lock_guard instead of pthreads for thread synchronization. This is more idiomatic C++ and safer as it uses RAII to ensure the mutex is always unlocked. This would require including <mutex> and removing <pthread.h>.
  2. The assignment *value = deftVal; inside the catch block is redundant because *value has already been set to deftVal on line 13.

Here is a suggested refactoring that applies both points:

static void ncclLoadParam(char const* env, int64_t deftVal, int64_t* value) {
  static std::mutex param_mutex;
  std::lock_guard<std::mutex> lock(param_mutex);
  const char* str = getenv(env);
  *value = deftVal;
  if (str && strlen(str) > 0) {
    try {
      *value = std::stoll(str);
    } catch (const std::exception& e) {
      // If parsing fails, *value keeps deftVal assigned earlier.
    }
  }
}

return value; \
}

// In order to intercept a NCCL environment getter function, add an additional line of NCCL_PRARM here.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

There's a typo in this explanatory comment. NCCL_PRARM should be NCCL_PARAM to match the macro defined above. This will help users who read this example.

// In order to intercept a NCCL environment getter function, add an additional line of NCCL_PARAM here.

@MC952-arch MC952-arch left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@chencjcj chencjcj left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@MC952-arch
MC952-arch merged commit 3d64ba1 into flagos-ai:main Oct 16, 2025
3 checks passed
liuhe-spec pushed a commit to liuhe-spec/FlagCX that referenced this pull request Nov 28, 2025
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.

3 participants