[MetaXGPU][FFI] Using tvm-ffi interface for MACA target#15
Conversation
Summary of ChangesHello @Five-HZ, 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 modernizes the MetaXGPU (MACA) target's integration with TVM by migrating its Foreign Function Interface (FFI) and module system to the latest Highlights
🧠 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. Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
The pull request migrates the MACA target to the new tvm-ffi interface. The migration is generally well-executed, but there are a few regressions in the runtime logic, specifically regarding synchronous memory copies and source inspection. Additionally, some FFI return types could be more idiomatic by using std::nullopt instead of null objects.
| static void GPUCopy(const void* from, void* to, size_t size, mcMemcpyKind kind, | ||
| mcStream_t stream) { | ||
| if (stream != 0) { | ||
| MACA_CALL(mcMemcpyAsync(to, from, size, kind, stream)); | ||
| } else { | ||
| MACA_CALL(mcMemcpy(to, from, size, kind)); | ||
| } | ||
| MACA_CALL(mcMemcpyAsync(to, from, size, kind, stream)); | ||
| } |
There was a problem hiding this comment.
The removal of the synchronous mcMemcpy path is a regression. In TVM, a null stream handle indicates that the operation should be synchronous with respect to the host. Always using mcMemcpyAsync makes these operations asynchronous, which can lead to race conditions if the caller expects the copy to be completed before the function returns (e.g., when copying data back to the host for immediate use).
static void GPUCopy(const void* from, void* to, size_t size, mcMemcpyKind kind,
mcStream_t stream) {
if (stream != nullptr) {
MACA_CALL(mcMemcpyAsync(to, from, size, kind, stream));
} else {
MACA_CALL(mcMemcpy(to, from, size, kind));
}
}| ffi::String InspectSource(const ffi::String& format) const final { | ||
| if (format == fmt_) return data_; | ||
| if (maca_source_.length() != 0) { | ||
| return maca_source_; | ||
| } else { | ||
| if (fmt_ == "fatbin") return data_; | ||
| return ""; | ||
| } | ||
| return ""; | ||
| } |
There was a problem hiding this comment.
The implementation of InspectSource incorrectly ignores the format argument when maca_source_ is present. It should only return the source code if the requested format is compatible (e.g., "c" or an empty string). Returning C source when a different format like "asm" is requested is misleading and violates the InspectSource contract. Additionally, returning binary data (data_) as source code when fmt_ == "fatbin" is generally incorrect for this API.
ffi::String InspectSource(const ffi::String& format) const final {
if (format == fmt_) return data_;
if (format == "c" || format == "") {
return maca_source_;
}
return "";
}| ICHECK_EQ(sptr_to_self.get(), this); | ||
| ICHECK_NE(name, symbol::tvm_module_main) << "Device function do not have main"; | ||
| auto it = fmap_.find(name); | ||
| if (it == fmap_.end()) return ffi::Function(); |
There was a problem hiding this comment.
When a function is not found in the module's function map, GetFunction should return std::nullopt instead of a null ffi::Function object. This is more consistent with the ffi::Optional return type and the standard pattern used in other TVM modules migrated to the new FFI.
if (it == fmap_.end()) return std::nullopt;| virtual void Start() { | ||
| int device_id; | ||
| MACA_CALL(mcGetDevice(&device_id)); | ||
| stream_ = TVMFFIEnvGetStream(kDLMACA, device_id); | ||
| MACA_CALL(mcEventRecord(start_, static_cast<mcStream_t>(stream_))); | ||
| } |
There was a problem hiding this comment.
Using mcGetDevice to determine the device ID for fetching the stream is potentially incorrect. A Timer is created for a specific Device, and its operations should be tied to that device regardless of the current thread's active device. The MACATimerNode should store the Device it was created for (passed in the factory function) and use its device_id to fetch the correct stream.
Fix some APIs based the lastest code.