endpoint.rs 3.85 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

16
use std::{pin::Pin, sync::Arc};
17

Neelay Shah's avatar
Neelay Shah committed
18
use dynamo_llm::{
19
    backend::Backend,
20
    engines::StreamingEngineAdapter,
21
    model_type::ModelType,
22
    preprocessor::{BackendInput, BackendOutput},
23
    types::{
24
25
26
        openai::chat_completions::{
            NvCreateChatCompletionRequest, NvCreateChatCompletionStreamResponse,
        },
27
28
        Annotated,
    },
29
};
30
use dynamo_runtime::engine::AsyncEngineStream;
Neelay Shah's avatar
Neelay Shah committed
31
use dynamo_runtime::pipeline::{
32
    network::Ingress, Context, ManyOut, Operator, SegmentSource, ServiceBackend, SingleIn, Source,
33
};
34
use dynamo_runtime::{protocols::Endpoint as EndpointId, DistributedRuntime};
35

36
use crate::EngineConfig;
37
38

pub async fn run(
39
    distributed_runtime: DistributedRuntime,
40
41
42
    path: String,
    engine_config: EngineConfig,
) -> anyhow::Result<()> {
43
    let cancel_token = distributed_runtime.primary_token().clone();
44
    let endpoint_id: EndpointId = path.parse()?;
45

46
47
48
49
50
51
52
53
54
    let component = distributed_runtime
        .namespace(&endpoint_id.namespace)?
        .component(&endpoint_id.component)?;
    let endpoint = component
        .service_builder()
        .create()
        .await?
        .endpoint(&endpoint_id.name);

55
    let (rt_fut, mut card) = match engine_config {
56
        EngineConfig::StaticFull { engine, mut model } => {
57
            let engine = Arc::new(StreamingEngineAdapter::new(engine));
58
59
60
61
62
            let ingress_chat = Ingress::<
                Context<NvCreateChatCompletionRequest>,
                Pin<Box<dyn AsyncEngineStream<Annotated<NvCreateChatCompletionStreamResponse>>>>,
            >::for_engine(engine)?;

63
64
65
66
            model.attach(&endpoint, ModelType::Chat).await?;
            let fut_chat = endpoint.endpoint_builder().handler(ingress_chat).start();

            (fut_chat, model.card().clone())
67
        }
68
69
        EngineConfig::StaticCore {
            engine: inner_engine,
70
            mut model,
71
        } => {
72
73
74
            // Pre-processing is done ingress-side, so it should be already done.
            let frontend =
                SegmentSource::<SingleIn<BackendInput>, ManyOut<Annotated<BackendOutput>>>::new();
75
76
77
            let backend = Backend::from_mdc(model.card().clone())
                .await?
                .into_operator();
78
79
80
81
82
83
            let engine = ServiceBackend::from_engine(inner_engine);
            let pipeline = frontend
                .link(backend.forward_edge())?
                .link(engine)?
                .link(backend.backward_edge())?
                .link(frontend)?;
84
            let ingress = Ingress::for_pipeline(pipeline)?;
85
86
87
88
89

            model.attach(&endpoint, ModelType::Backend).await?;
            let fut = endpoint.endpoint_builder().handler(ingress).start();

            (fut, model.card().clone())
90
91
92
93
        }
        EngineConfig::Dynamic(_) => {
            anyhow::bail!("Cannot use endpoint for both in and out");
        }
94
95
    };

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    tokio::select! {
        _ = rt_fut => {
            tracing::debug!("Endpoint ingress ended");
        }
        _ = cancel_token.cancelled() => {
        }
    }

    // Cleanup on shutdown
    if let Err(err) = card
        .delete_from_nats(distributed_runtime.nats_client())
        .await
    {
        tracing::error!(%err, "delete_from_nats error on shutdown");
    }

    Ok(())
}