Files
ncnn/tests/test_memorydata.cpp
nihui 8bc95a1e7f pnnx: store bool MemoryData attributes as raw int8 (#7006)
* pnnx: store bool MemoryData attributes as raw int8

Keep bool constants as one byte per element and select MemoryData
load_type=3. Implement the reserved raw int8 ModelBin loading mode and
pad exported bool data to four bytes so subsequent weights stay aligned.
This preserves the byte representation used by the proposed logical ops.

Add regression coverage for scalar and padded bool exports in both fp16
modes, multidimensional MemoryData, raw int8 loading through reference
and copy readers, trailing fp32 weights, and missing padding. Document
the format.

Based on the bool attribute serialization fixes proposed in:
https://github.com/Tencent/ncnn/pull/6956
https://github.com/Tencent/ncnn/pull/6866

Fixes #6855

* tools: preserve byte MemoryData when optimizing models

Save MemoryData load_type so raw int8 constants retain their byte
representation after ModelWriter saves the model. Skip fusion paths that
interpret MemoryData as float32 when its elements are not four bytes.

This change does not add bool arithmetic support or automatic type
promotion. Byte bool storage is intended for future logical, comparison
and selection operators.

Validated ncnnoptimize save and reload for scalar and one-to-five-element
bool constants in fp32 and fp16 storage modes, preserving byte values
and element size in all 12 cases.

---------

Co-authored-by: ktz03 <2484593937@qq.com>
Co-authored-by: Giulio Corradi <53930027+giuliocorradi@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-17 10:09:00 +08:00

83 lines
1.9 KiB
C++

// Copyright 2020 Tencent
// SPDX-License-Identifier: BSD-3-Clause
#include "testutil.h"
static void set_memorydata_params(ncnn::ParamDict& pd, const ncnn::Mat& a)
{
pd.set(0, a.w);
if (a.dims >= 2)
pd.set(1, a.h);
if (a.dims == 4)
pd.set(11, a.d);
if (a.dims >= 3)
pd.set(2, a.c);
}
static int test_memorydata(const ncnn::Mat& a)
{
ncnn::ParamDict pd;
set_memorydata_params(pd, a);
pd.set(21, a.elemsize == 1u ? 3 : 1);
std::vector<ncnn::Mat> weights(1);
weights[0] = a;
std::vector<ncnn::Mat> as(0);
int ret = test_layer("MemoryData", pd, weights, as, 1);
if (ret != 0)
{
fprintf(stderr, "test_memorydata failed a.dims=%d a=(%d %d %d %d)\n", a.dims, a.w, a.h, a.d, a.c);
}
return ret;
}
static int test_memorydata_4d()
{
return 0
|| test_memorydata(RandomMat(5, 7, 3, 16))
|| test_memorydata(RandomMat(3, 5, 4, 13))
|| test_memorydata(RandomS8Mat(5, 7, 3, 16))
|| test_memorydata(RandomS8Mat(3, 5, 4, 13));
}
static int test_memorydata_0()
{
return 0
|| test_memorydata(RandomMat(5, 7, 16))
|| test_memorydata(RandomMat(3, 5, 13))
|| test_memorydata(RandomS8Mat(5, 7, 16))
|| test_memorydata(RandomS8Mat(3, 5, 13));
}
static int test_memorydata_1()
{
return 0
|| test_memorydata(RandomMat(6, 16))
|| test_memorydata(RandomMat(7, 15))
|| test_memorydata(RandomS8Mat(6, 16))
|| test_memorydata(RandomS8Mat(7, 15));
}
static int test_memorydata_2()
{
return 0
|| test_memorydata(RandomMat(128))
|| test_memorydata(RandomMat(127))
|| test_memorydata(RandomS8Mat(128))
|| test_memorydata(RandomS8Mat(127));
}
int main()
{
SRAND(7767517);
return 0
|| test_memorydata_4d()
|| test_memorydata_0()
|| test_memorydata_1()
|| test_memorydata_2();
}