Initialization and copy routines
Procs
proc copyFrom[T](dst: var Tensor[T]; src: Tensor[T])
-
Copy the source tensor into the destination tensor. Both should have the same shape. If destination tensor is a view only the data exposed by the view is modified.
This is useful to update subslices of an existing tensor.
⚠️ Warning: The data exposed by the destination tensor will be overwritten. If destination tensor is a view, all views of that data will be changed. They however conserve their shape and strides.
Note: The copy is not recursive.
Source Edit proc copyFromRaw[T](dst: var Tensor[T]; buffer: ptr T; len: Natural)
- Copy data from the buffer into the destination tensor. Destination tensor size and buffer length should be the same Source Edit
proc deepCopy[T](dst: var Tensor[T]; src: Tensor[T])
-
Performs a deep copy of y and copies it into x. Deepcopy is recursive including for ref types and custom types that implement deepCopy.
Note that if x was already initialized with a storage, the storage will be detached from x. This does not write into existing storage.
Source Edit proc fromBuffer[T](rawBuffer: pointer; shape: varargs[int]): Tensor[T]
- Call fromBuffer with layout = rowMajor Source Edit
proc fromBuffer[T](rawBuffer: pointer; shape: varargs[int]; layout: static OrderType): Tensor[T]
-
Creates a Tensor[T] from a raw pointer. Make sure that the explicit type given to this proc actually matches the data stored behind the pointer! The size derived from the given shape must match the size of the buffer!
Its counterpart toUnsafeView can be used to obtain ptr UncheckedArray from a Tensor.
Source Edit proc fromBuffer[T](rawBuffer: ptr UncheckedArray[T]; shape: varargs[int]): Tensor[ T]
- Call fromBuffer with layout = rowMajor Source Edit
proc fromBuffer[T](rawBuffer: ptr UncheckedArray[T]; shape: varargs[int]; layout: static OrderType): Tensor[T]
-
Creates a Tensor[T] from a raw buffer, cast as ptr UncheckedArray[T]. The size derived from the given shape must match the size of the buffer!
If you type cast a raw pointer to ptr UncheckedArray[T] before handing it to this proc, make sure to cast to the correct type as we cannot check the validity of the type!
Its counterpart toUnsafeView can be used to obtain ptr UncheckedArray from a Tensor.
Source Edit func initTensorMetadata(result: var Tensor; size: var int; shape: Metadata; layout: static OrderType = rowMajor)
- result metadata and size will be initialized in-place Source Edit
func initTensorMetadata(result: var Tensor; size: var int; shape: openArray[int]; layout: static OrderType = rowMajor)
- result metadata and size will be initialized in-place Source Edit
proc newTensor[T](shape: Metadata): Tensor[T]
- Create a new tensor of type T with the given shape. Source Edit
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.
Source Edit proc setZero[T](t: var Tensor[T]; check_contiguous: static bool = true)
-
Reset/initialize the tensor data to binary zero. The tensor metadata is not touched. Input tensor must be contiguous. For seq based Tensors the underlying sequence will be reset and set back to the tensors size.
⚠️ Warning: The data of the input tensor will be overwritten. If destination tensor is a view, all views of that data will be changed. They however conserve their shape and strides.
Source Edit func toMetadata(s: varargs[int]): Metadata {....raises: [], tags: [], forbids: [].}
- Source Edit
func toUnsafeView[T: KnownSupportsCopyMem](t: Tensor[T]; aligned: static bool = true): ptr UncheckedArray[T] {.inline.}
-
Returns an unsafe view of the valid data as a ptr UncheckedArray. Its counterpart fromBuffer can be used to create a Tensor fromptr UncheckedArray.
Unsafe: the pointer can outlive the input tensor.
Source Edit