1 #
2 # Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 #
5 # This code is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License version 2 only, as
7 # published by the Free Software Foundation.
8 #
9 # This code is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 # version 2 for more details (a copy is included in the LICENSE file that
13 # accompanied this code).
14 #
15 # You should have received a copy of the GNU General Public License version
16 # 2 along with this work; if not, write to the Free Software Foundation,
17 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 #
19 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 # or visit www.oracle.com if you need additional information or have any
21 # questions.
22
23 import json
24
25 from onnx import AttributeProto
26 from onnx.defs import OpSchema, get_all_schemas_with_history
27
28 class OpSchemaEncoder(json.JSONEncoder):
29 def default(self, obj):
30 if isinstance(obj, OpSchema):
31 return {
32 "file": obj.file,
33 "line": obj.line,
34 "support_level": obj.support_level.name,
35 "doc": obj.doc,
36 "since_version": obj.since_version,
37 "deprecated": obj.deprecated,
38 "domain": obj.domain,
39 "name": obj.name,
40 "min_input": obj.min_input,
41 "max_input": obj.max_input,
42 "min_output": obj.min_output,
43 "max_output": obj.max_output,
44 "attributes": obj.attributes,
45 "inputs": obj.inputs,
46 "outputs": obj.outputs,
47 "type_constraints": obj.type_constraints,
48 "has_function": obj.has_function,
49 "has_context_dependent_function": obj.has_context_dependent_function,
50 "has_data_propagation_function": obj.has_data_propagation_function,
51 "has_type_and_shape_inference_function": obj.has_type_and_shape_inference_function,
52 # @@@ useful to decode to Java ONNX model and then Java source
53 # "function_body": obj.function_body.__str__()
54 }
55 elif isinstance(obj, OpSchema.FormalParameter):
56 return {
57 "name": obj.name,
58 # @@@ Convert to array of string, but might not be needed, see type_constraints
59 "types": obj.types.__str__(),
60 "type_str": obj.type_str,
61 "description": obj.description,
62 "option": obj.option.name,
63 "min_arity": obj.min_arity,
64 "is_homogeneous": obj.is_homogeneous,
65 "differentiation_category": obj.differentiation_category.name,
66 }
67 elif isinstance(obj, OpSchema.Attribute):
68 return {
69 "name": obj.name,
70 "description": obj.description,
71 "type": obj.type.name,
72 # @@@ extract default value from protobuf
73 "default_value": obj.default_value,
74 "required": obj.required,
75 }
76 elif isinstance(obj, AttributeProto):
77 if obj.type == AttributeProto.INT:
78 return obj.i;
79 elif obj.type == AttributeProto.FLOAT:
80 return obj.f;
81 elif obj.type == AttributeProto.STRING:
82 return obj.s.decode();
83 else:
84 return None;
85 elif isinstance(obj, OpSchema.TypeConstraintParam):
86 return {
87 "type_param_str": obj.type_param_str,
88 "description": obj.description,
89 "allowed_type_strs": obj.allowed_type_strs,
90 }
91 return super().default(obj)
92
93 schemas: list[OpSchema] = get_all_schemas_with_history()
94
95 json = json.dumps(schemas, cls=OpSchemaEncoder, indent=4)
96 print(json)