|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766 |
- // Tencent is pleased to support the open source community by making ncnn available.
- //
- // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
- //
- // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
- // in compliance with the License. You may obtain a copy of the License at
- //
- // https://opensource.org/licenses/BSD-3-Clause
- //
- // Unless required by applicable law or agreed to in writing, software distributed
- // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- // CONDITIONS OF ANY KIND, either express or implied. See the License for the
- // specific language governing permissions and limitations under the License.
-
- #include "mat.h"
-
- #if __ARM_NEON
- #include <arm_neon.h>
- #endif // __ARM_NEON
- #include "cpu.h"
- #include "layer.h"
- #include "layer_type.h"
-
- #include <math.h>
-
- #if NCNN_VULKAN
- #if NCNN_PLATFORM_API
- #if __ANDROID_API__ >= 26
- #include <android/hardware_buffer.h>
- #endif // __ANDROID_API__ >= 26
- #endif // NCNN_PLATFORM_API
- #endif // NCNN_VULKAN
-
- namespace ncnn {
-
- Mat Mat::clone(Allocator* _allocator) const
- {
- if (empty())
- return Mat();
-
- Mat m;
- if (dims == 1)
- m.create(w, elemsize, elempack, _allocator);
- else if (dims == 2)
- m.create(w, h, elemsize, elempack, _allocator);
- else if (dims == 3)
- m.create(w, h, c, elemsize, elempack, _allocator);
- else if (dims == 4)
- m.create(w, h, d, c, elemsize, elempack, _allocator);
-
- if (total() > 0)
- {
- if (cstep == m.cstep)
- memcpy(m.data, data, total() * elemsize);
- else
- {
- // copy by channel for differnet cstep
- size_t size = (size_t)w * h * d * elemsize;
- for (int i = 0; i < c; i++)
- {
- memcpy(m.channel(i), channel(i), size);
- }
- }
- }
-
- return m;
- }
-
- void Mat::clone_from(const ncnn::Mat& mat, Allocator* allocator)
- {
- *this = mat.clone(allocator);
- }
-
- Mat Mat::reshape(int _w, Allocator* _allocator) const
- {
- if (w * h * d * c != _w)
- return Mat();
-
- if (dims >= 3 && cstep != (size_t)w * h * d)
- {
- Mat m;
- m.create(_w, elemsize, elempack, _allocator);
-
- // flatten
- for (int i = 0; i < c; i++)
- {
- const void* ptr = (unsigned char*)data + i * cstep * elemsize;
- void* mptr = (unsigned char*)m.data + (size_t)i * w * h * d * elemsize;
- memcpy(mptr, ptr, (size_t)w * h * d * elemsize);
- }
-
- return m;
- }
-
- Mat m = *this;
-
- m.dims = 1;
- m.w = _w;
- m.h = 1;
- m.d = 1;
- m.c = 1;
-
- m.cstep = _w;
-
- return m;
- }
-
- Mat Mat::reshape(int _w, int _h, Allocator* _allocator) const
- {
- if (w * h * d * c != _w * _h)
- return Mat();
-
- if (dims >= 3 && cstep != (size_t)w * h * d)
- {
- Mat m;
- m.create(_w, _h, elemsize, elempack, _allocator);
-
- // flatten
- for (int i = 0; i < c; i++)
- {
- const void* ptr = (unsigned char*)data + i * cstep * elemsize;
- void* mptr = (unsigned char*)m.data + (size_t)i * w * h * d * elemsize;
- memcpy(mptr, ptr, (size_t)w * h * d * elemsize);
- }
-
- return m;
- }
-
- Mat m = *this;
-
- m.dims = 2;
- m.w = _w;
- m.h = _h;
- m.d = 1;
- m.c = 1;
-
- m.cstep = (size_t)_w * _h;
-
- return m;
- }
-
- Mat Mat::reshape(int _w, int _h, int _c, Allocator* _allocator) const
- {
- if (w * h * d * c != _w * _h * _c)
- return Mat();
-
- if (dims < 3)
- {
- if ((size_t)_w * _h != alignSize((size_t)_w * _h * elemsize, 16) / elemsize)
- {
- Mat m;
- m.create(_w, _h, _c, elemsize, elempack, _allocator);
-
- // align channel
- for (int i = 0; i < _c; i++)
- {
- const void* ptr = (unsigned char*)data + (size_t)i * _w * _h * elemsize;
- void* mptr = (unsigned char*)m.data + i * m.cstep * m.elemsize;
- memcpy(mptr, ptr, (size_t)_w * _h * elemsize);
- }
-
- return m;
- }
- }
- else if (c != _c)
- {
- // flatten and then align
- Mat tmp = reshape(_w * _h * _c, _allocator);
- return tmp.reshape(_w, _h, _c, _allocator);
- }
-
- Mat m = *this;
-
- m.dims = 3;
- m.w = _w;
- m.h = _h;
- m.d = 1;
- m.c = _c;
-
- m.cstep = alignSize((size_t)_w * _h * elemsize, 16) / elemsize;
-
- return m;
- }
-
- Mat Mat::reshape(int _w, int _h, int _d, int _c, Allocator* _allocator) const
- {
- if (w * h * d * c != _w * _h * _d * _c)
- return Mat();
-
- if (dims < 3)
- {
- if ((size_t)_w * _h * _d != alignSize((size_t)_w * _h * _d * elemsize, 16) / elemsize)
- {
- Mat m;
- m.create(_w, _h, _d, _c, elemsize, elempack, _allocator);
-
- // align channel
- for (int i = 0; i < _c; i++)
- {
- const void* ptr = (unsigned char*)data + (size_t)i * _w * _h * _d * elemsize;
- void* mptr = (unsigned char*)m.data + i * m.cstep * m.elemsize;
- memcpy(mptr, ptr, (size_t)_w * _h * _d * elemsize);
- }
-
- return m;
- }
- }
- else if (c != _c)
- {
- // flatten and then align
- Mat tmp = reshape(_w * _h * _d * _c, _allocator);
- return tmp.reshape(_w, _h, _d, _c, _allocator);
- }
-
- Mat m = *this;
-
- m.dims = 4;
- m.w = _w;
- m.h = _h;
- m.d = _d;
- m.c = _c;
-
- m.cstep = alignSize((size_t)_w * _h * _d * elemsize, 16) / elemsize;
-
- return m;
- }
-
- void Mat::create(int _w, size_t _elemsize, Allocator* _allocator)
- {
- if (dims == 1 && w == _w && elemsize == _elemsize && elempack == 1 && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = 1;
- allocator = _allocator;
-
- dims = 1;
- w = _w;
- h = 1;
- d = 1;
- c = 1;
-
- cstep = w;
-
- size_t totalsize = alignSize(total() * elemsize, 4);
- if (totalsize > 0)
- {
- if (allocator)
- data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount));
- else
- data = fastMalloc(totalsize + (int)sizeof(*refcount));
- }
-
- if (data)
- {
- refcount = (int*)(((unsigned char*)data) + totalsize);
- *refcount = 1;
- }
- }
-
- void Mat::create(int _w, int _h, size_t _elemsize, Allocator* _allocator)
- {
- if (dims == 2 && w == _w && h == _h && elemsize == _elemsize && elempack == 1 && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = 1;
- allocator = _allocator;
-
- dims = 2;
- w = _w;
- h = _h;
- d = 1;
- c = 1;
-
- cstep = (size_t)w * h;
-
- size_t totalsize = alignSize(total() * elemsize, 4);
- if (totalsize > 0)
- {
- if (allocator)
- data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount));
- else
- data = fastMalloc(totalsize + (int)sizeof(*refcount));
- }
-
- if (data)
- {
- refcount = (int*)(((unsigned char*)data) + totalsize);
- *refcount = 1;
- }
- }
-
- void Mat::create(int _w, int _h, int _c, size_t _elemsize, Allocator* _allocator)
- {
- if (dims == 3 && w == _w && h == _h && c == _c && elemsize == _elemsize && elempack == 1 && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = 1;
- allocator = _allocator;
-
- dims = 3;
- w = _w;
- h = _h;
- d = 1;
- c = _c;
-
- cstep = alignSize((size_t)w * h * elemsize, 16) / elemsize;
-
- size_t totalsize = alignSize(total() * elemsize, 4);
- if (totalsize > 0)
- {
- if (allocator)
- data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount));
- else
- data = fastMalloc(totalsize + (int)sizeof(*refcount));
- }
-
- if (data)
- {
- refcount = (int*)(((unsigned char*)data) + totalsize);
- *refcount = 1;
- }
- }
-
- void Mat::create(int _w, int _h, int _d, int _c, size_t _elemsize, Allocator* _allocator)
- {
- if (dims == 4 && w == _w && h == _h && d == _d && c == _c && elemsize == _elemsize && elempack == 1 && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = 1;
- allocator = _allocator;
-
- dims = 4;
- w = _w;
- h = _h;
- d = _d;
- c = _c;
-
- cstep = alignSize((size_t)w * h * d * elemsize, 16) / elemsize;
-
- size_t totalsize = alignSize(total() * elemsize, 4);
- if (totalsize > 0)
- {
- if (allocator)
- data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount));
- else
- data = fastMalloc(totalsize + (int)sizeof(*refcount));
- }
-
- if (data)
- {
- refcount = (int*)(((unsigned char*)data) + totalsize);
- *refcount = 1;
- }
- }
-
- void Mat::create(int _w, size_t _elemsize, int _elempack, Allocator* _allocator)
- {
- if (dims == 1 && w == _w && elemsize == _elemsize && elempack == _elempack && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = _elempack;
- allocator = _allocator;
-
- dims = 1;
- w = _w;
- h = 1;
- d = 1;
- c = 1;
-
- cstep = w;
-
- size_t totalsize = alignSize(total() * elemsize, 4);
- if (totalsize > 0)
- {
- if (allocator)
- data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount));
- else
- data = fastMalloc(totalsize + (int)sizeof(*refcount));
- }
-
- if (data)
- {
- refcount = (int*)(((unsigned char*)data) + totalsize);
- *refcount = 1;
- }
- }
-
- void Mat::create(int _w, int _h, size_t _elemsize, int _elempack, Allocator* _allocator)
- {
- if (dims == 2 && w == _w && h == _h && elemsize == _elemsize && elempack == _elempack && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = _elempack;
- allocator = _allocator;
-
- dims = 2;
- w = _w;
- h = _h;
- d = 1;
- c = 1;
-
- cstep = (size_t)w * h;
-
- size_t totalsize = alignSize(total() * elemsize, 4);
- if (totalsize > 0)
- {
- if (allocator)
- data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount));
- else
- data = fastMalloc(totalsize + (int)sizeof(*refcount));
- }
-
- if (data)
- {
- refcount = (int*)(((unsigned char*)data) + totalsize);
- *refcount = 1;
- }
- }
-
- void Mat::create(int _w, int _h, int _c, size_t _elemsize, int _elempack, Allocator* _allocator)
- {
- if (dims == 3 && w == _w && h == _h && c == _c && elemsize == _elemsize && elempack == _elempack && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = _elempack;
- allocator = _allocator;
-
- dims = 3;
- w = _w;
- h = _h;
- d = 1;
- c = _c;
-
- cstep = alignSize((size_t)w * h * elemsize, 16) / elemsize;
-
- size_t totalsize = alignSize(total() * elemsize, 4);
- if (totalsize > 0)
- {
- if (allocator)
- data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount));
- else
- data = fastMalloc(totalsize + (int)sizeof(*refcount));
- }
-
- if (data)
- {
- refcount = (int*)(((unsigned char*)data) + totalsize);
- *refcount = 1;
- }
- }
-
- void Mat::create(int _w, int _h, int _d, int _c, size_t _elemsize, int _elempack, Allocator* _allocator)
- {
- if (dims == 4 && w == _w && h == _h && d == _d && c == _c && elemsize == _elemsize && elempack == _elempack && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = _elempack;
- allocator = _allocator;
-
- dims = 4;
- w = _w;
- h = _h;
- d = _d;
- c = _c;
-
- cstep = alignSize((size_t)w * h * d * elemsize, 16) / elemsize;
-
- size_t totalsize = alignSize(total() * elemsize, 4);
- if (totalsize > 0)
- {
- if (allocator)
- data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount));
- else
- data = fastMalloc(totalsize + (int)sizeof(*refcount));
- }
-
- if (data)
- {
- refcount = (int*)(((unsigned char*)data) + totalsize);
- *refcount = 1;
- }
- }
-
- void Mat::create_like(const Mat& m, Allocator* _allocator)
- {
- int _dims = m.dims;
- if (_dims == 1)
- create(m.w, m.elemsize, m.elempack, _allocator);
- if (_dims == 2)
- create(m.w, m.h, m.elemsize, m.elempack, _allocator);
- if (_dims == 3)
- create(m.w, m.h, m.c, m.elemsize, m.elempack, _allocator);
- if (_dims == 4)
- create(m.w, m.h, m.d, m.c, m.elemsize, m.elempack, _allocator);
- }
-
- #if NCNN_VULKAN
- void Mat::create_like(const VkMat& m, Allocator* _allocator)
- {
- int _dims = m.dims;
- if (_dims == 1)
- create(m.w, m.elemsize, m.elempack, _allocator);
- if (_dims == 2)
- create(m.w, m.h, m.elemsize, m.elempack, _allocator);
- if (_dims == 3)
- create(m.w, m.h, m.c, m.elemsize, m.elempack, _allocator);
- if (_dims == 4)
- create(m.w, m.h, m.d, m.c, m.elemsize, m.elempack, _allocator);
- }
-
- void Mat::create_like(const VkImageMat& im, Allocator* _allocator)
- {
- int _dims = im.dims;
- if (_dims == 1)
- create(im.w, im.elemsize, im.elempack, _allocator);
- if (_dims == 2)
- create(im.w, im.h, im.elemsize, im.elempack, _allocator);
- if (_dims == 3)
- create(im.w, im.h, im.c, im.elemsize, im.elempack, _allocator);
- if (_dims == 4)
- create(im.w, im.h, im.d, im.c, im.elemsize, im.elempack, _allocator);
- }
- #endif // NCNN_VULKAN
-
- #if NCNN_VULKAN
- void VkMat::create(int _w, size_t _elemsize, VkAllocator* _allocator)
- {
- if (dims == 1 && w == _w && elemsize == _elemsize && elempack == 1 && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = 1;
- allocator = _allocator;
-
- dims = 1;
- w = _w;
- h = 1;
- d = 1;
- c = 1;
-
- cstep = w;
-
- if (total() > 0)
- {
- size_t totalsize = alignSize(total() * elemsize, 4);
-
- data = allocator->fastMalloc(totalsize);
- }
-
- if (data)
- {
- refcount = (int*)((unsigned char*)data + offsetof(VkBufferMemory, refcount));
- *refcount = 1;
- }
- }
-
- void VkMat::create(int _w, int _h, size_t _elemsize, VkAllocator* _allocator)
- {
- if (dims == 2 && w == _w && h == _h && elemsize == _elemsize && elempack == 1 && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = 1;
- allocator = _allocator;
-
- dims = 2;
- w = _w;
- h = _h;
- d = 1;
- c = 1;
-
- cstep = w * h;
-
- if (total() > 0)
- {
- size_t totalsize = alignSize(total() * elemsize, 4);
-
- data = allocator->fastMalloc(totalsize);
- }
-
- if (data)
- {
- refcount = (int*)((unsigned char*)data + offsetof(VkBufferMemory, refcount));
- *refcount = 1;
- }
- }
-
- void VkMat::create(int _w, int _h, int _c, size_t _elemsize, VkAllocator* _allocator)
- {
- if (dims == 3 && w == _w && h == _h && c == _c && elemsize == _elemsize && elempack == 1 && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = 1;
- allocator = _allocator;
-
- dims = 3;
- w = _w;
- h = _h;
- d = 1;
- c = _c;
-
- cstep = alignSize(w * h * elemsize, 16) / elemsize;
-
- if (total() > 0)
- {
- size_t totalsize = alignSize(total() * elemsize, 4);
-
- data = allocator->fastMalloc(totalsize);
- }
-
- if (data)
- {
- refcount = (int*)((unsigned char*)data + offsetof(VkBufferMemory, refcount));
- *refcount = 1;
- }
- }
-
- void VkMat::create(int _w, int _h, int _d, int _c, size_t _elemsize, VkAllocator* _allocator)
- {
- if (dims == 4 && w == _w && h == _h && d == _d && c == _c && elemsize == _elemsize && elempack == 1 && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = 1;
- allocator = _allocator;
-
- dims = 4;
- w = _w;
- h = _h;
- d = _d;
- c = _c;
-
- cstep = alignSize(w * h * d * elemsize, 16) / elemsize;
-
- if (total() > 0)
- {
- size_t totalsize = alignSize(total() * elemsize, 4);
-
- data = allocator->fastMalloc(totalsize);
- }
-
- if (data)
- {
- refcount = (int*)((unsigned char*)data + offsetof(VkBufferMemory, refcount));
- *refcount = 1;
- }
- }
-
- void VkMat::create(int _w, size_t _elemsize, int _elempack, VkAllocator* _allocator)
- {
- if (dims == 1 && w == _w && elemsize == _elemsize && elempack == _elempack && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = _elempack;
- allocator = _allocator;
-
- dims = 1;
- w = _w;
- h = 1;
- d = 1;
- c = 1;
-
- cstep = w;
-
- if (total() > 0)
- {
- size_t totalsize = alignSize(total() * elemsize, 4);
-
- data = allocator->fastMalloc(totalsize);
- }
-
- if (data)
- {
- refcount = (int*)((unsigned char*)data + offsetof(VkBufferMemory, refcount));
- *refcount = 1;
- }
- }
-
- void VkMat::create(int _w, int _h, size_t _elemsize, int _elempack, VkAllocator* _allocator)
- {
- if (dims == 2 && w == _w && h == _h && elemsize == _elemsize && elempack == _elempack && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = _elempack;
- allocator = _allocator;
-
- dims = 2;
- w = _w;
- h = _h;
- d = 1;
- c = 1;
-
- cstep = w * h;
-
- if (total() > 0)
- {
- size_t totalsize = alignSize(total() * elemsize, 4);
-
- data = allocator->fastMalloc(totalsize);
- }
-
- if (data)
- {
- refcount = (int*)((unsigned char*)data + offsetof(VkBufferMemory, refcount));
- *refcount = 1;
- }
- }
-
- void VkMat::create(int _w, int _h, int _c, size_t _elemsize, int _elempack, VkAllocator* _allocator)
- {
- if (dims == 3 && w == _w && h == _h && c == _c && elemsize == _elemsize && elempack == _elempack && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = _elempack;
- allocator = _allocator;
-
- dims = 3;
- w = _w;
- h = _h;
- d = 1;
- c = _c;
-
- cstep = alignSize(w * h * elemsize, 16) / elemsize;
-
- if (total() > 0)
- {
- size_t totalsize = alignSize(total() * elemsize, 4);
-
- data = allocator->fastMalloc(totalsize);
- }
-
- if (data)
- {
- refcount = (int*)((unsigned char*)data + offsetof(VkBufferMemory, refcount));
- *refcount = 1;
- }
- }
-
- void VkMat::create(int _w, int _h, int _d, int _c, size_t _elemsize, int _elempack, VkAllocator* _allocator)
- {
- if (dims == 4 && w == _w && h == _h && d == _d && c == _c && elemsize == _elemsize && elempack == _elempack && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = _elempack;
- allocator = _allocator;
-
- dims = 4;
- w = _w;
- h = _h;
- d = _d;
- c = _c;
-
- cstep = alignSize(w * h * d * elemsize, 16) / elemsize;
-
- if (total() > 0)
- {
- size_t totalsize = alignSize(total() * elemsize, 4);
-
- data = allocator->fastMalloc(totalsize);
- }
-
- if (data)
- {
- refcount = (int*)((unsigned char*)data + offsetof(VkBufferMemory, refcount));
- *refcount = 1;
- }
- }
-
- void VkMat::create_like(const Mat& m, VkAllocator* _allocator)
- {
- int _dims = m.dims;
- if (_dims == 1)
- create(m.w, m.elemsize, m.elempack, _allocator);
- if (_dims == 2)
- create(m.w, m.h, m.elemsize, m.elempack, _allocator);
- if (_dims == 3)
- create(m.w, m.h, m.c, m.elemsize, m.elempack, _allocator);
- if (_dims == 4)
- create(m.w, m.h, m.d, m.c, m.elemsize, m.elempack, _allocator);
- }
-
- void VkMat::create_like(const VkMat& m, VkAllocator* _allocator)
- {
- int _dims = m.dims;
- if (_dims == 1)
- create(m.w, m.elemsize, m.elempack, _allocator);
- if (_dims == 2)
- create(m.w, m.h, m.elemsize, m.elempack, _allocator);
- if (_dims == 3)
- create(m.w, m.h, m.c, m.elemsize, m.elempack, _allocator);
- if (_dims == 4)
- create(m.w, m.h, m.d, m.c, m.elemsize, m.elempack, _allocator);
- }
-
- void VkMat::create_like(const VkImageMat& im, VkAllocator* _allocator)
- {
- int _dims = im.dims;
- if (_dims == 1)
- create(im.w, im.elemsize, im.elempack, _allocator);
- if (_dims == 2)
- create(im.w, im.h, im.elemsize, im.elempack, _allocator);
- if (_dims == 3)
- create(im.w, im.h, im.c, im.elemsize, im.elempack, _allocator);
- if (_dims == 4)
- create(im.w, im.h, im.d, im.c, im.elemsize, im.elempack, _allocator);
- }
-
- void VkImageMat::create(int _w, size_t _elemsize, VkAllocator* _allocator)
- {
- if (dims == 1 && w == _w && elemsize == _elemsize && elempack == 1 && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = 1;
- allocator = _allocator;
-
- dims = 1;
- w = _w;
- h = 1;
- d = 1;
- c = 1;
-
- if (total() > 0)
- {
- data = allocator->fastMalloc(w, h, c, elemsize, elempack);
- }
-
- if (data)
- {
- refcount = (int*)((unsigned char*)data + offsetof(VkImageMemory, refcount));
- *refcount = 1;
- }
- }
-
- void VkImageMat::create(int _w, int _h, size_t _elemsize, VkAllocator* _allocator)
- {
- if (dims == 2 && w == _w && h == _h && elemsize == _elemsize && elempack == 1 && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = 1;
- allocator = _allocator;
-
- dims = 2;
- w = _w;
- h = _h;
- d = 1;
- c = 1;
-
- if (total() > 0)
- {
- data = allocator->fastMalloc(w, h, c, elemsize, elempack);
- }
-
- if (data)
- {
- refcount = (int*)((unsigned char*)data + offsetof(VkImageMemory, refcount));
- *refcount = 1;
- }
- }
-
- void VkImageMat::create(int _w, int _h, int _c, size_t _elemsize, VkAllocator* _allocator)
- {
- if (dims == 3 && w == _w && h == _h && c == _c && elemsize == _elemsize && elempack == 1 && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = 1;
- allocator = _allocator;
-
- dims = 3;
- w = _w;
- h = _h;
- d = 1;
- c = _c;
-
- if (total() > 0)
- {
- data = allocator->fastMalloc(w, h, c, elemsize, elempack);
- }
-
- if (data)
- {
- refcount = (int*)((unsigned char*)data + offsetof(VkImageMemory, refcount));
- *refcount = 1;
- }
- }
-
- void VkImageMat::create(int _w, int _h, int _d, int _c, size_t _elemsize, VkAllocator* _allocator)
- {
- if (dims == 4 && w == _w && h == _h && d == _d && c == _c && elemsize == _elemsize && elempack == 1 && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = 1;
- allocator = _allocator;
-
- dims = 4;
- w = _w;
- h = _h;
- d = _d;
- c = _c;
-
- if (total() > 0)
- {
- // underlying image is 3d
- data = allocator->fastMalloc(w, h * d, c, elemsize, elempack);
- }
-
- if (data)
- {
- refcount = (int*)((unsigned char*)data + offsetof(VkImageMemory, refcount));
- *refcount = 1;
- }
- }
-
- void VkImageMat::create(int _w, size_t _elemsize, int _elempack, VkAllocator* _allocator)
- {
- if (dims == 1 && w == _w && elemsize == _elemsize && elempack == _elempack && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = _elempack;
- allocator = _allocator;
-
- dims = 1;
- w = _w;
- h = 1;
- d = 1;
- c = 1;
-
- if (total() > 0)
- {
- data = allocator->fastMalloc(w, h, c, elemsize, elempack);
- }
-
- if (data)
- {
- refcount = (int*)((unsigned char*)data + offsetof(VkImageMemory, refcount));
- *refcount = 1;
- }
- }
-
- void VkImageMat::create(int _w, int _h, size_t _elemsize, int _elempack, VkAllocator* _allocator)
- {
- if (dims == 2 && w == _w && h == _h && elemsize == _elemsize && elempack == _elempack && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = _elempack;
- allocator = _allocator;
-
- dims = 2;
- w = _w;
- h = _h;
- d = 1;
- c = 1;
-
- if (total() > 0)
- {
- data = allocator->fastMalloc(w, h, c, elemsize, elempack);
- }
-
- if (data)
- {
- refcount = (int*)((unsigned char*)data + offsetof(VkImageMemory, refcount));
- *refcount = 1;
- }
- }
-
- void VkImageMat::create(int _w, int _h, int _c, size_t _elemsize, int _elempack, VkAllocator* _allocator)
- {
- if (dims == 3 && w == _w && h == _h && c == _c && elemsize == _elemsize && elempack == _elempack && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = _elempack;
- allocator = _allocator;
-
- dims = 3;
- w = _w;
- h = _h;
- d = 1;
- c = _c;
-
- if (total() > 0)
- {
- data = allocator->fastMalloc(w, h, c, elemsize, elempack);
- }
-
- if (data)
- {
- refcount = (int*)((unsigned char*)data + offsetof(VkImageMemory, refcount));
- *refcount = 1;
- }
- }
-
- void VkImageMat::create(int _w, int _h, int _d, int _c, size_t _elemsize, int _elempack, VkAllocator* _allocator)
- {
- if (dims == 4 && w == _w && h == _h && d == _d && c == _c && elemsize == _elemsize && elempack == _elempack && allocator == _allocator)
- return;
-
- release();
-
- elemsize = _elemsize;
- elempack = _elempack;
- allocator = _allocator;
-
- dims = 4;
- w = _w;
- h = _h;
- d = _d;
- c = _c;
-
- if (total() > 0)
- {
- // underlying image is 3d
- data = allocator->fastMalloc(w, h * d, c, elemsize, elempack);
- }
-
- if (data)
- {
- refcount = (int*)((unsigned char*)data + offsetof(VkImageMemory, refcount));
- *refcount = 1;
- }
- }
-
- void VkImageMat::create_like(const Mat& m, VkAllocator* _allocator)
- {
- int _dims = m.dims;
- if (_dims == 1)
- create(m.w, m.elemsize, m.elempack, _allocator);
- if (_dims == 2)
- create(m.w, m.h, m.elemsize, m.elempack, _allocator);
- if (_dims == 3)
- create(m.w, m.h, m.c, m.elemsize, m.elempack, _allocator);
- if (_dims == 4)
- create(m.w, m.h, m.d, m.c, m.elemsize, m.elempack, _allocator);
- }
-
- void VkImageMat::create_like(const VkMat& m, VkAllocator* _allocator)
- {
- int _dims = m.dims;
- if (_dims == 1)
- create(m.w, m.elemsize, m.elempack, _allocator);
- if (_dims == 2)
- create(m.w, m.h, m.elemsize, m.elempack, _allocator);
- if (_dims == 3)
- create(m.w, m.h, m.c, m.elemsize, m.elempack, _allocator);
- if (_dims == 4)
- create(m.w, m.h, m.d, m.c, m.elemsize, m.elempack, _allocator);
- }
-
- void VkImageMat::create_like(const VkImageMat& im, VkAllocator* _allocator)
- {
- int _dims = im.dims;
- if (_dims == 1)
- create(im.w, im.elemsize, im.elempack, _allocator);
- if (_dims == 2)
- create(im.w, im.h, im.elemsize, im.elempack, _allocator);
- if (_dims == 3)
- create(im.w, im.h, im.c, im.elemsize, im.elempack, _allocator);
- if (_dims == 4)
- create(im.w, im.h, im.d, im.c, im.elemsize, im.elempack, _allocator);
- }
- #endif // NCNN_VULKAN
-
- void Mat::substract_mean_normalize(const float* mean_vals, const float* norm_vals)
- {
- Layer* op;
-
- if (mean_vals && !norm_vals)
- {
- // substract mean only
- op = create_layer(LayerType::Bias);
-
- ParamDict pd;
- pd.set(0, c);
-
- op->load_param(pd);
-
- Mat weights[1];
- weights[0] = Mat(c);
- for (int q = 0; q < c; q++)
- {
- weights[0][q] = -mean_vals[q];
- }
-
- op->load_model(ModelBinFromMatArray(weights));
- }
- else if (!mean_vals && norm_vals)
- {
- // normalize only
- op = create_layer(LayerType::Scale);
-
- ParamDict pd;
- pd.set(0, c);
-
- op->load_param(pd);
-
- Mat weights[1];
- weights[0] = Mat(c);
- for (int q = 0; q < c; q++)
- {
- weights[0][q] = norm_vals[q];
- }
-
- op->load_model(ModelBinFromMatArray(weights));
- }
- else if (mean_vals && norm_vals)
- {
- // substract mean and normalize
- op = create_layer(LayerType::Scale);
-
- ParamDict pd;
- pd.set(0, c);
- pd.set(1, 1);
-
- op->load_param(pd);
-
- Mat weights[2];
- weights[0] = Mat(c);
- weights[1] = Mat(c);
- for (int q = 0; q < c; q++)
- {
- weights[0][q] = norm_vals[q];
- weights[1][q] = -mean_vals[q] * norm_vals[q];
- }
-
- op->load_model(ModelBinFromMatArray(weights));
- }
- else // if (!mean_vals && !norm_vals)
- {
- return;
- }
-
- Option opt;
- opt.num_threads = 1; // TODO
-
- op->create_pipeline(opt);
-
- op->forward_inplace(*this, opt);
-
- op->destroy_pipeline(opt);
-
- delete op;
- }
-
- Mat Mat::from_float16(const unsigned short* data, int size)
- {
- Mat m(size);
- if (m.empty())
- return m;
-
- float* ptr = m; //.data;
-
- #if __ARM_NEON && (__ARM_FP & 2)
- int nn = cpu_support_arm_vfpv4() ? size >> 2 : 0;
- int remain = size - (nn << 2);
- #else
- int remain = size;
- #endif // __ARM_NEON
-
- #if __ARM_NEON && (__ARM_FP & 2)
- #if __aarch64__
- if (nn > 0)
- {
- asm volatile(
- "0: \n"
- "ld1 {v0.4h}, [%1], #8 \n"
- "fcvtl v1.4s, v0.4h \n"
- "subs %w0, %w0, #1 \n"
- "st1 {v1.4s}, [%2], #16 \n"
- "bne 0b \n"
- : "=r"(nn), // %0
- "=r"(data), // %1
- "=r"(ptr) // %2
- : "0"(nn),
- "1"(data),
- "2"(ptr)
- : "cc", "memory", "v0", "v1");
- }
- #else
- if (nn > 0)
- {
- asm volatile(
- "0: \n"
- "pld [%1, #64] \n"
- "vld1.s16 {d0}, [%1]! \n"
- "vcvt.f32.f16 q1, d0 \n"
- "subs %0, #1 \n"
- "vst1.f32 {d2-d3}, [%2 :128]! \n"
- "bne 0b \n"
- : "=r"(nn), // %0
- "=r"(data), // %1
- "=r"(ptr) // %2
- : "0"(nn),
- "1"(data),
- "2"(ptr)
- : "cc", "memory", "q0", "q1");
- }
- #endif // __aarch64__
- #endif // __ARM_NEON
- for (; remain > 0; remain--)
- {
- *ptr = float16_to_float32(*data);
-
- data++;
- ptr++;
- }
-
- return m;
- }
-
- #if NCNN_VULKAN
- #if NCNN_PLATFORM_API
- #if __ANDROID_API__ >= 26
- VkImageMat VkImageMat::from_android_hardware_buffer(VkAndroidHardwareBufferImageAllocator* allocator)
- {
- int width = allocator->width();
- int height = allocator->height();
-
- return VkImageMat(width, height, allocator);
- }
- #endif // __ANDROID_API__ >= 26
- #endif // NCNN_PLATFORM_API
- #endif // NCNN_VULKAN
-
- unsigned short float32_to_float16(float value)
- {
- // 1 : 8 : 23
- union
- {
- unsigned int u;
- float f;
- } tmp;
-
- tmp.f = value;
-
- // 1 : 8 : 23
- unsigned short sign = (tmp.u & 0x80000000) >> 31;
- unsigned short exponent = (tmp.u & 0x7F800000) >> 23;
- unsigned int significand = tmp.u & 0x7FFFFF;
-
- // NCNN_LOGE("%d %d %d", sign, exponent, significand);
-
- // 1 : 5 : 10
- unsigned short fp16;
- if (exponent == 0)
- {
- // zero or denormal, always underflow
- fp16 = (sign << 15) | (0x00 << 10) | 0x00;
- }
- else if (exponent == 0xFF)
- {
- // infinity or NaN
- fp16 = (sign << 15) | (0x1F << 10) | (significand ? 0x200 : 0x00);
- }
- else
- {
- // normalized
- short newexp = exponent + (-127 + 15);
- if (newexp >= 31)
- {
- // overflow, return infinity
- fp16 = (sign << 15) | (0x1F << 10) | 0x00;
- }
- else if (newexp <= 0)
- {
- // Some normal fp32 cannot be expressed as normal fp16
- fp16 = (sign << 15) | (0x00 << 10) | 0x00;
- }
- else
- {
- // normal fp16
- fp16 = (sign << 15) | (newexp << 10) | (significand >> 13);
- }
- }
-
- return fp16;
- }
-
- float float16_to_float32(unsigned short value)
- {
- // 1 : 5 : 10
- unsigned short sign = (value & 0x8000) >> 15;
- unsigned short exponent = (value & 0x7c00) >> 10;
- unsigned short significand = value & 0x03FF;
-
- // NCNN_LOGE("%d %d %d", sign, exponent, significand);
-
- // 1 : 8 : 23
- union
- {
- unsigned int u;
- float f;
- } tmp;
- if (exponent == 0)
- {
- if (significand == 0)
- {
- // zero
- tmp.u = (sign << 31);
- }
- else
- {
- // denormal
- exponent = 0;
- // find non-zero bit
- while ((significand & 0x200) == 0)
- {
- significand <<= 1;
- exponent++;
- }
- significand <<= 1;
- significand &= 0x3FF;
- tmp.u = (sign << 31) | ((-exponent + (-15 + 127)) << 23) | (significand << 13);
- }
- }
- else if (exponent == 0x1F)
- {
- // infinity or NaN
- tmp.u = (sign << 31) | (0xFF << 23) | (significand << 13);
- }
- else
- {
- // normalized
- tmp.u = (sign << 31) | ((exponent + (-15 + 127)) << 23) | (significand << 13);
- }
-
- return tmp.f;
- }
-
- void copy_make_border(const Mat& src, Mat& dst, int top, int bottom, int left, int right, int type, float v, const Option& opt)
- {
- Layer* padding = create_layer(LayerType::Padding);
-
- ParamDict pd;
- pd.set(0, top);
- pd.set(1, bottom);
- pd.set(2, left);
- pd.set(3, right);
- pd.set(4, type);
- pd.set(5, v);
-
- padding->load_param(pd);
-
- padding->create_pipeline(opt);
-
- padding->forward(src, dst, opt);
-
- padding->destroy_pipeline(opt);
-
- delete padding;
- }
-
- void copy_make_border_3d(const Mat& src, Mat& dst, int top, int bottom, int left, int right, int front, int behind, int type, float v, const Option& opt)
- {
- Layer* padding = create_layer(LayerType::Padding);
-
- ParamDict pd;
- pd.set(0, top);
- pd.set(1, bottom);
- pd.set(2, left);
- pd.set(3, right);
- pd.set(4, type);
- pd.set(5, v);
- pd.set(7, front);
- pd.set(8, behind);
-
- padding->load_param(pd);
-
- padding->create_pipeline(opt);
-
- padding->forward(src, dst, opt);
-
- padding->destroy_pipeline(opt);
-
- delete padding;
- }
-
- void copy_cut_border(const Mat& src, Mat& dst, int top, int bottom, int left, int right, const Option& opt)
- {
- if (left + right > src.w || top + bottom > src.h)
- {
- NCNN_LOGE("copy_cut_border parameter error, top: %d, bottom: %d, left: %d, right: %d, src.w: %d, src.h: %d", top, bottom, left, right, src.w, src.h);
- return;
- }
- Layer* crop = create_layer(LayerType::Crop);
-
- ParamDict pd;
- pd.set(0, left);
- pd.set(1, top);
- pd.set(2, 0);
- pd.set(3, src.w - left - right);
- pd.set(4, src.h - top - bottom);
- pd.set(5, -233);
-
- crop->load_param(pd);
-
- crop->create_pipeline(opt);
-
- crop->forward(src, dst, opt);
-
- crop->destroy_pipeline(opt);
-
- delete crop;
- }
-
- void copy_cut_border_3d(const Mat& src, Mat& dst, int top, int bottom, int left, int right, int front, int behind, const Option& opt)
- {
- if (left + right > src.w || top + bottom > src.h || front + behind > src.d)
- {
- NCNN_LOGE("copy_cut_border_3d parameter error, top: %d, bottom: %d, left: %d, right: %d, front: %d, behind: %d, src.w: %d, src.h: %d, src.d: %d", top, bottom, left, right, front, behind, src.w, src.h, src.d);
- return;
- }
- Layer* crop = create_layer(LayerType::Crop);
-
- ParamDict pd;
- pd.set(0, left);
- pd.set(1, top);
- pd.set(13, front);
- pd.set(2, 0);
- pd.set(3, src.w - left - right);
- pd.set(4, src.h - top - bottom);
- pd.set(14, src.d - front - behind);
- pd.set(5, -233);
-
- crop->load_param(pd);
-
- crop->create_pipeline(opt);
-
- crop->forward(src, dst, opt);
-
- crop->destroy_pipeline(opt);
-
- delete crop;
- }
-
- void resize_nearest(const Mat& src, Mat& dst, int w, int h, const Option& opt)
- {
- Layer* interp = create_layer(LayerType::Interp);
-
- ParamDict pd;
- pd.set(0, 1);
- pd.set(3, h);
- pd.set(4, w);
-
- interp->load_param(pd);
-
- interp->create_pipeline(opt);
-
- interp->forward(src, dst, opt);
-
- interp->destroy_pipeline(opt);
-
- delete interp;
- }
-
- void resize_bilinear(const Mat& src, Mat& dst, int w, int h, const Option& opt)
- {
- Layer* interp = create_layer(LayerType::Interp);
-
- ParamDict pd;
- pd.set(0, 2);
- pd.set(3, h);
- pd.set(4, w);
-
- interp->load_param(pd);
-
- interp->create_pipeline(opt);
-
- interp->forward(src, dst, opt);
-
- interp->destroy_pipeline(opt);
-
- delete interp;
- }
-
- void resize_bicubic(const Mat& src, Mat& dst, int w, int h, const Option& opt)
- {
- Layer* interp = create_layer(LayerType::Interp);
-
- ParamDict pd;
- pd.set(0, 3);
- pd.set(3, h);
- pd.set(4, w);
-
- interp->load_param(pd);
-
- interp->create_pipeline(opt);
-
- interp->forward(src, dst, opt);
-
- interp->destroy_pipeline(opt);
-
- delete interp;
- }
-
- void convert_packing(const Mat& src, Mat& dst, int _elempack, const Option& opt)
- {
- Layer* packing = create_layer(LayerType::Packing);
-
- ParamDict pd;
- pd.set(0, _elempack);
-
- packing->load_param(pd);
-
- packing->create_pipeline(opt);
-
- packing->forward(src, dst, opt);
-
- packing->destroy_pipeline(opt);
-
- delete packing;
- }
-
- void flatten(const Mat& src, Mat& dst, const Option& opt)
- {
- Layer* flatten = create_layer(LayerType::Flatten);
-
- ParamDict pd;
-
- flatten->load_param(pd);
-
- flatten->create_pipeline(opt);
-
- flatten->forward(src, dst, opt);
-
- flatten->destroy_pipeline(opt);
-
- delete flatten;
- }
-
- void cast_float32_to_float16(const Mat& src, Mat& dst, const Option& opt)
- {
- Layer* cast = create_layer(LayerType::Cast);
-
- ParamDict pd;
- pd.set(0, 1);
- pd.set(1, 2);
-
- cast->load_param(pd);
-
- cast->create_pipeline(opt);
-
- cast->forward(src, dst, opt);
-
- cast->destroy_pipeline(opt);
-
- delete cast;
- }
-
- void cast_float16_to_float32(const Mat& src, Mat& dst, const Option& opt)
- {
- Layer* cast = create_layer(LayerType::Cast);
-
- ParamDict pd;
- pd.set(0, 2);
- pd.set(1, 1);
-
- cast->load_param(pd);
-
- cast->create_pipeline(opt);
-
- cast->forward(src, dst, opt);
-
- cast->destroy_pipeline(opt);
-
- delete cast;
- }
-
- void cast_int8_to_float32(const Mat& src, Mat& dst, const Option& opt)
- {
- Layer* cast = create_layer(LayerType::Cast);
-
- ParamDict pd;
- pd.set(0, 3);
- pd.set(1, 1);
-
- cast->load_param(pd);
-
- cast->create_pipeline(opt);
-
- cast->forward(src, dst, opt);
-
- cast->destroy_pipeline(opt);
-
- delete cast;
- }
-
- void cast_float32_to_bfloat16(const Mat& src, Mat& dst, const Option& opt)
- {
- Layer* cast = create_layer(LayerType::Cast);
-
- ParamDict pd;
- pd.set(0, 1);
- pd.set(1, 4);
-
- cast->load_param(pd);
-
- cast->create_pipeline(opt);
-
- cast->forward(src, dst, opt);
-
- cast->destroy_pipeline(opt);
-
- delete cast;
- }
-
- void cast_bfloat16_to_float32(const Mat& src, Mat& dst, const Option& opt)
- {
- Layer* cast = create_layer(LayerType::Cast);
-
- ParamDict pd;
- pd.set(0, 4);
- pd.set(1, 1);
-
- cast->load_param(pd);
-
- cast->create_pipeline(opt);
-
- cast->forward(src, dst, opt);
-
- cast->destroy_pipeline(opt);
-
- delete cast;
- }
-
- void quantize_to_int8(const Mat& src, Mat& dst, const Mat& scale_data, const Option& opt)
- {
- Layer* quantize = create_layer(LayerType::Quantize);
-
- ParamDict pd;
- pd.set(0, scale_data.w);
-
- quantize->load_param(pd);
-
- Mat weights[1];
- weights[0] = scale_data;
-
- quantize->load_model(ModelBinFromMatArray(weights));
-
- quantize->create_pipeline(opt);
-
- quantize->forward(src, dst, opt);
-
- quantize->destroy_pipeline(opt);
-
- delete quantize;
- }
-
- void dequantize_from_int32(const Mat& src, Mat& dst, const Mat& scale_data, const Mat& bias_data, const Option& opt)
- {
- Layer* dequantize = create_layer(LayerType::Dequantize);
-
- ParamDict pd;
- pd.set(0, scale_data.w);
- pd.set(1, bias_data.w);
-
- dequantize->load_param(pd);
-
- Mat weights[2];
- weights[0] = scale_data;
- weights[1] = bias_data;
-
- dequantize->load_model(ModelBinFromMatArray(weights));
-
- dequantize->create_pipeline(opt);
-
- dequantize->forward(src, dst, opt);
-
- dequantize->destroy_pipeline(opt);
-
- delete dequantize;
- }
-
- void requantize_from_int32_to_int8(const Mat& src, Mat& dst, const Mat& scale_in_data, const Mat& scale_out_data, const Mat& bias_data, int activation_type, const Mat& activation_params, const Option& opt)
- {
- Layer* requantize = create_layer(LayerType::Requantize);
-
- ParamDict pd;
- pd.set(0, scale_in_data.w);
- pd.set(1, scale_out_data.w);
- pd.set(2, bias_data.w);
- pd.set(3, activation_type);
- pd.set(4, activation_params);
-
- requantize->load_param(pd);
-
- Mat weights[3];
- weights[0] = scale_in_data;
- weights[1] = scale_out_data;
- weights[2] = bias_data;
-
- requantize->load_model(ModelBinFromMatArray(weights));
-
- requantize->create_pipeline(opt);
-
- requantize->forward(src, dst, opt);
-
- requantize->destroy_pipeline(opt);
-
- delete requantize;
- }
-
- } // namespace ncnn
|