Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"export": {
"opset_version": 17,
"batch_size": 1,
"export_params": true,
"do_constant_folding": true,
"verbose": false,
"dynamo": false,
"enable_hierarchy_tags": true,
"clean_onnx": false,
"hierarchy_tag_format": "full",
"input_tensors": [
{
"name": "input_ids",
"dtype": "int32",
"shape": [
1,
512
],
"value_range": [
0,
50265
]
},
{
"name": "bbox",
"dtype": "int32",
"shape": [
1,
512,
4
],
"value_range": [
0,
1000
]
},
{
"name": "attention_mask",
"dtype": "int32",
"shape": [
1,
512
],
"value_range": [
0,
2
]
},
{
"name": "token_type_ids",
"dtype": "int32",
"shape": [
1,
512
],
"value_range": [
0,
1
]
}
],
"output_tensors": [
{
"name": "start_logits"
},
{
"name": "end_logits"
}
]
},
"optim": {
"clamp_constant_values": true
},
"quant": {
"mode": "fp16",
"samples": 10,
"calibration_method": "minmax",
"weight_type": "uint8",
"activation_type": "uint8",
"per_channel": false,
"symmetric": false,
"weight_symmetric": null,
"activation_symmetric": null,
"save_calibration": false,
"distribution": "uniform",
"seed": null,
"calibration_load_path": null,
"calibration_save_path": null,
"op_types_to_quantize": null,
"nodes_to_exclude": null,
"task": "question-answering",
"model_id": "DmitrySpartak/layoutlm-invoices",
"model_type": "layoutlm",
"fp16_keep_io_types": true,
"fp16_op_block_list": null
},
"loader": {
"task": "question-answering",
"model_class": "LayoutLMForQuestionAnswering",
"model_type": "layoutlm"
},
"compile": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"export": {
"opset_version": 17,
"batch_size": 1,
"export_params": true,
"do_constant_folding": true,
"verbose": false,
"dynamo": false,
"enable_hierarchy_tags": true,
"clean_onnx": false,
"hierarchy_tag_format": "full",
"input_tensors": [
{
"name": "input_ids",
"dtype": "int32",
"shape": [
1,
512
],
"value_range": [
0,
50265
]
},
{
"name": "bbox",
"dtype": "int32",
"shape": [
1,
512,
4
],
"value_range": [
0,
1000
]
},
{
"name": "attention_mask",
"dtype": "int32",
"shape": [
1,
512
],
"value_range": [
0,
2
]
},
{
"name": "token_type_ids",
"dtype": "int32",
"shape": [
1,
512
],
"value_range": [
0,
1
]
}
],
"output_tensors": [
{
"name": "start_logits"
},
{
"name": "end_logits"
}
]
},
"optim": {
"clamp_constant_values": true
},
"quant": null,
"loader": {
"task": "question-answering",
"model_class": "LayoutLMForQuestionAnswering",
"model_type": "layoutlm"
}
}
24 changes: 23 additions & 1 deletion src/winml/modelkit/loader/resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,20 @@ def _upgrade_fill_mask_for_seq2seq(task: str, config: PretrainedConfig) -> str:
}


_ARCHITECTURE_SUFFIX_TASKS: dict[str, str] = {
"ForQuestionAnswering": "question-answering",
}


def _infer_task_from_architecture_suffix(config: PretrainedConfig) -> str | None:
"""Infer task from architecture head suffix when Optimum has no mapping."""
for arch_name in getattr(config, "architectures", None) or []:
for suffix, task in _ARCHITECTURE_SUFFIX_TASKS.items():
if arch_name.endswith(suffix):
return task
return None


def _resolve_task_modality(config: PretrainedConfig, task: str) -> str:
"""Upgrade a modality-blind ``feature-extraction`` to its modality-aware variant.

Expand Down Expand Up @@ -415,8 +429,16 @@ def _infer_task_from_architecture(config: PretrainedConfig) -> str:

Includes the encoder-decoder fill-mask -> text2text-generation correction.
"""
model_class = _resolve_model_class_from_config(config)
try:
task = _detect_task_from_model_class(model_class)
except ValueError:
suffix_task = _infer_task_from_architecture_suffix(config)
if suffix_task is None:
raise
task = suffix_task
return _upgrade_fill_mask_for_seq2seq(
_detect_task_from_model_class(_resolve_model_class_from_config(config)),
task,
config,
)

Expand Down
8 changes: 8 additions & 0 deletions tests/unit/loader/test_resolve_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ def test_seq2seq_fill_mask_is_upgraded_and_source_is_tasks_manager():
assert r.composite == {"encoder": "feature-extraction", "decoder": "text2text-generation"}


def test_architecture_suffix_fallback_resolves_layoutlm_question_answering():
r = resolve_task(_cfg("layoutlm", ["LayoutLMForQuestionAnswering"]))
assert r.task == "question-answering"
assert r.optimum_task == "question-answering"
assert r.model_class.__name__ == "AutoModelForQuestionAnswering"
assert r.source == TaskSource.TASKS_MANAGER


def test_user_task_preserved_verbatim_no_modality_upgrade():
r = resolve_task(_cfg("vit", ["ViTModel"]), task="feature-extraction")
assert r.task == "feature-extraction"
Expand Down
Loading