Skip to content

Commit ccb7c2f

Browse files
committed
Clarify Net layer and pipeline cleanup responsibilities
Replace the boolean cleanup mode with clear_layers for parameter-load failures. Keep pipeline destruction and allocator cleanup directly in Net::clear, and share registered layer destruction with CPU replacement paths. Validated existing Net and ParamDict tests, Net ASan/UBSan with leak detection, SqueezeNet inference, and Vulkan net.cpp compilation.
1 parent fcbabb4 commit ccb7c2f

1 file changed

Lines changed: 64 additions & 60 deletions

File tree

src/net.cpp

Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ class NetPrivate
5252
int do_forward_layer(const Layer* layer, std::vector<VkMat>& blob_mats_gpu, VkCompute& cmd, const Option& opt) const;
5353
#endif // NCNN_VULKAN
5454

55-
// unfinished parameter loads own layer objects but no pipelines
56-
void clear(bool destroy_pipeline);
55+
void clear_layers();
5756
void destroy_layer(Layer* layer);
5857
int load_shape_hints(Layer* layer, const ParamDict& pd);
5958

@@ -1439,7 +1438,7 @@ int Net::load_param(const DataReader& dr)
14391438
if (scan_net_value(dr, v) != 1) \
14401439
{ \
14411440
NCNN_LOGE("parse " #v " failed"); \
1442-
d->clear(false); \
1441+
d->clear_layers(); \
14431442
return -1; \
14441443
}
14451444

@@ -1531,7 +1530,7 @@ int Net::load_param(const DataReader& dr)
15311530
if (bottom_count < 0 || top_count < 0 || bottom_count > max_net_count || top_count > max_net_count)
15321531
{
15331532
NCNN_LOGE("invalid bottom_count or top_count %d %d", bottom_count, top_count);
1534-
d->clear(false);
1533+
d->clear_layers();
15351534
return -1;
15361535
}
15371536

@@ -1553,7 +1552,7 @@ int Net::load_param(const DataReader& dr)
15531552
if (!layer)
15541553
{
15551554
NCNN_LOGE("layer %s not exists or registered", layer_type);
1556-
d->clear(false);
1555+
d->clear_layers();
15571556
return -1;
15581557
}
15591558

@@ -1580,7 +1579,7 @@ int Net::load_param(const DataReader& dr)
15801579
if (blob_index >= blob_count)
15811580
{
15821581
NCNN_LOGE("too many blobs at layer %d bottom %d", i, j);
1583-
d->clear(false);
1582+
d->clear_layers();
15841583
return -1;
15851584
}
15861585

@@ -1607,7 +1606,7 @@ int Net::load_param(const DataReader& dr)
16071606
if (blob_index >= blob_count)
16081607
{
16091608
NCNN_LOGE("too many blobs at layer %d top %d", i, j);
1610-
d->clear(false);
1609+
d->clear_layers();
16111610
return -1;
16121611
}
16131612

@@ -1633,23 +1632,23 @@ int Net::load_param(const DataReader& dr)
16331632
if (pdlr != 0)
16341633
{
16351634
NCNN_LOGE("ParamDict load_param %d %s failed", i, layer_name);
1636-
d->clear(false);
1635+
d->clear_layers();
16371636
return -1;
16381637
}
16391638

16401639
// pull out top shape hints
16411640
if (d->load_shape_hints(layer, pd) != 0)
16421641
{
16431642
NCNN_LOGE("invalid shape hints at layer %d", i);
1644-
d->clear(false);
1643+
d->clear_layers();
16451644
return -1;
16461645
}
16471646

16481647
// pull out layer specific feature disabled set
16491648
if (pd.type(31) != 0 && pd.type(31) != 1 && pd.type(31) != 2)
16501649
{
16511650
NCNN_LOGE("invalid feature mask at layer %d", i);
1652-
d->clear(false);
1651+
d->clear_layers();
16531652
return -1;
16541653
}
16551654
layer->featmask = pd.get(31, 0);
@@ -1658,7 +1657,7 @@ int Net::load_param(const DataReader& dr)
16581657
if (lr != 0)
16591658
{
16601659
NCNN_LOGE("layer load_param %d %s failed", i, layer_name);
1661-
d->clear(false);
1660+
d->clear_layers();
16621661
return -1;
16631662
}
16641663

@@ -1679,7 +1678,7 @@ int Net::load_param(const DataReader& dr)
16791678
if (!layer_cpu)
16801679
{
16811680
NCNN_LOGE("layer %s not exists or registered", layer_type);
1682-
d->clear(false);
1681+
d->clear_layers();
16831682
return -1;
16841683
}
16851684

@@ -1696,7 +1695,7 @@ int Net::load_param(const DataReader& dr)
16961695
{
16971696
NCNN_LOGE("layer load_param %d %s failed", i, layer_name);
16981697
d->destroy_layer(layer_cpu);
1699-
d->clear(false);
1698+
d->clear_layers();
17001699
return -1;
17011700
}
17021701

@@ -1708,7 +1707,7 @@ int Net::load_param(const DataReader& dr)
17081707
if (layer->typeindex != LayerType::Input && layer->one_blob_only && (bottom_count != 1 || top_count != 1))
17091708
{
17101709
NCNN_LOGE("invalid bottom_count or top_count for one_blob_only layer %d", i);
1711-
d->clear(false);
1710+
d->clear_layers();
17121711
return -1;
17131712
}
17141713

@@ -1809,7 +1808,7 @@ int Net::load_param_bin(const DataReader& dr)
18091808
if (dr.read(&buf, sizeof(buf)) != sizeof(buf)) \
18101809
{ \
18111810
NCNN_LOGE("read " #buf " failed"); \
1812-
d->clear(false); \
1811+
d->clear_layers(); \
18131812
return -1; \
18141813
} \
18151814
if (sizeof(buf) == 2) \
@@ -1821,7 +1820,7 @@ int Net::load_param_bin(const DataReader& dr)
18211820
if (dr.read(&buf, sizeof(buf)) != sizeof(buf)) \
18221821
{ \
18231822
NCNN_LOGE("read " #buf " failed"); \
1824-
d->clear(false); \
1823+
d->clear_layers(); \
18251824
return -1; \
18261825
}
18271826
#endif
@@ -1910,14 +1909,14 @@ int Net::load_param_bin(const DataReader& dr)
19101909
if (typeindex < 0)
19111910
{
19121911
NCNN_LOGE("invalid layer type %d", typeindex);
1913-
d->clear(false);
1912+
d->clear_layers();
19141913
return -1;
19151914
}
19161915

19171916
if (bottom_count < 0 || top_count < 0 || bottom_count > max_net_count || top_count > max_net_count)
19181917
{
19191918
NCNN_LOGE("invalid bottom_count or top_count %d %d", bottom_count, top_count);
1920-
d->clear(false);
1919+
d->clear_layers();
19211920
return -1;
19221921
}
19231922

@@ -1940,7 +1939,7 @@ int Net::load_param_bin(const DataReader& dr)
19401939
if (!layer)
19411940
{
19421941
NCNN_LOGE("layer %d not exists or registered", typeindex);
1943-
d->clear(false);
1942+
d->clear_layers();
19441943
return -1;
19451944
}
19461945

@@ -1964,7 +1963,7 @@ int Net::load_param_bin(const DataReader& dr)
19641963
if (bottom_blob_index < 0 || bottom_blob_index >= blob_count)
19651964
{
19661965
NCNN_LOGE("layer %d invalid bottom_blob_index %d", i, bottom_blob_index);
1967-
d->clear(false);
1966+
d->clear_layers();
19681967
return -1;
19691968
}
19701969

@@ -1984,7 +1983,7 @@ int Net::load_param_bin(const DataReader& dr)
19841983
if (top_blob_index < 0 || top_blob_index >= blob_count)
19851984
{
19861985
NCNN_LOGE("layer %d invalid top_blob_index %d", i, top_blob_index);
1987-
d->clear(false);
1986+
d->clear_layers();
19881987
return -1;
19891988
}
19901989

@@ -2005,23 +2004,23 @@ int Net::load_param_bin(const DataReader& dr)
20052004
if (pdlr != 0)
20062005
{
20072006
NCNN_LOGE("ParamDict load_param_bin %d failed", i);
2008-
d->clear(false);
2007+
d->clear_layers();
20092008
return -1;
20102009
}
20112010

20122011
// pull out top shape hints
20132012
if (d->load_shape_hints(layer, pd) != 0)
20142013
{
20152014
NCNN_LOGE("invalid shape hints at layer %d", i);
2016-
d->clear(false);
2015+
d->clear_layers();
20172016
return -1;
20182017
}
20192018

20202019
// pull out layer specific feature disabled set
20212020
if (pd.type(31) != 0 && pd.type(31) != 1 && pd.type(31) != 2)
20222021
{
20232022
NCNN_LOGE("invalid feature mask at layer %d", i);
2024-
d->clear(false);
2023+
d->clear_layers();
20252024
return -1;
20262025
}
20272026
layer->featmask = pd.get(31, 0);
@@ -2030,7 +2029,7 @@ int Net::load_param_bin(const DataReader& dr)
20302029
if (lr != 0)
20312030
{
20322031
NCNN_LOGE("layer load_param %d failed", i);
2033-
d->clear(false);
2032+
d->clear_layers();
20342033
return -1;
20352034
}
20362035

@@ -2052,7 +2051,7 @@ int Net::load_param_bin(const DataReader& dr)
20522051
if (!layer_cpu)
20532052
{
20542053
NCNN_LOGE("layer %d not exists or registered", typeindex);
2055-
d->clear(false);
2054+
d->clear_layers();
20562055
return -1;
20572056
}
20582057

@@ -2067,7 +2066,7 @@ int Net::load_param_bin(const DataReader& dr)
20672066
{
20682067
NCNN_LOGE("layer load_param %d failed", i);
20692068
d->destroy_layer(layer_cpu);
2070-
d->clear(false);
2069+
d->clear_layers();
20712070
return -1;
20722071
}
20732072

@@ -2079,7 +2078,7 @@ int Net::load_param_bin(const DataReader& dr)
20792078
if (layer->typeindex != LayerType::Input && layer->one_blob_only && (bottom_count != 1 || top_count != 1))
20802079
{
20812080
NCNN_LOGE("invalid bottom_count or top_count for one_blob_only layer %d", i);
2082-
d->clear(false);
2081+
d->clear_layers();
20832082
return -1;
20842083
}
20852084

@@ -2606,7 +2605,7 @@ void NetPrivate::destroy_layer(Layer* layer)
26062605
}
26072606
}
26082607

2609-
void NetPrivate::clear(bool destroy_pipeline)
2608+
void NetPrivate::clear_layers()
26102609
{
26112610
blobs.clear();
26122611
for (size_t i = 0; i < layers.size(); i++)
@@ -2616,17 +2615,6 @@ void NetPrivate::clear(bool destroy_pipeline)
26162615
if (!layer)
26172616
continue;
26182617

2619-
if (destroy_pipeline)
2620-
{
2621-
Option opt1 = get_masked_option(opt, layer->featmask);
2622-
int dret = layer->destroy_pipeline(opt1);
2623-
if (dret != 0)
2624-
{
2625-
NCNN_LOGE("layer destroy_pipeline failed");
2626-
// ignore anyway
2627-
}
2628-
}
2629-
26302618
destroy_layer(layer);
26312619
}
26322620
layers.clear();
@@ -2636,43 +2624,59 @@ void NetPrivate::clear(bool destroy_pipeline)
26362624
input_blob_names.clear();
26372625
output_blob_names.clear();
26382626
#endif // NCNN_STRING
2627+
}
26392628

2640-
if (local_blob_allocator)
2629+
void Net::clear()
2630+
{
2631+
for (size_t i = 0; i < d->layers.size(); i++)
26412632
{
2642-
delete local_blob_allocator;
2643-
local_blob_allocator = 0;
2633+
Layer* layer = d->layers[i];
2634+
2635+
if (!layer)
2636+
continue;
2637+
2638+
Option opt1 = get_masked_option(opt, layer->featmask);
2639+
int dret = layer->destroy_pipeline(opt1);
2640+
if (dret != 0)
2641+
{
2642+
NCNN_LOGE("layer destroy_pipeline failed");
2643+
// ignore anyway
2644+
}
2645+
}
2646+
2647+
d->clear_layers();
2648+
2649+
if (d->local_blob_allocator)
2650+
{
2651+
delete d->local_blob_allocator;
2652+
d->local_blob_allocator = 0;
26442653
}
2645-
if (local_workspace_allocator)
2654+
if (d->local_workspace_allocator)
26462655
{
2647-
delete local_workspace_allocator;
2648-
local_workspace_allocator = 0;
2656+
delete d->local_workspace_allocator;
2657+
d->local_workspace_allocator = 0;
26492658
}
26502659

26512660
#if NCNN_VULKAN
2652-
if (weight_vkallocator)
2661+
if (d->weight_vkallocator)
26532662
{
2654-
delete weight_vkallocator;
2655-
weight_vkallocator = 0;
2663+
delete d->weight_vkallocator;
2664+
d->weight_vkallocator = 0;
26562665
}
2657-
if (weight_staging_vkallocator)
2666+
if (d->weight_staging_vkallocator)
26582667
{
2659-
delete weight_staging_vkallocator;
2660-
weight_staging_vkallocator = 0;
2668+
delete d->weight_staging_vkallocator;
2669+
d->weight_staging_vkallocator = 0;
26612670
}
2662-
if (pipeline_cache)
2671+
if (d->pipeline_cache)
26632672
{
2664-
delete pipeline_cache;
2665-
pipeline_cache = 0;
2673+
delete d->pipeline_cache;
2674+
d->pipeline_cache = 0;
26662675
opt.pipeline_cache = 0;
26672676
}
26682677
#endif // NCNN_VULKAN
26692678
}
26702679

2671-
void Net::clear()
2672-
{
2673-
d->clear(true);
2674-
}
2675-
26762680
Extractor Net::create_extractor() const
26772681
{
26782682
return Extractor(this, d->blobs.size());

0 commit comments

Comments
 (0)