Skip to content

Issue when cloning MetadataStore #333

@AyoubMDL

Description

@AyoubMDL

Hello,

When cloning a model whose nodes contain a MetadataStore, the underlying metadata objects are shallow copied. Therefore, the original and cloned models share references to the same metadata entries (see code below).

import onnx_ir as ir

def check_meta():
    ir_model = ir.from_onnx_text("""
                < ir_version: 10, opset_import: ["" : 21] >
                test_model (float[N, 32] X) => (float [N, ?] Y)
                {
                    Y = Relu(X)
                }
            """)

    # Add metadata to the node
    ir_model.graph[0].meta["test_meta"] = "test_value"

    # Clone the model
    cloned_ir_model = ir_model.clone()

    # Check that the metadata is cloned
    assert cloned_ir_model.graph[0].meta is not ir_model.graph[0].meta

    # Check that the metadata value is the same
    print(
        cloned_ir_model.graph[0].meta["test_meta"] is ir_model.graph[0].meta["test_meta"]
    )  # -> True

check_meta()

The fix is that MetadataStore content should be deep copied. I can open a PR if necessary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions