tensorflow/models · error · ValueError

sub_model_export_name is specified as

Error message

sub_model_export_name is specified as %s, but sub_model is None.

What it means

Raised when sub_model_export_name is given but the model returned by model_fn exposes no sub_model attribute, so there is nothing to export under that name. The mismatch is between the export configuration (sub_model_export_name) and the model built by model_fn (missing sub_model).

Solutions

  1. In model_fn, attach the secondary model: model.sub_model = <sub model> before returning it
  2. Omit sub_model_export_name if you only need to export the full model
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at official/legacy/bert/model_training_utils.py:274 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24). Data as JSON: /api/errors/f8389abe25a5c129. Report an issue: GitHub.

Appendix: source

Thrown at official/legacy/bert/model_training_utils.py:274

    raise ValueError(
        '`eval_step` is required when `eval_input_fn ` is not none.')
  if metric_fn and not callable(metric_fn):
    raise ValueError(
        'if `metric_fn` is specified, metric_fn must be a callable.')

  total_training_steps = steps_per_epoch * epochs  # pyrefly: ignore[unsupported-operation]
  train_iterator = _get_input_iterator(train_input_fn, strategy)
  eval_loss_metric = tf_keras.metrics.Mean('training_loss', dtype=tf.float32)

  with distribute_utils.get_strategy_scope(strategy):
    # To correctly place the model weights on accelerators,
    # model and optimizer should be created in scope.
    model, sub_model = model_fn()  # pyrefly: ignore[not-callable]
    if not hasattr(model, 'optimizer'):
      raise ValueError('User should set optimizer attribute to model '
                       'inside `model_fn`.')
    if sub_model_export_name and sub_model is None:
      raise ValueError('sub_model_export_name is specified as %s, but '
                       'sub_model is None.' % sub_model_export_name)

    callback_list = tf_keras.callbacks.CallbackList(
        callbacks=custom_callbacks, model=model)

    optimizer = model.optimizer

    if init_checkpoint:
      logging.info(
          'Checkpoint file %s found and restoring from '
          'initial checkpoint for core model.', init_checkpoint)
      checkpoint = tf.train.Checkpoint(model=sub_model, encoder=sub_model)
      checkpoint.read(init_checkpoint).assert_existing_objects_matched()
      logging.info('Loading from checkpoint file completed')

    train_loss_metric = tf_keras.metrics.Mean('training_loss', dtype=tf.float32)
    eval_metrics = metric_fn() if metric_fn else []
    if not isinstance(eval_metrics, list):

View on GitHub (pinned to e006f5f0d5)