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

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

Initialize a raw view from an existing metal buffer, interpreted as the specified scalar type.

```
init(metalBuffer: borrowing any MTLBuffer, byteOffset: Int = 0, scalarType: NDArray.ScalarType, shape: [Int], strides: [Int] = [], interleaveLayout: NDArray.InterleaveLayout? = nil)
```

## Discussion

`metalBuffer` must have `shared` storage mode.

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

Also note that the `shape/strides` must not be able to produce offsets that go outside of the range of `metalBuffer`.

This initializer is unsafe, you are responsible for ensuring that no other code (or GPU pipeline) writes
to the buffer while the resulting view is alive.

- metalBuffer: The metal buffer to be referenced by the resulting view.
- byteOffset: The offset into this metal buffer 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)