cellmap_flow.models.envs
Running a model in an environment of its own.
Some models need packages that cannot share cellmap-flow’s environment:
Cellpose 4 (Cellpose-SAM) wants a newer torch and numpy than the default
environment’s cellpose 3 allows, and transformer models pin their own
stacks. A model entry’s env says where such a model’s inference server,
and its finetuning job, run instead:
models:
my_model:
type: script
script_path: /path/to/my_model.py
env: cellpose4
env is one of:
the name of an environment in cellmap-flow’s
pixi.toml. The program runs aspixi run --frozen --manifest-path <pixi.toml> -e <name> ..., which installs the environment from the lockfile on first use (a server’s launcher installs it before submitting the job:jobs.launch._install_env_first). The manifest is the one in the checkout cellmap-flow is installed from, orCELLMAP_FLOW_PIXI_MANIFEST; pixi isPIXI_EXE(whichpixi runsets), else thepixion PATH.an absolute path to a conda environment or virtualenv that has cellmap-flow installed. The program runs as
<path>/bin/python -P -m <module>.
A value with a / in it, or starting with ~, is a path; anything
else is a pixi environment’s name.
A name that ~/.cellmap_flow/envs.yaml maps to a path is that path: an
alias, which wins over a pixi environment of the same name. It is how a
machine without pixi (a conda-only cluster account) supplies the
environments the model types default to:
cellpose4: /groups/lab/home/me/miniconda3/envs/cellpose4
The file is read each time it is needed, and CELLMAP_FLOW_ENVS_FILE
names another.
A model type can name the environment its models run in when the entry
gives none, as ModelConfig.default_env: a class attribute, or a
property that decides per model. The environment a model runs in
(model_env) is its entry’s env, else its type’s default_env,
else this one; env: current runs it in this one whatever its type’s
default. A default that cannot be used here falls back to this
environment with a warning (_usable_default says when and why), where
an explicit env that cannot be used is an error.
env is not a constructor argument of any model type, so it is not on
the model’s own form or command line: registry.build_model takes it
off the entry and sets ModelConfig.env, to_dict() and
launch_entry write it back (never the type’s default, so an exported
YAML says only what its author chose), and serving.launch takes it off
again to put the server in the environment, so the server never sees it.
Only the server and the trainer move. Everything else (the dashboard,
blockwise, infer --server-check) runs the model, when it runs it at
all, in the process’s own environment.
Attributes
Functions
|
Whether |
|
|
|
The aliases file's names and their paths, |
|
The pixi.toml whose environments |
|
Each environment |
|
The pixi to run: |
|
Whether this machine has a pixi to run ( |
|
|
|
The directory |
|
Whether |
|
Whether this process already runs in |
|
The environment a model runs in, or None for this one. |
|
|
|
|
|
Whether a |
|
The default environment of |
|
|
|
The pixi environment |
|
The program that serves a model in |
|
The python of |
|
|
|
The lib directory of |
|
Why the trainer cannot run in |
Module Contents
- cellmap_flow.models.envs.logger
- cellmap_flow.models.envs.PIXI_MANIFEST_ENV = 'CELLMAP_FLOW_PIXI_MANIFEST'
- cellmap_flow.models.envs.ALIASES_FILE_ENV = 'CELLMAP_FLOW_ENVS_FILE'
- cellmap_flow.models.envs.CURRENT = 'current'
- cellmap_flow.models.envs.is_path(env: str) bool
Whether
envnames a directory rather than a pixi environment.
- cellmap_flow.models.envs.aliases_file() pathlib.Path
CELLMAP_FLOW_ENVS_FILE, else~/.cellmap_flow/envs.yaml.
- cellmap_flow.models.envs.aliases() Dict[str, str]
The aliases file’s names and their paths,
~expanded; {} without one.Read on each call, so an alias added while the dashboard runs is used by the next launch.
- Raises:
ConfigError – the file is not a mapping of names to paths.
- cellmap_flow.models.envs.pixi_manifest() pathlib.Path
The pixi.toml whose environments
envnames.CELLMAP_FLOW_PIXI_MANIFEST, else the one at the root of the checkout this package is installed from (pixi installs it editable from there).
- cellmap_flow.models.envs.pixi_environments(manifest: pathlib.Path) Dict[str, dict]
Each environment
manifestdeclares, as a table withfeatures.defaultis always there, as pixi has it even when it is not declared. The list form (name = ["feature", ...]) becomes{"features": [...]}.
- cellmap_flow.models.envs.pixi_program() str
The pixi to run:
PIXI_EXE, else the one on PATH, else"pixi".
- cellmap_flow.models.envs.has_pixi() bool
Whether this machine has a pixi to run (
PIXI_EXEor one on PATH).
- cellmap_flow.models.envs.validate(env, model_name: str = 'model') str
envchecked, with a path’s~expanded; what the model keeps.An alias stays a name, so an exported YAML names the environment the same way on every machine, and each machine’s aliases file says where it is.
currentis kept as it is.- Raises:
ConfigError –
envis not a string, a path (an alias’s included) is relative or has nobin/python, or no pixi.toml declares an environment by that name.
- cellmap_flow.models.envs.prefix(env: str) str
The directory
envis installed in.For a pixi environment that is where pixi installs it by default,
.pixi/envs/<name>beside the manifest; a pixi configured with detached environments puts it elsewhere.
- cellmap_flow.models.envs.is_installed(env: str) bool
Whether
envhas a python yet (a pixi one is installed on first use).
- cellmap_flow.models.envs.is_running_in(env: str) bool
Whether this process already runs in
env: the server or trainer a model was moved into, where building the model is what it is there for.
- cellmap_flow.models.envs.effective(env, default_env, model_name: str = 'model') str | None
The environment a model runs in, or None for this one.
envis the entry’s own (validated when the entry was read): itself, unless it iscurrent. Without one,default_env, its type’s, when it can be used here (_usable_default).
- cellmap_flow.models.envs.model_env(model_config) str | None
effectiveof a model config’senvanddefault_env.
- cellmap_flow.models.envs.declared_default(cls)
cls.default_envas the class declares it: a name, None, or the property (or other descriptor) that decides it per model.
- cellmap_flow.models.envs.decides_per_model(declared) bool
Whether a
declared_defaultis decided per model, not one name.
- cellmap_flow.models.envs.type_default(cls) str | None
The default environment of
cls’s models when it is one for all of them, without building one; None when it has none or decides per model.
- cellmap_flow.models.envs.install_argv(env: str) List[str]
pixi installofenvfrom pixi.lock as it is (--frozen), aspixi run --frozenwould install it, so the lock is never rewritten.
- cellmap_flow.models.envs.pixi_env_of(argv: List[str]) str | None
The pixi environment
argvruns in when it isserver_argv’spixi run ... -e <env> ..., else None.
- cellmap_flow.models.envs.server_argv(env: str) List[str]
The program that serves a model in
env, before its--modeland-d.In a pixi environment that is the environment’s
cellmap_flow serve, as the deployment’s own CELLMAP_FLOW_SERVER_COMMAND is. A directory need not be on PATH, so there it is its python running the CLI; -P keeps the job’s working directory off sys.path, so a job started inside another checkout does not run that checkout’s cellmap_flow.
- cellmap_flow.models.envs.python(env: str) List[str]
The python of
env: a directory’sbin/python, orpixi run ... python.
- cellmap_flow.models.envs.python_argv(env: str, module: str) List[str]
python -P -m <module>inenv.
- cellmap_flow.models.envs.lib_dir(env: str) str
The lib directory of
env, for LD_LIBRARY_PATH.A pixi environment’s is in its default place (
prefix); with detached environments it is elsewhere, and the job then loads libraries aspixi runalone would.
- cellmap_flow.models.envs.finetune_problem(env: str) str | None
Why the trainer cannot run in
env, or None when it can (as far as can be told cheaply).A pixi environment is checked against the manifest: one of its features, the default feature included unless it sets
no-default-feature, must install peft. A directory (a path or an alias) is not checked; a trainer that cannot import there fails in its job’s log.