shader-slang / slang-torch

A Python package for calling Slang modules from PyTorch.
Other
36 stars 8 forks source link

float4x3 - class "float3" has no member "w" #21

Closed yinhaoz closed 1 week ago

yinhaoz commented 2 weeks ago

Hi there, I came across an error when using float4x3 matrix.

Here is the minimal slang code

// float4x3.slang

[AutoPyBindCUDA]
[CUDAKernel]
[Differentiable]
void matrix_mul(DiffTensorView float4x3_t, DiffTensorView result_t)
{
    uint32_t idx = cudaBlockIdx().x * cudaBlockDim().x + cudaThreadIdx().x;

    if (idx >= float4x3_t.size(0))
        return;

    // load from tensor
    float4x3 mat1 = float4x3(float4x3_t[uint3(idx, 0, 0)], float4x3_t[uint3(idx, 0, 1)], float4x3_t[uint3(idx, 0, 2)],
                             float4x3_t[uint3(idx, 1, 0)], float4x3_t[uint3(idx, 1, 1)], float4x3_t[uint3(idx, 1, 2)],
                             float4x3_t[uint3(idx, 2, 0)], float4x3_t[uint3(idx, 2, 1)], float4x3_t[uint3(idx, 2, 2)],
                             float4x3_t[uint3(idx, 3, 0)], float4x3_t[uint3(idx, 3, 1)], float4x3_t[uint3(idx, 3, 2)]);

    float4x3 result = mat1 * 2;

    // write to tensor
    result_t.storeOnce(uint3(idx, 0, 0), result[0][0]);
    result_t.storeOnce(uint3(idx, 0, 1), result[0][1]);
    result_t.storeOnce(uint3(idx, 0, 2), result[0][2]);
    result_t.storeOnce(uint3(idx, 1, 0), result[1][0]);
    result_t.storeOnce(uint3(idx, 1, 1), result[1][1]);
    result_t.storeOnce(uint3(idx, 1, 2), result[1][2]);
    result_t.storeOnce(uint3(idx, 2, 0), result[2][0]);
    result_t.storeOnce(uint3(idx, 2, 1), result[2][1]);
    result_t.storeOnce(uint3(idx, 2, 2), result[2][2]);
    result_t.storeOnce(uint3(idx, 3, 0), result[3][0]);
    result_t.storeOnce(uint3(idx, 3, 1), result[3][1]);
    result_t.storeOnce(uint3(idx, 3, 2), result[3][2]);
}

Then I load this module in python

import slangtorch
import pkg_resources
import torch

print('slangtorch: ', pkg_resources.get_distribution('slangtorch').version)
print('torch: ', torch.__version__)

slangtorch.loadModule('float4x3.slang')

Output

slangtorch:  1.2.6
torch:  2.0.1+cu117

// just list the error part
.slangtorch_cache/float4x3/44136fa355b3678a/float4x3_cuda.cu(2793): error: class "float3" has no member "w"
.slangtorch_cache/float4x3/44136fa355b3678a/float4x3_cuda.cu(2797): error: class "float3" has no member "w"
.slangtorch_cache/float4x3/44136fa355b3678a/float4x3_cuda.cu(2801): error: class "float3" has no member "w"
.slangtorch_cache/float4x3/44136fa355b3678a/float4x3_cuda.cu(636): error: class "float3" has no member "w" 
          detected during instantiation of "Matrix<T, ROWS, COLS> makeMatrix<T,ROWS,COLS>(T, T, T, T, T, T, T, T, T, T, T, T) [with T=float, ROWS=4, COLS=3]" 
.slangtorch_cache/float4x3/44136fa355b3678a/float4x3_cuda.cu(637): error: class "float3" has no member "w"
          detected during instantiation of "Matrix<T, ROWS, COLS> makeMatrix<T,ROWS,COLS>(T, T, T, T, T, T, T, T, T, T, T, T) [with T=float, ROWS=4, COLS=3]" 
.slangtorch_cache/float4x3/44136fa355b3678a/float4x3_cuda.cu(638): error: class "float3" has no member "w"
          detected during instantiation of "Matrix<T, ROWS, COLS> makeMatrix<T,ROWS,COLS>(T, T, T, T, T, T, T, T, T, T, T, T) [with T=float, ROWS=4, COLS=3]" 

The generated float4x3_cuda.cu is attached (change .txt to .cu) float4x3_cuda.txt

In the example, the error goes away if I change float4x3 to float3x4 or float3x3.

csyonghe commented 2 weeks ago

This is being fixed in https://github.com/shader-slang/slang/pull/5509.

csyonghe commented 1 week ago

The fix should now be available in v1.3.1.