-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[AINode] Update forecast interface #16978
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
Merged
CRZbulabula
merged 1 commit into
apache:master
from
yunbow30944:update_forecast_interface
Jan 7, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -21,84 +21,207 @@ | |||||
| import torch | ||||||
|
|
||||||
| from iotdb.ainode.core.exception import InferenceModelInternalException | ||||||
| from iotdb.ainode.core.model.model_info import ModelInfo | ||||||
| from iotdb.ainode.core.model.model_loader import load_model | ||||||
|
|
||||||
|
|
||||||
| class BasicPipeline(ABC): | ||||||
| def __init__(self, model_info, **model_kwargs): | ||||||
| def __init__(self, model_info: ModelInfo, **model_kwargs): | ||||||
| self.model_info = model_info | ||||||
| self.device = model_kwargs.get("device", "cpu") | ||||||
| self.model = load_model(model_info, device_map=self.device, **model_kwargs) | ||||||
|
|
||||||
| @abstractmethod | ||||||
| def preprocess(self, inputs): | ||||||
| def preprocess(self, inputs, **infer_kwargs): | ||||||
| """ | ||||||
| Preprocess the input before inference, including shape validation and value transformation. | ||||||
| """ | ||||||
| raise NotImplementedError("preprocess not implemented") | ||||||
|
|
||||||
| @abstractmethod | ||||||
| def postprocess(self, outputs: torch.Tensor): | ||||||
| def postprocess(self, outputs, **infer_kwargs): | ||||||
| """ | ||||||
| Post-process the outputs after the entire inference task. | ||||||
| """ | ||||||
| raise NotImplementedError("postprocess not implemented") | ||||||
|
|
||||||
|
|
||||||
| class ForecastPipeline(BasicPipeline): | ||||||
| def __init__(self, model_info, **model_kwargs): | ||||||
| def __init__(self, model_info: ModelInfo, **model_kwargs): | ||||||
| super().__init__(model_info, model_kwargs=model_kwargs) | ||||||
|
|
||||||
| def preprocess(self, inputs): | ||||||
| def preprocess( | ||||||
| self, | ||||||
| inputs: list[dict[str, dict[str, torch.Tensor] | torch.Tensor]], | ||||||
| **infer_kwargs, | ||||||
| ): | ||||||
| """ | ||||||
| The inputs should be 3D tensor: [batch_size, target_count, sequence_length]. | ||||||
| Preprocess the input data before passing it to the model for inference, validating the shape and type of the input data. | ||||||
| Args: | ||||||
| inputs (list[dict]): | ||||||
| The input data, a list of dictionaries, where each dictionary contains: | ||||||
| - 'targets': A tensor (1D or 2D) of shape (input_length,) or (target_count, input_length). | ||||||
| - 'past_covariates': A dictionary of tensors (optional), where each tensor has shape (input_length,). | ||||||
| - 'future_covariates': A dictionary of tensors (optional), where each tensor has shape (input_length,). | ||||||
| infer_kwargs (dict, optional): Additional keyword arguments for inference, such as: | ||||||
| - `output_length`(int): Used to check validation of 'future_covariates' if provided. | ||||||
| Raises: | ||||||
| ValueError: If the input format is incorrect (e.g., missing keys, invalid tensor shapes). | ||||||
| Returns: | ||||||
| The preprocessed inputs, validated and ready for model inference. | ||||||
| """ | ||||||
| if len(inputs.shape) != 3: | ||||||
| raise InferenceModelInternalException( | ||||||
| f"[Inference] Input must be: [batch_size, target_count, sequence_length], but receives {inputs.shape}" | ||||||
|
|
||||||
| if isinstance(inputs, list): | ||||||
| output_length = infer_kwargs.get("output_length", 96) | ||||||
| for idx, input_dict in enumerate(inputs): | ||||||
| # Check if the dictionary contains the expected keys | ||||||
| if not isinstance(input_dict, dict): | ||||||
| raise ValueError(f"Input at index {idx} is not a dictionary.") | ||||||
|
|
||||||
| required_keys = ["targets"] | ||||||
| for key in required_keys: | ||||||
| if key not in input_dict: | ||||||
| raise ValueError( | ||||||
| f"Key '{key}' is missing in input at index {idx}." | ||||||
| ) | ||||||
|
|
||||||
| # Check 'targets' is torch.Tensor and has the correct shape | ||||||
| targets = input_dict["targets"] | ||||||
| if not isinstance(targets, torch.Tensor): | ||||||
| raise ValueError( | ||||||
| f"'targets' must be torch.Tensor, but got {type(targets)} at index {idx}." | ||||||
| ) | ||||||
| if targets.ndim not in [1, 2]: | ||||||
| raise ValueError( | ||||||
| f"'targets' must have 1 or 2 dimensions, but got {targets.ndim} dimensions at index {idx}." | ||||||
| ) | ||||||
| # If targets is 2-d, check if the second dimension is input_length | ||||||
| if targets.ndim == 2: | ||||||
| n_variates, input_length = targets.shape | ||||||
| else: | ||||||
| input_length = targets.shape[ | ||||||
| 0 | ||||||
| ] # for 1-d targets, shape should be (input_length,) | ||||||
|
||||||
| ] # for 1-d targets, shape should be (input_length,) | |
| ] # for 1-d targets, infer input_length from the first (and only) dimension |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable 'n_variates' is assigned on line 104 but never used. If it's not needed for any validation or processing, consider removing this assignment or using it for additional validation logic.