KULeuven-MICAS / zigzag-imc

HW accelerator mapping optimization framework for in-memory computing
BSD 3-Clause "New" or "Revised" License
16 stars 2 forks source link

How to interpret spatial_mapping and temporal_mapping in the output #10

Closed Z-KN closed 6 months ago

Z-KN commented 7 months ago

This is the output from the default run. I wonder how to understand the output. It seems it only has mapping for inputs. Could you explain more about values in spatial mapping and temporal mapping? What about weights? How could this output file describe the overall execution process for all applications?

"inputs": {
   ...
      "spatial_mapping": {
            "spatial_mapping": {
                "O": [
                    [
                        [
                            "FX",
                            10.0
                        ],
                        [
                            "FY",
                            2.0
                        ]
                    ],
                    [
                        [
                            "K",
                            4.0
                        ]
                    ],
                    []
                ],
                "W": [
                    [],
                    [
                        [
                            "FX",
                            10.0
                        ],
                        [
                            "FY",
                            2.0
                        ],
                        [
                            "K",
                            4.0
                        ]
                    ],
                    []
                ],
                "I": [
                    [
                        [
                            "K",
                            4.0
                        ]
                    ],
                    [
                        [
                            "FX",
                            10.0
                        ],
                        [
                            "FY",
                            2.0
                        ]
                    ],
                    []
                ]
            }
        },
        "temporal_mapping": {
            "temporal_mapping": {
                "O": [
                    [],
                    [
                        [
                            "OY",
                            5
                        ],
                        [
                            "OY",
                            5
                        ],
                        [
                            "OX",
                            5
                        ],
                        [
                            "K",
                            4
                        ],
                        [
                            "K",
                            4
                        ],
                        [
                            "FY",
                            2
                        ]
                    ]
                ],
                "W": [
                    [
                        [
                            "OY",
                            5
                        ],
                        [
                            "OY",
                            5
                        ],
                        [
                            "OX",
                            5
                        ]
                    ],
                    [
                        [
                            "K",
                            4
                        ],
                        [
                            "K",
                            4
                        ],
                        [
                            "FY",
                            2
                        ]
                    ]
                ],
                "I": [
                    [],
                    [
                        [
                            "OY",
                            5
                        ],
                        [
                            "OY",
                            5
                        ],
                        [
                            "OX",
                            5
                        ],
                        [
                            "K",
                            4
                        ],
                        [
                            "K",
                            4
                        ],
                        [
                            "FY",
                            2
                        ]
                    ]
                ]
            }
}
JiacongSun commented 7 months ago

Hi Z-KN,

That is good question!

Just for others' reference, I suppose the output is clipped from file outputs/Dimc-ds_cnn-layer_LayerNode_0_complete.json, by running the following command:

python main_onnx.py --model zigzag/inputs/examples/workload/mlperf_tiny/ds_cnn.onnx --accelerator zigzag.inputs.examples.hardware.Dimc --mapping zigzag.inputs.examples.mapping.default_imc

For your questions:

The spatial mapping and temporal mapping are internally generated by ZigZag-IMC, and the format here aligns with the public ZigZag. As the mapper runs before the cost model executes, the mapping is placed under the input category in the output JSON file.

Regarding the content of the spatial mapping, it mirrors the definition in the input file but is structured based on hardware levels rather than dimensions. For each layer operand (i.e., O, W, I), each bracket corresponds to a hardware level, starting from the compute unit level to the top memory level for that operand. The spatial mapping indicates the parallelism of the operand on each hardware level.

Take the "O" in the spatial mapping as an example:

If any bracket is empty, it implies that each port of the upper level is connected to one port at the lower level. For example, the weight has 3 brackets, representing compute unit -> weight cells -> DRAM. Since the first bracket is empty, it means each weight cell is connected to a single compute unit.

Regarding the temporal mapping, each bracket corresponds to a memory level, starting from the bottom memory level to the top memory level related to that operand. Note the compute-unit level is excluded for the temporal mapping.

Take the "O" in the temporal mapping as an example:

Take the "W" as an example:

Combining the temporal mapping size with the spatial mapping size, you can exactly get the same layer size as defined in the onnx file.

If you're unfamiliar with the loop representation based on memory level, please refer to the ZigZag paper for a detailed explanation.

Let me know if there is still any confusion.

Regards, Jiacong

Z-KN commented 7 months ago

Thank you for your detailed explanation. So in a high-level view, spatial mapping is mapping elements spatially to existing hardware at a time, while temporal mapping means updating elements time by time. Is that right?

Another question is, as

the mapping is placed under the input category

so mapping is also an input. The default IMC mapping is

mapping = {
    "default": {
        "core_allocation": 1,
        # "spatial_mapping": {"D1": ("OX", 25), "D2": (("FX", 3), ("FY", 3))},
        "memory_operand_links": {"O": "O", "W": "I2", "I": "I1"},
        "spatial_mapping_hint": {"D1": ["K", "OX"], "D2": ["C", "FX", "FY"]},
    }
}

Does it mean users need to specify its mapping as input? I thought Zigzag will search the best mapping given an architecture. So architecture is an input, while mapping is an output. Is it the case?

JiacongSun commented 7 months ago

Regaridng your first question: yes, your understanding is totally correct!

Regarding your second question: The mapping file is necessary for ZigZag-IMC, as it not only defines the spatial mapping but also specifies the core allocation and the links between layer operands and memory operands. However, the spatial mapping definition is not necessary.

As for your requirement, ZigZag-IMC supports multiple use cases: