# Path: flaxformer/components/dense.py
# def dense(features, name, inputs, kernel_axis_names):
#   if self.weight_params is not None or self.act_params is not None:
#     # TODO: Push the "quantized vs not" decision down into the
#     # AQT library. Currently we make that decision here, because the AQT
#     # library doesn't support DenseGeneral, so there's extra reshapes here
#     # whose performance impact I don't know.
#     aqt_context = aqt_config.QuantContext(
#         update_bounds=False, collect_acts_stats=False)
#     aqt_hparams = aqt_flax_layers.DenseAqt.HParams(
#         weight_prec=self.weight_params.prec,
#         weight_half_shift=self.weight_params.half_shift,
#         quant_act=self.act_params,  # currently supports fixed bounds only.
#         quant_type=aqt.QuantType.aqt,
#         weight_quant_granularity=aqt_config.QuantGranularity.per_channel,
#     )
#     batch, seq_len, channels = inputs.shape
#     inputs = inputs.reshape((batch * seq_len, channels))
#     result = aqt_flax_layers.DenseAqt(
#         features=features,
#         hparams=aqt_hparams,
#         train=enable_dropout,
#         quant_context=aqt_context,
#         paxis_name=None,
#         # No "cross-replica" reduction expressed in the XLA graph at this
#         # stage. Will be imposed later, automatically, by XLA SPMD.
#         use_bias=self.use_bias,
#         kernel_init=self.kernel_init,
#         bias_init=self.bias_init,
#         dtype=self.dtype,
#         kernel_axis_names=kernel_axis_names,
#         name=name,
#     )(inputs, padding_mask=None)
#     return result.reshape((batch, seq_len, features))
#   else:
#     return DenseGeneral(
#         features,
#         use_bias=self.use_bias,
#         dtype=self.dtype,
#         kernel_init=self.kernel_init,
#         bias_init=self.bias_init,
#         kernel_axis_names=kernel_axis_names,
#         name=name)(
#             inputs)
# 
# Path: flaxformer/components/initializers.py
# def sinusoidal(min_scale: float = 1.0,
#                max_scale: float = 10000.0,
#                dtype: DType = jnp.float32) -> Initializer:
#   def init(key: PRNGKey, shape: Shape, dtype: DType = dtype) -> Array:
# def truncated_normal(mean: Union[float, Array] = 0.0,
#                      stddev: Union[float, Array] = 0.05,
#                      dtype: DType = jnp.float32) -> Initializer:
#   def init(key: PRNGKey, shape: Shape, dtype: DType = dtype) -> Array:
# 
# Path: flaxformer/types.py

# 
# Path: flaxformer/types.py

# 
# Path: flaxformer/types.py

# 
# Path: flaxformer/types.py



from typing import Optional
from flax import linen as nn
from flax.linen.initializers import zeros
from flaxformer.components import dense
from flaxformer.components import initializers
from flaxformer.types import Activation
from flaxformer.types import Array
from flaxformer.types import DType
from flaxformer.types import Initializer
import jax.numpy as jnp
    embed: `Embed` module of the BertEncoder for token_ids. We need it to
      extract the word embeddings. See tests on how to access this submodule.
    hidden_size: The output layer size, which is the number of classes.
    vocab_size: The vocabulary size.
    kernel_init: Initializer for the classifier dense layer kernel.
    dropout_rate: Dropout probability used across all the model layers.
    activation: Activation function.
    dtype: The dtype of the computation (default: float32).
  """
  encoder: nn.Module
  hidden_size: int
  vocab_size: int
  kernel_init: Initializer = initializers.truncated_normal(
      stddev=_DEFAULT_INIT_RANGE)
  dropout_rate: float = 0.
  activation: Activation = nn.gelu
  dtype: DType = jnp.float32

  def setup(self):
    self.mlm_hidden_layer = dense.DenseGeneral(
        features=self.hidden_size,
        kernel_init=self.kernel_init,
        dtype=self.dtype,
        use_bias=True,
        name='dense')
    self.layer_norm = nn.LayerNorm(
        epsilon=_DEFAULT_LAYER_NORM, name='layer_norm')
    self.bias = self.param('bias', zeros, (self.vocab_size,))

  def __call__(self, encoded_inputs: Array, *,

# Path: flaxformer/components/dense.py
# def dense(features, name, inputs, kernel_axis_names):
#   if self.weight_params is not None or self.act_params is not None:
#     # TODO: Push the "quantized vs not" decision down into the
#     # AQT library. Currently we make that decision here, because the AQT
#     # library doesn't support DenseGeneral, so there's extra reshapes here
#     # whose performance impact I don't know.
#     aqt_context = aqt_config.QuantContext(
#         update_bounds=False, collect_acts_stats=False)
#     aqt_hparams = aqt_flax_layers.DenseAqt.HParams(
#         weight_prec=self.weight_params.prec,
#         weight_half_shift=self.weight_params.half_shift,
#         quant_act=self.act_params,  # currently supports fixed bounds only.
#         quant_type=aqt.QuantType.aqt,
#         weight_quant_granularity=aqt_config.QuantGranularity.per_channel,
#     )
#     batch, seq_len, channels = inputs.shape
#     inputs = inputs.reshape((batch * seq_len, channels))
#     result = aqt_flax_layers.DenseAqt(
#         features=features,
#         hparams=aqt_hparams,
#         train=enable_dropout,
#         quant_context=aqt_context,
#         paxis_name=None,
#         # No "cross-replica" reduction expressed in the XLA graph at this
#         # stage. Will be imposed later, automatically, by XLA SPMD.
#         use_bias=self.use_bias,
#         kernel_init=self.kernel_init,
#         bias_init=self.bias_init,
#         dtype=self.dtype,
#         kernel_axis_names=kernel_axis_names,
#         name=name,
#     )(inputs, padding_mask=None)
#     return result.reshape((batch, seq_len, features))
#   else:
#     return DenseGeneral(
#         features,
#         use_bias=self.use_bias,
#         dtype=self.dtype,
#         kernel_init=self.kernel_init,
#         bias_init=self.bias_init,
#         kernel_axis_names=kernel_axis_names,
#         name=name)(
#             inputs)
# 
# Path: flaxformer/components/initializers.py
# def sinusoidal(min_scale: float = 1.0,
#                max_scale: float = 10000.0,
#                dtype: DType = jnp.float32) -> Initializer:
#   def init(key: PRNGKey, shape: Shape, dtype: DType = dtype) -> Array:
# def truncated_normal(mean: Union[float, Array] = 0.0,
#                      stddev: Union[float, Array] = 0.05,
#                      dtype: DType = jnp.float32) -> Initializer:
#   def init(key: PRNGKey, shape: Shape, dtype: DType = dtype) -> Array:
# 
# Path: flaxformer/types.py

# 
# Path: flaxformer/types.py

# 
# Path: flaxformer/types.py

# 
# Path: flaxformer/types.py



# Path: flaxformer/architectures/bert/heads.py
from typing import Optional
from flax import linen as nn
from flax.linen.initializers import zeros
from flaxformer.components import dense
from flaxformer.components import initializers
from flaxformer.types import Activation
from flaxformer.types import Array
from flaxformer.types import DType
from flaxformer.types import Initializer
import jax.numpy as jnp

    embed: `Embed` module of the BertEncoder for token_ids. We need it to
      extract the word embeddings. See tests on how to access this submodule.
    hidden_size: The output layer size, which is the number of classes.
    vocab_size: The vocabulary size.
    kernel_init: Initializer for the classifier dense layer kernel.
    dropout_rate: Dropout probability used across all the model layers.
    activation: Activation function.
    dtype: The dtype of the computation (default: float32).
  """
  encoder: nn.Module
  hidden_size: int
  vocab_size: int
  kernel_init: Initializer = initializers.truncated_normal(
      stddev=_DEFAULT_INIT_RANGE)
  dropout_rate: float = 0.
  activation: Activation = nn.gelu
  dtype: DType = jnp.float32

  def setup(self):
    self.mlm_hidden_layer = dense.DenseGeneral(
        features=self.hidden_size,
        kernel_init=self.kernel_init,
        dtype=self.dtype,
        use_bias=True,
        name='dense')
    self.layer_norm = nn.LayerNorm(
        epsilon=_DEFAULT_LAYER_NORM, name='layer_norm')
    self.bias = self.param('bias', zeros, (self.vocab_size,))

  def __call__(self, encoded_inputs: Array, *,

               masked_positions: Optional[Array]) -> Array: