-
Notifications
You must be signed in to change notification settings - Fork 359
The Implementation for Bring Your Own Local Model (BYOM) API #928
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
base: main
Are you sure you want to change the base?
Changes from all commits
29a1bad
15e1dde
f1cb59a
c1f1b8b
0c6a729
a2dd9b2
a1d7854
398f702
75314ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,7 +60,7 @@ | |
| * Incremented with each release. | ||
| * Used to request the API function table via FoundryLocalGetApi. | ||
| * ----------------------------------------------------------------------- */ | ||
| #define FOUNDRY_LOCAL_API_VERSION 1 | ||
| #define FOUNDRY_LOCAL_API_VERSION 2 | ||
|
|
||
| /* ----------------------------------------------------------------------- | ||
| * Platform export macros (C version) | ||
|
|
@@ -202,6 +202,12 @@ typedef enum flDeviceType { | |
| FOUNDRY_LOCAL_DEVICE_NPU = 3 | ||
| } flDeviceType; | ||
|
|
||
| typedef enum flCatalogType { | ||
| FOUNDRY_LOCAL_CATALOG_PUBLIC = 0, | ||
| FOUNDRY_LOCAL_CATALOG_LOCAL = 1, | ||
| FOUNDRY_LOCAL_CATALOG_PRIVATE = 2, | ||
| } flCatalogType; | ||
|
|
||
| /// Tensor element data types. Values match ONNX TensorProto.DataType. | ||
| typedef enum flTensorDataType { | ||
| FOUNDRY_LOCAL_TENSOR_UNDEFINED = 0, | ||
|
|
@@ -256,6 +262,16 @@ typedef enum flTensorDataType { | |
| #define FOUNDRY_LOCAL_MODEL_PROP_TOOL_CALL_END_STR "tool_call_end" ///< optional tool call end marker token | ||
| #define FOUNDRY_LOCAL_MODEL_PROP_REASONING_START_STR "reasoning_start" ///< optional reasoning/think start marker token | ||
| #define FOUNDRY_LOCAL_MODEL_PROP_REASONING_END_STR "reasoning_end" ///< optional reasoning/think end marker token | ||
| #define FOUNDRY_LOCAL_MODEL_PROP_DEVICE_TYPE_STR "device_type" ///< CPU, GPU, or NPU | ||
| #define FOUNDRY_LOCAL_MODEL_PROP_EP_STR "execution_provider" ///< optional execution provider | ||
| #define FOUNDRY_LOCAL_MODEL_PROP_ENTITY_TYPE_STR "entity_type" ///< fixed to "Model" for BYOM | ||
| #define FOUNDRY_LOCAL_MODEL_PROP_AUTHOR_STR "author" ///< optional | ||
| #define FOUNDRY_LOCAL_MODEL_PROP_QUANTIZATION_STR "quantization" ///< optional | ||
| #define FOUNDRY_LOCAL_MODEL_PROP_CREATION_TIME_STR "creation_time" ///< ISO-8601 UTC timestamp | ||
|
|
||
| /* flModelInfo registration properties */ | ||
| #define FOUNDRY_LOCAL_REG_MODEL_PATH "model_path" | ||
| #define FOUNDRY_LOCAL_REG_ALIAS "alias" | ||
|
Comment on lines
+273
to
+274
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these two values be provided via metadata properties or directly to the Instead of 'alias' should the user provide 'id' in format |
||
|
|
||
| /* flModelInfo Int properties. Comments provide details on the type and expected values. */ | ||
| #define FOUNDRY_LOCAL_MODEL_PROP_SUPPORTS_TOOL_CALLING_INT "supports_tool_calling" ///< optional bool (not set or -1=unknown, 0=false, 1=true) | ||
|
|
@@ -265,6 +281,9 @@ typedef enum flTensorDataType { | |
| #define FOUNDRY_LOCAL_MODEL_PROP_CREATED_AT_UNIX_INT "created_at_unix" ///< Unix timestamp. default=0 | ||
| #define FOUNDRY_LOCAL_MODEL_PROP_IS_TEST_MODEL_INT "is_test_model" ///< bool (0=false, 1=true) | ||
| #define FOUNDRY_LOCAL_MODEL_PROP_CONTEXT_LENGTH_INT "context_length" ///< optional int64_t | ||
| #define FOUNDRY_LOCAL_MODEL_PROP_VERSION_INT "version" ///< optional non-negative integer | ||
| #define FOUNDRY_LOCAL_MODEL_PROP_FILESIZE_BYTES_INT "file_size_bytes" ///< optional int64_t | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why bytes if the 'real' catalog property is fileSizeMb? |
||
| #define FOUNDRY_LOCAL_MODEL_PROP_SUPPORTS_HYBRID_REASONING_INT "supports_hybrid_reasoning" ///< optional bool | ||
|
|
||
| #define FOUNDRY_LOCAL_MODEL_PROP_INPUT_MODALITIES_STR "input_modalities" ///< optional, comma-separated | ||
| #define FOUNDRY_LOCAL_MODEL_PROP_OUTPUT_MODALITIES_STR "output_modalities" ///< optional, comma-separated | ||
|
|
@@ -706,6 +725,12 @@ typedef struct flApi { | |
| bool FL_API_T(Manager_IsShutdownRequested, _In_ const flManager* manager); | ||
|
|
||
| // End V1 | ||
| FL_API_STATUS(Manager_GetCatalogByType, _In_ const flManager* manager, flCatalogType catalog_type, | ||
| _Outptr_ flCatalog** out_catalog); | ||
| FL_API_STATUS(Manager_GetCatalogByName, _In_ const flManager* manager, _In_ const char* catalog_name, | ||
| _Outptr_ flCatalog** out_catalog); | ||
|
Comment on lines
+728
to
+731
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this something the user needs to know about? Internally we could have a local catalog that we automatically use for RegisterModel, but that seems like it could stay an implementation detail and there's just a single user facing flCatalog. Ideally the internals of FL aggregate across the various catalogs available (public/private/local) and the user doesn't need to care about that detail. V1 had a catalog aggregator class that I didn't port across as it hadn't been used yet. We could potentially add that concept back in. |
||
|
|
||
| // End V2 | ||
| /* Append new function pointers at the end for future versions and add marker for the end of each version */ | ||
| } flApi; | ||
|
|
||
|
|
@@ -969,6 +994,15 @@ struct flCatalogApi { | |
| _In_opt_ const char* model_name, int32_t max_versions, _Outptr_ flModelList** out_models); | ||
|
|
||
| // End V1 | ||
| /// Register a model in a local catalog. The input ModelInfo is copied. | ||
| FL_API_STATUS(RegisterModel, _In_ flCatalog* catalog, _In_ const flModelInfo* model_info, | ||
| _Outptr_ flModel** out_model); | ||
| /// Unregister by alias or model ID without deleting model assets. | ||
| FL_API_STATUS(UnregisterModel, _In_ flCatalog* catalog, _In_ const char* alias_or_model_id); | ||
| /// List models explicitly registered in this local catalog. | ||
| FL_API_STATUS(GetLocalModels, _In_ const flCatalog* catalog, _Outptr_ flModelList** out_models); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed? Does a user need to know/care that a model was added directly? We generally want to keep the public API as minimal as possible. Anything we add here is permanent so it must have a clear requirement to justify expanding the API surface. |
||
|
|
||
| // End V2 | ||
| }; | ||
|
|
||
| /* --- Model API --------------------------------------------------------- */ | ||
|
|
@@ -1038,6 +1072,17 @@ struct flModelApi { | |
| int64_t FL_API_T(Info_GetIntProperty, _In_ const flModelInfo* info, _In_ const char* key, int64_t default_value); | ||
|
|
||
| // End V1 | ||
| /// Create a caller-owned mutable ModelInfo. Release it with ReleaseModelInfo. | ||
| FL_API_STATUS(CreateModelInfo, _Outptr_ flModelInfo** out_info); | ||
| void FL_API_T(ReleaseModelInfo, _Frees_ptr_opt_ flModelInfo* info); | ||
| FL_API_STATUS(Info_SetStringProperty, _In_ flModelInfo* info, _In_ const char* key, _In_ const char* value); | ||
| FL_API_STATUS(Info_SetIntProperty, _In_ flModelInfo* info, _In_ const char* key, int64_t value); | ||
| FL_API_STATUS(Info_SerializeToFile, _In_ const flModelInfo* info, _In_ const char* file_path); | ||
| FL_API_STATUS(Info_DeserializeFromFile, _In_ const char* file_path, _Outptr_ flModelInfo** out_info); | ||
| /// Create a caller-owned deep copy. Release it with ReleaseModelInfo. | ||
| FL_API_STATUS(Info_Clone, _In_ const flModelInfo* info, _Outptr_ flModelInfo** out_info); | ||
|
Comment on lines
+1080
to
+1083
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naively I would have expected the user adding a model to create the model info in memory and provide that in a RegisterModel call. Why does the user need to serialize/deserialize or clone? |
||
|
|
||
| // End V2 | ||
| }; | ||
|
|
||
| #ifdef __cplusplus | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,9 @@ namespace detail { | |
| /// Returns nullptr if the library does not support the requested API version. | ||
| inline const flApi* api() { | ||
| static const flApi* p = FoundryLocalGetApi(FOUNDRY_LOCAL_API_VERSION); | ||
| if (!p) { | ||
| throw std::runtime_error("Foundry Local runtime does not support the API version requested by this header"); | ||
| } | ||
| return p; | ||
| } | ||
|
|
||
|
|
@@ -299,14 +302,33 @@ struct Runtime { | |
| std::optional<std::string_view> execution_provider; | ||
| }; | ||
|
|
||
| enum class CatalogType { | ||
| Public = FOUNDRY_LOCAL_CATALOG_PUBLIC, | ||
| Local = FOUNDRY_LOCAL_CATALOG_LOCAL, | ||
| Private = FOUNDRY_LOCAL_CATALOG_PRIVATE, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is private catalog ready? |
||
| }; | ||
|
|
||
| // =========================================================================== | ||
| // ModelInfo — non-owning read-only view | ||
| // ModelInfo — owning mutable value or non-owning read-only view | ||
| // =========================================================================== | ||
|
|
||
| /// Non-owning view over an opaque flModelInfo. Lifetime is tied to the owning Model/Catalog. Immutable. | ||
| /// Opaque model metadata. Default construction creates an owning mutable value for registration. | ||
| /// Construction from `const flModelInfo&` creates a non-owning read-only view tied to its Model/Catalog. | ||
| class ModelInfo { | ||
| public: | ||
| explicit ModelInfo(const flModelInfo& info) noexcept : info_(&info) {} | ||
| ModelInfo(); | ||
| explicit ModelInfo(const flModelInfo& info) noexcept : handle_(&info) {} | ||
|
|
||
| /// Create an independent, owning, mutable deep copy, including when the source is a borrowed view. | ||
| ModelInfo(const ModelInfo& other); | ||
| ModelInfo& operator=(const ModelInfo& other); | ||
| ModelInfo(ModelInfo&&) noexcept = default; | ||
| ModelInfo& operator=(ModelInfo&&) noexcept = default; | ||
|
Comment on lines
+322
to
+326
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need copy and assignment? |
||
|
|
||
| ModelInfo& SetStringProperty(const char* key, const char* value); | ||
| ModelInfo& SetIntProperty(const char* key, int64_t value); | ||
| void SerializeToFile(const std::string& file_path) const; | ||
| static ModelInfo DeserializeFromFile(const std::string& file_path); | ||
|
|
||
| // Core identity. | ||
| std::string_view Id() const noexcept; | ||
|
|
@@ -376,8 +398,11 @@ class ModelInfo { | |
| std::optional<std::string_view> Capabilities() const noexcept; | ||
|
|
||
| private: | ||
| explicit ModelInfo(flModelInfo& info); | ||
| static std::string_view safe(const char* s) noexcept { return s ? s : ""; } | ||
| const flModelInfo* info_; | ||
| detail::Base<flModelInfo> handle_; | ||
|
|
||
| friend class Catalog; | ||
| }; | ||
|
|
||
| // =========================================================================== | ||
|
|
@@ -781,6 +806,15 @@ class ICatalog { | |
| virtual ModelList GetModelVersions(const std::string& model_alias, | ||
| const std::string& variant_name = {}, | ||
| int max_versions = 50) = 0; | ||
| virtual std::unique_ptr<IModel> RegisterModel(const ModelInfo&) { | ||
| throw Error("models can only be registered in a local catalog", FOUNDRY_LOCAL_ERROR_INVALID_ARGUMENT); | ||
| } | ||
| virtual void UnregisterModel(const std::string&) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggest "Deregister" |
||
| throw Error("models can only be unregistered from a local catalog", FOUNDRY_LOCAL_ERROR_INVALID_ARGUMENT); | ||
| } | ||
| virtual ModelList GetLocalModels() const { | ||
| throw Error("local model listing is unsupported by this catalog", FOUNDRY_LOCAL_ERROR_INVALID_ARGUMENT); | ||
| } | ||
| }; | ||
|
|
||
| // =========================================================================== | ||
|
|
@@ -806,6 +840,9 @@ class Catalog final : public ICatalog { | |
| ModelList GetModelVersions(const std::string& model_alias, | ||
| const std::string& variant_name = {}, | ||
| int max_versions = 50) override; | ||
| std::unique_ptr<IModel> RegisterModel(const ModelInfo& model_info) override; | ||
| void UnregisterModel(const std::string& alias_or_model_id) override; | ||
| ModelList GetLocalModels() const override; | ||
|
|
||
| private: | ||
| detail::Base<flCatalog> handle_; | ||
|
|
@@ -835,6 +872,8 @@ class Manager { | |
|
|
||
| /// Get the catalog for querying models. Creates on first call, caches internally. | ||
| ICatalog& GetCatalog() const; | ||
| ICatalog& GetCatalog(CatalogType type) const; | ||
| ICatalog& GetCatalog(const std::string& catalog_name) const; | ||
|
|
||
| /// Start the embedded web service. | ||
| void StartWebService(); | ||
|
|
@@ -870,7 +909,9 @@ class Manager { | |
| detail::Base<flManager> handle_; | ||
| Configuration config_; | ||
| mutable std::unique_ptr<Catalog> catalog_; | ||
| mutable std::unique_ptr<Catalog> local_catalog_; | ||
| mutable std::unique_ptr<std::once_flag> catalog_once_{std::make_unique<std::once_flag>()}; | ||
| mutable std::unique_ptr<std::once_flag> local_catalog_once_{std::make_unique<std::once_flag>()}; | ||
| }; | ||
|
|
||
| // =========================================================================== | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is private catalog ready yet? if not, I don't think we should allow this option at all.