Skip to content

Commit

Permalink
Fix issue 639 (#640)
Browse files Browse the repository at this point in the history
* Make newTensorUninit[T]() create a rank-1 empty tensor

Similar to same change done to `newTensor` on the previous commit.

* Make newTensor[T]() create a rank-1 empty tensor

Up until now calling newTensor without arguments would create a rank-0 tensor which does not work well (e.g. it reports its size as size 0)! Instead we now create a rank-1 empty tensor when no shape is provided.

It is still possible to explicitly create a rank-0 tensor by explicitly passing an empty shape (i.e. `[]`) to newTensor (e.g. `newTensor[float]([])`). This can be useful to create "sentinel" values for procedures that take tensors as arguments.

* Fix issue #639 (`size` returns 1 for rank-0 tensors)

This fixes #639.

While this adds an extra check to `size` which might be called frequently, I have not seen a major difference on several of the benchmarks.
  • Loading branch information
AngelEzquerra authored Mar 22, 2024
1 parent 7ad9903 commit 9cf5b41
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/arraymancer/laser/dynamic_stack_arrays.nim
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ func `$`*(a: DynamicStackArray): string =
result.add("]")

func product*[T:SomeNumber](a: DynamicStackArray[T]): T =
if unlikely(a.len == 0):
return 0
result = 1
for value in items(a):
result *= value
Expand Down
10 changes: 9 additions & 1 deletion src/arraymancer/laser/tensor/initialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,14 @@ proc setZero*[T](t: var Tensor[T], check_contiguous: static bool = true) =
chunk_size * sizeof(T)
)

proc newTensor*[T](shape: varargs[int]): Tensor[T] =
proc newTensor*[T](shape: varargs[int] = [0]): Tensor[T] =
## Create a new tensor of type T with the given shape.
##
## If no shape is provided, we create an empty rank-1 tensor.
## To create a rank-0 tensor, explicitly pass and empty shape `[]`.
##
## Note that in general it is not a good idea to use rank-0 tensors.
## However, they can be used as "sentinel" values for Tensor arguments.
var size: int
initTensorMetadata(result, size, shape)
allocCpuStorage(result.storage, size)
Expand All @@ -193,6 +200,7 @@ proc newTensor*[T](shape: varargs[int]): Tensor[T] =
setZero(result, check_contiguous = false)

proc newTensor*[T](shape: Metadata): Tensor[T] =
## Create a new tensor of type T with the given shape.
var size: int
initTensorMetadata(result, size, shape)
allocCpuStorage(result.storage, size)
Expand Down
21 changes: 11 additions & 10 deletions src/arraymancer/tensor/init_cpu.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ import std / [random, math]

export initialization

proc newTensorUninit*[T](shape: varargs[int]): Tensor[T] {.noinit, inline.} =
## Creates a new Tensor on Cpu backend
proc newTensorUninit*[T](shape: varargs[int] = [0]): Tensor[T] {.noinit, inline.} =
## Creates a new uninitialized Tensor of type `T` on the Cpu backend
## Input:
## - Shape of the Tensor
## - Type of its elements
## - Shape of the Tensor (defaults to an empty rank-1 tensor)
## Result:
## - A Tensor of the proper shape with NO initialization
## Warning ⚠
## Tensor data is uninitialized and contains garbage.
## Warnings ⚠:
## - Tensor data is uninitialized and contains garbage.
## - If no shape is provided, a 1D tensor of size 0 is created.
## It is possible to create a rank-0 tensor by explicitly
## providing an empty shape `[]` (e.g. `newTensorUninit[float]([])`).
var size: int
initTensorMetadata(result, size, shape)
allocCpuStorage(result.storage, size)
Expand All @@ -42,14 +44,13 @@ proc newTensorUninit*[T](size: int): Tensor[T] {.noinit, inline.} =
result = newTensorUninit[T]([size])

proc newTensorUninit*[T](shape: Metadata): Tensor[T] {.noinit, inline.} =
## Creates a new Tensor on Cpu backend
## Creates a new uninitialized Tensor of type `T` on the Cpu backend
## Input:
## - Shape of the Tensor
## - Type of its elements
## Result:
## - A Tensor of the proper shape with NO initialization
## Warning ⚠
## Tensor data is uninitialized and contains garbage.
## Warning ⚠:
## - Tensor data is uninitialized and contains garbage.
var size: int
initTensorMetadata(result, size, shape)
allocCpuStorage(result.storage, size)
Expand Down

0 comments on commit 9cf5b41

Please sign in to comment.