Skip to content

Instantiating Slang defined struct that contains Tensor in Python causes problems #977

@StreamVersus

Description

@StreamVersus

About: Linux, Python3.13, latest SlangPY, tested on Vulkan and cpu - same issues

Struct for testing:

import slangpy;

struct Weights
{
    ITensor<float, 2> weights;
}

Three distinct cases:

  1. Using numpy array as data:
device = spy.create_device(include_paths=[pathlib.Path(__file__).parent])
layers = spy.Module.load_from_file(device, "layers.slang")

# ================================================================
# Helpers
# ================================================================

def tensor(data: np.ndarray) -> spy.Tensor:
    return spy.Tensor.from_numpy(device, data)

def randn(*shape) -> np.ndarray:
    return np.random.randn(*shape).astype(np.float32)

weights = layers.Weights(
    weights = randn(VOCAB_SIZE, EMBED_DIM)
)

causes:

RuntimeError: numpy array shape dim 1 does not match the expected shape (-1 != 64)

-1 is dimensions that it expects, it might happen if check assumes tensor is static/we try to replace it, but here its uninitialized

  1. Using tensor as data:
weights = layers.Weights(
    weights = tensor(randn(VOCAB_SIZE, EMBED_DIM)) # added tensor() wrap to first one
)
[ERROR] (rhi) slang: error[E41014]: type cannot be packed for dynamic dispatch
   --> /home/stream/projects/venv/lib/python3.13/site-packages/slangpy/slang/tensor.slang:286:15
    |
286 | public struct Tensor<T, let D : int> : ITensor<T, D>
    |               ^^^^^^ type 'Tensor' contains fields that cannot be packed into ordinary bytes for dynamic dispatch.
----'

Traceback (most recent call last):
  File "/home/stream/projects/kernels/training_test.py", line 33, in <module>
    weights = layers.Weights(
        weights = tensor(randn(VOCAB_SIZE, EMBED_DIM))
    )
  File "/home/stream/projects/venv/lib/python3.13/site-packages/slangpy/core/struct.py", line 204, in __call__
    return self.__getattr__("__init")(*args, **kwds)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
RuntimeError: m_rhi_command_encoder->finish(rhi_command_buffer.writeRef()) failed with error: -2147467259 (SLANG_FAIL)

RHI messages:
slang: error[E41014]: type cannot be packed for dynamic dispatch
   --> /home/stream/projects/venv/lib/python3.13/site-packages/slangpy/slang/tensor.slang:286:15
    |
286 | public struct Tensor<T, let D : int> : ITensor<T, D>
    |               ^^^^^^ type 'Tensor' contains fields that cannot be packed into ordinary bytes for dynamic dispatch.
----'


/project/src/sgl/device/command.cpp:891 in function finish
  1. Using compile-time sized arrays instead of ITensor simply causes freezes on big arrays, probably due to being allocated in other type of memory(on cpu it should be stack, causing stack overflows), it probably should fail with actual error, works on small sizes

It happens with any type of Tensor(ITensor, RW version, just Tensor, DiffTensor), and currently i see no way to actually make struct around Tensor.

Metadata

Metadata

Assignees

No one assigned
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions