def _validate_model_into_device(model_name: str, model_properties: dict, device: str, calling_func: str = None) -> bool:
    '''
    Note: this function should only be called by `_update_available_models` for threading safeness.

    A function to detect if the device have enough memory to load the target model.
    If not, it will try to eject some models to spare the space.
    Args:
        model_name: The name of the model to load
        model_properties: The model properties of the model
        device: The target device to laod the model
    Returns:
        True we have enough space for the model
        Raise an error and return False if we can't find enough space for the model.
    '''
    if calling_func not in ["unit_test", "_update_available_models"]:
        raise RuntimeError("This function should only be called by `update_available_models` or `unit_test` for "
                           "thread safeness.")

    model_size = get_model_size(model_name, model_properties)
    if _check_memory_threshold_for_model(device, model_size, calling_func=_validate_model_into_device.__name__):
        return True
    else:
        model_cache_key_for_device = [key for key in list(_available_models) if key.endswith(device)]
        sorted_key_for_device = sorted(model_cache_key_for_device,
                                       key=lambda x: _available_models[x][
                                           AvailableModelsKey.most_recently_used_time])
        for key in sorted_key_for_device:
            logger.info(
                f"Eject model = `{key.split('||')[0]}` with size = `{_available_models[key].get('model_size', constants.DEFAULT_MODEL_SIZE)}` from device = `{device}` "
                f"to save space for model = `{model_name}`.")
            del _available_models[key]
            if _check_memory_threshold_for_model(device, model_size, calling_func=_validate_model_into_device.__name__):
                return True

        if _check_memory_threshold_for_model(device, model_size,
                                             calling_func=_validate_model_into_device.__name__) is False:
            raise ModelCacheManagementError(
                f"Marqo CANNOT find enough space to load model = `{model_name}` in device = `{device}`.\n"
                f"Marqo tried to eject all the models on this device = `{device}` but still can't find enough space. \n"
                f"Please use a smaller model or increase the memory threshold.")

    Note: this function should only be called by `_update_available_models` for threading safeness.

    A function to detect if the device have enough memory to load the target model.
    If not, it will try to eject some models to spare the space.
    Args:
        model_name: The name of the model to load
        model_properties: The model properties of the model
        device: The target device to laod the model
    Returns:
        True we have enough space for the model
        Raise an error and return False if we can't find enough space for the model.
    
src/marqo/inference/native_inference/load_model.py