# PinnedMemory

```python
class PinnedMemory
```

An NPU-accessible pinned (physically contiguous) memory buffer.

A [PinnedMemory](<https://docs.mobilint.com/runtime/en/v1.4/api/python/PinnedMemory/index.md>) wraps a buffer that is pinned in device memory so the NPU can read from / write to it directly, enabling zero-copy inference via [`Model.infer_pinned_memory`](<https://docs.mobilint.com/runtime/en/v1.4/api/python/Model/#infer-pinned-memory>). Instances are created through [`Accelerator.allocate_pinned_memory`](<https://docs.mobilint.com/runtime/en/v1.4/api/python/Accelerator/#allocate-pinned-memory>), not constructed directly.

> [!NOTE]
>
> This is an experimental API and is only supported on REGULUS device.

## Attributes

| Name | Description |
| --- | --- |
| [`shape`](<#shape>) | Returns the shape of the buffer. |
| [`dtype`](<#dtype>) | Returns the numpy dtype of the buffer's elements. |
| [`size`](<#size>) | Returns the size of the buffer in bytes. |
| [`is_available`](<#is-available>) | Returns whether the buffer is allocated and still usable. |

### shape

```python
property shape: _Shape
```

Returns the shape of the buffer.

### dtype

```python
property dtype: np.dtype
```

Returns the numpy dtype of the buffer's elements.

### size

```python
property size: int
```

Returns the size of the buffer in bytes.

### is\_available

```python
property is_available: bool
```

Returns whether the buffer is allocated and still usable.

## Methods

| Name | Description |
| --- | --- |
| [`__init__`](<#init>) | Internal constructor. |
| [`__getitem__`](<#getitem>) | Reads element(s) from the pinned buffer via indexing. |
| [`__setitem__`](<#setitem>) | Writes value(s) into the pinned buffer via indexing. |
| [`__len__`](<#len>) | Returns the length of the buffer's first dimension. |

### \_\_init\_\_

```python
def __init__(
    self,
    _pinned_memory: Union[
        _cQbRuntime.PinnedMemoryFloat, _cQbRuntime.PinnedMemoryUint8
    ],
)
```

Internal constructor.

**Parameters**

- `_pinned_memory` — The underlying C++ pinned-memory object. Use [`Accelerator.allocate_pinned_memory`](<https://docs.mobilint.com/runtime/en/v1.4/api/python/Accelerator/#allocate-pinned-memory>) to create instances.

### \_\_getitem\_\_

```python
def __getitem__(self, key)
```

Reads element(s) from the pinned buffer via indexing.

Supports the same indexing as numpy (integers, slices, tuples, etc.), e.g. `pm[0]`, `pm[1, 2]`, `pm[:10]`. The data is read directly from the pinned buffer without an intermediate copy.

**Parameters**

- `key` — The index or slice.

**Returns**

The selected element or a view into the pinned buffer.

### \_\_setitem\_\_

```python
def __setitem__(self, key, value)
```

Writes value(s) into the pinned buffer via indexing.

Writes directly into the pinned buffer, e.g. `pm[0] = 1.0`, `pm[:10] = src`. This is the recommended way to fill an input buffer before [`Model.infer_pinned_memory`](<https://docs.mobilint.com/runtime/en/v1.4/api/python/Model/#infer-pinned-memory>).

**Parameters**

- `key` — The index or slice.
- `value` — The value(s) to write.

### \_\_len\_\_

```python
def __len__(self) -> int
```

Returns the length of the buffer's first dimension.
