# SPDX-License-Identifier: Apache-2.0

from typing import List, Optional, Set, Tuple

import torch
import torch.nn as nn

from vllm.distributed.parallel_state import (get_pp_group, get_tp_group,
                                             init_model_parallel_group,
                                             patch_model_parallel_group)
from vllm.logger import init_logger
from vllm.model_executor.layers.sampler import SamplerOutput
from vllm.model_executor.model_loader.weight_utils import default_weight_loader
from vllm.sequence import ExecuteModelRequest
from vllm.spec_decode.interfaces import SpeculativeProposals
from vllm.spec_decode.multi_step_worker import MultiStepWorker
from vllm.spec_decode.proposer_worker_base import ProposerWorkerBase

logger = init_logger(__name__)


class _DummyModel(nn.Module):
    pass


class SmallerTpPpProposerWorker(ProposerWorkerBase):
    """Class which allows a speculative draft model to run with smaller tensor
    parallel degree and pipeline parallel degree than target model.
    This reduces the communication overhead of small draft models.

    To implement this feature, this class differs behavior based on is_dummy
    flag, where dummy means worker that does not participate draft generation.
    Participating workers use a smaller tp group by patching vLLM's tensor
    parallel group temporarily during forward passes of draft models.
    """

    @classmethod
    def maybe_wrap_worker(cls, worker, draft_tensor_parallel_size: int,
                          target_tensor_parallel_size: int,
                          draft_pipeline_parallel_size: int,
                          target_pipeline_parallel_size: int):
        """Wrap the worker in a SmallerTpProposerWorker if necessary.
        """
        if draft_tensor_parallel_size == target_tensor_parallel_size and \
                draft_pipeline_parallel_size == target_pipeline_parallel_size:
            return worker

        # gpu ranks that will generate draft tokens together
        draft_tp_ranks = list(range(draft_tensor_parallel_size))
        draft_pp_ranks = list(range(draft_pipeline_parallel_size))

        logger.info("Wrapping {%s} in {%s}", type(worker), cls)
        return cls(worker, draft_tp_ranks, draft_pp_ranks)

    def __init__(self, worker: MultiStepWorker, draft_tp_ranks: List[int],
                 draft_pp_ranks: List[int]):
        """Create a SmallerTpPpProposerWorker.

        Args:
            worker (MultiStepWorker): an actual worker wrapped with this class
            draft_tp_ranks (List[int]): if this value is given, only the GPU tp
             ranks written in this value participate in draft generation
            draft_pp_ranks (List[int]): if this value is given, only the GPU pp
             ranks written in this value participate in draft generation
        """
        self._worker = worker
        self._draft_tp_ranks = draft_tp_ranks
        self._draft_pp_ranks = draft_pp_ranks

        # init during init_device
        self._is_dummy = False
        self._tp_group = None
        self._pp_group = None

    def _patch_model_parallel_group(self):
        """Temporarily patch the global pp group state with its own pp group
        state.
        """
        return patch_model_parallel_group(self._tp_group, self._pp_group)

    def init_device(self) -> None:
        # The non-driver workers need to be dummy
    
        self._is_dummy = get_tp_group().rank  not in self._draft_tp_ranks \
                         and get_pp_group().rank  not in self._draft_pp_ranks
                         
        
        
        # dummy workers do nothing
        if self._is_dummy:
            return

        # creates tp and pp process group containing only a subset of gpu ranks
        local_tp_rank = get_tp_group().local_rank
        local_pp_rank = get_pp_group().local_rank
        tp_backend = torch.distributed.get_backend(get_tp_group().device_group)
        pp_backend = torch.distributed.get_backend(get_pp_group().device_group)
        
        all_ranks = torch.arange(len(self._draft_tp_ranks) * len(self._draft_pp_ranks)).reshape( len(self._draft_pp_ranks), len(self._draft_tp_ranks))
        group_ranks = all_ranks.view(-1, len(self._draft_tp_ranks)).unbind(0)
        group_ranks = [x.tolist() for x in group_ranks]
        
        self._tp_group = init_model_parallel_group(group_ranks,
                                                   local_tp_rank, tp_backend)
        
        group_ranks = all_ranks.transpose(0, 1).reshape(
        -1, len(self._draft_pp_ranks)).unbind(0)
        group_ranks = [x.tolist() for x in group_ranks]
        self._pp_group = init_model_parallel_group(group_ranks,
                                                   local_pp_rank, pp_backend)

        with self._patch_model_parallel_group():
            self._worker.init_device()

    def set_include_gpu_probs_tensor(self) -> None:
        if self._is_dummy:
            return

        # Need include_gpu_probs_tensor for multi_step_worker
        self._worker.set_include_gpu_probs_tensor()

    def set_should_modify_greedy_probs_inplace(self) -> None:
        if self._is_dummy:
            return

        self._worker.set_should_modify_greedy_probs_inplace()

    def load_model(self) -> None:
        if self._is_dummy:
            return

        with self._patch_model_parallel_group():
            self._worker.load_model()

    def determine_num_available_blocks(self) -> Tuple[int, int]:
        if self._is_dummy:
            # this case is not used now
            return -1, -1

        with self._patch_model_parallel_group():
            return self._worker.determine_num_available_blocks()

    def initialize_cache(self, num_gpu_blocks: int,
                         num_cpu_blocks: int) -> None:
        if self._is_dummy:
            return

        with self._patch_model_parallel_group():
            self._worker.initialize_cache(num_gpu_blocks, num_cpu_blocks)

    def sampler_output(
        self,
        execute_model_req: ExecuteModelRequest,
        sample_len: int,
        seq_ids_with_bonus_token_in_last_step: Set[int],
    ) -> Tuple[List[SamplerOutput], bool]:
        # Do not check _is_dummy, as it's always called by get_spec_proposals
        return self._worker.sampler_output(
            execute_model_req, sample_len,
            seq_ids_with_bonus_token_in_last_step)

    def get_spec_proposals(
        self,
        execute_model_req: ExecuteModelRequest,
        seq_ids_with_bonus_token_in_last_step: Set[int],
    ) -> SpeculativeProposals:
        """Produce speculations given an input batch of sequences. The number of
        speculative tokens per sequence is determined by max_proposal_len.
        """
        if self._is_dummy:
            return SpeculativeProposals(None, None, None)

        with self._patch_model_parallel_group():
            return self._worker.get_spec_proposals(
                execute_model_req, seq_ids_with_bonus_token_in_last_step)

    def get_model(self) -> nn.Module:
        if self._is_dummy:
            return _DummyModel()

        with self._patch_model_parallel_group():
            return self._worker.get_model()

    def execute_model(
        self,
        execute_model_req: Optional[ExecuteModelRequest] = None
    ) -> List[SamplerOutput]:
        if self._is_dummy:
            return []

        with self._patch_model_parallel_group():
            return self._worker.execute_model(execute_model_req)

    def get_cache_block_size_bytes(self) -> int:
        if self._is_dummy:
            # by returning zero, target worker can use the entire kv cache space
            return 0

        return self._worker.get_cache_block_size_bytes()

    @property
    def vocab_size(self) -> int:
        return self._worker.vocab_size

    def maybe_load_lm_head_weight(
        self,
        lm_head_weight: torch.Tensor,
    ) -> None:
        if self._is_dummy:
            return

        with self._patch_model_parallel_group():
            weight_loader = getattr(
                self._worker.worker.model_runner.model_runner.model.\
                    lm_head.weight,
                "weight_loader",
                default_weight_loader)
            weight_loader(
                self._worker.worker.model_runner.model_runner.model.\
                    lm_head.weight,
                lm_head_weight)
