
    6bi,                         S r SSKJs  Jr  SSKJr  SSKJr  SSK	J
r  SSKJr  SSKJr  \R                   r\R"                  r\R$                  r\R&                  r\" SS	5       " S
 S\R(                  5      5       rg)z"Keras hashing preprocessing layer.    N)backend)
base_layer)preprocessing_utils)layer_utils)keras_exportzkeras.layers.Hashingz/keras.layers.experimental.preprocessing.Hashingc                   \   ^  \ rS rSrSr    S
U 4S jjrS rS rS rS r	U 4S jr
S	rU =r$ )Hashing"   a.  A preprocessing layer which hashes and bins categorical features.

This layer transforms categorical inputs to hashed output. It element-wise
converts a ints or strings to ints in a fixed range. The stable hash
function uses `tensorflow::ops::Fingerprint` to produce the same output
consistently across all platforms.

This layer uses [FarmHash64](https://github.com/google/farmhash) by default,
which provides a consistent hashed output across different platforms and is
stable across invocations, regardless of device and context, by mixing the
input bits thoroughly.

If you want to obfuscate the hashed output, you can also pass a random
`salt` argument in the constructor. In that case, the layer will use the
[SipHash64](https://github.com/google/highwayhash) hash function, with
the `salt` value serving as additional input to the hash function.

For an overview and full list of preprocessing layers, see the preprocessing
[guide](https://www.tensorflow.org/guide/tf_keras/preprocessing_layers).

**Example (FarmHash64)**

>>> layer = tf.keras.layers.Hashing(num_bins=3)
>>> inp = [['A'], ['B'], ['C'], ['D'], ['E']]
>>> layer(inp)
<tf.Tensor: shape=(5, 1), dtype=int64, numpy=
  array([[1],
         [0],
         [1],
         [1],
         [2]])>

**Example (FarmHash64) with a mask value**

>>> layer = tf.keras.layers.Hashing(num_bins=3, mask_value='')
>>> inp = [['A'], ['B'], [''], ['C'], ['D']]
>>> layer(inp)
<tf.Tensor: shape=(5, 1), dtype=int64, numpy=
  array([[1],
         [1],
         [0],
         [2],
         [2]])>

**Example (SipHash64)**

>>> layer = tf.keras.layers.Hashing(num_bins=3, salt=[133, 137])
>>> inp = [['A'], ['B'], ['C'], ['D'], ['E']]
>>> layer(inp)
<tf.Tensor: shape=(5, 1), dtype=int64, numpy=
  array([[1],
         [2],
         [1],
         [0],
         [2]])>

**Example (Siphash64 with a single integer, same as `salt=[133, 133]`)**

>>> layer = tf.keras.layers.Hashing(num_bins=3, salt=133)
>>> inp = [['A'], ['B'], ['C'], ['D'], ['E']]
>>> layer(inp)
<tf.Tensor: shape=(5, 1), dtype=int64, numpy=
  array([[0],
         [0],
         [2],
         [1],
         [0]])>

Args:
  num_bins: Number of hash bins. Note that this includes the `mask_value`
    bin, so the effective number of bins is `(num_bins - 1)` if `mask_value`
    is set.
  mask_value: A value that represents masked inputs, which are mapped to
    index 0. `None` means no mask term will be added and the
    hashing will start at index 0. Defaults to `None`.
  salt: A single unsigned integer or None.
    If passed, the hash function used will be SipHash64, with these values
    used as an additional input (known as a "salt" in cryptography).
    These should be non-zero. If `None`, uses the FarmHash64 hash function.
    It also supports tuple/list of 2 unsigned integer numbers, see
    reference paper for details. Defaults to `None`.
  output_mode: Specification for the output of the layer. Values can bes
    `"int"`, `"one_hot"`, `"multi_hot"`, or
    `"count"` configuring the layer as follows:
      - `"int"`: Return the integer bin indices directly.
      - `"one_hot"`: Encodes each individual element in the input into an
        array the same size as `num_bins`, containing a 1 at the input's bin
        index. If the last dimension is size 1, will encode on that
        dimension.  If the last dimension is not size 1, will append a new
        dimension for the encoded output.
      - `"multi_hot"`: Encodes each sample in the input into a single array
        the same size as `num_bins`, containing a 1 for each bin index
        index present in the sample. Treats the last dimension as the sample
        dimension, if input shape is `(..., sample_length)`, output shape
        will be `(..., num_tokens)`.
      - `"count"`: As `"multi_hot"`, but the int array contains a count of
        the number of times the bin index appeared in the sample.
    Defaults to `"int"`.
  sparse: Boolean. Only applicable to `"one_hot"`, `"multi_hot"`,
    and `"count"` output modes. If True, returns a `SparseTensor` instead of
    a dense `Tensor`. Defaults to `False`.
  **kwargs: Keyword arguments to construct a layer.

Input shape:
  A single or list of string, int32 or int64 `Tensor`,
  `SparseTensor` or `RaggedTensor` of shape `(batch_size, ...,)`

Output shape:
  An int64 `Tensor`, `SparseTensor` or `RaggedTensor` of shape
  `(batch_size, ...)`. If any input is `RaggedTensor` then output is
  `RaggedTensor`, otherwise if any input is `SparseTensor` then output is
  `SparseTensor`, otherwise the output is `Tensor`.

Reference:
  - [SipHash with salt](https://www.131002.net/siphash/siphash.pdf)

c                   > Ub  US::  a  [        SU S35      eSU;  d  US   c3  U[        :X  a  [        R                  O[        R
                  " 5       US'   OAUS:X  a;  [        R                  " US   5      R                  (       d  [        R                  US'   [        TU ]$  " S0 UD6  U[        :X  aB  [        R                  " U R                  5      R                  (       d  US   n[        SU 35      e[        R                  " U[        [        [        [        4U R                   R"                  SS9  U(       a  U[        :X  a  [        S	U S
U 35      eXl        X l        Ub  SOSU l        X@l        XPl        S U l        Ub^  [1        U[2        [4        45      (       a  [7        U5      S:X  a  X0l        g [1        U[8        5      (       a	  X3/U l        g [        SU S35      eg )Nr   zYThe `num_bins` for `Hashing` cannot be `None` or non-positive values. Received: num_bins=.dtypeintzMWhen `output_mode="int"`, `dtype` should be an integer type. Received: dtype=output_mode)allowable_strings
layer_namearg_namezi`sparse` may only be true if `output_mode` is `"one_hot"`, `"multi_hot"`, or `"count"`. Received: sparse=z and output_mode=TF   znThe `salt` argument for `Hashing` can only be a tuple of size 2 integers, or a single integer. Received: salt= )
ValueErrorINTtfint64r   floatxas_dtype
is_integersuper__init__compute_dtyper   validate_string_argONE_HOT	MULTI_HOTCOUNT	__class____name__num_bins
mask_valuestrong_hashr   sparsesalt
isinstancetuplelistlenr   )	selfr%   r&   r)   r   r(   kwargsinput_dtyper#   s	           c/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/layers/preprocessing/hashing.pyr   Hashing.__init__   s    x1};;C*AG  & F7O$;'3.GNN4D 7O 5 VG_)E)P)P !hhF7O"6" 3KK 2 23>> /K))47  	''"GY>~~.."		
 kS($$*8 ,*m-  !$#'#34&	$..3t9> 	D#&&!L	 &&*V1.      c                    [         R                  " U5      n[        U[        R                  5      (       aD  [        R                  " UR
                  U R                  UR                  5      UR                  S9nOU R                  U5      n[         R                  " UU R                  U R                  U R                  U R                  S9$ )N)indicesvaluesdense_shape)r   depthr(   r   )utilsensure_tensorr*   r   SparseTensorr5   _hash_values_to_binsr6   r7   encode_categorical_inputsr   r%   r(   r   )r.   inputsr5   s      r1   callHashing.call   s    $$V,fboo..oo00?"..G //7G..((--;;$$
 	
r3   c                 Z   U R                   nSnU R                  b+  US:  a%  US-  n[        R                  " XR                  5      nUR                  R
                  (       a  [        R                  " U5      nU R                  (       a*  [        R                  R                  XSU R                  S9nO[        R                  R                  XSS9nUbW  [        R                  " U[        R                  " U5      5      n[        R                  " U[        R                  " U5      U5      nU$ )z6Converts a non-sparse tensor of values to bin indices.N   hash)namekey)rD   )r%   r&   r   equalr   r   	as_stringr'   stringsto_hash_bucket_strongr)   to_hash_bucket_fastadd	ones_likewhere
zeros_like)r.   r6   	hash_binsmasks       r1   r<   Hashing._hash_values_to_bins   s    MM	??&9q=NI88FOO4D<<""\\&)FZZ55DII 6 F ZZ33 4 F VVFBLL$89FXXdBMM&$96BFr3   c                     U$ )Nr   )r.   input_shapes     r1   compute_output_shapeHashing.compute_output_shape  s    r3   c                     U R                  UR                  5      n[        U[        R                  5      (       a  [        R                  " X R
                  S9$ [        R                  " X R
                  S9$ )N)shaper   )rT   rW   r*   r   SparseTensorSpecr   
TensorSpec)r.   
input_specoutput_shapes      r1   compute_output_signature Hashing.compute_output_signature  s^    001A1ABj""5"566&&"*<*<  ==|;M;MNNr3   c                    > [         TU ]  5       nUR                  U R                  U R                  U R
                  U R                  U R                  S.5        U$ )N)r%   r)   r&   r   r(   )r   
get_configupdater%   r)   r&   r   r(   )r.   configr#   s     r1   r_   Hashing.get_config  sM    #% MM		"oo#//++	
 r3   )r&   r%   r   r)   r(   r'   )NNr   F)r$   
__module____qualname____firstlineno____doc__r   r?   r<   rT   r\   r_   __static_attributes____classcell__)r#   s   @r1   r	   r	   "   s>    tr HT
$2O r3   r	   )rf   tensorflow.compat.v2compatv2r   tf_keras.srcr   tf_keras.src.enginer   !tf_keras.src.layers.preprocessingr   r9   tf_keras.src.utilsr    tensorflow.python.util.tf_exportr   r   r!   r    r"   Layerr	   r   r3   r1   <module>rr      sv    ) " !   * J * :iiOO	
-- MCj CCr3   