Skip to content

Commit 7cea14a

Browse files
author
Robert Muchsel
authored
Prevent malformed kernel headers on Windows when not using --mexpress (#244)
* Prevent malformed kernel headers on Windows when not using --mexpress * Add `--apb` as alias for `--no-pll`
1 parent 06ee5a0 commit 7cea14a

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

izer/commandline.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ def get_parser() -> argparse.Namespace:
5454
mgroup = group.add_mutually_exclusive_group()
5555
mgroup.add_argument('--pll', action='store_true', default=None,
5656
help="enable PLL (default: automatic)")
57-
mgroup.add_argument('--no-pll', action='store_false', dest='pll',
57+
mgroup.add_argument('--no-pll', '--apb', action='store_false', dest='pll',
5858
help="disable PLL (default: automatic)")
5959
mgroup = group.add_mutually_exclusive_group()
6060
mgroup.add_argument('--balance-speed', action='store_true', default=True,
61-
help="balance data and weight loading speed and power (default: True)")
61+
help="balance data and weight loading speed and power (default: true)")
6262
mgroup.add_argument('--max-speed', action='store_false', dest='balance_speed',
6363
help="load data and weights as fast as possible (MAX78002 only, "
64-
"requires --pll, default: False)")
64+
"requires --pll, default: false)")
6565
group.add_argument('--config-file', required=True, metavar='S',
6666
help="YAML configuration file containing layer configuration")
6767
group.add_argument('--checkpoint-file', metavar='S',
@@ -188,7 +188,7 @@ def get_parser() -> argparse.Namespace:
188188
"default: false)")
189189
group.add_argument('--no-fifo-wait', dest='fifo_wait', action='store_false', default=True,
190190
help="do not check the FIFO for available space (requires matching source "
191-
"speed to inference, default: False)")
191+
"speed to inference, default: false)")
192192
group.add_argument('--fifo-go', action='store_true', default=False,
193193
help="start processing before first FIFO push (default: false)")
194194
group.add_argument('--slow-load', type=int, metavar='N', default=0,
@@ -282,13 +282,13 @@ def get_parser() -> argparse.Namespace:
282282
group.add_argument('--debug-snoop', action='store_true', default=False,
283283
help="insert snoop register debug code (default: False)")
284284
group.add_argument('--snoop-loop', action='store_true', default=False,
285-
help="insert snoop loop (default: False)")
285+
help="insert snoop loop (default: false)")
286286
group.add_argument('--ignore-hw-limits', action='store_true', default=False,
287-
help="ignore certain hardware limits (default: False)")
287+
help="ignore certain hardware limits (default: false)")
288288
group.add_argument('--ignore-bn', action='store_true', default=False,
289-
help="ignore BatchNorm weights in checkpoint file (default: False)")
289+
help="ignore BatchNorm weights in checkpoint file (default: false)")
290290
group.add_argument('--ignore-activation', action='store_true', default=False,
291-
help="ignore activations in YAML file (default: False)")
291+
help="ignore activations in YAML file (default: false)")
292292
group.add_argument('--no-greedy-kernel', action='store_false', dest='greedy_kernel_allocator',
293293
default=True,
294294
help="do not use greedy kernel memory allocator (default: use)")

izer/kernels.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def load( # pylint: disable=too-many-branches,too-many-statements
100100
kernel_map = np.full((tc.dev.MAX_PROC, tc.dev.MASK_WIDTH_LARGE),
101101
fill_value=_INVALID_VALUE, dtype=np.int64)
102102
kernels_used = np.zeros((tc.dev.MAX_PROC, tc.dev.MASK_WIDTH_LARGE), dtype=np.int64)
103-
kernel_data = np.zeros((tc.dev.MAX_PROC, tc.dev.MASK_WIDTH_LARGE, 9), dtype=np.int8)
103+
kernel_data = np.zeros((tc.dev.MAX_PROC, tc.dev.MASK_WIDTH_LARGE, 9), dtype=np.uint8)
104104
# There are four 32-bit words per 9-byte kernel.
105105
# The value map is initialized with zeros so we can later ignore unused entries and use
106106
# memcpy() on initialized and uninitialized data.
@@ -438,6 +438,8 @@ def add_kernel_data(ll, p, col_target, b):
438438
kernel_map[p][col] = ll
439439

440440
assert kernels_used[p][col] <= 8
441+
assert isinstance(b, np.int64), f'Kernel is type {type(b)} instead of numpy.int64'
442+
assert 0 <= b <= 255, f'Trying to add kernel value {b}'
441443
kernel_data[p][col][8 - kernels_used[p][col]] = b & 0xff
442444
kernels_used[p][col] += 1
443445

@@ -457,7 +459,7 @@ def add_kernel_data(ll, p, col_target, b):
457459
col_target, col_bytes = divmod(start_col * ksize * in_exp, 9)
458460
# Pad out the leftovers
459461
for _ in range(col_bytes // qfactor): # FIXME for quantization
460-
col_target = add_kernel_data(ll, p, col_target, 0)
462+
col_target = add_kernel_data(ll, p, col_target, np.int64(0))
461463

462464
out_range = out_expand[ll] if conv_groups[ll] == 1 else 1
463465
for expand in range(out_range):
@@ -506,8 +508,10 @@ def add_kernel_data(ll, p, col_target, b):
506508
& (2**abs(quantization[ll])-1)
507509
if not flatten[ll]:
508510
k |= this_kern << (i * abs(quantization[ll]))
509-
else:
511+
elif len(k) > 0:
510512
k = np.append(k, this_kern)
513+
else:
514+
k = this_kern
511515
n += 1
512516
mask >>= 1
513517
if debug:
@@ -525,8 +529,8 @@ def add_kernel_data(ll, p, col_target, b):
525529
),
526530
)
527531
for i in range(0, len(k) // qfactor):
528-
e = 0
529-
for j in range(qfactor):
532+
e = k[i * qfactor]
533+
for j in range(1, qfactor):
530534
e |= k[i * qfactor + j] << (j * abs(quantization[ll]))
531535
col_target = add_kernel_data(ll, p, col_target, e)
532536
else:
@@ -536,7 +540,7 @@ def add_kernel_data(ll, p, col_target, b):
536540

537541
else: # When expanding, need to pad with zero kernels if needed
538542
for _ in range(ksize // qfactor):
539-
col_target = add_kernel_data(ll, p, col_target, 0)
543+
col_target = add_kernel_data(ll, p, col_target, np.int64(0))
540544

541545
# Consume kernels
542546
if not flatten[ll]:
@@ -552,7 +556,7 @@ def add_kernel_data(ll, p, col_target, b):
552556
and kernels_used[p][kern_offs[ll] + col_target] > 0: # Partials
553557
col_target += 1
554558
while col_target - start_col < kern_len[ll]:
555-
col_target = add_kernel_data(ll, p, col_target, 0)
559+
col_target = add_kernel_data(ll, p, col_target, np.int64(0))
556560
if flatten[ll]:
557561
kern_len[ll] = col_target
558562
elif not state.new_kernel_loader:

izer/toplevel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ def c_define(
888888
prefix, formatting = fmt.split('%')
889889
memfile.write(f'#define {define_name} {{ \\\n ')
890890
for i, e in enumerate(array):
891-
memfile.write(f'{prefix}{e:{formatting}}')
891+
memfile.write(f'{prefix}{e & 0xffffffff:{formatting}}')
892892
if i + 1 < len(array):
893893
memfile.write(', ')
894894
if (i + 1) % columns == 0:

0 commit comments

Comments
 (0)