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

Add support for returning classifier scores in transformers output #8512

Merged
merged 3 commits into from
May 24, 2023

Conversation

BenWilson2
Copy link
Member

Related Issues/PRs

#xxx

What changes are proposed in this pull request?

Add support for returning score attributes for label classification in text-based LLM classification pipelines for transformers

How is this patch tested?

  • Existing unit/integration tests
  • New unit/integration tests
  • Manual tests (describe details, including test results, below)

Validation that serving works correctly for ZeroShotClassificationPipelines and TextClassificationPipelines. Existing unit tests and integration tests have been updated to conform to the updated return types.

Does this PR change the documentation?

  • No. You can skip the rest of this section.
  • Yes. Make sure the changed pages / sections render correctly in the documentation preview.

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.

Added return scores for text-based classification pipelines in transformers

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/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/breaking-change - The PR will be mentioned in the "Breaking Changes" section
  • 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/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

Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
{0: "POSITIVE"},
{0: "POSITIVE"},
]
assert len(values.to_dict()) == 2
Copy link
Member Author

Choose a reason for hiding this comment

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

all of these validations are being moved to structural relationships since scores are somewhat non-deterministic depending on the model used.

@github-actions github-actions bot added area/models MLmodel format, model serialization/deserialization, flavors rn/feature Mention under Features in Changelogs. labels May 23, 2023
@mlflow-automation
Copy link
Collaborator

mlflow-automation commented May 23, 2023

Documentation preview for 4bca13a will be available here when this CircleCI job completes successfully.

More info

for entry in data:
for label, score in zip(entry["labels"], entry["scores"]):
flattened_data.append(
{"sequence": entry["sequence"], "labels": label, "score": score}
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
{"sequence": entry["sequence"], "labels": label, "score": score}
{"sequence": entry["sequence"], "label": label, "score": score}

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 used these keys in order to match exactly what the return dict naming formats are for the transformers pipelines. I think it might be a little confusing to users that if they call the pipeline directly they get a response that says "labels" but if they use it in serving it says "label". This was just to make it consistent with the transformers package implementation :)

outputs=Schema(
[
ColSpec("string", name="sequence"),
ColSpec("string", name="labels"),
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
ColSpec("string", name="labels"),
ColSpec("string", name="label"),

Copy link
Member Author

Choose a reason for hiding this comment

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

same as above (purely for consistency with the transformers API)

Copy link
Collaborator

@WeichenXu123 WeichenXu123 left a comment

Choose a reason for hiding this comment

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

LGTM after addressing comments

Comment on lines 1829 to 1837
{'sequence': {0: 'My dog loves to eat spaghetti',
1: 'My dog loves to eat spaghetti',
2: 'My dog hates going to the vet',
3: 'My dog hates going to the vet'},
'label': {0: 'happy', 1: 'sad', 2: 'sad', 3: 'happy'},
'score': {0: 0.9896970987319946,
1: 0.010302911512553692,
2: 0.957074761390686,
3: 0.042925238609313965}}
Copy link
Member

@harupy harupy May 24, 2023

Choose a reason for hiding this comment

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

Can we replace this with what the dataframe looks like because this function returns a dataframe. It's difficult to guess what this functions returns from the converted dictionary.

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. Changing to the .to_string() output format

Comment on lines 1664 to 1665
# interim_output = self._parse_lists_of_dict_to_list_of_str(raw_output, output_key)
# output = self._parse_list_output_for_multiple_candidate_pipelines(interim_output)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# interim_output = self._parse_lists_of_dict_to_list_of_str(raw_output, output_key)
# output = self._parse_list_output_for_multiple_candidate_pipelines(interim_output)

Copy link
Member Author

Choose a reason for hiding this comment

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

TY for the catch :)

Comment on lines 2545 to 2531
"outputs": '[{"type": "string"}]',
"outputs": '[{"name": "sequence", "type": "string"}, {"name": "labels", '
'"type": "string"}, {"name": "score", "type": "double"}]',
Copy link
Member

@harupy harupy May 24, 2023

Choose a reason for hiding this comment

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

Can we use dict instead of string here?

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 to match the output of ModelSignature.to_dict(). The alternative might be to construct the ModelSignature instance results direclty to a dict instead of a JSON encoded representation, but that will probably make this test far more complicated to follow.

@@ -2425,12 +2425,12 @@ Pipeline Type Input Type Output Type
Instructional Text Generation str or List[str] str or List[str]
Conversational str or List[str] str or List[str]
Summarization str or List[str] str or List[str]
Text Classification str or List[str] str or List[str]
Text Classification str or List[str] pd.DataFrame
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to include the dataframe schema like this?

pd.DataFrame (dtypes: {foo: int, bar: float})

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 idea :)

@@ -1800,6 +1807,52 @@ def _coerce_exploded_dict_to_single_dict(self, data):
else:
return data

def _parse_zero_shot_text_classifier_output_to_df(self, data):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def _parse_zero_shot_text_classifier_output_to_df(self, data):
def _{ flatten | expand }_zero_shot_text_classifier_output(self, data):

Nit: I'd use flatten or expand here :)

Copy link
Member Author

Choose a reason for hiding this comment

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

agreed. flatten is more appropriate. Changed!

Copy link
Member

@harupy harupy left a comment

Choose a reason for hiding this comment

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

Left some comments, looks good to me once they are addressed :)

Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
Signed-off-by: Ben Wilson <benjamin.wilson@databricks.com>
@BenWilson2 BenWilson2 enabled auto-merge (squash) May 24, 2023 19:25
@BenWilson2 BenWilson2 merged commit 4dcf9e3 into mlflow:master May 24, 2023
35 checks passed
@BenWilson2 BenWilson2 deleted the classifier-standardization branch May 24, 2023 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/models MLmodel format, model serialization/deserialization, flavors rn/feature Mention under Features in Changelogs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants