<commit_before>import tf_encrypted as tfe
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tf_encrypted.keras.engine.sequential import Sequential as tfe_Sequential
from tf_encrypted.keras.engine.input_layer import InputLayer as tfe_InputLayer



# When instantiating tfe layers, exclude not supported arguments in TFE. 
args_not_supported_by_tfe = ['self', 'kwargs', 
                            'activity_regularizer', 'kernel_regularizer', 
                            'bias_regularizer', 'kernel_constraint', 
                            'bias_constraint','dilation_rate']



class Sequential(Sequential):
    def __init__(self, *args, **kwargs):
        super(Sequential, self).__init__(*args, **kwargs)


    def share(self, prot=None):

        tfe_model = tfe_Sequential()

        # NOTE[Yann]: run with keras until tfe is ready
        with prot:
            for keras_layer in self._layers:
                # Don't need to instantiate 'InputLayer'. Will be automatically
                # created when `mode.predict()`.
                if _get_layer_name(keras_layer) == 'InputLayer':
                    batch_input_shape = keras_layer._batch_input_shape
                    supply_next = True
                    if not any(batch_input_shape):
                        raise NotImplementedError("Please provide batch_size or batch_input_shape "
                                                  "keyword arguments when creating your syft.keras "
                                                  "model")
                    continue
                if supply_next:
                    tfe_layer  = _instantiate_tfe_layer(keras_layer, batch_input_shape)
                    supply_next = False
                else:
                    tfe_layer  = _instantiate_tfe_layer(keras_layer, None)

                tfe_model.add(tfe_layer)

        return tfe_model


def _instantiate_tfe_layer(keras_layer, batch_input_shape):

    # Get dictionary with layer attributes
    keras_layer_attr = keras_layer.__dict__

    keras_layer_name = _get_layer_name(keras_layer)

    # Identify the TFE layer corresponding to the Keras layer
    tfe_layer_cls = getattr(tfe.keras.layers, keras_layer_name)

    # Extract argument list expected by layer __init__
    tfe_arg_list = list(tfe_layer_cls.__dict__['__init__'].__code__.co_varnames)

    # Remove arguments currenlty not supported by TFE layers
    tfe_arg_list = trim_tfe_args(tfe_arg_list, args_not_supported_by_tfe)

    # Load weights from Keras layer into TFE layer
    if 'kernel_initializer' in tfe_arg_list:
        k_initializer = tf.keras.initializers.Constant(keras_layer.get_weights()[0])
        keras_layer_attr['kernel_initializer'] = k_initializer

    # [NOTE] This might break if use_bias=False
    if 'bias_initializer' in tfe_arg_list:
        b_initializer = tf.keras.initializers.Constant(keras_layer.get_weights()[1])
        keras_layer_attr['bias_initializer'] = b_initializer

    # When the keras layer has been instantiated, the activation attribute
    # gets assigned an activation function (e.g. keras.activations.relu())
    # Needs to be converted back to activation name (e.g. `relu`), so it
    # uses TFE activation.
    if 'activation' in tfe_arg_list:
        activation_name = keras_layer_attr['activation'].__name__
        keras_layer_attr['activation'] = activation_name

    tfe_kwargs = {k: keras_layer_attr[k] for k in tfe_arg_list}

    if batch_input_shape is not None:
      tfe_kwargs['batch_input_shape'] = batch_input_shape

    return tfe_layer_cls(**tfe_kwargs)


def _get_layer_name(keras_layer_cls):<commit_msg>handle bias=None<commit_after>import tf_encrypted as tfe
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tf_encrypted.keras.engine.sequential import Sequential as tfe_Sequential
from tf_encrypted.keras.engine.input_layer import InputLayer as tfe_InputLayer



# When instantiating tfe layers, exclude not supported arguments in TFE. 
args_not_supported_by_tfe = ['self', 'kwargs', 
                            'activity_regularizer', 'kernel_regularizer', 
                            'bias_regularizer', 'kernel_constraint', 
                            'bias_constraint','dilation_rate']



class Sequential(Sequential):
    def __init__(self, *args, **kwargs):
        super(Sequential, self).__init__(*args, **kwargs)


    def share(self, prot=None):

        tfe_model = tfe_Sequential()

        # NOTE[Yann]: run with keras until tfe is ready
        with prot:
            for keras_layer in self._layers:
                # Don't need to instantiate 'InputLayer'. Will be automatically
                # created when `mode.predict()`.
                if _get_layer_name(keras_layer) == 'InputLayer':
                    batch_input_shape = keras_layer._batch_input_shape
                    supply_next = True
                    if not any(batch_input_shape):
                        raise NotImplementedError("Please provide batch_size or batch_input_shape "
                                                  "keyword arguments when creating your syft.keras "
                                                  "model")
                    continue
                if supply_next:
                    tfe_layer  = _instantiate_tfe_layer(keras_layer, batch_input_shape)
                    supply_next = False
                else:
                    tfe_layer  = _instantiate_tfe_layer(keras_layer, None)

                tfe_model.add(tfe_layer)

        return tfe_model


def _instantiate_tfe_layer(keras_layer, batch_input_shape):

    # Get dictionary with layer attributes
    keras_layer_attr = keras_layer.__dict__

    keras_layer_name = _get_layer_name(keras_layer)

    # Identify the TFE layer corresponding to the Keras layer
    tfe_layer_cls = getattr(tfe.keras.layers, keras_layer_name)

    # Extract argument list expected by layer __init__
    tfe_arg_list = list(tfe_layer_cls.__dict__['__init__'].__code__.co_varnames)

    # Remove arguments currenlty not supported by TFE layers
    tfe_arg_list = trim_tfe_args(tfe_arg_list, args_not_supported_by_tfe)

    # Load weights from Keras layer into TFE layer
    if 'kernel_initializer' in tfe_arg_list:
        k_initializer = tf.keras.initializers.Constant(keras_layer.get_weights()[0])
        keras_layer_attr['kernel_initializer'] = k_initializer

    # [NOTE] This might break if use_bias=False
    if 'bias_initializer' in tfe_arg_list and keras_layer_attr['use_bias']:
        b_initializer = tf.keras.initializers.Constant(keras_layer.get_weights()[1])
        keras_layer_attr['bias_initializer'] = b_initializer

    # When the keras layer has been instantiated, the activation attribute
    # gets assigned an activation function (e.g. keras.activations.relu())
    # Needs to be converted back to activation name (e.g. `relu`), so it
    # uses TFE activation.
    if 'activation' in tfe_arg_list:
        activation_name = keras_layer_attr['activation'].__name__
        keras_layer_attr['activation'] = activation_name

    tfe_kwargs = {k: keras_layer_attr[k] for k in tfe_arg_list}

    if batch_input_shape is not None:
      tfe_kwargs['batch_input_shape'] = batch_input_shape

    return tfe_layer_cls(**tfe_kwargs)


def _get_layer_name(keras_layer_cls):
{'fraction_non_alphanumeric': 0.06998635743519782, 'fraction_numerical': 0.0005457025920873125, 'mean_word_length': 2.6545363908275172, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6487781', 'n_tokens_mistral': 2208, 'n_tokens_neox': 2105, 'n_words': 504}