use std::fmt::{Display, Formatter};
const STRUCTURED_PREFIX: &str = "mx1:";
const METADATA_FLAG: &str = "m";
const FULL_WEIGHT_FLAG: &str = "-";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EntryKey {
pub model_name: String,
pub revision: Option<String>,
pub metadata_only: bool,
}
impl EntryKey {
pub fn new(
model_name: impl Into<String>,
revision: Option<String>,
metadata_only: bool,
) -> Self {
Self {
model_name: model_name.into(),
revision,
metadata_only,
}
}
fn needs_structured_form(&self) -> bool {
self.revision.is_some()
|| self.metadata_only
|| self.model_name.starts_with(STRUCTURED_PREFIX)
}
pub fn parse(key: &str) -> Self {
let Some(fields) = key.strip_prefix(STRUCTURED_PREFIX) else {
return Self::new(key, None, false);
};
let mut parts = fields.splitn(3, ':');
match (parts.next(), parts.next(), parts.next()) {
(Some(revision), Some(flags), Some(model_name)) => Self::new(
model_name,
(!revision.is_empty()).then(|| revision.to_string()),
flags == METADATA_FLAG,
),
_ => Self::new(key, None, false),
}
}
pub fn belongs_to(&self, model_name: &str) -> bool {
self.model_name == model_name
}
}
impl Display for EntryKey {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if !self.needs_structured_form() {
return f.write_str(&self.model_name);
}
let flags = if self.metadata_only {
METADATA_FLAG
} else {
FULL_WEIGHT_FLAG
};
write!(
f,
"{STRUCTURED_PREFIX}{}:{flags}:{}",
self.revision.as_deref().unwrap_or(""),
self.model_name
)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn roundtrip(key: EntryKey) {
assert_eq!(EntryKey::parse(&key.to_string()), key);
}
#[test]
fn unpinned_full_weight_entry_encodes_to_the_bare_model_name() {
let key = EntryKey::new("google-t5/t5-small", None, false);
assert_eq!(key.to_string(), "google-t5/t5-small");
roundtrip(key);
}
#[test]
fn every_component_survives_a_roundtrip() {
roundtrip(EntryKey::new(
"org/model",
Some("abc123".to_string()),
false,
));
roundtrip(EntryKey::new("org/model", None, true));
roundtrip(EntryKey::new("org/model", Some("abc123".to_string()), true));
roundtrip(EntryKey::new("gs://bucket/org/model/rev-1", None, false));
}
#[test]
fn a_model_name_cannot_impersonate_the_encoding() {
for name in [
"gs://bucket/models/foo:m:bar",
"gs://bucket/models/foo#metadata",
"gs://bucket/models/foo@rev:abc123",
"mx1:abc123:m:gs://bucket/models/foo",
"mx1:",
":::",
] {
roundtrip(EntryKey::new(name, None, false));
roundtrip(EntryKey::new(name, None, true));
roundtrip(EntryKey::new(name, Some("abc123".to_string()), false));
}
}
#[test]
fn a_name_that_looks_like_a_key_stays_distinct_from_the_key_it_mimics() {
let mimic = EntryKey::new("mx1:abc123:m:org/model", None, false);
let real = EntryKey::new("org/model", Some("abc123".to_string()), true);
assert_ne!(mimic.to_string(), real.to_string());
assert_eq!(EntryKey::parse(&mimic.to_string()), mimic);
assert_eq!(EntryKey::parse(&real.to_string()), real);
}
#[test]
fn a_model_name_with_a_colon_keeps_its_revision_and_weight_mode() {
let key = EntryKey::new("ngc/org/model:1.0", Some("abc123".to_string()), true);
let parsed = EntryKey::parse(&key.to_string());
assert_eq!(parsed.model_name, "ngc/org/model:1.0");
assert_eq!(parsed.revision.as_deref(), Some("abc123"));
assert!(parsed.metadata_only);
}
#[test]
fn two_revisions_of_one_model_get_distinct_keys() {
let first = EntryKey::new("org/model", Some("abc123".to_string()), false).to_string();
let second = EntryKey::new("org/model", Some("def456".to_string()), false).to_string();
assert_ne!(first, second);
}
#[test]
fn metadata_only_and_full_weight_entries_do_not_collide() {
let revision = Some("abc123".to_string());
assert_ne!(
EntryKey::new("org/model", revision.clone(), true).to_string(),
EntryKey::new("org/model", revision, false).to_string()
);
}
#[test]
fn a_legacy_key_parses_as_unpinned_and_full_weight() {
let parsed = EntryKey::parse("meta-llama/Llama-3.1-70B");
assert_eq!(parsed.model_name, "meta-llama/Llama-3.1-70B");
assert!(parsed.revision.is_none());
assert!(!parsed.metadata_only);
}
#[test]
fn belongs_to_ignores_revision_and_weight_mode() {
let key = EntryKey::new("org/model", Some("abc123".to_string()), true);
assert!(key.belongs_to("org/model"));
assert!(!key.belongs_to("org/other"));
}
}