Skip to content

Init Tensor with an array of scalar by sharing the memory. #11948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorch+Tensor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,35 @@ public class Tensor<T: Scalar>: Equatable {
))
}

/// Initializes a tensor without copying the data from an existing array.
///
/// - Parameters:
/// - scalars: An `inout` array of scalar values to share memory with.
/// - shape: An array of integers representing the desired tensor shape. If empty, the shape is inferred as `[scalars.count]`.
/// - strides: An array of integers representing the tensor strides.
/// - dimensionOrder: An array of integers indicating the order of dimensions.
/// - shapeDynamism: A `ShapeDynamism` value indicating the shape dynamism.
public convenience init(
_ scalars: inout [T],
shape: [Int] = [],
strides: [Int] = [],
dimensionOrder: [Int] = [],
shapeDynamism: ShapeDynamism = .dynamicBound
) {
let newShape = shape.isEmpty ? [scalars.count] : shape
precondition(scalars.count == elementCount(ofShape: newShape))
self.init(scalars.withUnsafeMutableBufferPointer {
AnyTensor(
bytesNoCopy: $0.baseAddress!,
shape: newShape,
strides: strides,
dimensionOrder: dimensionOrder,
dataType: T.dataType,
shapeDynamism: shapeDynamism
)
})
}

/// Initializes a tensor with an array of scalar values.
///
/// - Parameters:
Expand Down
Loading
Loading