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

feat: graph space simplification #151

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions neps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
from neps.plot.tensorboard_eval import tblogger
from neps.search_spaces import (
Architecture,
ArchitectureParameter,
Categorical,
CategoricalParameter,
Constant,
ConstantParameter,
Float,
FloatParameter,
Function,
FunctionParameter,
GraphGrammar,
Integer,
IntegerParameter,
Expand All @@ -25,7 +22,6 @@
"Categorical",
"Constant",
"Function",
"ArchitectureParameter",
"CategoricalParameter",
"ConstantParameter",
"FloatParameter",
Expand Down
4 changes: 0 additions & 4 deletions neps/search_spaces/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from neps.search_spaces.architecture.api import (
Architecture,
ArchitectureParameter,
Function,
FunctionParameter,
)
from neps.search_spaces.architecture.graph_grammar import (
CoreGraphGrammar,
Expand All @@ -26,7 +23,6 @@

__all__ = [
"Architecture",
"ArchitectureParameter",
"Categorical",
"CategoricalParameter",
"Constant",
Expand Down
239 changes: 91 additions & 148 deletions neps/search_spaces/architecture/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,155 +51,98 @@ def _build(graph, set_recursive_attribute):
graph.edges[e].update(set_recursive_attribute(op_name, predecessor_values))


def Architecture(**kwargs):
"""Factory function."""
if "structure" not in kwargs:
raise ValueError("Factory function requires structure")
if not isinstance(kwargs["structure"], list) or len(kwargs["structure"]) == 1:
base = GraphGrammar

class _FunctionParameter(base):
def __init__(
self,
structure: Grammar
| list[Grammar]
| ConstrainedGrammar
| list[ConstrainedGrammar]
| str
| list[str]
| dict
| list[dict],
primitives: dict,
# TODO: Follow this rabbit hole for `constraint_kwargs`,
# it can all be deleted my friend
constraint_kwargs: dict | None = None,
name: str = "ArchitectureParameter",
set_recursive_attribute: Callable | None = None,
**kwargs,
):
local_vars = locals()
self.input_kwargs = {
args: local_vars[args]
for args in inspect.getfullargspec(self.__init__).args # type: ignore[misc]
if args != "self"
}
self.input_kwargs.update(**kwargs)

if isinstance(structure, list):
structures = [
_dict_structure_to_str(
st,
primitives,
repetitive_mapping=kwargs.get(
"terminal_to_sublanguage_map", None
),
)
if isinstance(st, dict)
else st
for st in structure
]
_structures = []
for st in structures:
if isinstance(st, str):
if constraint_kwargs is None:
_st = Grammar.fromstring(st)
else:
_st = ConstrainedGrammar.fromstring(st)
_st.set_constraints(**constraint_kwargs)
_structures.append(_st) # type: ignore[has-type]
structures = _structures

super().__init__(
grammars=structures,
terminal_to_op_names=primitives,
edge_attr=False,
**kwargs,
class Architecture(GraphGrammar):
def __init__(
self,
structure: Grammar | ConstrainedGrammar | str | dict
| list[Grammar | ConstrainedGrammar | str | dict],
primitives: dict,
# TODO: Follow this rabbit hole for `constraint_kwargs`,
# it can all be deleted my friend
constraint_kwargs: dict | None = None,
name: str = "Architecture",
set_recursive_attribute: Callable | None = None,
**kwargs,
):
local_vars = locals()
self.input_kwargs = {
args: local_vars[args]
for args in inspect.getfullargspec(self.__init__).args # type: ignore[misc]
if args != "self"
}
self.input_kwargs.update(**kwargs)

if isinstance(structure, list):
structures = [
_dict_structure_to_str(
st,
primitives,
repetitive_mapping=kwargs.get(
"terminal_to_sublanguage_map", None
),
)
else:
if isinstance(structure, dict):
structure = _dict_structure_to_str(structure, primitives)

if isinstance(structure, str):
if isinstance(st, dict)
else st
for st in structure
]
_structures = []
for st in structures:
if isinstance(st, str):
if constraint_kwargs is None:
structure = Grammar.fromstring(structure)
_st = Grammar.fromstring(st)
else:
structure = ConstrainedGrammar.fromstring(structure)
structure.set_constraints(**constraint_kwargs) # type: ignore[union-attr]

super().__init__(
grammar=structure, # type: ignore[arg-type]
terminal_to_op_names=primitives,
edge_attr=False,
**kwargs,
)

self._set_recursive_attribute = set_recursive_attribute
self.name: str = name

def to_pytorch(self) -> nn.Module:
self.clear_graph()
if len(self.nodes()) == 0:
composed_function = self.compose_functions()
# part below is required since PyTorch has no standard functional API
self.graph_to_self(composed_function)
self.prune_graph()

if self._set_recursive_attribute:
m = _build(self, self._set_recursive_attribute)

if m is not None:
return m

self.compile()
self.update_op_names()
return super().to_pytorch() # create PyTorch model

def create_new_instance_from_id(self, identifier: str):
g = Architecture(**self.input_kwargs) # type: ignore[arg-type]
g.load_from(identifier)
return g

return _FunctionParameter(**kwargs)


def ArchitectureParameter(**kwargs):
"""Deprecated: Use `Architecture` instead of `ArchitectureParameter`.

This function remains for backward compatibility and will raise a deprecation
warning if used.
"""
import warnings

warnings.warn(
(
"Usage of 'neps.ArchitectureParameter' is deprecated and will be removed in"
" future releases. Please use 'neps.Architecture' instead."
),
DeprecationWarning,
stacklevel=2,
)

return Architecture(**kwargs)


Function = Architecture


def FunctionParameter(**kwargs):
"""Deprecated: Use `Function` instead of `FunctionParameter`.

This function remains for backward compatibility and will raise a deprecation
warning if used.
"""
import warnings

warnings.warn(
(
"Usage of 'neps.FunctionParameter' is deprecated and will be removed in"
" future releases. Please use 'neps.Function' instead."
),
DeprecationWarning,
stacklevel=2,
)
_st = ConstrainedGrammar.fromstring(st)
_st.set_constraints(**constraint_kwargs)
_structures.append(_st) # type: ignore[has-type]
structures = _structures

super().__init__(
grammars=structures,
terminal_to_op_names=primitives,
edge_attr=False,
**kwargs,
)
else:
if isinstance(structure, dict):
structure = _dict_structure_to_str(structure, primitives)

if isinstance(structure, str):
if constraint_kwargs is None:
structure = Grammar.fromstring(structure)
else:
structure = ConstrainedGrammar.fromstring(structure)
structure.set_constraints(**constraint_kwargs) # type: ignore[union-attr]

super().__init__(
grammar=structure, # type: ignore[arg-type]
terminal_to_op_names=primitives,
edge_attr=False,
**kwargs,
)

self._set_recursive_attribute = set_recursive_attribute
self.name: str = name

def to_pytorch(self) -> nn.Module:
self.clear_graph()
if len(self.nodes()) == 0:
composed_function = self.compose_functions()
# part below is required since PyTorch has no standard functional API
self.graph_to_self(composed_function)
self.prune_graph()

if self._set_recursive_attribute:
m = _build(self, self._set_recursive_attribute)

if m is not None:
return m

self.compile()
self.update_op_names()
return super().to_pytorch() # create PyTorch model

def create_new_instance_from_id(self, identifier: str):
g = Architecture(**self.input_kwargs) # type: ignore[arg-type]
g.load_from(identifier)
return g

return Function(**kwargs)
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def run_pipeline(some_architecture, some_float, some_integer, some_cat):


pipeline_space = dict(
some_architecture=neps.Function(
some_architecture=neps.Architecture(
set_recursive_attribute=set_recursive_attribute,
structure=structure,
primitives=primitives,
Expand Down
2 changes: 1 addition & 1 deletion neps_examples/experimental/hierarchical_architecture.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def run_pipeline(architecture: neps.Function):


pipeline_space = dict(
architecture=neps.Function(
architecture=neps.Architecture(
set_recursive_attribute=set_recursive_attribute,
structure=structure,
primitives=primitives,
Expand Down