emelex 1.1.1

Apple Silicon local inference toolkit powered by MLX
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
//! Model capability facts and evidence.

use std::{
	collections::{BTreeMap, BTreeSet},
	fmt,
	str::FromStr,
};

use serde::{Deserialize, Serialize};

/// Input or output modality.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Modality {
	/// Text.
	Text,
	/// Images.
	Image,
	/// Audio.
	Audio,
}

/// Model task family.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Task {
	/// Autoregressive text generation.
	TextGeneration,
	/// Conversational generation through a chat template.
	Chat,
	/// Tool/function invocation.
	ToolUse,
	/// Structured JSON output by instruction.
	StructuredOutput,
	/// Explicit reasoning/thinking spans.
	Reasoning,
	/// Structured translation through a translation-shaped chat template
	/// (TranslateGemma-style per-message language pairs).
	Translation,
}

/// Confidence state for a capability or compatibility fact.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum VerificationStatus {
	/// Derived from static repository metadata.
	Estimated,
	/// Confirmed by a successful local runtime probe.
	Verified,
}

/// MTP capability progression.
#[derive(
	Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum MtpSupport {
	/// No MTP claim or layout.
	#[default]
	Absent,
	/// Repository metadata advertises MTP.
	Advertised,
	/// Runtime load and parity gate verified this exact layout.
	RuntimeVerified,
}

/// Origin of one discovered fact.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum EvidenceSource {
	/// Hugging Face search metadata or tags.
	HubMetadata,
	/// Repository file tree.
	RepositoryTree,
	/// `config.json`.
	Config,
	/// Tokenizer configuration or chat template.
	Tokenizer,
	/// Safetensors index or headers.
	Weights,
	/// Successful local runtime probe.
	Runtime,
}

/// Evidence supporting a trait.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct TraitEvidence {
	/// Namespaced trait key.
	pub trait_key: String,
	/// Evidence origin.
	pub source: EvidenceSource,
	/// Human-readable observation.
	pub detail: String,
}

impl TraitEvidence {
	/// Construct one capability-evidence record.
	pub fn new(
		trait_key: impl Into<String>,
		source: EvidenceSource,
		detail: impl Into<String>,
	) -> Self {
		Self {
			trait_key: trait_key.into(),
			source,
			detail: detail.into(),
		}
	}
}

/// Strength of one capability claim.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum TraitConfidence {
	/// Repository metadata makes an explicit claim.
	Advertised,
	/// Emelex inferred the capability from static artifacts.
	Inferred,
	/// A local runtime probe confirmed the capability.
	RuntimeVerified,
}

/// Optional model sizing facts.
///
/// `None` means the relevant artifact was not inspected. It never means zero.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct ModelSizing {
	/// Exact selected runnable weight bytes.
	pub weights_bytes: Option<u64>,
	/// Estimated peak residency for [`Self::evaluated_context_tokens`].
	pub estimated_residency_bytes: Option<u64>,
	/// Context used for the residency estimate.
	pub evaluated_context_tokens: Option<usize>,
	/// Architecture-declared maximum context.
	pub max_context_tokens: Option<usize>,
}

/// Defaults advertised by a checkpoint's `generation_config.json`.
///
/// These are evidence, not an override of explicit Emelex configuration or
/// per-load policy.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[non_exhaustive]
pub struct ModelGenerationDefaults {
	/// Sampling mode advertised by the model.
	pub do_sample: Option<bool>,
	/// Sampling temperature.
	pub temperature: Option<f32>,
	/// Nucleus-sampling threshold.
	pub top_p: Option<f32>,
	/// Top-k cutoff.
	pub top_k: Option<u32>,
	/// Suggested generation ceiling.
	pub max_new_tokens: Option<usize>,
}

/// Static and verified model capabilities.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct ModelTraits {
	/// Accepted input modalities.
	pub input: BTreeSet<Modality>,
	/// Produced output modalities.
	pub output: BTreeSet<Modality>,
	/// Supported task families.
	pub tasks: BTreeSet<Task>,
	/// MLX-optimized repository/layout.
	pub mlx: bool,
	/// MTP progression.
	pub mtp: MtpSupport,
	/// Sizing facts whose absence remains distinguishable from zero.
	#[serde(default)]
	pub sizing: Option<ModelSizing>,
	/// Checkpoint-advertised generation defaults.
	#[serde(default)]
	pub generation_defaults: ModelGenerationDefaults,
	/// Namespaced forward-compatible extension facts.
	pub extras: BTreeMap<String, serde_json::Value>,
	/// Confidence for each namespaced trait key.
	pub confidence: BTreeMap<String, TraitConfidence>,
	/// Evidence trail for derived facts.
	pub evidence: Vec<TraitEvidence>,
}

impl Default for ModelTraits {
	fn default() -> Self {
		Self {
			input: BTreeSet::new(),
			output: BTreeSet::new(),
			tasks: BTreeSet::new(),
			mlx: false,
			mtp: MtpSupport::Absent,
			sizing: None,
			generation_defaults: ModelGenerationDefaults::default(),
			extras: BTreeMap::new(),
			confidence: BTreeMap::new(),
			evidence: Vec::new(),
		}
	}
}

impl ModelTraits {
	/// Test one validated capability filter.
	pub fn satisfies(&self, filter: &TraitFilter) -> bool {
		self.satisfies_predicate(filter.predicate())
	}

	fn satisfies_predicate(&self, predicate: &TraitPredicate) -> bool {
		match predicate {
			TraitPredicate::Capability(key) => self.satisfies_key(key),
			TraitPredicate::MinimumConfidence { key, confidence } => {
				self.satisfies_key(key)
					&& self
						.confidence
						.get(key)
						.is_some_and(|actual| actual >= confidence)
			}
			TraitPredicate::AtMost { metric, value } => {
				self.metric(*metric).is_some_and(|actual| actual <= *value)
			}
			TraitPredicate::AtLeast { metric, value } => {
				self.metric(*metric).is_some_and(|actual| actual >= *value)
			}
			TraitPredicate::MinimumMtp(stage) => self.mtp >= *stage,
		}
	}

	fn satisfies_key(&self, key: &str) -> bool {
		match key {
			"input:text" => self.input.contains(&Modality::Text),
			"input:image" => self.input.contains(&Modality::Image),
			"input:audio" => self.input.contains(&Modality::Audio),
			"output:text" => self.output.contains(&Modality::Text),
			"output:image" => self.output.contains(&Modality::Image),
			"output:audio" => self.output.contains(&Modality::Audio),
			"task:text_generation" => self.tasks.contains(&Task::TextGeneration),
			"task:chat" => self.tasks.contains(&Task::Chat),
			"task:translation" => self.tasks.contains(&Task::Translation),
			"interaction:tools" => self.tasks.contains(&Task::ToolUse),
			"interaction:system_prompt" => self
				.extras
				.get("interaction:system_prompt")
				.and_then(serde_json::Value::as_bool)
				.unwrap_or(false),
			"interaction:reasoning" => self.tasks.contains(&Task::Reasoning),
			"interaction:reasoning_history" | "interaction:thinking_toggle" => self
				.extras
				.get(key)
				.and_then(serde_json::Value::as_bool)
				.unwrap_or(false),
			"interaction:structured_output" => self.tasks.contains(&Task::StructuredOutput),
			"acceleration:mlx" => self.mlx,
			"acceleration:mtp" => self.mtp == MtpSupport::RuntimeVerified,
			"acceleration:mtp_advertised" => matches!(
				self.mtp,
				MtpSupport::Advertised | MtpSupport::RuntimeVerified
			),
			other => self
				.extras
				.get(other)
				.and_then(serde_json::Value::as_bool)
				.unwrap_or(false),
		}
	}

	fn metric(&self, metric: TraitMetric) -> Option<u64> {
		let sizing = self.sizing.as_ref()?;
		match metric {
			TraitMetric::WeightsBytes => sizing.weights_bytes,
			TraitMetric::EstimatedResidencyBytes => sizing.estimated_residency_bytes,
			TraitMetric::EvaluatedContextTokens => sizing
				.evaluated_context_tokens
				.and_then(|value| u64::try_from(value).ok()),
			TraitMetric::MaximumContextTokens => sizing
				.max_context_tokens
				.and_then(|value| u64::try_from(value).ok()),
		}
	}

	/// Evidence confidence for a capability key.
	pub fn confidence(&self, filter: &TraitFilter) -> Option<TraitConfidence> {
		match filter.predicate() {
			TraitPredicate::Capability(key) | TraitPredicate::MinimumConfidence { key, .. } => {
				self.confidence.get(key).copied()
			}
			TraitPredicate::AtMost { .. }
			| TraitPredicate::AtLeast { .. }
			| TraitPredicate::MinimumMtp(_) => None,
		}
	}
}

/// Numeric capability metric used by range predicates.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum TraitMetric {
	/// Exact selected runnable weights.
	WeightsBytes,
	/// Estimated peak residency.
	EstimatedResidencyBytes,
	/// Context used by the estimate.
	EvaluatedContextTokens,
	/// Architecture-declared maximum context.
	MaximumContextTokens,
}

/// Typed capability predicate.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum TraitPredicate {
	/// Boolean capability or extension fact.
	Capability(String),
	/// Capability with a minimum evidence confidence.
	MinimumConfidence {
		/// Namespaced capability key.
		key: String,
		/// Required confidence floor.
		confidence: TraitConfidence,
	},
	/// Numeric upper bound.
	AtMost {
		/// Metric being compared.
		metric: TraitMetric,
		/// Inclusive maximum.
		value: u64,
	},
	/// Numeric lower bound.
	AtLeast {
		/// Metric being compared.
		metric: TraitMetric,
		/// Inclusive minimum.
		value: u64,
	},
	/// Minimum MTP verification stage.
	MinimumMtp(MtpSupport),
}

/// Validated namespaced capability filter.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TraitFilter {
	source: String,
	predicate: TraitPredicate,
}

impl TraitFilter {
	/// Parse a known filter or an explicit `extension:<name>` filter.
	///
	/// # Errors
	///
	/// Returns [`TraitFilterError`] for typos and unsafe extension keys.
	pub fn parse(value: impl Into<String>) -> Result<Self, TraitFilterError> {
		let value = value.into();
		let extension = value.strip_prefix("extension:");
		let predicate = if known_capability(&value)
			|| extension.is_some_and(|name| {
				!name.is_empty()
					&& name.len() <= 128
					&& name.bytes().all(|byte| {
						byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-' | b'.' | b':')
					})
			}) {
			TraitPredicate::Capability(value.clone())
		} else if let Some(rest) = value.strip_prefix("confidence:") {
			let (confidence, key) = rest
				.split_once(':')
				.ok_or_else(|| TraitFilterError(value.clone()))?;
			let confidence =
				parse_confidence(confidence).ok_or_else(|| TraitFilterError(value.clone()))?;
			if !known_capability(key) && !valid_extension_key(key) {
				return Err(TraitFilterError(value));
			}
			TraitPredicate::MinimumConfidence {
				key: key.to_string(),
				confidence,
			}
		} else if let Some(stage) = value.strip_prefix("mtp_stage>=") {
			TraitPredicate::MinimumMtp(
				parse_mtp_stage(stage).ok_or_else(|| TraitFilterError(value.clone()))?,
			)
		} else if let Some((metric, amount)) = parse_metric_predicate(&value, "<=") {
			TraitPredicate::AtMost {
				metric,
				value: amount?,
			}
		} else if let Some((metric, amount)) = parse_metric_predicate(&value, ">=") {
			TraitPredicate::AtLeast {
				metric,
				value: amount?,
			}
		} else {
			return Err(TraitFilterError(value));
		};
		Ok(Self {
			source: value,
			predicate,
		})
	}

	/// Canonical namespaced key.
	pub fn as_str(&self) -> &str {
		&self.source
	}

	/// Parsed typed predicate.
	pub const fn predicate(&self) -> &TraitPredicate {
		&self.predicate
	}
}

impl fmt::Display for TraitFilter {
	fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
		formatter.write_str(self.as_str())
	}
}

const fn known_capability(value: &str) -> bool {
	matches!(
		value.as_bytes(),
		b"input:text"
			| b"input:image"
			| b"input:audio"
			| b"output:text"
			| b"output:image"
			| b"output:audio"
			| b"task:text_generation"
			| b"task:chat"
			| b"task:translation"
			| b"interaction:tools"
			| b"interaction:system_prompt"
			| b"interaction:reasoning"
			| b"interaction:reasoning_history"
			| b"interaction:thinking_toggle"
			| b"interaction:structured_output"
			| b"acceleration:mlx"
			| b"acceleration:mtp"
			| b"acceleration:mtp_advertised"
	)
}

fn valid_extension_key(value: &str) -> bool {
	value.strip_prefix("extension:").is_some_and(|name| {
		!name.is_empty()
			&& name.len() <= 128
			&& name.bytes().all(|byte| {
				byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-' | b'.' | b':')
			})
	})
}

fn parse_confidence(value: &str) -> Option<TraitConfidence> {
	match value {
		"advertised" => Some(TraitConfidence::Advertised),
		"inferred" => Some(TraitConfidence::Inferred),
		"runtime_verified" => Some(TraitConfidence::RuntimeVerified),
		_ => None,
	}
}

fn parse_mtp_stage(value: &str) -> Option<MtpSupport> {
	match value {
		"absent" => Some(MtpSupport::Absent),
		"advertised" => Some(MtpSupport::Advertised),
		"runtime_verified" => Some(MtpSupport::RuntimeVerified),
		_ => None,
	}
}

fn parse_metric_predicate(
	value: &str,
	operator: &str,
) -> Option<(TraitMetric, Result<u64, TraitFilterError>)> {
	let (name, amount) = value.split_once(operator)?;
	let metric = match name {
		"weights_bytes" => TraitMetric::WeightsBytes,
		"residency_bytes" => TraitMetric::EstimatedResidencyBytes,
		"context_tokens" => TraitMetric::EvaluatedContextTokens,
		"max_context_tokens" => TraitMetric::MaximumContextTokens,
		_ => return None,
	};
	Some((
		metric,
		amount
			.parse()
			.map_err(|_| TraitFilterError(value.to_string())),
	))
}

impl FromStr for TraitFilter {
	type Err = TraitFilterError;

	fn from_str(value: &str) -> Result<Self, Self::Err> {
		Self::parse(value)
	}
}

impl Serialize for TraitFilter {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: serde::Serializer,
	{
		serializer.serialize_str(self.as_str())
	}
}

impl<'de> Deserialize<'de> for TraitFilter {
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
	where
		D: serde::Deserializer<'de>,
	{
		let value = String::deserialize(deserializer)?;
		Self::parse(value).map_err(serde::de::Error::custom)
	}
}

/// Invalid capability filter.
#[derive(Debug, thiserror::Error, Clone, PartialEq, Eq)]
#[error("unknown model trait filter {0:?}")]
pub struct TraitFilterError(String);

#[cfg(test)]
#[path = "traits_tests.rs"]
mod tests;