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

Init Config from Python: typer.Option default value not working #10727

Closed
janvdp opened this issue Apr 28, 2022 · 3 comments · Fixed by #10788
Closed

Init Config from Python: typer.Option default value not working #10727

janvdp opened this issue Apr 28, 2022 · 3 comments · Fixed by #10788
Labels
bug Bugs and behaviour differing from documentation feat / cli Feature: Command-line interface feat / config Feature: Training config

Comments

@janvdp
Copy link

janvdp commented Apr 28, 2022

Goal

Create the config.cfg from Python and not from the command line.

From the source:
https://github.com/explosion/spaCy/blob/master/spacy/cli/init_config.py#L28

How to reproduce the behaviour

Preparation

from pathlib import Path
from spacy.cli.init_config import init_config_cli, Optimizations

my_config = Path("./my_config.cfg")

This should work, ...

# This code 
init_config_cli(output_file=my_config, lang=lang, pipeline="ner",optimize=Optimizations.efficiency) 

..., however:

To generate a more effective transformer-based config (GPU-only),
install the spacy-transformers package and re-run this command. The config
generated now does not use transformers.
---------------------------------------------------------------------------
UndefinedError                            Traceback (most recent call last)
/tmp/ipykernel_664/2418944264.py in <module>
      3 
      4 #init_config_cli(output_file=my_config, lang=lang, pipeline="ner",optimize=Optimizations.efficiency, gpu=False, pretraining=False)
----> 5 init_config_cli(output_file=my_config, lang=lang, pipeline="ner",optimize=Optimizations.efficiency) #, gpu=False, pretraining=False)
      6 
      7 

/opt/conda/lib/python3.8/site-packages/spacy/cli/init_config.py in init_config_cli(output_file, lang, pipeline, optimize, gpu, pretraining, force_overwrite)
     60             exits=1,
     61         )
---> 62     config = init_config(
     63         lang=lang,
     64         pipeline=pipeline,

/opt/conda/lib/python3.8/site-packages/spacy/cli/init_config.py in init_config(lang, pipeline, optimize, gpu, pretraining, silent)
    171         )
    172         variables["transformer_data"] = None
--> 173     base_template = template.render(variables).strip()
    174     # Giving up on getting the newlines right in jinja for now
    175     base_template = re.sub(r"\n\n\n+", "\n\n", base_template)

/opt/conda/lib/python3.8/site-packages/jinja2/environment.py in render(self, *args, **kwargs)
   1289             return concat(self.root_render_func(ctx))  # type: ignore
   1290         except Exception:
-> 1291             self.environment.handle_exception()
   1292 
   1293     async def render_async(self, *args: t.Any, **kwargs: t.Any) -> str:

/opt/conda/lib/python3.8/site-packages/jinja2/environment.py in handle_exception(self, source)
    923         from .debug import rewrite_traceback_stack
    924 
--> 925         raise rewrite_traceback_stack(source=source)
    926 
    927     def join_path(self, template: str, parent: str) -> str:

<template> in top-level template code()

/opt/conda/lib/python3.8/site-packages/jinja2/environment.py in getitem(self, obj, argument)
    453         """Get an item or attribute of an object but prefer the item."""
    454         try:
--> 455             return obj[argument]
    456         except (AttributeError, TypeError, LookupError):
    457             if isinstance(argument, str):

UndefinedError: 'None' has no attribute 'efficiency'

The problem

The problem is related to typer.Option: fastapi/typer#106, which does not set the correct default value for gpu and pretraining:

gpu: bool = Opt(False, "--gpu", "-G", help="Whether the model can run on GPU. This will impact the choice of architecture, pretrained weights and related hyperparameters."),
pretraining: bool = Opt(False, "--pretraining", "-pt", help="Include config for pretraining (with 'spacy pretrain')"),
(I'm not sure why this does work from the command line.

E.g.: if you print the params gpu and pretraining, you get

<typer.models.OptionInfo object at 0x7f7eb4592430>
<typer.models.OptionInfo object at 0x7f7eb4592f10>

instead of their similar default value False.

Workaround

As a workaround, I force the value of gpu and pretraining:

init_config_cli(output_file=my_config, lang=lang, pipeline="ner",optimize=Optimizations.efficiency, gpu=False, pretraining=False)

Your Environment

  • spaCy version: 3.2.4
  • Platform: Linux-5.4.0-109-generic-x86_64-with-glibc2.17
  • Python version: 3.8.12
@polm polm added feat / cli Feature: Command-line interface feat / config Feature: Training config labels May 2, 2022
@rmitsch
Copy link
Contributor

rmitsch commented May 11, 2022

Yeah, that should be fixed. Thanks for doing some digging already, @janvdp! We'll look into this.

@rmitsch rmitsch added the bug Bugs and behaviour differing from documentation label May 11, 2022
@adrianeboyd adrianeboyd linked a pull request May 17, 2022 that will close this issue
3 tasks
@rmitsch
Copy link
Contributor

rmitsch commented Jun 17, 2022

#10788 tackles this by introducing default values for init_config() (which are in synch with those from init_config_cli()). So if want to initialize your config from Python code, please use init_config().

@github-actions
Copy link
Contributor

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bugs and behaviour differing from documentation feat / cli Feature: Command-line interface feat / config Feature: Training config
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants