o
    پi                     @   s   d Z ddlmZ 	 ddlmZmZmZ ddlZddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZmZ ddlmZmZ G dd deZG dd dejZG dd deZegZdS )a-  
Copyright 2023-2024 SGLang Team
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.
    )
add_prefix)IterableOptionalTupleN)nn)LlamaConfig)get_pp_group)LogitsProcessor)QuantizationConfig)ParallelLMHeadVocabParallelEmbedding)ForwardBatchPPProxyTensors)LlamaDecoderLayerLlamaForCausalLMc                       s>   e Zd Z			ddededee deddf
 fd	d
Z  Z	S )r   r   N configlayer_idquant_configprefixreturnc                    s6   t  |||| |dkr| `t| ddd  d S d S )Nr   input_layernormc                 S   s   | S )N )xr   r   Q/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/models/llama_eagle.py<lambda>5   s    z,LlamaDecoderLayer.__init__.<locals>.<lambda>)super__init__r   setattr)selfr   r   r   r   	__class__r   r   r   (   s
   zLlamaDecoderLayer.__init__)r   Nr   )
__name__
__module____qualname__r   intr   r
   strr   __classcell__r   r   r    r   r   '   s    r   c                       sl   e Zd Z		ddedee deddf fddZ		dd	ej	d
ej	de
dej	dee dej	fddZ  ZS )
LlamaModelNr   r   r   r   r   c                    sr   t     | _ j| _t j jtdd| _t	 fddt
 jD | _tj jd  j| _d S )Nembed_tokens)r   c              	      s(   g | ]}t  |td | dqS )zlayers.r   r   )r   r   ).0ir   r   r   r   r   
<listcomp>H   s    z'LlamaModel.__init__.<locals>.<listcomp>   )r   r   r   
vocab_sizer   hidden_sizer   r)   r   
ModuleListrangenum_hidden_layerslayerstorchLinearfcr   r   r   r   r    r-   r   r   9   s   
zLlamaModel.__init__	input_ids	positionsforward_batchinput_embedspp_proxy_tensorsc           
      C   sp   |d u r
|  |}n|}| tj||jjfdd}d }tt| jD ]}| j| }	|	||||\}}q#|| S )N)dim)	r)   r8   r6   cat	spec_infohidden_statesr3   lenr5   )
r   r:   r;   r<   r=   r>   rC   residualr,   layerr   r   r   forwardT   s    

zLlamaModel.forwardNr   )NN)r"   r#   r$   r   r   r
   r&   r   r6   Tensorr   r   rG   r'   r   r   r    r   r(   8   s6     r(   c                	       sT   e Zd Z		ddedee deddfddZd	ee	ee
jf  f fd
dZ  ZS )LlamaForCausalLMEagleNr   r   r   r   r   c                 C   s   t j|  || _|| _t | _t||td|d| _	| jj
r%| j	j| _ntt|d|j|j|td|d| _t|| _d| _d S )Nmodelr*   hot_vocab_sizelm_headF)r   Moduler   r   r   r   pp_groupr(   r   rK   tie_word_embeddingsr)   rM   r   getattrr0   r1   r	   logits_processorcapture_aux_hidden_statesr9   r   r   r   r   r   s"   

zLlamaForCausalLMEagle.__init__weightsc                    s4   |D ]\}}d|vrd| }t  ||fg qd S )NrM   zmodel.)r   load_weights)r   rT   nameloaded_weightr    r   r   rU      s   z"LlamaForCausalLMEagle.load_weightsrH   )r"   r#   r$   r   r   r
   r&   r   r   r   r6   rI   rU   r'   r   r   r    r   rJ   q   s    
(rJ   )__doc__sglang.srt.utilsr   typingr   r   r   r6   r   transformersr   sglang.srt.distributedr   "sglang.srt.layers.logits_processorr	   *sglang.srt.layers.quantization.base_configr
   *sglang.srt.layers.vocab_parallel_embeddingr   r   ,sglang.srt.model_executor.forward_batch_infor   r   sglang.srt.models.llamar   r   rN   r(   rJ   
EntryClassr   r   r   r   <module>   s"    9
$