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

[Search] Consolidate ML model fetch calls #176257

Merged
merged 22 commits into from
Feb 8, 2024

Conversation

demjened
Copy link
Contributor

@demjened demjened commented Feb 5, 2024

Summary

With the introduction of fetch_ml_models.ts, the fetching and enriching of ML models for Search purposes has been consolidated in that API. This allows us to remove the dependency on the older method that works with ML plugin-specific TrainedModel entities.

This PR makes the following changes:

  • Switch over code that depend on ML models to use the new function from fetch_ml_models.ts (that already does sorting/filtering).
  • Move the fetch process to ml_inference_logic.ts, and begin periodically polling after mounting the logic. This enables passing down values to lower components, e.g. model_select_logic.ts, instead of repeating the fetch there.
  • Use MlModel instead of TrainedModel/MlTrainedModelConfig. This requires adding some missing properties to MlModel: types, inputFieldNames, version.
  • Remove the old fetch methods (x-pack/plugins/enterprise_search/server/lib/ml/ml_*_logic.ts).
  • Remove the "no models available" component and condition, since as of 8.12 at least the ELSER/E5 placeholders are always present.

Checklist

@demjened demjened added release_note:skip Skip the PR/issue when compiling release notes Team:EnterpriseSearch v8.13.0 labels Feb 5, 2024
if (!selectedMLModel || configuration.existingPipeline) return null;
const modelType = getMLType(getMlModelTypesForModelConfig(selectedMLModel));
if (!selectedModel || configuration.existingPipeline) return null;
const modelType = getMLType([selectedModel.type]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this could be simplified by using selectedModel.type directly. I'll check if there are any side effects and modify the code in a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at getMLType() I think this is still needed to show the correct type for the lang_ident model. I think without that check this might have show built-in instead. But you should confirm that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call - it does change the type from lang_ident to classification. I think that's OK - the built-in language identification model's type is classification, and I don't see any logic that depends on the value of modelType. But for the sake of less moving parts I restored the original value parsing in this line and in another.

!API_REQUEST_COMPLETE_STATUSES.includes(mlModelsStatus) ||
!API_REQUEST_COMPLETE_STATUSES.includes(mappingStatus),
() => [selectors.mappingStatus],
(mappingStatus: Status) => !API_REQUEST_COMPLETE_STATUSES.includes(mappingStatus),
Copy link
Contributor Author

@demjened demjened Feb 5, 2024

Choose a reason for hiding this comment

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

The isLoading flag for the pipeline configuration panel no longer depends on the status of model fetching. The "models loading" state is now displayed in the model selector. Other steps of the workflow (e.g. generating pipeline configuration) depend on the models data, but it's very unlikely the user will get to that stage before the models finish loading (and even if they do, they just need to wait a little).

If we keep this dependency, the pipeline configuration panel's rendering will be blocked by the fetching of the models.

Comment on lines +149 to +150
() => [selectors.selectableModelsFromMLInferenceLogic],
(selectableModels) => selectableModels, // Pass-through
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wonder if we need these pass-through selectors, or we should just expose the imported selectors directly like startPollingModels.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Discussed with @TattdCodeMonkey that we don't need plain pass-through selectors. I'll remove these in a follow-up PR.

@demjened
Copy link
Contributor Author

demjened commented Feb 5, 2024

/ci

@demjened demjened marked this pull request as ready for review February 5, 2024 21:52
@demjened demjened requested a review from a team February 5, 2024 21:52
events: {},
events: ({ actions }) => ({
afterMount: () => {
actions.startPollingModels();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For some reason the GET /ml/models API is called twice upon opening the pipeline config flyout, and populates the models, so this hook is technically not needed. But I can't find what triggers the other call, so I'm leaving it here while I investigate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The 2nd call is caused by useEffect() -> setIndexName in add_inference_pipeline_flyout.tsx. I removed the listener, moved the fetch operations there, and removed the afterMount hook.

@demjened
Copy link
Contributor Author

demjened commented Feb 5, 2024

@elasticmachine merge upstream

@demjened demjened force-pushed the demjened/remove-ml-model-fetch-redundancy branch from 05403ad to 92019fd Compare February 7, 2024 21:40
Comment on lines +61 to +64
// Trigger fetching of initial data: existing ML pipelines, available models, index mapping
makeMlInferencePipelinesRequest(undefined);
startPollingModels();
makeMappingRequest({ indexName });
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved initial fetch operations here from setIndexName listener.

@demjened
Copy link
Contributor Author

demjened commented Feb 8, 2024

@elasticmachine merge upstream

Copy link
Member

@sphilipse sphilipse left a comment

Choose a reason for hiding this comment

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

LGTM overall :)

@@ -103,9 +107,6 @@ export const AddInferencePipelineContent = ({ onClose }: AddInferencePipelineFly
</EuiFlyoutBody>
);
}
if (supportedMLModels.length === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 good to see this removed since we allow the lang ident model now there should never be 0 ML Models

if (!selectedMLModel || configuration.existingPipeline) return null;
const modelType = getMLType(getMlModelTypesForModelConfig(selectedMLModel));
if (!selectedModel || configuration.existingPipeline) return null;
const modelType = getMLType([selectedModel.type]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at getMLType() I think this is still needed to show the correct type for the lang_ident model. I think without that check this might have show built-in instead. But you should confirm that.

@@ -359,7 +349,7 @@ export const MLInferenceLogic = kea<
},
startTextExpansionModelSuccess: () => {
// Refresh ML models list when the text expansion model is started
actions.makeMLModelsRequest(undefined);
actions.startPollingModels();
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should have a refreshModels action on CachedFetchModlesApiLogic instead of re-using startPollingModels to force an update.

I'm fine with this for now, but its an improvement we could consider, it may not be worth the overhead though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@TattdCodeMonkey For my understanding - is the issue that this action happens outside the pipeline configuration screen (ELSER callout), and so it unnecessarily starts polling the models rather than refreshing them once?

Copy link
Contributor

@TattdCodeMonkey TattdCodeMonkey Feb 8, 2024

Choose a reason for hiding this comment

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

It's more a semantic quibble, that really isn't important.

If I under the code correctly we are calling startPollingModels() here to force a refresh the model list after an update instead of waiting for the next poll, correct? If thats the case it would be a little nicer to have an action that is descriptive of that intent vs re-using startPollingModels().

BUT calling startPollingModels() works, so is it worth the complexity of introducing another action that may require other special handing to not interfere with the existing polling? maybe not, I could make the argument both ways. So feel free to ignore me this time if you want :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha - yeah, it might not be worth the effort. But let's revisit this once these components and logics have been refactored.

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
enterpriseSearch 2278 2273 -5

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
enterpriseSearch 2.7MB 2.7MB -4.1KB
Unknown metric groups

miscellaneous assets size

id before after diff
enterpriseSearch 3.4MB 3.3MB -90.0KB

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@demjened demjened merged commit e3f1d12 into main Feb 8, 2024
16 checks passed
@demjened demjened deleted the demjened/remove-ml-model-fetch-redundancy branch February 8, 2024 20:36
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Feb 8, 2024
CoenWarmer pushed a commit to CoenWarmer/kibana that referenced this pull request Feb 15, 2024
## Summary

With the introduction of
[fetch_ml_models.ts](https://github.com/elastic/kibana/blob/main/x-pack/plugins/enterprise_search/server/lib/ml/fetch_ml_models.ts),
the fetching and enriching of ML models for Search purposes has been
consolidated in that API. This allows us to remove the dependency on the
older method that works with ML plugin-specific `TrainedModel` entities.

This PR makes the following changes:
- Switch over code that depend on ML models to use the new function from
`fetch_ml_models.ts` (that already does sorting/filtering).
- Move the fetch process to `ml_inference_logic.ts`, and begin
periodically polling after mounting the logic. This enables passing down
values to lower components, e.g. `model_select_logic.ts`, instead of
repeating the fetch there.
- Use `MlModel` instead of `TrainedModel/MlTrainedModelConfig`. This
requires adding some missing properties to `MlModel`: `types`,
`inputFieldNames`, `version`.
- Remove the old fetch methods
(`x-pack/plugins/enterprise_search/server/lib/ml/ml_*_logic.ts`).
- Remove the "no models available" component and condition, since as of
8.12 at least the ELSER/E5 placeholders are always present.

### Checklist
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
CoenWarmer pushed a commit to CoenWarmer/kibana that referenced this pull request Feb 15, 2024
## Summary

With the introduction of
[fetch_ml_models.ts](https://github.com/elastic/kibana/blob/main/x-pack/plugins/enterprise_search/server/lib/ml/fetch_ml_models.ts),
the fetching and enriching of ML models for Search purposes has been
consolidated in that API. This allows us to remove the dependency on the
older method that works with ML plugin-specific `TrainedModel` entities.

This PR makes the following changes:
- Switch over code that depend on ML models to use the new function from
`fetch_ml_models.ts` (that already does sorting/filtering).
- Move the fetch process to `ml_inference_logic.ts`, and begin
periodically polling after mounting the logic. This enables passing down
values to lower components, e.g. `model_select_logic.ts`, instead of
repeating the fetch there.
- Use `MlModel` instead of `TrainedModel/MlTrainedModelConfig`. This
requires adding some missing properties to `MlModel`: `types`,
`inputFieldNames`, `version`.
- Remove the old fetch methods
(`x-pack/plugins/enterprise_search/server/lib/ml/ml_*_logic.ts`).
- Remove the "no models available" component and condition, since as of
8.12 at least the ELSER/E5 placeholders are always present.

### Checklist
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
fkanout pushed a commit to fkanout/kibana that referenced this pull request Mar 4, 2024
## Summary

With the introduction of
[fetch_ml_models.ts](https://github.com/elastic/kibana/blob/main/x-pack/plugins/enterprise_search/server/lib/ml/fetch_ml_models.ts),
the fetching and enriching of ML models for Search purposes has been
consolidated in that API. This allows us to remove the dependency on the
older method that works with ML plugin-specific `TrainedModel` entities.

This PR makes the following changes:
- Switch over code that depend on ML models to use the new function from
`fetch_ml_models.ts` (that already does sorting/filtering).
- Move the fetch process to `ml_inference_logic.ts`, and begin
periodically polling after mounting the logic. This enables passing down
values to lower components, e.g. `model_select_logic.ts`, instead of
repeating the fetch there.
- Use `MlModel` instead of `TrainedModel/MlTrainedModelConfig`. This
requires adding some missing properties to `MlModel`: `types`,
`inputFieldNames`, `version`.
- Remove the old fetch methods
(`x-pack/plugins/enterprise_search/server/lib/ml/ml_*_logic.ts`).
- Remove the "no models available" component and condition, since as of
8.12 at least the ELSER/E5 placeholders are always present.

### Checklist
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting release_note:skip Skip the PR/issue when compiling release notes Team:EnterpriseSearch v8.13.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants