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

Track losses with tensorboard #11568

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Conversation

SwamiKannan
Copy link

@SwamiKannan SwamiKannan commented Mar 2, 2024

Description

This PR allows us to track progress of the loss for each step and for each epoch with Tensorboard. It is specifically written to minimize changes to the existing repo. Hence, if the SentenceTransformer repo finally updates with a reporting framework, these changes can be reverted with just two lines and the deletion of one file.

Added screenshot of tensorboard report running with this script

Type of Change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Added new notebook (that tests end-to-end)
  • I stared at the code and made sure it makes sense

Suggested Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added Google Colab support for the newly added notebooks.
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I ran make format; make lint to appease the lint gods

Added screenshot of my test run

tensorboard

This .py file has a class "TBSentenceTransformer" that is inherited from SentenceTransformer. This class is to be used instead of SentenceTransformer if we want to log our training progress using TensorBoard. 
I have been waiting for the SentenceTransformer team to integrate tensorboard into their repo but it seems like they are taking some time doing it since it is ground up. This is an alternative to that. Code has been written to minimize the impact on any of the repos using SentenceTransformers i.e. if at any time, SentenceTransformers do come up with a reporting framework, it will be easy to revert to that if required with minimal changes to the code
A couple of lines added to the creation of the SentenceTransformersFinetuneEngine class:
1. Add a parameter log_path that specifies where the tensorboard logs should be stored
2. Update the constructor: self.model = = TBSentenceTransformer() if the log_path is specified else self.model = SentenceTransformer()
Updated class inherited from SentenceTransformers to include tensorboard reporting
Contains an inherited class from SentenceTransformer called TBSentenceTransformer that allows llamaindex's SentenceTransformersFinetuneEngine to track and record loss while finetuning using tensorboard.
Credit to this PR: UKPLab/sentence-transformers#1532 , by Ahmed Magdy https://github.com/Mogady
Updated SentenceTransformersFinetuneEngine's self.model to accept a TBSentenceTransformer object if a log_path is specified or revert to accepting a SentenceTransformer object. Initial commits had some mistakes..
@dosubot dosubot bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Mar 2, 2024
from llama_index.finetuning.types import BaseEmbeddingFinetuneEngine


class SentenceTransformersFinetuneEngine(BaseEmbeddingFinetuneEngine):
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the change here for this file?

Copy link
Author

@SwamiKannan SwamiKannan Mar 3, 2024

Choose a reason for hiding this comment

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

So if a log_path is provided,
self.model will be a TBSentenceTransformer object
else if no log_path is provided,
self.model will be a standard SentenceTransformer object

Lines 40-43 in sentence_transformer.py

if log_path:
     self.model = TBSentenceTransformer(model_id, writer_path = log_path)
else:
    self.model = SentenceTransformer(model_id)

Copy link
Collaborator

Choose a reason for hiding this comment

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

did you mean to commit these two files in the root?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, there were two files to be committed.
These were to be in the same folder as sentence_transformer.py i.e. in finetuning\embeddings\

Copy link
Author

Choose a reason for hiding this comment

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

Do correct me if I have missed something?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think these files belong in the root of the entire repo 👀

Copy link
Author

Choose a reason for hiding this comment

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

oh god.... i'm so sorry... that was a mistake. Resolved on my repo so I am assuming it is replicated in this PR?
Apologies. This is the first time I am committing to such a large and useful repo so I may be a little lost

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Mar 3, 2024
Copy link
Author

@SwamiKannan SwamiKannan left a comment

Choose a reason for hiding this comment

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

Deleted an extraneous "from" syntax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants