2022-10-15 16:29:28 +08:00
|
|
|
#pragma once
|
|
|
|
#include "core/operator.h"
|
|
|
|
|
|
|
|
namespace infini {
|
2023-02-13 22:48:20 +08:00
|
|
|
/**
|
|
|
|
* @brief See https://arxiv.org/abs/1502.03167 for the detail of batch
|
|
|
|
* normalization.
|
|
|
|
*
|
|
|
|
*/
|
2022-10-15 16:29:28 +08:00
|
|
|
class BatchNormObj : public OperatorObj {
|
|
|
|
float momentum, eps;
|
2023-04-18 15:10:33 +08:00
|
|
|
bool trainingMode;
|
2022-10-15 16:29:28 +08:00
|
|
|
|
|
|
|
public:
|
2023-02-13 22:48:20 +08:00
|
|
|
/**
|
|
|
|
* @brief Construct a new BatchNorm object.
|
|
|
|
*
|
|
|
|
* @param graph The computation graph that this operator belongs to.
|
|
|
|
* @param input The input tensor of BatchNorm. For image data, the input
|
|
|
|
* shape is usually [N, C, H, W].
|
|
|
|
* @param output The output tensor of BatchNorm, which should have the same
|
|
|
|
* shape as the input tensor.
|
|
|
|
* @param mean The mean tensor, which has a shape of [C].
|
|
|
|
* @param var The var tensor, which has a shape of [C].
|
|
|
|
* @param scale The scale tensor, which has a shape of [C].
|
|
|
|
* @param bias The bias tensor, which has a shape of [C].
|
|
|
|
* @param momentum Factor used in computing the running mean and variance.
|
|
|
|
* Default is 0.9.
|
|
|
|
* @param eps The epsilon value to use to avoid division by zero. Default is
|
|
|
|
* 1e-5.
|
2023-04-18 15:10:33 +08:00
|
|
|
* @param trainingMode Set to true when used for training.
|
2023-02-13 22:48:20 +08:00
|
|
|
*/
|
2022-10-15 16:29:28 +08:00
|
|
|
BatchNormObj(GraphObj *graph, Tensor input, Tensor output, Tensor mean,
|
|
|
|
Tensor var, Tensor scale, Tensor bias, float momentum = 0.9,
|
2023-04-18 15:10:33 +08:00
|
|
|
float eps = 1e-5, bool trainingMode = false);
|
2023-02-12 18:27:52 +08:00
|
|
|
OP_CLONE(BatchNormObj);
|
2023-11-23 13:11:50 +08:00
|
|
|
optional<vector<Shape>> inferShape(const TensorVec &inputs) override;
|
2022-10-15 16:29:28 +08:00
|
|
|
std::string toString() const override;
|
|
|
|
|
|
|
|
// output size will be 3 when training
|
2023-03-25 12:04:24 +08:00
|
|
|
int numInputs() const override { return 5; }
|
|
|
|
int numOutputs() const override { return outputs.size(); }
|
|
|
|
float getMomentum() const { return momentum; }
|
|
|
|
float getEps() const { return eps; }
|
2023-04-18 15:10:33 +08:00
|
|
|
bool getTrainingMode() const { return trainingMode; }
|
2022-10-15 16:29:28 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
vector<int> getWorkloadVector() const override;
|
|
|
|
vector<int> getOpAttrVector() const override;
|
|
|
|
|
|
|
|
vector<DataType> inferDataType(const TensorVec &inputs) const override;
|
|
|
|
};
|
|
|
|
} // namespace infini
|