LCOV - code coverage report
Current view: top level - xenolith/utils/shadernn/src/layers - XLSnnConvLayer.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 17 0.0 %
Date: 2024-05-06 04:51:23 Functions: 0 14 0.0 %

          Line data    Source code
       1             : /**
       2             :  Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
       3             : 
       4             :  Permission is hereby granted, free of charge, to any person obtaining a copy
       5             :  of this software and associated documentation files (the "Software"), to deal
       6             :  in the Software without restriction, including without limitation the rights
       7             :  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
       8             :  copies of the Software, and to permit persons to whom the Software is
       9             :  furnished to do so, subject to the following conditions:
      10             : 
      11             :  The above copyright notice and this permission notice shall be included in
      12             :  all copies or substantial portions of the Software.
      13             : 
      14             :  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      15             :  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      16             :  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      17             :  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      18             :  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      19             :  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      20             :  THE SOFTWARE.
      21             :  **/
      22             : 
      23             : #ifndef SRC_LAYERS_XLSNNCONVLAYER_H_
      24             : #define SRC_LAYERS_XLSNNCONVLAYER_H_
      25             : 
      26             : #include "XLSnnLayer.h"
      27             : 
      28             : namespace stappler::xenolith::shadernn {
      29             : 
      30             : struct MatVec {
      31             :         Extent2 extent;
      32             :         Vector<float> data;
      33             : 
      34           0 :         MatVec(Extent2 e) : extent(e) {
      35           0 :                 data.resize(e.width * e.height, 0.0f);
      36           0 :         }
      37             : 
      38           0 :         void set(uint32_t i, uint32_t j, float value) {
      39           0 :                 (data.data() + i * extent.width)[j] = value;
      40           0 :         }
      41             : 
      42           0 :         float at(int i0) const {
      43           0 :                 return data[i0];
      44             :         }
      45             : };
      46             : 
      47             : class ConvLayer : public Layer {
      48             : public:
      49           0 :         virtual ~ConvLayer() = default;
      50             : 
      51           0 :         uint32_t getActivation() const { return toInt(_activation); }
      52           0 :         uint32_t getKernelSize() const { return _kernelSize; }
      53           0 :         uint32_t getStride() const { return _stride; }
      54             : 
      55             :         Extent3 getKernelExtent() const;
      56             : 
      57           0 :         bool useBias() const { return _biases.size() > 0; }
      58             : 
      59             :         BytesView getKernelImageData() const;
      60             :         BytesView getBiasBufferData() const;
      61             : 
      62             : protected:
      63             :         Vector<MatVec> _weightsCvM;
      64             :         Vector<float> _weightsData;
      65             :         Vector<uint16_t> _weightsDataF16;
      66             :         Vector<float> _biases;
      67             :         Activation _activation;
      68             :         uint32_t _kernelSize = 0;
      69             :         uint32_t _stride = 0;
      70             : };
      71             : 
      72             : class Conv2DLayer : public ConvLayer {
      73             : public:
      74             :         static bool oihw2hwo4i4(const Vector<MatVec> &inputWeights, Vector<float> &outVec, int inChannels, int outChannels,
      75             :                         int fw, int fh, int unit = 4);
      76             : 
      77             :         struct BatchNormalization {
      78             :                 Vector<float> beta;
      79             :                 Vector<float> gamma;
      80             :                 Vector<float> mean;
      81             :                 Vector<float> variance;
      82             :         };
      83             : 
      84           0 :         virtual ~Conv2DLayer() = default;
      85             : 
      86             :         virtual bool init(Model *, StringView tag, size_t idx, const Value &) override;
      87             : 
      88             :         virtual LayerTransformInfo getOutputTransform() const override;
      89             : 
      90             :         UVec4 getPaddingOffset() const;
      91             : 
      92           0 :         StringView getPaddingMode() const { return _paddingMode; }
      93             : 
      94           0 :         bool useBatchNormalization() const { return _useBatchNormalization; }
      95             : 
      96           0 :         float getLeakyReluAlpha() const { return _leakyReluAlpha; }
      97             : 
      98             :         BytesView getNormBetaBufferData() const;
      99             :         BytesView getNormGammaBufferData() const;
     100             :         BytesView getNormMeanBufferData() const;
     101             :         BytesView getNormVarianceBufferData() const;
     102             : 
     103             :         virtual const core::QueuePassData *prepare(core::Queue::Builder &builder,
     104             :                         Map<Layer *, const core::AttachmentData *> inputs,
     105             :                         Map<Attachment *, const core::AttachmentData *> attachments) override;
     106             : 
     107             : protected:
     108             :         BatchNormalization _batchNormalization;
     109             :         bool _useBatchNormalization = false;
     110             :         bool _useMultiInputs = false;
     111             :         float _leakyReluAlpha = 0.0f;
     112             :         bool _useUniformShaders = true;
     113             :         uint32_t _paddingT = 0;
     114             :         uint32_t _paddingB = 0;
     115             :         uint32_t _paddingL = 0;
     116             :         uint32_t _paddingR = 0;
     117             :         String _paddingValue;
     118             :         String _paddingMode = "constant";
     119             : };
     120             : 
     121             : }
     122             : 
     123             : #endif /* SRC_LAYERS_XLSNNCONVLAYER_H_ */

Generated by: LCOV version 1.14