Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The loggingLevel setup in Transformers-CFG will polute the user program #50

Closed
Saibo-creator opened this issue Jun 4, 2024 · 1 comment

Comments

@Saibo-creator
Copy link
Collaborator

The probelmatic line is here

logging.basicConfig(level=log_level)

reproduce

import torch
import logging  

if __name__ == "__main__":


    from transformers import AutoModelForCausalLM, AutoTokenizer
    from transformers_cfg.grammar_utils import IncrementalGrammarConstraint
    from transformers_cfg.generation.logits_process import GrammarConstrainedLogitsProcessor


    # logging.basicConfig has already been called in the transformers_cfg library, so this will not work
    # the above function can only be called once in a python session
    # so we should NEVER call logging.basicConfig in a library! 
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)

    # you will not see this message in the console because the logging level is set to WARNING inside the transformers_cfg library
    logger.info("Loading model and tokenizer")

Something which works is below:

import torch
import logging  

if __name__ == "__main__":

    logging.basicConfig(level=logging.INFO)

    from transformers import AutoModelForCausalLM, AutoTokenizer
    # the function call inside the transformers_cfg will not work because we have already called logging.basicConfig above
    from transformers_cfg.grammar_utils import IncrementalGrammarConstraint
    from transformers_cfg.generation.logits_process import GrammarConstrainedLogitsProcessor



    logger = logging.getLogger(__name__)

    # you will SEE this log message because the logging level is set to INFO
    logger.info("Loading model and tokenizer")

Reason

import os
import logging


def setup_logging():
    log_level_name = os.getenv(
        "TCFG_LOG_LEVEL", "WARNING"
    ).upper()  # Default to WARNING if not set
    log_levels = {
        "DEBUG": logging.DEBUG,
        "INFO": logging.INFO,
        "WARNING": logging.WARNING,
        "ERROR": logging.ERROR,
        "CRITICAL": logging.CRITICAL,
    }
    log_level = log_levels.get(log_level_name, logging.WARNING)
   # this is BAD, this function configures the root logger of the entire program, so it will overwrite the user's logging level defined in their script when they import the library
    logging.basicConfig(level=log_level)

Solution

import logging

# Create a logger for the library
logger = logging.getLogger('transformers_cfg')
# the level will propagate to loggers with submodule scope
logger.setLevel(logging.WARNING)
@Saibo-creator
Copy link
Collaborator Author

solved in #51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant