CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
108 lines
3.8 KiB
Python
108 lines
3.8 KiB
Python
## Copyright © Advanced Micro Devices, Inc., or its affiliates.
|
|
## SPDX-License-Identifier: MIT
|
|
|
|
import torch
|
|
from tensor import TensorAttributes, dump_data_as_binary, load_data_from_binary
|
|
from common import DTypeConverter, NODE_REGISTRY
|
|
from batchnorm_inference import BatchnormInference
|
|
from itertools import chain
|
|
import json
|
|
import os
|
|
|
|
|
|
def node_from_dict(node_dict: dict, tensors: dict[int, TensorAttributes]):
|
|
if node_dict["type"] not in NODE_REGISTRY.keys():
|
|
raise RuntimeError("Unsupported node type: " + node_dict["type"])
|
|
|
|
return NODE_REGISTRY[node_dict["type"]].from_dict(node_dict, tensors)
|
|
|
|
|
|
class Graph:
|
|
def __init__(
|
|
self,
|
|
nodes,
|
|
dtype: torch.dtype = None,
|
|
io_data_type: torch.dtype = None,
|
|
compute_data_type: torch.dtype = None,
|
|
intermediate_data_type: torch.dtype = None,
|
|
name: str = "",
|
|
):
|
|
if dtype != None and (
|
|
io_data_type == None
|
|
and compute_data_type == None
|
|
and intermediate_data_type == None
|
|
):
|
|
io_data_type = compute_data_type = intermediate_data_type = dtype
|
|
elif dtype != None:
|
|
raise ValueError(
|
|
"type must be set, or all of io_data_type, compute_data_type and intermediate_data_type must be"
|
|
)
|
|
|
|
self.nodes = nodes
|
|
self.tensors = dict()
|
|
for node in nodes:
|
|
inputs = node.inputs.__dict__.values()
|
|
outputs = node.outputs.__dict__.values()
|
|
tensors = filter(
|
|
lambda x: type(x) == TensorAttributes, chain(inputs, outputs)
|
|
)
|
|
self.tensors.update({tensor.uid: tensor for tensor in tensors})
|
|
|
|
self.io_data_type = io_data_type
|
|
self.compute_data_type = compute_data_type
|
|
self.intermediate_data_type = intermediate_data_type
|
|
self.name = name
|
|
|
|
# TODO Implement execute for more complicated graphs
|
|
# def execute():
|
|
|
|
def as_dict(self):
|
|
tensors = [tensor.as_dict() for tensor in self.tensors.values()]
|
|
return {
|
|
"nodes": [node.as_dict() for node in self.nodes],
|
|
"tensors": tensors,
|
|
"io_data_type": DTypeConverter.to_string(self.io_data_type),
|
|
"compute_data_type": DTypeConverter.to_string(self.compute_data_type),
|
|
"intermediate_data_type": DTypeConverter.to_string(
|
|
self.intermediate_data_type
|
|
),
|
|
"name": self.name,
|
|
}
|
|
|
|
def save(self, base_filename: str):
|
|
with open(base_filename + ".json", "w") as file:
|
|
json.dump(self.as_dict(), file)
|
|
for tensor in self.tensors.values():
|
|
dump_data_as_binary(
|
|
"{}.tensor{}.bin".format(base_filename, tensor.uid), tensor
|
|
)
|
|
|
|
@staticmethod
|
|
def from_file(base_path: str):
|
|
with open(base_path + ".json") as file:
|
|
d = json.load(file)
|
|
|
|
tensor_list = [
|
|
TensorAttributes.from_dict(tensorDict) for tensorDict in d["tensors"]
|
|
]
|
|
tensor_map = {tensor.uid: tensor for tensor in tensor_list}
|
|
|
|
nodes = [node_from_dict(node_dict, tensor_map) for node_dict in d["nodes"]]
|
|
|
|
for uid, tensor in tensor_map.items():
|
|
filepath = "{}.tensor{}".format(base_path, uid)
|
|
if os.path.exists(filepath):
|
|
load_data_from_binary(filepath, tensor)
|
|
|
|
io_data_type = DTypeConverter.from_string(d["io_data_type"])
|
|
compute_data_type = DTypeConverter.from_string(d["compute_data_type"])
|
|
intermediate_data_type = DTypeConverter.from_string(d["intermediate_data_type"])
|
|
|
|
return Graph(
|
|
nodes,
|
|
io_data_type=io_data_type,
|
|
compute_data_type=compute_data_type,
|
|
intermediate_data_type=intermediate_data_type,
|
|
name=d["name"],
|
|
)
|