|
| 1 | +# KServe and Triton model repositories |
| 2 | + |
| 3 | +OpenMed can package an exported ONNX token classifier as a Triton model |
| 4 | +repository and use a user-operated Triton or KServe endpoint as its inference |
| 5 | +backend. Tokenization, source offsets, and entity decoding remain in the |
| 6 | +OpenMed process. The server receives numeric tensors and returns `logits`. |
| 7 | + |
| 8 | +OpenMed does not install or start Triton or KServe. Operate the serving system |
| 9 | +separately and apply the authentication, network isolation, and transport |
| 10 | +controls required for your deployment. |
| 11 | + |
| 12 | +## Install client and export dependencies |
| 13 | + |
| 14 | +Repository generation needs ONNX support. Remote HTTP and gRPC inference use |
| 15 | +client libraries in the `triton` extra: |
| 16 | + |
| 17 | +```bash |
| 18 | +pip install 'openmed[onnx,triton]' |
| 19 | +``` |
| 20 | + |
| 21 | +The extra contains no KServe or Triton server distribution. HTTP follows the |
| 22 | +KServe V2 JSON protocol. gRPC uses a small, wire-compatible subset of the |
| 23 | +KServe V2 protobuf contract and the standard |
| 24 | +`inference.GRPCInferenceService/ModelInfer` method. |
| 25 | + |
| 26 | +## 1. Export and build the repository |
| 27 | + |
| 28 | +Start with an OpenMed ONNX token-classification artifact produced by |
| 29 | +`openmed.onnx.convert`. For `max_batch_size > 0`, every graph input and output |
| 30 | +must have a dynamic leading batch dimension. The graph must expose |
| 31 | +`input_ids`, `attention_mask`, and a `logits` output. |
| 32 | + |
| 33 | +```python |
| 34 | +from pathlib import Path |
| 35 | + |
| 36 | +from openmed.onnx import convert |
| 37 | +from openmed.service.backends import write_triton_model_repository |
| 38 | + |
| 39 | +artifact_dir = Path("build/openmed-pii-onnx") |
| 40 | +convert( |
| 41 | + "OpenMed/OpenMed-PII-SuperClinical-Small-44M-v1", |
| 42 | + artifact_dir, |
| 43 | +) |
| 44 | + |
| 45 | +repository = write_triton_model_repository( |
| 46 | + artifact_dir / "model.onnx", |
| 47 | + "build/model-repository", |
| 48 | + model_name="openmed_pii", |
| 49 | + version=1, |
| 50 | + max_batch_size=8, |
| 51 | +) |
| 52 | +print(repository.model_dir) |
| 53 | +``` |
| 54 | + |
| 55 | +The adapter derives tensor names, data types, and non-batch dimensions from |
| 56 | +the ONNX graph and renders a deterministic `config.pbtxt` using Triton's |
| 57 | +`onnxruntime` backend: |
| 58 | + |
| 59 | +```text |
| 60 | +build/model-repository/ |
| 61 | +└── openmed_pii/ |
| 62 | + ├── config.pbtxt |
| 63 | + └── 1/ |
| 64 | + └── model.onnx |
| 65 | +``` |
| 66 | + |
| 67 | +For a graph with external tensor data, the adapter uses Triton's documented |
| 68 | +multi-file ONNX form and copies every referenced sidecar: |
| 69 | + |
| 70 | +```text |
| 71 | +build/model-repository/openmed_pii/1/ |
| 72 | +└── model.onnx/ |
| 73 | + ├── model.onnx |
| 74 | + └── weights.bin |
| 75 | +``` |
| 76 | + |
| 77 | +Absolute, parent-relative, and symlink-escaping external-data paths are |
| 78 | +rejected. Existing numeric model versions are never overwritten, and a new |
| 79 | +version must have the same derived schema as the existing config. |
| 80 | + |
| 81 | +Recheck a copied repository before mounting it: |
| 82 | + |
| 83 | +```python |
| 84 | +from openmed.service.backends import validate_triton_model_repository |
| 85 | + |
| 86 | +validate_triton_model_repository( |
| 87 | + "build/model-repository", |
| 88 | + model_name="openmed_pii", |
| 89 | + version=1, |
| 90 | +) |
| 91 | +``` |
| 92 | + |
| 93 | +## 2. Serve it in your infrastructure |
| 94 | + |
| 95 | +Mount `build/model-repository` into your own Triton deployment and point |
| 96 | +`--model-repository` at that mount. For KServe, configure its Triton runtime or |
| 97 | +storage initializer to expose the same repository. Verify the V2 model-ready |
| 98 | +endpoint before routing OpenMed traffic: |
| 99 | + |
| 100 | +```bash |
| 101 | +curl --fail https://triton.example/v2/models/openmed_pii/ready |
| 102 | +``` |
| 103 | + |
| 104 | +Server installation, GPU tuning, autoscaling, credentials, and lifecycle |
| 105 | +management remain outside OpenMed. |
| 106 | + |
| 107 | +## 3. Select remote inference through configuration |
| 108 | + |
| 109 | +Keep the original artifact or model repository available to the OpenMed |
| 110 | +client. Its fast tokenizer and `id2label` metadata are used locally; only the |
| 111 | +ONNX graph executes on the remote server. |
| 112 | + |
| 113 | +```python |
| 114 | +from openmed.core import ModelLoader, OpenMedConfig |
| 115 | + |
| 116 | +config = OpenMedConfig( |
| 117 | + backend="remote", |
| 118 | + remote_inference_endpoint="https://triton.example", |
| 119 | + remote_inference_protocol="http", |
| 120 | + remote_inference_model_name="openmed_pii", |
| 121 | + remote_inference_model_version="1", |
| 122 | + remote_inference_tokenizer="build/openmed-pii-onnx", |
| 123 | + remote_inference_timeout_seconds=30, |
| 124 | + remote_inference_verify_tls=True, |
| 125 | +) |
| 126 | + |
| 127 | +loader = ModelLoader(config) |
| 128 | +pipeline = loader.create_pipeline( |
| 129 | + "OpenMed/OpenMed-PII-SuperClinical-Small-44M-v1", |
| 130 | + task="token-classification", |
| 131 | + aggregation_strategy="simple", |
| 132 | +) |
| 133 | +entities = pipeline("Synthetic patient Alice Nguyen") |
| 134 | +``` |
| 135 | + |
| 136 | +No pipeline call-site change is required. `OpenMedConfig.backend` selects the |
| 137 | +remote implementation through the normal `ModelLoader.create_pipeline` path. |
| 138 | +The pipeline accepts one string or a batch and returns the same entity |
| 139 | +dictionary shape and source spans as local ONNX inference. |
| 140 | + |
| 141 | +The settings can also be stored in the normal flat TOML configuration: |
| 142 | + |
| 143 | +```toml |
| 144 | +backend = "remote" |
| 145 | +remote_inference_endpoint = "https://kserve.example" |
| 146 | +remote_inference_protocol = "http" |
| 147 | +remote_inference_model_name = "openmed_pii" |
| 148 | +remote_inference_model_version = "1" |
| 149 | +remote_inference_tokenizer = "/models/openmed-pii-onnx" |
| 150 | +remote_inference_timeout_seconds = 30.0 |
| 151 | +remote_inference_verify_tls = true |
| 152 | +``` |
| 153 | + |
| 154 | +For Triton gRPC, use a gRPC target and protocol: |
| 155 | + |
| 156 | +```python |
| 157 | +config = OpenMedConfig( |
| 158 | + backend="remote", |
| 159 | + remote_inference_endpoint="grpcs://triton.example:8001", |
| 160 | + remote_inference_protocol="grpc", |
| 161 | + remote_inference_model_name="openmed_pii", |
| 162 | + remote_inference_tokenizer="/models/openmed-pii-onnx", |
| 163 | +) |
| 164 | +``` |
| 165 | + |
| 166 | +`grpc://` and bare `host:port` targets are plaintext. `grpcs://` uses TLS and |
| 167 | +does not permit certificate verification to be disabled. HTTP deployments |
| 168 | +should likewise keep `remote_inference_verify_tls=true`. |
| 169 | + |
| 170 | +## Privacy and offline boundary |
| 171 | + |
| 172 | +The adapter does not log source text, request tensors, response bodies, or raw |
| 173 | +server errors. It sends token IDs, attention masks, and tokenizer-required |
| 174 | +token-type IDs rather than the original string. These tensors can still encode |
| 175 | +sensitive content, so treat the endpoint as inside the trusted PHI boundary, |
| 176 | +use transport encryption, and enforce authentication and network policy in |
| 177 | +your serving infrastructure. |
| 178 | + |
| 179 | +Remote inference is explicit only: it is never auto-selected. It is also |
| 180 | +rejected when `local_only=True` or `OPENMED_OFFLINE=1`, because either setting |
| 181 | +promises a no-egress runtime. |
| 182 | + |
| 183 | +Protocol references: |
| 184 | + |
| 185 | +- [Triton model repository layout](https://docs.nvidia.com/deeplearning/triton-inference-server/user-guide/docs/user_guide/model_repository.html) |
| 186 | +- [Triton model configuration](https://docs.nvidia.com/deeplearning/triton-inference-server/user-guide/docs/user_guide/model_configuration.html) |
| 187 | +- [KServe V2 inference protocol](https://kserve.github.io/website/docs/concepts/architecture/data-plane/v2-protocol) |
0 commit comments