-
Notifications
You must be signed in to change notification settings - Fork 187
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
Fix device handling and logits concatenation in OliveEvaluator #1615
Open
tezheng
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
tezheng:fix/eval_support_dict_as_output
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 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 |
---|---|---|
|
@@ -196,7 +196,11 @@ def compute_throughput(metric: Metric, latencies: Any) -> MetricResult: | |
class _OliveEvaluator(OliveEvaluator): | ||
@staticmethod | ||
def device_string_to_torch_device(device: Device): | ||
return torch.device("cuda") if device == Device.GPU else torch.device(device) | ||
try: | ||
return torch.device("cuda") if device == Device.GPU else torch.device(device) | ||
except (ValueError, TypeError, RuntimeError): | ||
logger.warning("Device %s is not supported in torch, fallback to CPU instead.", device) | ||
return torch.device("cpu") | ||
|
||
@classmethod | ||
def io_bind_enabled(cls, metric: Metric, inference_settings: Dict) -> bool: | ||
|
@@ -462,7 +466,7 @@ def _inference( | |
if is_single_tensor_output: | ||
logits = torch.cat(logits, dim=0) | ||
else: | ||
logits = {k: torch.cat(logits[k], dim=0) for k in output_names} | ||
logits = {k: torch.cat(logits_dict[k], dim=0) for k in output_names} | ||
|
||
tuning_result_file = inference_settings.get("tuning_result_file") | ||
if tuning_result_file: | ||
|
@@ -736,6 +740,7 @@ def _inference( | |
preds = [] | ||
targets = [] | ||
logits = [] | ||
logits_dict = collections.defaultdict(list) | ||
device = _OliveEvaluator.device_string_to_torch_device(device) | ||
run_kwargs = metric.get_run_kwargs() | ||
if device: | ||
|
@@ -748,15 +753,20 @@ def _inference( | |
# it is expensive to convert to list and then convert back to torch tensor | ||
preds.append(outputs.cpu()) | ||
targets.append(labels.cpu()) | ||
logits.append( | ||
result.logits.cpu() | ||
if not isinstance(result, torch.Tensor) and getattr(result, "logits", None) is not None | ||
else result.cpu() | ||
) | ||
if isinstance(result, torch.Tensor): | ||
logits.append(result.cpu()) | ||
elif isinstance(result, (list, tuple)): | ||
logits.append([r.cpu() for r in result]) | ||
elif isinstance(result, dict): | ||
for k in result: | ||
logits_dict[k].append(result[k].cpu()) | ||
Comment on lines
+756
to
+762
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. How can we handle the case when result has logits attribute? 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. You mean result like |
||
# concatenate along the batch dimension | ||
preds = torch.cat(preds, dim=0) | ||
targets = torch.cat(targets, dim=0) | ||
logits = torch.cat(logits, dim=0) | ||
if not logits_dict: | ||
logits = torch.cat(logits, dim=0) | ||
else: | ||
logits = {k: torch.cat(logits_dict[k], dim=0) for k in logits_dict} | ||
# move model to cpu | ||
if device: | ||
session.to("cpu") | ||
|
This file contains 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.
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.
maybe we can just do
torch.device("cuda") if device == Device.GPU else torch.device("cpu")
. Otherwise, this would raise the warning for npu targets everytime. since this method is only used when evaluating pytorch models, I think it's save to just return cuda or cpu only.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.
So we will only consider
cuda
andcpu
, for now at least? Are we planning to support device like Applemps
?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.
yes, there are no plans for other torch devices like mps for now.