Skip to content

Proposal: Registry Pattern for mapping between f and jet_f #84

@TimSiebert1

Description

@TimSiebert1

We could use the following registry that stores our f->jet_f

@dataclass
class JetRegistry:
    _map: dict[Callable, Callable] = field(default_factory=dict)
    _frozen: bool = False

    def register(self, *funcs: Callable, allow_override: bool = False):
        def deco(jet_f: Callable):
            for f in funcs:
                if not allow_override and f in self._map:
                    raise RuntimeError(
                        f"Jet for {getattr(f,'__name__',f)} already exists"
                    )
                self._map[f] = jet_f
            return jet_f
        return deco

    def get(self, f: Callable) -> Callable:
        return self._map[f]

    def mapping(self) -> dict[Callable, Callable]:
        return dict(self._map)  # copy

""" 
maybe also the possibility to freeze the registry
    def freeze(self):
        self._frozen = True
"""

If JET = JetRegistry(), users could extend the registry using

@JET.register(torch.sin, math.sin)
def jet_sin(
    s: PrimalAndCoefficients, K: int, is_taylor: tuple[bool, ...]
) -> ValueAndCoefficients:
    pass

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions