tqchen commented on code in PR #169:
URL: https://github.com/apache/tvm-ffi/pull/169#discussion_r2442229465


##########
docs/guides/kernel_library_guide.rst:
##########
@@ -0,0 +1,146 @@
+.. Licensed to the Apache Software Foundation (ASF) under one
+.. or more contributor license agreements.  See the NOTICE file
+.. distributed with this work for additional information
+.. regarding copyright ownership.  The ASF licenses this file
+.. to you under the Apache License, Version 2.0 (the
+.. "License"); you may not use this file except in compliance
+.. with the License.  You may obtain a copy of the License at
+..
+..   http://www.apache.org/licenses/LICENSE-2.0
+..
+.. Unless required by applicable law or agreed to in writing,
+.. software distributed under the License is distributed on an
+.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+.. KIND, either express or implied.  See the License for the
+.. specific language governing permissions and limitations
+.. under the License.
+
+====================
+Kernel Library Guide
+====================
+
+This guide serves as a quick start for composing a kernel from scratch, or 
migrating a kernel from externel frameworks. It covers the core concepts in TVM 
FFI, such as tensor, stream.
+
+Tensor
+======
+
+Tensor is the most important input for a kernel libaray. In PyTorch C++ 
extensions, kernel library usually takes ``at::Tensor`` as tensor input. In TVM 
FFI, we introduce two types of tensor, ``ffi::Tensor`` and ``ffi::TensorView``.
+
+Tensor and TensorView
+---------------------
+
+Both ``ffi::Tensor`` and ``ffi::TensorView`` are designed to represent tensors 
in TVM FFI eco-system. The main difference is whether it is an owning tensor 
pointer.
+
+:ffi::Tensor:
+ ``ffi::Tensor`` is a completely onwing tensor pointer, pointing to TVM FFI 
tensor object. TVM FFI handles the lifetime of ``ffi::Tensor`` by retaining a 
strong reference.
+
+:ffi::TensorView:
+ ``ffi::TensorView`` is a light weight non-owning tnesor pointer, pointeing to 
a TVM FFI tensor or external tensor object. TVM FFI does not retain its 
reference. So users are responsible for ensuring the lifetime of tensor object 
to which the ``ffi::TensorView`` points.
+
+TVM FFI can automatically convert the input tensor at Python side, e.g. 
``torch.Tensor``, to both ``ffi::Tensor`` or ``ffi::TensorView`` at C++ side, 
depends on the C++ function arguments. However, for more flexibility and better 
compatibility, we **recommand** to use ``ffi::TensorView`` in practice. Since 
some frameworks, like JAX, cannot provide strong referenced tensor, as 
``ffi::Tensor`` expected.
+
+Tensor as Argument
+------------------
+
+Typically, we expect that all tensors are pre-allocated at Python side and 
passed in via TVM FFI, including the output tensor. And TVM FFI will convert 
them into ``ffi::TensorView`` at runtime. For the optional arguments, 
``ffi::Optional`` is the best practice. Here is an example of a kernel 
definition at C++ side and calling at Python side.

Review Comment:
   we can support std::optional, `ffi::Optional` is mainly to stablize the ABI 
of the optional and saves some space for now, but we can update to also support 
std::optional as well as arguments.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to