diff --git a/src/mldebug/layer_info.py b/src/mldebug/layer_info.py index 6901595..c075394 100644 --- a/src/mldebug/layer_info.py +++ b/src/mldebug/layer_info.py @@ -144,17 +144,55 @@ def _pick_instantiation(funcs, kname): "superkernel_reducemean_templated", ] +# Bytes per element for buffer_info dtypes (non-X2; X2 sizes are already in bytes) SIZE_BYTES = { - "bfloat16": 2, - "float16": 2, "int8_t": 1, + "int8": 1, "uint8_t": 1, + "uint8": 1, "int16_t": 2, - "uint16": 1, # Size of x2 designs is already in bytes + "int16": 2, "uint16_t": 2, + "uint16": 2, + "bfloat16": 2, + "bfloat16_t": 2, + "bf16": 2, + "float16": 2, + "float16_t": 2, + "fp16": 2, + "half": 2, + "int32_t": 4, + "int32": 4, + "uint32_t": 4, + "uint32": 4, + "float": 4, + "float32": 4, + "fp32": 4, + "int64_t": 8, + "int64": 8, + "uint64_t": 8, + "uint64": 8, + "double": 8, + "float64": 8, } +def get_size_bytes(dtype, x2=False): + """ + Bytes per element for a buffer_info dtype, or None when dtype is absent. + Unknown dtypes are fatal so a buffer is never dumped with the wrong size. + """ + if x2: + return 1 + if dtype is None: + return None + if dtype not in SIZE_BYTES: + raise RuntimeError( + f"Unknown buffer dtype '{dtype}' in buffer_info. Add its size to SIZE_BYTES." + ) + return SIZE_BYTES[dtype] + + @dataclass class L1Buffer: """ @@ -187,7 +225,16 @@ class Buffer: """ def __init__( - self, entry, buf_type, size_shift, aie_iface, ifm=False, ofm=False, wts=False, tg=False + self, + entry, + buf_type, + size_shift, + aie_iface, + ifm=False, + ofm=False, + wts=False, + tg=False, + x2=False, ): """ Initializes a Buffer object that stores L1 and L2 buffer mappings. @@ -201,6 +248,7 @@ def __init__( ofm (bool, optional): Is output feature map buffer. wts (bool, optional): Is weights buffer. tg (bool, optional): Built from the mladf report for a templated graph layer. + x2 (bool, optional): X2 design; sizes are already in bytes. """ self.l1 = None self.l2 = [] @@ -209,7 +257,7 @@ def __init__( self.ofm = ofm self.wts = wts self.tg = tg - size_shift = self._get_size_shift(entry, size_shift) + size_shift = self._get_size_shift(entry, size_shift, x2) # pong can be missing (no double buffering) if "l1_ping" in entry: @@ -256,22 +304,22 @@ def __init__( # LOGGER.verbose_print(split_msg) buf_id += 1 - def _get_size_shift(self, entry, size_shift): + def _get_size_shift(self, entry, size_shift, x2=False): """ Get the size in bytes for the specific buffer dtype, falling back to size_shift. Args: entry (dict): Buffer metadata dictionary with optional 'dtype'. size_shift (int): Fallback size shift if dtype absent. + x2 (bool): X2 design; sizes are already in bytes. Returns: int: Calculated byte shift for buffer data. """ # dtype is a per-buffer param - dtype = entry.get("dtype") - # int8 and uint8 are 1 byte - if dtype is not None: - return SIZE_BYTES.get(dtype, 1) + size_bytes = get_size_bytes(entry.get("dtype"), x2) + if size_bytes is not None: + return size_bytes # size shift is a global param # size_shift 0 => int8 # size_shift 1 => bf16 @@ -346,6 +394,7 @@ def __init__( mladf_report, num_batches=1, device_batch_size=1, + x2=False, ): """ Initialize a Layer object using given metadata, populating buffer and kernel/stamp lists. @@ -362,7 +411,9 @@ def __init__( device_batch_size (int): buffer_info's device_batch_size, before any single-replica collapse. Converts buffer_info's all-batch replica counts into per-batch stamp counts. + x2 (bool): X2 design; buffer sizes are already in bytes. """ + self.x2 = x2 self.flexml_ids = [] self.l3_ifm_buffers = [] self.l3_ofm_buffers = [] @@ -561,7 +612,7 @@ def _match_l3_buffer(self, fm, l3_buffer_names, l3_buffer_sizes, size_shift): for full_l3_name in l3_buffer_names: if sub_name in full_l3_name: if size.get("type"): - size_shift = SIZE_BYTES.get(size["type"], 1) + size_shift = get_size_bytes(size["type"], self.x2) buffer = L3Buffer( name=full_l3_name, tensor_name=size.get("tensor_name"), @@ -594,7 +645,10 @@ def _initialize_l3_buffers(self, info, version): for _, entry in enumerate(info["ifm"], start=1): if "l3" in entry: self._match_l3_buffer( - "ifm", entry["l3_buffer_names"], entry["l3"], SIZE_BYTES.get(entry.get("dtype"), 1) + "ifm", + entry["l3_buffer_names"], + entry["l3"], + get_size_bytes(entry.get("dtype"), self.x2) or 1, ) else: fms = ["ifm", "ifm2", "ofm"] @@ -604,7 +658,7 @@ def _initialize_l3_buffers(self, info, version): name, info[name]["l3_buffer_names"], info[name]["l3"], - SIZE_BYTES.get(info[name].get("dtype"), 1), + get_size_bytes(info[name].get("dtype"), self.x2) or 1, ) def _initialize_buffers(self, info, aie_iface, size_shift, version): @@ -617,21 +671,22 @@ def _initialize_buffers(self, info, aie_iface, size_shift, version): size_shift (int): Size shift parameter from .meta. version (Version): Software version object. """ + x2 = self.x2 # Many entries have the string "ifm" in them if version > Version.from_string("1.2"): if "ifm" in info: for i, entry in enumerate(info["ifm"], start=1): - self.in_buffers.append(Buffer(entry, f"ifm{i}", size_shift, aie_iface, ifm=True)) + self.in_buffers.append(Buffer(entry, f"ifm{i}", size_shift, aie_iface, ifm=True, x2=x2)) else: for name in ["ifm", "ifm2"]: if name in info: - self.in_buffers.append(Buffer(info[name], name, size_shift, aie_iface, ifm=True)) + self.in_buffers.append(Buffer(info[name], name, size_shift, aie_iface, ifm=True, x2=x2)) for name in ["wts"]: if "wts" in info: - self.wts_buffers.append(Buffer(info[name], name, size_shift, aie_iface, wts=True)) + self.wts_buffers.append(Buffer(info[name], name, size_shift, aie_iface, wts=True, x2=x2)) for name in ["ofm"]: if name in info: - self.out_buffers.append(Buffer(info[name], name, size_shift, aie_iface, ofm=True)) + self.out_buffers.append(Buffer(info[name], name, size_shift, aie_iface, ofm=True, x2=x2)) def format_stamps(self): """ @@ -1057,6 +1112,7 @@ def _init_layers(self, raw_info, aie_iface, num_stamps, num_batches = 1, skip_la self.mladf_report, num_batches=num_batches, device_batch_size=self.layout[0], + x2=self.x2, ) if skip_layers and layer.layer_order in skip_layers: LOGGER.verbose_print(f"[WARNING] unsupported layer {layer.layer_order} will be skipped.")