<!--
{
  "availability" : [
    "iOS: 27.0.0 -",
    "iPadOS: 27.0.0 -",
    "tvOS: 27.0.0 -",
    "visionOS: 27.0.0 -",
    "watchOS: 27.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreAI",
  "identifier" : "/documentation/CoreAI/NDArray/RawView/init(bytes:byteOffset:scalarType:shape:strides:interleaveLayout:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Core AI",
      "CoreAI"
    ],
    "preciseIdentifier" : "s:13CoreAIRuntime7NDArrayV7RawViewV5bytes10byteOffset10scalarType5shape7strides16interleaveLayoutAEs0D4SpanV_SiAC06ScalarJ0OSaySiGApC010InterleaveN0VSgtcfc"
  },
  "title" : "init(bytes:byteOffset:scalarType:shape:strides:interleaveLayout:)"
}
-->

# init(bytes:byteOffset:scalarType:shape:strides:interleaveLayout:)

Initialize a raw view from existing raw memory, interpreted as the specified scalar type.

```
init(bytes: RawSpan, byteOffset: Int = 0, scalarType: NDArray.ScalarType, shape: [Int], strides: [Int] = [], interleaveLayout: NDArray.InterleaveLayout? = nil)
```

## Discussion

The lifetime of the resulting view copies the lifetime of the provided span.

Note that the provided `scalarType` will be stored and later checked if you attempt to
convert the raw view to a typed view.

```swift
let myBytes: RawSpan = ...
let ndArrayRawView = NDArray.RawView(bytes: myBytes, scalarType: .float32, shape: [4, 5])

// This conversion is checked. If the type does not match the originally provided scalar type
// it will trap at runtime.
let floatView = ndArrayRawView.view(as: Float32.self)
```

The `shape/strides` must not be able to produce offsets that go outside of the range of `bytes`.
As such, negative strides are not supported by this initializer.

**Performance note**: Making an ndArray view from a normal VM pointer to be used as an input or backing of an [`InferenceFunction`](/documentation/CoreAI/InferenceFunction)
could be a performance anti-pattern if you intend to pass the same underlying memory in repeatedly to many inferences. This can be a function of
which compute units the model was specialized for.
For example if the inference function runs compute on the GPU or Neural Engine, you may incur a copy of the view into the required memory primitive
if it cannot be toll-free converted.

- bytes: The raw bytes to be referenced by the resulting view.
- byteOffset: The offset into the span to be interpreted as the start of this view.
- scalarType: The type of scalars in the provided span.
- shape: The shape of the resulting view.
- strides: The strides of the resulting view. If left empty, they will be computed as contiguous row-major.
- interleaveLayout: Which dimension is interleaved and by what factor.
  See [`NDArray.InterleaveLayout`](/documentation/CoreAI/NDArray/InterleaveLayout-swift.struct).

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)