def to_float16_array(
    array: np.ndarray, out: ndarrays_pb2.Float16Array | None = None
) -> ndarrays_pb2.Float16Array:
    """Populate a new or provided Float16Array message with given numpy array.

    Args:
        array: Array to add.
        out: Message to add to. If None, create new message.
    """
    if out is None:
        out = ndarrays_pb2.Float16Array()
    endianness, dtype = _to_dtype(array.dtype, "f2")
    out.shape[:] = array.shape
    out.endianness = endianness
    out.flat_bytes = array.astype(dtype, copy=False).tobytes()
    return out
Populate a new or provided Float16Array message with given numpy array.

    Args:
        array: Array to add.
        out: Message to add to. If None, create new message.
    
cirq-google/cirq_google/api/v2/ndarrays.py