Skip to content

Commit 62627cb

Browse files
committed
Cleanup irbuilder
Signed-off-by: Ganesan Ramalingam <[email protected]>
1 parent 86e10dd commit 62627cb

File tree

4 files changed

+8
-57
lines changed

4 files changed

+8
-57
lines changed

onnxscript/converter.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def set_type_info(value: ir.Value, typeinfo: ta.TypeAnnotationValue) -> None:
120120
value.type = type_and_shape.type
121121
value.shape = type_and_shape.shape
122122
except AttributeError:
123+
# TODO: This needs to be fixed.
123124
pass
124125
value.meta["typeinfo"] = typeinfo
125126

@@ -166,7 +167,6 @@ def __init__(
166167
source: Optional[str] = None,
167168
default_opset: Optional[values.Opset] = None,
168169
):
169-
self.ir_builder = irbuilder.IRBuilder()
170170
self.source = source
171171
if global_names is not None:
172172
# We make a copy in case function eval modifies it.
@@ -1115,7 +1115,7 @@ def ret(exp, i, suffix):
11151115
t = None
11161116
else:
11171117
t = self.returntype[i]
1118-
self._current_fn.append_output(
1118+
self._current_fn.outputs.append(
11191119
make_value(return_var.name, t, self._source_of(stmt))
11201120
)
11211121
return return_var
@@ -1324,7 +1324,7 @@ def _translate_loop_stmt(self, loop_stmt: Union[ast.For, ast.While]) -> None:
13241324
[],
13251325
)
13261326

1327-
self._current_fn.append_output(
1327+
self._current_fn.outputs.append(
13281328
make_value(
13291329
o_cond_out,
13301330
onnx_types.BOOL,
@@ -1342,7 +1342,7 @@ def _translate_loop_stmt(self, loop_stmt: Union[ast.For, ast.While]) -> None:
13421342
ov = self._emit_copy(ov, pv)
13431343
# TODO: retrieve variable type for the annotation if any.
13441344
typeinfo = None
1345-
self._current_fn.append_output(
1345+
self._current_fn.outputs.append(
13461346
make_value(ov.name, typeinfo, self._source_of(loop_stmt))
13471347
)
13481348
body = self._exit_scope()
@@ -1389,7 +1389,7 @@ def _translate_block(
13891389
# To return an outer-scope variable, an ONNX Graph has to
13901390
# use an explicit copy via Identity.
13911391
output = self._emit_copy(output, pvar)
1392-
self._current_fn.append_output(
1392+
self._current_fn.outputs.append(
13931393
make_value(
13941394
output.name,
13951395
pv_val.typeinfo,
@@ -1413,7 +1413,7 @@ def _translate_block(
14131413

14141414
# TODO: retrieve the annotation if any.
14151415
typeinfo = None
1416-
self._current_fn.append_output(make_value(ovar.name, typeinfo, source))
1416+
self._current_fn.outputs.append(make_value(ovar.name, typeinfo, source))
14171417
graph = self._exit_scope()
14181418
return graph.graph
14191419

onnxscript/evaluator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ def make_tensor_name() -> str:
447447
model = onnx.helper.make_model( # noqa: TID251
448448
graph,
449449
opset_imports=[opset_id],
450-
ir_version=irbuilder.select_ir_version(schema.since_version, domain=schema.domain),
450+
ir_version=values.select_ir_version(schema.since_version, domain=schema.domain),
451451
)
452452
model = onnx.shape_inference.infer_shapes(model)
453453

onnxscript/irbuilder.py

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,8 @@
1616

1717
logger = logging.getLogger("onnxscript")
1818

19-
20-
def select_ir_version(version: int, domain: str = "") -> int:
21-
"""Selects a suitable ONNX ir_version for a given opset version."""
22-
if domain == "":
23-
domain = "ai.onnx"
24-
if (domain, version) not in onnx.helper.OP_SET_ID_VERSION_MAP:
25-
return max(
26-
v for k, v in onnx.helper.OP_SET_ID_VERSION_MAP.items() if k[0] == "ai.onnx"
27-
)
28-
return onnx.helper.OP_SET_ID_VERSION_MAP[domain, version]
29-
30-
3119
TypeAnnotationValue = onnxscript.type_annotation.TypeAnnotationValue
3220

33-
3421
class IRFunction(ir.Function):
3522
"""Represents a function in the IR."""
3623

@@ -43,11 +30,6 @@ def __init__(self, name: str, domain: str = "") -> None:
4330
self.nested_functions: dict[str, IRFunction] = {}
4431
self.outer_scope_variables: dict[Any, Any] = {}
4532

46-
@property
47-
def docstring(self) -> str:
48-
"""Returns the docstring of this function."""
49-
return self.doc_string or ""
50-
5133
@property
5234
def assigned_names(self) -> Sequence[str]:
5335
"""Returns the list of variables assigned to by this function."""
@@ -84,9 +66,6 @@ def append_parameter(self, parameter: ir.Value | ir.Attr) -> None:
8466
raise TypeError(f"Expected ir.Value or ir.Attr, got {type(parameter)}")
8567
self.attributes.add(parameter)
8668

87-
def append_output(self, var: ir.Value) -> None:
88-
self.outputs.append(var)
89-
9069
def add_nested_function(self, fun: IRFunction) -> None:
9170
self.nested_functions[fun.name] = fun
9271

@@ -117,31 +96,3 @@ def to_function_proto(self) -> onnx.FunctionProto:
11796
"""Converts this instance into a `onnx.FunctionProto`."""
11897
return ir.to_proto(self)
11998

120-
121-
# IRBuilder: abstracts out details of the IR in the python-to-IR converter
122-
123-
124-
def set_type_info(value: ir.Value, typeinfo: TypeAnnotationValue) -> None:
125-
"""Sets the type information on an IR value."""
126-
try:
127-
type_and_shape = ir.from_proto(typeinfo.to_type_proto())
128-
value.type = type_and_shape.type
129-
value.shape = type_and_shape.shape
130-
except AttributeError:
131-
pass
132-
value.meta["typeinfo"] = typeinfo
133-
134-
135-
def make_value(
136-
varname: str, typeinfo: TypeAnnotationValue, sourceinfo: SourceInfo
137-
) -> ir.Value:
138-
value = ir.Value(name=varname)
139-
value.meta.setdefault("sourceinfo", sourceinfo)
140-
if typeinfo is not None:
141-
set_type_info(value, typeinfo)
142-
return value
143-
144-
145-
class IRBuilder:
146-
def __init__(self):
147-
self.functions = {}

onnxscript/values.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ def _op_schema_from_function_ir(
457457
function_ir.name,
458458
opset.domain,
459459
since_version=opset.version,
460-
doc=function_ir.docstring,
460+
doc=function_ir.doc_string or "",
461461
inputs=formal_inputs,
462462
outputs=formal_outputs,
463463
type_constraints=[constraint.as_tuple() for constraint in type_to_constraint.values()],

0 commit comments

Comments
 (0)