Skip to content

Commit 39c055d

Browse files
authored
crop axes starts ends expression (#5976)
* skip dynamic tensor index * handle clone oom
1 parent 4e34da4 commit 39c055d

24 files changed

Lines changed: 2493 additions & 302 deletions

docs/developer-guide/operators.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,9 @@ y = crop(x)
493493
| 9 | starts | array | [ ] | |
494494
| 10 | ends | array | [ ] | |
495495
| 11 | axes | array | [ ] | |
496+
| 19 | starts_expr | str | "" | |
497+
| 20 | ends_expr | str | "" | |
498+
| 21 | axes_expr | str | "" | |
496499

497500
# CumulativeSum
498501

@@ -1699,6 +1702,7 @@ y = reshape(x)
16991702
| 1 | h | int | -233 | |
17001703
| 11 | d | int | -233 | |
17011704
| 2 | c | int | -233 | |
1705+
| 6 | shape_expr | str | "" | |
17021706

17031707
Reshape flag:
17041708
- 0 = copy from bottom

src/layer/arm/crop_arm.cpp

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,21 @@ int Crop_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
143143
int elempack = bottom_blob.elempack;
144144

145145
#if __ARM_NEON
146-
if (elempack == 8)
146+
int _woffset, _hoffset, _doffset, _coffset;
147+
int _outw, _outh, _outd, _outc;
148+
if (!starts_expr.empty() && !ends_expr.empty())
149+
{
150+
std::vector<Mat> bottom_blob_shapes(1);
151+
bottom_blob_shapes[0] = bottom_blob.shape();
152+
eval_crop_expr(bottom_blob_shapes, _woffset, _hoffset, _doffset, _coffset, _outw, _outh, _outd, _outc);
153+
}
154+
else
147155
{
148-
int _woffset, _hoffset, _doffset, _coffset;
149-
int _outw, _outh, _outd, _outc;
150156
resolve_crop_roi(bottom_blob.shape(), _woffset, _hoffset, _doffset, _coffset, _outw, _outh, _outd, _outc);
157+
}
151158

159+
if (elempack == 8)
160+
{
152161
if (dims == 1)
153162
{
154163
int out_elempack = _outw % 8 == 0 ? 8 : _outw % 4 == 0 ? 4 : 1;
@@ -218,7 +227,7 @@ int Crop_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
218227

219228
if (_outw == w && _outh == h)
220229
{
221-
top_blob = bottom_blob_sliced.clone();
230+
top_blob = bottom_blob_sliced.clone(opt.blob_allocator);
222231
if (top_blob.empty())
223232
return -100;
224233
}
@@ -260,7 +269,7 @@ int Crop_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
260269

261270
if (_outw == w && _outh == h && _outd == d)
262271
{
263-
top_blob = bottom_blob_sliced.clone();
272+
top_blob = bottom_blob_sliced.clone(opt.blob_allocator);
264273
if (top_blob.empty())
265274
return -100;
266275
}
@@ -291,10 +300,6 @@ int Crop_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
291300

292301
if (elempack == 4)
293302
{
294-
int _woffset, _hoffset, _doffset, _coffset;
295-
int _outw, _outh, _outd, _outc;
296-
resolve_crop_roi(bottom_blob.shape(), _woffset, _hoffset, _doffset, _coffset, _outw, _outh, _outd, _outc);
297-
298303
if (dims == 1)
299304
{
300305
int out_elempack = _outw % 4 == 0 ? 4 : 1;
@@ -364,7 +369,7 @@ int Crop_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
364369

365370
if (_outw == w && _outh == h)
366371
{
367-
top_blob = bottom_blob_sliced.clone();
372+
top_blob = bottom_blob_sliced.clone(opt.blob_allocator);
368373
if (top_blob.empty())
369374
return -100;
370375
}
@@ -406,7 +411,7 @@ int Crop_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
406411

407412
if (_outw == w && _outh == h && _outd == d)
408413
{
409-
top_blob = bottom_blob_sliced.clone();
414+
top_blob = bottom_blob_sliced.clone(opt.blob_allocator);
410415
if (top_blob.empty())
411416
return -100;
412417
}
@@ -468,19 +473,28 @@ int Crop_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& to
468473
Mat& top_blob = top_blobs[0];
469474

470475
#if __ARM_NEON
471-
if (elempack == 8)
476+
int _woffset, _hoffset, _doffset, _coffset;
477+
int _outw, _outh, _outd, _outc;
478+
if (!starts_expr.empty() && !ends_expr.empty())
472479
{
473-
int _woffset, _hoffset, _doffset, _coffset;
474-
int _outw, _outh, _outd, _outc;
475-
if (woffset == -233)
476-
{
477-
resolve_crop_roi(bottom_blob.shape(), (const int*)reference_blob, _woffset, _hoffset, _doffset, _coffset, _outw, _outh, _outd, _outc);
478-
}
479-
else
480+
std::vector<Mat> bottom_blob_shapes(bottom_blobs.size());
481+
for (size_t i = 0; i < bottom_blobs.size(); i++)
480482
{
481-
resolve_crop_roi(bottom_blob.shape(), reference_blob.shape(), _woffset, _hoffset, _doffset, _coffset, _outw, _outh, _outd, _outc);
483+
bottom_blob_shapes[i] = bottom_blobs[i].shape();
482484
}
485+
eval_crop_expr(bottom_blob_shapes, _woffset, _hoffset, _doffset, _coffset, _outw, _outh, _outd, _outc);
486+
}
487+
else if (woffset == -233)
488+
{
489+
resolve_crop_roi(bottom_blob.shape(), (const int*)reference_blob, _woffset, _hoffset, _doffset, _coffset, _outw, _outh, _outd, _outc);
490+
}
491+
else
492+
{
493+
resolve_crop_roi(bottom_blob.shape(), reference_blob.shape(), _woffset, _hoffset, _doffset, _coffset, _outw, _outh, _outd, _outc);
494+
}
483495

496+
if (elempack == 8)
497+
{
484498
if (dims == 1)
485499
{
486500
int out_elempack = _outw % 8 == 0 ? 8 : _outw % 4 == 0 ? 4 : 1;
@@ -550,7 +564,7 @@ int Crop_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& to
550564

551565
if (_outw == w && _outh == h)
552566
{
553-
top_blob = bottom_blob_sliced.clone();
567+
top_blob = bottom_blob_sliced.clone(opt.blob_allocator);
554568
if (top_blob.empty())
555569
return -100;
556570
}
@@ -592,7 +606,7 @@ int Crop_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& to
592606

593607
if (_outw == w && _outh == h && _outd == d)
594608
{
595-
top_blob = bottom_blob_sliced.clone();
609+
top_blob = bottom_blob_sliced.clone(opt.blob_allocator);
596610
if (top_blob.empty())
597611
return -100;
598612
}
@@ -623,17 +637,6 @@ int Crop_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& to
623637

624638
if (elempack == 4)
625639
{
626-
int _woffset, _hoffset, _doffset, _coffset;
627-
int _outw, _outh, _outd, _outc;
628-
if (woffset == -233)
629-
{
630-
resolve_crop_roi(bottom_blob.shape(), (const int*)reference_blob, _woffset, _hoffset, _doffset, _coffset, _outw, _outh, _outd, _outc);
631-
}
632-
else
633-
{
634-
resolve_crop_roi(bottom_blob.shape(), reference_blob.shape(), _woffset, _hoffset, _doffset, _coffset, _outw, _outh, _outd, _outc);
635-
}
636-
637640
if (dims == 1)
638641
{
639642
int out_elempack = _outw % 4 == 0 ? 4 : 1;
@@ -703,7 +706,7 @@ int Crop_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& to
703706

704707
if (_outw == w && _outh == h)
705708
{
706-
top_blob = bottom_blob_sliced.clone();
709+
top_blob = bottom_blob_sliced.clone(opt.blob_allocator);
707710
if (top_blob.empty())
708711
return -100;
709712
}
@@ -745,7 +748,7 @@ int Crop_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& to
745748

746749
if (_outw == w && _outh == h && _outd == d)
747750
{
748-
top_blob = bottom_blob_sliced.clone();
751+
top_blob = bottom_blob_sliced.clone(opt.blob_allocator);
749752
if (top_blob.empty())
750753
return -100;
751754
}
@@ -775,32 +778,23 @@ int Crop_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& to
775778
}
776779
#endif // __ARM_NEON
777780

778-
Mat bottom_blob_unpacked = bottom_blob;
779-
if (elempack != 1)
781+
std::vector<Mat> bottom_blobs_unpacked(bottom_blobs.size());
782+
for (size_t i = 0; i < bottom_blobs.size(); i++)
780783
{
781-
Option opt_pack1 = opt;
782-
opt_pack1.blob_allocator = opt.workspace_allocator;
783-
784-
convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack1);
785-
if (bottom_blob_unpacked.empty())
786-
return -100;
787-
}
784+
Mat bottom_blob_unpacked = bottom_blobs[i];
785+
if (elempack != 1)
786+
{
787+
Option opt_pack1 = opt;
788+
opt_pack1.blob_allocator = opt.workspace_allocator;
788789

789-
Mat reference_blob_unpacked = reference_blob;
790-
if (ref_elempack != 1)
791-
{
792-
Option opt_pack1 = opt;
793-
opt_pack1.blob_allocator = opt.workspace_allocator;
790+
convert_packing(bottom_blobs[i], bottom_blob_unpacked, 1, opt_pack1);
791+
if (bottom_blob_unpacked.empty())
792+
return -100;
793+
}
794794

795-
convert_packing(reference_blob, reference_blob_unpacked, 1, opt_pack1);
796-
if (reference_blob_unpacked.empty())
797-
return -100;
795+
bottom_blobs_unpacked[i] = bottom_blob_unpacked;
798796
}
799797

800-
std::vector<Mat> bottom_blobs_unpacked(2);
801-
bottom_blobs_unpacked[0] = bottom_blob_unpacked;
802-
bottom_blobs_unpacked[1] = reference_blob_unpacked;
803-
804798
return Crop::forward(bottom_blobs_unpacked, top_blobs, opt);
805799
}
806800

0 commit comments

Comments
 (0)