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(hugr-py): use serialized extensions in python #1459

Merged
merged 4 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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
14 changes: 14 additions & 0 deletions hugr-py/src/hugr/std/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
"""Types and operations from the standard extension set."""

from pathlib import Path

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

_EXT_DIR = Path(__file__).parent / "_json_defs"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



def _load_extension(name: str) -> Extension:
replacement = name.replace(".", "/")
path = _EXT_DIR / f"{replacement}.json"
with path.open() as f:
return PdExtension.model_validate_json(f.read()).deserialize()
2 changes: 2 additions & 0 deletions hugr-py/src/hugr/std/_json_defs/README.md
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure if the jsons would be kept when building the wheels, but they are still there after running poetry build 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes sorry should have said I checked that

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
Loading