chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:47:05 +08:00
commit 4f3b7da785
7394 changed files with 2005594 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
Onnx op definition loading
---------------------------------
Setup
-------
Use anaconda and install onnx:
```
conda install onnx
```
Generate a file
---------------------
```
python onnx_def_gen.py
```
This will generate a file with all op definitions
loadable as NodeProto in onnx serialized as a text file
split by --\n.
@@ -0,0 +1,82 @@
import onnx
from onnx import version_converter, helper, ModelProto
# Referenced from: https://github.com/onnx/onnx/issues/2660#issuecomment-605874784
def add_value_info_for_constants(model : onnx.ModelProto):
"""
Currently onnx.shape_inference doesn't use the shape of initializers, so add
that info explicitly as ValueInfoProtos.
Mutates the model.
Args:
model: The ModelProto to update.
"""
# All (top-level) constants will have ValueInfos before IRv4 as they are all inputs
if model.ir_version < 4:
return
def add_const_value_infos_to_graph(graph : onnx.GraphProto):
inputs = {i.name for i in graph.input}
existing_info = {vi.name: vi for vi in graph.value_info}
for init in graph.initializer:
# Check it really is a constant, not an input
if init.name in inputs:
continue
# The details we want to add
elem_type = init.data_type
shape = init.dims
# Get existing or create new value info for this constant
vi = existing_info.get(init.name)
if vi is None:
vi = graph.value_info.add()
vi.name = init.name
# Even though it would be weird, we will not overwrite info even if it doesn't match
tt = vi.type.tensor_type
if tt.elem_type == onnx.TensorProto.UNDEFINED:
tt.elem_type = elem_type
if not tt.HasField("shape"):
# Ensure we set an empty list if the const is scalar (zero dims)
tt.shape.dim.extend([])
for dim in shape:
tt.shape.dim.add().dim_value = dim
graph_input = graph.input.add()
graph_input.name = vi.name
graph_input.type.tensor_type.elem_type = elem_type
# Handle subgraphs
for node in graph.node:
for attr in node.attribute:
# Ref attrs refer to other attrs, so we don't need to do anything
if attr.ref_attr_name != "":
continue
if attr.type == onnx.AttributeProto.GRAPH:
add_const_value_infos_to_graph(attr.g)
if attr.type == onnx.AttributeProto.GRAPHS:
for g in attr.graphs:
add_const_value_infos_to_graph(g)
return add_const_value_infos_to_graph(model.graph)
def summarize_model(input: ModelProto):
return f'Inputs {len(input.graph.input)} Nodes {len(input.graph.node)} Initializer {len(input.graph.initializer)} Value info {len(input.graph.value_info)}'
model = onnx.load('C:\\Users\\agibs\\Downloads\\V9\\V9\\best_bracket.onnx')
kotlin_model = onnx.load('C:\\Users\\agibs\\Documents\\GitHub\\dl4j-PR-split\\deeplearning4j\\nd4j\\samediff-import\\samediff-import-onnx\\input-adjusted-model.onnx')
input_names_2 = [node.name for node in kotlin_model.graph.node]
input_init__names_2 = [initializer.name for initializer in kotlin_model.graph.initializer]
#model = onnx.shape_inference.infer_shapes(model)
add_value_info_for_constants(model)
input_names = [node.name for node in model.graph.node]
input_init__names = [initializer.name for initializer in model.graph.initializer]
input_val_info__names = [value_info.name for value_info in model.graph.value_info]
converted_model = version_converter.convert_version(kotlin_model, 13)
converted_input_val_info__names = [value_info.name for value_info in converted_model.graph.value_info]
converted_node_names = [node.name for node in converted_model.graph.node]
onnx.save(converted_model,'output.onnx')
print('Converted model')
+137
View File
@@ -0,0 +1,137 @@
# /* ******************************************************************************
# *
# *
# * This program and the accompanying materials are made available under the
# * terms of the Apache License, Version 2.0 which is available at
# * https://www.apache.org/licenses/LICENSE-2.0.
# *
# * See the NOTICE file distributed with this work for additional
# * information regarding copyright ownership.
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# * License for the specific language governing permissions and limitations
# * under the License.
# *
# * SPDX-License-Identifier: Apache-2.0
# ******************************************************************************/
from onnx.defs import get_all_schemas
from onnx import NodeProto,GraphProto
from google.protobuf import text_format
import onnx.helper
nodes = []
schemas = get_all_schemas()
def load_node(input_str):
"""
Return a node
:param input_str:
:return:
"""
node_proto = NodeProto()
text_format.Parse(input_str,node_proto)
return node_proto
# default values for each type for serialization
def convert_attr_type_to_enum(attr_value):
"""
Pass in an attribute from OpDescriptor and
get back out the equivalent enum value
for conversion to an attribute proto.
:param attr_value: the attribute value
:return:
"""
if str(attr_value.type) == 'AttrType.INTS':
return 7
elif str(attr_value.type) == 'AttrType.UNDEFINED':
return 0
elif str(attr_value.type) == 'AttrType.FLOATS':
return 6
elif str(attr_value.type) == 'AttrType.GRAPH':
return 5
elif str(attr_value.type) == 'AttrType.GRAPHS':
return 10
elif str(attr_value.type) == 'AttrType.INT':
return 2
elif str(attr_value.type) == 'AttrType.STRING':
return 3
elif str(attr_value.type) == 'AttrType.TENSOR':
return 4
elif str(attr_value.type) == 'AttrType.TENSORS':
return 9
elif str(attr_value.type) == 'AttrType.SPARSE_TENSOR':
return 11
elif str(attr_value.type) == 'AttrType.SPARSE_TENSORS':
return 12
elif str(attr_value.type) == 'AttrType.FLOAT':
return 1
elif str(attr_value.type) == 'AttrType.STRINGS':
return 8
else:
raise Exception('Invalid type passed in')
def create_node_from_schema(schema):
"""
Convert an OpSchema to a NodeProto
:param schema: the input OpSchema
:return: the equivalent NodeProto
"""
node_proto = NodeProto()
for attribute in schema.attributes:
attr_value = schema.attributes[attribute]
if attr_value.default_value.name == '':
attr_value_new = onnx.helper.make_attribute(attr_value.name,'')
attr_value_new.type = convert_attr_type_to_enum(attr_value)
node_proto.attribute.append(attr_value_new)
else:
node_proto.attribute.append(attr_value.default_value)
node_proto.op_type = schema.name
node_proto.doc_string = schema.doc
node_proto.name = schema.name
for input_arr in schema.inputs:
input_types = input_arr.types
type_attr = onnx.helper.make_attribute(input_arr.name + '-types', [str(data_type).replace('tensor(', '').replace(')', '') for data_type in input_types])
node_proto.attribute.append(type_attr)
if node_proto.input is None:
node_proto.input = []
node_proto.input.append(input_arr.name)
for output_arr in schema.outputs:
if node_proto.output is None:
node_proto.output = []
output_types = output_arr.types
type_attr = onnx.helper.make_attribute(output_arr.name + '-types',
[str(data_type).replace('tensor(', '').replace(')', '') for data_type
in output_types])
node_proto.attribute.append(type_attr)
node_proto.output.append(output_arr.name)
return node_proto
nodes = [create_node_from_schema(schema) for schema
in sorted(schemas, key=lambda s: s.name)]
graph_proto = GraphProto()
graph_proto.node.extend(nodes)
text_proto = text_format.MessageToString(graph_proto)
with open('onnx-op-defs.pb', 'wb') as f:
f.write(graph_proto.SerializeToString())
with open('onnx-op-def.pbtxt','w+') as f:
f.write(text_proto)
# for node in nodes:
# message_to_string = text_format.MessageToString(node, as_utf8=True)
# node_2 = load_node(message_to_string)
# f.write(message_to_string + '----f\n')
# with open('onnx.pbtxt','r') as f:
# nodes = [load_node(node_str) for node_str in f.read().split('----f\n')]
# print(nodes)
+29
View File
@@ -0,0 +1,29 @@
# /* ******************************************************************************
# *
# *
# * This program and the accompanying materials are made available under the
# * terms of the Apache License, Version 2.0 which is available at
# * https://www.apache.org/licenses/LICENSE-2.0.
# *
# * See the NOTICE file distributed with this work for additional
# * information regarding copyright ownership.
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# * License for the specific language governing permissions and limitations
# * under the License.
# *
# * SPDX-License-Identifier: Apache-2.0
# ******************************************************************************/
import tensorflow as tf
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
def graph_as_func():
S = tf.Variable(tf.constant([1, 2, 3, 4]))
result = tf.scatter_add(S, [0], [10])
return result
a_function_that_uses_a_graph = tf.function(graph_as_func)
print(a_function_that_uses_a_graph.__attr__)
converted = convert_variables_to_constants_v2(a_function_that_uses_a_graph)
print(type(converted))
+20
View File
@@ -0,0 +1,20 @@
# /* ******************************************************************************
# *
# *
# * This program and the accompanying materials are made available under the
# * terms of the Apache License, Version 2.0 which is available at
# * https://www.apache.org/licenses/LICENSE-2.0.
# *
# * See the NOTICE file distributed with this work for additional
# * information regarding copyright ownership.
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# * License for the specific language governing permissions and limitations
# * under the License.
# *
# * SPDX-License-Identifier: Apache-2.0
# ******************************************************************************/
import onnx
loaded = onnx.load('lenet.onnx')
+35
View File
@@ -0,0 +1,35 @@
# /* ******************************************************************************
# *
# *
# * This program and the accompanying materials are made available under the
# * terms of the Apache License, Version 2.0 which is available at
# * https://www.apache.org/licenses/LICENSE-2.0.
# *
# * See the NOTICE file distributed with this work for additional
# * information regarding copyright ownership.
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# * License for the specific language governing permissions and limitations
# * under the License.
# *
# * SPDX-License-Identifier: Apache-2.0
# ******************************************************************************/
from onnx_tf.common import attr_converter,attr_translator
from onnx_tf.handlers.backend import *
import onnx_tf
import onnx_tf.handlers.handler
import sys,inspect
import tensorflow as tf
from onnx_tf.backend import TensorflowBackend
current_module = sys.modules['onnx_tf.handlers.backend']
modules = inspect.getmembers(current_module)
for name, obj in modules:
obj_modules = inspect.getmembers(obj)
for name2,module2 in obj_modules:
if inspect.isclass(module2):
result = module2
print(module2)