Skip to content

[WIP] [ROCm] Migrate SMI device queries from rocm_smi to amd_smi - #1113

Closed
nurmukhametov wants to merge 1 commit into
mainfrom
anurmukh/migrate-rocm-smi-to-amd-smi
Closed

nurmukhametov wants to merge 1 commit into
mainfrom
anurmukh/migrate-rocm-smi-to-amd-smi

Conversation

@nurmukhametov

Copy link
Copy Markdown
Member

Motivation

To get review.

Technical Details

RCCL dropped librocm_smi64 in ROCm 7.13 and links libamd_smi instead. The two libraries cannot coexist in one process. XLA now switches SMI libraries at the same boundary RCCL does, so only one is ever present. Selection is compile and link time only, via a version gated :smi target in BUILD.tpl and #if (TF_ROCM_VERSION >= 71300) in the three existing sources. The ROCm floor stays at 7.1; bumping it to 7.13 later reduces to deleting the #else branches.

Submission Checklist

@nurmukhametov
nurmukhametov force-pushed the anurmukh/migrate-rocm-smi-to-amd-smi branch from 1cba3a7 to 1d0de6d Compare August 18, 2026 10:21
@nurmukhametov nurmukhametov added the claude-review Request a Claude AI code review for this PR label Aug 18, 2026
@github-actions github-actions Bot removed the claude-review Request a Claude AI code review for this PR label Aug 18, 2026
Comment thread third_party/gpus/rocm/BUILD.tpl Resolved
uint16_t width;
};

#if (TF_ROCM_VERSION >= 71300)

@i-chaochen i-chaochen Aug 18, 2026 •

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.

this macro guarad on rocm version will be challenged by upstream to change as the runtime check..

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't think so. Why should they challenge that?

@i-chaochen i-chaochen Aug 18, 2026 •

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.

they against these new added #ifdef e.g., openxla#47447 (review) and openxla#46895 (comment)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

they against these new added #ifdef e.g., openxla#47447 (review) and openxla#46895 (comment)

IMO, these are completely different cases.

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.

Split the implementation into separate files and choose the impl via bazel.

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.

I'm not sure how this will play with you work to use amd_smi to get memory bandwidth. Not sure what we have here is simpler than just moving to amd_smi and having dlopen workaround for pre 7.14.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Split the implementation into separate files and choose the impl via bazel.

Done.

I'm not sure how this will play with you work to use amd_smi to get memory bandwidth. Not sure what we have here is simpler than just moving to amd_smi and having dlopen workaround for pre 7.14.

I will implement it for amd_smi only. Below 7.13, it will just keep today's behavior, the per-gfx constant.

@nurmukhametov
nurmukhametov force-pushed the anurmukh/migrate-rocm-smi-to-amd-smi branch from 1d0de6d to 8b44a37 Compare August 18, 2026 11:37
Comment on lines +69 to +71
// amd_smi reports pcie_speed directly in MT/s, unlike rocm_smi below.
return PcieLinkStatus{pcie_info.pcie_metric.pcie_speed,
pcie_info.pcie_metric.pcie_width};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit / fragility: The comment says amd_smi reports speed "directly in MT/s." Could you add a reference to the specific amd_smi header or doc that confirms this? The rocm_smi path below documents its 0.1 GT/s unit, which is great — having the same clarity here would protect against silent regressions if the semantics ever change across amd_smi versions.

A sanity check (e.g., LOG_IF(WARNING, speed > 128000)) would also catch unexpected unit changes early, but that's optional.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Put the reference to amd_smi header.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Resolved - the amdsmi.h reference in rocm_smi_util_amd_smi.cc:132 documents the unit clearly.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Resolved — amd_smi header reference added. Thanks.

Comment on lines +169 to +173
amdsmi_bdf_t bdf = {};
bdf.bdf.domain_number = target_bdf.domain;
bdf.bdf.bus_number = target_bdf.bus;
bdf.bdf.device_number = target_bdf.device;
bdf.bdf.function_number = target_bdf.function;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: BdfComponents uses uint64_t for all fields (domain, bus, device, function), but amdsmi_bdf_t's internal fields are narrower (typically uint32_t/uint16_t/bitfields). The assignments here involve implicit narrowing conversions. While PCI BDF values are always small enough in practice, explicit static_casts would make the assumption visible and prevent compiler warnings under stricter settings.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The field types match at uint64_t, and static_cast neither documents nor silences anything

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Resolved - understood, BdfComponents fields are uint64_t and the amdsmi_bdf_t accessors return matching types, so no narrowing occurs.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Resolved — agreed, the field types already match at uint64_t so no narrowing occurs and a static_cast would be noise.


// Returns true if src reaches dst over an xGMI link.
bool IsXgmiPeer(SmiDeviceHandle src, SmiDeviceHandle dst) {
uint64_t hops = 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: hops is written by both amdsmi_topo_get_link_type and rsmi_topo_get_link_type but is never read afterward. Moving it inside each #if/#else block (or at minimum annotating with [[maybe_unused]]) would be cleaner.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is not unused, it is used. Made a comment about it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Resolved - the comment at line 148 makes clear hops is a required out-parameter. Thanks for clarifying.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Resolved — hops is a required out-parameter for both amdsmi_topo_get_link_type and rsmi_topo_get_link_type. Clarifying comment added.

Comment thread third_party/gpus/rocm/BUILD.tpl Outdated
Comment on lines +347 to +348
data = glob([
"%{rocm_root}/lib/libamd_smi.so*" if _use_amd_smi else "%{rocm_root}/lib/librocm_smi64.so*",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

observation: glob() will silently produce an empty list if the expected library is missing from the ROCm installation (e.g., ROCm 7.13 installed but libamd_smi.so not present). The error would only surface at runtime. This is inherent to glob-based patterns and not new to this PR — just flagging in case a more explicit fail-fast check (e.g., a Starlark fail() when the glob returns empty) is desired.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The glob is indeed silent when empty (XLA sets --incompatible_disallow_empty_glob=false in .bazelrc:5), but it is not what gates this. rocm_lib_import passes interface_library to cc_import(shared_library = ...) as a bare label, never through a glob, so a missing library is a build-time error naming the exact path, not a runtime surprise.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Resolved - correct, interface_library is a bare label so a missing .so is a build-time error, not a silent runtime issue.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Resolved — good point about interface_library in cc_import making a missing .so a build-time error regardless of the glob behavior. The safety net is at the right layer.

@claude

claude Bot commented Aug 18, 2026

Copy link
Copy Markdown

Review Summary

Clean migration from rocm_smi to amd_smi for ROCm >= 7.13. The version gating, Bazel plumbing, and SmiDeviceHandle abstraction are well structured, and the rationale for the 7.13 boundary (matching RCCL's switch, avoiding symbol conflicts) is clearly documented.

One item worth verifying before merge: the peer == *device pointer comparison in rocm_xgmi_topology.cc relies on amdsmi_get_processor_handle_from_bdf and amdsmi_get_processor_handles returning the same opaque pointer for the same physical device. If they don't, the self-skip won't fire.

The remaining inline comments are minor (implicit narrowing, unused variable, documentation asks).

@nurmukhametov
nurmukhametov force-pushed the anurmukh/migrate-rocm-smi-to-amd-smi branch from 8b44a37 to 0ce954d Compare August 19, 2026 11:11
Comment thread third_party/gpus/rocm/BUILD.tpl Outdated
static_library = "%{rocm_root}/lib/libhsakmt.a",
visibility = ["//visibility:public"],
)
# RCCL links librocm_smi64 below ROCm 7.13 and libamd_smi from 7.13 on. XLA

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.

leave both libs and use alias at point of usage in xla/stream_executor/rocm/BUILD. The point of adding both is to not care which one is actuality used.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done

@nurmukhametov nurmukhametov added the claude-review Request a Claude AI code review for this PR label Aug 21, 2026
@claude

claude Bot commented Aug 21, 2026

Copy link
Copy Markdown

Re-review Summary

All 5 previous findings have been addressed by the developer:

  • Pointer identity for device comparison — verified as the library's own equality definition
  • PCIe speed units (MT/s) — amd_smi header reference added to the comment
  • BdfComponents narrowing — field types already match at uint64_t, no narrowing occurs
  • hops out-parameter — clarifying comment added; it is a required parameter, not unused
  • glob() silent empty list — interface_library in cc_import provides the build-time safety net

No new issues found. The refactoring cleanly splits the SMI backends into separate compilation units with correct build-time selection via rocm_version_number() >= 71300.

@github-actions github-actions Bot removed the claude-review Request a Claude AI code review for this PR label Aug 21, 2026
// Returns std::nullopt if not found.
std::optional<uint32_t> FindDeviceIndex(const BdfComponents& target_bdf);
// Returns handles for every SMI visible GPU, in SMI enumeration order. Empty
// on failure. Callers must hold rocm_smi_mutex.

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.

Good comment on mutex. Maybe use TF_EXCLUSIVE_LOCKS_REQUIRED here. Not that we check of it. But it is somewhat selfdocumenting.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done


// Finds the SMI device that matches the given PCI bus ID.
// Returns std::nullopt if not found. Callers must hold rocm_smi_mutex.
std::optional<SmiDeviceHandle> FindDevice(const BdfComponents& target_bdf);

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.

Does it make sense for this to be StatusOr? I guess that would avoid most of the need for kSmiLibraryName and caller can chose if to just vlog the message or treat it as something serious. Also we can ensure we only log the message once per device. I won't hold of PR on this. This can be a follow up. This std::optional somehow passed the initial review on upstream.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, it makes sense. I have removed kSmiLibraryName completely as well.

@nurmukhametov
nurmukhametov force-pushed the anurmukh/migrate-rocm-smi-to-amd-smi branch from 927e3cc to 40dd19d Compare August 25, 2026 14:30
<< pci_bus_id;
return std::nullopt;
}
absl::StatusOr<SmiDeviceHandle> device = FindDevice(*bdf);

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.

*ASIGN_OR_RETURN macro?

@i-chaochen i-chaochen Aug 25, 2026 •

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.

Actually this reminders me @nurmukhametov shall we rm this part https://github.com/ROCm/xla/blob/rocm-dev-infra/.claude/skills/review-xla-pr/SKILL.md#11-xla-project-specific-c-conventions due to openxla@eb278d3 ?

These stale skills might hurt claude code review quality? Instead of giving the specific instruction, maybe we just let Claude to follow other existing error handle APIs in the codebase.

In addition, we're still using opus-4.6, wondering can we use opus-4.8?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

*ASIGN_OR_RETURN macro?

Done.

Actually this reminders me @nurmukhametov shall we rm this part https://github.com/ROCm/xla/blob/rocm-dev-infra/.claude/skills/review-xla-pr/SKILL.md#11-xla-project-specific-c-conventions due to openxla@eb278d3 ?

These stale skills might hurt claude code review quality? Instead of giving the specific instruction, maybe we just let Claude to follow other existing error handle APIs in the codebase.

Yeah, could be. The stale things need to be removed for sure.

In addition, we're still using opus-4.6, wondering can we use opus-4.8?

We need to use the latest model.


if (!InitRocmSmi()) return std::nullopt;
if (!InitSmi()) return absl::UnavailableError("SMI is not available");

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.

Maybe make InitSmi return status.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done.

gulsumgudukbay added a commit that referenced this pull request Aug 26, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 26, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 26, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 26, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 26, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 26, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 26, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 26, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 26, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 26, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 26, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 26, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 27, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 27, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 27, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 27, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 27, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 27, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 27, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
gulsumgudukbay added a commit that referenced this pull request Aug 27, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
i-chaochen pushed a commit that referenced this pull request Aug 27, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
i-chaochen pushed a commit that referenced this pull request Aug 27, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
i-chaochen pushed a commit that referenced this pull request Aug 27, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
i-chaochen pushed a commit that referenced this pull request Aug 27, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).
mminutoli pushed a commit that referenced this pull request Aug 28, 2026
Cherry-pick of #1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).

(cherry picked from commit 45f7885)
hugomano pushed a commit to zml/xla that referenced this pull request Sep 2, 2026
)

Cherry-pick of ROCm#1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).

(cherry picked from commit 45f7885)
hugomano pushed a commit to zml/xla that referenced this pull request Sep 7, 2026
)

Cherry-pick of ROCm#1113 (02298de) with SMI-only Bazel changes
on this release branch. Unblocks JAX wheel builds when librocm_smi64
is absent from the ROCm SDK (ROCm/TheRock#6852).

(cherry picked from commit 45f7885)
@nurmukhametov

Copy link
Copy Markdown
Member Author

It was merged upstream in openxla@ca92135

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