-
Notifications
You must be signed in to change notification settings - Fork 15
Extend BatchNorm with rank-matched channels and optional momentum #278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
f50f15c
a001dfb
206eed0
df6d839
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,8 +37,8 @@ namespace fusilli { | |
| // each channel C. The input X has logical shape [N, C, *] where C is at | ||
| // dimension 1 in logical (NCHW) order. | ||
| // | ||
| // Scale (gamma), bias (beta), running mean, and running variance are all 1D | ||
| // tensors of shape [C]. | ||
| // Scale (gamma), bias (beta), running mean, and running variance are all | ||
| // rank-matched tensors of shape [1, C, 1, ..., 1]. | ||
| // | ||
| // Inference: requires running MEAN and VAR; outputs Y only. | ||
| // Training: running MEAN and VAR are optional; outputs Y, SAVED_MEAN, and | ||
|
|
@@ -125,12 +125,12 @@ class BatchNormNode : public NodeCRTP<BatchNormNode> { | |
| FUSILLI_RETURN_ERROR_IF(!eT->isScalar(), ErrorCode::InvalidAttribute, | ||
| "BatchNorm epsilon must be a scalar constant"); | ||
|
|
||
| // Momentum checks. | ||
| // Momentum checks (optional — omitting uses the PyTorch default 0.1). | ||
| std::shared_ptr<TensorAttr> mT = batchnormAttr.getMomentum(); | ||
| FUSILLI_RETURN_ERROR_IF(!mT, ErrorCode::AttributeNotSet, | ||
| "BatchNorm momentum not set"); | ||
| FUSILLI_RETURN_ERROR_IF(!mT->isScalar(), ErrorCode::InvalidAttribute, | ||
| "BatchNorm momentum must be a scalar constant"); | ||
| if (mT) { | ||
| FUSILLI_RETURN_ERROR_IF(!mT->isScalar(), ErrorCode::InvalidAttribute, | ||
| "BatchNorm momentum must be a scalar constant"); | ||
| } | ||
IanWood1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return ok(); | ||
| } | ||
|
|
@@ -145,25 +145,34 @@ class BatchNormNode : public NodeCRTP<BatchNormNode> { | |
| std::shared_ptr<TensorAttr> yT = batchnormAttr.getY(); | ||
|
|
||
| const std::vector<int64_t> &xDim = xT->getDim(); | ||
| const std::vector<int64_t> channel1DDim = {xDim[1]}; | ||
| const std::vector<int64_t> channel1DStride = {1}; | ||
| size_t xRank = xDim.size(); | ||
| // Build the rank-matched channel dim: [1, C, 1, ..., 1] | ||
| std::vector<int64_t> channelRankMatchedDim(xRank, 1); | ||
| channelRankMatchedDim[1] = xDim[1]; | ||
|
|
||
| auto infer1DTensor = [&](const std::shared_ptr<TensorAttr> &t) { | ||
| auto inferChannelTensor = [&](const std::shared_ptr<TensorAttr> &t) { | ||
| if (t->getDim().empty()) | ||
| t->setDim(channel1DDim); | ||
| if (t->getStride().empty()) | ||
| t->setStride(channel1DStride); | ||
| t->setDim(channelRankMatchedDim); | ||
| if (t->getStride().empty()) { | ||
| const std::vector<int64_t> &dim = t->getDim(); | ||
| size_t rank = dim.size(); | ||
| // Compute contiguous stride for the rank-matched tensor. | ||
| std::vector<int64_t> stride(rank, 1); | ||
| for (int64_t i = (int64_t)rank - 2; i >= 0; --i) | ||
| stride[i] = stride[i + 1] * dim[i + 1]; | ||
| t->setStride(stride); | ||
| } | ||
rsuderman marked this conversation as resolved.
Show resolved
Hide resolved
rsuderman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| // Infer 1D channel tensors. | ||
| // Infer rank-matched channel tensors. | ||
| if (auto sT = batchnormAttr.getSCALE()) | ||
| infer1DTensor(sT); | ||
| inferChannelTensor(sT); | ||
| if (auto bT = batchnormAttr.getBIAS()) | ||
| infer1DTensor(bT); | ||
| inferChannelTensor(bT); | ||
| if (auto meanT = batchnormAttr.getMEAN()) | ||
| infer1DTensor(meanT); | ||
| inferChannelTensor(meanT); | ||
| if (auto varT = batchnormAttr.getVAR()) | ||
| infer1DTensor(varT); | ||
| inferChannelTensor(varT); | ||
|
|
||
| // Infer shape and stride of output Y tensor (same as X). | ||
| if (yT->getDim().empty()) | ||
|
|
@@ -173,8 +182,8 @@ class BatchNormNode : public NodeCRTP<BatchNormNode> { | |
|
|
||
| // Infer saved statistics shapes for training. | ||
| if (isTrainingForwardPhase()) { | ||
| infer1DTensor(batchnormAttr.getSAVED_MEAN()); | ||
| infer1DTensor(batchnormAttr.getSAVED_INV_VARIANCE()); | ||
| inferChannelTensor(batchnormAttr.getSAVED_MEAN()); | ||
| inferChannelTensor(batchnormAttr.getSAVED_INV_VARIANCE()); | ||
| } | ||
|
|
||
| return ok(); | ||
|
|
@@ -188,7 +197,6 @@ class BatchNormNode : public NodeCRTP<BatchNormNode> { | |
| std::shared_ptr<TensorAttr> yT = batchnormAttr.getY(); | ||
|
|
||
| const std::vector<int64_t> &xDim = xT->getDim(); | ||
| const std::vector<int64_t> expectedCDim = {xDim[1]}; | ||
|
|
||
| // Shape check for output Y tensor. | ||
| FUSILLI_RETURN_ERROR_IF( | ||
|
|
@@ -202,32 +210,51 @@ class BatchNormNode : public NodeCRTP<BatchNormNode> { | |
| "' is neither contiguous nor channels-last as " | ||
| "defined by its stride"); | ||
|
|
||
| // Shape checks for 1D channel tensors. | ||
| auto check1DShape = [&](const std::shared_ptr<TensorAttr> &t, | ||
| const std::string &name) -> ErrorObject { | ||
| // Shape checks for rank-matched channel tensors of form [1, C, 1, ..., 1]. | ||
| auto checkChannelShape = [&](const std::shared_ptr<TensorAttr> &t, | ||
| const std::string &name) -> ErrorObject { | ||
| if (!t) | ||
| return ok(); | ||
| const std::vector<int64_t> &tDim = t->getDim(); | ||
| const std::vector<int64_t> &tStride = t->getStride(); | ||
| size_t xRank = xDim.size(); | ||
|
|
||
| if (tDim.size() == xRank) { | ||
| // Rank-matched form [1, C, 1, ..., 1]: channel dim must equal C and | ||
| // all other dims must be 1. | ||
| bool validShape = (tDim[1] == xDim[1]); | ||
| for (size_t i = 0; i < xRank && validShape; ++i) | ||
| if (i != 1 && tDim[i] != 1) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be directly used as the condition for FUSILLI_RETURN_ERROR_IF? |
||
| validShape = false; | ||
| FUSILLI_RETURN_ERROR_IF( | ||
| !validShape, ErrorCode::InvalidAttribute, | ||
| "BatchNorm tensor " + name + | ||
| " must be rank-matched with ones in all non-feature dimensions"); | ||
|
||
| FUSILLI_RETURN_ERROR_IF( | ||
| tStride.size() != xRank || tStride[1] != 1, | ||
| ErrorCode::InvalidAttribute, | ||
| "BatchNorm tensor " + name + | ||
| " must have unit stride at the channel dimension"); | ||
rsuderman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return ok(); | ||
| } | ||
|
|
||
| FUSILLI_RETURN_ERROR_IF( | ||
| t->getDim() != expectedCDim, ErrorCode::InvalidAttribute, | ||
| true, ErrorCode::InvalidAttribute, | ||
| "BatchNorm tensor " + name + | ||
| " must be 1D with size equal to channel dimension C"); | ||
| FUSILLI_RETURN_ERROR_IF(t->getStride() != std::vector<int64_t>{1}, | ||
| ErrorCode::InvalidAttribute, | ||
| "BatchNorm tensor " + name + | ||
| " must have unit stride"); | ||
| " must be rank-matched with ones in all non-feature dimensions"); | ||
| return ok(); | ||
| }; | ||
|
|
||
| FUSILLI_CHECK_ERROR(check1DShape(batchnormAttr.getSCALE(), "SCALE")); | ||
| FUSILLI_CHECK_ERROR(check1DShape(batchnormAttr.getBIAS(), "BIAS")); | ||
| FUSILLI_CHECK_ERROR(check1DShape(batchnormAttr.getMEAN(), "MEAN")); | ||
| FUSILLI_CHECK_ERROR(check1DShape(batchnormAttr.getVAR(), "VAR")); | ||
| FUSILLI_CHECK_ERROR(checkChannelShape(batchnormAttr.getSCALE(), "SCALE")); | ||
| FUSILLI_CHECK_ERROR(checkChannelShape(batchnormAttr.getBIAS(), "BIAS")); | ||
| FUSILLI_CHECK_ERROR(checkChannelShape(batchnormAttr.getMEAN(), "MEAN")); | ||
| FUSILLI_CHECK_ERROR(checkChannelShape(batchnormAttr.getVAR(), "VAR")); | ||
|
|
||
| if (isTrainingForwardPhase()) { | ||
| FUSILLI_CHECK_ERROR( | ||
| check1DShape(batchnormAttr.getSAVED_MEAN(), "SAVED_MEAN")); | ||
| FUSILLI_CHECK_ERROR(check1DShape(batchnormAttr.getSAVED_INV_VARIANCE(), | ||
| "SAVED_INV_VARIANCE")); | ||
| checkChannelShape(batchnormAttr.getSAVED_MEAN(), "SAVED_MEAN")); | ||
| FUSILLI_CHECK_ERROR(checkChannelShape(batchnormAttr.getSAVED_INV_VARIANCE(), | ||
| "SAVED_INV_VARIANCE")); | ||
| } | ||
|
|
||
| return ok(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the same behavior as hipdnn/cudnn?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mentioned above - it should be an optional value altogether, e.g. inference mode doesn't use it. But it does not even appear consistent without pytorch's usecase. I won't be surprised if we have to come by and tweak this once we determine the pytorch integration plan.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took a look at HipDNN and it appears momentum is optional but never given a default. Is the pytorch integration possible by setting a default value there instead of here? I think we want to keep behavior in line with hipdnn as much as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what this means. Does pytorch always omit momentum in their testing and just use the defafult (0.1)?