diff --git a/mindspore/lite/nnacl/fp32_grad/binary_cross_entropy.c b/mindspore/lite/nnacl/fp32_grad/binary_cross_entropy.c new file mode 100644 index 00000000000..00f8bfb0e43 --- /dev/null +++ b/mindspore/lite/nnacl/fp32_grad/binary_cross_entropy.c @@ -0,0 +1,60 @@ +/* + * Copyright 2020 Huawei Technologies Co., Ltd + * + * 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. + */ + +#include +#include "nnacl/fp32_grad/binary_cross_entropy.h" + +static void BinaryCrossEntropyLossKernel(const int input_size, const int reduction, const float *input_x, + const float *input_y, const float *weight, float *loss, float *tmp_loss) { + float epsilon = 1e-12; + if (reduction == 0) { + for (int i = 0; i < input_size; i++) { + float value = + -weight[i] * (input_y[i] * logf(input_x[i] + epsilon) + (1 - input_y[i]) * logf(1 - input_x[i] + epsilon)); + loss[i] = value; + } + } else { + for (int i = 0; i < input_size; i++) { + float value = + -weight[i] * (input_y[i] * logf(input_x[i] + epsilon) + (1 - input_y[i]) * logf(1 - input_x[i] + epsilon)); + tmp_loss[i] = value; + } + } +} + +void BinaryCrossEntropy(const int input_size, const int reduction, const float *input_x, const float *input_y, + const float *weight, float *loss, float *tmp_loss) { + loss[0] = 0.0f; + BinaryCrossEntropyLossKernel(input_size, reduction, input_x, input_y, weight, loss, tmp_loss); + if (reduction != 0) { + if (input_size % 2 == 1) { + tmp_loss[0] += tmp_loss[input_size - 1]; + } + for (int stride = input_size / 2; stride > 0; stride >>= 1) { + for (int i = 0; i < stride; i++) { + tmp_loss[i] += tmp_loss[i + stride]; + } + + if (stride > 2 && stride % 2 == 1) { + tmp_loss[0] += tmp_loss[stride - 1]; + } + } + loss[0] += tmp_loss[0]; + if (reduction == 1) { + loss[0] /= input_size; + } + } +} diff --git a/mindspore/lite/nnacl/fp32_grad/binary_cross_entropy.h b/mindspore/lite/nnacl/fp32_grad/binary_cross_entropy.h new file mode 100644 index 00000000000..dc921691cd3 --- /dev/null +++ b/mindspore/lite/nnacl/fp32_grad/binary_cross_entropy.h @@ -0,0 +1,36 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_BINARY_CROSS_ENTROPY_H_ +#define MINDSPORE_LITE_NNACL_BINARY_CROSS_ENTROPY_H_ + +#include "nnacl/op_base.h" + +typedef struct BinaryCrossEntropyParameter { + OpParameter op_parameter_; + int reduction; +} BinaryCrossEntropyParameter; + +#ifdef __cplusplus +extern "C" { +#endif + +void BinaryCrossEntropy(const int input_size, const int reduction, const float *input_x, const float *input_y, + const float *weight, float *loss, float *tmp_loss); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_BINARY_CROSS_ENTROPY_H_ diff --git a/mindspore/lite/nnacl/fp32_grad/binary_cross_entropy_grad.c b/mindspore/lite/nnacl/fp32_grad/binary_cross_entropy_grad.c new file mode 100644 index 00000000000..b6fd8b86433 --- /dev/null +++ b/mindspore/lite/nnacl/fp32_grad/binary_cross_entropy_grad.c @@ -0,0 +1,42 @@ +/* + * Copyright 2020 Huawei Technologies Co., Ltd + * + * 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. + */ + +#include "nnacl/fp32_grad/binary_cross_entropy_grad.h" + +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +int BinaryCrossEntropyGrad(const int input_size, const int reduction, const float *input_x, const float *input_y, + const float *weight, const float *dloss, float *dx) { + float epsilon = 1e-12; + if (reduction == 0) { + for (int i = 0; i < input_size; i++) { + float denominator = MAX(input_x[i] * (1 - input_x[i]), epsilon); + float value = weight[i] * (input_x[i] - input_y[i]) / denominator; + dx[i] = value * dloss[i]; + } + } else { + float dloss1 = dloss[0]; + if (reduction == 1) { + dloss1 = dloss[0] / input_size; + } + for (int i = 0; i < input_size; i++) { + float denominator = MAX(input_x[i] * (1 - input_x[i]), epsilon); + float value = weight[i] * (input_x[i] - input_y[i]) / denominator; + dx[i] = value * dloss1; + } + } + return 0; +} diff --git a/mindspore/lite/nnacl/fp32_grad/binary_cross_entropy_grad.h b/mindspore/lite/nnacl/fp32_grad/binary_cross_entropy_grad.h new file mode 100644 index 00000000000..f3c7c3952da --- /dev/null +++ b/mindspore/lite/nnacl/fp32_grad/binary_cross_entropy_grad.h @@ -0,0 +1,36 @@ +/** + * Copyright 2020 Huawei Technologies Co., Ltd + * + * 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. + */ +#ifndef MINDSPORE_LITE_NNACL_BINARY_CROSS_ENTROPY_GRAD_H_ +#define MINDSPORE_LITE_NNACL_BINARY_CROSS_ENTROPY_GRAD_H_ + +#include "nnacl/op_base.h" + +typedef struct BinaryCrossEntropyGradParameter { + OpParameter op_parameter_; + int reduction; +} BinaryCrossEntropyGradParameter; + +#ifdef __cplusplus +extern "C" { +#endif + +int BinaryCrossEntropyGrad(const int input_size, const int reduction, const float *input_x, const float *input_y, + const float *weight, const float *dloss, float *dx); + +#ifdef __cplusplus +} +#endif +#endif // MINDSPORE_LITE_NNACL_BINARY_CROSS_ENTROPY_GRAD_H_ diff --git a/mindspore/lite/src/ops/assign_add.cc b/mindspore/lite/src/ops/assign_add.cc index 882c86a1768..fe3c874fba4 100644 --- a/mindspore/lite/src/ops/assign_add.cc +++ b/mindspore/lite/src/ops/assign_add.cc @@ -15,6 +15,11 @@ */ #include "src/ops/assign_add.h" + +#ifndef PRIMITIVE_WRITEABLE +#include "src/ops/ops_register.h" +#endif + namespace mindspore { namespace lite { #ifdef PRIMITIVE_WRITEABLE @@ -58,7 +63,13 @@ int AssignAdd::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffe fbb->Finish(prim_offset); return RET_OK; } + +PrimitiveC *AssignAddCreator(const schema::Primitive *primitive) { + return PrimitiveC::NewPrimitiveC(primitive); +} +Registry AssignAddRegistry(schema::PrimitiveType_AssignAdd, AssignAddCreator); #endif + int AssignAdd::InferShape(std::vector inputs_, std::vector outputs_) { Tensor *x = inputs_[0]; Tensor *y = inputs_[1]; diff --git a/mindspore/lite/src/ops/binary_cross_entropy.cc b/mindspore/lite/src/ops/binary_cross_entropy.cc index 73cd9335530..f0d02fdbfec 100644 --- a/mindspore/lite/src/ops/binary_cross_entropy.cc +++ b/mindspore/lite/src/ops/binary_cross_entropy.cc @@ -17,6 +17,10 @@ #include #include "src/ops/binary_cross_entropy.h" +#ifndef PRIMITIVE_WRITEABLE +#include "src/ops/ops_register.h" +#endif + namespace mindspore { namespace lite { #ifdef PRIMITIVE_WRITEABLE @@ -85,6 +89,11 @@ int BinaryCrossEntropy::UnPackToFlatBuilder(const schema::Primitive *primitive, } int BinaryCrossEntropy::GetReduction() const { return this->primitive_->value_as_BinaryCrossEntropy()->reduction(); } + +PrimitiveC *BinaryCrossEntropyCreator(const schema::Primitive *primitive) { + return PrimitiveC::NewPrimitiveC(primitive); +} +Registry BinaryCrossEntropyRegistry(schema::PrimitiveType_BinaryCrossEntropy, BinaryCrossEntropyCreator); #endif int BinaryCrossEntropy::InferShape(std::vector inputs_, std::vector outputs_) { Tensor *x = inputs_[0]; diff --git a/mindspore/lite/src/ops/binary_cross_entropy_grad.cc b/mindspore/lite/src/ops/binary_cross_entropy_grad.cc index 0fe7739992e..3b161ad4dc9 100644 --- a/mindspore/lite/src/ops/binary_cross_entropy_grad.cc +++ b/mindspore/lite/src/ops/binary_cross_entropy_grad.cc @@ -17,6 +17,10 @@ #include #include "src/ops/binary_cross_entropy_grad.h" +#ifndef PRIMITIVE_WRITEABLE +#include "src/ops/ops_register.h" +#endif + namespace mindspore { namespace lite { #ifdef PRIMITIVE_WRITEABLE @@ -92,6 +96,11 @@ int BinaryCrossEntropyGrad::UnPackToFlatBuilder(const schema::Primitive *primiti int BinaryCrossEntropyGrad::GetReduction() const { return this->primitive_->value_as_BinaryCrossEntropyGrad()->reduction(); } + +PrimitiveC *BinaryCrossEntropyGradCreator(const schema::Primitive *primitive) { + return PrimitiveC::NewPrimitiveC(primitive); +} +Registry BinaryCrossEntropyGradRegistry(schema::PrimitiveType_BinaryCrossEntropyGrad, BinaryCrossEntropyGradCreator); #endif int BinaryCrossEntropyGrad::InferShape(std::vector inputs_, std::vector outputs_) { Tensor *x = inputs_[0]; diff --git a/mindspore/lite/src/ops/cast.cc b/mindspore/lite/src/ops/cast.cc index 9a9ef7f94b3..367a7a6212b 100644 --- a/mindspore/lite/src/ops/cast.cc +++ b/mindspore/lite/src/ops/cast.cc @@ -89,7 +89,7 @@ int Cast::InferShape(std::vector inputs_, std::vector output MS_ASSERT(input != nullptr); auto output = outputs_.front(); MS_ASSERT(output != nullptr); - if (inputs_.size() != kSingleNum || outputs_.size() != kSingleNum) { + if (outputs_.size() != kSingleNum) { MS_LOG(ERROR) << "tensor number is error."; return RET_INPUT_TENSOR_ERROR; } diff --git a/mindspore/lite/src/ops/gather.cc b/mindspore/lite/src/ops/gather.cc index fbfa07c5a25..93f28be14be 100644 --- a/mindspore/lite/src/ops/gather.cc +++ b/mindspore/lite/src/ops/gather.cc @@ -54,8 +54,15 @@ int Gather::UnPackAttr(const Primitive &prim, const std::vector &inp delete gather_attr; return RET_ERROR; } - gather_attr->axis = GetValue(prim.GetAttr("axis")); - gather_attr->batchDims = GetValue(prim.GetAttr("batchDims")); + if (inputs[2]->isa()) { + ValueNodePtr axis_tensor = inputs[2]->cast(); + int axis = GetValue(axis_tensor->value()); + gather_attr->axis = axis; + } else { + MS_LOG(ERROR) << "input axis is not value node."; + return RET_ERROR; + } + gather_attr->batchDims = 0; this->primitive_->value.value = gather_attr; } return RET_OK; @@ -85,8 +92,7 @@ Registry GatherRegistry(schema::PrimitiveType_Gather, GatherCreator); int Gather::InferShape(std::vector inputs_, std::vector outputs_) { MS_ASSERT(this->primitive_ != nullptr); if (inputs_.size() != kDoubleNum) { - MS_LOG(ERROR) << "Gather should have two inputs"; - return RET_INPUT_TENSOR_ERROR; + MS_LOG(DEBUG) << "Gather should have two inputs"; } if (outputs_.size() != kSingleNum) { MS_LOG(ERROR) << "Gather should have one outputs"; diff --git a/mindspore/lite/src/ops/oneslike.cc b/mindspore/lite/src/ops/oneslike.cc index 166a5326d27..419c7be533e 100644 --- a/mindspore/lite/src/ops/oneslike.cc +++ b/mindspore/lite/src/ops/oneslike.cc @@ -16,6 +16,10 @@ #include "src/ops/oneslike.h" +#ifndef PRIMITIVE_WRITEABLE +#include "src/ops/ops_register.h" +#endif + namespace mindspore { namespace lite { #ifdef PRIMITIVE_WRITEABLE @@ -59,6 +63,11 @@ int OnesLike::UnPackToFlatBuilder(const schema::Primitive *primitive, flatbuffer fbb->Finish(prim_offset); return RET_OK; } + +PrimitiveC *OnesLikeCreator(const schema::Primitive *primitive) { + return PrimitiveC::NewPrimitiveC(primitive); +} +Registry OnesLikeRegistry(schema::PrimitiveType_OnesLike, OnesLikeCreator); #endif int OnesLike::InferShape(std::vector inputs_, std::vector outputs_) { Tensor *x = inputs_[0]; diff --git a/mindspore/lite/src/ops/populate/activation_grad_populate.cc b/mindspore/lite/src/ops/populate/activation_grad_populate.cc new file mode 100644 index 00000000000..54cfcbc0f73 --- /dev/null +++ b/mindspore/lite/src/ops/populate/activation_grad_populate.cc @@ -0,0 +1,41 @@ +/** + * Copyright 2019-2020 Huawei Technologies Co., Ltd + * + * 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. + */ + +#include "src/ops/activation_grad.h" +#include "src/ops/primitive_c.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32_grad/activation_grad.h" + +namespace mindspore { +namespace lite { +OpParameter *PopulateActivationGradParameter(const mindspore::lite::PrimitiveC *primitive) { + ActivationGradParameter *act_param = + reinterpret_cast(malloc(sizeof(ActivationGradParameter))); + if (act_param == nullptr) { + MS_LOG(ERROR) << "malloc ActivationParameter failed."; + return nullptr; + } + memset(act_param, 0, sizeof(ActivationGradParameter)); + act_param->op_parameter.type_ = primitive->Type(); + auto activation = + reinterpret_cast(const_cast(primitive)); + act_param->type_ = static_cast(activation->GetType()); + act_param->alpha_ = activation->GetAlpha(); + return reinterpret_cast(act_param); +} +Registry ActivationGradParameterRegistry(schema::PrimitiveType_ActivationGrad, PopulateActivationGradParameter); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/adam_populate.cc b/mindspore/lite/src/ops/populate/adam_populate.cc new file mode 100644 index 00000000000..ec06f365897 --- /dev/null +++ b/mindspore/lite/src/ops/populate/adam_populate.cc @@ -0,0 +1,36 @@ +/** + * Copyright 2019-2020 Huawei Technologies Co., Ltd + * + * 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. + */ + +#include "src/ops/adam.h" +#include "src/ops/primitive_c.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +OpParameter *PopulateAdamParameter(const mindspore::lite::PrimitiveC *primitive) { + OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "malloc Adam Parameter failed."; + return nullptr; + } + memset(param, 0, sizeof(OpParameter)); + param->type_ = primitive->Type(); + return param; +} + +Registry AdamParameterRegistry(schema::PrimitiveType_Adam, PopulateAdamParameter); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/assign_add_populate.cc b/mindspore/lite/src/ops/populate/assign_add_populate.cc new file mode 100644 index 00000000000..7169e07b24a --- /dev/null +++ b/mindspore/lite/src/ops/populate/assign_add_populate.cc @@ -0,0 +1,36 @@ +/** + * Copyright 2019-2020 Huawei Technologies Co., Ltd + * + * 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. + */ + +#include "src/ops/assign_add.h" +#include "src/ops/primitive_c.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +OpParameter *PopulateAssignAddParameter(const mindspore::lite::PrimitiveC *primitive) { + OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "malloc AssignAdd Parameter failed."; + return nullptr; + } + memset(param, 0, sizeof(OpParameter)); + param->type_ = primitive->Type(); + return param; +} + +Registry AssignAddParameterRegistry(schema::PrimitiveType_AssignAdd, PopulateAssignAddParameter); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/assign_populate.cc b/mindspore/lite/src/ops/populate/assign_populate.cc new file mode 100644 index 00000000000..86710bfd446 --- /dev/null +++ b/mindspore/lite/src/ops/populate/assign_populate.cc @@ -0,0 +1,36 @@ +/** + * Copyright 2019-2020 Huawei Technologies Co., Ltd + * + * 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. + */ + +#include "src/ops/assign.h" +#include "src/ops/primitive_c.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +OpParameter *PopulateAssignParameter(const mindspore::lite::PrimitiveC *primitive) { + OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "malloc Assign Parameter failed."; + return nullptr; + } + memset(param, 0, sizeof(OpParameter)); + param->type_ = primitive->Type(); + return param; +} + +Registry AssignParameterRegistry(schema::PrimitiveType_Assign, PopulateAssignParameter); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/bias_grad_populate.cc b/mindspore/lite/src/ops/populate/bias_grad_populate.cc new file mode 100644 index 00000000000..d19a8a22783 --- /dev/null +++ b/mindspore/lite/src/ops/populate/bias_grad_populate.cc @@ -0,0 +1,37 @@ +/** + * Copyright 2019-2020 Huawei Technologies Co., Ltd + * + * 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. + */ + +#include "src/ops/primitive_c.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/arithmetic_common.h" + +namespace mindspore { +namespace lite { +OpParameter *PopulateBiasGradParameter(const mindspore::lite::PrimitiveC *primitive) { + ArithmeticParameter *arithmetic_param = reinterpret_cast(malloc(sizeof(ArithmeticParameter))); + if (arithmetic_param == nullptr) { + MS_LOG(ERROR) << "malloc ArithmeticParameter failed."; + return nullptr; + } + memset(arithmetic_param, 0, sizeof(ArithmeticParameter)); + arithmetic_param->op_parameter_.type_ = primitive->Type(); + + return reinterpret_cast(arithmetic_param); +} +Registry PopulateBiasGradParameterParameterRegistry(schema::PrimitiveType_BiasGrad, PopulateBiasGradParameter); + +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/binary_cross_entropy_grad_populate.cc b/mindspore/lite/src/ops/populate/binary_cross_entropy_grad_populate.cc new file mode 100644 index 00000000000..0087432b081 --- /dev/null +++ b/mindspore/lite/src/ops/populate/binary_cross_entropy_grad_populate.cc @@ -0,0 +1,42 @@ +/** + * Copyright 2019-2020 Huawei Technologies Co., Ltd + * + * 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. + */ + +#include "src/ops/binary_cross_entropy_grad.h" +#include "src/ops/primitive_c.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32_grad/binary_cross_entropy_grad.h" + +namespace mindspore { +namespace lite { +OpParameter *PopulateBinaryCrossEntropyGradParameter(const mindspore::lite::PrimitiveC *primitive) { + BinaryCrossEntropyGradParameter *bce_param = + reinterpret_cast(malloc(sizeof(BinaryCrossEntropyGradParameter))); + if (bce_param == nullptr) { + MS_LOG(ERROR) << "malloc BinaryCrossEntropyGrad Parameter failed."; + return nullptr; + } + memset(bce_param, 0, sizeof(BinaryCrossEntropyGradParameter)); + bce_param->op_parameter_.type_ = primitive->Type(); + auto param = + reinterpret_cast(const_cast(primitive)); + bce_param->reduction = param->GetReduction(); + return reinterpret_cast(bce_param); +} + +Registry BinaryCrossEntropyGradParameterRegistry(schema::PrimitiveType_BinaryCrossEntropyGrad, + PopulateBinaryCrossEntropyGradParameter); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/binary_cross_entropy_populate.cc b/mindspore/lite/src/ops/populate/binary_cross_entropy_populate.cc new file mode 100644 index 00000000000..1e150a21faf --- /dev/null +++ b/mindspore/lite/src/ops/populate/binary_cross_entropy_populate.cc @@ -0,0 +1,42 @@ +/** + * Copyright 2019-2020 Huawei Technologies Co., Ltd + * + * 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. + */ + +#include "src/ops/binary_cross_entropy.h" +#include "src/ops/primitive_c.h" +#include "src/ops/populate/populate_register.h" +#include "nnacl/fp32_grad/binary_cross_entropy.h" + +namespace mindspore { +namespace lite { +OpParameter *PopulateBinaryCrossEntropyParameter(const mindspore::lite::PrimitiveC *primitive) { + BinaryCrossEntropyParameter *bce_param = + reinterpret_cast(malloc(sizeof(BinaryCrossEntropyParameter))); + if (bce_param == nullptr) { + MS_LOG(ERROR) << "malloc BinaryCrossEntropy Parameter failed."; + return nullptr; + } + memset(bce_param, 0, sizeof(BinaryCrossEntropyParameter)); + bce_param->op_parameter_.type_ = primitive->Type(); + auto param = + reinterpret_cast(const_cast(primitive)); + bce_param->reduction = param->GetReduction(); + return reinterpret_cast(bce_param); +} + +Registry BinaryCrossEntropyParameterRegistry(schema::PrimitiveType_BinaryCrossEntropy, + PopulateBinaryCrossEntropyParameter); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/oneslike_populate.cc b/mindspore/lite/src/ops/populate/oneslike_populate.cc new file mode 100644 index 00000000000..71b5a05a622 --- /dev/null +++ b/mindspore/lite/src/ops/populate/oneslike_populate.cc @@ -0,0 +1,36 @@ +/** + * Copyright 2019-2020 Huawei Technologies Co., Ltd + * + * 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. + */ + +#include "src/ops/oneslike.h" +#include "src/ops/primitive_c.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +OpParameter *PopulateOnesLikeParameter(const mindspore::lite::PrimitiveC *primitive) { + OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "malloc OnesLike Parameter failed."; + return nullptr; + } + memset(param, 0, sizeof(OpParameter)); + param->type_ = primitive->Type(); + return param; +} + +Registry OnesLikeParameterRegistry(schema::PrimitiveType_OnesLike, PopulateOnesLikeParameter); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/populate/unsorted_segment_sum_populate.cc b/mindspore/lite/src/ops/populate/unsorted_segment_sum_populate.cc new file mode 100644 index 00000000000..0d72aaf9127 --- /dev/null +++ b/mindspore/lite/src/ops/populate/unsorted_segment_sum_populate.cc @@ -0,0 +1,37 @@ +/** + * Copyright 2019-2020 Huawei Technologies Co., Ltd + * + * 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. + */ + +#include "src/ops/unsorted_segment_sum.h" +#include "src/ops/primitive_c.h" +#include "src/ops/populate/populate_register.h" + +namespace mindspore { +namespace lite { +OpParameter *PopulateUnsortedSegmentSumParameter(const mindspore::lite::PrimitiveC *primitive) { + OpParameter *param = reinterpret_cast(malloc(sizeof(OpParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "malloc UnsortedSegmentSum Parameter failed."; + return nullptr; + } + memset(param, 0, sizeof(OpParameter)); + param->type_ = primitive->Type(); + return param; +} + +Registry UnsortedSegmentSumParameterRegistry(schema::PrimitiveType_UnsortedSegmentSum, + PopulateUnsortedSegmentSumParameter); +} // namespace lite +} // namespace mindspore diff --git a/mindspore/lite/src/ops/slice.cc b/mindspore/lite/src/ops/slice.cc index 87854669a91..c36fcce8582 100644 --- a/mindspore/lite/src/ops/slice.cc +++ b/mindspore/lite/src/ops/slice.cc @@ -94,6 +94,12 @@ int Slice::UnPackAttr(const Primitive &prim, const std::vector &inpu } } } + std::vector axes; + axes.clear(); + for (size_t i = 0; i < attr->begin.size(); i++) { + axes.push_back(i); + } + attr->axes = axes; } this->primitive_->value.value = attr; } diff --git a/mindspore/lite/src/ops/unsorted_segment_sum.cc b/mindspore/lite/src/ops/unsorted_segment_sum.cc index f9b4573b4c0..d60d30a48e8 100644 --- a/mindspore/lite/src/ops/unsorted_segment_sum.cc +++ b/mindspore/lite/src/ops/unsorted_segment_sum.cc @@ -17,6 +17,10 @@ #include #include "src/ops/unsorted_segment_sum.h" +#ifndef PRIMITIVE_WRITEABLE +#include "src/ops/ops_register.h" +#endif + namespace mindspore { namespace lite { #ifdef PRIMITIVE_WRITEABLE @@ -69,6 +73,11 @@ int UnsortedSegmentSum::GetNumSegments() const { int ret = this->primitive_->value_as_UnsortedSegmentSum()->numSegments(); return ret; } + +PrimitiveC *UnsortedSegmentSumCreator(const schema::Primitive *primitive) { + return PrimitiveC::NewPrimitiveC(primitive); +} +Registry UnsortedSegmentSumRegistry(schema::PrimitiveType_UnsortedSegmentSum, UnsortedSegmentSumCreator); #endif int UnsortedSegmentSum::InferShape(std::vector inputs_, std::vector outputs_) { // check inputs and outputs