feat: add DFS-based ffi.ReprPrint for unified object repr#454
feat: add DFS-based ffi.ReprPrint for unified object repr#454tqchen merged 1 commit intoapache:mainfrom
Conversation
Summary of ChangesHello @junrushao, 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 enhances the string representation ( 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
|
cb25c51 to
493d366
Compare
There was a problem hiding this comment.
Code Review
This pull request introduces a centralized, reflection-based representation system (ffi.ReprPrint) for FFI objects, which is a great improvement for debuggability and consistency. The implementation correctly handles cycles and duplicate references in object graphs using a BFS traversal. The changes to delegate __repr__ in Python containers to this new system are clean. The new tests are comprehensive. I've found a few areas for improvement regarding code duplication, efficiency, and error handling, which are detailed in the specific comments. Overall, this is a solid feature addition.
4f8962e to
02b2f36
Compare
3d0562f to
8d10187
Compare
tqchen
left a comment
There was a problem hiding this comment.
Would be great to discuss the text format a bit mainly in our behavior of common refererence, is there a precedence we can refer to?
|
Looks like great improvement, would be good to discuss a bit how are we thinking in terms of repr printing and behavior against python repr. The repr do not exactly need round trippable(serialization perhaps is a better choice there). So there is a question whether we want to have the duplicated value printing (or as an option), and whether it should be default. The default behavior of python atm is simply expand. Expansion also could make sense for cases like immutable data structure. Say a shape value get reference in multiple places beause of the way we copy the data structure x = (1,2,3)
y = (1,4)
print([y, y])
> [(1,4), (1,4)]
# circle case
x = [12]
x.append(x)
> [12, [...]]This being said, there can be value in cases where we might want duplicated value printing. Perhaps we can do it under a flag. |
| return String(FormatBytes(obj->data, obj->size)); | ||
| } | ||
|
|
||
| String ReprTensor(const TensorObj* obj, const Function& fn_repr) { |
There was a problem hiding this comment.
although this one is concise, personally i think it is good to be explicit here
Tensor(shape=(1, 2), dtype="float32", device="cuda:0")There was a problem hiding this comment.
I think it's a matter of personal taste, but also please consider the output length and readability to users. In that case:
float32[10, 20]@gpu:0
seems an overall win.
There was a problem hiding this comment.
i agree it is a close call. My original thinking is ideally align with existing ones and not inventing new syntax (that users needs to learn from). Indeed the output synax would be longer, but being explicit allows user not having to learn what float32[10, 20] means.
Just to note some of the nit comments when i read the new syntax:
- when looking at it, i would have questions like "is it maps to an on stack raw array?" (in which case it is not, and it maps to a tensor).
Tensor(shape=(1, 2), dtype="float32", device="cuda:0")avoids that confusion at the cost of slightly longer
- Another minor nit is that
@gpu:0syntax conflicts a bit with@{addr}although it is really nit.
There was a problem hiding this comment.
I updated the syntax. @0x{addr} shows up only when TVM_FFI_REPR_WITH_ADDR is set to 1. It means there's no point of confusion
8d10187 to
3d1fe18
Compare
3d1fe18 to
9df6b09
Compare
|
Updated the text format and duplication handling |
9df6b09 to
a709442
Compare
Behavior from the latest commit: @tqchen LMK if it looks good to you |
a709442 to
997cbf1
Compare
- Single C++ ffi.ReprPrint function handles all types - DFS with 3-state tracking (NotVisited/InProgress/Done): - DAGs: memoized repr returned in full on re-encounter - Cycles: detected via InProgress state, shown as ... - Addresses hidden by default; set TVM_FFI_REPR_WITH_ADDR=1 to show - Per-field Repr(false) to exclude fields from repr output - Built-in repr for String, Bytes, Tensor, Shape, Array, List, Map - All Python __repr__ methods delegate to this function
997cbf1 to
cf535ab
Compare
Summary
ffi.ReprPrintfunction produces human-readable repr for any TVM FFI value...TVM_FFI_REPR_WITH_ADDR=1to showRepr(false)InfoTrait to exclude fields from repr output__repr__methods delegate to this functionFormat Examples
Test plan
TVM_FFI_REPR_WITH_ADDRtest_container.py) pass with updated Array format