Skip to content

Commit

Permalink
feat(hugr-py): use serialized extensions in python (#1459)
Browse files Browse the repository at this point in the history
Copies standard extensions to hugr-py source tree, as poetry won't
include anything from outside the tree (not symlinks either).

Existing op/type wrappers now just point to loaded extensions.

Closes #1450
  • Loading branch information
ss2165 committed Aug 21, 2024
1 parent d6edcd7 commit a61f4df
Show file tree
Hide file tree
Showing 21 changed files with 4,602 additions and 52 deletions.
9 changes: 5 additions & 4 deletions hugr-py/src/hugr/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from dataclasses import dataclass, field
from functools import cached_property
from typing import TYPE_CHECKING, Protocol, TypeVar, runtime_checkable
from typing import TYPE_CHECKING, ClassVar, Protocol, TypeVar, runtime_checkable

from typing_extensions import Self

Expand Down Expand Up @@ -398,11 +398,12 @@ class RegisteredOp(AsExtOp):
"""

#: Known operation definition.
const_op_def: ext.OpDef # must be initialised by register_op
const_op_def: ClassVar[ext.OpDef] # may be set by registered_op decorator.

def op_def(self) -> ext.OpDef:
@classmethod
def op_def(cls) -> ext.OpDef:
# override for AsExtOp.op_def
return self.const_op_def
return cls.const_op_def


@dataclass()
Expand Down
2 changes: 1 addition & 1 deletion hugr-py/src/hugr/serialization/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class OpDef(ConfiguredBaseModel, populate_by_name=True):
misc: dict[str, Any] | None = None
signature: PolyFuncType | None = None
binary: bool = False
lower_funcs: list[FixedHugr]
lower_funcs: list[FixedHugr] = pd.Field(default_factory=list)

def deserialize(self, extension: ext.Extension) -> ext.OpDef:
return extension.add_op_def(
Expand Down
12 changes: 12 additions & 0 deletions hugr-py/src/hugr/std/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
"""Types and operations from the standard extension set."""

import pkgutil

from hugr.ext import Extension
from hugr.serialization.extension import Extension as PdExtension


def _load_extension(name: str) -> Extension:
replacement = name.replace(".", "/")
json_str = pkgutil.get_data(__name__, f"_json_defs/{replacement}.json")
assert json_str is not None
return PdExtension.model_validate_json(json_str).deserialize()
2 changes: 2 additions & 0 deletions hugr-py/src/hugr/std/_json_defs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The files in this directory are generated and copied in, don't edit by hand. See
repository justfile `gen-extensions` command.
222 changes: 222 additions & 0 deletions hugr-py/src/hugr/std/_json_defs/arithmetic/conversions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
{
"version": "0.1.0",
"name": "arithmetic.conversions",
"extension_reqs": [
"arithmetic.float.types",
"arithmetic.int.types"
],
"types": {},
"values": {},
"operations": {
"convert_s": {
"extension": "arithmetic.conversions",
"name": "convert_s",
"description": "signed int to float",
"signature": {
"params": [
{
"tp": "BoundedNat",
"bound": 7
}
],
"body": {
"input": [
{
"t": "Opaque",
"extension": "arithmetic.int.types",
"id": "int",
"args": [
{
"tya": "Variable",
"idx": 0,
"cached_decl": {
"tp": "BoundedNat",
"bound": 7
}
}
],
"bound": "C"
}
],
"output": [
{
"t": "Opaque",
"extension": "arithmetic.float.types",
"id": "float64",
"args": [],
"bound": "C"
}
],
"extension_reqs": []
}
},
"binary": false
},
"convert_u": {
"extension": "arithmetic.conversions",
"name": "convert_u",
"description": "unsigned int to float",
"signature": {
"params": [
{
"tp": "BoundedNat",
"bound": 7
}
],
"body": {
"input": [
{
"t": "Opaque",
"extension": "arithmetic.int.types",
"id": "int",
"args": [
{
"tya": "Variable",
"idx": 0,
"cached_decl": {
"tp": "BoundedNat",
"bound": 7
}
}
],
"bound": "C"
}
],
"output": [
{
"t": "Opaque",
"extension": "arithmetic.float.types",
"id": "float64",
"args": [],
"bound": "C"
}
],
"extension_reqs": []
}
},
"binary": false
},
"trunc_s": {
"extension": "arithmetic.conversions",
"name": "trunc_s",
"description": "float to signed int",
"signature": {
"params": [
{
"tp": "BoundedNat",
"bound": 7
}
],
"body": {
"input": [
{
"t": "Opaque",
"extension": "arithmetic.float.types",
"id": "float64",
"args": [],
"bound": "C"
}
],
"output": [
{
"t": "Sum",
"s": "General",
"rows": [
[
{
"t": "Opaque",
"extension": "arithmetic.int.types",
"id": "int",
"args": [
{
"tya": "Variable",
"idx": 0,
"cached_decl": {
"tp": "BoundedNat",
"bound": 7
}
}
],
"bound": "C"
}
],
[
{
"t": "Opaque",
"extension": "prelude",
"id": "error",
"args": [],
"bound": "C"
}
]
]
}
],
"extension_reqs": []
}
},
"binary": false
},
"trunc_u": {
"extension": "arithmetic.conversions",
"name": "trunc_u",
"description": "float to unsigned int",
"signature": {
"params": [
{
"tp": "BoundedNat",
"bound": 7
}
],
"body": {
"input": [
{
"t": "Opaque",
"extension": "arithmetic.float.types",
"id": "float64",
"args": [],
"bound": "C"
}
],
"output": [
{
"t": "Sum",
"s": "General",
"rows": [
[
{
"t": "Opaque",
"extension": "arithmetic.int.types",
"id": "int",
"args": [
{
"tya": "Variable",
"idx": 0,
"cached_decl": {
"tp": "BoundedNat",
"bound": 7
}
}
],
"bound": "C"
}
],
[
{
"t": "Opaque",
"extension": "prelude",
"id": "error",
"args": [],
"bound": "C"
}
]
]
}
],
"extension_reqs": []
}
},
"binary": false
}
}
}
Loading

0 comments on commit a61f4df

Please sign in to comment.