Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support all float types in MatrixView #1282

Merged
merged 5 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust/lance-arrow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ arrow-schema = { workspace = true }
serde_json = { workspace = true }
serde = { workspace = true }
half = { workspace = true }
num-traits = { workspace = true }
2 changes: 2 additions & 0 deletions rust/lance-arrow/src/bfloat16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use arrow_data::ArrayData;
use arrow_schema::{ArrowError, DataType};
use half::bf16;

pub struct BFloat16Type {}

pub struct BFloat16Array {
inner: FixedSizeBinaryArray,
}
Expand Down
62 changes: 62 additions & 0 deletions rust/lance-arrow/src/floats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2023 Lance Developers.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Floats Array

use arrow_array::{Array, Float16Array, Float32Array, Float64Array};
use half::{bf16, f16};
use num_traits::{Float, FromPrimitive};

use super::bfloat16::BFloat16Array;

/// [FloatArray] is a trait that is implemented by all float type arrays.
pub trait FloatArray: Array + From<Vec<Self::Native>> {
type Native: Float + FromPrimitive;

/// Returns a reference to the underlying data as a slice.
fn as_slice(&self) -> &[Self::Native];
}

impl FloatArray for BFloat16Array {
type Native = bf16;

fn as_slice(&self) -> &[Self::Native] {
// TODO: apache/arrow-rs#4820
todo!()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm it looks like we are blocked here because FSB returns an owned buffer from values().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a TODO to fix this once we have apache/arrow-rs#4820 done and released upstream.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
}

impl FloatArray for Float16Array {
type Native = f16;

fn as_slice(&self) -> &[Self::Native] {
self.values()
}
}

impl FloatArray for Float32Array {
type Native = f32;

fn as_slice(&self) -> &[Self::Native] {
self.values()
}
}

impl FloatArray for Float64Array {
type Native = f64;

fn as_slice(&self) -> &[Self::Native] {
self.values()
}
}
2 changes: 2 additions & 0 deletions rust/lance-arrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use arrow_schema::{ArrowError, DataType, Field, FieldRef, Fields, Schema};
pub mod schema;
pub use schema::*;
pub mod bfloat16;
pub mod floats;
pub use floats::*;

type Result<T> = std::result::Result<T, ArrowError>;

Expand Down
7 changes: 3 additions & 4 deletions rust/lance-linalg/src/distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,9 @@ impl TryFrom<&str> for DistanceType {
"l2" | "euclidean" => Ok(Self::L2),
"cosine" => Ok(Self::Cosine),
"dot" => Ok(Self::Dot),
_ => Err(ArrowError::InvalidArgumentError(
format!("Metric type '{s}' is not supported,")
+ "only 'l2'/'euclidean', 'cosine', and 'dot' are supported.",
)),
_ => Err(ArrowError::InvalidArgumentError(format!(
"Metric type '{s}' is not supported"
))),
}
}
}
18 changes: 9 additions & 9 deletions rust/lance-linalg/src/kmeans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ use std::cmp::min;
use std::collections::HashSet;
use std::sync::Arc;

use arrow_array::cast::AsArray;
use arrow_array::FixedSizeListArray;
use arrow_array::types::Float32Type;
use arrow_array::{
builder::Float32Builder, cast::as_primitive_array, new_empty_array, Array, Float32Array,
cast::{as_primitive_array, AsArray},
new_empty_array, Array, FixedSizeListArray, Float32Array,
};
use arrow_schema::{ArrowError, DataType};
use arrow_select::concat::concat;
Expand Down Expand Up @@ -154,12 +154,12 @@ async fn kmeans_random_init(
let chosen = (0..data.len() / dimension)
.choose_multiple(&mut rng, k)
.to_vec();
let mut builder = Float32Builder::with_capacity(k * dimension);
let mut builder: Vec<f32> = Vec::with_capacity(k * dimension);
for i in chosen {
builder.append_slice(&data.values()[i * dimension..(i + 1) * dimension]);
builder.extend(data.values()[i * dimension..(i + 1) * dimension].iter());
}
let mut kmeans = KMeans::empty(k, dimension, metric_type);
kmeans.centroids = Arc::new(builder.finish());
kmeans.centroids = Arc::new(builder.into());
Ok(kmeans)
}

Expand Down Expand Up @@ -246,7 +246,7 @@ impl KMeanMembership {
}
let centroids = concat(&mean_refs).unwrap();
Ok(KMeans {
centroids: Arc::new(as_primitive_array(centroids.as_ref()).clone()),
centroids: Arc::new(centroids.as_ref().as_primitive::<Float32Type>().clone()),
dimension,
k: self.k,
metric_type: self.metric_type,
Expand Down Expand Up @@ -319,7 +319,7 @@ impl KMeans {
/// - *metric_type*: the metric type to calculate distance.
/// - *rng*: random generator.
pub async fn init_random(
data: &MatrixView,
data: &MatrixView<Float32Array>,
k: usize,
metric_type: MetricType,
rng: impl Rng,
Expand Down Expand Up @@ -431,7 +431,7 @@ impl KMeans {
/// let kmeans = membership.to_kmeans();
/// }
/// ```
pub async fn train_once(&self, data: &MatrixView) -> KMeanMembership {
pub async fn train_once(&self, data: &MatrixView<Float32Array>) -> KMeanMembership {
self.compute_membership(data.data().clone()).await
}

Expand Down
Loading