yaoyaoding commented on code in PR #169: URL: https://github.com/apache/tvm-ffi/pull/169#discussion_r2474935897
########## docs/guides/kernel_library_guide.rst: ########## @@ -0,0 +1,161 @@ +.. 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 shipping python version and framework agnostic kernel libraries with TVM FFI. + +Tensor +====== + +TVM FFI provide minimal set of data structures to represent tensors from frameworks and allows us to build kernels for frameworks. In TVM FFI, we support two types of tensor constructs, ``ffi::Tensor`` and ``ffi::TensorView`` that can be used to represent a tensor from machine learning frameworks, such as PyTorch, XLA, JAX, and so on. + +Tensor and TensorView +--------------------- + +Both ``ffi::Tensor`` and ``ffi::TensorView`` are designed to represent tensors from ML frameworks that interact with the TVM FFI ABI. The main difference is whether it is an owning tensor structure. + +ffi::Tensor + ``ffi::Tensor`` is a completely onwing tensor pointer, pointing to a TVM FFI tensor object. TVM FFI handles the lifetime of ``ffi::Tensor`` by retaining a strong reference. + +ffi::TensorView + ``ffi::TensorView`` is non-owning view of an existing tensor. It is backed by ``DLTensor`` structure in DLPack. Since it is a non-owning view, it is the user's responsibility to ensure the lifetime of underlying tensor data and attributes of the viewed tensor object. + +We **recommend** to use ``ffi::TensorView`` when possible, that helps us to support more cases, including cases where only view but not strong reference are passed, like XLA buffer. It is also more lightweight. + +Tensor Attributes +----------------- + +For the sake of convenience, ``ffi::TensorView`` and ``ffi::Tensor`` align the following attributes retrieval mehtods to ``at::Tensor`` interface: + +``dim``, ``sizes``, ``size``, ``strides``, ``stride``, ``numel``, ``data_ptr``, ``device``, ``is_contiguous`` + +DLDataType + In TVM FFI, tensor data types are stored as ``DLDataType`` which is defined by DLPack protocol. + +DLDevice + In TVM FFI, tensor device information are stored as ``DLDevice`` which is defined by DLPack protocol. + +ShapeView + In TVM FFI, tensor shapes and strides attributes retrieval are returned as ``ShapeView``. It is an iterate-able data structure storing the shapes or strides data as ``int64_t`` array. + +Tensor Allocation Review Comment: I see, sounds good to me! I also feel that passing the tensor explicitly by the user is a good design choice. Another approach that I can think is allow the kernel provider to manage it. Currently, in hidet/tilus, we have a runtime library shipped with hidet package to have such functionality (like request a zero-initialized workspace). I want to remove the hidet runtime library completely in the future to make kernels easy to ship tilus-generated kernels. It would be good to have a general mechanism in tvm-ffi to provide a general key-based storage to the kernel providers to use like: ```c++ std::optional<tvm::ffi::Any> tvm::ffi::getStorageObject(std::string key); void tvm::ffi::setStorageObject(std::string key, std::ffi::Any); ``` so that we can manage the internal storage easily. Our current recommendation still requires a runtime library (https://tvm.apache.org/ffi/guides/compiler_integration.html#recommended-approach-for-state-management) cc @tqchen -- 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]
