Project System¶
The project system is the central abstraction in Forgather. A Project resolves a configuration file through the template inheritance chain and provides access to all configured components.
Related documentation:
- Core Concepts — projects, templates, and the configuration pipeline
- Configuration Overview — template system and YAML configuration
- Syntax Reference — complete reference for line statements and YAML tags
- Low-level API — the API underlying the
Projectabstraction
Quick Example¶
from forgather.project import Project
proj = Project("train_tiny_llama.yaml")
# Materialize the full training script
training_script = proj()
# Materialize individual components
model_factory = proj("model")
train_dataset = proj("train_dataset")
model = model_factory()
forgather.project.Project
dataclass
¶
Central user-facing abstraction for a Forgather ML experiment.
A Project loads a YAML configuration file through a Jinja2 template
inheritance chain, parses it into a node graph, and can materialise any
named target from that graph into live Python objects. It is the primary
entry point for interactive experiment development and for training scripts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_name
|
str
|
Name of the configuration template to load (e.g. |
''
|
project_dir
|
str or PathLike
|
Path to the project directory. Must contain a |
'.'
|
**kwargs
|
Additional keyword arguments forwarded to the Jinja2 preprocessor as template variables. |
{}
|
Attributes:
| Name | Type | Description |
|---|---|---|
config_name |
str
|
Name of the selected configuration; automatically set to the project
default when config_name is empty or |
project_dir |
str
|
Absolute path to the project directory. |
meta |
MetaConfig
|
Parsed project metadata (search paths, default config, etc.). |
environment |
ConfigEnvironment
|
Jinja2 + YAML preprocessing environment used to load templates. |
config |
Any
|
The parsed node graph produced from the preprocessed YAML. |
pp_config |
str
|
The fully preprocessed YAML text (after Jinja2 rendering), useful for debugging template issues. |
Examples:
Load a project from the current directory using the default configuration:
Load a specific configuration and materialise individual targets:
>>> proj = Project("train_tiny_llama.yaml", "examples/tutorials/tiny_llama")
>>> model = proj("model")
>>> model, tokenizer = proj("model", "tokenizer")
Notes
When debugging a configuration it is usually easier to construct the project
incrementally for better diagnostic messages. See project_config.ipynb
for a step-by-step notebook example.
Source code in src/forgather/project.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
load_config(config_name, **kwargs)
¶
Load and parse the named configuration template.
Preprocesses the template through the Jinja2 environment, then parses
the resulting YAML into a node graph. The results are stored in
self.config and self.pp_config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_name
|
str
|
Name of the configuration template to load, relative to the
project's |
required |
**kwargs
|
Additional keyword arguments forwarded to the Jinja2 preprocessor as template variables. |
{}
|
Source code in src/forgather/project.py
add_template(name, data)
¶
Add an in-memory template definition to the Jinja2 loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Template name used to reference this template from other templates
(e.g. via |
required |
data
|
str
|
Raw template source text. |
required |
Source code in src/forgather/project.py
__call__(*args, asdict=False, **kwargs)
¶
Materialise one or more targets from the loaded configuration graph.
Each call traverses the node graph and constructs fresh Python objects for the requested targets. Calling this method multiple times will produce independent object instances; share a single call when you need objects that reference each other (e.g. model and optimizer sharing the same parameter tensors).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
str
|
Names of the output targets to build. When called with no
arguments (or with a single empty string), the |
()
|
asdict
|
bool
|
When |
False
|
**kwargs
|
Additional context variables forwarded to the graph materialisation engine. |
{}
|
Returns:
| Type | Description |
|---|---|
object
|
The materialised |
object
|
The single materialised target when exactly one name is given and
asdict is |
generator
|
A generator yielding the materialised targets in order when multiple
names are given and asdict is |
DotDict
|
A dot-accessible dictionary mapping every requested target name to
its materialised object when asdict is |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no configuration has been loaded (i.e. |
Examples:
Build the default main target:
Build a single named target:
Unpack multiple targets in one call (avoids duplicate construction):
Collect targets into a dot-accessible dict:
Source code in src/forgather/project.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
forgather.meta_config.MetaConfig
dataclass
¶
Project metadata loaded from meta.yaml.
MetaConfig reads and parses the meta.yaml file that sits at the
root of every Forgather project. It resolves template search paths,
locates the workspace root by walking up the directory tree, and exposes
the configuration values needed by :class:~forgather.project.Project to
set up its :class:ConfigEnvironment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_dir
|
str or PathLike
|
Path to the project directory containing |
'.'
|
meta_name
|
str
|
Name of the metadata file to load. Defaults to |
PROJECT_META_NAME
|
Attributes:
| Name | Type | Description |
|---|---|---|
project_dir |
str
|
Path to the project directory as supplied to |
name |
str
|
Name of the meta file (e.g. |
project_name |
str or None
|
Human-readable project name declared in |
description |
str or None
|
Short project description declared in |
meta_path |
str
|
Absolute path to the meta file. |
searchpath |
list of str
|
Ordered list of absolute directory paths searched for config templates.
Derived from the |
system_path |
str or None
|
Optional system-level template search path from |
config_prefix |
str
|
Sub-directory inside the search path where leaf configuration files
live. Defaults to |
default_cfg |
str or None
|
Name of the default configuration file as declared in |
config_dict |
dict
|
Raw dictionary parsed from |
workspace_root |
str
|
Absolute path to the workspace root directory (the directory that
contains |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the project directory does not exist, |
Examples:
>>> meta = MetaConfig("/path/to/my_project")
>>> print(meta.project_name)
My Project
>>> print(meta.searchpath)
['/path/to/my_project/templates', '/path/to/workspace/forgather_workspace']
Source code in src/forgather/meta_config.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | |
default_config()
¶
Return the name of the default configuration template.
Returns:
| Type | Description |
|---|---|
str
|
The value of |
Source code in src/forgather/meta_config.py
config_path(config_template=None)
¶
Return the template-relative path for the given configuration name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_template
|
str or None
|
Name of the configuration template (e.g. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Path of the form |
Source code in src/forgather/meta_config.py
find_templates(prefix='', suffix='.yaml')
¶
Iterate over all templates in the search path matching a prefix and suffix.
Walks every directory in :attr:searchpath, descending into the
sub-directory given by prefix, and yields (name, path) pairs for
every file whose name ends with suffix. Hidden directories
(names starting with ".") are skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
Sub-directory to search within each search-path entry. Defaults to
|
''
|
suffix
|
str
|
File extension filter. Defaults to |
'.yaml'
|
Yields:
| Name | Type | Description |
|---|---|---|
template_name |
str
|
Template name relative to the prefixed search directory, suitable
for use with :meth: |
template_path |
str
|
Filesystem path to the template file. |
Examples:
Find all templates under a models directory in any search-path entry:
>>> for template_name, template_path in meta.find_templates("models"):
... print(template_name, template_path)
Source code in src/forgather/meta_config.py
find_workspace_dir(project_dir)
staticmethod
¶
Walk up the directory tree to find the Forgather workspace root.
The workspace root is the nearest ancestor directory that contains a
forgather_workspace/ sub-directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_dir
|
str
|
Starting directory for the upward search. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Absolute path to the workspace root directory. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no |
Source code in src/forgather/meta_config.py
find_project_dir(project_dir)
staticmethod
¶
Walk up the directory tree to find the nearest Forgather project directory.
A project directory is one that directly contains a meta.yaml file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_dir
|
str
|
Starting directory for the upward search. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Absolute path to the nearest project directory that contains
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If no project directory is found at or above project_dir. |
Source code in src/forgather/meta_config.py
forgather.config.ConfigEnvironment
¶
Jinja2 preprocessing and YAML parsing environment for Forgather configurations.
ConfigEnvironment wraps a :class:~forgather.preprocess.PPEnvironment
(a customised Jinja2 environment) and a suite of custom YAML constructors
that translate !call, !singleton, !factory, !partial, and
!var tags into :class:~forgather.latent.Node objects. The result of
:meth:load is a :class:Config containing both the parsed node graph and
the preprocessed YAML text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
searchpath
|
str, os.PathLike, or iterable of str/PathLike
|
Directories searched for templates, in priority order. Non-existent
directories are silently ignored. Defaults to |
tuple('.')
|
pp_environment
|
Environment or None
|
A pre-configured Jinja2 environment to use instead of the default
:class: |
None
|
global_vars
|
dict or None
|
Variables injected into the Jinja2 global namespace and available in
every template. Merged with any variables already present in
pp_environment. Defaults to |
None
|
Examples:
>>> env = ConfigEnvironment(searchpath=["/path/to/templates"])
>>> config = env.load("configs/train.yaml")
>>> node_graph, pp_text = config.get()
Source code in src/forgather/config.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 | |
preprocess(config_path, /, **kwargs)
¶
Render a configuration template through Jinja2 and return the result.
Locates config_path in the search path, renders it with the configured global variables plus any extra kwargs, and returns the resulting YAML text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str or PathLike
|
Template path relative to the search path (e.g. |
required |
**kwargs
|
Additional keyword arguments passed as Jinja2 template variables, overriding globals for this render. |
{}
|
Returns:
| Type | Description |
|---|---|
ConfigText
|
The rendered YAML text. :class: |
Source code in src/forgather/config.py
preprocess_with_trace(config_path, /, **kwargs)
¶
Preprocess config_path and also return the per-template trace.
Runs :meth:preprocess inside :func:capture_pp so the second element
of the returned tuple is the ordered list of
(template_name, preprocessed_source) pairs that participated in the
render — the same data that pp_verbose prints to stdout, but
returned programmatically.
Returns:
| Type | Description |
|---|---|
(ConfigText, list[tuple[str, str]])
|
The fully rendered text plus the per-template trace (load order). |
Source code in src/forgather/config.py
preprocess_from_string(config, /, **kwargs)
¶
Render a configuration template supplied as a string through Jinja2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
str
|
Raw template source text (may contain Jinja2 directives). |
required |
**kwargs
|
Additional keyword arguments passed as Jinja2 template variables. |
{}
|
Returns:
| Type | Description |
|---|---|
ConfigText
|
The rendered YAML text. |
Source code in src/forgather/config.py
load(config_path, /, **kwargs)
¶
Preprocess and parse a configuration file into a node graph.
Combines :meth:preprocess and :meth:load_from_ppstring into a
single call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str or PathLike
|
Template path relative to the search path. |
required |
**kwargs
|
Additional Jinja2 template variables forwarded to :meth: |
{}
|
Returns:
| Type | Description |
|---|---|
Config
|
Container holding the parsed node graph and the preprocessed YAML text. |
Raises:
| Type | Description |
|---|---|
Exception
|
Any YAML or node-graph parse error, annotated with the numbered preprocessed source for easier debugging. |
Source code in src/forgather/config.py
load_from_string(config, /, **kwargs)
¶
Preprocess and parse a configuration supplied as a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
str
|
Raw template source text. |
required |
**kwargs
|
Additional Jinja2 template variables forwarded to
:meth: |
{}
|
Returns:
| Type | Description |
|---|---|
Config
|
Container holding the parsed node graph and the preprocessed YAML text. |
Source code in src/forgather/config.py
load_from_ppstring(pp_config)
¶
Parse an already-preprocessed YAML string into a node graph.
Parses pp_config with the custom YAML constructors (!call,
!singleton, !factory, !partial, !var, etc.) and
validates the resulting graph with :meth:~forgather.latent.Latent.check.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pp_config
|
str
|
Fully rendered YAML text (output of Jinja2 preprocessing). |
required |
Returns:
| Type | Description |
|---|---|
Config
|
Container holding the parsed node graph and pp_config. |
Raises:
| Type | Description |
|---|---|
Exception
|
Any YAML parse error or node-graph validation error, annotated with line-numbered source text. |
Source code in src/forgather/config.py
render_code(config_path, /, *, target='main', **kwargs)
¶
Render config_path as Python source via :func:forgather.codegen.generate_code.
Mirrors the forgather code CLI: preprocesses + parses the config,
looks up target (default "main") in the resulting node graph,
and runs the codegen template. When target is None the entire
config graph is rendered (useful for reviewing every materialisable
target in one document).
Raises:
| Type | Description |
|---|---|
PreprocessError
|
Jinja2 preprocessing failed (delegated from :meth: |
YamlParseError
|
The preprocessed text was not valid YAML. |
CodeGenError
|
target was not found in the config or codegen itself raised. |
Source code in src/forgather/config.py
find_referenced_templates(template_name, /, **kwargs)
¶
Iterate over the full template inheritance hierarchy for a given template.
Traces actual template loading at render time so that dynamic
extends / include references (those whose targets are computed
by Jinja2 expressions) are captured in addition to statically declared
ones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template_name
|
str or PathLike
|
Name of the root template to analyse (relative to the search path). |
required |
**kwargs
|
Forwarded to the inner :meth: |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
level |
int
|
Depth of this template in the hierarchy (0 = root). |
name |
str
|
Template name as it appears in the loader. |
filename |
str
|
Filesystem path to the template file. |
Source code in src/forgather/config.py
get_template_dependencies(template_name, /, **kwargs)
¶
Return raw dependency relationships for a template, suitable for graph generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template_name
|
str or PathLike
|
Name of the root template to analyse. |
required |
**kwargs
|
Forwarded to the inner :meth: |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
load_sequence |
list of tuple[str, str]
|
Ordered list of |
dependencies_dict |
dict[str, set[str]]
|
Mapping from each template name to the set of template names it
directly references (via |