Skip to content

Conversation

@tqchen
Copy link
Member

@tqchen tqchen commented Jun 24, 2025

This PR provides functions that adapts old VisitAttrs reflection utilities to use new reflection mechanism when available.

These adapter would allow us to gradually transition the object def from old VisitAttrs based mechanism to new mechanism.

  • For all objects
    • Replace VisitAttrs with static void RegisterReflection() that registers the fields
    • Call T::ReflectionDef() in TVM_STATIC_INIT_BLOCK in cc file
  • For subclass of AttrsNode: subclass AttrsNodeReflAdapter instead
    • Do the same steps as above and replace TVM_ATTRS
    • Provide explicit declaration of _type_key and TVM_FFI_DEFINE_FINAL_OBJECT_INFO

We will send followup PRs to do the gradual transition. Once all transition is completed, we will remove AttrsVisitor and only go through the new mechanism.

@tqchen tqchen changed the title 11;rgb:1414/1414/1414# This is the 1st commit message: [FFI] Provide Field Visit bridge so we can do gradual transition Jun 24, 2025
@tqchen tqchen force-pushed the attrs-refl branch 2 times, most recently from d7eb220 to f6db7c8 Compare June 24, 2025 15:26
@tqchen
Copy link
Member Author

tqchen commented Jun 24, 2025

Example

// can he in header file
struct TestAttrs : public AttrsNodeReflAdapter<TestAttrs> {
  int axis;
  String name;
  Array<PrimExpr> padding;
  TypedEnvFunc<int(int)> func;

  // replaces VisitAttrs
  static void RegisterReflection() {
    namespace refl = tvm::ffi::reflection;
    refl::ObjectDef<TestAttrs>()
        .def_ro("axis", &TestAttrs::axis, "axis field", refl::DefaultValue(10))
        .def_ro("name", &TestAttrs::name, "name")
        .def_ro("padding", &TestAttrs::padding, "padding of input",
                refl::DefaultValue(Array<PrimExpr>({0, 0})))
        .def_ro("func", &TestAttrs::func, "some random env function",
                refl::DefaultValue(TypedEnvFunc<int(int)>(nullptr)));
  }

  static constexpr const char* _type_key = "attrs.TestAttrs";
  TVM_FFI_DECLARE_FINAL_OBJECT_INFO(TestAttrs, BaseAttrsNode);
};

// in cc file
TVM_FFI_STATIC_INIT_BLOCK({ 
      // can group multiple such registration
     TestAttrs::RegisterReflection();
});

// still needed for now for the shash/equal mechanism
TVM_REGISTER_NODE_TYPE(TestAttrs);

This PR provides functions that adapts old VisitAttrs reflection utilities
to use new reflection mechanism when available.

These adapter would allow us to gradually transition the object
def from old VisitAttrs based mechanism to new mechanism.

- For all objects
  - Replace VisitAttrs with static void RegisterReflection() that registers the fields
  - Call T::ReflectionDef() in TVM_STATIC_INIT_BLOCK in cc file
- For subclass of AttrsNode<T>: subclass AttrsNodeReflAdapter<T> instead
  - Do the same steps as above and replace TVM_ATTRS
  - Provide explicit declaration of _type_key and TVM_FFI_DEFINE_FINAL_OBJECT_INFO

We will send followup PRs to do the gradual transition. Once all transition
is completed, we will remove AttrsVisitor and only go through the new mechanism.
@MasterJH5574 MasterJH5574 merged commit bcfd0af into apache:main Jun 25, 2025
12 checks passed
ShiboXing pushed a commit to ShiboXing/tvm that referenced this pull request Aug 10, 2025
…che#18091)

This PR provides functions that adapts old VisitAttrs reflection utilities
to use new reflection mechanism when available.

These adapter would allow us to gradually transition the object
def from old VisitAttrs based mechanism to new mechanism.

- For all objects
  - Replace VisitAttrs with static void RegisterReflection() that registers the fields
  - Call T::ReflectionDef() in TVM_STATIC_INIT_BLOCK in cc file
- For subclass of AttrsNode<T>: subclass AttrsNodeReflAdapter<T> instead
  - Do the same steps as above and replace TVM_ATTRS
  - Provide explicit declaration of _type_key and TVM_FFI_DEFINE_FINAL_OBJECT_INFO

We will send followup PRs to do the gradual transition. Once all transition
is completed, we will remove AttrsVisitor and only go through the new mechanism.
tqchen added a commit to tqchen/tvm that referenced this pull request Sep 13, 2025
…che#18091)

This PR provides functions that adapts old VisitAttrs reflection utilities
to use new reflection mechanism when available.

These adapter would allow us to gradually transition the object
def from old VisitAttrs based mechanism to new mechanism.

- For all objects
  - Replace VisitAttrs with static void RegisterReflection() that registers the fields
  - Call T::ReflectionDef() in TVM_STATIC_INIT_BLOCK in cc file
- For subclass of AttrsNode<T>: subclass AttrsNodeReflAdapter<T> instead
  - Do the same steps as above and replace TVM_ATTRS
  - Provide explicit declaration of _type_key and TVM_FFI_DEFINE_FINAL_OBJECT_INFO

We will send followup PRs to do the gradual transition. Once all transition
is completed, we will remove AttrsVisitor and only go through the new mechanism.
tqchen added a commit to tqchen/tvm that referenced this pull request Sep 13, 2025
…che#18091)

This PR provides functions that adapts old VisitAttrs reflection utilities
to use new reflection mechanism when available.

These adapter would allow us to gradually transition the object
def from old VisitAttrs based mechanism to new mechanism.

- For all objects
  - Replace VisitAttrs with static void RegisterReflection() that registers the fields
  - Call T::ReflectionDef() in TVM_STATIC_INIT_BLOCK in cc file
- For subclass of AttrsNode<T>: subclass AttrsNodeReflAdapter<T> instead
  - Do the same steps as above and replace TVM_ATTRS
  - Provide explicit declaration of _type_key and TVM_FFI_DEFINE_FINAL_OBJECT_INFO

We will send followup PRs to do the gradual transition. Once all transition
is completed, we will remove AttrsVisitor and only go through the new mechanism.
tqchen added a commit to tqchen/tvm that referenced this pull request Sep 13, 2025
…che#18091)

This PR provides functions that adapts old VisitAttrs reflection utilities
to use new reflection mechanism when available.

These adapter would allow us to gradually transition the object
def from old VisitAttrs based mechanism to new mechanism.

- For all objects
  - Replace VisitAttrs with static void RegisterReflection() that registers the fields
  - Call T::ReflectionDef() in TVM_STATIC_INIT_BLOCK in cc file
- For subclass of AttrsNode<T>: subclass AttrsNodeReflAdapter<T> instead
  - Do the same steps as above and replace TVM_ATTRS
  - Provide explicit declaration of _type_key and TVM_FFI_DEFINE_FINAL_OBJECT_INFO

We will send followup PRs to do the gradual transition. Once all transition
is completed, we will remove AttrsVisitor and only go through the new mechanism.
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.

2 participants