
    2VhK[                         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 d dlmZ d d	l	m
Z
 d d
lmZ d dlmZ  e
d       G d de             Zy)    N)activations)constraints)dtype_policies)initializers)ops)
quantizers)regularizers)keras_export)	InputSpec)Layerzkeras.layers.Densec                        e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 d fd	Zd Zed        ZddZd Z		 	 	 ddZ
d Zd	 Z fd
Zd Zd Zd Zd ZddZddZddZd Z xZS )Densea  Just your regular densely-connected NN layer.

    `Dense` implements the operation:
    `output = activation(dot(input, kernel) + bias)`
    where `activation` is the element-wise activation function
    passed as the `activation` argument, `kernel` is a weights matrix
    created by the layer, and `bias` is a bias vector created by the layer
    (only applicable if `use_bias` is `True`).

    Note: If the input to the layer has a rank greater than 2, `Dense`
    computes the dot product between the `inputs` and the `kernel` along the
    last axis of the `inputs` and axis 0 of the `kernel` (using `tf.tensordot`).
    For example, if input has dimensions `(batch_size, d0, d1)`, then we create
    a `kernel` with shape `(d1, units)`, and the `kernel` operates along axis 2
    of the `input`, on every sub-tensor of shape `(1, 1, d1)` (there are
    `batch_size * d0` such sub-tensors). The output in this case will have
    shape `(batch_size, d0, units)`.

    Args:
        units: Positive integer, dimensionality of the output space.
        activation: Activation function to use.
            If you don't specify anything, no activation is applied
            (ie. "linear" activation: `a(x) = x`).
        use_bias: Boolean, whether the layer uses a bias vector.
        kernel_initializer: Initializer for the `kernel` weights matrix.
        bias_initializer: Initializer for the bias vector.
        kernel_regularizer: Regularizer function applied to
            the `kernel` weights matrix.
        bias_regularizer: Regularizer function applied to the bias vector.
        activity_regularizer: Regularizer function applied to
            the output of the layer (its "activation").
        kernel_constraint: Constraint function applied to
            the `kernel` weights matrix.
        bias_constraint: Constraint function applied to the bias vector.
        lora_rank: Optional integer. If set, the layer's forward pass
            will implement LoRA (Low-Rank Adaptation)
            with the provided rank. LoRA sets the layer's kernel
            to non-trainable and replaces it with a delta over the
            original kernel, obtained via multiplying two lower-rank
            trainable matrices. This can be useful to reduce the
            computation cost of fine-tuning large dense layers.
            You can also enable LoRA on an existing
            `Dense` layer by calling `layer.enable_lora(rank)`.
        lora_alpha: Optional integer. If set, this parameter scales the
            low-rank adaptation delta (computed as the product of two lower-rank
            trainable matrices) during the forward pass. The delta is scaled by
            `lora_alpha / lora_rank`, allowing you to fine-tune the strength of
            the LoRA adjustment independently of `lora_rank`.

    Input shape:
        N-D tensor with shape: `(batch_size, ..., input_dim)`.
        The most common situation would be
        a 2D input with shape `(batch_size, input_dim)`.

    Output shape:
        N-D tensor with shape: `(batch_size, ..., units)`.
        For instance, for a 2D input with shape `(batch_size, input_dim)`,
        the output would have shape `(batch_size, units)`.
    c                    t        |   dd|i| || _        t        j                  |      | _        || _        t        j                  |      | _        t        j                  |      | _	        t        j                  |      | _        t        j                  |      | _        t        j                  |	      | _        t        j                  |
      | _        || _        ||n|| _        d| _        t'        d      | _        d| _        y )Nactivity_regularizerF   )min_ndimT )super__init__unitsr   get
activationuse_biasr   kernel_initializerbias_initializerr	   kernel_regularizerbias_regularizerr   kernel_constraintbias_constraint	lora_rank
lora_alphalora_enabledr   
input_specsupports_masking)selfr   r   r   r   r   r   r   r   r   r   r    r!   kwargs	__class__s                 K/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/layers/core/dense.pyr   zDense.__init__M   s      	M.BMfM
%//*5 "."2"23E"F , 0 01A B"."2"23E"F , 0 01A B!,1B!C*?"(2(>*I!#Q/ $    c                 D   |d   | j                   f}| j                  r| j                  || j                         | j                  dk7  r9| j                  d|| j                  | j
                  | j                        | _        | j                  rE| j                  d| j                   f| j                  | j                  | j                        | _        nd | _        t        dd|d   i      | _        d	| _        | j                   r| j#                  | j                          y y )
N)modeint8kernel)nameshapeinitializerregularizer
constraintbiasr   )r   axesT)r   quantization_modequantized_build
add_weightr   r   r   _kernelr   r   r   r   r4   r   r#   builtr    enable_lora)r%   input_shapekernel_shapes      r(   buildzDense.buildm   s   #B4!!  D4J4J K!!V+  ??" 33 3311 + DL ==zzm 11 11// ( DI DI#Qb+b/5JK
>>T^^, r)   c                    | j                   st        d      | j                  rQ| j                  | j                  | j
                  z  t        j                  | j                  | j                        z  z   S | j                  S )Nz3You must build the layer before accessing `kernel`.)
r:   AttributeErrorr"   r9   r!   r    r   matmullora_kernel_alora_kernel_b)r%   s    r(   r.   zDense.kernel   ss    zz E  <<$..0

4--t/A/AB#C C C ||r)   c                     t        j                  || j                        }| j                   t        j                  || j                        }| j
                  | j                  |      }|S N)r   rA   r.   r4   addr   )r%   inputstrainingxs       r(   callz
Dense.call   sQ    JJvt{{+99 499%A??&"Ar)   c                 L    t        |      }| j                  |d<   t        |      S )Nr+   )listr   tuple)r%   r<   output_shapes      r(   compute_output_shapezDense.compute_output_shape   s%    K(::R\""r)   c                    | j                   rt        d      | j                  st        d      | j                  rt        d      | j                  j                          | j                  d| j                  j                  d   |ft        j                  |      | j                        | _        | j                  d|| j                  j                  d   ft        j                  |      | j                        | _        d	| j                  _        | j                  j!                          d
| _        || _        ||| _        y || _        y )Nz}Lora is incompatible with kernel constraints. In order to enable lora on this layer, remove the `kernel_constraint` argument.z3Cannot enable lora on a layer that isn't yet built.z>lora is already enabled. This can only be done once per layer.rB   r   )r/   r0   r1   r2   rC      FT)r   
ValueErrorr:   r"   _trackerunlockr8   r.   r0   r   r   r   rB   rC   r9   	trainablelockr    r!   )r%   rankr!   a_initializerb_initializers        r(   r;   zDense.enable_lora   s4    !!0 
 zzE  P  	!__ ;;$$Q'.$((7//	 - 
 "__ **1-.$((7//	 - 
 "' (2(>*Dr)   c                    | j                   sy | j                         \  }}|g}| j                  r|j                  | j                         | j
                  | j
                  dk(  r|j                  |       n| j
                  dk(  r|j                  | j                         |j                  | j                         |j                  | j                         |j                  | j                         |j                  | j                         |j                  | j                         n| j                  | j
                        t        |      D ]  \  }}||t        |      <    y Nr-   float8)r:   _get_kernel_with_merged_lorar   appendr4   r6   inputs_scaleinputs_amax_historykernel_scalekernel_amax_historyoutputs_grad_scaleoutputs_grad_amax_history_quantization_mode_error	enumeratestr)r%   storekernel_valuera   target_variablesivariables          r(   save_own_variableszDense.save_own_variables   s-   zz &*%F%F%H"l(>==##DII.!!-%%/ ''5''83 ''(9(9: ''(@(@A ''(9(9: ''(@(@A ''(?(?@ ''(F(FG33D4J4JKK$%56 	%KAx$E#a&M	%r)   c                 J   | j                   s| j                  |       | j                  sy | j                  g}| j                  r|j                  | j                         | j                  | j                  dk(  r|j                  | j                         n| j                  dk(  r|j                  | j                         |j                  | j                         |j                  | j                         |j                  | j                         |j                  | j                         |j                  | j                         n| j                  | j                        t        |      D ]"  \  }}|j!                  |t#        |                $ | j                   r| j$                  j!                  t'        j(                  | j$                  j*                               | j,                  j!                  t'        j(                  | j,                  j*                               y y r[   )r"   _check_load_own_variablesr:   r9   r   r^   r4   r6   ra   r_   r`   rb   rc   rd   re   rf   assignrg   rB   r   zerosr0   rC   )r%   rh   rj   rk   rl   s        r(   load_own_variableszDense.load_own_variables   s     **51zz !LL>==##DII.!!-%%/ ''(9(9:''83 ''(9(9: ''(@(@A ''(9(9: ''(@(@A ''(?(?@ ''(F(FG33D4J4JKK$%56 	+KAxOOE#a&M*	+%%cii0B0B0H0H&IJ%%cii0B0B0H0H&IJ r)   c                 V   t         |          }| j                  t        j                  | j
                        | j                  t        j                  | j                        t        j                  | j                        t        j                  | j                        t        j                  | j                        t        j                  | j                        t        j                  | j                        d	}| j                   r| j                   |d<   | j"                  |d<   i ||S )N)	r   r   r   r   r   r   r   r   r   r    r!   )r   
get_configr   r   	serializer   r   r   r   r   r	   r   r   r   r   r   r    r!   )r%   base_configconfigr'   s      r(   rt   zDense.get_config  s    g(*ZZ%//@"."8"8''# !- 6 6t7L7L M"."8"8''# !- 6 6t7L7L M!,!6!6t7M7M!N*44T5I5IJ
 >>"&..F;#'??F< (+(((r)   c                 
   | j                   | j                  z   }t        |j                               t        |      k7  rt        |      dk(  rY| j                  sMt        d| j                   dt        |j                                d| j                   d| j                   d	      t        d| j                   dt        |       dt        |j                                d	|D cg c]  }|j                   c}       y c c}w )
Nr   zLayer 'zY' was never built and thus it doesn't have any variables. However the weights file lists z variables for this layer.
In most cases, this error indicates that either:

1. The layer is owned by a parent layer that implements a `build()` method, but calling the parent's `build()` method did NOT create the state of the child layer 'z'. A `build()` method must create ALL state for the layer, including the state of any children layers.

2. You need to implement the `def build_from_config(self, config)` method on layer 'a-  ', to specify how to rebuild it during loading. In this case, you might also want to implement the method that generates the build config at saving time, `def get_build_config(self)`. The method `build_from_config()` is meant to create the state of the layer (i.e. its variables) upon deserialization.z' expected z variables, but received z% variables during loading. Expected: )_trainable_variables_non_trainable_variableslenkeysr:   rR   r/   )r%   rh   all_varsvs       r(   ro   zDense._check_load_own_variables  s    ,,t/L/LLuzz|H-8}!$** dii[ )669%**,6G5H I( )-		{ 3!
 "& ,NN . $))KH ? uzz|$% &.67aff78: 3 .: 8s   #D c                     |dk(  r| j                  |       d| _        y |dk(  r| j                          d| _        y | j                  |      )Nr-   r\   T)_int8_build_float8_buildre   _is_quantized)r%   r=   r,   s      r(   r7   zDense.quantized_build;  sT    6>\*
 "	 X  " //55r)   c                     t        j                  d      | _        | j                  d|ddd      | _        | j                  d| j
                  fd	d
      | _        y )Nr+   axisr.   rq   r-   F)r/   r0   r1   dtyperU   ra   ones)r/   r0   r1   rU   )r   AbsMaxQuantizerinputs_quantizerr8   r9   r   ra   )r%   r=   s     r(   r   zDense._int8_buildD  sc     * : : C ' 
 !OO::-	 , 
r)   c                    ddl m} t        | j                  d|j                        }ddddddd	}|fd
ddddd	} | j
                  dddi|| _         | j
                  dddi|| _         | j
                  dddi|| _         | j
                  dddi|| _	         | j
                  dddi|| _
         | j
                  dddi|| _        y )Nr   )QuantizedFloat8DTypePolicyamax_history_lengthr   r   float32TF)r0   r1   r   rU   autocastoverwrite_with_gradientrq   r/   r_   r`   ra   rb   rc   rd   )keras.src.dtype_policiesr   getattrdtype_policydefault_amax_history_lengthr8   r_   r`   ra   rb   rc   rd   )r%   r   r   scale_kwargsamax_history_kwargss        r(   r   zDense._float8_buildT  s+   G &!&BB
 !'+
 *+"'+
 ,DOOPP<P#24?? $
&$
*=$
  ,DOOPP<P#24?? $
&$
*=$
  #2$// #
%#
)5#
 *9 *
,*
0C*
&r)   c                 J    t         j                   fd       } ||t        j                   j                        t        j                   j                              } j
                  rpt        j                  | j                        }t        j                  | j                        }t        j                  | j                   j                  z  |z        } j                   t        j                  | j                        } j                   j                  |      }|S )Nc                    d dfd
}j                  |       \  } }t        j                  |       }t        j                  |j                        }t        j
                  |t        j                  |            }||fS )N)upstreamc                     | |\  } t        j                  t        j                  j                              }t        j                  | t        j
                  |            }|d d fS )N)r   )r   dividecastcompute_dtyperA   	transpose)r   argsfloat_kernelinputs_gradr.   ra   r%   s       r(   grad_fnzFDense._int8_call.<locals>.matmul_with_inputs_gradient.<locals>.grad_fn  s]    #"&KX"zzHHV4+=+=>   "jj3==3NO#T400r)   )r   r   rA   r   r   r   multiply)rG   r.   ra   r   r_   rI   r%   s    ``   r(   matmul_with_inputs_gradientz5Dense._int8_call.<locals>.matmul_with_inputs_gradient  sr    (, 1 1 $(#8#8#@ FL

66*AD../A

1cll<FGAg:r)   )r   custom_gradientconvert_to_tensorr9   ra   r"   rA   rB   rC   rF   r!   r    r4   r   )r%   rG   rH   r   rI   lora_xs   `     r(   
_int8_callzDense._int8_call  s    				 
	$ (!!$,,/!!$"3"34

 ZZ(:(:;FZZ(:(:;FDOOdnn<FGA99 499%A??&"Ar)   c           
           j                   rt        d      t        j                   fd       }t        j                   fd       }t        j                   ||t        j
                   j                        t        j
                   j                               |t        j
                   j                        t        j
                   j                        t        j
                   j                                    } ||t        j
                   j                        t        j
                   j                              } j                  q j                  } j                  j                  dk(  r6t        j                   |d      }t        j                   ||j"                        }t        j$                  ||      } j&                   j'                  |      }|S )Nz.Currently, `_float8_call` doesn't support LoRAc                 n   r}t        j                  t        j                  |d      |t        j                  t        t        j                  d      j                        d            t        j                  | |      nd d t        j                  | |dj                        }d d dfd
}||fS )Nr   r   float8_e4m3fnr   r   	variablesc                     | |\  } | fS rE   r   )r   r   r   new_amax_history	new_scales      r(   gradzEDense._float8_call.<locals>.quantized_dequantize_inputs.<locals>.grad  s    #"&KX,<<<r)   )r   compute_float8_scaler   maxr   float	ml_dtypesfinfocompute_float8_amax_historyquantize_and_dequantizer   )	rG   scaleamax_history
qdq_inputsr   r   r   r%   rH   s	        @@r(   quantized_dequantize_inputsz7Dense._float8_call.<locals>.quantized_dequantize_inputs  s    &;;GGLq1HHiooo>BBCY	 $.#I#IL$  !	#' #;;0B0BJ &*T =
 t##r)   c                 &    dddfd
}| |fS )z;Quantize-dequantize the output gradient but not the output.Nr   c                 V   | |\  } t        j                  t        j                  d      t        j                  t        t        j                  d      j                        d            }t        j                  | dj                        }t        j                  |       }|||fS )Nr   r   float8_e5m2r   )r   r   r   r   r   r   r   r   r   r   r   )	r   r   r   r   qdq_upstreamr   r   r   r%   s	         r(   r   zFDense._float8_call.<locals>.quantized_dequantize_outputs.<locals>.grad  s    #"&KX&;;GGLq1HHioom<@@A9	  *AAe]D4F4F  $.#I#Il$  $Y0@@@r)   r   )outputsr   r   r   r%   s    `` r(   quantized_dequantize_outputsz8Dense._float8_call.<locals>.quantized_dequantize_outputs  s"     &*T A A$ D= r)   r   bfloat16)r"   NotImplementedErrorr   r   rA   r   r_   r`   r9   ra   rb   rc   rd   r4   r   r   r   r   rF   r   )r%   rG   rH   r   r   rI   r4   	bias_bf16s   ` `     r(   _float8_callzDense._float8_call  s   %@  
			$ 
	$4 
			! 
	!. JJ'%%d&7&78%%d&>&>?
 (%%dll3%%d&7&78%%d&>&>?
  )!!$"9"9:!!$"@"@A

 99  99D  ..);HHT:6	xx	4::64 A??&"Ar)   c                 D   |r,t        |       t        ur| j                  | j                        | j                  j
                  }|dk(  r>t        j                  | j                  dd      \  }}t        j                  |d      }| `| j                  ||       |dk(  r6| j                  j                         | j                  j                         | j                  j                  6t        j                   | d| j                  j"                         }|| _        y y )Nr-   r   Tr   to_numpyr   _from_)typer   _not_implemented_errorquantizer9   r0   r   abs_max_quantizer   squeezer7   rp   ra   r   r6   r   r   r/   )r%   r,   
type_checkr=   ri   ra   policys          r(   r   zDense.quantize  s    4:U2--dmm<<||))6>)3)D)D1t*&L, ;;|!<L\406>LL-$$\2 ..6#''4&t7H7H7M7M6N(OPF &D 7r)   c                    | j                   j                  | j                  }| j                  }| j                  rt        j                  ||      }t        j                  || j                  | j                  z  t        j                  | j                  | j                        z        }t        j                  |dd      \  }}t        j                  |d      }||fS | j                   d fS )Nr   Tr   r   )r   r6   r9   ra   r"   r   r   rF   r!   r    rA   rB   rC   r   r   r   r.   )r%   ri   ra   s      r(   r]   z"Dense._get_kernel_with_merged_lora  s    ..:<<L,,L    #zz,E"ww __t~~5jj!3!3T5G5GHI 
 .8-H-H q4.*l  #{{<a@--{{D  r)   )NTglorot_uniformrq   NNNNNNNrE   )N
he_uniformrq   )T)__name__
__module____qualname____doc__r   r>   propertyr.   rJ   rO   r;   rm   rr   rt   ro   r7   r   r   r   r   r   r]   __classcell__)r'   s   @r(   r   r      s    :~ + !%@-: 	 	# "&IP%2K:), H"
 )
V!FYv',!r)   r   )r   	keras.srcr   r   r   r   r   r   r	   keras.src.api_exportr
   keras.src.layers.input_specr   keras.src.layers.layerr   r   r   r)   r(   <module>r      sJ     ! ! $ "    " - 1 ( "#U!E U! $U!r)   