File tree Expand file tree Collapse file tree 2 files changed +241
-24
lines changed
extension/apple/ExecuTorch Expand file tree Collapse file tree 2 files changed +241
-24
lines changed Original file line number Diff line number Diff line change @@ -693,6 +693,35 @@ public class Tensor<T: Scalar>: Equatable {
693693 ) )
694694 }
695695
696+ /// Initializes a tensor without copying the data from an existing array.
697+ ///
698+ /// - Parameters:
699+ /// - scalars: An `inout` array of scalar values to share memory with.
700+ /// - shape: An array of integers representing the desired tensor shape. If empty, the shape is inferred as `[scalars.count]`.
701+ /// - strides: An array of integers representing the tensor strides.
702+ /// - dimensionOrder: An array of integers indicating the order of dimensions.
703+ /// - shapeDynamism: A `ShapeDynamism` value indicating the shape dynamism.
704+ public convenience init (
705+ _ scalars: inout [ T ] ,
706+ shape: [ Int ] = [ ] ,
707+ strides: [ Int ] = [ ] ,
708+ dimensionOrder: [ Int ] = [ ] ,
709+ shapeDynamism: ShapeDynamism = . dynamicBound
710+ ) {
711+ let newShape = shape. isEmpty ? [ scalars. count] : shape
712+ precondition ( scalars. count == elementCount ( ofShape: newShape) )
713+ self . init ( scalars. withUnsafeMutableBufferPointer {
714+ AnyTensor (
715+ bytesNoCopy: $0. baseAddress!,
716+ shape: newShape,
717+ strides: strides,
718+ dimensionOrder: dimensionOrder,
719+ dataType: T . dataType,
720+ shapeDynamism: shapeDynamism
721+ )
722+ } )
723+ }
724+
696725 /// Initializes a tensor with an array of scalar values.
697726 ///
698727 /// - Parameters:
You can’t perform that action at this time.
0 commit comments