Skip to content

Refined QDQ recipes of BERT/CLIP/VIT for QC and AMD. #1797

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

Closed
wants to merge 4 commits into from

Conversation

tezheng
Copy link
Contributor

@tezheng tezheng commented Apr 27, 2025

Describe your changes

Checklist before requesting a review

  • Add unit tests for this change.
  • Make sure all tests can pass.
  • Update documents if necessary.
  • Lint and apply fixes to your code by running lintrunner -a
  • Is this a user-facing change? If yes, give a description of this change to be included in the release notes.
  • Is this PR including examples changes? If yes, please remember to update example documentation in a follow-up PR.

(Optional) Issue link

Copy link
Contributor

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

lintrunner found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

Copy link
Contributor

Choose a reason for hiding this comment

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

since these are all new files, are still keeping the old qdq config jsons?

Copy link
Contributor

Choose a reason for hiding this comment

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

bumping this again. Are we keeping both the old and new qdq configs?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

We have already released parts of these in AITK and I created a pr #1893 to align the recipes. It is part of this pr: CLIP text + vision, bert and vit qdq.
Please take a look @jambayk CC @tezheng

I updated the previous qdq configs directly

Copy link
Contributor

Choose a reason for hiding this comment

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

For others, we could take time to merge if feel needed

@tezheng tezheng force-pushed the zhengte/qdq_bert_clip_vit branch from b38f396 to 1d827ae Compare April 29, 2025 06:44

subset = kwargs.get("subset")
split = kwargs.get("split", "train")
ds = load_dataset(dataset, name=subset, split=split)

Check warning

Code scanning / lintrunner

PYLINT/W0621 Warning

Redefining name 'ds' from outer scope (line 149) (redefined-outer-name)
See redefined-outer-name.

# Save the dataset to CSV
data_frame = ds.to_pandas()
print(f"Dataset size: {len(data_frame)}")

Check warning

Code scanning / lintrunner

RUFF/T201 Warning

# Save the dataset to CSV
data_frame = ds.to_pandas()
print(f"Dataset size: {len(data_frame)}")
print(data_frame.head())

Check warning

Code scanning / lintrunner

RUFF/T201 Warning

print(data_frame.head())

data_frame.to_csv(args.output_csv, index=False)
print(f"Dataset saved to {args.output_csv}")

Check warning

Code scanning / lintrunner

RUFF/T201 Warning

@tezheng tezheng force-pushed the zhengte/qdq_bert_clip_vit branch from 5860e49 to b8e068d Compare April 29, 2025 16:53
@tezheng tezheng enabled auto-merge (squash) April 30, 2025 05:48
@tezheng
Copy link
Contributor Author

tezheng commented Apr 30, 2025

@jambayk I think the lint errors are false alarms, do you have any ideas/suggestions?

@jambayk
Copy link
Contributor

jambayk commented Apr 30, 2025

@jambayk I think the lint errors are false alarms, do you have any ideas/suggestions?

@tezheng There were some lint rules and requirements update on main. Please try lintrunner init to use the latest. For this PRm I have pushed a commit with the lint fixes.

@Registry.register_post_process()
def bert_scl_post_process(outputs):
"""Post-processing for Sequence Classification task."""
match outputs:
Copy link
Contributor

Choose a reason for hiding this comment

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

Olive still supports python 3.9 so this syntax is invalid. lint fails because of this.

@tezheng tezheng force-pushed the zhengte/qdq_bert_clip_vit branch from 20a6c6f to b4d82b1 Compare May 2, 2025 13:39
{ "surgeon": "MatMulAddToGemm" }
]
},
"transformer_optimizer": {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this might not be required? with opset 20, the model gets exported with layernorm operator and quant_preprocess in onnxstatic quantization performs gelu fusion automatically

"type": "GraphSurgeries",
"surgeries": [
{ "surgeon": "ReplaceAttentionMaskValue", "replacement": -100.0 },
{ "surgeon": "MatMulAddToGemm" }
Copy link
Contributor

@jambayk jambayk May 6, 2025

Choose a reason for hiding this comment

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

can you also add this new surgery "PowReduceSumPowDiv2LpNorm" for the clip models?

"type": "GraphSurgeries",
"surgeries": [
{ "surgeon": "ReplaceAttentionMaskValue", "replacement": -100.0 },
{ "surgeon": "MatMulAddToGemm" }
Copy link
Contributor

Choose a reason for hiding this comment

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

this works well for qc. not sure if it's recommended for amd.

"model_script": "google_bert_script.py"
},
"passes": {
"conversion": { "type": "OnnxConversion", "target_opset": 20, "dynamic": true, "use_dynamo_exporter": false },
Copy link
Contributor

Choose a reason for hiding this comment

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

dynamic and use_dynamo_exporter options are not required

return Dataset.from_pandas(pd.DataFrame(data))


def parse_args():
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need this and the main part?

"model_type": "bert",
"opt_level": 1,
"optimization_options": {
"enable_gelu": false,
Copy link
Contributor

Choose a reason for hiding this comment

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

this will not compile on AMD NPU

@tezheng tezheng force-pushed the zhengte/qdq_bert_clip_vit branch from e259c65 to 5e394d6 Compare May 14, 2025 02:43
)
)

print("Text encoding latency", model.text_model.latency)

Check warning

Code scanning / lintrunner

RUFF/T201 Warning

)

print("Text encoding latency", model.text_model.latency)
print("Image encoding latency", model.vision_model.latency)

Check warning

Code scanning / lintrunner

RUFF/T201 Warning

args.tokenizer,
)

print("Evaluation result", format_output(result))

Check warning

Code scanning / lintrunner

RUFF/T201 Warning


def prepare_session(
self,
inference_settings: Optional[dict[str, Any]] = None,

Check warning

Code scanning / lintrunner

RUFF/UP007 Warning

self,
inference_settings: Optional[dict[str, Any]] = None,
device: Device = Device.CPU,
execution_providers: Optional[Union[str, list[str]]] = None,

Check warning

Code scanning / lintrunner

RUFF/UP007 Warning

self,
inference_settings: Optional[dict[str, Any]] = None,
device: Device = Device.CPU,
execution_providers: Optional[Union[str, list[str]]] = None,

Check warning

Code scanning / lintrunner

RUFF/UP007 Warning

inference_settings: Optional[dict[str, Any]] = None,
device: Device = Device.CPU,
execution_providers: Optional[Union[str, list[str]]] = None,
rank: Optional[int] = None,

Check warning

Code scanning / lintrunner

RUFF/UP007 Warning

**model.config.to_dict(),
**(self.model_attributes or {}),
}
except Exception as _:

Check notice

Code scanning / CodeQL

Empty except Note

'except' clause does nothing but pass and there is no explanatory comment.

Copilot Autofix

AI about 1 month ago

To fix the issue, we should handle the exception appropriately by logging the error. This ensures that any failure during the assignment to self.model_attributes is recorded, aiding in debugging and maintaining transparency. If the exception is non-critical, we can log it as a warning or info message. Additionally, we should add a comment explaining why the exception is being handled in this way.


Suggested changeset 1
olive/model/handler/pytorch.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/olive/model/handler/pytorch.py b/olive/model/handler/pytorch.py
--- a/olive/model/handler/pytorch.py
+++ b/olive/model/handler/pytorch.py
@@ -182,4 +182,5 @@
             }
-        except Exception as _:
-            pass
+        except Exception as e:
+            # Log the exception to ensure visibility while allowing the program to continue.
+            logger.warning("Failed to set model attributes from model.config: %s", str(e))
 
EOF
@@ -182,4 +182,5 @@
}
except Exception as _:
pass
except Exception as e:
# Log the exception to ensure visibility while allowing the program to continue.
logger.warning("Failed to set model attributes from model.config: %s", str(e))

Copilot is powered by AI and may make mistakes. Always verify output.
@jambayk
Copy link
Contributor

jambayk commented Jun 16, 2025

closing this is it has gone stale and new PRs related to this has merged or are open.

@jambayk jambayk closed this Jun 16, 2025
auto-merge was automatically disabled June 16, 2025 21:19

Pull request was closed

@jambayk jambayk deleted the zhengte/qdq_bert_clip_vit branch June 20, 2025 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants