Model adapter overview(模型适配器概述)¶
Model adapters are a generalized framework to enable Foundry to interoperate with arbitrary models. Model adapters are one of the two components that make a model:
- Model artifacts: The model files, parameters, weights, containers, or credentials where a trained model is saved.
- Model adapter: The logic and the environment dependencies needed for Foundry to interact with the model artifacts to load, initialize, and perform inference with the model.
Model adapters can enable Foundry to interoperate with the following:
- Models trained in Foundry
- Model files trained outside of Foundry
- Models containerized outside of Foundry and pushed into the Foundry Docker registry
- Models trained and hosted outside of Foundry
Adapter components¶
Palantir interacts with all models in the same way by interfacing with the model adapter class of that model version.
Initialization¶
Since models can be created once, and used in multiple places within the platform, the adapter needs to be aware of how to initialize an instance of models from either the weights or the underlying container.
For weights trained within the platform, users should use the @auto_serialize annotation to leverage built-in serializers that should work with most model types. In advanced cases where serialization / deserialization logic must be explicitly specified, see: load() and _save() methods.
For container and external models, the adapter is initialized using either the init_container(), or init_external() method, so that it can be used for inference. For both types of models, the load() method is also called when the model is initialized, but it then calls init_container() or init_external() in the background so that only the context that is relevant to these types of models (for example, ContainerizedApplicationContext or ExternalModelContext) is provided to the model instances. As an alternative to using init_container() or init_external(), users can overwrite the load() method to define how these models should be initialized.
:::callout{theme="neutral"}
To use a model in a transform, the environment must contain the model adapter's dependencies. To prevent dependency conflicts between model adapters and transform environments, set use_sidecar = True in ModelInput to run the model in an isolated container. predict() requests will be automatically routed to the sidecar without additional code changes. Review the ModelInput class reference for more details.
:::
API¶
Each adapter is required to declare an API description. The platform relies on this description, which includes the expected inputs, outputs, column names and type, for enabling integrations with other applications for a variety of model consumption patterns.
For supported types and examples of API definitions, review the API reference.
Inference¶
Once initialized, the adapter can be used for inference for either batch or interactive workloads. The inference logic must be defined as part of the predict() method.
The platform uses the provided API definition to call the predict() method with the defined names and types so that inference can be performed.

For more information on creating model adapters, refer to the documentation on creating model adapters. You can also consult the full Python API on the model adapter reference page.
中文翻译¶
模型适配器概述¶
模型适配器(Model adapter)是一个通用框架,使 Foundry 能够与任意模型进行互操作。模型适配器是构成模型的两个组件之一:
- 模型产物(Model artifacts): 保存训练后模型的模型文件、参数、权重、容器或凭证。
- 模型适配器(Model adapter): Foundry 与模型产物进行交互所需的逻辑和环境依赖,用于加载、初始化模型并执行推理。
模型适配器可使 Foundry 与以下内容进行互操作:
适配器组件¶
Palantir 通过与模型版本的模型适配器类(model adapter class)进行交互,以相同方式与所有模型交互。
初始化¶
由于模型可以创建一次并在平台内多处使用,适配器需要了解如何从权重或底层容器初始化模型实例。
对于在平台内训练的权重,用户应使用 @auto_serialize 注解来利用内置的序列化器,这些序列化器适用于大多数模型类型。在必须明确指定序列化/反序列化逻辑的高级场景中,请参阅:load() 和 _save() 方法。
对于容器模型和外部模型,适配器通过 init_container() 或 init_external() 方法进行初始化,以便用于推理。对于这两种类型的模型,在模型初始化时也会调用 load() 方法,但该方法会在后台调用 init_container() 或 init_external(),从而仅向模型实例提供与这些类型模型相关的上下文(例如 ContainerizedApplicationContext 或 ExternalModelContext)。作为使用 init_container() 或 init_external() 的替代方案,用户可以重写 load() 方法来定义这些模型的初始化方式。
:::callout{theme="neutral"}
要在转换(transform)中使用模型,环境中必须包含模型适配器的依赖项。为防止模型适配器与转换环境之间的依赖冲突,请在 ModelInput 中设置 use_sidecar = True,以便在隔离容器中运行模型。predict() 请求将自动路由到 sidecar,无需额外修改代码。有关更多详细信息,请参阅 ModelInput 类参考。
:::
API¶
每个适配器都需要声明一个 API 描述。平台依赖此描述(包括预期的输入、输出、列名和类型)来实现与其他应用程序的集成,以支持多种模型消费模式。
有关支持的 API 定义类型和示例,请参阅 API 参考。
推理¶
初始化后,适配器可用于批处理或交互式工作负载的推理。推理逻辑必须作为 predict() 方法的一部分进行定义。
平台使用提供的 API 定义,以定义的名称和类型调用 predict() 方法,从而执行推理。

有关创建模型适配器的更多信息,请参阅创建模型适配器的文档。您还可以在模型适配器参考页面上查阅完整的 Python API。