Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TensorBoard callback no longer adds hparams #23999

Merged
merged 1 commit into from
Jun 5, 2023

Conversation

bri25yu
Copy link
Contributor

@bri25yu bri25yu commented Jun 4, 2023

What does this PR do?

The TensorBoardCallback.on_train_begin function calls add_hparams with an empty metric_dict parameter, meaning that the only information that is logged is from args.sanitized_dict(). This is duplicated information from the previous self.tb_writer.add_text("args", args.to_json_string()). As a result, the add_hparams call is unnecessary and this PR removes it.

Adjacently fixes #21821.

I'm aware of the following TensorBoard related documentation:

None of these docs need to be updated in this PR.

A sanity check test:

"""
Minimal replication of https://github.com/huggingface/transformers/issues/21821
"""
from os import listdir
from shutil import rmtree

from transformers import TrainingArguments
from transformers.integrations import TensorBoardCallback


output_dir = "output_dir"

args = TrainingArguments(output_dir=output_dir, logging_dir=output_dir)

def has_extra_file():
    return len(listdir(output_dir)) > 1


class DummyControl:
    should_training_stop = None


class DummyState:
    is_world_process_zero = True
    is_hyper_param_search = False


class NoHParamsTensorBoardCallback(TensorBoardCallback):
    # This is a copy of `TensorBoardCallback.on_train_begin` unless specified otherwise
    def on_train_begin(self, args, state, control, **kwargs):
        if not state.is_world_process_zero:
            return

        log_dir = None

        if state.is_hyper_param_search:
            trial_name = state.trial_name
            if trial_name is not None:
                log_dir = os.path.join(args.logging_dir, trial_name)

        if self.tb_writer is None:
            self._init_summary_writer(args, log_dir)

        if self.tb_writer is not None:
            self.tb_writer.add_text("args", args.to_json_string())
            if "model" in kwargs:
                model = kwargs["model"]
                if hasattr(model, "config") and model.config is not None:
                    model_config_json = model.config.to_json_string()
                    self.tb_writer.add_text("model_config", model_config_json)

            ###########################
            # START no hparams call
            ###########################

            # Original code:
            # # Version of TensorBoard coming from tensorboardX does not have this method.
            # if hasattr(self.tb_writer, "add_hparams"):
            #     self.tb_writer.add_hparams(args.to_sanitized_dict(), metric_dict={})

            ###########################
            # END no hparams call
            ###########################


rmtree(output_dir, ignore_errors=True)
TensorBoardCallback().on_train_begin(args, DummyState(), DummyControl())
print(f"With the call to `add_hparams`, has extra file is {has_extra_file()}")

rmtree(output_dir, ignore_errors=True)
NoHParamsTensorBoardCallback().on_train_begin(args, DummyState(), DummyControl())
print(f"WithOUT the call to `add_hparams`, has extra file is {has_extra_file()}")

rmtree(output_dir, ignore_errors=True)  # Cleanup

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed.

Copy link
Collaborator

@sgugger sgugger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for cleaning this up!

@sgugger sgugger merged commit 2872f96 into huggingface:main Jun 5, 2023
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

@bri25yu bri25yu deleted the tb-no-longer-adds-hparams branch June 19, 2023 20:32
novice03 pushed a commit to novice03/transformers that referenced this pull request Jun 23, 2023
tensorboard callback no longer adds hparams
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Two tfevent files are being generated for each run of trainer
3 participants