Skip to content

Redundant preprocessing & inference for SMILES explanations and predictions #28

Description

@aditya0by0

Problem statement

The backend currently exposes two API endpoints:
https://github.com/ChEB-AI/chebifier-web/blob/49ace5beb84d4e607fe500a0bac7ffdd17546a30/backend/app.py#L22-L26

  1. /api/classify

  2. /api/details (SMILES explanation)

Current behavior

When a user submits a list of SMILES from the frontend:

  1. The classification API is called.

  2. SMILES are preprocessed and inference is run.

  3. Predictions are returned to the frontend.

  4. For each predicted SMILES, a “Details” button is shown.

  5. When the user clicks Details:

    • The backend again preprocesses the same SMILES
    • Runs another full inference pass to generate explanations

Another minor issue: if details (explanation) of a smiles is already fetched, when I add another smiles, run prediction and click again on the details of first smiles, the api gets called again.

Relevant line showing repeated execution:

Concrete example: ELECTRA

For the ELECTRA model, prediction and explanation are handled by entirely separate pipelines:

  • Explanation pipeline
    explain_smiles

    def explain_smiles(self, smiles) -> dict:
    from chebai.preprocessing.reader import EMBEDDING_OFFSET
    reader = self.reader_cls()
    token_dict = reader.to_data(dict(features=smiles, labels=None))
    tokens = np.array(token_dict["features"]).astype(int).tolist()
    result = self.calculate_results([token_dict])

  • Prediction pipeline
    predict_smiles_list (via nn_predictor)

    @modelwise_smiles_lru_cache.batch_decorator
    def predict_smiles_list(self, smiles_list: list[str]) -> list:
    """Returns a list with the length of smiles_list, each element is either None (=failure) or a dictionary
    Of classes and predicted values."""

As a result:

  • The same SMILES go through preprocessing multiple times
  • The same model runs inference multiple times
  • This adds unnecessary latency and compute cost
  • The problem scales poorly when many SMILES or repeated explanation requests are involved

Proposed solution

Generate and cache explanations during the initial prediction step.

Specifically:

  1. During /api/classify:

    • Run prediction and explanation together in a single pipeline

    • Store:

      • Predictions
      • Explanations
    • Cache both using the existing custom caching mechanism (which already supports this)

  2. During /api/details:

    • Do not rerun preprocessing or inference

    • Simply fetch the precomputed explanation from the cache using:

      • (model, smiles) as the lookup key

cc: @sfluegel05

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions