|
| 1 | +# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from dataclasses import dataclass |
| 18 | +from typing import TYPE_CHECKING, Protocol, cast |
| 19 | + |
| 20 | +from megatron.core.process_groups_config import ProcessGroupCollection |
| 21 | +from megatron.core.transformer import MegatronModule |
| 22 | + |
| 23 | +from nemo_rl.models.megatron.draft.utils import build_draft_model |
| 24 | +from nemo_rl.models.policy.draft_config import Eagle3DraftConfig |
| 25 | + |
| 26 | +if TYPE_CHECKING: |
| 27 | + from megatron.bridge.models.model_provider import ModelProviderMixin |
| 28 | + |
| 29 | + |
| 30 | +class DraftSpeculator(Protocol): |
| 31 | + """Model-build operation selected by a configured draft speculator.""" |
| 32 | + |
| 33 | + config: Eagle3DraftConfig |
| 34 | + |
| 35 | + def build_model( |
| 36 | + self, |
| 37 | + *, |
| 38 | + model_provider: ModelProviderMixin, |
| 39 | + pg_collection: ProcessGroupCollection, |
| 40 | + policy_model_chunk: MegatronModule, |
| 41 | + ) -> MegatronModule | None: |
| 42 | + """Build the draft model for the policy chunk that owns it.""" |
| 43 | + |
| 44 | + |
| 45 | +@dataclass(frozen=True) |
| 46 | +class Eagle3Speculator: |
| 47 | + """Current EAGLE-3 draft speculator.""" |
| 48 | + |
| 49 | + config: Eagle3DraftConfig |
| 50 | + |
| 51 | + def build_model( |
| 52 | + self, |
| 53 | + *, |
| 54 | + model_provider: ModelProviderMixin, |
| 55 | + pg_collection: ProcessGroupCollection, |
| 56 | + policy_model_chunk: MegatronModule, |
| 57 | + ) -> MegatronModule | None: |
| 58 | + """Build the EAGLE-3 draft model with the existing implementation.""" |
| 59 | + return build_draft_model( |
| 60 | + model_provider=model_provider, |
| 61 | + draft_config=self.config, |
| 62 | + pg_collection=pg_collection, |
| 63 | + policy_model_chunk=policy_model_chunk, |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +_SPECULATOR_FACTORIES: dict[str, type[Eagle3Speculator]] = { |
| 68 | + "eagle3": Eagle3Speculator, |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +def resolve_draft_speculator( |
| 73 | + config: Eagle3DraftConfig | None, |
| 74 | +) -> DraftSpeculator | None: |
| 75 | + """Resolve an enabled draft configuration to its speculator.""" |
| 76 | + if config is None or not config.enabled: |
| 77 | + return None |
| 78 | + return cast( |
| 79 | + DraftSpeculator, |
| 80 | + _SPECULATOR_FACTORIES[config.speculator_type](config), |
| 81 | + ) |
0 commit comments