You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "junrushao (via GitHub)" <gi...@apache.org> on 2026/02/22 04:57:20 UTC

[PR] feat: Add `tvm_ffi.Function.__init__` [tvm-ffi]

junrushao opened a new pull request, #470:
URL: https://github.com/apache/tvm-ffi/pull/470

   Enable creating `tvm_ffi.Function` directly from a Python callable or an existing Function instance via its constructor, e.g. `tvm_ffi.Function(add)`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] feat: Add `tvm_ffi.Function.__init__` [tvm-ffi]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #470:
URL: https://github.com/apache/tvm-ffi/pull/470#discussion_r2837133639


##########
python/tvm_ffi/cython/function.pxi:
##########
@@ -889,6 +889,27 @@ cdef class Function(Object):
     def __cinit__(self) -> None:
         self.c_release_gil = _RELEASE_GIL_BY_DEFAULT
 
+    def __init__(self, func: Callable[..., Any]) -> None:
+        """Initialize a Function from a Python callable.
+
+        This constructor allows creating a `tvm_ffi.Function` directly
+        from a Python function or another `tvm_ffi.Function` instance.
+
+        Parameters
+        ----------
+        func : Callable[..., Any]
+            The Python callable to wrap.
+        """
+        cdef TVMFFIObjectHandle chandle = NULL
+        if isinstance(func, Function):
+            chandle = (<Object>func).chandle
+            TVMFFIObjectIncRef(chandle)

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   It's possible for a `Function` object to have a `NULL` handle if it has been moved from (e.g., using `_move()`). Creating a new `Function` from such an object would result in another invalid `Function` object, which could lead to a crash or undefined behavior later when it's used. It's safer to detect this case and raise an error early.
   
   You could also add a test case to `test_pyfunc_init` to verify this behavior:
   ```python
       # Test creating from a moved-from function raises ValueError
       f_to_move = tvm_ffi.Function(add)
       _ = f_to_move._move()
       with pytest.raises(ValueError, match="Cannot initialize from a moved-from Function object"):
           tvm_ffi.Function(f_to_move)
   ```
   
   ```
               chandle = (<Object>func).chandle
               if chandle == NULL:
                   raise ValueError("Cannot initialize from a moved-from Function object.")
               TVMFFIObjectIncRef(chandle)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] feat: Add `tvm_ffi.Function.__init__` [tvm-ffi]

Posted by "junrushao (via GitHub)" <gi...@apache.org>.
junrushao closed pull request #470: feat: Add `tvm_ffi.Function.__init__`
URL: https://github.com/apache/tvm-ffi/pull/470


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] feat: Add `tvm_ffi.Function.__init__` [tvm-ffi]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on PR #470:
URL: https://github.com/apache/tvm-ffi/pull/470#issuecomment-3940167705

   ## Summary of Changes
   
   Hello @junrushao, I'm Gemini Code Assist[^1]! 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 enhances the `tvm_ffi.Function` class by introducing a new constructor. This change simplifies the process of wrapping Python callables or duplicating existing `Function` instances, providing a more intuitive and direct way to integrate Python functions into the TVM FFI system. The update improves usability and maintains type safety through explicit error handling.
   
   ### Highlights
   
   * **New Constructor for tvm_ffi.Function**: A new `__init__` method has been added to the `tvm_ffi.Function` class, allowing direct creation of `Function` instances from Python callables or existing `tvm_ffi.Function` objects.
   * **Type Hinting and Implementation**: The type stub (`.pyi`) has been updated with the new constructor's signature, and the Cython implementation (`.pxi`) includes logic for handling different input types and raising `TypeError` for invalid inputs.
   * **Comprehensive Testing**: A new test case, `test_pyfunc_init`, has been added to verify the functionality of the new constructor, covering creation from Python callables, existing `Function` instances, and error handling for invalid types.
   
   🧠 **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](https://codeassist.google/code-review/login) to enable Memory in your admin console.**
   
   <details>
   <summary><b>Changelog</b></summary>
   
   * **python/tvm_ffi/core.pyi**
       * Added the `__init__` method signature to the `Function` class.
   * **python/tvm_ffi/cython/function.pxi**
       * Implemented the `__init__` method for the `Function` class.
       * Enabled initialization from a Python callable.
       * Enabled initialization from an existing `tvm_ffi.Function` instance.
       * Added type checking to ensure the `func` argument is callable or a `Function` instance, raising `TypeError` otherwise.
       * Included docstrings for the new `__init__` method.
   * **tests/python/test_function.py**
       * Introduced `test_pyfunc_init` to verify `Function` creation from Python callables.
       * Added a test case for creating `Function` from an existing `tvm_ffi.Function`.
       * Included a test to confirm `TypeError` is raised for non-callable inputs.
   </details>
   
   <details>
   <summary><b>Using Gemini Code Assist</b></summary>
   <br>
   
   The full guide for Gemini Code Assist can be found on our [documentation page](https://developers.google.com/gemini-code-assist/docs/review-github-code), here are some quick tips.
   
   <b>Invoking Gemini</b>
   
   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.
   
   <b>Customization</b>
   
   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](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github).
   
   <b>Limitations & Feedback</b>
   
   Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: 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](https://google.qualtrics.com/jfe/form/SV_2cyuGuTWsEw84yG).
   
   <b>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](https://cloud.google.com/products/gemini/code-assist).</b>
   </details>
   
   
   [^1]: Review the [Privacy Notices](https://policies.google.com/privacy), [Generative AI Prohibited Use Policy](https://policies.google.com/terms/generative-ai/use-policy), [Terms of Service](https://policies.google.com/terms), and learn how to configure Gemini Code Assist in GitHub [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github). Gemini can make mistakes, so double check it and [use code with caution](https://support.google.com/legal/answer/13505487).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org