From 92ba9052fe4d241c1f987a64bbdc6a276d6e63cd Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 28 Mar 2023 16:26:42 -0700 Subject: Update a1-02-slangpy.md --- docs/user-guide/a1-02-slangpy.md | 42 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) (limited to 'docs/user-guide') diff --git a/docs/user-guide/a1-02-slangpy.md b/docs/user-guide/a1-02-slangpy.md index 7bcc47389..625f97ee3 100644 --- a/docs/user-guide/a1-02-slangpy.md +++ b/docs/user-guide/a1-02-slangpy.md @@ -62,7 +62,7 @@ TorchTensor square_fwd(TorchTensor input) return result; } ``` -Here, we first call `TorchTensor.alloc` to allocate a 2D-tensor that has the same size as the input. +Here, we first call `TorchTensor.zerosLike` to allocate a 2D-tensor that has the same size as the input. This function returns a `TorchTensor` object that represents a CPU handle of a PyTorch tensor. Then we launch `square_fwd_kernel` with the `__dispatch_kernel` syntax. Note that we can directly pass `TorchTensor` arguments to a `TensorView` parameter and the compiler will automatically convert @@ -223,7 +223,7 @@ As shown in previous tutorial, Slang has defined the `TorchTensor` and `Tenso tensors. The `TorchTensor` represents the CPU view of a tensor and provides methods to allocate a new tensor object. The `TensorView` represents the GPU view of a tensor and provides accesors to read write tensor data. -Following is a list of builtin methods provided by each type. +Following is a list of builtin methods and attributes for PyTorch interop. ### `static TorchTensor TorchTensor.alloc(uint x, uint y, ...)` Allocates a new PyTorch tensor with the given dimensions. @@ -263,4 +263,40 @@ Returns the `threadIdx` variable in CUDA. Returns the `blockIdx` variable in CUDA. ### `cudaBlockDim()` -Returns the `blockDim` variable in CUDA. \ No newline at end of file +Returns the `blockDim` variable in CUDA. + +### `[CudaKernel]` attribute +Marks a function as a CUDA kernel (maps to a `__global__` function) + +### `[TorchEntryPoint]` attribute +Marks a function for export to Python. Functions marked with `[TorchEntryPoint]` will be accessible from a loaded module returned by `slangpy.loadModule`. + +### `[CudaDeviceExport]` attribute +Marks a function as a cuda device function, and ensures the compiler to include it in the generated cuda source. + +## Type Marshalling Between Slang and Python + +The return types and parameters types of an exported `[TorchEntryPoint]` function can be a basic type (e.g. `float`, `int` etc.), a vector type (e.g. `float3`), a `TorchTensor` type, an array type, or a struct type. + +When you use struct or array types in the function signature, it will be exposed as a Python tuple. +For example, +```csharp +struct MyReturnType +{ + TorchTensor tensors[3]; + float v; +} + +[TorchEntryPoint] +MyReturnType myFunc() +{ + ... +} +``` + +Calling `myFunc` from python will result in a python tuple in the form of +``` +[[tensor, tensor, tensor], float] +``` + +The same transform rules applies to parameter types. -- cgit v1.2.3