Skip to content
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

Add nn_functor Check #7910

Merged
merged 39 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
8566a03
add bias_add_check
MARD1NO Mar 28, 2022
e50270b
add bias_add error test
MARD1NO Mar 28, 2022
b7a955b
fix conv2d nhwc bias_add error
MARD1NO Mar 28, 2022
c22fc0a
add nhwc conv test
MARD1NO Mar 28, 2022
aa908bc
add bias_add_error test
MARD1NO Mar 28, 2022
2c6425a
Merge branch 'master' into add_nn_functor_exception
MARD1NO Apr 14, 2022
188c4e5
Add bias add error check
MARD1NO Apr 14, 2022
6c1aa89
Rename
MARD1NO Apr 14, 2022
d16386f
add batch matmul error check
MARD1NO Apr 14, 2022
6db887e
add matmul check error msg
MARD1NO Apr 14, 2022
2f190b0
remove annotation
MARD1NO Apr 14, 2022
ac883b8
add fused mlp error msg check
MARD1NO Apr 14, 2022
2fd90af
Add pixel shuffle check test
MARD1NO Apr 14, 2022
a25c9c2
add more test until normalization add relu functor
MARD1NO Apr 14, 2022
0ce495d
refine error message
hjchen2 Apr 19, 2022
c4e6c2f
Merge branch 'master' into dev_refine_error_msg
hjchen2 Apr 20, 2022
a556c8e
Merge branch 'master' into add_nn_functor_exception
MARD1NO May 5, 2022
048e466
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow into …
hjchen2 May 5, 2022
6837ce6
Merge remote-tracking branch 'origin/dev_refine_error_msg' into add_n…
MARD1NO May 5, 2022
2f772d0
finish all nnfunctor check msg
MARD1NO May 5, 2022
f8b03d3
handle type error
hjchen2 May 5, 2022
d523734
remove useless symbol
MARD1NO May 5, 2022
cace34c
Merge branch 'dev_refine_error_msg' of github.com:Oneflow-Inc/oneflow…
MARD1NO May 5, 2022
ab17330
modify back to TypeError
MARD1NO May 5, 2022
cfee482
Merge branch 'master' into add_nn_functor_exception
MARD1NO Jun 6, 2022
b1c617d
fix all comment
MARD1NO Jun 6, 2022
a44ab0c
Remove redundant code
MARD1NO Jun 6, 2022
fa49d9f
Remove pad ndim check
MARD1NO Jun 6, 2022
5a02da8
Merge branch 'master' into add_nn_functor_exception
MARD1NO Jun 6, 2022
4e426e9
Merge branch 'master' into add_nn_functor_exception
MARD1NO Jun 7, 2022
cc599be
fix bias add space
MARD1NO Jun 7, 2022
db9d3c6
Merge branch 'add_nn_functor_exception' of github.com:Oneflow-Inc/one…
MARD1NO Jun 7, 2022
a27b57c
Merge branch 'master' into add_nn_functor_exception
mergify[bot] Jun 7, 2022
d2a950f
Merge branch 'master' into add_nn_functor_exception
mergify[bot] Jun 7, 2022
4d2429f
Merge branch 'master' into add_nn_functor_exception
mergify[bot] Jun 7, 2022
cdad56b
Merge branch 'master' into add_nn_functor_exception
mergify[bot] Jun 7, 2022
4418e70
Merge branch 'master' into add_nn_functor_exception
mergify[bot] Jun 7, 2022
c68e998
fix check logic cause ci gpu not always gpu:0
MARD1NO Jun 8, 2022
456544b
Merge branch 'add_nn_functor_exception' of github.com:Oneflow-Inc/one…
MARD1NO Jun 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 66 additions & 36 deletions oneflow/core/functional/impl/nn_functor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ class BiasAddFunctor {
const int64_t num_axes = x->shape()->NumAxes();
axis_val += num_axes;
}
CHECK_LT_OR_RETURN(axis_val, x->shape()->NumAxes())
<< Error::IndexError() << "Dimension out of range (expected to be in range of [ -"
<< x->shape()->NumAxes() << "," << x->shape()->NumAxes() - 1 << "], but got " << axis_val
<< ")";
CHECK_EQ_OR_RETURN(x->shape()->At(axis_val), bias->shape()->At(0))
<< Error::RuntimeError() << "The size of tensor x " << x->shape()->ToString()
<< " must match the size of tensor b " << bias->shape()->ToString() << " at dimension "
<< axis_val;
JUST(attrs.SetAttr<int32_t>("axis", axis_val));
return OpInterpUtil::Dispatch<Tensor>(*op_, {x, bias}, attrs);
}
Expand All @@ -82,8 +90,12 @@ class ConvBaseFunctor {
const std::string& channel_pos) const {
MutableAttrMap conv_attrs;
std::vector<int32_t> kernel_size_vec(num_spatial_dims_);
int32_t channel_idx = 1;
int32_t kernel_idx_offset = 2;
if (channel_pos == "channels_last") { kernel_idx_offset = 1; }
if (channel_pos == "channels_last") {
kernel_idx_offset = 1;
channel_idx = kernel_idx_offset + num_spatial_dims_;
}

for (int i = 0; i < num_spatial_dims_; i++) {
kernel_size_vec.at(i) = ((weight->shape())->At(i + kernel_idx_offset));
Expand All @@ -98,9 +110,7 @@ class ConvBaseFunctor {
const std::shared_ptr<one::Tensor>& conv_out =
JUST(OpInterpUtil::Dispatch<Tensor>(*conv_op_, {x, weight}, conv_attrs));
if (bias) {
MutableAttrMap bias_attrs;
JUST(bias_attrs.SetAttr<int32_t>("axis", 1));
return OpInterpUtil::Dispatch<Tensor>(*bias_op_, {conv_out, JUST(bias)}, bias_attrs);
return functional::BiasAdd(conv_out, JUST(bias), channel_idx);
} else {
return conv_out;
}
Expand Down Expand Up @@ -222,15 +232,18 @@ class MatMulFunctor {
const auto& b_shape = b->shape();

// TODO(): Support 1-d tensor by dot.
CHECK_GE_OR_RETURN(a_shape->NumAxes(), 2) << "Tensor a's dim should >= 2";
CHECK_GE_OR_RETURN(b_shape->NumAxes(), 2) << "Tensor b's dim should >= 2";
CHECK_GE_OR_RETURN(a_shape->NumAxes(), 2)
<< Error::RuntimeError() << "Tensor a's dim should >= 2";
CHECK_GE_OR_RETURN(b_shape->NumAxes(), 2)
<< Error::RuntimeError() << "Tensor b's dim should >= 2";

MutableAttrMap attrs;
JUST(attrs.SetAttr<bool>("transpose_a", transpose_a));
JUST(attrs.SetAttr<bool>("transpose_b", transpose_b));
JUST(attrs.SetAttr<double>("alpha", alpha));
if (a_shape->NumAxes() != b_shape->NumAxes()) {
CHECK_EQ_OR_RETURN(b_shape->NumAxes(), 2)
<< Error::RuntimeError()
<< "Not support number of dimensions of a being less than number of dimensions of b!";
return OpInterpUtil::Dispatch<Tensor>(*bcast_matmul_op_, {a, b}, attrs);
}
Expand Down Expand Up @@ -433,9 +446,10 @@ class FusedMLPFunctor {
const TensorTuple& biases, bool skip_final_activation) const {
const int64_t weight_size = weights.size();
const int64_t bias_size = biases.size();
CHECK_GE_OR_RETURN(weight_size, 1) << "The number of weights should be greater equal than 1. ";
CHECK_GE_OR_RETURN(weight_size, 1)
<< Error::RuntimeError() << "The number of weights should be greater equal than 1. ";
CHECK_EQ_OR_RETURN(weight_size, bias_size)
<< "The number of weights should be equal to biases. ";
<< Error::RuntimeError() << "The number of weights should be equal to biases. ";
int64_t n = 0, k = 0;
/*
x: (m, k)
Expand All @@ -449,13 +463,16 @@ class FusedMLPFunctor {
const auto& bias_shape = biases[i]->shape();

// TODO(): Support Fused batch/broadcast matmul.
CHECK_EQ_OR_RETURN(weight_shape->NumAxes(), 2) << "Weight's dim should == 2";
CHECK_EQ_OR_RETURN(bias_shape->NumAxes(), 1) << "Bias's dim should == 1";
CHECK_EQ_OR_RETURN(weight_shape->NumAxes(), 2)
<< Error::RuntimeError() << "Weight's dim size should == 2";
CHECK_EQ_OR_RETURN(bias_shape->NumAxes(), 1)
<< Error::RuntimeError() << "Bias's dim size should == 1";

n = weight_shape->At(0);
CHECK_EQ_OR_RETURN(bias_shape->At(0), n) << "Bias's dim is not equal to weight's last dim. ";
CHECK_EQ_OR_RETURN(bias_shape->At(0), n)
<< Error::RuntimeError() << "Bias's dim is not equal to weight's first dim. ";
CHECK_EQ_OR_RETURN(weight_shape->At(1), k)
<< "weight's first dim should be equal to input's last dim. ";
<< Error::RuntimeError() << "weight's second dim should be equal to input's second dim. ";

// Set for next layer.
k = n;
Expand Down Expand Up @@ -564,13 +581,14 @@ class PixelShuffleFunctor {
PixelShuffleFunctor() {}
Maybe<Tensor> operator()(const std::shared_ptr<one::Tensor>& x, const int64_t& h_upscale_factor,
const int64_t& w_upscale_factor) const {
CHECK_OR_RETURN(x->ndim() == 4) << "Only Accept 4D Tensor";
CHECK_OR_RETURN(x->ndim() == 4) << Error::RuntimeError() << "Only Accept 4D Tensor";
const int64_t batch = x->shape()->At(0);
const int64_t channel = x->shape()->At(1);
const int64_t height = x->shape()->At(2);
const int64_t width = x->shape()->At(3);
std::shared_ptr<one::Tensor> out;
CHECK_OR_RETURN(channel % (h_upscale_factor * w_upscale_factor) == 0)
<< Error::RuntimeError()
<< "The channels of input tensor must be divisible by (upscale_factor * upscale_factor) or "
"(h_upscale_factor * w_upscale_factor)";
const int64_t new_c = static_cast<int>(channel / (h_upscale_factor * w_upscale_factor));
Expand Down Expand Up @@ -742,7 +760,7 @@ class LossFunctorBase {
public:
Maybe<Tensor> apply_reduction(const Maybe<Tensor>& x, const std::string& reduction) const {
CHECK_OR_RETURN(reduction == "none" || reduction == "sum" || reduction == "mean")
<< "Reduction should be none, sum or mean.";
<< Error::RuntimeError() << "Reduction should be none, sum or mean.";
if (reduction == "sum") { return functional::ReduceSum(JUST(x), {}, false); }
if (reduction == "mean") { return functional::ReduceMean(JUST(x), {}, false); }
return x;
Expand Down Expand Up @@ -950,12 +968,15 @@ class NllLossFunctor {
const Optional<one::Tensor>& weight, const int64_t& ignore_index,
const std::string& reduction) const {
CHECK_OR_RETURN(reduction == "none" || reduction == "sum" || reduction == "mean")
<< "Reduction should be none, sum or mean.";
<< Error::RuntimeError() << "Reduction should be none, sum or mean.";

const auto& input_shape = input->shape();
const auto& target_shape = target->shape();
CHECK_LE_OR_RETURN(input_shape->NumAxes(), 5);
CHECK_EQ_OR_RETURN(input_shape->NumAxes() - 1, target_shape->NumAxes());
CHECK_LE_OR_RETURN(input_shape->NumAxes(), 5)
<< Error::RuntimeError() << "The number of input's axis should be less equal to 5. ";
CHECK_EQ_OR_RETURN(input_shape->NumAxes() - 1, target_shape->NumAxes())
<< Error::RuntimeError()
<< "The number of input's axis should be equal to the number of target's axis - 1. ";

MutableAttrMap attrs;
JUST(attrs.SetAttr<int64_t>("ignore_index", ignore_index));
Expand Down Expand Up @@ -1016,7 +1037,7 @@ class CrossEntropyFunctor {
const Optional<one::Tensor>& weight, const int64_t& ignore_index,
const std::string& reduction) const {
CHECK_OR_RETURN(reduction == "none" || reduction == "sum" || reduction == "mean")
<< "Reduction should be none, sum or mean.";
<< Error::RuntimeError() << "Reduction should be none, sum or mean.";
const auto& input_shape = input->shape();
const auto& target_shape = target->shape();
MutableAttrMap attrs;
Expand Down Expand Up @@ -1227,7 +1248,8 @@ class SparseSoftmaxCrossEntropyFunctor {
sbp.mutable_broadcast_parallel();
new_sbp_parallels.emplace_back(sbp);
} else {
CHECK_EQ_OR_RETURN(split_axis, 0);
CHECK_EQ_OR_RETURN(split_axis, 0)
<< Error::RuntimeError() << "Split axis must equal to 0. ";
new_sbp_parallels.emplace_back(sbp_parallel);
}
} else {
Expand Down Expand Up @@ -1406,7 +1428,8 @@ class CtcLossFunctor {
CHECK_OR_RETURN([&]() -> bool {
if ((reduction != "none") && (reduction != "sum") && (reduction != "mean")) return false;
return true;
}());
}()) << Error::RuntimeError()
<< "Reduction should be none, sum or mean.";
if (reduction == "sum") { return functional::ReduceSum(out, {}, false); }
if (reduction == "mean") {
return sequence_function(functional::Clamp)
Expand Down Expand Up @@ -1441,7 +1464,8 @@ class TripletMarginLossFunctor {
CHECK_OR_RETURN([&]() -> bool {
if ((reduction != "none") && (reduction != "sum") && (reduction != "mean")) return false;
return true;
}());
}()) << Error::RuntimeError()
<< "Reduction should be none, sum or mean.";
auto da_p = JUST(VectorNorm(
JUST(ScalarAdd(eps, JUST(Sub(anchor, positive, /*alpha=*/1.0, /*inplace=*/false)),
/*alpha=*/1)),
Expand Down Expand Up @@ -1539,14 +1563,14 @@ class NormalFunctor {
if (optional_dtype.has_value()) {
CHECK_OR_RETURN(output_tensor_dtype == dtype)
<< Error::RuntimeError() << "data type " << dtype->name()
<< " does not match data type of out parameter (" << output_tensor_dtype->name();
<< " does not match data type of out parameter " << output_tensor_dtype->name();
}
dtype = output_tensor_dtype;
Symbol<Device> out_tensor_device = JUST(out_tensor->device());
if (optional_device.has_value()) {
CHECK_OR_RETURN(out_tensor_device == JUST(optional_device))
<< Error::RuntimeError() << "device type " << device->ToString()
<< " does not match device type of out parameter (" << out_tensor_device->ToString();
<< " does not match device type of out parameter " << out_tensor_device->ToString();
}
device = out_tensor_device;
}
Expand Down Expand Up @@ -1687,13 +1711,14 @@ class NormalizationFunctor {
JUST(attrs.SetAttr<float>("momentum", 1.0 - momentum));

CHECK_OR_RETURN((moving_mean && moving_variance) || (!moving_mean && !moving_variance))
<< Error::RuntimeError()
<< "Both moving_mean and moving_variance should be None or Tensor.";

std::shared_ptr<one::Tensor> gamma_val;
std::shared_ptr<one::Tensor> beta_val;

CHECK_GE_OR_RETURN(x->shape()->NumAxes(), 2)
<< "NumAxes of x should be greater or equal than 2. ";
<< Error::RuntimeError() << "NumAxes of x should be greater or equal than 2. ";
if (gamma.has_value() && beta.has_value()) {
gamma_val = JUST(gamma);
beta_val = JUST(beta);
Expand All @@ -1705,7 +1730,7 @@ class NormalizationFunctor {

if (!training) {
CHECK_OR_RETURN(moving_mean && moving_variance)
<< "Must have moving_mean and moving_variance in eval mode.";
<< Error::RuntimeError() << "Must have moving_mean and moving_variance in eval mode.";
return OpInterpUtil::Dispatch<one::Tensor>(
*norm_eval_op_, {x, JUST(moving_mean), JUST(moving_variance), gamma_val, beta_val},
attrs);
Expand Down Expand Up @@ -1801,10 +1826,11 @@ class NormalizationAddReluFunctor {
JUST(attrs.SetAttr<float>("momentum", 1.0f - momentum));

CHECK_OR_RETURN((moving_mean && moving_variance) || (!moving_mean && !moving_variance))
<< Error::RuntimeError()
<< "Both moving_mean and moving_variance should be None or Tensor.";
if (!is_training) {
CHECK_OR_RETURN(moving_mean && moving_variance)
<< "Must have moving_mean and moving_variance in eval mode.";
<< Error::RuntimeError() << "Must have moving_mean and moving_variance in eval mode.";
const auto& normalize_result = JUST(OpInterpUtil::Dispatch<one::Tensor>(
*norm_eval_op_, {x, JUST(moving_mean), JUST(moving_variance), gamma, beta}, attrs));
if (addend) {
Expand Down Expand Up @@ -1856,12 +1882,13 @@ class PadFunctor {
const std::string& mode, const Scalar& value) const {
const int64_t ndim = x->shape()->NumAxes();
CHECK_LE_OR_RETURN(pad.size(), 2 * ndim)
<< "Pad size should less than or equal to input axes * 2.";
<< Error::RuntimeError() << "Pad size should less than or equal to input axes * 2.";
MutableAttrMap attrs;
JUST(attrs.SetAttr<std::vector<int64_t>>("padding", pad));
if (mode == "constant") {
CHECK_EQ_OR_RETURN(pad.size() % 2, 0)
<< "Length of pad must be even but instead it equals " << pad.size();
<< Error::RuntimeError() << "Length of pad must be even but instead it equals "
<< pad.size();
if (IsFloatingDataType(x->dtype()->data_type())
|| x->dtype()->data_type() == DataType::kFloat16) {
JUST(attrs.SetAttr<double>("floating_constant_value", value.As<double>()));
Expand All @@ -1888,6 +1915,7 @@ class PadFunctor {
const int64_t pad_h = x->shape()->dim_vec().at(2);
const int64_t pad_w = x->shape()->dim_vec().at(3);
CHECK_OR_RETURN(pad[2] < pad_h && pad[3] < pad_h && pad[0] < pad_w && pad[1] < pad_w)
<< Error::RuntimeError()
<< "padding size should be less than the corresponding input dimension!";
return OpInterpUtil::Dispatch<Tensor>(*reflect_pad_, {x}, attrs);
} else if (mode == "replicate") {
Expand Down Expand Up @@ -2036,7 +2064,8 @@ class UnfoldFunctor {
const std::vector<int32_t>& strides) const {
const auto& x_shape = x->shape();
// Only Support 4d tensor now.
CHECK_EQ_OR_RETURN(x_shape->NumAxes(), 4) << "Input Tensor dim should == 4";
CHECK_EQ_OR_RETURN(x_shape->NumAxes(), 4)
<< Error::RuntimeError() << "Input Tensor dim should == 4";
MutableAttrMap attrs;
JUST(attrs.SetAttr<std::string>("data_format", data_format));
JUST(attrs.SetAttr<std::vector<int32_t>>("kernel_size", kernel_size));
Expand All @@ -2062,7 +2091,8 @@ class FoldFunctor {
const std::vector<int32_t>& strides) const {
const auto& x_shape = x->shape();
// Only Support 3d tensor fold now. format is (N, C*K*K, L)
CHECK_EQ_OR_RETURN(x_shape->NumAxes(), 3) << "Input Tensor dim should == 3";
CHECK_EQ_OR_RETURN(x_shape->NumAxes(), 3)
<< Error::RuntimeError() << "Input Tensor dim should == 3";
MutableAttrMap attrs;
JUST(attrs.SetAttr<std::string>("data_format", data_format));
JUST(attrs.SetAttr<std::vector<int32_t>>("output_size", output_size));
Expand All @@ -2085,9 +2115,8 @@ class OneHotFunctor {
}
Maybe<Tensor> operator()(const std::shared_ptr<one::Tensor>& input, const int64_t& num_classes,
const Scalar& on_value, const Scalar& off_value) const {
if (IsFloatingDataType(input->dtype()->data_type())) {
OF_RUNTIME_ERROR() << "one_hot is only applicable to index tensor.";
}
CHECK_OR_RETURN(!IsFloatingDataType(input->dtype()->data_type()))
<< Error::RuntimeError() << "one_hot is only applicable to index tensor.";
MutableAttrMap attrs;
if (num_classes == -1) {
std::vector<int32_t> axis(input->ndim());
Expand Down Expand Up @@ -2190,9 +2219,10 @@ class L2NormalizeFunctor {
const auto final_dim = ndims - 1;

auto axis_ = axis >= 0 ? axis : axis + ndims;
CHECK_GE_OR_RETURN(axis_, 0) << "Axis should >=0 but axis is " << axis_ << " now.";
CHECK_LE_OR_RETURN(axis_, final_dim)
<< "Axis should <" << ndims << " but axis is " << axis_ << " now.";
CHECK_GE_OR_RETURN(axis_, 0) << Error::RuntimeError() << "Axis should >=0 but axis is " << axis_
<< " now.";
CHECK_LE_OR_RETURN(axis_, final_dim) << Error::RuntimeError() << "Axis should < " << ndims
<< " but axis is " << axis_ << " now.";

MutableAttrMap attrs;
JUST(attrs.SetAttr<float>("epsilon", epsilon));
Expand Down
Loading