-
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
Open
Selena Yang (selenayang888)
wants to merge
6
commits into
main
Choose a base branch
from
syang/bring-your-own-model
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
29a1bad
the implementation for BYOM
selenayang888 15e1dde
Add deep-copy semantics for ModelInfo
selenayang888 f1cb59a
Resolve conflicts
selenayang888 c1f1b8b
Fix the comment about "Deferred registrations"
selenayang888 0c6a729
fixed execution-provider override bug
selenayang888 a2dd9b2
Merge remote-tracking branch 'origin/main' into syang/bring-your-own-…
selenayang888 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 3 | ||
|
|
||
| /* ----------------------------------------------------------------------- | ||
| * 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, | ||
|
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 yet? if not, I don't think we should allow this option at all. |
||
| } 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" | ||
|
|
||
| /* 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 | ||
| #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); | ||
|
|
||
| // 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); | ||
|
|
||
| // End V2 | ||
| }; | ||
|
|
||
| /* --- Model API --------------------------------------------------------- */ | ||
|
|
@@ -1038,6 +1072,19 @@ 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); | ||
|
|
||
| // End V2 | ||
| /// Create a caller-owned deep copy. Release it with ReleaseModelInfo. | ||
| FL_API_STATUS(Info_Clone, _In_ const flModelInfo* info, _Outptr_ flModelInfo** out_info); | ||
|
|
||
| // End V3 | ||
| }; | ||
|
|
||
| #ifdef __cplusplus | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
| 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&) { | ||
| 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>()}; | ||
| }; | ||
|
|
||
| // =========================================================================== | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
why 1 --> 3 and not 1 --> 2?