Skip to main content

lance_arrow/
bfloat16.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4//! bfloat16 support for Apache Arrow.
5
6use std::fmt::Formatter;
7use std::slice;
8
9use arrow_array::{Array, FixedSizeBinaryArray, builder::BooleanBufferBuilder};
10use arrow_buffer::{Buffer, MutableBuffer};
11use arrow_data::ArrayData;
12use arrow_schema::{ArrowError, DataType, Field as ArrowField};
13use half::bf16;
14
15use crate::{ARROW_EXT_NAME_KEY, FloatArray};
16
17/// The name of the bfloat16 extension in Arrow metadata
18pub const BFLOAT16_EXT_NAME: &str = "lance.bfloat16";
19
20/// Check whether the given field is a bfloat16 field
21///
22/// A field is a bfloat16 field if it has a data type of `FixedSizeBinary(2)` and the metadata
23/// contains the bfloat16 extension name.
24pub fn is_bfloat16_field(field: &ArrowField) -> bool {
25    field.data_type() == &DataType::FixedSizeBinary(2)
26        && field
27            .metadata()
28            .get(ARROW_EXT_NAME_KEY)
29            .map(|name| name == BFLOAT16_EXT_NAME)
30            .unwrap_or_default()
31}
32
33/// The bfloat16 data type
34///
35/// This implements the [`ArrowFloatType`](crate::floats::ArrowFloatType) trait for bfloat16 values.
36#[derive(Debug)]
37pub struct BFloat16Type {}
38
39/// An array of bfloat16 values
40///
41/// Note that bfloat16 is not the same thing as fp16 which is supported natively by arrow-rs.
42#[derive(Clone)]
43pub struct BFloat16Array {
44    inner: FixedSizeBinaryArray,
45}
46
47impl std::fmt::Debug for BFloat16Array {
48    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
49        write!(f, "BFloat16Array\n[\n")?;
50        from_arrow::print_long_array(&self.inner, f, |array, i, f| {
51            if array.is_null(i) {
52                write!(f, "null")
53            } else {
54                let binary_values = array.value(i);
55                let value =
56                    bf16::from_bits(u16::from_le_bytes([binary_values[0], binary_values[1]]));
57                write!(f, "{:?}", value)
58            }
59        })?;
60        write!(f, "]")
61    }
62}
63
64impl BFloat16Array {
65    pub fn from_iter_values(iter: impl IntoIterator<Item = bf16>) -> Self {
66        let values: Vec<bf16> = iter.into_iter().collect();
67        values.into()
68    }
69
70    pub fn len(&self) -> usize {
71        self.inner.len()
72    }
73
74    pub fn is_empty(&self) -> bool {
75        self.inner.is_empty()
76    }
77
78    pub fn is_null(&self, i: usize) -> bool {
79        self.inner.is_null(i)
80    }
81
82    pub fn null_count(&self) -> usize {
83        self.inner.null_count()
84    }
85
86    pub fn iter(&self) -> BFloat16Iter<'_> {
87        BFloat16Iter {
88            array: self,
89            index: 0,
90        }
91    }
92
93    pub fn value(&self, i: usize) -> bf16 {
94        assert!(
95            i < self.len(),
96            "Trying to access an element at index {} from a BFloat16Array of length {}",
97            i,
98            self.len()
99        );
100        // Safety:
101        // `i < self.len()
102        unsafe { self.value_unchecked(i) }
103    }
104
105    /// # Safety
106    /// Caller must ensure that `i < self.len()`
107    pub unsafe fn value_unchecked(&self, i: usize) -> bf16 {
108        let binary_value = self.inner.value_unchecked(i);
109        bf16::from_bits(u16::from_le_bytes([binary_value[0], binary_value[1]]))
110    }
111
112    pub fn into_inner(self) -> FixedSizeBinaryArray {
113        self.inner
114    }
115}
116
117impl FromIterator<Option<bf16>> for BFloat16Array {
118    fn from_iter<I: IntoIterator<Item = Option<bf16>>>(iter: I) -> Self {
119        let mut buffer = MutableBuffer::new(10);
120        // No null buffer builder :(
121        let mut nulls = BooleanBufferBuilder::new(10);
122        let mut len = 0;
123
124        for maybe_value in iter {
125            if let Some(value) = maybe_value {
126                let bytes = value.to_le_bytes();
127                buffer.extend(bytes);
128            } else {
129                buffer.extend([0u8, 0u8]);
130            }
131            nulls.append(maybe_value.is_some());
132            len += 1;
133        }
134
135        let null_buffer = nulls.finish();
136        let num_valid = null_buffer.count_set_bits();
137        let null_buffer = if num_valid == len {
138            None
139        } else {
140            Some(null_buffer.into_inner())
141        };
142
143        let array_data = ArrayData::builder(DataType::FixedSizeBinary(2))
144            .len(len)
145            .add_buffer(buffer.into())
146            .null_bit_buffer(null_buffer);
147        // SAFETY: the value buffer contains exactly `2 * len` bytes (two bytes
148        // pushed per iteration of the loop above, including the zero-fill for
149        // null slots), which matches the `FixedSizeBinary(2)` storage layout.
150        // The null bit buffer, when present, has `len` bits appended above, so
151        // its length covers the array's logical range.
152        let array_data = unsafe { array_data.build_unchecked() };
153        Self {
154            inner: FixedSizeBinaryArray::from(array_data),
155        }
156    }
157}
158
159impl FromIterator<bf16> for BFloat16Array {
160    fn from_iter<I: IntoIterator<Item = bf16>>(iter: I) -> Self {
161        Self::from_iter_values(iter)
162    }
163}
164
165impl From<Vec<bf16>> for BFloat16Array {
166    fn from(data: Vec<bf16>) -> Self {
167        let len = data.len();
168        // Zero-copy: `bf16` is `#[repr(transparent)]` over `u16` and derives
169        // `bytemuck::Pod`, so `cast_vec` reinterprets the allocation in place —
170        // no per-element copy or heap alloc. The crate-root `compile_error!`
171        // pins `target_endian = "little"`, so the resulting bytes match the
172        // `FixedSizeBinary(2)` on-disk order Lance writes elsewhere.
173        let raw: Vec<u16> = bytemuck::cast_vec(data);
174        let array_data = ArrayData::builder(DataType::FixedSizeBinary(2))
175            .len(len)
176            .add_buffer(Buffer::from_vec(raw));
177        // SAFETY: the value buffer contains exactly `2 * len` bytes — one
178        // `u16` per element after the layout-compatible cast — matching the
179        // `FixedSizeBinary(2)` storage layout. No null buffer is attached, so
180        // every element is logically valid.
181        let array_data = unsafe { array_data.build_unchecked() };
182        Self {
183            inner: FixedSizeBinaryArray::from(array_data),
184        }
185    }
186}
187
188impl TryFrom<FixedSizeBinaryArray> for BFloat16Array {
189    type Error = ArrowError;
190
191    fn try_from(value: FixedSizeBinaryArray) -> Result<Self, Self::Error> {
192        if value.value_length() == 2 {
193            Ok(Self { inner: value })
194        } else {
195            Err(ArrowError::InvalidArgumentError(
196                "FixedSizeBinaryArray must have a value length of 2".to_string(),
197            ))
198        }
199    }
200}
201
202impl PartialEq<Self> for BFloat16Array {
203    fn eq(&self, other: &Self) -> bool {
204        self.inner.eq(&other.inner)
205    }
206}
207
208pub struct BFloat16Iter<'a> {
209    array: &'a BFloat16Array,
210    index: usize,
211}
212
213impl<'a> Iterator for BFloat16Iter<'a> {
214    type Item = Option<bf16>;
215
216    fn next(&mut self) -> Option<Self::Item> {
217        if self.index >= self.array.len() {
218            return None;
219        }
220        let i = self.index;
221        self.index += 1;
222        if self.array.is_null(i) {
223            Some(None)
224        } else {
225            Some(Some(self.array.value(i)))
226        }
227    }
228}
229
230/// Methods that are lifted from arrow-rs temporarily until they are made public.
231mod from_arrow {
232    use arrow_array::Array;
233
234    /// Helper function for printing potentially long arrays.
235    pub(super) fn print_long_array<A, F>(
236        array: &A,
237        f: &mut std::fmt::Formatter,
238        print_item: F,
239    ) -> std::fmt::Result
240    where
241        A: Array,
242        F: Fn(&A, usize, &mut std::fmt::Formatter) -> std::fmt::Result,
243    {
244        let head = std::cmp::min(10, array.len());
245
246        for i in 0..head {
247            if array.is_null(i) {
248                writeln!(f, "  null,")?;
249            } else {
250                write!(f, "  ")?;
251                print_item(array, i, f)?;
252                writeln!(f, ",")?;
253            }
254        }
255        if array.len() > 10 {
256            if array.len() > 20 {
257                writeln!(f, "  ...{} elements...,", array.len() - 20)?;
258            }
259
260            let tail = std::cmp::max(head, array.len() - 10);
261
262            for i in tail..array.len() {
263                if array.is_null(i) {
264                    writeln!(f, "  null,")?;
265                } else {
266                    write!(f, "  ")?;
267                    print_item(array, i, f)?;
268                    writeln!(f, ",")?;
269                }
270            }
271        }
272        Ok(())
273    }
274}
275
276impl FloatArray<BFloat16Type> for FixedSizeBinaryArray {
277    type FloatType = BFloat16Type;
278
279    /// Returns the underlying `bf16` values as a borrowed slice.
280    ///
281    /// # Preconditions
282    ///
283    /// - `value_length()` must be 2 (the `FixedSizeBinary(2)` storage shape
284    ///   used by [`BFloat16Array`]). Asserted at entry.
285    /// - The value buffer must be at least 2-byte aligned. Lance's in-tree
286    ///   constructors always satisfy this: value buffers are built either via
287    ///   `MutableBuffer` (aligned to arrow-buffer's `ALIGNMENT` constant, ≥32
288    ///   bytes) or via `Buffer::from_vec::<u16>` (aligned to `align_of::<u16>()`
289    ///   == 2); both meet `bf16`'s 2-byte requirement. Externally-built
290    ///   `FixedSizeBinaryArray`s arriving via FFI, IPC, or
291    ///   `Buffer::from_custom_allocation` are not required by arrow-rs to be
292    ///   aligned beyond a single byte; passing one to this method violates the
293    ///   precondition. A `debug_assert` below catches such inputs in debug and
294    ///   test builds.
295    ///
296    /// # Endianness
297    ///
298    /// `lance-arrow` is gated on `target_endian = "little"` at the crate root,
299    /// so this method always returns values in the same byte order Lance writes
300    /// (see [`BFloat16Array::value`] and the [`FromIterator`] impls).
301    fn as_slice(&self) -> &[bf16] {
302        assert_eq!(
303            self.value_length(),
304            2,
305            "BFloat16 arrays must use FixedSizeBinary(2) storage"
306        );
307        debug_assert_eq!(
308            (self.value_data().as_ptr() as usize) % std::mem::align_of::<bf16>(),
309            0,
310            "BFloat16 value buffer must be at least 2-byte aligned"
311        );
312        // SAFETY:
313        // - The assert above pins `value_size == 2`, so `value_data().len() / 2`
314        //   equals the array's logical element count.
315        //   `FixedSizeBinaryArray::From<ArrayData>` constructs its value buffer
316        //   as `buffers[0].slice_with_length(offset * 2, len * 2)` (arrow-array
317        //   `fixed_size_binary_array.rs`), so `value_data()` already returns
318        //   the offset-adjusted slice. Do not replace `value_data()` with an
319        //   accessor that returns the un-sliced backing buffer.
320        // - `bf16` is `#[repr(transparent)]` over `u16` (size 2, alignment 2);
321        //   every `u16` bit pattern is a valid `bf16`, so any byte content
322        //   yields a defined value — never UB.
323        // - Alignment is the caller's responsibility per the precondition
324        //   documented above. The `debug_assert_eq!` immediately preceding this
325        //   block catches violations in debug and test builds only — release
326        //   builds rely on callers honoring the precondition. arrow-rs
327        //   declares `FixedSizeBinary(n)`'s
328        //   `BufferSpec::FixedWidth { alignment: align_of::<u8>() == 1 }`
329        //   (arrow-data `data.rs`), so arrow-rs alone does not guarantee
330        //   2-byte alignment. Lance's in-tree construction paths build value
331        //   buffers via `MutableBuffer` (arrow-buffer `ALIGNMENT` constant,
332        //   ≥32 bytes) or `Buffer::from_vec::<u16>` (2-byte aligned), both of
333        //   which satisfy `bf16`'s 2-byte requirement.
334        // - The returned slice borrows from `self`; the underlying ref-counted,
335        //   immutable Arrow buffer cannot be mutated or freed for the slice's
336        //   lifetime.
337        unsafe {
338            slice::from_raw_parts(
339                self.value_data().as_ptr() as *const bf16,
340                self.value_data().len() / 2,
341            )
342        }
343    }
344
345    fn from_values(values: Vec<bf16>) -> Self {
346        BFloat16Array::from(values).into_inner()
347    }
348}
349
350#[cfg(test)]
351mod tests {
352    use super::*;
353
354    #[test]
355    fn test_basics() {
356        let values: Vec<f32> = vec![1.0, 2.0, 3.0];
357        let values: Vec<bf16> = values.iter().map(|v| bf16::from_f32(*v)).collect();
358
359        let array = BFloat16Array::from_iter_values(values.clone());
360        let array2 = BFloat16Array::from(values.clone());
361        assert_eq!(array, array2);
362        assert_eq!(array.len(), 3);
363
364        // Pin the raw little-endian bytes emitted by `From<Vec<bf16>>` (rewritten to
365        // reinterpret the Vec via `bytemuck::cast_vec`), so a layout/byte-order
366        // regression is caught directly rather than only through Debug formatting.
367        // bf16 is the high 16 bits of the f32: 1.0->0x3F80, 2.0->0x4000, 3.0->0x4040.
368        let inner = array2.clone().into_inner();
369        let raw_bytes: Vec<u8> = (0..inner.len())
370            .flat_map(|i| inner.value(i).to_vec())
371            .collect();
372        assert_eq!(raw_bytes, vec![0x80, 0x3F, 0x00, 0x40, 0x40, 0x40]);
373
374        let expected_fmt = "BFloat16Array\n[\n  1.0,\n  2.0,\n  3.0,\n]";
375        assert_eq!(expected_fmt, format!("{:?}", array));
376
377        for (expected, value) in values.iter().zip(array.iter()) {
378            assert_eq!(Some(*expected), value);
379        }
380
381        for (expected, value) in values.as_slice().iter().zip(array2.iter()) {
382            assert_eq!(Some(*expected), value);
383        }
384
385        let arrow_array = array.into_inner();
386        assert_eq!(arrow_array.as_slice(), values.as_slice());
387    }
388
389    #[test]
390    fn test_nulls() {
391        let values: Vec<Option<bf16>> =
392            vec![Some(bf16::from_f32(1.0)), None, Some(bf16::from_f32(3.0))];
393        let array = BFloat16Array::from_iter(values.clone());
394        assert_eq!(array.len(), 3);
395        assert_eq!(array.null_count(), 1);
396
397        let expected_fmt = "BFloat16Array\n[\n  1.0,\n  null,\n  3.0,\n]";
398        assert_eq!(expected_fmt, format!("{:?}", array));
399
400        for (expected, value) in values.iter().zip(array.iter()) {
401            assert_eq!(*expected, value);
402        }
403    }
404}