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.defs import (
26     AttributeProto,
27     OpSchema,
28     get_all_schemas_with_history,
29 )
30 
31 class OpSchemaEncoder(json.JSONEncoder):
32     def default(self, obj):
33         if isinstance(obj, OpSchema):
34             return {
35                 "file": obj.file,
36                 "line": obj.line,
37                 "support_level": obj.support_level.name,
38                 "doc": obj.doc,
39                 "since_version": obj.since_version,
40                 "deprecated": obj.deprecated,
41                 "domain": obj.domain,
42                 "name": obj.name,
43                 "min_input": obj.min_input,
44                 "max_input": obj.max_input,
45                 "min_output": obj.min_output,
46                 "max_output": obj.max_output,
47                 "attributes": obj.attributes,
48                 "inputs": obj.inputs,
49                 "outputs": obj.outputs,
50                 "type_constraints": obj.type_constraints,
51                 "has_function": obj.has_function,
52                 "has_context_dependent_function": obj.has_context_dependent_function,
53                 "has_data_propagation_function": obj.has_data_propagation_function,
54                 "has_type_and_shape_inference_function": obj.has_type_and_shape_inference_function,
55                 # @@@ useful to decode to Java ONNX model and then Java source
56                 # "function_body": obj.function_body.__str__()
57             }
58         elif isinstance(obj, OpSchema.FormalParameter):
59             return {
60                 "name": obj.name,
61                 # @@@ Convert to array of string, but might not be needed, see type_constraints
62                 "types": obj.types.__str__(),
63                 "type_str": obj.type_str,
64                 "description": obj.description,
65                 "option": obj.option.name,
66                 "min_arity": obj.min_arity,
67                 "is_homogeneous": obj.is_homogeneous,
68                 "differentiation_category": obj.differentiation_category.name,
69             }
70         elif isinstance(obj, OpSchema.Attribute):
71             return {
72                 "name": obj.name,
73                 "description": obj.description,
74                 "type": obj.type.name,
75                 # @@@ extract default value from protobuf
76                 "default_value": obj.default_value,
77                 "required": obj.required,
78             }
79         elif isinstance(obj, AttributeProto):
80             if obj.type == AttributeProto.INT:
81                 return obj.i;
82             elif obj.type == AttributeProto.FLOAT:
83                 return obj.f;
84             elif obj.type == AttributeProto.STRING:
85                 return obj.s.decode();
86             else:
87                 return None;
88         elif isinstance(obj, OpSchema.TypeConstraintParam):
89             return {
90                 "type_param_str": obj.type_param_str,
91                 "description": obj.description,
92                 "allowed_type_strs": obj.allowed_type_strs,
93             }
94         return super().default(obj)
95 
96 schemas: list[OpSchema] = get_all_schemas_with_history()
97 
98 json = json.dumps(schemas, cls=OpSchemaEncoder, indent=4)
99 print(json)