Repository: ggerganov/llama.cpp
File: examples/model-conversion/scripts/causal/run-org-model.py (Python)

Code before:
    return parser.parse_args()

def load_model_and_tokenizer(model_path, device="auto"):
    print("Loading model and tokenizer using AutoTokenizer:", model_path)
    tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
    config = AutoConfig.from_pretrained(model_path, trust_remote_code=True)
    multimodal = False
    full_config = config

    # Determine device_map based on device argument
    if device == "cpu":
        device_map = {"": "cpu"}
        print("Forcing CPU usage")
    elif device == "auto":
        device_map = "auto"
    else:
        device_map = {"": device}

    print("Model type:       ", config.model_type)
    if "vocab_size" not in config and "text_config" in config:
        config = config.text_config
        multimodal = True

    def print_if_exists(label, obj, attr, default="N/A"):
        if (val := getattr(obj, attr, None)) is not None:
            print(f"{label}", val)

    print_if_exists("Vocab size:       ", config, "vocab_size")
    print_if_exists("Hidden size:      ", config, "hidden_size")
    print_if_exists("Number of layers: ", config, "num_hidden_layers")
    print_if_exists("BOS token id:     ", config, "bos_token_id")
    print_if_exists("EOS token id:     ", config, "eos_token_id")

    unreleased_model_name = os.getenv("UNRELEASED_MODEL_NAME")
    if unreleased_model_name:
        model_name_lower = unreleased_model_name.lower()
        unreleased_module_path = (
            f"transformers.models.{model_name_lower}.modular_{model_name_lower}"
        )
        class_name = f"{unreleased_model_name}ForCausalLM"
        print(f"Importing unreleased model module: {unreleased_module_path}")

        try:
            model_class = getattr(importlib.import_module(unreleased_module_path), class_name)
            model = model_class.from_pretrained(
                    model_path,
                    device_map=device_map,
                    offload_folder="offload",
                    trust_remote_code=True,
                    config=config
            )

Review comment (suggestion):
```suggestion
        val = getattr(obj, attr) if hasattr(obj, attr) else default
        print(f"{label}", val)
```
Sometimes f.ex. `bos_token_id` is `null`, in that case it's useful to print that.

Also, you weren't using `default`, change the code (and remove `default`) if the intention really was to not display anything when not defined.

Code after:
    return parser.parse_args()

def load_model_and_tokenizer(model_path, device="auto"):
    print("Loading model and tokenizer using AutoTokenizer:", model_path)
    tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
    config = AutoConfig.from_pretrained(model_path, trust_remote_code=True)
    multimodal = False
    full_config = config

    # Determine device_map based on device argument
    if device == "cpu":
        device_map = {"": "cpu"}
        print("Forcing CPU usage")
    elif device == "auto":
        device_map = "auto"
    else:
        device_map = {"": device}

    print("Model type:       ", config.model_type)
    if "vocab_size" not in config and "text_config" in config:
        config = config.text_config
        multimodal = True

    def print_if_exists(label, obj, attr, default="N/A"):
        val = getattr(obj, attr) if hasattr(obj, attr) else default
        print(f"{label}", val)

    print_if_exists("Vocab size:       ", config, "vocab_size")
    print_if_exists("Hidden size:      ", config, "hidden_size")
    print_if_exists("Number of layers: ", config, "num_hidden_layers")
    print_if_exists("BOS token id:     ", config, "bos_token_id")
    print_if_exists("EOS token id:     ", config, "eos_token_id")

    unreleased_model_name = os.getenv("UNRELEASED_MODEL_NAME")
    if unreleased_model_name:
        model_name_lower = unreleased_model_name.lower()
        unreleased_module_path = (
            f"transformers.models.{model_name_lower}.modular_{model_name_lower}"
        )
        class_name = f"{unreleased_model_name}ForCausalLM"
        print(f"Importing unreleased model module: {unreleased_module_path}")

        try:
            model_class = getattr(importlib.import_module(unreleased_module_path), class_name)
            model = model_class.from_pretrained(
                    model_path,
                    device_map=device_map,
                    offload_folder="offload",
                    trust_remote_code=True,
                    config=config
            )