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

Error when using absl flags with sacred #855

Open
kirtanp opened this issue Jan 4, 2022 · 4 comments
Open

Error when using absl flags with sacred #855

kirtanp opened this issue Jan 4, 2022 · 4 comments

Comments

@kirtanp
Copy link

kirtanp commented Jan 4, 2022

I am trying to use an absl flag flag in a sacred script. Here is a minimal example

from sacred import Experiment
from absl import flags, app
flags.DEFINE_bool("local", False,
                  "Runs without SEML if True")
FLAGS = flags.FLAGS
flags.FLAGS.mark_as_parsed()

ex = Experiment()

if(FLAGS.local):
  ex.add_config("config.yaml")

@ex.automain
def main(_config):
  print("abcdwhatever")

My use case is that I want a global flag that can be passed to the python script to decide whether to read config from the yaml file or not.

The issue is that when I call the script python3 script.py --local=True. I get the error

Usage:
  dummy [(with UPDATE...)] [options]
  dummy help [COMMAND]
  dummy (-h | --help)
  dummy COMMAND [(with UPDATE...)] [options]

I got the same error when I tried to use argparse instead of absl, and looking at this (slightly related) issue #709, it seems to be caused by post_process_name and/or join_paths. I don't know if this is a bug or expected behaviour, but in any case my questions is: Is it possible to use a global absl or argparse flag along with sacred?

@Jarvis73
Copy link

Jarvis73 commented Jan 6, 2022

@kirtanp
Sacred configs should be defined inside the script under a function decorated by @ex.config

@ex.config
def config():
    key1 = value1
    key2 = value2
    key3 = value3

According to the Doc, you can update predefined configs by an extra config file as follow

python3 script.py with config.yaml

or directly provide new values

python3 script.py with key1=new_value

Command-line inputs of scripts running with sacred(i.e., @ex.automain) will be managed by sacred. So I think the argparse and absl flag are both not working here.

@thequilo
Copy link
Collaborator

thequilo commented Jan 6, 2022

Hello @kirtanp and a happy new year! I'll add to @Jarvis73's answer:

You can define command-line flags in sacred if you really need to. But for loading config files I would recommend the already mentioned python script.py with config.yaml.

@kirtanp
Copy link
Author

kirtanp commented Jan 7, 2022

Thanks @Jarvis73 and @thequilo ! To explain a bit more, the reason I was doing this was that I am using SEML, which builds on top of Sacred but uses a different way of getting the config params.

However, when I run the script locally I just want to use Sacred and add the params through a yaml file, which is just a single line change in the script. I just wanted a global flag which would define whether I want to make this single line change in a sacred script of not.

I thought that it would not make sense to have 'local' as part of sacred config or decorated by ex.config because the sacred config is not globally accessible. That is, I can only access the sacred config inside a function decorated by ex.config (and in this case I can't do ex.add_config("config.yaml") inside a function decorated by ex.config) or within the automain. And it wasn't clear that the config could be updated from within the automain. So a global flag sitting outside the sacred config seems like the cleanest solution. I feel like there could also be other applications where it would be useful to have a globally available 'meta-config' which does not store any parameters for the experiment but for how the script should run, and it doesn't seem to be possible easily.

In any case, for me the solution with python3 script.py with config.yaml should work perfectly fine! Thanks for your help!

@Guptajakala
Copy link

It'd be nicer if sacred could be dropped-in without modifying too much code. I'm working on top of a repo which extensively uses argparse...

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

4 participants