dag-ml-data 0.2.17__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/PKG-INFO +1 -1
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/Cargo.toml +2 -2
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/buffer.rs +13 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/error.rs +8 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/lib.rs +2 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/model.rs +129 -3
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/nd_tensor.rs +98 -27
- dag_ml_data-0.3.0/dag-ml-data-core/src/source_payload.rs +431 -0
- dag_ml_data-0.3.0/dag-ml-data-core/tests/adapter_contribution.rs +64 -0
- dag_ml_data-0.3.0/dag-ml-data-core/tests/fixtures/image_first_pixel.adapter.json +15 -0
- dag_ml_data-0.3.0/dag-ml-data-core/tests/fixtures/source_payload.v1.json +39 -0
- dag_ml_data-0.3.0/dag-ml-data-core/tests/source_payload.rs +87 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-py/Cargo.lock +4 -4
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-py/Cargo.toml +2 -2
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/pyproject.toml +1 -1
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/LICENSE +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/adapter.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/aggregation.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/alignment.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/buffer_file_store.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/builtin_models.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/collation.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/content_hash.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/coordinator.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/fingerprint.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/fitted_adapter.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/fusion.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/handle.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/ids.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/plan.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/planner.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/predict_cohort.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/relation.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-core/src/representation_registry.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-py/.gitignore +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-py/LICENSE +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-py/README.md +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/dag-ml-data-py/src/lib.rs +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/python/dag_ml_data/__init__.py +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/python/dag_ml_data/__init__.pyi +0 -0
- {dag_ml_data-0.2.17 → dag_ml_data-0.3.0}/python/dag_ml_data/py.typed +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "dag-ml-data-core"
|
|
3
3
|
description = "Core data contracts, schemas and plans for dag-ml-data."
|
|
4
|
-
version = "0.
|
|
4
|
+
version = "0.3.0"
|
|
5
5
|
edition = "2021"
|
|
6
6
|
rust-version = "1.88.0"
|
|
7
7
|
license = "CECILL-2.1 OR AGPL-3.0-or-later"
|
|
@@ -12,5 +12,5 @@ homepage = "https://github.com/GBeurier/dag-ml-data"
|
|
|
12
12
|
[dependencies]
|
|
13
13
|
serde = { version = "^1.0", features = ["derive"] }
|
|
14
14
|
serde_json = { version = "^1.0", features = ["float_roundtrip"] }
|
|
15
|
-
sha2 = "=0.11.
|
|
15
|
+
sha2 = "=0.11.1"
|
|
16
16
|
thiserror = "^2.0"
|
|
@@ -8,8 +8,10 @@ use crate::error::{DataError, Result};
|
|
|
8
8
|
use crate::handle::{CoordinatorFeatureBlock, CoordinatorFeatureBlockF64, CoordinatorFeatureTable};
|
|
9
9
|
use crate::ids::{ObservationId, RepresentationId, SourceId};
|
|
10
10
|
|
|
11
|
+
/// Layout version of [`NumericFeatureBufferManifest`].
|
|
11
12
|
pub const NUMERIC_FEATURE_BUFFER_MANIFEST_SCHEMA_VERSION: u32 = 1;
|
|
12
13
|
|
|
14
|
+
/// Identity-indexed numeric columns with per-cell missing values.
|
|
13
15
|
#[derive(Clone, Debug, PartialEq)]
|
|
14
16
|
pub struct NumericFeatureBuffer {
|
|
15
17
|
pub feature_set_id: String,
|
|
@@ -20,6 +22,7 @@ pub struct NumericFeatureBuffer {
|
|
|
20
22
|
row_index_by_observation: BTreeMap<ObservationId, usize>,
|
|
21
23
|
}
|
|
22
24
|
|
|
25
|
+
/// Provider-wide descriptor and fingerprint for one numeric feature buffer.
|
|
23
26
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
|
24
27
|
pub struct NumericFeatureBufferManifest {
|
|
25
28
|
pub schema_version: u32,
|
|
@@ -34,6 +37,7 @@ pub struct NumericFeatureBufferManifest {
|
|
|
34
37
|
pub buffer_fingerprint: String,
|
|
35
38
|
}
|
|
36
39
|
|
|
40
|
+
/// Record of one numeric feature buffer bound to a materialized data handle.
|
|
37
41
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
|
38
42
|
pub struct NumericFeatureBufferBinding {
|
|
39
43
|
pub feature_set_id: String,
|
|
@@ -44,6 +48,7 @@ pub struct NumericFeatureBufferBinding {
|
|
|
44
48
|
pub buffer_fingerprint: String,
|
|
45
49
|
}
|
|
46
50
|
|
|
51
|
+
/// Row-major f64 input with one observation ID per row and optional cell validity.
|
|
47
52
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
48
53
|
pub struct NumericFeatureMatrixF64 {
|
|
49
54
|
pub feature_set_id: String,
|
|
@@ -55,6 +60,7 @@ pub struct NumericFeatureMatrixF64 {
|
|
|
55
60
|
pub validity_mask: Option<Vec<bool>>,
|
|
56
61
|
}
|
|
57
62
|
|
|
63
|
+
/// Column-major f64 input with shared row identities and optional per-column masks.
|
|
58
64
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
59
65
|
pub struct NumericFeatureMatrixF64Columnar {
|
|
60
66
|
pub feature_set_id: String,
|
|
@@ -66,11 +72,13 @@ pub struct NumericFeatureMatrixF64Columnar {
|
|
|
66
72
|
pub validity_masks: Option<Vec<Vec<bool>>>,
|
|
67
73
|
}
|
|
68
74
|
|
|
75
|
+
/// Validated numeric buffers keyed by their feature-set identifiers.
|
|
69
76
|
#[derive(Clone, Debug, Default, PartialEq)]
|
|
70
77
|
pub struct NumericFeatureBufferStore {
|
|
71
78
|
buffers: BTreeMap<String, NumericFeatureBuffer>,
|
|
72
79
|
}
|
|
73
80
|
|
|
81
|
+
/// Immutable numeric buffers with bindings scoped to materialized data handles.
|
|
74
82
|
#[derive(Clone, Debug, Default, PartialEq)]
|
|
75
83
|
pub struct NumericFeatureBufferArena {
|
|
76
84
|
store: NumericFeatureBufferStore,
|
|
@@ -794,6 +802,7 @@ impl NumericFeatureBufferStore {
|
|
|
794
802
|
}
|
|
795
803
|
|
|
796
804
|
impl NumericFeatureBufferArena {
|
|
805
|
+
/// Creates an arena with the supplied store and no live handle bindings.
|
|
797
806
|
pub fn new(store: NumericFeatureBufferStore) -> Self {
|
|
798
807
|
Self {
|
|
799
808
|
store,
|
|
@@ -801,10 +810,12 @@ impl NumericFeatureBufferArena {
|
|
|
801
810
|
}
|
|
802
811
|
}
|
|
803
812
|
|
|
813
|
+
/// Lists the provider-wide buffer manifests independently of handle lifetime.
|
|
804
814
|
pub fn manifests(&self) -> Result<Vec<NumericFeatureBufferManifest>> {
|
|
805
815
|
self.store.manifests()
|
|
806
816
|
}
|
|
807
817
|
|
|
818
|
+
/// Stores bindings for matching representations and covered source observations.
|
|
808
819
|
pub fn bind_data_handle(
|
|
809
820
|
&mut self,
|
|
810
821
|
data_handle: u64,
|
|
@@ -825,10 +836,12 @@ impl NumericFeatureBufferArena {
|
|
|
825
836
|
Ok(bindings)
|
|
826
837
|
}
|
|
827
838
|
|
|
839
|
+
/// Removes the data handle's bindings. Returns whether a binding record existed.
|
|
828
840
|
pub fn release_data_handle(&mut self, data_handle: u64) -> bool {
|
|
829
841
|
self.data_bindings.remove(&data_handle).is_some()
|
|
830
842
|
}
|
|
831
843
|
|
|
844
|
+
/// Lists the buffer bindings of a data handle. A missing binding record is an error.
|
|
832
845
|
pub fn bindings_for_data_handle(
|
|
833
846
|
&self,
|
|
834
847
|
data_handle: u64,
|
|
@@ -21,11 +21,17 @@ pub struct DataErrorDescriptor {
|
|
|
21
21
|
pub context: BTreeMap<String, Value>,
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
/// Validation, compatibility and lifecycle errors returned by data contracts.
|
|
25
|
+
///
|
|
26
|
+
/// Bindings can use [`DataError::descriptor`] to export the stable diagnostic
|
|
27
|
+
/// category, code and context with a human-readable message.
|
|
24
28
|
#[derive(Debug, Error)]
|
|
25
29
|
pub enum DataError {
|
|
30
|
+
/// An identifier failed the grammar for its typed identifier wrapper.
|
|
26
31
|
#[error("invalid identifier `{value}`: {reason}")]
|
|
27
32
|
InvalidIdentifier { value: String, reason: &'static str },
|
|
28
33
|
|
|
34
|
+
/// Input, projection or a provider definition failed semantic validation.
|
|
29
35
|
#[error("data contract validation failed: {0}")]
|
|
30
36
|
Validation(String),
|
|
31
37
|
|
|
@@ -66,6 +72,7 @@ pub enum DataError {
|
|
|
66
72
|
actual: &'static str,
|
|
67
73
|
},
|
|
68
74
|
|
|
75
|
+
/// The JSON payload could not be decoded or encoded for the contract.
|
|
69
76
|
#[error("serialization error: {0}")]
|
|
70
77
|
Serialization(#[from] serde_json::Error),
|
|
71
78
|
}
|
|
@@ -223,6 +230,7 @@ impl DataError {
|
|
|
223
230
|
}
|
|
224
231
|
}
|
|
225
232
|
|
|
233
|
+
/// The result type shared by data contracts, providers and bindings.
|
|
226
234
|
pub type Result<T> = std::result::Result<T, DataError>;
|
|
227
235
|
|
|
228
236
|
#[cfg(test)]
|
|
@@ -29,6 +29,7 @@ pub mod planner;
|
|
|
29
29
|
pub mod predict_cohort;
|
|
30
30
|
pub mod relation;
|
|
31
31
|
pub mod representation_registry;
|
|
32
|
+
pub mod source_payload;
|
|
32
33
|
|
|
33
34
|
pub use adapter::*;
|
|
34
35
|
pub use aggregation::*;
|
|
@@ -51,3 +52,4 @@ pub use planner::*;
|
|
|
51
52
|
pub use predict_cohort::*;
|
|
52
53
|
pub use relation::*;
|
|
53
54
|
pub use representation_registry::*;
|
|
55
|
+
pub use source_payload::*;
|
|
@@ -76,7 +76,10 @@ pub enum CoordinateDType {
|
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
/// Axis coordinate values: either an explicit per-index list or a regular
|
|
79
|
-
/// numeric grid
|
|
79
|
+
/// numeric grid with one value per axis index. The index is converted to
|
|
80
|
+
/// binary64 and `start + i * step` uses separate binary64 multiplication and
|
|
81
|
+
/// addition. The represented coordinates must be finite and strictly ordered
|
|
82
|
+
/// in the direction of `step`.
|
|
80
83
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
81
84
|
#[serde(tag = "kind", rename_all = "snake_case")]
|
|
82
85
|
pub enum CoordinateValues {
|
|
@@ -128,11 +131,11 @@ impl CoordinateSpec {
|
|
|
128
131
|
"axis `{axis_name}` regular-grid coordinates require numeric dtype"
|
|
129
132
|
)));
|
|
130
133
|
}
|
|
131
|
-
|
|
134
|
+
let Some(size) = size else {
|
|
132
135
|
return Err(DataError::Validation(format!(
|
|
133
136
|
"axis `{axis_name}` regular-grid coordinates require a known axis size"
|
|
134
137
|
)));
|
|
135
|
-
}
|
|
138
|
+
};
|
|
136
139
|
if !start.is_finite() || !step.is_finite() {
|
|
137
140
|
return Err(DataError::Validation(format!(
|
|
138
141
|
"axis `{axis_name}` regular-grid start/step must be finite"
|
|
@@ -148,6 +151,7 @@ impl CoordinateSpec {
|
|
|
148
151
|
"axis `{axis_name}` regular-grid coordinates are inherently ordered; set ordered=true"
|
|
149
152
|
)));
|
|
150
153
|
}
|
|
154
|
+
validate_regular_grid(axis_name, size, *start, *step)?;
|
|
151
155
|
}
|
|
152
156
|
}
|
|
153
157
|
Ok(())
|
|
@@ -218,6 +222,82 @@ impl CoordinateSpec {
|
|
|
218
222
|
}
|
|
219
223
|
}
|
|
220
224
|
|
|
225
|
+
fn validate_regular_grid(axis_name: &str, size: usize, start: f64, step: f64) -> Result<()> {
|
|
226
|
+
if size < 2 {
|
|
227
|
+
return Ok(());
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
let last_index = size - 1;
|
|
231
|
+
let last_offset = last_index as f64 * step;
|
|
232
|
+
let last = start + last_offset;
|
|
233
|
+
if !last.is_finite() {
|
|
234
|
+
return Err(DataError::Validation(format!(
|
|
235
|
+
"axis `{axis_name}` regular-grid coordinate at index {last_index} is not finite"
|
|
236
|
+
)));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
let coincident = |left, right| {
|
|
240
|
+
DataError::Validation(format!(
|
|
241
|
+
"axis `{axis_name}` regular-grid coordinates at indices {left} and {right} coincide in binary64"
|
|
242
|
+
))
|
|
243
|
+
};
|
|
244
|
+
// The first adjacent integer indices that round to the same binary64.
|
|
245
|
+
const FIRST_REPEATED_INDEX: u64 = (1 << f64::MANTISSA_DIGITS) + 1;
|
|
246
|
+
if last_index as u64 >= FIRST_REPEATED_INDEX {
|
|
247
|
+
return Err(coincident(FIRST_REPEATED_INDEX - 1, FIRST_REPEATED_INDEX));
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Multiplication by a fixed step and addition of a finite start are
|
|
251
|
+
// monotone under rounding. Thus finite endpoints rule out interior
|
|
252
|
+
// overflow, and only equal adjacent values can violate strict ordering.
|
|
253
|
+
let offset_bound = last_offset.abs();
|
|
254
|
+
let coordinate_bound = start.abs().max(last.abs());
|
|
255
|
+
let offset_spacing = offset_bound.next_up() - offset_bound;
|
|
256
|
+
let coordinate_spacing = coordinate_bound.next_up() - coordinate_bound;
|
|
257
|
+
// Adding zero introduces no new rounding.
|
|
258
|
+
if start == 0.0 && step.abs() > offset_spacing {
|
|
259
|
+
return Ok(());
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// At these spacings, multiples are exactly representable throughout the
|
|
263
|
+
// respective bounded ranges. This also covers grids with one-ULP steps.
|
|
264
|
+
let offsets_exact = step % offset_spacing == 0.0;
|
|
265
|
+
if offsets_exact && start % coordinate_spacing == 0.0 && step % coordinate_spacing == 0.0 {
|
|
266
|
+
return Ok(());
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Each rounded operation can reduce an adjacent difference by at most
|
|
270
|
+
// one spacing (half a spacing at each end).
|
|
271
|
+
let offset_error = if offsets_exact { 0.0 } else { offset_spacing };
|
|
272
|
+
if step.abs() - offset_error > coordinate_spacing {
|
|
273
|
+
return Ok(());
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
let value_at = |index: usize| start + index as f64 * step;
|
|
277
|
+
if value_at(last_index - 1) == last {
|
|
278
|
+
return Err(coincident(last_index as u64 - 1, last_index as u64));
|
|
279
|
+
}
|
|
280
|
+
// A compact schema can declare far more coordinates than can be visited
|
|
281
|
+
// during admission. Bound exhaustive validation when the proof above is
|
|
282
|
+
// inconclusive, and never accept a grid from a partial check.
|
|
283
|
+
const MAX_ADJACENT_CHECKS: usize = 1 << 16;
|
|
284
|
+
let checks = (last_index - 1).min(MAX_ADJACENT_CHECKS);
|
|
285
|
+
let mut previous = start;
|
|
286
|
+
for index in 1..=checks {
|
|
287
|
+
let current = value_at(index);
|
|
288
|
+
if current == previous {
|
|
289
|
+
return Err(coincident(index as u64 - 1, index as u64));
|
|
290
|
+
}
|
|
291
|
+
previous = current;
|
|
292
|
+
}
|
|
293
|
+
if checks < last_index - 1 {
|
|
294
|
+
return Err(DataError::Validation(format!(
|
|
295
|
+
"axis `{axis_name}` regular-grid ordering could not be verified within {MAX_ADJACENT_CHECKS} adjacent checks. Use grid parameters with more separation in binary64 or provide the explicit coordinates"
|
|
296
|
+
)));
|
|
297
|
+
}
|
|
298
|
+
Ok(())
|
|
299
|
+
}
|
|
300
|
+
|
|
221
301
|
/// Strictly monotonic in EITHER direction (ascending or descending); rejects
|
|
222
302
|
/// equal/unorderable neighbours.
|
|
223
303
|
fn require_strictly_monotonic<T>(
|
|
@@ -1421,6 +1501,52 @@ mod tests {
|
|
|
1421
1501
|
assert!(unordered.validate("wl", Some(3), false).is_err());
|
|
1422
1502
|
}
|
|
1423
1503
|
|
|
1504
|
+
#[test]
|
|
1505
|
+
fn regular_grid_coordinates_must_be_representable_in_binary64() {
|
|
1506
|
+
let validate = |size, start, step| {
|
|
1507
|
+
coord(
|
|
1508
|
+
CoordinateDType::Numeric,
|
|
1509
|
+
true,
|
|
1510
|
+
CoordinateValues::RegularGrid { start, step },
|
|
1511
|
+
)
|
|
1512
|
+
.validate("wavelength", Some(size), false)
|
|
1513
|
+
};
|
|
1514
|
+
|
|
1515
|
+
assert!(validate(1, 1e308, 1e308).is_ok());
|
|
1516
|
+
assert!(validate(4, 1.0, f64::EPSILON).is_ok());
|
|
1517
|
+
assert!(validate(4, 1.0, -f64::EPSILON / 2.0).is_ok());
|
|
1518
|
+
assert!(validate(4, 0.0, f64::from_bits(1)).is_ok());
|
|
1519
|
+
|
|
1520
|
+
for (size, start, step, message) in [
|
|
1521
|
+
(4, 1e308, 1e308, "is not finite"),
|
|
1522
|
+
// A fused multiply-add would report a finite endpoint.
|
|
1523
|
+
(3, -1e308, 1e308, "is not finite"),
|
|
1524
|
+
(4, 1e16, 1.0, "coincide in binary64"),
|
|
1525
|
+
(4, -1e16, -1.0, "coincide in binary64"),
|
|
1526
|
+
// Both the first and last adjacent pairs differ.
|
|
1527
|
+
(5, 9_007_199_254_740_992.0, 1.5, "indices 2 and 3 coincide"),
|
|
1528
|
+
] {
|
|
1529
|
+
let error = validate(size, start, step).unwrap_err();
|
|
1530
|
+
assert_eq!(error.code(), "data_contract_validation");
|
|
1531
|
+
let diagnostic = error.to_string();
|
|
1532
|
+
assert!(diagnostic.contains("`wavelength`"), "{diagnostic}");
|
|
1533
|
+
assert!(diagnostic.contains(message), "{diagnostic}");
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
let inconclusive = validate(1 << 18, 1.0, -f64::EPSILON / 2.0).unwrap_err();
|
|
1537
|
+
assert!(inconclusive
|
|
1538
|
+
.to_string()
|
|
1539
|
+
.contains("ordering could not be verified"));
|
|
1540
|
+
|
|
1541
|
+
#[cfg(target_pointer_width = "64")]
|
|
1542
|
+
{
|
|
1543
|
+
let last_distinct_index = 1 << f64::MANTISSA_DIGITS;
|
|
1544
|
+
assert!(validate(last_distinct_index / 2, 0.0, 1.0).is_ok());
|
|
1545
|
+
assert!(validate(last_distinct_index / 2 + 1, 0.0, 0.1).is_ok());
|
|
1546
|
+
assert!(validate(last_distinct_index + 2, 0.0, 1.0).is_err());
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1424
1550
|
#[test]
|
|
1425
1551
|
fn explicit_coordinates_must_match_known_size_and_be_non_empty() {
|
|
1426
1552
|
let wrong_len = coord(CoordinateDType::Numeric, false, explicit(nums(&[1.0, 2.0])));
|
|
@@ -22,7 +22,7 @@ use crate::coordinator::CoordinatorRelationSet;
|
|
|
22
22
|
use crate::error::{DataError, Result};
|
|
23
23
|
use crate::ids::{ObservationId, RepresentationId, SampleId, SourceId};
|
|
24
24
|
|
|
25
|
-
/// Schema version of [`NdTensorManifest`]
|
|
25
|
+
/// Schema version of [`NdTensorManifest`].
|
|
26
26
|
pub const ND_TENSOR_MANIFEST_SCHEMA_VERSION: u32 = 1;
|
|
27
27
|
|
|
28
28
|
/// Maximum supported tensor rank. Bounds the shape-product arithmetic and keeps
|
|
@@ -35,11 +35,22 @@ pub const ND_TENSOR_MAX_RANK: usize = 16;
|
|
|
35
35
|
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
|
36
36
|
#[serde(rename_all = "snake_case")]
|
|
37
37
|
pub enum NdTensorDType {
|
|
38
|
+
/// IEEE-754 binary64, encoded in little-endian order.
|
|
38
39
|
F64,
|
|
40
|
+
/// IEEE-754 binary32, encoded in little-endian order.
|
|
39
41
|
F32,
|
|
42
|
+
/// Unsigned 8-bit integer.
|
|
40
43
|
U8,
|
|
44
|
+
/// Signed 32-bit integer, encoded in little-endian order.
|
|
41
45
|
I32,
|
|
46
|
+
/// Boolean encoded as a single byte with value zero or one.
|
|
42
47
|
Bool,
|
|
48
|
+
/// Unsigned 16-bit integer, encoded in little-endian order.
|
|
49
|
+
U16,
|
|
50
|
+
/// Signed 16-bit integer, encoded in little-endian order.
|
|
51
|
+
I16,
|
|
52
|
+
/// Unsigned 32-bit integer, encoded in little-endian order.
|
|
53
|
+
U32,
|
|
43
54
|
}
|
|
44
55
|
|
|
45
56
|
impl NdTensorDType {
|
|
@@ -51,6 +62,8 @@ impl NdTensorDType {
|
|
|
51
62
|
NdTensorDType::U8 => 1,
|
|
52
63
|
NdTensorDType::I32 => 4,
|
|
53
64
|
NdTensorDType::Bool => 1,
|
|
65
|
+
NdTensorDType::U16 | NdTensorDType::I16 => 2,
|
|
66
|
+
NdTensorDType::U32 => 4,
|
|
54
67
|
}
|
|
55
68
|
}
|
|
56
69
|
|
|
@@ -67,6 +80,9 @@ impl NdTensorDType {
|
|
|
67
80
|
NdTensorDType::U8 => 3,
|
|
68
81
|
NdTensorDType::I32 => 4,
|
|
69
82
|
NdTensorDType::Bool => 5,
|
|
83
|
+
NdTensorDType::U16 => 6,
|
|
84
|
+
NdTensorDType::I16 => 7,
|
|
85
|
+
NdTensorDType::U32 => 8,
|
|
70
86
|
}
|
|
71
87
|
}
|
|
72
88
|
}
|
|
@@ -75,9 +91,13 @@ impl NdTensorDType {
|
|
|
75
91
|
/// layer copies any strided borrowed input into this shape before calling in.
|
|
76
92
|
#[derive(Clone, Debug, PartialEq)]
|
|
77
93
|
pub struct NdTensorInput {
|
|
94
|
+
/// Provider-local tensor identifier. The store rejects duplicates.
|
|
78
95
|
pub tensor_id: String,
|
|
96
|
+
/// Representation served by this tensor. Binds to a matching data plan.
|
|
79
97
|
pub representation_id: RepresentationId,
|
|
98
|
+
/// Name of the declared batch container.
|
|
80
99
|
pub container: String,
|
|
100
|
+
/// Exact element dtype of the supplied bytes.
|
|
81
101
|
pub dtype: NdTensorDType,
|
|
82
102
|
/// Full shape; `shape[0]` is the sample/observation axis.
|
|
83
103
|
pub shape: Vec<usize>,
|
|
@@ -88,8 +108,8 @@ pub struct NdTensorInput {
|
|
|
88
108
|
pub sample_ids: Option<Vec<SampleId>>,
|
|
89
109
|
/// Contiguous row-major bytes; `len == product(shape) * dtype.element_size()`.
|
|
90
110
|
///
|
|
91
|
-
/// Multibyte elements
|
|
92
|
-
/// This layer
|
|
111
|
+
/// Multibyte elements must be encoded **little-endian**.
|
|
112
|
+
/// This layer stores the bytes verbatim and never reinterprets them as
|
|
93
113
|
/// native-endian values, so the byte order is part of the input contract,
|
|
94
114
|
/// not something the layer can fix up. The content fingerprint hashes these
|
|
95
115
|
/// bytes as-is; pinning little-endian here is what makes the fingerprint
|
|
@@ -114,34 +134,54 @@ pub struct NdTensor {
|
|
|
114
134
|
row_index_by_observation: BTreeMap<ObservationId, usize>,
|
|
115
135
|
/// Bytes per axis-0 row: `product(shape[1..]) * element_size`.
|
|
116
136
|
row_stride_bytes: usize,
|
|
137
|
+
fingerprint: String,
|
|
117
138
|
}
|
|
118
139
|
|
|
119
140
|
/// Provider-wide manifest of one stored tensor (no payload bytes).
|
|
120
141
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
|
121
142
|
pub struct NdTensorManifest {
|
|
143
|
+
/// Layout version of this manifest.
|
|
122
144
|
pub schema_version: u32,
|
|
145
|
+
/// Provider-local tensor identifier.
|
|
123
146
|
pub tensor_id: String,
|
|
147
|
+
/// Representation served by the tensor.
|
|
124
148
|
pub representation_id: RepresentationId,
|
|
149
|
+
/// Name of the declared batch container.
|
|
125
150
|
pub container: String,
|
|
151
|
+
/// Exact element dtype.
|
|
126
152
|
pub dtype: NdTensorDType,
|
|
153
|
+
/// Tensor extents in axis order, with observations on axis zero.
|
|
127
154
|
pub shape: Vec<usize>,
|
|
155
|
+
/// Observation IDs in stored axis-zero order.
|
|
128
156
|
pub observation_ids: Vec<ObservationId>,
|
|
157
|
+
/// Number of stored observations.
|
|
129
158
|
pub row_count: usize,
|
|
159
|
+
/// Number of bytes per element.
|
|
130
160
|
pub element_bytes: usize,
|
|
161
|
+
/// Total canonical payload length in bytes.
|
|
131
162
|
pub data_bytes: usize,
|
|
163
|
+
/// Fingerprint of the stored tensor before projection.
|
|
132
164
|
pub tensor_fingerprint: String,
|
|
133
165
|
}
|
|
134
166
|
|
|
135
167
|
/// Binding of one stored tensor to a materialized data handle.
|
|
136
168
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
|
137
169
|
pub struct NdTensorBinding {
|
|
170
|
+
/// Identifier of the bound tensor.
|
|
138
171
|
pub tensor_id: String,
|
|
172
|
+
/// Representation requested at materialization.
|
|
139
173
|
pub representation_id: RepresentationId,
|
|
174
|
+
/// Name of the declared batch container.
|
|
140
175
|
pub container: String,
|
|
176
|
+
/// Exact element dtype.
|
|
141
177
|
pub dtype: NdTensorDType,
|
|
178
|
+
/// Sources whose observations are covered by the tensor.
|
|
142
179
|
pub source_ids: Vec<SourceId>,
|
|
180
|
+
/// Extents of the stored tensor before any view selection.
|
|
143
181
|
pub shape: Vec<usize>,
|
|
182
|
+
/// Number of rows in the stored tensor.
|
|
144
183
|
pub row_count: usize,
|
|
184
|
+
/// Fingerprint of the stored tensor before projection.
|
|
145
185
|
pub tensor_fingerprint: String,
|
|
146
186
|
}
|
|
147
187
|
|
|
@@ -149,14 +189,23 @@ pub struct NdTensorBinding {
|
|
|
149
189
|
/// the number of selected rows; `data` is contiguous row-major bytes.
|
|
150
190
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
151
191
|
pub struct NdTensorBlock {
|
|
192
|
+
/// Identifier of the projected tensor.
|
|
152
193
|
pub tensor_id: String,
|
|
194
|
+
/// Representation served by the tensor.
|
|
153
195
|
pub representation_id: RepresentationId,
|
|
196
|
+
/// Name of the declared batch container.
|
|
154
197
|
pub container: String,
|
|
198
|
+
/// Exact element dtype.
|
|
155
199
|
pub dtype: NdTensorDType,
|
|
200
|
+
/// Tensor extents in axis order, with the selected observation count first.
|
|
156
201
|
pub shape: Vec<usize>,
|
|
202
|
+
/// Observation IDs in relation order.
|
|
157
203
|
pub observation_ids: Vec<ObservationId>,
|
|
204
|
+
/// Corresponding sample IDs from the relations.
|
|
158
205
|
pub sample_ids: Vec<SampleId>,
|
|
206
|
+
/// Canonical contiguous row-major bytes, with little-endian multibyte elements.
|
|
159
207
|
pub data: Vec<u8>,
|
|
208
|
+
/// Optional flags in relation order, one for each observation row.
|
|
160
209
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
161
210
|
pub row_presence: Option<Vec<bool>>,
|
|
162
211
|
}
|
|
@@ -283,7 +332,7 @@ impl NdTensor {
|
|
|
283
332
|
// All dims are > 0 (rejected above), so row_count > 0.
|
|
284
333
|
let row_stride_bytes = expected_bytes / row_count;
|
|
285
334
|
|
|
286
|
-
|
|
335
|
+
let mut tensor = Self {
|
|
287
336
|
tensor_id,
|
|
288
337
|
representation_id,
|
|
289
338
|
container,
|
|
@@ -294,7 +343,10 @@ impl NdTensor {
|
|
|
294
343
|
row_presence,
|
|
295
344
|
row_index_by_observation,
|
|
296
345
|
row_stride_bytes,
|
|
297
|
-
|
|
346
|
+
fingerprint: String::new(),
|
|
347
|
+
};
|
|
348
|
+
tensor.fingerprint = tensor.compute_fingerprint();
|
|
349
|
+
Ok(tensor)
|
|
298
350
|
}
|
|
299
351
|
|
|
300
352
|
fn contains_observation(&self, observation_id: &ObservationId) -> bool {
|
|
@@ -385,7 +437,7 @@ impl NdTensor {
|
|
|
385
437
|
/// per-dtype LE encoding. The dtype tag and the byte-length prefix mean
|
|
386
438
|
/// identical bytes under a different dtype or a different shape never
|
|
387
439
|
/// collide.
|
|
388
|
-
fn
|
|
440
|
+
fn compute_fingerprint(&self) -> String {
|
|
389
441
|
let mut hasher = StreamingHasher::new(b"dag-ml-data.nd-tensor.v2\0");
|
|
390
442
|
hasher.absorb_str(&self.tensor_id);
|
|
391
443
|
hasher.absorb_str(self.representation_id.as_str());
|
|
@@ -408,11 +460,11 @@ impl NdTensor {
|
|
|
408
460
|
}
|
|
409
461
|
}
|
|
410
462
|
}
|
|
411
|
-
|
|
463
|
+
hasher.finalize_hex()
|
|
412
464
|
}
|
|
413
465
|
|
|
414
|
-
fn manifest(&self) ->
|
|
415
|
-
|
|
466
|
+
fn manifest(&self) -> NdTensorManifest {
|
|
467
|
+
NdTensorManifest {
|
|
416
468
|
schema_version: ND_TENSOR_MANIFEST_SCHEMA_VERSION,
|
|
417
469
|
tensor_id: self.tensor_id.clone(),
|
|
418
470
|
representation_id: self.representation_id.clone(),
|
|
@@ -423,12 +475,12 @@ impl NdTensor {
|
|
|
423
475
|
row_count: self.shape[0],
|
|
424
476
|
element_bytes: self.dtype.element_size(),
|
|
425
477
|
data_bytes: self.data.len(),
|
|
426
|
-
tensor_fingerprint: self.fingerprint()
|
|
427
|
-
}
|
|
478
|
+
tensor_fingerprint: self.fingerprint.clone(),
|
|
479
|
+
}
|
|
428
480
|
}
|
|
429
481
|
|
|
430
|
-
fn binding_for_sources(&self, source_ids: Vec<SourceId>) ->
|
|
431
|
-
|
|
482
|
+
fn binding_for_sources(&self, source_ids: Vec<SourceId>) -> NdTensorBinding {
|
|
483
|
+
NdTensorBinding {
|
|
432
484
|
tensor_id: self.tensor_id.clone(),
|
|
433
485
|
representation_id: self.representation_id.clone(),
|
|
434
486
|
container: self.container.clone(),
|
|
@@ -436,8 +488,8 @@ impl NdTensor {
|
|
|
436
488
|
source_ids,
|
|
437
489
|
shape: self.shape.clone(),
|
|
438
490
|
row_count: self.shape[0],
|
|
439
|
-
tensor_fingerprint: self.fingerprint()
|
|
440
|
-
}
|
|
491
|
+
tensor_fingerprint: self.fingerprint.clone(),
|
|
492
|
+
}
|
|
441
493
|
}
|
|
442
494
|
}
|
|
443
495
|
|
|
@@ -457,13 +509,14 @@ impl NdTensorStore {
|
|
|
457
509
|
Ok(Self { tensors })
|
|
458
510
|
}
|
|
459
511
|
|
|
512
|
+
/// Reports whether the store has any tensors.
|
|
460
513
|
pub fn is_empty(&self) -> bool {
|
|
461
514
|
self.tensors.is_empty()
|
|
462
515
|
}
|
|
463
516
|
|
|
464
517
|
/// Provider-wide manifests for every stored tensor.
|
|
465
518
|
pub fn manifests(&self) -> Result<Vec<NdTensorManifest>> {
|
|
466
|
-
self.tensors.values().map(NdTensor::manifest).collect()
|
|
519
|
+
Ok(self.tensors.values().map(NdTensor::manifest).collect())
|
|
467
520
|
}
|
|
468
521
|
|
|
469
522
|
/// Bindings for tensors whose representation matches `representation_id` and
|
|
@@ -491,7 +544,7 @@ impl NdTensorStore {
|
|
|
491
544
|
.iter()
|
|
492
545
|
.all(|relation| tensor.contains_observation(&relation.observation_id))
|
|
493
546
|
{
|
|
494
|
-
bindings.push(tensor.binding_for_sources(Vec::new())
|
|
547
|
+
bindings.push(tensor.binding_for_sources(Vec::new()));
|
|
495
548
|
}
|
|
496
549
|
continue;
|
|
497
550
|
}
|
|
@@ -507,7 +560,7 @@ impl NdTensorStore {
|
|
|
507
560
|
}
|
|
508
561
|
}
|
|
509
562
|
if !covered_sources.is_empty() {
|
|
510
|
-
bindings.push(tensor.binding_for_sources(covered_sources)
|
|
563
|
+
bindings.push(tensor.binding_for_sources(covered_sources));
|
|
511
564
|
}
|
|
512
565
|
}
|
|
513
566
|
Ok(bindings)
|
|
@@ -528,6 +581,7 @@ impl NdTensorStore {
|
|
|
528
581
|
}
|
|
529
582
|
|
|
530
583
|
impl NdTensorArena {
|
|
584
|
+
/// Creates an arena with an immutable store and no live handle bindings.
|
|
531
585
|
pub fn new(store: NdTensorStore) -> Self {
|
|
532
586
|
Self {
|
|
533
587
|
store,
|
|
@@ -557,14 +611,17 @@ impl NdTensorArena {
|
|
|
557
611
|
Ok(bindings)
|
|
558
612
|
}
|
|
559
613
|
|
|
614
|
+
/// Removes the data handle's bindings. Returns whether a binding record existed.
|
|
560
615
|
pub fn release_data_handle(&mut self, data_handle: u64) -> bool {
|
|
561
616
|
self.data_bindings.remove(&data_handle).is_some()
|
|
562
617
|
}
|
|
563
618
|
|
|
619
|
+
/// Lists the provider-wide tensor manifests independently of handle lifetime.
|
|
564
620
|
pub fn manifests(&self) -> Result<Vec<NdTensorManifest>> {
|
|
565
621
|
self.store.manifests()
|
|
566
622
|
}
|
|
567
623
|
|
|
624
|
+
/// Lists the tensor bindings of a data handle. A missing binding record is an error.
|
|
568
625
|
pub fn bindings_for_data_handle(&self, data_handle: u64) -> Result<Vec<NdTensorBinding>> {
|
|
569
626
|
let bindings = self.data_bindings.get(&data_handle).ok_or_else(|| {
|
|
570
627
|
DataError::Validation(format!(
|
|
@@ -752,6 +809,10 @@ mod tests {
|
|
|
752
809
|
.unwrap();
|
|
753
810
|
assert_eq!(bindings.len(), 1);
|
|
754
811
|
assert_eq!(bindings[0].source_ids, vec![SourceId::new("cam").unwrap()]);
|
|
812
|
+
assert_eq!(
|
|
813
|
+
bindings[0].tensor_fingerprint,
|
|
814
|
+
arena.manifests().unwrap()[0].tensor_fingerprint
|
|
815
|
+
);
|
|
755
816
|
|
|
756
817
|
// Bound source projects.
|
|
757
818
|
let block = arena
|
|
@@ -839,11 +900,14 @@ mod tests {
|
|
|
839
900
|
assert_eq!(manifests[0].shape, vec![3, 2, 2]);
|
|
840
901
|
assert_eq!(manifests[0].data_bytes, 12);
|
|
841
902
|
assert_eq!(manifests[0].element_bytes, 1);
|
|
842
|
-
assert_eq!(
|
|
903
|
+
assert_eq!(
|
|
904
|
+
manifests[0].tensor_fingerprint,
|
|
905
|
+
"a9e737d57e93b2cef613655dbe278cead3e52f26ad6cad2617c4628f6b2cdaaf"
|
|
906
|
+
);
|
|
843
907
|
}
|
|
844
908
|
|
|
845
909
|
fn fp(input: NdTensorInput) -> String {
|
|
846
|
-
NdTensor::from_input(input).unwrap().fingerprint
|
|
910
|
+
NdTensor::from_input(input).unwrap().fingerprint
|
|
847
911
|
}
|
|
848
912
|
|
|
849
913
|
#[test]
|
|
@@ -858,9 +922,8 @@ mod tests {
|
|
|
858
922
|
#[test]
|
|
859
923
|
fn tensor_fingerprint_is_deterministic_across_calls_and_clone() {
|
|
860
924
|
let tensor = NdTensor::from_input(rgb_input()).unwrap();
|
|
861
|
-
|
|
862
|
-
assert_eq!(
|
|
863
|
-
assert_eq!(once, tensor.clone().fingerprint().unwrap());
|
|
925
|
+
assert_eq!(tensor.fingerprint, tensor.compute_fingerprint());
|
|
926
|
+
assert_eq!(tensor.fingerprint, tensor.clone().fingerprint);
|
|
864
927
|
}
|
|
865
928
|
|
|
866
929
|
#[test]
|
|
@@ -934,7 +997,16 @@ mod tests {
|
|
|
934
997
|
let mut as_u8 = as_i32.clone();
|
|
935
998
|
as_u8.dtype = NdTensorDType::U8;
|
|
936
999
|
as_u8.shape = vec![1, 4];
|
|
937
|
-
|
|
1000
|
+
let mut as_u32 = as_i32.clone();
|
|
1001
|
+
as_u32.dtype = NdTensorDType::U32;
|
|
1002
|
+
let mut as_u16 = as_i32.clone();
|
|
1003
|
+
as_u16.dtype = NdTensorDType::U16;
|
|
1004
|
+
as_u16.shape = vec![1, 2];
|
|
1005
|
+
let mut as_i16 = as_u16.clone();
|
|
1006
|
+
as_i16.dtype = NdTensorDType::I16;
|
|
1007
|
+
assert_ne!(fp(as_i32.clone()), fp(as_u8));
|
|
1008
|
+
assert_ne!(fp(as_i32), fp(as_u32));
|
|
1009
|
+
assert_ne!(fp(as_u16), fp(as_i16));
|
|
938
1010
|
}
|
|
939
1011
|
|
|
940
1012
|
#[test]
|
|
@@ -1006,12 +1078,11 @@ mod tests {
|
|
|
1006
1078
|
data,
|
|
1007
1079
|
row_presence: None,
|
|
1008
1080
|
};
|
|
1009
|
-
let tensor = NdTensor::from_input(input).unwrap();
|
|
1010
1081
|
let start = std::time::Instant::now();
|
|
1011
|
-
let fingerprint =
|
|
1082
|
+
let fingerprint = NdTensor::from_input(input).unwrap().fingerprint;
|
|
1012
1083
|
let elapsed = start.elapsed();
|
|
1013
1084
|
println!(
|
|
1014
|
-
"nd tensor fingerprint({rows}x{cols} f32) = {:.3} ms (fp={fingerprint})",
|
|
1085
|
+
"nd tensor validation and fingerprint({rows}x{cols} f32) = {:.3} ms (fp={fingerprint})",
|
|
1015
1086
|
elapsed.as_secs_f64() * 1e3
|
|
1016
1087
|
);
|
|
1017
1088
|
assert_eq!(fingerprint.len(), 64);
|