def _decorate_prog_with_scope_if_not_present(prog: Program):
    """
    For a program without any scope info, we manually add scopes to every op,
    in ordere to test that all graph passes can preserve the source scope info.
    """

    def _is_scopes_present_in_program(prog: Program) -> bool:
        """
        Return True is any op already has the scopes info.
        """

        def _is_scopes_present_in_block(block: Block) -> bool:
            for op in block.operations:
                for b in op.blocks:
                    if _is_scopes_present_in_block(b):
                        return True
                if len(op.scopes) > 0:
                    return True

        for func in prog.functions.values():
            if _is_scopes_present_in_block(func):
                return True

    def _decorate_prog_with_default_torch_scope(prog: Program):
        """
        Decorate every op in the program with a default TORCHSCRIPT_MODULE_TYPE scope info.
        """

        def _decorate_block_with_default_torch_scope(block: Block):
            for op in block.operations:
                for b in op.blocks:
                    _decorate_block_with_default_torch_scope(b)
                assert ScopeSource.TORCHSCRIPT_MODULE_TYPE not in op.scopes
                op.scopes[ScopeSource.TORCHSCRIPT_MODULE_TYPE] = ["dummy"]

        for func in prog.functions.values():
            _decorate_block_with_default_torch_scope(func)

        prog._add_essential_scope_source(ScopeSource.TORCHSCRIPT_MODULE_TYPE)

    if not _is_scopes_present_in_program(prog):
        _decorate_prog_with_default_torch_scope(prog)

    For a program without any scope info, we manually add scopes to every op,
    in ordere to test that all graph passes can preserve the source scope info.
    
coremltools/converters/mil/testing_utils.py