IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).

Python function

bfloat16_as_uint16_to_float32

bfloat16_as_uint16_to_float32()​

max.driver.bfloat16_as_uint16_to_float32(arr)

source

Converts bfloat16 bit patterns (stored as uint16) to float32.

The conversion is exact: a bfloat16 value is the top 16 bits of a float32.

For example, read a bfloat16 buffer back as float32:

import numpy as np
from max.driver import (
    Buffer,
    bfloat16_as_uint16_to_float32,
    float32_to_bfloat16_as_uint16,
)
from max.dtype import DType

bits = float32_to_bfloat16_as_uint16(np.array([1.5], np.float32))
buffer = Buffer.from_numpy(bits).view(DType.bfloat16)
values = bfloat16_as_uint16_to_float32(
    buffer.view(DType.uint16).to_numpy()
)

Parameters:

arr (ndarray[tuple[Any, ...], dtype[uint16]]) – A uint16 array holding bfloat16 bit patterns.

Returns:

A float32 array with the same shape as arr.

Return type:

ndarray[tuple[Any, …], dtype[float32]]