Skip to content

Add tutorials, examples, and guides for the Transformers Flavor#10360

Merged
BenWilson2 merged 9 commits intomlflow:masterfrom
BenWilson2:transformers-quickstart
Nov 30, 2023
Merged

Add tutorials, examples, and guides for the Transformers Flavor#10360
BenWilson2 merged 9 commits intomlflow:masterfrom
BenWilson2:transformers-quickstart

Conversation

@BenWilson2
Copy link
Member

@BenWilson2 BenWilson2 commented Nov 11, 2023

🛠 DevTools 🛠

Open in GitHub Codespaces

Install mlflow from this PR

pip install git+https://github.com/mlflow/mlflow.git@refs/pull/10360/merge

Checkout with GitHub CLI

gh pr checkout 10360

Related Issues/PRs

#xxx

What changes are proposed in this pull request?

Refactor the content in models.rst to move the transformers information to an "in-depth" guide.
Migrate the existing examples to notebook-based tutorials
Provide an overview landing page for the flavor that explains the flavor at a high level

How is this PR tested?

  • Existing unit/integration tests
  • New unit/integration tests
  • Manual tests

Does this PR require documentation update?

  • No. You can skip the rest of this section.
  • Yes. I've updated:
    • Examples
    • API references
    • Instructions

Release Notes

Is this a user-facing change?

  • No. You can skip the rest of this section.
  • Yes. Give a description of this change to be included in the release notes for MLflow users.

Overhaul the transformers flavor docs and add use-case tutorials

What component(s), interfaces, languages, and integrations does this PR affect?

Components

  • area/artifacts: Artifact stores and artifact logging
  • area/build: Build and test infrastructure for MLflow
  • area/docs: MLflow documentation pages
  • area/examples: Example code
  • area/gateway: AI Gateway service, Gateway client APIs, third-party Gateway integrations
  • area/model-registry: Model Registry service, APIs, and the fluent client calls for Model Registry
  • area/models: MLmodel format, model serialization/deserialization, flavors
  • area/recipes: Recipes, Recipe APIs, Recipe configs, Recipe Templates
  • area/projects: MLproject format, project running backends
  • area/scoring: MLflow Model server, model deployment tools, Spark UDFs
  • area/server-infra: MLflow Tracking server backend
  • area/tracking: Tracking Service, tracking client APIs, autologging

Interface

  • area/uiux: Front-end, user experience, plotting, JavaScript, JavaScript dev server
  • area/docker: Docker use across MLflow's components, such as MLflow Projects and MLflow Models
  • area/sqlalchemy: Use of SQLAlchemy in the Tracking Service or Model Registry
  • area/windows: Windows support

Language

  • language/r: R APIs and clients
  • language/java: Java APIs and clients
  • language/new: Proposals for new client languages

Integrations

  • integrations/azure: Azure and Azure ML integrations
  • integrations/sagemaker: SageMaker integrations
  • integrations/databricks: Databricks integrations

How should the PR be classified in the release notes? Choose one:

  • rn/none - No description will be included. The PR will be mentioned only by the PR number in the "Small Bugfixes and Documentation Updates" section
  • rn/breaking-change - The PR will be mentioned in the "Breaking Changes" section
  • rn/feature - A new user-facing feature worth mentioning in the release notes
  • rn/bug-fix - A user-facing bug fix worth mentioning in the release notes
  • rn/documentation - A user-facing documentation change worth mentioning in the release notes

@github-actions github-actions bot added area/docs Documentation issues rn/documentation Mention under Documentation Changes in Changelogs. labels Nov 11, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Nov 11, 2023

Documentation preview for 5575c89 will be available here when this CircleCI job completes successfully.

More info

@harupy
Copy link
Member

harupy commented Nov 17, 2023

This font is a bit difficult to read (for me). Can we use a different font?

image

"\n",
"generation_pipeline = transformers.pipeline(\n",
" task=task,\n",
" model=\"declare-lab/flan-alpaca-large\",\n",
Copy link
Member

Choose a reason for hiding this comment

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

can we use a smaller model?

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately, the smaller version of the flan-alpaca model is only suitable for CI testing. The results that are returned from the 2 smaller models are so bad that it makes the tutorial look like it's broken. There are 4 larger models than this one which provide dramatically improved results, but they're so large that the runtime on CPU for executing the tutorial is measured in tens of minutes. This version of the model is a fairly good compromise for size vs. ability

Comment on lines +371 to +446
"cell_type": "markdown",
"metadata": {},
"source": [
"Formatting Predictions for Tutorial Readability\n",
"Please note that the following function, `format_predictions`, is designed specifically for enhancing the readability of model predictions within this Jupyter Notebook environment. It **is not a standard component** of the model's inference pipeline and is **not necessary for the actual usage** of the model in production or other applications.\n",
"\n",
"The function takes a list of prediction strings and formats each one by:\n",
"\n",
"- Splitting the prediction text into individual sentences.\n",
"- Ensuring that each sentence is properly stripped of leading/trailing whitespace and ends with a period.\n",
"- Joining the sentences back together with newline characters for clear visual separation.\n",
"\n",
"This formatting is particularly useful in a notebook setting where we want to present the output in a more readable and presentable manner for demonstration purposes."
]
},
Copy link
Member

@harupy harupy Nov 17, 2023

Choose a reason for hiding this comment

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

Do we need this explanation? It seems like what format_predictions does is simple string manipulation. I don't think we need cover details for that. IMO, just stating "Format the output nicely" should be fine.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call. Removed everything but the first 2 sentences (and shortened them).

"\n",
"Welcome to our first tutorial on leveraging the power of **Transformers** with **MLflow**. This tutorial is designed for beginners who are just getting started with machine learning workflows and model management. We will walk you through a simple example that demonstrates the integration of the Transformers library with MLflow, a platform that simplifies the machine learning lifecycle.\n",
"\n",
"### What are Transformers?\n",
Copy link
Member

Choose a reason for hiding this comment

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

do we need to explain what transformers are?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm making no assumptions about users knowledge with any introduction notebook flavor. That being said, that's the only place such a high-level explanation should happen within a collection of tutorials (I made sure that the only "what is this thing that we're learning about in this tutorial" for the more advanced tutorials in transformers don't have a 'what is transformers' entry). The other tutorials focus on things like "What is Whisper?" in this spot.

"cell_type": "markdown",
"metadata": {},
"source": [
"## Setting the tracking server\n",
Copy link
Member

Choose a reason for hiding this comment

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

Can we remove this section? For demonstrating purposes, I think we can just use the default local file store.

Copy link
Member Author

Choose a reason for hiding this comment

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

We have a follow-on PR to replace this cell in all notebooks with:

  1. Explain how to connect to hosted MLflow (Databricks CE)
  2. Explain how to connect to self-hosted tracking server
  3. Explain how to use local mode if you don't want to start a tracking server.

I'll be updating all notebook cells with the same descriptive cell to maintain consistency next sprint.

Copy link
Member

@harupy harupy Nov 21, 2023

Choose a reason for hiding this comment

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

Can we explain these in a different section because they are unrelated to transformers?

"\n",
"In this tutorial, we delve into the world of language translation by leveraging the power of [Transformers](https://huggingface.co/docs/transformers/model_doc/whisper) and MLflow. This guide is crafted for practitioners with a grasp of machine learning concepts who seek to streamline their translation model workflows. We will showcase the use of MLflow to log, manage, and serve a cutting-edge translation model - the `google/flan-t5-base` from the [🤗 Hugging Face](https://huggingface.co/) library.\n",
"\n",
"### What is `flan-t5-base`?\n",
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to explain this? Can you just provide a link to the huggingface hub?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea. With an arbitrary model like this, the external link is better. I'll remove this section and insert a link

Copy link
Collaborator

@B-Step62 B-Step62 left a comment

Choose a reason for hiding this comment

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

Thanks a lot for really ellaborate work for the refactoring and tutorials! I love the contents and detailed navigation, just added some personal suggestions to make it visually consice:)

<article class="simple-grid">
<div class="simple-card">
<a href="transformers/index.html">
<div class="header">
Copy link
Collaborator

@B-Step62 B-Step62 Nov 20, 2023

Choose a reason for hiding this comment

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

Screenshot 2023-11-20 at 10 11 31

super-nit: On my browser, the card is displayed w/ wide space between header and description. Is it intended?

Copy link
Member Author

@BenWilson2 BenWilson2 Nov 21, 2023

Choose a reason for hiding this comment

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

That was a design request from UX to ensure that all cards have their <p> content start at the same height with an offset from the border of the <h2> at the top.
I think we'll be refining the CSS for this, though. (In a later PR :) )

- **Framework Interoperability**: Flexibility to train models in one framework and deploy in another, supporting PyTorch, TensorFlow, and JAX, with additional export options for ONNX and TorchScript.
- **Community**: A highly active and engaged community for collaboration and support through forums and the Hugging Face Hub.

MLflow's Transformers flavor is designed to harness the power of the Transformers library within the MLflow ecosystem, providing tools for:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Reading down from here might be a bit verbose, as (1) this bullet points (2) "Features" section (3) intro of "MLflow Flavor for Transformers" below, appears to repeat similar things. Can we consolidate these into one?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call. Dramatically simplifed these sections and condensed the information for a faster read.

^^^^^^^^^^^^^^^^^^^^

Once a model is trained, it needs to be deployed for inference. MLflow's integration with Transformers simplifies this by providing
functions such as :py:func:`mlflow.transformers.load_model` and :py:func:`mlflow.pyfunc.load_model`, which allow for easy model serving.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you think it's nice to have a link to specific section in the detailed guide, or/and a list of steps for serving saved transformer model with code snippet? At this point some ppl may wonder "it says easy but how to do that?"

Copy link
Member Author

Choose a reason for hiding this comment

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

good idea! Added one for loading as a pyfunc (to the guide) and one for inference configurations for scalable serving.

</article>
</section>

Download the Tutorial Notebooks to try them locally
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Can we move these links to each notebook? I doublt ppl use these buttons w/o opening each tutorial.

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately, we can't render these or properly trigger a local notebook download from within the Jupyter notebook render. nbsphinx (the plugin for sphinx that renders the .ipynb as .html) does not allow for custom JS or CSS to propagate to the rendered html (I tried). In order for us to actually have a notebook download to local on all browsers, we have to specify some custom JS to override the settings that the server (s3 website hosting) specifies for how to handle specific file types (otherwise, it will, by browser default, render the raw text of the notebook content directly in the browser).
We might be able to come up with something different with a change to a different platform for html generation, but for now, we have to have some external to the notebook rendering mechanism that generates a link / button to download the files.

To see an example of how to fine-tune a model and log the results in MLflow in an end-to-end example of creating a spam classifier pipeline,
see the `fine-tuning tutorial <tutorials/fine-tuning/transformers-fine-tuning.html>`_.

For a frame of reference of what the experience is within MLflow for fine-tuning a transformers foundation model, the image below shows the
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you clarify why we need two MLflow Runs for a single fine-tuning task? If I understand correctly, the green one is produced by # Log the pipeline part of the tutorial, but I'm not fully sure why we have to start a separate run for it, instead of within the training run which I think is more intuitive...

If having two runs is our recommended practice, we can show training run alone here and move the other run to deployment section and explain the reason behind that, cuz it's not directly relevant to experiment tracking, rather more productionization purpose. WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not really a recommended task, per se. The original intention was to reduce the complexity. Splitting the runs up like this is fairly confusing, though. I unified them and updated the screen capture visualization as well :) Good idea

"- **Reproducibility**: Record everything needed to reproduce a model prediction, from data preprocessing to model parameters.\n",
"- **Deployment**: Streamline the process of deploying your Transformers models to production.\n",
"\n",
"### What Will You Learn?\n",
Copy link
Collaborator

@B-Step62 B-Step62 Nov 20, 2023

Choose a reason for hiding this comment

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

nit: I love this kind of "objective" section, but would be even nicer if it's shown on top of the page, because I can quickly decide whether or not the tutorial is for me or not.

Copy link
Member Author

Choose a reason for hiding this comment

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

Great call out. Updated all of the notebooks.

"- For the purposes of generating a signature later on, as well as having a visual indicator of the expected input data to our model when loading as a `pyfunc`, we next set up an input_example that contains sample prompts.\n",
"- Finally, we define parameters that will be used to control the behavior of the model during inference, such as the maximum length of the generated text and whether to sample multiple times.\n",
"\n",
"### Understanding Pipelines\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about collapse those introductory sections? I think we should have these description for Pipelines, Signatures, etc, for ppl who directly jumped into the tutorial, but it takes larger space than the main task.

<h1>Understanding Pipelines</h1>
<span>Pipelines are a high-level abstraction provided by the Transformers library that simplifies the process of using models for inference. They encapsulate the complexity of the underlying code, offering a straightforward API for a variety of tasks, such as text classification, question answering, and in our case, text generation. </span><br/><br/>

<details>
<summary>
    <b>Expand to learn about Transformer Pipelines.</b>
</summary>
<h2>The pipeline() function</h2>
<span>The pipeline() function is a versatile tool that can be used to create a pipeline for any supported task. When you specify a task, the function returns a pipeline object tailored for that task, constructing the required calls to sub-components (a tokenizer, encoder, generative model, etc.) in the order needed to fulfill the needs of the specified task. This abstraction dramatically simplifies the code required to use these models and their respective components.</span>
</details>
Screenshot 2023-11-20 at 11 36 38

Copy link
Member Author

Choose a reason for hiding this comment

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

This is an AWESOME IDEA! thank you so much for this. Updating the tutorials to make the notebooks cleaner :)

@@ -0,0 +1,1096 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems the notebook layout becomes a bit noisy on browser, due to random spacing makes it hard to visually capture the section structure. Is there any easy way we can clean this up?

Screenshot 2023-11-20 at 11 38 30

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's the styling of our template with how it formats the different elements. I'm going to only use h2 at the top of the notebook and increment the header sections to try and make it more visually appealing

Comment on lines +183 to +170
.. toctree::
:maxdepth: 2
:hidden:

tutorials/text-generation/text-generation.ipynb
tutorials/audio-transcription/whisper.ipynb
tutorials/translation/component-translation.ipynb
tutorials/conversational/conversational-model.ipynb
tutorials/fine-tuning/transformers-fine-tuning.ipynb
Copy link
Member

@harupy harupy Nov 20, 2023

Choose a reason for hiding this comment

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

Any notebooks that can be reviewed in separate PRs to make it easier to review them?

Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
@BenWilson2 BenWilson2 force-pushed the transformers-quickstart branch from 41b6167 to ef2d45f Compare November 22, 2023 16:24
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
" <ul>\n",
" <li><strong>Import</strong>: We import the necessary libraries: transformers for building our NLP model and mlflow for model tracking and management.</li>\n",
" <li><strong>Task Definition</strong>: We then define the task for our pipeline, which in this case is <code>text2text-generation`</code> This task involves generating new text based on the input text.</li>\n",
" <li><strong>Pipeline Declaration</strong>: Next, we create a generation_pipeline using the <code>pipeline</code> function from the Transformers library. This pipeline is configured to use the <code>declare-lab/flan-alpaca-base model</code>, which is a pre-trained model suitable for text generation.</li>\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
" <li><strong>Pipeline Declaration</strong>: Next, we create a generation_pipeline using the <code>pipeline</code> function from the Transformers library. This pipeline is configured to use the <code>declare-lab/flan-alpaca-base model</code>, which is a pre-trained model suitable for text generation.</li>\n",
" <li><strong>Pipeline Declaration</strong>: Next, we create a generation_pipeline using the <code>pipeline</code> function from the Transformers library. This pipeline is configured to use the <code>declare-lab/flan-alpaca-large model</code>, which is a pre-trained model suitable for text generation.</li>\n",

Copy link
Member Author

Choose a reason for hiding this comment

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

ah good catch!

Copy link
Collaborator

@B-Step62 B-Step62 left a comment

Choose a reason for hiding this comment

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

LGTM!
QQ: Can we re-run notebooks and deploy the changes after MLflow 2.9.0 release? Not a blocker, but those annoying pydantic warnings should be gone in the new version.

" input_example,\n",
" mlflow.transformers.generate_signature_output(generation_pipeline, input_example),\n",
" parameters,\n",
")"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
")"
")\n"
"signature"

" \"Please tell me a joke about hiking.\",\n",
" ],\n",
" params={\"temperature\": 0.7},\n",
")"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
")"
")\n"
"\n"
"# Format each prediction for notebook readability\n",
"formatted_predictions = format_predictions(predictions)\n",
"\n",
"for i, formatted_text in enumerate(formatted_predictions):\n",
" print(f\"Response to prompt {i+1}:\\n{formatted_text}\\n\")"

we explained formatting in previous cell already, so I think it's ok to just show it here (like we do in the advanced tutorials)

Copy link
Member Author

Choose a reason for hiding this comment

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

great point. Removed that extra markdown cell and concatenated the code cells :)

"name": "stderr",
"output_type": "stream",
"text": [
"huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Can we remove this warning? It's shown many times across the notebooks.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep! I have added warnings silencing for all UserWarnings (pydantic, setuptools) and added the fix for the tokenizer parallelization warning.

Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
@BenWilson2
Copy link
Member Author

LGTM! QQ: Can we re-run notebooks and deploy the changes after MLflow 2.9.0 release? Not a blocker, but those annoying pydantic warnings should be gone in the new version.

The warnings have been removed :)

Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
@BenWilson2 BenWilson2 merged commit 54c546a into mlflow:master Nov 30, 2023
@BenWilson2 BenWilson2 deleted the transformers-quickstart branch November 30, 2023 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/docs Documentation issues rn/documentation Mention under Documentation Changes in Changelogs.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants