Skip to content

Documentation error: Tensor constructor with element_type parameter doesn't exist #537

@szihs

Description

@szihs

Issue Description

The autodiff documentation references a Tensor constructor API that does not exist in the current SlangPy implementation.

Documentation Reference

Source:

x = spy.Tensor.numpy(device, np.array([1, 2, 3, 4], dtype=np.float32)).with_grads()

The documentation shows:

result = spy.Tensor(device, element_type=module.float, shape=(4,))
module.polynomial(a=2, b=8, c=-1, x=x, _result=result)

Actual API

The actual Tensor.__init__ signature is:

(self, storage: 'Buffer', dtype: 'SlangType', shape: 'TShapeOrTuple', strides: 'Optional[TShapeOrTuple]' = None, offset: 'int' = 0, grad_in: 'Optional[Tensor]' = None, grad_out: 'Optional[Tensor]' = None)

Issues:

  1. No element_type parameter exists - it should be dtype
  2. Constructor requires storage as first parameter, not device
  3. The constructor is low-level and not intended for typical usage

Correct Approach

Users should use factory methods instead:

# Correct way to pre-allocate a tensor
result = spy.Tensor.empty(device, shape=(4,), dtype=module.float)
module.polynomial(a=2, b=8, c=-1, x=x, _result=result)

Or use the string-based approach:

result: spy.Tensor = module.polynomial(a=2, b=8, c=-1, x=x, _result="tensor")

Related Discussion

Discord thread: https://discord.com/channels/1303735196696445038/1305995870046650368/1422744660593344643

Suggested Fix

Update the documentation to show one of these correct patterns:

  • spy.Tensor.empty(device, shape=..., dtype=...)
  • spy.Tensor.zeros(device, shape=..., dtype=...)
  • Or keep using the _result="tensor" string approach which works fine

Environment

  • Checked against current SlangPy installation
  • All factory methods (empty, zeros, from_numpy) consistently use dtype, not element_type

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions