This repository was archived by the owner on Aug 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 886
add config option for ipex (https://github.com/min-jean-cho/serve/blob/ipex_enable/examples/IPEX/README.md) #1319
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -2,17 +2,25 @@ | |
| Base default handler to load torchscript or eager mode [state_dict] models | ||
| Also, provides handle method per torch serve custom model specification | ||
| """ | ||
|
|
||
| import abc | ||
| import logging | ||
| import os | ||
| import importlib.util | ||
| import time | ||
| import torch | ||
|
|
||
| from ..utils.util import list_classes_from_module, load_label_mapping | ||
|
|
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| ipex_enabled = False | ||
| if os.environ.get("TS_IPEX_ENABLE", "false") == "true": | ||
| try: | ||
| import intel_extension_for_pytorch as ipex | ||
| ipex_enabled = True | ||
| except ImportError as error: | ||
| logger.warning("IPEX was not installed. Please install IPEX if wanted.") | ||
|
|
||
| class BaseHandler(abc.ABC): | ||
| """ | ||
|
|
@@ -33,7 +41,7 @@ def __init__(self): | |
|
|
||
| def initialize(self, context): | ||
| """Initialize function loads the model.pt file and initialized the model object. | ||
| First try to load torchscript else load eager mode state_dict based model. | ||
| First try to load torchscript else load eager mode state_dict based model. | ||
|
|
||
| Args: | ||
| context (context): It is a JSON Object containing information | ||
|
|
@@ -44,7 +52,8 @@ def initialize(self, context): | |
|
|
||
| """ | ||
| properties = context.system_properties | ||
| self.map_location = "cuda" if torch.cuda.is_available() and properties.get("gpu_id") is not None else "cpu" | ||
| self.map_location = "cuda" if torch.cuda.is_available( | ||
| ) and properties.get("gpu_id") is not None else "cpu" | ||
| self.device = torch.device( | ||
| self.map_location + ":" + str(properties.get("gpu_id")) | ||
| if torch.cuda.is_available() and properties.get("gpu_id") is not None | ||
|
|
@@ -63,7 +72,8 @@ def initialize(self, context): | |
|
|
||
| if model_file: | ||
| logger.debug("Loading eager model") | ||
| self.model = self._load_pickled_model(model_dir, model_file, model_pt_path) | ||
| self.model = self._load_pickled_model( | ||
| model_dir, model_file, model_pt_path) | ||
| self.model.to(self.device) | ||
| else: | ||
| logger.debug("Loading torchscript model") | ||
|
|
@@ -73,6 +83,9 @@ def initialize(self, context): | |
| self.model = self._load_torchscript_model(model_pt_path) | ||
|
|
||
| self.model.eval() | ||
| if ipex_enabled: | ||
| self.model = self.model.to(memory_format=torch.channels_last) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this apply only to vision models? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but benign to non-CNN models. |
||
| self.model = ipex.optimize(self.model) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @min-jean-cho does it choose optimization level 01 by default?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, ipex will choose level 'O1' by default. |
||
|
|
||
| logger.debug('Model file %s loaded successfully', model_pt_path) | ||
|
|
||
|
|
@@ -203,7 +216,8 @@ def handle(self, data, context): | |
| output = self.explain_handle(data_preprocess, data) | ||
|
|
||
| stop_time = time.time() | ||
| metrics.add_time('HandlerTime', round((stop_time - start_time) * 1000, 2), None, 'ms') | ||
| metrics.add_time('HandlerTime', round( | ||
| (stop_time - start_time) * 1000, 2), None, 'ms') | ||
| return output | ||
|
|
||
| def explain_handle(self, data_preprocess, raw_data): | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.