shape.hpp 2.6 KB
Newer Older
C
Chon 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

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. */

#pragma once

#include <stdio.h>
#include <vector>

20 21
#include "lite/backends/fpga/KD/alignment.h"
#include "lite/backends/fpga/KD/layout.hpp"
C
Chon 已提交
22

Y
Yan Chunwei 已提交
23
namespace paddle {
C
Chon 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
namespace zynqmp {

static struct NCHW nchw_;
static struct NHWC nhwc_;
static struct NC nc_;
static struct NHW nhw_;
static struct N n_;

class Shape {
 public:
  explicit Shape(std::vector<int> dims) { dims_ = dims; }

  Shape(LayoutType type, std::vector<int> dims) {
    dims_ = dims;
    setLayoutType(type);
  }

  Shape(const Shape& src) {
    dims_ = src.dims_;
    setLayoutType(src.layoutType_);
  }

  bool shouldAlign() {
    return layout_->alignedElementCount(dims_) != layout_->elementCount(dims_);
  }

  int num() {
    int index = layout_->numIndex();
    return index == -1 ? 1 : dims_[index];
  }

  int channel() {
    int index = layout_->channelIndex();
    return index == -1 ? 1 : dims_[index];
  }

  int height() {
    int index = layout_->heightIndex();
    return index == -1 ? 1 : dims_[index];
  }

  int width() {
    int index = layout_->widthIndex();
    return index == -1 ? 1 : dims_[index];
  }

  int dimSize() { return dims_.size(); }

  std::vector<int> dims() { return dims_; }

  size_t memorySize(int cellSize) {
    return layout_->alignedElementCount(dims_) * cellSize;
  }

  int numel() { return layout_->elementCount(dims_); }

Y
Yan Chunwei 已提交
80 81
  int alignedElementCount() { return layout_->alignedElementCount(dims_); }

C
Chon 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  void setLayoutType(LayoutType layout) {
    this->layoutType_ = layout;
    switch (layout) {
      case NCHW:
        layout_ = &nchw_;
        break;
      case NHWC:
        layout_ = &nhwc_;
        break;
      case NC:
        layout_ = &nc_;
        break;
      case NHW:
        layout_ = &nhw_;
        break;
      case N:
        layout_ = &n_;
        break;
      default:
        break;
    }
  }

Y
Yan Chunwei 已提交
105 106
  void print() {}

C
Chon 已提交
107 108 109 110 111 112 113 114 115
  int operator[](int index) { return dims_[index]; }

 private:
  LayoutType layoutType_;
  Layout* layout_ = &nhwc_;
  std::vector<int> dims_;
};

}  // namespace zynqmp
Y
Yan Chunwei 已提交
116
}  // namespace paddle