pytorch/test/onnx/expect/TestOperators.test_chunk.expect
Thiago Crepaldi 4f4e2c1c08 Add constant node sizes to proto size calculation (#111097)
Fixes #110982

https://github.com/pytorch/pytorch/pull/62257 deprecated `torch.onnx.export(use_external_data_format: bool=...)`  argument, but it seems the introduced `EncoderBase::GetGraphProtoSize` has a bug and doesn't detect models > 2GB when onnx Constant nodes are large (and responsible for the size overflow)

This PR adds the constant node to the total size of the model, along with initializers.

In python, what we need to do is:

```python
import onnx

def compute_tensor_size(tensor):
    # Compute the size of the tensor based on its shape and data type
    size = tensor.size * tensor.itemsize
    return size

def sum_constant_and_initializer_sizes(model_path):
    # Load the ONNX model
    model = onnx.load(model_path)

    total_size = 0
    initializer_size = 0
    constant_size = 0

    # Compute the size of constant nodes
    for node in model.graph.node:
        if node.op_type == 'Constant':
            constant_value = node.attribute[0].t
            # Convert constant value to numpy array
            constant_array = onnx.numpy_helper.to_array(constant_value)
            # Compute the size of the constant tensor
            tensor_size = compute_tensor_size(constant_array)
            total_size += tensor_size
            constant_size += tensor_size

    # Compute the size of initializer nodes that are not graph inputs
    for initializer in model.graph.initializer:
        if initializer.name not in [input.name for input in model.graph.input]:
            # Convert the shape and data type information to calculate size
            # tensor = onnx.helper.tensor_value_info_to_tensor(input)
            tensor = onnx.numpy_helper.to_array(initializer)
            tensor_size = compute_tensor_size(tensor)
            total_size += tensor_size
            initializer_size += tensor_size

    return total_size, constant_size, initializer_size

model_path = '/path/to/model.onnx'
total_size, constant_size, initializer_size = sum_constant_and_initializer_sizes(model_path)

print("Total size of constant nodes in bytes:", constant_size)
print("Total size of initializer nodes (excluding graph inputs) in bytes:", initializer_size)
print("Total size of constant and initializer nodes (excluding graph inputs) in bytes:", total_size)
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/111097
Approved by: https://github.com/justinchuby, https://github.com/zhipenghan
2023-10-14 00:37:02 +00:00

196 lines
3.3 KiB
Text

ir_version: 8
producer_name: "pytorch"
producer_version: "CURRENT_VERSION"
graph {
node {
input: "onnx::Shape_0"
output: "onnx::Gather_1"
name: "Shape_6"
op_type: "Shape"
}
node {
output: "onnx::Gather_2"
name: "Constant_7"
op_type: "Constant"
attribute {
name: "value"
t {
dims: 1
data_type: 7
raw_data: "\000\000\000\000\000\000\000\000"
}
type: TENSOR
}
}
node {
input: "onnx::Gather_1"
input: "onnx::Gather_2"
output: "onnx::Add_3"
name: "Gather_8"
op_type: "Gather"
attribute {
name: "axis"
i: 0
type: INT
}
}
node {
output: "onnx::Slice_4"
name: "Constant_9"
op_type: "Constant"
attribute {
name: "value"
t {
dims: 1
data_type: 7
raw_data: "\000\000\000\000\000\000\000\000"
}
type: TENSOR
}
}
node {
output: "onnx::Add_5"
name: "Constant_10"
op_type: "Constant"
attribute {
name: "value"
t {
dims: 1
data_type: 7
raw_data: "\001\000\000\000\000\000\000\000"
}
type: TENSOR
}
}
node {
input: "onnx::Add_3"
input: "onnx::Add_5"
output: "onnx::Div_6"
name: "Add_11"
op_type: "Add"
}
node {
output: "onnx::Div_7"
name: "Constant_12"
op_type: "Constant"
attribute {
name: "value"
t {
dims: 1
data_type: 7
raw_data: "\002\000\000\000\000\000\000\000"
}
type: TENSOR
}
}
node {
input: "onnx::Div_6"
input: "onnx::Div_7"
output: "onnx::Mul_8"
name: "Div_13"
op_type: "Div"
}
node {
output: "onnx::Mul_9"
name: "Constant_14"
op_type: "Constant"
attribute {
name: "value"
t {
dims: 1
data_type: 7
raw_data: "\001\000\000\000\000\000\000\000"
}
type: TENSOR
}
}
node {
input: "onnx::Mul_8"
input: "onnx::Mul_9"
output: "onnx::Slice_10"
name: "Mul_15"
op_type: "Mul"
}
node {
input: "onnx::Shape_0"
input: "onnx::Slice_4"
input: "onnx::Slice_10"
input: "onnx::Gather_2"
output: "11"
name: "Slice_16"
op_type: "Slice"
}
node {
output: "onnx::Mul_12"
name: "Constant_17"
op_type: "Constant"
attribute {
name: "value"
t {
dims: 1
data_type: 7
raw_data: "\002\000\000\000\000\000\000\000"
}
type: TENSOR
}
}
node {
input: "onnx::Mul_8"
input: "onnx::Mul_12"
output: "onnx::Slice_13"
name: "Mul_18"
op_type: "Mul"
}
node {
input: "onnx::Shape_0"
input: "onnx::Slice_10"
input: "onnx::Slice_13"
input: "onnx::Gather_2"
output: "14"
name: "Slice_19"
op_type: "Slice"
}
name: "main_graph"
input {
name: "onnx::Shape_0"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 3
}
}
}
}
}
output {
name: "11"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 2
}
}
}
}
}
output {
name: "14"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
}
}
}
}
}
opset_import {
version: 17
}