-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathlib.rs
More file actions
81 lines (64 loc) · 3.09 KB
/
Copy pathlib.rs
File metadata and controls
81 lines (64 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
//! Types and functionality for working with tensors, vectors, and related mathematical constructs
//! including unit vectors, spherical coordinates, and similarity measures such as cosine
//! similarity.
use vortex_array::arrays::scalar_fn::plugin::ScalarFnArrayPlugin;
use vortex_array::dtype::session::DTypeSessionExt;
use vortex_array::scalar_fn::session::ScalarFnSessionExt;
use vortex_array::session::ArraySessionExt;
use vortex_session::VortexSession;
use crate::fixed_shape::FixedShapeTensor;
use crate::scalar_fns::cosine_similarity::CosineSimilarity;
use crate::scalar_fns::inner_product::InnerProduct;
use crate::scalar_fns::l2_denorm::L2Denorm;
use crate::scalar_fns::l2_norm::L2Norm;
use crate::scalar_fns::sorf_transform::SorfTransform;
use crate::vector::Vector;
pub mod matcher;
pub mod scalar_fns;
pub mod fixed_shape;
pub mod vector;
pub mod encodings;
pub mod vector_search;
mod utils;
/// Environment variable that gates registration of the tensor scalar-fn array plugins (the array
/// encodings that let [`CosineSimilarity`], [`InnerProduct`], [`L2Denorm`], [`L2Norm`], and
/// [`SorfTransform`] persist in a Vortex file). When unset, only the scalar functions themselves
/// are registered; readers of files containing serialized tensor scalar-fn arrays will fail to
/// deserialize. Opt-in by setting the variable to any non-empty value.
pub const SCALAR_FN_ARRAY_TENSOR_PLUGIN_ENV: &str = "VX_SCALAR_FN_ARRAY_TENSOR_PLUGIN";
/// Initialize the Vortex tensor library with a Vortex session.
pub fn initialize(session: &VortexSession) {
session.dtypes().register(Vector);
session.dtypes().register(FixedShapeTensor);
let session_fns = session.scalar_fns();
session_fns.register(CosineSimilarity);
session_fns.register(InnerProduct);
session_fns.register(L2Denorm);
session_fns.register(L2Norm);
session_fns.register(SorfTransform);
// Registering the scalar-fn array plugins lets the tensor scalar fns be serialized as array
// encodings inside Vortex files. Gate this on an env var so applications that do not intend
// to persist these encodings do not pay the registry cost or widen their stable-encoding
// surface unintentionally.
if std::env::var_os(SCALAR_FN_ARRAY_TENSOR_PLUGIN_ENV).is_some_and(|v| !v.is_empty()) {
let session_arrays = session.arrays();
session_arrays.register(ScalarFnArrayPlugin::new(CosineSimilarity));
session_arrays.register(ScalarFnArrayPlugin::new(InnerProduct));
session_arrays.register(ScalarFnArrayPlugin::new(L2Denorm));
session_arrays.register(ScalarFnArrayPlugin::new(L2Norm));
session_arrays.register(ScalarFnArrayPlugin::new(SorfTransform));
}
}
#[cfg(test)]
mod tests {
use std::sync::LazyLock;
use vortex_array::session::ArraySession;
use vortex_session::VortexSession;
pub static SESSION: LazyLock<VortexSession> = LazyLock::new(|| {
let session = VortexSession::empty().with::<ArraySession>();
crate::initialize(&session);
session
});
}