diff --git a/mindspore/lite/c_ops/CMakeLists.txt b/mindspore/lite/c_ops/CMakeLists.txt new file mode 100644 index 0000000000..06ad3db3f3 --- /dev/null +++ b/mindspore/lite/c_ops/CMakeLists.txt @@ -0,0 +1,3 @@ +file(GLOB_RECURSE C_OPS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.cc) + +add_library(c_ops_mid OBJECT ${C_OPS_SRC}) \ No newline at end of file diff --git a/mindspore/lite/c_ops/abs.h b/mindspore/lite/c_ops/abs.h new file mode 100644 index 0000000000..17ad481607 --- /dev/null +++ b/mindspore/lite/c_ops/abs.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic_self.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_ABS_H_ +#define LITE_MINDSPORE_LITE_C_OPS_ABS_H_ + +namespace mindspore { +class Abs : public ArithmeticSelf { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Abs(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} +#else + explicit Abs(schema::Primitive *primitive) : ArithmeticSelf(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_ABS_H_ diff --git a/mindspore/lite/c_ops/activation.cc b/mindspore/lite/c_ops/activation.cc new file mode 100644 index 0000000000..1a0bcaead1 --- /dev/null +++ b/mindspore/lite/c_ops/activation.cc @@ -0,0 +1,35 @@ +/** + * 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 "c_ops/activation.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Activation::GetType() const { return this->primitive->value.AsActivation()->type; } +float Activation::GetAlpha() const { return this->primitive->value.AsActivation()->alpha; } + +void Activation::SetType(int type) { this->primitive->value.AsActivation()->type = (schema::ActivationType)type; } +void Activation::SetAlpha(float alpha) { this->primitive->value.AsActivation()->alpha = alpha; } + +#else + +int Activation::GetType() const { return this->primitive->value_as_Activation()->type(); } +float Activation::GetAlpha() const { return this->primitive->value_as_Activation()->alpha(); } + +void Activation::SetType(int type) {} +void Activation::SetAlpha(float alpha) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/activation.h b/mindspore/lite/c_ops/activation.h new file mode 100644 index 0000000000..4c2bf33ae8 --- /dev/null +++ b/mindspore/lite/c_ops/activation.h @@ -0,0 +1,46 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_ACTIVATION_H_ +#define LITE_MINDSPORE_LITE_C_OPS_ACTIVATION_H_ + +namespace mindspore { +class Activation : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Activation(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Activation(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int GetType() const; + float GetAlpha() const; + void SetType(int type); + void SetAlpha(float alpha); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_ACTIVATION_H_ diff --git a/mindspore/lite/c_ops/activation_grad.cc b/mindspore/lite/c_ops/activation_grad.cc new file mode 100644 index 0000000000..5632cad14d --- /dev/null +++ b/mindspore/lite/c_ops/activation_grad.cc @@ -0,0 +1,33 @@ +/** + * 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 "c_ops/activation_grad.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int ActivationGrad::GetType() const { return this->primitive->value.AsActivationGrad()->type; } + +void ActivationGrad::SetType(int type) { + this->primitive->value.AsActivationGrad()->type = (schema::ActivationGradType)type; +} + +#else + +int ActivationGrad::GetType() const { return this->primitive->value_as_ActivationGrad()->type(); } + +void ActivationGrad::SetType(int type) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/activation_grad.h b/mindspore/lite/c_ops/activation_grad.h new file mode 100644 index 0000000000..be51505afa --- /dev/null +++ b/mindspore/lite/c_ops/activation_grad.h @@ -0,0 +1,44 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_ACTIVATION_GRAD_H_ +#define LITE_MINDSPORE_LITE_C_OPS_ACTIVATION_GRAD_H_ + +namespace mindspore { +class ActivationGrad : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit ActivationGrad(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit ActivationGrad(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int GetType() const; + void SetType(int type); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_ACTIVATION_GRAD_H_ diff --git a/mindspore/lite/c_ops/add.cc b/mindspore/lite/c_ops/add.cc new file mode 100644 index 0000000000..e69010a4a5 --- /dev/null +++ b/mindspore/lite/c_ops/add.cc @@ -0,0 +1,33 @@ +/** + * 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 "c_ops/add.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Add::GetActivationType() const { return this->primitive->value.AsAdd()->activationType; } + +void Add::SetActivationType(int activation_type) { + this->primitive->value.AsAdd()->activationType = (schema::ActivationType)activation_type; +} + +#else + +int Add::GetActivationType() const { return this->primitive->value_as_Add()->activationType(); } + +void Add::SetActivationType(int activation_type) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/add.h b/mindspore/lite/c_ops/add.h new file mode 100644 index 0000000000..70be03b5c3 --- /dev/null +++ b/mindspore/lite/c_ops/add.h @@ -0,0 +1,43 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif +#ifndef LITE_MINDSPORE_LITE_C_OPS_ADD_H_ +#define LITE_MINDSPORE_LITE_C_OPS_ADD_H_ + +namespace mindspore { +class Add : public Arithmetic { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Add(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} +#else + explicit Add(schema::Primitive *primitive) : Arithmetic(primitive) {} +#endif + int GetActivationType() const; + void SetActivationType(int activation_type); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_ADD_H_ diff --git a/mindspore/lite/c_ops/addn.cc b/mindspore/lite/c_ops/addn.cc new file mode 100644 index 0000000000..b868ed8aaf --- /dev/null +++ b/mindspore/lite/c_ops/addn.cc @@ -0,0 +1,59 @@ +/** + * 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 "c_ops/addn.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int AddN::GetN() const { return this->primitive->value.AsAddN()->N; } + +void AddN::SetN(int n) { this->primitive->value.AsAddN()->N = n; } + +#else + +int AddN::GetN() const { return this->primitive->value_as_AddN()->N(); } + +void AddN::SetN(int n) {} +#endif +namespace { +constexpr int kLeastInputNum = 2; +} +int AddN::InferShape(std::vector inputs, std::vector outputs) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs.front(); + MS_ASSERT(input != nullptr); + auto output = outputs.front(); + MS_ASSERT(output != nullptr); + if (inputs.size() < kLeastInputNum) { + MS_LOG(ERROR) << "input size" << inputs.size() << " is error!"; + return 1; + } + for (int i = 1; i < inputs.size(); ++i) { + if (inputs.at(i)->shape() != inputs.at(0)->shape()) { + MS_LOG(ERROR) << "AddN inputs shape is not equal!"; + return 1; + } + if (inputs.at(i)->data_type() != inputs.at(0)->data_type()) { + MS_LOG(ERROR) << "AddN all input data type should be the same!"; + return 1; + } + } + output->SetFormat(input->GetFormat()); + output->set_shape(input->shape()); + output->set_data_type(input->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/addn.h b/mindspore/lite/c_ops/addn.h new file mode 100644 index 0000000000..19e845c44f --- /dev/null +++ b/mindspore/lite/c_ops/addn.h @@ -0,0 +1,45 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_ADD_N_H_ +#define LITE_MINDSPORE_LITE_C_OPS_ADD_N_H_ + +namespace mindspore { +class AddN : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit AddN(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit AddN(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetN() const; + void SetN(int n); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_ADD_N_H_ diff --git a/mindspore/lite/c_ops/argmax.cc b/mindspore/lite/c_ops/argmax.cc new file mode 100644 index 0000000000..fd9e24a871 --- /dev/null +++ b/mindspore/lite/c_ops/argmax.cc @@ -0,0 +1,75 @@ +/** + * 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 "c_ops/argmax.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int ArgMax::GetAxis() const { return this->primitive->value.AsArgMax()->axis; } +bool ArgMax::GetOutMaxValue() const { return this->primitive->value.AsArgMax()->outMaxValue; } +int ArgMax::GetTopK() const { return this->primitive->value.AsArgMax()->topK; } +bool ArgMax::GetKeepDims() const { return this->primitive->value.AsArgMax()->keepDims; } +int ArgMax::GetAxisType() const { return this->primitive->value.AsArgMax()->axisType; } + +void ArgMax::SetAxis(int axis) { this->primitive->value.AsArgMax()->axis = axis; } +void ArgMax::SetOutMaxValue(bool out_max_value) { this->primitive->value.AsArgMax()->outMaxValue = out_max_value; } +void ArgMax::SetTopK(int top_k) { this->primitive->value.AsArgMax()->topK = top_k; } +void ArgMax::SetKeepDims(bool keep_dims) { this->primitive->value.AsArgMax()->keepDims = keep_dims; } +void ArgMax::SetAxisType(int axis_type) { this->primitive->value.AsArgMax()->axisType = axis_type; } + +#else + +int ArgMax::GetAxis() const { return this->primitive->value_as_ArgMax()->axis(); } +bool ArgMax::GetOutMaxValue() const { return this->primitive->value_as_ArgMax()->outMaxValue(); } +int ArgMax::GetTopK() const { return this->primitive->value_as_ArgMax()->topK(); } +bool ArgMax::GetKeepDims() const { return this->primitive->value_as_ArgMax()->keepDims(); } +int ArgMax::GetAxisType() const { return this->primitive->value_as_ArgMax()->axisType(); } + +void ArgMax::SetAxis(int axis) {} +void ArgMax::SetOutMaxValue(bool out_max_value) {} +void ArgMax::SetTopK(int top_k) {} +void ArgMax::SetKeepDims(bool keep_dims) {} +void ArgMax::SetAxisType(int axis_type) {} +#endif +int ArgMax::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + if (inputs_.size() != kSingleNum || outputs_.size() != kSingleNum) { + MS_LOG(ERROR) << "tensor number is error."; + } + + std::vector output_shape(input->shape()); + auto input_shape_size = input->shape().size(); + int axis = GetAxis() < 0 ? GetAxis() + input_shape_size : GetAxis(); + if (axis >= input_shape_size || axis < 0) { + MS_LOG(ERROR) << "Invalid axis " << GetAxis() << ", input shape size: " << input_shape_size; + return 1; + } + if (GetTopK() == 1 && !GetKeepDims()) { + output_shape.erase(output_shape.begin() + axis); + } else { + output_shape[axis] = GetTopK(); + } + + output->SetFormat(input->GetFormat()); + output->set_shape(output_shape); + output->set_data_type(input->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/argmax.h b/mindspore/lite/c_ops/argmax.h new file mode 100644 index 0000000000..fc4e305bd5 --- /dev/null +++ b/mindspore/lite/c_ops/argmax.h @@ -0,0 +1,53 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_ARG_MAX_H_ +#define LITE_MINDSPORE_LITE_C_OPS_ARG_MAX_H_ + +namespace mindspore { +class ArgMax : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit ArgMax(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit ArgMax(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetAxis() const; + bool GetOutMaxValue() const; + int GetTopK() const; + bool GetKeepDims() const; + int GetAxisType() const; + void SetAxis(int axis); + void SetOutMaxValue(bool out_max_value); + void SetTopK(int top_k); + void SetKeepDims(bool keep_dims); + void SetAxisType(int axis_type); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_ARG_MAX_H_ diff --git a/mindspore/lite/c_ops/argmin.cc b/mindspore/lite/c_ops/argmin.cc new file mode 100644 index 0000000000..6a2274af4f --- /dev/null +++ b/mindspore/lite/c_ops/argmin.cc @@ -0,0 +1,74 @@ +/** + * 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 "c_ops/argmin.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int ArgMin::GetAxis() const { return this->primitive->value.AsArgMin()->axis; } +bool ArgMin::GetOutMaxValue() const { return this->primitive->value.AsArgMin()->outMaxValue; } +int ArgMin::GetTopK() const { return this->primitive->value.AsArgMin()->topK; } +bool ArgMin::GetKeepDims() const { return this->primitive->value.AsArgMin()->keepDims; } +int ArgMin::GetAxisType() const { return this->primitive->value.AsArgMin()->axisType; } + +void ArgMin::SetAxis(int axis) { this->primitive->value.AsArgMin()->axis = axis; } +void ArgMin::SetOutMaxValue(bool out_max_value) { this->primitive->value.AsArgMin()->outMaxValue = out_max_value; } +void ArgMin::SetTopK(int top_k) { this->primitive->value.AsArgMin()->topK = top_k; } +void ArgMin::SetKeepDims(bool keep_dims) { this->primitive->value.AsArgMin()->keepDims = keep_dims; } +void ArgMin::SetAxisType(int axis_type) { this->primitive->value.AsArgMin()->axisType = axis_type; } + +#else + +int ArgMin::GetAxis() const { return this->primitive->value_as_ArgMin()->axis(); } +bool ArgMin::GetOutMaxValue() const { return this->primitive->value_as_ArgMin()->outMaxValue(); } +int ArgMin::GetTopK() const { return this->primitive->value_as_ArgMin()->topK(); } +bool ArgMin::GetKeepDims() const { return this->primitive->value_as_ArgMin()->keepDims(); } +int ArgMin::GetAxisType() const { return this->primitive->value_as_ArgMin()->axisType(); } + +void ArgMin::SetAxis(int axis) {} +void ArgMin::SetOutMaxValue(bool out_max_value) {} +void ArgMin::SetTopK(int top_k) {} +void ArgMin::SetKeepDims(bool keep_dims) {} +void ArgMin::SetAxisType(int axis_type) {} +#endif +int ArgMin::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + if (inputs_.size() != kSingleNum || outputs_.size() != kSingleNum) { + MS_LOG(ERROR) << "tensor number is error."; + } + auto input_shape_size = input->shape().size(); + int axis = GetAxis() < 0 ? GetAxis() + input_shape_size : GetAxis(); + if (axis >= input_shape_size || axis < 0) { + MS_LOG(ERROR) << "Invalid axis " << GetAxis() << ", input shape size: " << input_shape_size; + return 1; + } + std::vector output_shape(input->shape()); + if (GetTopK() == 1 && !GetKeepDims()) { + output_shape.erase(output_shape.begin() + axis); + } else { + output_shape[axis] = GetTopK(); + } + + output->SetFormat(input->GetFormat()); + output->set_shape(output_shape); + output->set_data_type(input->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/argmin.h b/mindspore/lite/c_ops/argmin.h new file mode 100644 index 0000000000..8442f583fb --- /dev/null +++ b/mindspore/lite/c_ops/argmin.h @@ -0,0 +1,53 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_ARG_MIN_H_ +#define LITE_MINDSPORE_LITE_C_OPS_ARG_MIN_H_ + +namespace mindspore { +class ArgMin : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit ArgMin(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit ArgMin(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetAxis() const; + bool GetOutMaxValue() const; + int GetTopK() const; + bool GetKeepDims() const; + int GetAxisType() const; + void SetAxis(int axis); + void SetOutMaxValue(bool out_max_value); + void SetTopK(int top_k); + void SetKeepDims(bool keep_dims); + void SetAxisType(int axis_type); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_ARG_MIN_H_ diff --git a/mindspore/lite/c_ops/arithmetic.cc b/mindspore/lite/c_ops/arithmetic.cc new file mode 100644 index 0000000000..4baefd9777 --- /dev/null +++ b/mindspore/lite/c_ops/arithmetic.cc @@ -0,0 +1,99 @@ +/** + * 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 "c_ops/arithmetic.h" + +namespace mindspore { +int Arithmetic::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + if (inputs_.size() != kDoubleNum) { + MS_LOG(ERROR) << "The number of input must be " << kDoubleNum; + return 1; + } + if (outputs_.size() != kSingleNum) { + MS_LOG(ERROR) << "The number of output must be " << kSingleNum; + return 1; + } + auto input0 = inputs_[0]; + MS_ASSERT(input0 != nullptr); + auto input1 = inputs_[1]; + MS_ASSERT(input1 != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + + auto input_shape0 = input0->shape(); + auto input_shape1 = input1->shape(); + auto format = input0->GetFormat(); + in_shape0_.resize(5); + in_shape1_.resize(5); + out_shape_.resize(5); + + ndim_ = input_shape0.size(); + if (input_shape0.size() < input_shape1.size()) { + ndim_ = input_shape1.size(); + auto fill_dim_num = input_shape1.size() - input_shape0.size(); + int j = 0; + for (int i = 0; i < input_shape1.size(); i++) { + if (i < fill_dim_num) { + in_shape0_[i] = 1; + } else { + in_shape0_[i] = input_shape0[j++]; + } + in_shape1_[i] = input_shape1[i]; + } + format = input0->GetFormat(); + } else if (input_shape0.size() > input_shape1.size()) { + ndim_ = input_shape0.size(); + auto fill_dim_num = input_shape0.size() - input_shape1.size(); + int j = 0; + for (int i = 0; i < input_shape0.size(); i++) { + if (i < fill_dim_num) { + in_shape1_[i] = 1; + } else { + in_shape1_[i] = input_shape1[j++]; + } + in_shape0_[i] = input_shape0[i]; + } + } else { + for (int i = 0; i < input_shape0.size(); i++) { + in_shape1_[i] = input_shape1[i]; + in_shape0_[i] = input_shape0[i]; + } + } + + std::vector output_shape; + for (size_t i = 0; i < ndim_; i++) { + if (in_shape0_[i] != in_shape1_[i]) { + if (in_shape0_[i] == 1) { + out_shape_[i] = in_shape1_[i]; + } else if (in_shape1_[i] == 1) { + out_shape_[i] = in_shape0_[i]; + } else { + MS_LOG(ERROR) << "shapes of input tensors can not be broadCasted"; + return -1; + } + broadcasting_ = true; + } else { + out_shape_[i] = in_shape0_[i]; + } + output_shape.push_back(out_shape_[i]); + } + output->SetFormat(format); + output->set_shape(output_shape); + output->set_data_type(input0->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/arithmetic.h b/mindspore/lite/c_ops/arithmetic.h new file mode 100644 index 0000000000..9aef72a0f4 --- /dev/null +++ b/mindspore/lite/c_ops/arithmetic.h @@ -0,0 +1,55 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_ARITHMETIC_H_ +#define LITE_MINDSPORE_LITE_C_OPS_ARITHMETIC_H_ + +namespace mindspore { +class Arithmetic : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Arithmetic(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Arithmetic(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + bool Broadcasting() { return this->broadcasting_; } + int NDims() { return this->ndim_; } + std::vector InShape0() { return this->in_shape0_; } + std::vector InShape1() { return this->in_shape1_; } + std::vector OutputShape() { return this->out_shape_; } + + protected: + bool broadcasting_ = false; + int ndim_; + std::vector in_shape0_; + std::vector in_shape1_; + std::vector out_shape_; +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_ARITHMETIC_H_ diff --git a/mindspore/lite/c_ops/arithmetic_self.cc b/mindspore/lite/c_ops/arithmetic_self.cc new file mode 100644 index 0000000000..4032b69d6f --- /dev/null +++ b/mindspore/lite/c_ops/arithmetic_self.cc @@ -0,0 +1,34 @@ +/** + * 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 "c_ops/arithmetic_self.h" + +namespace mindspore { +int ArithmeticSelf::InferShape(std::vector inputs_, + std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + + output->SetFormat(input->GetFormat()); + output->set_shape(input->shape()); + output->set_data_type(input->data_type()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/arithmetic_self.h b/mindspore/lite/c_ops/arithmetic_self.h new file mode 100644 index 0000000000..4b70b62168 --- /dev/null +++ b/mindspore/lite/c_ops/arithmetic_self.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif +#ifndef LITE_MINDSPORE_LITE_C_OPS_ARITHMETIC_SELF_H_ +#define LITE_MINDSPORE_LITE_C_OPS_ARITHMETIC_SELF_H_ + +namespace mindspore { +class ArithmeticSelf : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit ArithmeticSelf(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit ArithmeticSelf(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_ARITHMETIC_SELF_H_ diff --git a/mindspore/lite/c_ops/batch_norm.cc b/mindspore/lite/c_ops/batch_norm.cc new file mode 100644 index 0000000000..9d4fe336a0 --- /dev/null +++ b/mindspore/lite/c_ops/batch_norm.cc @@ -0,0 +1,31 @@ +/** + * 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 "c_ops/batch_norm.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +float BatchNorm::GetEpsilon() const { return this->primitive->value.AsBatchNorm()->epsilon; } + +void BatchNorm::SetEpsilon(float epsilon) { this->primitive->value.AsBatchNorm()->epsilon = epsilon; } + +#else + +float BatchNorm::GetEpsilon() const { return this->primitive->value_as_BatchNorm()->epsilon(); } + +void BatchNorm::SetEpsilon(float epsilon) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/batch_norm.h b/mindspore/lite/c_ops/batch_norm.h new file mode 100644 index 0000000000..6d660fb6d2 --- /dev/null +++ b/mindspore/lite/c_ops/batch_norm.h @@ -0,0 +1,44 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_BATCH_NORM_H_ +#define LITE_MINDSPORE_LITE_C_OPS_BATCH_NORM_H_ + +namespace mindspore { +class BatchNorm : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit BatchNorm(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit BatchNorm(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + float GetEpsilon() const; + void SetEpsilon(float epsilon); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_BATCH_NORM_H_ diff --git a/mindspore/lite/c_ops/batch_to_space.cc b/mindspore/lite/c_ops/batch_to_space.cc new file mode 100644 index 0000000000..5019b07286 --- /dev/null +++ b/mindspore/lite/c_ops/batch_to_space.cc @@ -0,0 +1,114 @@ +/** + * 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 "c_ops/batch_to_space.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector BatchToSpace::GetBlockShape() const { return this->primitive->value.AsBatchToSpace()->blockShape; } +std::vector BatchToSpace::GetCrops() const { return this->primitive->value.AsBatchToSpace()->crops; } + +void BatchToSpace::SetBlockShape(const std::vector &block_shape) { + this->primitive->value.AsBatchToSpace()->blockShape = block_shape; +} +void BatchToSpace::SetCrops(const std::vector &crops) { this->primitive->value.AsBatchToSpace()->crops = crops; } + +#else + +std::vector BatchToSpace::GetBlockShape() const { + auto fb_vector = this->primitive->value_as_BatchToSpace()->blockShape(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +std::vector BatchToSpace::GetCrops() const { + auto fb_vector = this->primitive->value_as_BatchToSpace()->crops(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void BatchToSpace::SetBlockShape(const std::vector &block_shape) {} +void BatchToSpace::SetCrops(const std::vector &crops) {} +#endif +namespace { +constexpr int kBatchToSpaceOutputNum = 1; +constexpr int kBatchToSpaceInputNum = 1; +constexpr int kBlockShapeSize = 2; +constexpr int kCropsSize = 4; +} // namespace + +int BatchToSpace::InferShape(std::vector inputs, std::vector outputs) { + MS_ASSERT(this->primitive != nullptr); + if (outputs.size() != kBatchToSpaceOutputNum || inputs.size() != kBatchToSpaceInputNum) { + MS_LOG(ERROR) << "Invalid output/input size! output size: " << outputs.size() << ",input size: " << inputs.size(); + return 1; + } + + auto input = inputs.at(0); + if (input->GetFormat() != schema::Format_NHWC) { + MS_LOG(ERROR) << "batch_to_space only support NHWC now!"; + return 1; + } + auto input_shape = input->shape(); + if (input_shape.size() != kDimension_4d) { + MS_LOG(ERROR) << "input shape dimension size should == " << kDimension_4d; + return 1; + } + + auto block_shape = GetBlockShape(); + if (block_shape.size() != kBlockShapeSize) { + MS_LOG(ERROR) << "Block shape size should be " << kBlockShapeSize; + return 1; + } + auto crops = GetCrops(); + if (crops.size() != kCropsSize) { + MS_LOG(ERROR) << "Crops size should be " << kCropsSize; + return 1; + } + size_t mul_block_shape = 1; + + for (size_t i = 0; i < kBlockShapeSize; ++i) { + if (block_shape[i] <= 0) { + MS_LOG(ERROR) << "Input block_shape should > 0!"; + return 1; + } + if (input_shape[NHWC_N] % block_shape[i]) { + MS_LOG(ERROR) << "Dimension n " << input_shape[NHWC_N] << " can not divide block_shape[" << i << "] " + << block_shape[i]; + return 1; + } + mul_block_shape *= block_shape[i]; + } + + if (input_shape[NHWC_N] < mul_block_shape) { + MS_LOG(ERROR) << "Dimension n " << input_shape[NHWC_N] << " < product of block shape!"; + return 1; + } + for (size_t i = 0; i < kCropsSize; ++i) { + if (crops[i] < 0) { + MS_LOG(ERROR) << "Input crops should >= 0"; + return 1; + } + } + std::vector output_shape(input_shape.size()); + output_shape[NHWC_N] = input_shape[NHWC_N] / mul_block_shape; + output_shape[NHWC_H] = input_shape[NHWC_H] * block_shape[0] - crops[0] - crops[1]; + output_shape[NHWC_W] = input_shape[NHWC_W] * block_shape[1] - crops[2] - crops[3]; + output_shape[NHWC_C] = input_shape[NHWC_C]; + + outputs[0]->SetFormat(input->GetFormat()); + outputs[0]->set_shape(output_shape); + outputs[0]->set_data_type(input->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/batch_to_space.h b/mindspore/lite/c_ops/batch_to_space.h new file mode 100644 index 0000000000..d0fb08cf59 --- /dev/null +++ b/mindspore/lite/c_ops/batch_to_space.h @@ -0,0 +1,47 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_BATCH_TO_SPACE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_BATCH_TO_SPACE_H_ + +namespace mindspore { +class BatchToSpace : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit BatchToSpace(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit BatchToSpace(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + std::vector GetBlockShape() const; + std::vector GetCrops() const; + void SetBlockShape(const std::vector &block_shape); + void SetCrops(const std::vector &crops); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_BATCH_TO_SPACE_H_ diff --git a/mindspore/lite/c_ops/bias_add.cc b/mindspore/lite/c_ops/bias_add.cc new file mode 100644 index 0000000000..4bb6f1d9a0 --- /dev/null +++ b/mindspore/lite/c_ops/bias_add.cc @@ -0,0 +1,34 @@ +/** + * 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 "c_ops/bias_add.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector BiasAdd::GetAxis() const { return this->primitive->value.AsBiasAdd()->axis; } + +void BiasAdd::SetAxis(const std::vector &axis) { this->primitive->value.AsBiasAdd()->axis = axis; } + +#else + +std::vector BiasAdd::GetAxis() const { + auto fb_vector = this->primitive->value_as_BiasAdd()->axis(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void BiasAdd::SetAxis(const std::vector &axis) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/bias_add.h b/mindspore/lite/c_ops/bias_add.h new file mode 100644 index 0000000000..b5241ff904 --- /dev/null +++ b/mindspore/lite/c_ops/bias_add.h @@ -0,0 +1,44 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_BIAS_ADD_H_ +#define LITE_MINDSPORE_LITE_C_OPS_BIAS_ADD_H_ + +namespace mindspore { +class BiasAdd : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit BiasAdd(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit BiasAdd(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + std::vector GetAxis() const; + void SetAxis(const std::vector &axis); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_BIAS_ADD_H_ diff --git a/mindspore/lite/c_ops/bias_grad.cc b/mindspore/lite/c_ops/bias_grad.cc new file mode 100644 index 0000000000..fec73f758f --- /dev/null +++ b/mindspore/lite/c_ops/bias_grad.cc @@ -0,0 +1,34 @@ +/** + * 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 "c_ops/bias_grad.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector BiasGrad::GetAxis() const { return this->primitive->value.AsBiasGrad()->axis; } + +void BiasGrad::SetAxis(const std::vector &axis) { this->primitive->value.AsBiasGrad()->axis = axis; } + +#else + +std::vector BiasGrad::GetAxis() const { + auto fb_vector = this->primitive->value_as_BiasGrad()->axis(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void BiasGrad::SetAxis(const std::vector &axis) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/bias_grad.h b/mindspore/lite/c_ops/bias_grad.h new file mode 100644 index 0000000000..bf2fa9ad9b --- /dev/null +++ b/mindspore/lite/c_ops/bias_grad.h @@ -0,0 +1,44 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_BIAS_GRAD_H_ +#define LITE_MINDSPORE_LITE_C_OPS_BIAS_GRAD_H_ + +namespace mindspore { +class BiasGrad : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit BiasGrad(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit BiasGrad(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + std::vector GetAxis() const; + void SetAxis(const std::vector &axis); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_BIAS_GRAD_H_ diff --git a/mindspore/lite/c_ops/bn_grad_input.cc b/mindspore/lite/c_ops/bn_grad_input.cc new file mode 100644 index 0000000000..2b116d9eb3 --- /dev/null +++ b/mindspore/lite/c_ops/bn_grad_input.cc @@ -0,0 +1,35 @@ +/** + * 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 "c_ops/bn_grad_input.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +float BNGradInput::GetEps() const { return this->primitive->value.AsBNGradInput()->eps; } +int BNGradInput::GetChannels() const { return this->primitive->value.AsBNGradInput()->channels; } + +void BNGradInput::SetEps(float eps) { this->primitive->value.AsBNGradInput()->eps = eps; } +void BNGradInput::SetChannels(int channels) { this->primitive->value.AsBNGradInput()->channels = channels; } + +#else + +float BNGradInput::GetEps() const { return this->primitive->value_as_BNGradInput()->eps(); } +int BNGradInput::GetChannels() const { return this->primitive->value_as_BNGradInput()->channels(); } + +void BNGradInput::SetEps(float eps) {} +void BNGradInput::SetChannels(int channels) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/bn_grad_input.h b/mindspore/lite/c_ops/bn_grad_input.h new file mode 100644 index 0000000000..11ff9bdb5f --- /dev/null +++ b/mindspore/lite/c_ops/bn_grad_input.h @@ -0,0 +1,46 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_B_N_GRAD_INPUT_H_ +#define LITE_MINDSPORE_LITE_C_OPS_B_N_GRAD_INPUT_H_ + +namespace mindspore { +class BNGradInput : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit BNGradInput(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit BNGradInput(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + float GetEps() const; + int GetChannels() const; + void SetEps(float eps); + void SetChannels(int channels); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_B_N_GRAD_INPUT_H_ diff --git a/mindspore/lite/c_ops/broadcast_to.cc b/mindspore/lite/c_ops/broadcast_to.cc new file mode 100644 index 0000000000..071f3e1fdd --- /dev/null +++ b/mindspore/lite/c_ops/broadcast_to.cc @@ -0,0 +1,79 @@ +/** + * 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 "c_ops/broadcast_to.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector BroadcastTo::GetDstShape() const { return this->primitive->value.AsBroadcastTo()->dst_shape; } + +void BroadcastTo::SetDstShape(const std::vector &dst_shape) { + this->primitive->value.AsBroadcastTo()->dst_shape = dst_shape; +} + +#else + +std::vector BroadcastTo::GetDstShape() const { + auto fb_vector = this->primitive->value_as_BroadcastTo()->dst_shape(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void BroadcastTo::SetDstShape(const std::vector &dst_shape) {} +#endif +namespace { +constexpr int kBroadcastToInputNum = 1; +constexpr int kBroadcastToOutputNum = 1; +} // namespace + +int BroadcastTo::InferShape(std::vector inputs, std::vector outputs) { + MS_ASSERT(this->primitive != nullptr); + if (inputs.size() != kBroadcastToInputNum || outputs.size() != kBroadcastToOutputNum) { + MS_LOG(ERROR) << "input size:" << inputs.size() << ", output size:" << outputs.size(); + return 1; + } + auto input = inputs.at(0); + std::vector dst_shape(this->primitive->value_as_BroadcastTo()->dst_shape()->begin(), + this->primitive->value_as_BroadcastTo()->dst_shape()->end()); + auto input_shape = input->shape(); + std::vector shape(dst_shape.size()); + int input_shape_index = input_shape.size() - 1; + if (input_shape.size() > dst_shape.size()) { + MS_LOG(ERROR) << "input shape size " << input_shape.size() << " should <= broadcast to shape size " + << dst_shape.size() << "!"; + return 1; + } + + for (int i = dst_shape.size() - 1; i >= 0; --i) { + if (dst_shape[i] < 0) { + MS_LOG(ERROR) << "shape[" << i << "] = " << dst_shape[i] << " ] should be > 0!"; + return 1; + } + if (input_shape_index >= 0) { + auto dim = input_shape[input_shape_index]; + if (dim != dst_shape[i] && dim != 1) { + MS_LOG(ERROR) << "Invalid broadcast shape!"; + return 1; + } + } + shape[i] = dst_shape[i]; + --input_shape_index; + } + outputs[0]->SetFormat(input->GetFormat()); + outputs[0]->set_shape(shape); + outputs[0]->set_data_type(input->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/broadcast_to.h b/mindspore/lite/c_ops/broadcast_to.h new file mode 100644 index 0000000000..94f95bf090 --- /dev/null +++ b/mindspore/lite/c_ops/broadcast_to.h @@ -0,0 +1,45 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_BROADCAST_TO_H_ +#define LITE_MINDSPORE_LITE_C_OPS_BROADCAST_TO_H_ + +namespace mindspore { +class BroadcastTo : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit BroadcastTo(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit BroadcastTo(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + std::vector GetDstShape() const; + void SetDstShape(const std::vector &dst_shape); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_BROADCAST_TO_H_ diff --git a/mindspore/lite/c_ops/caffe_p_relu.cc b/mindspore/lite/c_ops/caffe_p_relu.cc new file mode 100644 index 0000000000..bc176215ea --- /dev/null +++ b/mindspore/lite/c_ops/caffe_p_relu.cc @@ -0,0 +1,33 @@ +/** + * 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 "c_ops/caffe_p_relu.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +bool CaffePReLU::GetChannelShared() const { return this->primitive->value.AsCaffePReLU()->channelShared; } + +void CaffePReLU::SetChannelShared(bool channel_shared) { + this->primitive->value.AsCaffePReLU()->channelShared = channel_shared; +} + +#else + +bool CaffePReLU::GetChannelShared() const { return this->primitive->value_as_CaffePReLU()->channelShared(); } + +void CaffePReLU::SetChannelShared(bool channel_shared) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/caffe_p_relu.h b/mindspore/lite/c_ops/caffe_p_relu.h new file mode 100644 index 0000000000..b4f5002884 --- /dev/null +++ b/mindspore/lite/c_ops/caffe_p_relu.h @@ -0,0 +1,45 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#include "c_ops/activation.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_CAFFE_P_RE_L_U_H_ +#define LITE_MINDSPORE_LITE_C_OPS_CAFFE_P_RE_L_U_H_ + +namespace mindspore { +class CaffePReLU : public Activation { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit CaffePReLU(schema::PrimitiveT *primitive) : Activation(primitive) {} +#else + explicit CaffePReLU(schema::Primitive *primitive) : Activation(primitive) {} +#endif + bool GetChannelShared() const; + void SetChannelShared(bool channel_shared); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_CAFFE_P_RE_L_U_H_ diff --git a/mindspore/lite/c_ops/cast.cc b/mindspore/lite/c_ops/cast.cc new file mode 100644 index 0000000000..1c3df5b44a --- /dev/null +++ b/mindspore/lite/c_ops/cast.cc @@ -0,0 +1,64 @@ +/** + * 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 "c_ops/cast.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Cast::GetSrcT() const { return this->primitive->value.AsCast()->srcT; } +int Cast::GetDstT() const { return this->primitive->value.AsCast()->dstT; } + +void Cast::SetSrcT(int src_t) { this->primitive->value.AsCast()->srcT = src_t; } +void Cast::SetDstT(int dst_t) { this->primitive->value.AsCast()->dstT = dst_t; } + +#else + +int Cast::GetSrcT() const { return this->primitive->value_as_Cast()->srcT(); } +int Cast::GetDstT() const { return this->primitive->value_as_Cast()->dstT(); } + +void Cast::SetSrcT(int src_t) {} +void Cast::SetDstT(int dst_t) {} +#endif +int Cast::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + if (inputs_.size() != kSingleNum || outputs_.size() != kSingleNum) { + MS_LOG(ERROR) << "tensor number is error."; + return 1; + } + + MS_ASSERT(cast_prim != nullptr); + if (input->data_type() != GetSrcT()) { + MS_LOG(ERROR) << "input dataType is error"; + return 1; + } + if (kSupportDataType.find(input->data_type()) == kSupportDataType.end()) { + MS_LOG(ERROR) << "Unsupport input data type " << input->data_type(); + return 1; + } + if (GetDstT() != kNumberTypeFloat && GetDstT() != kNumberTypeFloat32) { + MS_LOG(ERROR) << "Invalid output datatype " << GetDstT(); + return 1; + } + output->SetFormat(input->GetFormat()); + output->set_shape(input->shape()); + output->set_data_type(input->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/cast.h b/mindspore/lite/c_ops/cast.h new file mode 100644 index 0000000000..b49f1fd49c --- /dev/null +++ b/mindspore/lite/c_ops/cast.h @@ -0,0 +1,47 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_CAST_H_ +#define LITE_MINDSPORE_LITE_C_OPS_CAST_H_ + +namespace mindspore { +class Cast : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Cast(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Cast(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetSrcT() const; + int GetDstT() const; + void SetSrcT(int src_t); + void SetDstT(int dst_t); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_CAST_H_ diff --git a/mindspore/lite/c_ops/clip.cc b/mindspore/lite/c_ops/clip.cc new file mode 100644 index 0000000000..0eeb12e4a6 --- /dev/null +++ b/mindspore/lite/c_ops/clip.cc @@ -0,0 +1,35 @@ +/** + * 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 "c_ops/clip.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +float Clip::GetMax() const { return this->primitive->value.AsClip()->max; } +float Clip::GetMin() const { return this->primitive->value.AsClip()->min; } + +void Clip::SetMax(float max) { this->primitive->value.AsClip()->max = max; } +void Clip::SetMin(float min) { this->primitive->value.AsClip()->min = min; } + +#else + +float Clip::GetMax() const { return this->primitive->value_as_Clip()->max(); } +float Clip::GetMin() const { return this->primitive->value_as_Clip()->min(); } + +void Clip::SetMax(float max) {} +void Clip::SetMin(float min) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/clip.h b/mindspore/lite/c_ops/clip.h new file mode 100644 index 0000000000..12893ec1ef --- /dev/null +++ b/mindspore/lite/c_ops/clip.h @@ -0,0 +1,46 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_CLIP_H_ +#define LITE_MINDSPORE_LITE_C_OPS_CLIP_H_ + +namespace mindspore { +class Clip : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Clip(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Clip(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + float GetMax() const; + float GetMin() const; + void SetMax(float max); + void SetMin(float min); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_CLIP_H_ diff --git a/mindspore/lite/c_ops/concat.cc b/mindspore/lite/c_ops/concat.cc new file mode 100644 index 0000000000..2de77c2f72 --- /dev/null +++ b/mindspore/lite/c_ops/concat.cc @@ -0,0 +1,93 @@ +/** + * 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 "c_ops/concat.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Concat::GetAxis() const { return this->primitive->value.AsConcat()->axis; } +int Concat::GetN() const { return this->primitive->value.AsConcat()->n; } + +void Concat::SetAxis(int axis) { this->primitive->value.AsConcat()->axis = axis; } +void Concat::SetN(int n) { this->primitive->value.AsConcat()->n = n; } + +#else + +int Concat::GetAxis() const { return this->primitive->value_as_Concat()->axis(); } +int Concat::GetN() const { return this->primitive->value_as_Concat()->n(); } + +void Concat::SetAxis(int axis) {} +void Concat::SetN(int n) {} +#endif +namespace { +constexpr int kConcatOutputNum = 1; +} +int Concat::InferShape(std::vector inputs_, std::vector outputs_) { + if (this->primitive == nullptr) { + MS_LOG(ERROR) << "primitive is nullptr!"; + return 1; + } + auto input0 = inputs_.front(); + auto output = outputs_.front(); + if (outputs_.size() != kConcatOutputNum) { + MS_LOG(ERROR) << "output size is error"; + return 1; + } + MS_ASSERT(concat_prim != nullptr); + auto input0_shape = inputs_.at(0)->shape(); + int axis = GetAxis() < 0 ? GetAxis() + input0_shape.size() : GetAxis(); + if (axis < 0 || axis >= input0_shape.size()) { + MS_LOG(ERROR) << "Invalid axis: " << axis; + return 1; + } + + auto input0_shape_without_axis = input0_shape; + input0_shape_without_axis.erase(input0_shape_without_axis.begin() + axis); + auto input0_data_type = inputs_.at(0)->data_type(); + schema::Format input0_format = inputs_[0]->GetFormat(); + int output_axis_dim = input0_shape.at(axis); + for (size_t i = 1; i < inputs_.size(); ++i) { + if (inputs_.at(i)->data_type() != input0_data_type) { + MS_LOG(ERROR) << "All inputs should have the same data type!"; + return 1; + } + + if (inputs_.at(i)->GetFormat() != input0_format) { + MS_LOG(ERROR) << "All input format should be the same!"; + return 1; + } + auto shape_tmp = inputs_.at(i)->shape(); + if (shape_tmp.size() != input0_shape.size()) { + MS_LOG(ERROR) << "All inputs should have the same dim num!"; + return 1; + } + auto axis_tmp = shape_tmp[axis]; + shape_tmp.erase(shape_tmp.begin() + axis); + if (input0_shape_without_axis != shape_tmp) { + MS_LOG(ERROR) << "Inputs should have the same dim except axis!"; + return 1; + } + output_axis_dim += axis_tmp; + } + auto output_shape = input0_shape; + output_shape[axis] = output_axis_dim; + outputs_[0]->set_shape(output_shape); + output->set_data_type(input0->data_type()); + output->SetFormat(input0->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/concat.h b/mindspore/lite/c_ops/concat.h new file mode 100644 index 0000000000..193fb43f2b --- /dev/null +++ b/mindspore/lite/c_ops/concat.h @@ -0,0 +1,47 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_CONCAT_H_ +#define LITE_MINDSPORE_LITE_C_OPS_CONCAT_H_ + +namespace mindspore { +class Concat : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Concat(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Concat(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetAxis() const; + int GetN() const; + void SetAxis(int axis); + void SetN(int n); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_CONCAT_H_ diff --git a/mindspore/lite/c_ops/conv2d.cc b/mindspore/lite/c_ops/conv2d.cc new file mode 100644 index 0000000000..25c0795247 --- /dev/null +++ b/mindspore/lite/c_ops/conv2d.cc @@ -0,0 +1,163 @@ +/** + * 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 "mindspore/lite/c_ops/conv2d.h" +namespace mindspore { +int Conv2D::PadUp() const { return this->pad_u_; } +int Conv2D::PadDown() const { return this->pad_d_; } +int Conv2D::PadLeft() const { return this->pad_l_; } +int Conv2D::PadRight() const { return this->pad_r_; } +#ifdef PRIMITIVE_WRITEABLE +int Conv2D::GetFormat() const { return this->primitive->value.AsConv2D()->format; } +int Conv2D::GetGroup() const { return this->primitive->value.AsConv2D()->group; } +int Conv2D::GetChannelIn() const { return this->primitive->value.AsConv2D()->channelIn; } +int Conv2D::GetChannelOut() const { return this->primitive->value.AsConv2D()->channelOut; } +int Conv2D::GetKernelW() const { return this->primitive->value.AsConv2D()->kernelW; } +int Conv2D::GetKernelH() const { return this->primitive->value.AsConv2D()->kernelH; } +int Conv2D::GetStrideW() const { return this->primitive->value.AsConv2D()->strideW; } +int Conv2D::GetStrideH() const { return this->primitive->value.AsConv2D()->strideH; } +int Conv2D::GetPadMode() const { return this->primitive->value.AsConv2D()->padMode; } +int Conv2D::GetPadUp() const { return this->primitive->value.AsConv2D()->padUp; } +int Conv2D::GetPadDown() const { return this->primitive->value.AsConv2D()->padDown; } +int Conv2D::GetPadLeft() const { return this->primitive->value.AsConv2D()->padLeft; } +int Conv2D::GetPadRight() const { return this->primitive->value.AsConv2D()->padRight; } +int Conv2D::GetDilateW() const { return this->primitive->value.AsConv2D()->dilateW; } +int Conv2D::GetDilateH() const { return this->primitive->value.AsConv2D()->dilateH; } +bool Conv2D::GetHasBias() const { return this->primitive->value.AsConv2D()->hasBias; } +int Conv2D::GetActivationType() const { return this->primitive->value.AsConv2D()->activationType; } + +void Conv2D::SetFormat(int format) { this->primitive->value.AsConv2D()->format = (schema::Format)format; } +void Conv2D::SetGroup(int group) { this->primitive->value.AsConv2D()->group = group; } +void Conv2D::SetChannelIn(int channel_in) { this->primitive->value.AsConv2D()->channelIn = channel_in; } +void Conv2D::SetChannelOut(int channel_out) { this->primitive->value.AsConv2D()->channelOut = channel_out; } +void Conv2D::SetKernelW(int kernel_w) { this->primitive->value.AsConv2D()->kernelW = kernel_w; } +void Conv2D::SetKernelH(int kernel_h) { this->primitive->value.AsConv2D()->kernelH = kernel_h; } +void Conv2D::SetStrideW(int stride_w) { this->primitive->value.AsConv2D()->strideW = stride_w; } +void Conv2D::SetStrideH(int stride_h) { this->primitive->value.AsConv2D()->strideH = stride_h; } +void Conv2D::SetPadMode(int pad_mode) { this->primitive->value.AsConv2D()->padMode = (schema::PadMode)pad_mode; } +void Conv2D::SetPadUp(int pad_up) { this->primitive->value.AsConv2D()->padUp = pad_up; } +void Conv2D::SetPadDown(int pad_down) { this->primitive->value.AsConv2D()->padDown = pad_down; } +void Conv2D::SetPadLeft(int pad_left) { this->primitive->value.AsConv2D()->padLeft = pad_left; } +void Conv2D::SetPadRight(int pad_right) { this->primitive->value.AsConv2D()->padRight = pad_right; } +void Conv2D::SetDilateW(int dilate_w) { this->primitive->value.AsConv2D()->dilateW = dilate_w; } +void Conv2D::SetDilateH(int dilate_h) { this->primitive->value.AsConv2D()->dilateH = dilate_h; } +void Conv2D::SetHasBias(bool has_bias) { this->primitive->value.AsConv2D()->hasBias = has_bias; } +void Conv2D::SetActivationType(int activation_type) { + this->primitive->value.AsConv2D()->activationType = (schema::ActivationType)activation_type; +} + +#else + +int Conv2D::GetFormat() const { return this->primitive->value_as_Conv2D()->format(); } +int Conv2D::GetGroup() const { return this->primitive->value_as_Conv2D()->group(); } +int Conv2D::GetChannelIn() const { return this->primitive->value_as_Conv2D()->channelIn(); } +int Conv2D::GetChannelOut() const { return this->primitive->value_as_Conv2D()->channelOut(); } +int Conv2D::GetKernelW() const { return this->primitive->value_as_Conv2D()->kernelW(); } +int Conv2D::GetKernelH() const { return this->primitive->value_as_Conv2D()->kernelH(); } +int Conv2D::GetStrideW() const { return this->primitive->value_as_Conv2D()->strideW(); } +int Conv2D::GetStrideH() const { return this->primitive->value_as_Conv2D()->strideH(); } +int Conv2D::GetPadMode() const { return this->primitive->value_as_Conv2D()->padMode(); } +int Conv2D::GetPadUp() const { return this->primitive->value_as_Conv2D()->padUp(); } +int Conv2D::GetPadDown() const { return this->primitive->value_as_Conv2D()->padDown(); } +int Conv2D::GetPadLeft() const { return this->primitive->value_as_Conv2D()->padLeft(); } +int Conv2D::GetPadRight() const { return this->primitive->value_as_Conv2D()->padRight(); } +int Conv2D::GetDilateW() const { return this->primitive->value_as_Conv2D()->dilateW(); } +int Conv2D::GetDilateH() const { return this->primitive->value_as_Conv2D()->dilateH(); } +bool Conv2D::GetHasBias() const { return this->primitive->value_as_Conv2D()->hasBias(); } +int Conv2D::GetActivationType() const { return this->primitive->value_as_Conv2D()->activationType(); } + +void Conv2D::SetFormat(int format) {} +void Conv2D::SetGroup(int group) {} +void Conv2D::SetChannelIn(int channel_in) {} +void Conv2D::SetChannelOut(int channel_out) {} +void Conv2D::SetKernelW(int kernel_w) {} +void Conv2D::SetKernelH(int kernel_h) {} +void Conv2D::SetStrideW(int stride_w) {} +void Conv2D::SetStrideH(int stride_h) {} +void Conv2D::SetPadMode(int pad_mode) {} +void Conv2D::SetPadUp(int pad_up) {} +void Conv2D::SetPadDown(int pad_down) {} +void Conv2D::SetPadLeft(int pad_left) {} +void Conv2D::SetPadRight(int pad_right) {} +void Conv2D::SetDilateW(int dilate_w) {} +void Conv2D::SetDilateH(int dilate_h) {} +void Conv2D::SetHasBias(bool has_bias) {} +void Conv2D::SetActivationType(int activation_type) {} +#endif +void Conv2D::ConvInferShape(int input_h, int input_w, int *output_h, int *output_w) { + MS_ASSERT(this->primitive != nullptr); + int kernel_w = GetKernelW(); + int kernel_h = GetKernelH(); + int stride_w = GetStrideW(); + int stride_h = GetStrideH(); + int dilate_w = GetDilateW(); + int dilate_h = GetDilateH(); + pad_l_ = GetPadLeft(); + pad_u_ = GetPadUp(); + pad_d_ = GetPadDown(); + pad_r_ = GetPadRight(); + + if (GetPadMode() == schema::PadMode_SAME) { + *output_w = std::ceil(static_cast(input_w) / static_cast(stride_w)); + *output_h = std::ceil(static_cast(input_h) / static_cast(stride_h)); + auto pad_h_all = ((*output_h - 1) * stride_h + (kernel_h - 1) * dilate_h + 1 - input_h); + auto pad_w_all = ((*output_w - 1) * stride_w + (kernel_w - 1) * dilate_w + 1 - input_w); + pad_u_ = pad_h_all / 2; + pad_d_ = pad_h_all - pad_u_; + pad_l_ = pad_w_all / 2; + pad_r_ = pad_w_all - pad_l_; + } else { + *output_w = std::ceil((static_cast(input_w) + pad_l_ + pad_r_ - + (static_cast(kernel_w) - 1) * static_cast(dilate_w)) / + static_cast(stride_w)); + *output_h = std::ceil((static_cast(input_h) + pad_u_ + pad_d_ - + (static_cast(kernel_h) - 1) * static_cast(dilate_h)) / + static_cast(stride_h)); + } +} + +int Conv2D::InferShape(std::vector inputs_, std::vector outputs_) { + if (inputs_.size() != 2 && inputs_.size() != 3) { + MS_LOG(ERROR) << "Add should has two or three inputs"; + return 1; + } + if (outputs_.size() != 1) { + MS_LOG(ERROR) << "Add should has one outputs"; + return 1; + } + auto *input_tensor = inputs_.front(); + auto *weight_tensor = inputs_.at(1); + auto *out_tensor = outputs_.front(); + MS_ASSERT(input_tensor != nullptr); + MS_ASSERT(out_tensor != nullptr); + + auto in_shape = input_tensor->shape(); + int input_h = in_shape.at(1); + int input_w = in_shape.at(2); + int output_w = 0, output_h = 0; + + this->ConvInferShape(input_h, input_w, &output_h, &output_w); + + std::vector out_shape{input_tensor->shape()}; + out_shape.at(1) = output_h; + out_shape.at(2) = output_w; + out_shape.at(3) = weight_tensor->shape()[0]; + out_tensor->set_shape(out_shape); + out_tensor->SetFormat(input_tensor->GetFormat()); + out_tensor->set_data_type(input_tensor->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/conv2d.h b/mindspore/lite/c_ops/conv2d.h new file mode 100644 index 0000000000..020d50e4d7 --- /dev/null +++ b/mindspore/lite/c_ops/conv2d.h @@ -0,0 +1,91 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_CONV2_D_H_ +#define LITE_MINDSPORE_LITE_C_OPS_CONV2_D_H_ + +namespace mindspore { +class Conv2D : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Conv2D(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Conv2D(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int PadUp() const; + int PadDown() const; + int PadLeft() const; + int PadRight() const; + + int GetFormat() const; + int GetGroup() const; + int GetChannelIn() const; + int GetChannelOut() const; + int GetKernelW() const; + int GetKernelH() const; + int GetStrideW() const; + int GetStrideH() const; + int GetPadMode() const; + int GetPadUp() const; + int GetPadDown() const; + int GetPadLeft() const; + int GetPadRight() const; + int GetDilateW() const; + int GetDilateH() const; + bool GetHasBias() const; + int GetActivationType() const; + void SetFormat(int format); + void SetGroup(int group); + void SetChannelIn(int channel_in); + void SetChannelOut(int channel_out); + void SetKernelW(int kernel_w); + void SetKernelH(int kernel_h); + void SetStrideW(int stride_w); + void SetStrideH(int stride_h); + void SetPadMode(int pad_mode); + void SetPadUp(int pad_up); + void SetPadDown(int pad_down); + void SetPadLeft(int pad_left); + void SetPadRight(int pad_right); + void SetDilateW(int dilate_w); + void SetDilateH(int dilate_h); + void SetHasBias(bool has_bias); + void SetActivationType(int activation_type); + + protected: + void ConvInferShape(int input_h, int input_w, int *output_h, int *output_w); + + protected: + int pad_u_ = 0; + int pad_d_ = 0; + int pad_l_ = 0; + int pad_r_ = 0; +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_CONV2_D_H_ diff --git a/mindspore/lite/c_ops/conv2d_grad_filter.cc b/mindspore/lite/c_ops/conv2d_grad_filter.cc new file mode 100644 index 0000000000..b556a25eb4 --- /dev/null +++ b/mindspore/lite/c_ops/conv2d_grad_filter.cc @@ -0,0 +1,107 @@ +/** + * 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 "c_ops/conv2d_grad_filter.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Conv2DGradFilter::GetFormat() const { return this->primitive->value.AsConv2DGradFilter()->format; } +int Conv2DGradFilter::GetGroup() const { return this->primitive->value.AsConv2DGradFilter()->group; } +int Conv2DGradFilter::GetChannelIn() const { return this->primitive->value.AsConv2DGradFilter()->channelIn; } +int Conv2DGradFilter::GetChannelOut() const { return this->primitive->value.AsConv2DGradFilter()->channelOut; } +int Conv2DGradFilter::GetKernelW() const { return this->primitive->value.AsConv2DGradFilter()->kernelW; } +int Conv2DGradFilter::GetKernelH() const { return this->primitive->value.AsConv2DGradFilter()->kernelH; } +int Conv2DGradFilter::GetStrideW() const { return this->primitive->value.AsConv2DGradFilter()->strideW; } +int Conv2DGradFilter::GetStrideH() const { return this->primitive->value.AsConv2DGradFilter()->strideH; } +int Conv2DGradFilter::GetPadMode() const { return this->primitive->value.AsConv2DGradFilter()->padMode; } +int Conv2DGradFilter::GetPadUp() const { return this->primitive->value.AsConv2DGradFilter()->padUp; } +int Conv2DGradFilter::GetPadDown() const { return this->primitive->value.AsConv2DGradFilter()->padDown; } +int Conv2DGradFilter::GetPadLeft() const { return this->primitive->value.AsConv2DGradFilter()->padLeft; } +int Conv2DGradFilter::GetPadRight() const { return this->primitive->value.AsConv2DGradFilter()->padRight; } +int Conv2DGradFilter::GetDilateW() const { return this->primitive->value.AsConv2DGradFilter()->dilateW; } +int Conv2DGradFilter::GetDilateH() const { return this->primitive->value.AsConv2DGradFilter()->dilateH; } +bool Conv2DGradFilter::GetHasBias() const { return this->primitive->value.AsConv2DGradFilter()->hasBias; } +int Conv2DGradFilter::GetActivationType() const { return this->primitive->value.AsConv2DGradFilter()->activationType; } + +void Conv2DGradFilter::SetFormat(int format) { + this->primitive->value.AsConv2DGradFilter()->format = (schema::Format)format; +} +void Conv2DGradFilter::SetGroup(int group) { this->primitive->value.AsConv2DGradFilter()->group = group; } +void Conv2DGradFilter::SetChannelIn(int channel_in) { + this->primitive->value.AsConv2DGradFilter()->channelIn = channel_in; +} +void Conv2DGradFilter::SetChannelOut(int channel_out) { + this->primitive->value.AsConv2DGradFilter()->channelOut = channel_out; +} +void Conv2DGradFilter::SetKernelW(int kernel_w) { this->primitive->value.AsConv2DGradFilter()->kernelW = kernel_w; } +void Conv2DGradFilter::SetKernelH(int kernel_h) { this->primitive->value.AsConv2DGradFilter()->kernelH = kernel_h; } +void Conv2DGradFilter::SetStrideW(int stride_w) { this->primitive->value.AsConv2DGradFilter()->strideW = stride_w; } +void Conv2DGradFilter::SetStrideH(int stride_h) { this->primitive->value.AsConv2DGradFilter()->strideH = stride_h; } +void Conv2DGradFilter::SetPadMode(int pad_mode) { + this->primitive->value.AsConv2DGradFilter()->padMode = (schema::PadMode)pad_mode; +} +void Conv2DGradFilter::SetPadUp(int pad_up) { this->primitive->value.AsConv2DGradFilter()->padUp = pad_up; } +void Conv2DGradFilter::SetPadDown(int pad_down) { this->primitive->value.AsConv2DGradFilter()->padDown = pad_down; } +void Conv2DGradFilter::SetPadLeft(int pad_left) { this->primitive->value.AsConv2DGradFilter()->padLeft = pad_left; } +void Conv2DGradFilter::SetPadRight(int pad_right) { this->primitive->value.AsConv2DGradFilter()->padRight = pad_right; } +void Conv2DGradFilter::SetDilateW(int dilate_w) { this->primitive->value.AsConv2DGradFilter()->dilateW = dilate_w; } +void Conv2DGradFilter::SetDilateH(int dilate_h) { this->primitive->value.AsConv2DGradFilter()->dilateH = dilate_h; } +void Conv2DGradFilter::SetHasBias(bool has_bias) { this->primitive->value.AsConv2DGradFilter()->hasBias = has_bias; } +void Conv2DGradFilter::SetActivationType(int activation_type) { + this->primitive->value.AsConv2DGradFilter()->activationType = (schema::ActivationType)activation_type; +} + +#else + +int Conv2DGradFilter::GetFormat() const { return this->primitive->value_as_Conv2DGradFilter()->format(); } +int Conv2DGradFilter::GetGroup() const { return this->primitive->value_as_Conv2DGradFilter()->group(); } +int Conv2DGradFilter::GetChannelIn() const { return this->primitive->value_as_Conv2DGradFilter()->channelIn(); } +int Conv2DGradFilter::GetChannelOut() const { return this->primitive->value_as_Conv2DGradFilter()->channelOut(); } +int Conv2DGradFilter::GetKernelW() const { return this->primitive->value_as_Conv2DGradFilter()->kernelW(); } +int Conv2DGradFilter::GetKernelH() const { return this->primitive->value_as_Conv2DGradFilter()->kernelH(); } +int Conv2DGradFilter::GetStrideW() const { return this->primitive->value_as_Conv2DGradFilter()->strideW(); } +int Conv2DGradFilter::GetStrideH() const { return this->primitive->value_as_Conv2DGradFilter()->strideH(); } +int Conv2DGradFilter::GetPadMode() const { return this->primitive->value_as_Conv2DGradFilter()->padMode(); } +int Conv2DGradFilter::GetPadUp() const { return this->primitive->value_as_Conv2DGradFilter()->padUp(); } +int Conv2DGradFilter::GetPadDown() const { return this->primitive->value_as_Conv2DGradFilter()->padDown(); } +int Conv2DGradFilter::GetPadLeft() const { return this->primitive->value_as_Conv2DGradFilter()->padLeft(); } +int Conv2DGradFilter::GetPadRight() const { return this->primitive->value_as_Conv2DGradFilter()->padRight(); } +int Conv2DGradFilter::GetDilateW() const { return this->primitive->value_as_Conv2DGradFilter()->dilateW(); } +int Conv2DGradFilter::GetDilateH() const { return this->primitive->value_as_Conv2DGradFilter()->dilateH(); } +bool Conv2DGradFilter::GetHasBias() const { return this->primitive->value_as_Conv2DGradFilter()->hasBias(); } +int Conv2DGradFilter::GetActivationType() const { + return this->primitive->value_as_Conv2DGradFilter()->activationType(); +} + +void Conv2DGradFilter::SetFormat(int format) {} +void Conv2DGradFilter::SetGroup(int group) {} +void Conv2DGradFilter::SetChannelIn(int channel_in) {} +void Conv2DGradFilter::SetChannelOut(int channel_out) {} +void Conv2DGradFilter::SetKernelW(int kernel_w) {} +void Conv2DGradFilter::SetKernelH(int kernel_h) {} +void Conv2DGradFilter::SetStrideW(int stride_w) {} +void Conv2DGradFilter::SetStrideH(int stride_h) {} +void Conv2DGradFilter::SetPadMode(int pad_mode) {} +void Conv2DGradFilter::SetPadUp(int pad_up) {} +void Conv2DGradFilter::SetPadDown(int pad_down) {} +void Conv2DGradFilter::SetPadLeft(int pad_left) {} +void Conv2DGradFilter::SetPadRight(int pad_right) {} +void Conv2DGradFilter::SetDilateW(int dilate_w) {} +void Conv2DGradFilter::SetDilateH(int dilate_h) {} +void Conv2DGradFilter::SetHasBias(bool has_bias) {} +void Conv2DGradFilter::SetActivationType(int activation_type) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/conv2d_grad_filter.h b/mindspore/lite/c_ops/conv2d_grad_filter.h new file mode 100644 index 0000000000..b38df2e095 --- /dev/null +++ b/mindspore/lite/c_ops/conv2d_grad_filter.h @@ -0,0 +1,76 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_CONV2_D_GRAD_FILTER_H_ +#define LITE_MINDSPORE_LITE_C_OPS_CONV2_D_GRAD_FILTER_H_ + +namespace mindspore { +class Conv2DGradFilter : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Conv2DGradFilter(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Conv2DGradFilter(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int GetFormat() const; + int GetGroup() const; + int GetChannelIn() const; + int GetChannelOut() const; + int GetKernelW() const; + int GetKernelH() const; + int GetStrideW() const; + int GetStrideH() const; + int GetPadMode() const; + int GetPadUp() const; + int GetPadDown() const; + int GetPadLeft() const; + int GetPadRight() const; + int GetDilateW() const; + int GetDilateH() const; + bool GetHasBias() const; + int GetActivationType() const; + void SetFormat(int format); + void SetGroup(int group); + void SetChannelIn(int channel_in); + void SetChannelOut(int channel_out); + void SetKernelW(int kernel_w); + void SetKernelH(int kernel_h); + void SetStrideW(int stride_w); + void SetStrideH(int stride_h); + void SetPadMode(int pad_mode); + void SetPadUp(int pad_up); + void SetPadDown(int pad_down); + void SetPadLeft(int pad_left); + void SetPadRight(int pad_right); + void SetDilateW(int dilate_w); + void SetDilateH(int dilate_h); + void SetHasBias(bool has_bias); + void SetActivationType(int activation_type); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_CONV2_D_GRAD_FILTER_H_ diff --git a/mindspore/lite/c_ops/conv2d_grad_input.cc b/mindspore/lite/c_ops/conv2d_grad_input.cc new file mode 100644 index 0000000000..c9a82ad7a8 --- /dev/null +++ b/mindspore/lite/c_ops/conv2d_grad_input.cc @@ -0,0 +1,105 @@ +/** + * 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 "c_ops/conv2d_grad_input.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Conv2DGradInput::GetFormat() const { return this->primitive->value.AsConv2DGradInput()->format; } +int Conv2DGradInput::GetGroup() const { return this->primitive->value.AsConv2DGradInput()->group; } +int Conv2DGradInput::GetChannelIn() const { return this->primitive->value.AsConv2DGradInput()->channelIn; } +int Conv2DGradInput::GetChannelOut() const { return this->primitive->value.AsConv2DGradInput()->channelOut; } +int Conv2DGradInput::GetKernelW() const { return this->primitive->value.AsConv2DGradInput()->kernelW; } +int Conv2DGradInput::GetKernelH() const { return this->primitive->value.AsConv2DGradInput()->kernelH; } +int Conv2DGradInput::GetStrideW() const { return this->primitive->value.AsConv2DGradInput()->strideW; } +int Conv2DGradInput::GetStrideH() const { return this->primitive->value.AsConv2DGradInput()->strideH; } +int Conv2DGradInput::GetPadMode() const { return this->primitive->value.AsConv2DGradInput()->padMode; } +int Conv2DGradInput::GetPadUp() const { return this->primitive->value.AsConv2DGradInput()->padUp; } +int Conv2DGradInput::GetPadDown() const { return this->primitive->value.AsConv2DGradInput()->padDown; } +int Conv2DGradInput::GetPadLeft() const { return this->primitive->value.AsConv2DGradInput()->padLeft; } +int Conv2DGradInput::GetPadRight() const { return this->primitive->value.AsConv2DGradInput()->padRight; } +int Conv2DGradInput::GetDilateW() const { return this->primitive->value.AsConv2DGradInput()->dilateW; } +int Conv2DGradInput::GetDilateH() const { return this->primitive->value.AsConv2DGradInput()->dilateH; } +bool Conv2DGradInput::GetHasBias() const { return this->primitive->value.AsConv2DGradInput()->hasBias; } +int Conv2DGradInput::GetActivationType() const { return this->primitive->value.AsConv2DGradInput()->activationType; } + +void Conv2DGradInput::SetFormat(int format) { + this->primitive->value.AsConv2DGradInput()->format = (schema::Format)format; +} +void Conv2DGradInput::SetGroup(int group) { this->primitive->value.AsConv2DGradInput()->group = group; } +void Conv2DGradInput::SetChannelIn(int channel_in) { + this->primitive->value.AsConv2DGradInput()->channelIn = channel_in; +} +void Conv2DGradInput::SetChannelOut(int channel_out) { + this->primitive->value.AsConv2DGradInput()->channelOut = channel_out; +} +void Conv2DGradInput::SetKernelW(int kernel_w) { this->primitive->value.AsConv2DGradInput()->kernelW = kernel_w; } +void Conv2DGradInput::SetKernelH(int kernel_h) { this->primitive->value.AsConv2DGradInput()->kernelH = kernel_h; } +void Conv2DGradInput::SetStrideW(int stride_w) { this->primitive->value.AsConv2DGradInput()->strideW = stride_w; } +void Conv2DGradInput::SetStrideH(int stride_h) { this->primitive->value.AsConv2DGradInput()->strideH = stride_h; } +void Conv2DGradInput::SetPadMode(int pad_mode) { + this->primitive->value.AsConv2DGradInput()->padMode = (schema::PadMode)pad_mode; +} +void Conv2DGradInput::SetPadUp(int pad_up) { this->primitive->value.AsConv2DGradInput()->padUp = pad_up; } +void Conv2DGradInput::SetPadDown(int pad_down) { this->primitive->value.AsConv2DGradInput()->padDown = pad_down; } +void Conv2DGradInput::SetPadLeft(int pad_left) { this->primitive->value.AsConv2DGradInput()->padLeft = pad_left; } +void Conv2DGradInput::SetPadRight(int pad_right) { this->primitive->value.AsConv2DGradInput()->padRight = pad_right; } +void Conv2DGradInput::SetDilateW(int dilate_w) { this->primitive->value.AsConv2DGradInput()->dilateW = dilate_w; } +void Conv2DGradInput::SetDilateH(int dilate_h) { this->primitive->value.AsConv2DGradInput()->dilateH = dilate_h; } +void Conv2DGradInput::SetHasBias(bool has_bias) { this->primitive->value.AsConv2DGradInput()->hasBias = has_bias; } +void Conv2DGradInput::SetActivationType(int activation_type) { + this->primitive->value.AsConv2DGradInput()->activationType = (schema::ActivationType)activation_type; +} + +#else + +int Conv2DGradInput::GetFormat() const { return this->primitive->value_as_Conv2DGradInput()->format(); } +int Conv2DGradInput::GetGroup() const { return this->primitive->value_as_Conv2DGradInput()->group(); } +int Conv2DGradInput::GetChannelIn() const { return this->primitive->value_as_Conv2DGradInput()->channelIn(); } +int Conv2DGradInput::GetChannelOut() const { return this->primitive->value_as_Conv2DGradInput()->channelOut(); } +int Conv2DGradInput::GetKernelW() const { return this->primitive->value_as_Conv2DGradInput()->kernelW(); } +int Conv2DGradInput::GetKernelH() const { return this->primitive->value_as_Conv2DGradInput()->kernelH(); } +int Conv2DGradInput::GetStrideW() const { return this->primitive->value_as_Conv2DGradInput()->strideW(); } +int Conv2DGradInput::GetStrideH() const { return this->primitive->value_as_Conv2DGradInput()->strideH(); } +int Conv2DGradInput::GetPadMode() const { return this->primitive->value_as_Conv2DGradInput()->padMode(); } +int Conv2DGradInput::GetPadUp() const { return this->primitive->value_as_Conv2DGradInput()->padUp(); } +int Conv2DGradInput::GetPadDown() const { return this->primitive->value_as_Conv2DGradInput()->padDown(); } +int Conv2DGradInput::GetPadLeft() const { return this->primitive->value_as_Conv2DGradInput()->padLeft(); } +int Conv2DGradInput::GetPadRight() const { return this->primitive->value_as_Conv2DGradInput()->padRight(); } +int Conv2DGradInput::GetDilateW() const { return this->primitive->value_as_Conv2DGradInput()->dilateW(); } +int Conv2DGradInput::GetDilateH() const { return this->primitive->value_as_Conv2DGradInput()->dilateH(); } +bool Conv2DGradInput::GetHasBias() const { return this->primitive->value_as_Conv2DGradInput()->hasBias(); } +int Conv2DGradInput::GetActivationType() const { return this->primitive->value_as_Conv2DGradInput()->activationType(); } + +void Conv2DGradInput::SetFormat(int format) {} +void Conv2DGradInput::SetGroup(int group) {} +void Conv2DGradInput::SetChannelIn(int channel_in) {} +void Conv2DGradInput::SetChannelOut(int channel_out) {} +void Conv2DGradInput::SetKernelW(int kernel_w) {} +void Conv2DGradInput::SetKernelH(int kernel_h) {} +void Conv2DGradInput::SetStrideW(int stride_w) {} +void Conv2DGradInput::SetStrideH(int stride_h) {} +void Conv2DGradInput::SetPadMode(int pad_mode) {} +void Conv2DGradInput::SetPadUp(int pad_up) {} +void Conv2DGradInput::SetPadDown(int pad_down) {} +void Conv2DGradInput::SetPadLeft(int pad_left) {} +void Conv2DGradInput::SetPadRight(int pad_right) {} +void Conv2DGradInput::SetDilateW(int dilate_w) {} +void Conv2DGradInput::SetDilateH(int dilate_h) {} +void Conv2DGradInput::SetHasBias(bool has_bias) {} +void Conv2DGradInput::SetActivationType(int activation_type) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/conv2d_grad_input.h b/mindspore/lite/c_ops/conv2d_grad_input.h new file mode 100644 index 0000000000..bbd4e5d36a --- /dev/null +++ b/mindspore/lite/c_ops/conv2d_grad_input.h @@ -0,0 +1,76 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_CONV2_D_GRAD_INPUT_H_ +#define LITE_MINDSPORE_LITE_C_OPS_CONV2_D_GRAD_INPUT_H_ + +namespace mindspore { +class Conv2DGradInput : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Conv2DGradInput(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Conv2DGradInput(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int GetFormat() const; + int GetGroup() const; + int GetChannelIn() const; + int GetChannelOut() const; + int GetKernelW() const; + int GetKernelH() const; + int GetStrideW() const; + int GetStrideH() const; + int GetPadMode() const; + int GetPadUp() const; + int GetPadDown() const; + int GetPadLeft() const; + int GetPadRight() const; + int GetDilateW() const; + int GetDilateH() const; + bool GetHasBias() const; + int GetActivationType() const; + void SetFormat(int format); + void SetGroup(int group); + void SetChannelIn(int channel_in); + void SetChannelOut(int channel_out); + void SetKernelW(int kernel_w); + void SetKernelH(int kernel_h); + void SetStrideW(int stride_w); + void SetStrideH(int stride_h); + void SetPadMode(int pad_mode); + void SetPadUp(int pad_up); + void SetPadDown(int pad_down); + void SetPadLeft(int pad_left); + void SetPadRight(int pad_right); + void SetDilateW(int dilate_w); + void SetDilateH(int dilate_h); + void SetHasBias(bool has_bias); + void SetActivationType(int activation_type); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_CONV2_D_GRAD_INPUT_H_ diff --git a/mindspore/lite/c_ops/cos.h b/mindspore/lite/c_ops/cos.h new file mode 100644 index 0000000000..d2f3b84a80 --- /dev/null +++ b/mindspore/lite/c_ops/cos.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic_self.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_COS_H_ +#define LITE_MINDSPORE_LITE_C_OPS_COS_H_ + +namespace mindspore { +class Cos : public ArithmeticSelf { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Cos(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} +#else + explicit Cos(schema::Primitive *primitive) : ArithmeticSelf(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_COS_H_ diff --git a/mindspore/lite/c_ops/crop.cc b/mindspore/lite/c_ops/crop.cc new file mode 100644 index 0000000000..1332190ec3 --- /dev/null +++ b/mindspore/lite/c_ops/crop.cc @@ -0,0 +1,55 @@ +/** + * 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 "c_ops/crop.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +long Crop::GetAxis() const { return this->primitive->value.AsCrop()->axis; } +std::vector Crop::GetOffsets() const { return this->primitive->value.AsCrop()->offsets; } + +void Crop::SetAxis(long axis) { this->primitive->value.AsCrop()->axis = axis; } +void Crop::SetOffsets(const std::vector &offsets) { this->primitive->value.AsCrop()->offsets = offsets; } + +#else + +long Crop::GetAxis() const { return this->primitive->value_as_Crop()->axis(); } +std::vector Crop::GetOffsets() const { + auto fb_vector = this->primitive->value_as_Crop()->offsets(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void Crop::SetAxis(long axis) {} +void Crop::SetOffsets(const std::vector &offsets) {} +#endif +namespace { +constexpr int kCropOutputNum = 1; +constexpr int kCropInputNum = 2; +} // namespace + +int Crop::InferShape(std::vector inputs, std::vector outputs) { + MS_ASSERT(this->primitive != nullptr); + if (outputs.size() != kCropOutputNum || inputs.size() != kCropInputNum) { + MS_LOG(ERROR) << "Invalid output/input size! output size: " << outputs.size() << ",input size: " << inputs.size(); + return 1; + } + outputs[0]->set_shape(inputs[1]->shape()); + outputs[0]->SetFormat(inputs[0]->GetFormat()); + outputs[0]->set_data_type(inputs[0]->data_type()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/crop.h b/mindspore/lite/c_ops/crop.h new file mode 100644 index 0000000000..c7d2c32da4 --- /dev/null +++ b/mindspore/lite/c_ops/crop.h @@ -0,0 +1,47 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_CROP_H_ +#define LITE_MINDSPORE_LITE_C_OPS_CROP_H_ + +namespace mindspore { +class Crop : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Crop(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Crop(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + long GetAxis() const; + std::vector GetOffsets() const; + void SetAxis(long axis); + void SetOffsets(const std::vector &offsets); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_CROP_H_ diff --git a/mindspore/lite/c_ops/deconv2d.cc b/mindspore/lite/c_ops/deconv2d.cc new file mode 100644 index 0000000000..eb8777e4b5 --- /dev/null +++ b/mindspore/lite/c_ops/deconv2d.cc @@ -0,0 +1,145 @@ +/** + * 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 "c_ops/deconv2d.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int DeConv2D::GetFormat() const { return this->primitive->value.AsDeConv2D()->format; } +int DeConv2D::GetGroup() const { return this->primitive->value.AsDeConv2D()->group; } +int DeConv2D::GetChannelIn() const { return this->primitive->value.AsDeConv2D()->channelIn; } +int DeConv2D::GetChannelOut() const { return this->primitive->value.AsDeConv2D()->channelOut; } +int DeConv2D::GetKernelW() const { return this->primitive->value.AsDeConv2D()->kernelW; } +int DeConv2D::GetKernelH() const { return this->primitive->value.AsDeConv2D()->kernelH; } +int DeConv2D::GetStrideW() const { return this->primitive->value.AsDeConv2D()->strideW; } +int DeConv2D::GetStrideH() const { return this->primitive->value.AsDeConv2D()->strideH; } +int DeConv2D::GetPadMode() const { return this->primitive->value.AsDeConv2D()->padMode; } +int DeConv2D::GetPadUp() const { return this->primitive->value.AsDeConv2D()->padUp; } +int DeConv2D::GetPadDown() const { return this->primitive->value.AsDeConv2D()->padDown; } +int DeConv2D::GetPadLeft() const { return this->primitive->value.AsDeConv2D()->padLeft; } +int DeConv2D::GetPadRight() const { return this->primitive->value.AsDeConv2D()->padRight; } +int DeConv2D::GetDilateW() const { return this->primitive->value.AsDeConv2D()->dilateW; } +int DeConv2D::GetDilateH() const { return this->primitive->value.AsDeConv2D()->dilateH; } +bool DeConv2D::GetHasBias() const { return this->primitive->value.AsDeConv2D()->hasBias; } +int DeConv2D::GetActivationType() const { return this->primitive->value.AsDeConv2D()->activationType; } + +void DeConv2D::SetFormat(int format) { this->primitive->value.AsDeConv2D()->format = (schema::Format)format; } +void DeConv2D::SetGroup(int group) { this->primitive->value.AsDeConv2D()->group = group; } +void DeConv2D::SetChannelIn(int channel_in) { this->primitive->value.AsDeConv2D()->channelIn = channel_in; } +void DeConv2D::SetChannelOut(int channel_out) { this->primitive->value.AsDeConv2D()->channelOut = channel_out; } +void DeConv2D::SetKernelW(int kernel_w) { this->primitive->value.AsDeConv2D()->kernelW = kernel_w; } +void DeConv2D::SetKernelH(int kernel_h) { this->primitive->value.AsDeConv2D()->kernelH = kernel_h; } +void DeConv2D::SetStrideW(int stride_w) { this->primitive->value.AsDeConv2D()->strideW = stride_w; } +void DeConv2D::SetStrideH(int stride_h) { this->primitive->value.AsDeConv2D()->strideH = stride_h; } +void DeConv2D::SetPadMode(int pad_mode) { this->primitive->value.AsDeConv2D()->padMode = (schema::PadMode)pad_mode; } +void DeConv2D::SetPadUp(int pad_up) { this->primitive->value.AsDeConv2D()->padUp = pad_up; } +void DeConv2D::SetPadDown(int pad_down) { this->primitive->value.AsDeConv2D()->padDown = pad_down; } +void DeConv2D::SetPadLeft(int pad_left) { this->primitive->value.AsDeConv2D()->padLeft = pad_left; } +void DeConv2D::SetPadRight(int pad_right) { this->primitive->value.AsDeConv2D()->padRight = pad_right; } +void DeConv2D::SetDilateW(int dilate_w) { this->primitive->value.AsDeConv2D()->dilateW = dilate_w; } +void DeConv2D::SetDilateH(int dilate_h) { this->primitive->value.AsDeConv2D()->dilateH = dilate_h; } +void DeConv2D::SetHasBias(bool has_bias) { this->primitive->value.AsDeConv2D()->hasBias = has_bias; } +void DeConv2D::SetActivationType(int activation_type) { + this->primitive->value.AsDeConv2D()->activationType = (schema::ActivationType)activation_type; +} + +#else + +int DeConv2D::GetFormat() const { return this->primitive->value_as_DeConv2D()->format(); } +int DeConv2D::GetGroup() const { return this->primitive->value_as_DeConv2D()->group(); } +int DeConv2D::GetChannelIn() const { return this->primitive->value_as_DeConv2D()->channelIn(); } +int DeConv2D::GetChannelOut() const { return this->primitive->value_as_DeConv2D()->channelOut(); } +int DeConv2D::GetKernelW() const { return this->primitive->value_as_DeConv2D()->kernelW(); } +int DeConv2D::GetKernelH() const { return this->primitive->value_as_DeConv2D()->kernelH(); } +int DeConv2D::GetStrideW() const { return this->primitive->value_as_DeConv2D()->strideW(); } +int DeConv2D::GetStrideH() const { return this->primitive->value_as_DeConv2D()->strideH(); } +int DeConv2D::GetPadMode() const { return this->primitive->value_as_DeConv2D()->padMode(); } +int DeConv2D::GetPadUp() const { return this->primitive->value_as_DeConv2D()->padUp(); } +int DeConv2D::GetPadDown() const { return this->primitive->value_as_DeConv2D()->padDown(); } +int DeConv2D::GetPadLeft() const { return this->primitive->value_as_DeConv2D()->padLeft(); } +int DeConv2D::GetPadRight() const { return this->primitive->value_as_DeConv2D()->padRight(); } +int DeConv2D::GetDilateW() const { return this->primitive->value_as_DeConv2D()->dilateW(); } +int DeConv2D::GetDilateH() const { return this->primitive->value_as_DeConv2D()->dilateH(); } +bool DeConv2D::GetHasBias() const { return this->primitive->value_as_DeConv2D()->hasBias(); } +int DeConv2D::GetActivationType() const { return this->primitive->value_as_DeConv2D()->activationType(); } + +void DeConv2D::SetFormat(int format) {} +void DeConv2D::SetGroup(int group) {} +void DeConv2D::SetChannelIn(int channel_in) {} +void DeConv2D::SetChannelOut(int channel_out) {} +void DeConv2D::SetKernelW(int kernel_w) {} +void DeConv2D::SetKernelH(int kernel_h) {} +void DeConv2D::SetStrideW(int stride_w) {} +void DeConv2D::SetStrideH(int stride_h) {} +void DeConv2D::SetPadMode(int pad_mode) {} +void DeConv2D::SetPadUp(int pad_up) {} +void DeConv2D::SetPadDown(int pad_down) {} +void DeConv2D::SetPadLeft(int pad_left) {} +void DeConv2D::SetPadRight(int pad_right) {} +void DeConv2D::SetDilateW(int dilate_w) {} +void DeConv2D::SetDilateH(int dilate_h) {} +void DeConv2D::SetHasBias(bool has_bias) {} +void DeConv2D::SetActivationType(int activation_type) {} +#endif +int DeConv2D::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto weight = inputs_.at(1); + MS_ASSERT(weight != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + + int32_t input_h = input->Height(); + int32_t input_w = input->Width(); + + int32_t output_n = input->Batch(); + int32_t output_h = 0; + int32_t output_w = 0; + int32_t output_c = weight->Channel(); + + int kernel_w = GetKernelW(); + int kernel_h = GetKernelH(); + int stride_w = GetStrideW(); + int stride_h = GetStrideH(); + int dilate_w = GetDilateW(); + int dilate_h = GetDilateH(); + pad_l_ = GetPadLeft(); + pad_u_ = GetPadUp(); + pad_d_ = GetPadDown(); + pad_r_ = GetPadRight(); + auto pad_mode = (schema::PadMode)GetPadMode(); + + if (pad_mode == schema::PadMode_CAFFE) { + output_h = (input_h - 1) * stride_h + ((kernel_h - 1) * dilate_h + 1) - pad_u_ - pad_d_; + output_w = (input_w - 1) * stride_w + ((kernel_w - 1) * dilate_w + 1) - pad_l_ - pad_r_; + } else if (pad_mode == schema::PadMode_SAME) { + output_h = input_h * stride_h; + output_w = input_w * stride_w; + } else if (pad_mode == schema::PadMode_VALID) { + output_h = (input_h - 1) * stride_h + kernel_h; + output_w = (input_w - 1) * stride_w + kernel_w; + } else { + MS_LOG(ERROR) << "unsupported pad mode for deconv"; + } + + std::vector out_shape = {output_n, output_h, output_w, output_c}; + output->set_shape(out_shape); + output->SetFormat(input->GetFormat()); + output->set_data_type(input->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/deconv2d.h b/mindspore/lite/c_ops/deconv2d.h new file mode 100644 index 0000000000..b94e1a2a4f --- /dev/null +++ b/mindspore/lite/c_ops/deconv2d.h @@ -0,0 +1,88 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_DE_CONV2_D_H_ +#define LITE_MINDSPORE_LITE_C_OPS_DE_CONV2_D_H_ + +namespace mindspore { +class DeConv2D : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit DeConv2D(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit DeConv2D(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetFormat() const; + int GetGroup() const; + int GetChannelIn() const; + int GetChannelOut() const; + int GetKernelW() const; + int GetKernelH() const; + int GetStrideW() const; + int GetStrideH() const; + int GetPadMode() const; + int GetPadUp() const; + int GetPadDown() const; + int GetPadLeft() const; + int GetPadRight() const; + int GetDilateW() const; + int GetDilateH() const; + bool GetHasBias() const; + int GetActivationType() const; + void SetFormat(int format); + void SetGroup(int group); + void SetChannelIn(int channel_in); + void SetChannelOut(int channel_out); + void SetKernelW(int kernel_w); + void SetKernelH(int kernel_h); + void SetStrideW(int stride_w); + void SetStrideH(int stride_h); + void SetPadMode(int pad_mode); + void SetPadUp(int pad_up); + void SetPadDown(int pad_down); + void SetPadLeft(int pad_left); + void SetPadRight(int pad_right); + void SetDilateW(int dilate_w); + void SetDilateH(int dilate_h); + void SetHasBias(bool has_bias); + void SetActivationType(int activation_type); + + int PadUp() const { return this->pad_u_; } + int PadDown() const { return this->pad_d_; } + int PadLeft() const { return this->pad_l_; } + int PadRight() const { return this->pad_r_; } + + protected: + int pad_u_ = 0; + int pad_d_ = 0; + int pad_l_ = 0; + int pad_r_ = 0; +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_DE_CONV2_D_H_ diff --git a/mindspore/lite/c_ops/dedepthwise_conv2d.cc b/mindspore/lite/c_ops/dedepthwise_conv2d.cc new file mode 100644 index 0000000000..17c813a502 --- /dev/null +++ b/mindspore/lite/c_ops/dedepthwise_conv2d.cc @@ -0,0 +1,161 @@ +/** + * 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 "c_ops/dedepthwise_conv2d.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int DeDepthwiseConv2D::GetFormat() const { return this->primitive->value.AsDeDepthwiseConv2D()->format; } +int DeDepthwiseConv2D::GetChannelIn() const { return this->primitive->value.AsDeDepthwiseConv2D()->channelIn; } +int DeDepthwiseConv2D::GetChannelMultiplier() const { + return this->primitive->value.AsDeDepthwiseConv2D()->channelMultiplier; +} +int DeDepthwiseConv2D::GetKernelW() const { return this->primitive->value.AsDeDepthwiseConv2D()->kernelW; } +int DeDepthwiseConv2D::GetKernelH() const { return this->primitive->value.AsDeDepthwiseConv2D()->kernelH; } +int DeDepthwiseConv2D::GetStrideW() const { return this->primitive->value.AsDeDepthwiseConv2D()->strideW; } +int DeDepthwiseConv2D::GetStrideH() const { return this->primitive->value.AsDeDepthwiseConv2D()->strideH; } +int DeDepthwiseConv2D::GetPadMode() const { return this->primitive->value.AsDeDepthwiseConv2D()->padMode; } +int DeDepthwiseConv2D::GetPadUp() const { return this->primitive->value.AsDeDepthwiseConv2D()->padUp; } +int DeDepthwiseConv2D::GetPadDown() const { return this->primitive->value.AsDeDepthwiseConv2D()->padDown; } +int DeDepthwiseConv2D::GetPadLeft() const { return this->primitive->value.AsDeDepthwiseConv2D()->padLeft; } +int DeDepthwiseConv2D::GetPadRight() const { return this->primitive->value.AsDeDepthwiseConv2D()->padRight; } +int DeDepthwiseConv2D::GetDilateW() const { return this->primitive->value.AsDeDepthwiseConv2D()->dilateW; } +int DeDepthwiseConv2D::GetDilateH() const { return this->primitive->value.AsDeDepthwiseConv2D()->dilateH; } +bool DeDepthwiseConv2D::GetHasBias() const { return this->primitive->value.AsDeDepthwiseConv2D()->hasBias; } +int DeDepthwiseConv2D::GetActivationType() const { + return this->primitive->value.AsDeDepthwiseConv2D()->activationType; +} + +void DeDepthwiseConv2D::SetFormat(int format) { + this->primitive->value.AsDeDepthwiseConv2D()->format = (schema::Format)format; +} +void DeDepthwiseConv2D::SetChannelIn(int channel_in) { + this->primitive->value.AsDeDepthwiseConv2D()->channelIn = channel_in; +} +void DeDepthwiseConv2D::SetChannelMultiplier(int channel_multiplier) { + this->primitive->value.AsDeDepthwiseConv2D()->channelMultiplier = channel_multiplier; +} +void DeDepthwiseConv2D::SetKernelW(int kernel_w) { this->primitive->value.AsDeDepthwiseConv2D()->kernelW = kernel_w; } +void DeDepthwiseConv2D::SetKernelH(int kernel_h) { this->primitive->value.AsDeDepthwiseConv2D()->kernelH = kernel_h; } +void DeDepthwiseConv2D::SetStrideW(int stride_w) { this->primitive->value.AsDeDepthwiseConv2D()->strideW = stride_w; } +void DeDepthwiseConv2D::SetStrideH(int stride_h) { this->primitive->value.AsDeDepthwiseConv2D()->strideH = stride_h; } +void DeDepthwiseConv2D::SetPadMode(int pad_mode) { + this->primitive->value.AsDeDepthwiseConv2D()->padMode = (schema::PadMode)pad_mode; +} +void DeDepthwiseConv2D::SetPadUp(int pad_up) { this->primitive->value.AsDeDepthwiseConv2D()->padUp = pad_up; } +void DeDepthwiseConv2D::SetPadDown(int pad_down) { this->primitive->value.AsDeDepthwiseConv2D()->padDown = pad_down; } +void DeDepthwiseConv2D::SetPadLeft(int pad_left) { this->primitive->value.AsDeDepthwiseConv2D()->padLeft = pad_left; } +void DeDepthwiseConv2D::SetPadRight(int pad_right) { + this->primitive->value.AsDeDepthwiseConv2D()->padRight = pad_right; +} +void DeDepthwiseConv2D::SetDilateW(int dilate_w) { this->primitive->value.AsDeDepthwiseConv2D()->dilateW = dilate_w; } +void DeDepthwiseConv2D::SetDilateH(int dilate_h) { this->primitive->value.AsDeDepthwiseConv2D()->dilateH = dilate_h; } +void DeDepthwiseConv2D::SetHasBias(bool has_bias) { this->primitive->value.AsDeDepthwiseConv2D()->hasBias = has_bias; } +void DeDepthwiseConv2D::SetActivationType(int activation_type) { + this->primitive->value.AsDeDepthwiseConv2D()->activationType = (schema::ActivationType)activation_type; +} + +#else + +int DeDepthwiseConv2D::GetFormat() const { return this->primitive->value_as_DeDepthwiseConv2D()->format(); } +int DeDepthwiseConv2D::GetChannelIn() const { return this->primitive->value_as_DeDepthwiseConv2D()->channelIn(); } +int DeDepthwiseConv2D::GetChannelMultiplier() const { + return this->primitive->value_as_DeDepthwiseConv2D()->channelMultiplier(); +} +int DeDepthwiseConv2D::GetKernelW() const { return this->primitive->value_as_DeDepthwiseConv2D()->kernelW(); } +int DeDepthwiseConv2D::GetKernelH() const { return this->primitive->value_as_DeDepthwiseConv2D()->kernelH(); } +int DeDepthwiseConv2D::GetStrideW() const { return this->primitive->value_as_DeDepthwiseConv2D()->strideW(); } +int DeDepthwiseConv2D::GetStrideH() const { return this->primitive->value_as_DeDepthwiseConv2D()->strideH(); } +int DeDepthwiseConv2D::GetPadMode() const { return this->primitive->value_as_DeDepthwiseConv2D()->padMode(); } +int DeDepthwiseConv2D::GetPadUp() const { return this->primitive->value_as_DeDepthwiseConv2D()->padUp(); } +int DeDepthwiseConv2D::GetPadDown() const { return this->primitive->value_as_DeDepthwiseConv2D()->padDown(); } +int DeDepthwiseConv2D::GetPadLeft() const { return this->primitive->value_as_DeDepthwiseConv2D()->padLeft(); } +int DeDepthwiseConv2D::GetPadRight() const { return this->primitive->value_as_DeDepthwiseConv2D()->padRight(); } +int DeDepthwiseConv2D::GetDilateW() const { return this->primitive->value_as_DeDepthwiseConv2D()->dilateW(); } +int DeDepthwiseConv2D::GetDilateH() const { return this->primitive->value_as_DeDepthwiseConv2D()->dilateH(); } +bool DeDepthwiseConv2D::GetHasBias() const { return this->primitive->value_as_DeDepthwiseConv2D()->hasBias(); } +int DeDepthwiseConv2D::GetActivationType() const { + return this->primitive->value_as_DeDepthwiseConv2D()->activationType(); +} + +void DeDepthwiseConv2D::SetFormat(int format) {} +void DeDepthwiseConv2D::SetChannelIn(int channel_in) {} +void DeDepthwiseConv2D::SetChannelMultiplier(int channel_multiplier) {} +void DeDepthwiseConv2D::SetKernelW(int kernel_w) {} +void DeDepthwiseConv2D::SetKernelH(int kernel_h) {} +void DeDepthwiseConv2D::SetStrideW(int stride_w) {} +void DeDepthwiseConv2D::SetStrideH(int stride_h) {} +void DeDepthwiseConv2D::SetPadMode(int pad_mode) {} +void DeDepthwiseConv2D::SetPadUp(int pad_up) {} +void DeDepthwiseConv2D::SetPadDown(int pad_down) {} +void DeDepthwiseConv2D::SetPadLeft(int pad_left) {} +void DeDepthwiseConv2D::SetPadRight(int pad_right) {} +void DeDepthwiseConv2D::SetDilateW(int dilate_w) {} +void DeDepthwiseConv2D::SetDilateH(int dilate_h) {} +void DeDepthwiseConv2D::SetHasBias(bool has_bias) {} +void DeDepthwiseConv2D::SetActivationType(int activation_type) {} +#endif +int DeDepthwiseConv2D::InferShape(std::vector inputs_, + std::vector outputs_) { + if (inputs_.size() != kDoubleNum && inputs_.size() != kMultiNum) { + MS_LOG(ERROR) << "inputs number is invalid"; + return 1; + } + if (outputs_.size() != kSingleNum) { + MS_LOG(ERROR) << "output number is invalid"; + return 1; + } + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto weight = inputs_.at(1); + MS_ASSERT(weight != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + + auto in_shape = input->shape(); + int input_h = in_shape.at(1); + int input_w = in_shape.at(2); + int input_channel = in_shape.at(3); + int output_w = 0, output_h = 0; + + pad_l_ = GetPadLeft(); + pad_u_ = GetPadUp(); + pad_d_ = GetPadDown(); + pad_r_ = GetPadRight(); + output_h = GetStrideH() * (input_h - 1) * GetKernelH() - pad_u_ - pad_d_; + output_w = GetStrideW() * (input_w - 1) * GetKernelW() - pad_l_ - pad_r_; + if ((output_h + GetPadUp() + GetPadDown() - GetKernelH()) % GetStrideH() != 0) { + output_h += (output_h + GetPadLeft() + GetPadRight() - GetKernelH()) % GetStrideH(); + } + if ((output_w + GetPadLeft() + GetPadRight() - GetKernelW()) % GetStrideW() != 0) { + output_w += (output_w + GetPadLeft() + GetPadRight() - GetKernelW()) % GetStrideW(); + } + std::vector out_shape{input->shape()}; + out_shape.at(1) = output_h; + out_shape.at(2) = output_w; + if (GetChannelMultiplier() * input_channel != weight->shape()[0]) { + MS_LOG(ERROR) << "Conv depthwise only support group equals output channel."; + return 1; + } + out_shape.at(3) = weight->shape()[0] * weight->shape()[3]; // in_channel * out_channel + + output->set_shape(out_shape); + output->SetFormat(input->GetFormat()); + output->set_data_type(input->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/dedepthwise_conv2d.h b/mindspore/lite/c_ops/dedepthwise_conv2d.h new file mode 100644 index 0000000000..e77e281797 --- /dev/null +++ b/mindspore/lite/c_ops/dedepthwise_conv2d.h @@ -0,0 +1,86 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_DE_DEPTHWISE_CONV2_D_H_ +#define LITE_MINDSPORE_LITE_C_OPS_DE_DEPTHWISE_CONV2_D_H_ + +namespace mindspore { +class DeDepthwiseConv2D : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit DeDepthwiseConv2D(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit DeDepthwiseConv2D(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetFormat() const; + int GetChannelIn() const; + int GetChannelMultiplier() const; + int GetKernelW() const; + int GetKernelH() const; + int GetStrideW() const; + int GetStrideH() const; + int GetPadMode() const; + int GetPadUp() const; + int GetPadDown() const; + int GetPadLeft() const; + int GetPadRight() const; + int GetDilateW() const; + int GetDilateH() const; + bool GetHasBias() const; + int GetActivationType() const; + void SetFormat(int format); + void SetChannelIn(int channel_in); + void SetChannelMultiplier(int channel_multiplier); + void SetKernelW(int kernel_w); + void SetKernelH(int kernel_h); + void SetStrideW(int stride_w); + void SetStrideH(int stride_h); + void SetPadMode(int pad_mode); + void SetPadUp(int pad_up); + void SetPadDown(int pad_down); + void SetPadLeft(int pad_left); + void SetPadRight(int pad_right); + void SetDilateW(int dilate_w); + void SetDilateH(int dilate_h); + void SetHasBias(bool has_bias); + void SetActivationType(int activation_type); + + int PadUp() const { return this->pad_u_; } + int PadDown() const { return this->pad_d_; } + int PadLeft() const { return this->pad_l_; } + int PadRight() const { return this->pad_r_; } + + protected: + int pad_u_ = 0; + int pad_d_ = 0; + int pad_l_ = 0; + int pad_r_ = 0; +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_DE_DEPTHWISE_CONV2_D_H_ diff --git a/mindspore/lite/c_ops/depth_to_space.cc b/mindspore/lite/c_ops/depth_to_space.cc new file mode 100644 index 0000000000..52a7794ad0 --- /dev/null +++ b/mindspore/lite/c_ops/depth_to_space.cc @@ -0,0 +1,75 @@ +/** + * 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 "c_ops/depth_to_space.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int DepthToSpace::GetBlockSize() const { return this->primitive->value.AsDepthToSpace()->blockSize; } +int DepthToSpace::GetFormat() const { return this->primitive->value.AsDepthToSpace()->format; } + +void DepthToSpace::SetBlockSize(int block_size) { this->primitive->value.AsDepthToSpace()->blockSize = block_size; } +void DepthToSpace::SetFormat(int format) { this->primitive->value.AsDepthToSpace()->format = format; } + +#else + +int DepthToSpace::GetBlockSize() const { return this->primitive->value_as_DepthToSpace()->blockSize(); } +int DepthToSpace::GetFormat() const { return this->primitive->value_as_DepthToSpace()->format(); } + +void DepthToSpace::SetBlockSize(int block_size) {} +void DepthToSpace::SetFormat(int format) {} +#endif +namespace { +constexpr int kDepthToSpaceOutputNum = 1; +constexpr int kDepthToSpaceInputNum = 1; +} // namespace + +int DepthToSpace::InferShape(std::vector inputs, std::vector outputs) { + MS_ASSERT(this->primitive != nullptr); + if (outputs.size() != kDepthToSpaceOutputNum || inputs.size() != kDepthToSpaceInputNum) { + MS_LOG(ERROR) << "Invalid output/input size! output size: " << outputs.size() << ",input size: " << inputs.size(); + return 1; + } + + auto input = inputs.at(0); + if (input->GetFormat() != schema::Format_NHWC) { + MS_LOG(ERROR) << "depth_to_space only support NHWC now!"; + return 1; + } + auto input_shape = input->shape(); + if (input_shape.size() != kDimension_4d) { + MS_LOG(ERROR) << "input shape dimension size should == " << kDimension_4d; + return 1; + } + + int32_t block_size = GetBlockSize(); + if (input_shape[NHWC_C] % (block_size * block_size) != 0 || input_shape[NHWC_C] == 0) { + MS_LOG(ERROR) << "input dimension c size " << input_shape[NHWC_C] << " should be mulitple of block_size(" + << block_size << ") * block_size)!"; + return 1; + } + std::vector output_shape(input_shape.size()); + output_shape[NHWC_N] = input_shape[NHWC_N]; + output_shape[NHWC_H] = input_shape[NHWC_H] * block_size; + output_shape[NHWC_W] = input_shape[NHWC_W] * block_size; + output_shape[NHWC_C] = input_shape[NHWC_C] / (block_size * block_size); + outputs[0]->set_shape(output_shape); + outputs[0]->set_data_type(input->data_type()); + outputs[0]->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/depth_to_space.h b/mindspore/lite/c_ops/depth_to_space.h new file mode 100644 index 0000000000..1c02c25320 --- /dev/null +++ b/mindspore/lite/c_ops/depth_to_space.h @@ -0,0 +1,47 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_DEPTH_TO_SPACE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_DEPTH_TO_SPACE_H_ + +namespace mindspore { +class DepthToSpace : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit DepthToSpace(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit DepthToSpace(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetBlockSize() const; + int GetFormat() const; + void SetBlockSize(int block_size); + void SetFormat(int format); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_DEPTH_TO_SPACE_H_ diff --git a/mindspore/lite/c_ops/depthwise_conv2d.cc b/mindspore/lite/c_ops/depthwise_conv2d.cc new file mode 100644 index 0000000000..beb6e8ddf8 --- /dev/null +++ b/mindspore/lite/c_ops/depthwise_conv2d.cc @@ -0,0 +1,164 @@ +/** + * 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 "c_ops/depthwise_conv2d.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int DepthwiseConv2D::GetFormat() const { return this->primitive->value.AsDepthwiseConv2D()->format; } +int DepthwiseConv2D::GetChannelIn() const { return this->primitive->value.AsDepthwiseConv2D()->channelIn; } +int DepthwiseConv2D::GetChannelMultiplier() const { + return this->primitive->value.AsDepthwiseConv2D()->channelMultiplier; +} +int DepthwiseConv2D::GetKernelW() const { return this->primitive->value.AsDepthwiseConv2D()->kernelW; } +int DepthwiseConv2D::GetKernelH() const { return this->primitive->value.AsDepthwiseConv2D()->kernelH; } +int DepthwiseConv2D::GetStrideW() const { return this->primitive->value.AsDepthwiseConv2D()->strideW; } +int DepthwiseConv2D::GetStrideH() const { return this->primitive->value.AsDepthwiseConv2D()->strideH; } +int DepthwiseConv2D::GetPadMode() const { return this->primitive->value.AsDepthwiseConv2D()->padMode; } +int DepthwiseConv2D::GetPadUp() const { return this->primitive->value.AsDepthwiseConv2D()->padUp; } +int DepthwiseConv2D::GetPadDown() const { return this->primitive->value.AsDepthwiseConv2D()->padDown; } +int DepthwiseConv2D::GetPadLeft() const { return this->primitive->value.AsDepthwiseConv2D()->padLeft; } +int DepthwiseConv2D::GetPadRight() const { return this->primitive->value.AsDepthwiseConv2D()->padRight; } +int DepthwiseConv2D::GetDilateW() const { return this->primitive->value.AsDepthwiseConv2D()->dilateW; } +int DepthwiseConv2D::GetDilateH() const { return this->primitive->value.AsDepthwiseConv2D()->dilateH; } +bool DepthwiseConv2D::GetHasBias() const { return this->primitive->value.AsDepthwiseConv2D()->hasBias; } +int DepthwiseConv2D::GetActivationType() const { return this->primitive->value.AsDepthwiseConv2D()->activationType; } + +void DepthwiseConv2D::SetFormat(int format) { + this->primitive->value.AsDepthwiseConv2D()->format = (schema::Format)format; +} +void DepthwiseConv2D::SetChannelIn(int channel_in) { + this->primitive->value.AsDepthwiseConv2D()->channelIn = channel_in; +} +void DepthwiseConv2D::SetChannelMultiplier(int channel_multiplier) { + this->primitive->value.AsDepthwiseConv2D()->channelMultiplier = channel_multiplier; +} +void DepthwiseConv2D::SetKernelW(int kernel_w) { this->primitive->value.AsDepthwiseConv2D()->kernelW = kernel_w; } +void DepthwiseConv2D::SetKernelH(int kernel_h) { this->primitive->value.AsDepthwiseConv2D()->kernelH = kernel_h; } +void DepthwiseConv2D::SetStrideW(int stride_w) { this->primitive->value.AsDepthwiseConv2D()->strideW = stride_w; } +void DepthwiseConv2D::SetStrideH(int stride_h) { this->primitive->value.AsDepthwiseConv2D()->strideH = stride_h; } +void DepthwiseConv2D::SetPadMode(int pad_mode) { + this->primitive->value.AsDepthwiseConv2D()->padMode = (schema::PadMode)pad_mode; +} +void DepthwiseConv2D::SetPadUp(int pad_up) { this->primitive->value.AsDepthwiseConv2D()->padUp = pad_up; } +void DepthwiseConv2D::SetPadDown(int pad_down) { this->primitive->value.AsDepthwiseConv2D()->padDown = pad_down; } +void DepthwiseConv2D::SetPadLeft(int pad_left) { this->primitive->value.AsDepthwiseConv2D()->padLeft = pad_left; } +void DepthwiseConv2D::SetPadRight(int pad_right) { this->primitive->value.AsDepthwiseConv2D()->padRight = pad_right; } +void DepthwiseConv2D::SetDilateW(int dilate_w) { this->primitive->value.AsDepthwiseConv2D()->dilateW = dilate_w; } +void DepthwiseConv2D::SetDilateH(int dilate_h) { this->primitive->value.AsDepthwiseConv2D()->dilateH = dilate_h; } +void DepthwiseConv2D::SetHasBias(bool has_bias) { this->primitive->value.AsDepthwiseConv2D()->hasBias = has_bias; } +void DepthwiseConv2D::SetActivationType(int activation_type) { + this->primitive->value.AsDepthwiseConv2D()->activationType = (schema::ActivationType)activation_type; +} + +#else + +int DepthwiseConv2D::GetFormat() const { return this->primitive->value_as_DepthwiseConv2D()->format(); } +int DepthwiseConv2D::GetChannelIn() const { return this->primitive->value_as_DepthwiseConv2D()->channelIn(); } +int DepthwiseConv2D::GetChannelMultiplier() const { + return this->primitive->value_as_DepthwiseConv2D()->channelMultiplier(); +} +int DepthwiseConv2D::GetKernelW() const { return this->primitive->value_as_DepthwiseConv2D()->kernelW(); } +int DepthwiseConv2D::GetKernelH() const { return this->primitive->value_as_DepthwiseConv2D()->kernelH(); } +int DepthwiseConv2D::GetStrideW() const { return this->primitive->value_as_DepthwiseConv2D()->strideW(); } +int DepthwiseConv2D::GetStrideH() const { return this->primitive->value_as_DepthwiseConv2D()->strideH(); } +int DepthwiseConv2D::GetPadMode() const { return this->primitive->value_as_DepthwiseConv2D()->padMode(); } +int DepthwiseConv2D::GetPadUp() const { return this->primitive->value_as_DepthwiseConv2D()->padUp(); } +int DepthwiseConv2D::GetPadDown() const { return this->primitive->value_as_DepthwiseConv2D()->padDown(); } +int DepthwiseConv2D::GetPadLeft() const { return this->primitive->value_as_DepthwiseConv2D()->padLeft(); } +int DepthwiseConv2D::GetPadRight() const { return this->primitive->value_as_DepthwiseConv2D()->padRight(); } +int DepthwiseConv2D::GetDilateW() const { return this->primitive->value_as_DepthwiseConv2D()->dilateW(); } +int DepthwiseConv2D::GetDilateH() const { return this->primitive->value_as_DepthwiseConv2D()->dilateH(); } +bool DepthwiseConv2D::GetHasBias() const { return this->primitive->value_as_DepthwiseConv2D()->hasBias(); } +int DepthwiseConv2D::GetActivationType() const { return this->primitive->value_as_DepthwiseConv2D()->activationType(); } + +void DepthwiseConv2D::SetFormat(int format) {} +void DepthwiseConv2D::SetChannelIn(int channel_in) {} +void DepthwiseConv2D::SetChannelMultiplier(int channel_multiplier) {} +void DepthwiseConv2D::SetKernelW(int kernel_w) {} +void DepthwiseConv2D::SetKernelH(int kernel_h) {} +void DepthwiseConv2D::SetStrideW(int stride_w) {} +void DepthwiseConv2D::SetStrideH(int stride_h) {} +void DepthwiseConv2D::SetPadMode(int pad_mode) {} +void DepthwiseConv2D::SetPadUp(int pad_up) {} +void DepthwiseConv2D::SetPadDown(int pad_down) {} +void DepthwiseConv2D::SetPadLeft(int pad_left) {} +void DepthwiseConv2D::SetPadRight(int pad_right) {} +void DepthwiseConv2D::SetDilateW(int dilate_w) {} +void DepthwiseConv2D::SetDilateH(int dilate_h) {} +void DepthwiseConv2D::SetHasBias(bool has_bias) {} +void DepthwiseConv2D::SetActivationType(int activation_type) {} +#endif +int DepthwiseConv2D::InferShape(std::vector inputs_, + std::vector outputs_) { + if (inputs_.size() != kDoubleNum && inputs_.size() != kMultiNum) { + MS_LOG(ERROR) << "inputs number is invalid"; + return 1; + } + if (outputs_.size() != kSingleNum) { + MS_LOG(ERROR) << "output number is invalid"; + return 1; + } + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto weight = inputs_.at(1); + MS_ASSERT(weight != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + + auto in_shape = input->shape(); + int input_h = in_shape.at(1); + int input_w = in_shape.at(2); + int input_channel = in_shape.at(3); + int output_w = 0, output_h = 0; + + pad_l_ = GetPadLeft(); + pad_u_ = GetPadUp(); + pad_d_ = GetPadDown(); + pad_r_ = GetPadRight(); + if (GetPadMode() == schema::PadMode_SAME) { + output_h = std::ceil(static_cast(input_h) / static_cast(GetStrideH())); + output_w = std::ceil(static_cast(input_w) / static_cast(GetStrideW())); + auto pad_h_all = ((output_h - 1) * GetStrideH() + (GetKernelH() - 1) * GetDilateH() + 1 - input_h); + auto pad_w_all = ((output_w - 1) * GetStrideW() + (GetKernelW() - 1) * GetDilateW() + 1 - input_w); + pad_u_ = pad_h_all / 2; + pad_d_ = pad_h_all - pad_u_; + pad_l_ = pad_w_all / 2; + pad_r_ = pad_w_all - pad_l_; + } else { + output_h = std::ceil((static_cast(input_h) + pad_u_ + pad_d_ - + (static_cast(GetKernelH()) - 1) * static_cast(GetDilateH())) / + static_cast(GetStrideH())); + output_w = std::ceil((static_cast(input_w) + pad_l_ + pad_r_ - + (static_cast(GetKernelW()) - 1) * static_cast(GetDilateW())) / + static_cast(GetStrideW())); + } + std::vector out_shape{input->shape()}; + out_shape.at(1) = output_h; + out_shape.at(2) = output_w; + if (GetChannelMultiplier() * input_channel != weight->shape()[0]) { + MS_LOG(ERROR) << "Conv depthwise only support group equals output channel."; + return 1; + } + out_shape.at(3) = weight->shape()[0] * weight->shape()[3]; // in_channel * out_channel + + output->set_shape(out_shape); + output->SetFormat(input->GetFormat()); + output->set_data_type(input->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/depthwise_conv2d.h b/mindspore/lite/c_ops/depthwise_conv2d.h new file mode 100644 index 0000000000..5de8d7224d --- /dev/null +++ b/mindspore/lite/c_ops/depthwise_conv2d.h @@ -0,0 +1,86 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_DEPTHWISE_CONV2_D_H_ +#define LITE_MINDSPORE_LITE_C_OPS_DEPTHWISE_CONV2_D_H_ + +namespace mindspore { +class DepthwiseConv2D : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit DepthwiseConv2D(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit DepthwiseConv2D(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetFormat() const; + int GetChannelIn() const; + int GetChannelMultiplier() const; + int GetKernelW() const; + int GetKernelH() const; + int GetStrideW() const; + int GetStrideH() const; + int GetPadMode() const; + int GetPadUp() const; + int GetPadDown() const; + int GetPadLeft() const; + int GetPadRight() const; + int GetDilateW() const; + int GetDilateH() const; + bool GetHasBias() const; + int GetActivationType() const; + void SetFormat(int format); + void SetChannelIn(int channel_in); + void SetChannelMultiplier(int channel_multiplier); + void SetKernelW(int kernel_w); + void SetKernelH(int kernel_h); + void SetStrideW(int stride_w); + void SetStrideH(int stride_h); + void SetPadMode(int pad_mode); + void SetPadUp(int pad_up); + void SetPadDown(int pad_down); + void SetPadLeft(int pad_left); + void SetPadRight(int pad_right); + void SetDilateW(int dilate_w); + void SetDilateH(int dilate_h); + void SetHasBias(bool has_bias); + void SetActivationType(int activation_type); + + int PadUp() const { return this->pad_u_; } + int PadDown() const { return this->pad_d_; } + int PadLeft() const { return this->pad_l_; } + int PadRight() const { return this->pad_r_; } + + protected: + int pad_u_ = 0; + int pad_d_ = 0; + int pad_l_ = 0; + int pad_r_ = 0; +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_DEPTHWISE_CONV2_D_H_ diff --git a/mindspore/lite/c_ops/detection_post_process.cc b/mindspore/lite/c_ops/detection_post_process.cc new file mode 100644 index 0000000000..ca75aafc93 --- /dev/null +++ b/mindspore/lite/c_ops/detection_post_process.cc @@ -0,0 +1,131 @@ +/** + * 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 "c_ops/detection_post_process.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int DetectionPostProcess::GetFormat() const { return this->primitive->value.AsDetectionPostProcess()->format; } +int DetectionPostProcess::GetInputSize() const { return this->primitive->value.AsDetectionPostProcess()->inputSize; } +float DetectionPostProcess::GetHScale() const { return this->primitive->value.AsDetectionPostProcess()->hScale; } +float DetectionPostProcess::GetWScale() const { return this->primitive->value.AsDetectionPostProcess()->wScale; } +float DetectionPostProcess::GetXScale() const { return this->primitive->value.AsDetectionPostProcess()->xScale; } +float DetectionPostProcess::GetYScale() const { return this->primitive->value.AsDetectionPostProcess()->yScale; } +float DetectionPostProcess::GetNmsIouThreshold() const { + return this->primitive->value.AsDetectionPostProcess()->NmsIouThreshold; +} +float DetectionPostProcess::GetNmsScoreThreshold() const { + return this->primitive->value.AsDetectionPostProcess()->NmsScoreThreshold; +} +long DetectionPostProcess::GetMaxDetections() const { + return this->primitive->value.AsDetectionPostProcess()->MaxDetections; +} +long DetectionPostProcess::GetDetectionsPreClass() const { + return this->primitive->value.AsDetectionPostProcess()->DetectionsPreClass; +} +long DetectionPostProcess::GetMaxClassesPreDetection() const { + return this->primitive->value.AsDetectionPostProcess()->MaxClassesPreDetection; +} +long DetectionPostProcess::GetNumClasses() const { return this->primitive->value.AsDetectionPostProcess()->NumClasses; } +bool DetectionPostProcess::GetUseRegularNms() const { + return this->primitive->value.AsDetectionPostProcess()->UseRegularNms; +} + +void DetectionPostProcess::SetFormat(int format) { + this->primitive->value.AsDetectionPostProcess()->format = (schema::Format)format; +} +void DetectionPostProcess::SetInputSize(int input_size) { + this->primitive->value.AsDetectionPostProcess()->inputSize = input_size; +} +void DetectionPostProcess::SetHScale(float h_scale) { + this->primitive->value.AsDetectionPostProcess()->hScale = h_scale; +} +void DetectionPostProcess::SetWScale(float w_scale) { + this->primitive->value.AsDetectionPostProcess()->wScale = w_scale; +} +void DetectionPostProcess::SetXScale(float x_scale) { + this->primitive->value.AsDetectionPostProcess()->xScale = x_scale; +} +void DetectionPostProcess::SetYScale(float y_scale) { + this->primitive->value.AsDetectionPostProcess()->yScale = y_scale; +} +void DetectionPostProcess::SetNmsIouThreshold(float nms_iou_threshold) { + this->primitive->value.AsDetectionPostProcess()->NmsIouThreshold = nms_iou_threshold; +} +void DetectionPostProcess::SetNmsScoreThreshold(float nms_score_threshold) { + this->primitive->value.AsDetectionPostProcess()->NmsScoreThreshold = nms_score_threshold; +} +void DetectionPostProcess::SetMaxDetections(long max_detections) { + this->primitive->value.AsDetectionPostProcess()->MaxClassesPreDetection = max_detections; +} +void DetectionPostProcess::SetDetectionsPreClass(long detections_pre_class) { + this->primitive->value.AsDetectionPostProcess()->DetectionsPreClass = detections_pre_class; +} +void DetectionPostProcess::SetMaxClassesPreDetection(long max_classes_pre_detection) { + this->primitive->value.AsDetectionPostProcess()->MaxClassesPreDetection = max_classes_pre_detection; +} +void DetectionPostProcess::SetNumClasses(long num_classes) { + this->primitive->value.AsDetectionPostProcess()->NumClasses = num_classes; +} +void DetectionPostProcess::SetUseRegularNms(bool use_regular_nms) { + this->primitive->value.AsDetectionPostProcess()->UseRegularNms = use_regular_nms; +} + +#else + +int DetectionPostProcess::GetFormat() const { return this->primitive->value_as_DetectionPostProcess()->format(); } +int DetectionPostProcess::GetInputSize() const { return this->primitive->value_as_DetectionPostProcess()->inputSize(); } +float DetectionPostProcess::GetHScale() const { return this->primitive->value_as_DetectionPostProcess()->hScale(); } +float DetectionPostProcess::GetWScale() const { return this->primitive->value_as_DetectionPostProcess()->wScale(); } +float DetectionPostProcess::GetXScale() const { return this->primitive->value_as_DetectionPostProcess()->xScale(); } +float DetectionPostProcess::GetYScale() const { return this->primitive->value_as_DetectionPostProcess()->yScale(); } +float DetectionPostProcess::GetNmsIouThreshold() const { + return this->primitive->value_as_DetectionPostProcess()->NmsIouThreshold(); +} +float DetectionPostProcess::GetNmsScoreThreshold() const { + return this->primitive->value_as_DetectionPostProcess()->NmsScoreThreshold(); +} +long DetectionPostProcess::GetMaxDetections() const { + return this->primitive->value_as_DetectionPostProcess()->MaxDetections(); +} +long DetectionPostProcess::GetDetectionsPreClass() const { + return this->primitive->value_as_DetectionPostProcess()->DetectionsPreClass(); +} +long DetectionPostProcess::GetMaxClassesPreDetection() const { + return this->primitive->value_as_DetectionPostProcess()->MaxClassesPreDetection(); +} +long DetectionPostProcess::GetNumClasses() const { + return this->primitive->value_as_DetectionPostProcess()->NumClasses(); +} +bool DetectionPostProcess::GetUseRegularNms() const { + return this->primitive->value_as_DetectionPostProcess()->UseRegularNms(); +} + +void DetectionPostProcess::SetFormat(int format) {} +void DetectionPostProcess::SetInputSize(int input_size) {} +void DetectionPostProcess::SetHScale(float h_scale) {} +void DetectionPostProcess::SetWScale(float w_scale) {} +void DetectionPostProcess::SetXScale(float x_scale) {} +void DetectionPostProcess::SetYScale(float y_scale) {} +void DetectionPostProcess::SetNmsIouThreshold(float nms_iou_threshold) {} +void DetectionPostProcess::SetNmsScoreThreshold(float nms_score_threshold) {} +void DetectionPostProcess::SetMaxDetections(long max_detections) {} +void DetectionPostProcess::SetDetectionsPreClass(long detections_pre_class) {} +void DetectionPostProcess::SetMaxClassesPreDetection(long max_classes_pre_detection) {} +void DetectionPostProcess::SetNumClasses(long num_classes) {} +void DetectionPostProcess::SetUseRegularNms(bool use_regular_nms) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/detection_post_process.h b/mindspore/lite/c_ops/detection_post_process.h new file mode 100644 index 0000000000..6be3feb61b --- /dev/null +++ b/mindspore/lite/c_ops/detection_post_process.h @@ -0,0 +1,68 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_DETECTION_POST_PROCESS_H_ +#define LITE_MINDSPORE_LITE_C_OPS_DETECTION_POST_PROCESS_H_ + +namespace mindspore { +class DetectionPostProcess : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit DetectionPostProcess(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit DetectionPostProcess(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int GetFormat() const; + int GetInputSize() const; + float GetHScale() const; + float GetWScale() const; + float GetXScale() const; + float GetYScale() const; + float GetNmsIouThreshold() const; + float GetNmsScoreThreshold() const; + long GetMaxDetections() const; + long GetDetectionsPreClass() const; + long GetMaxClassesPreDetection() const; + long GetNumClasses() const; + bool GetUseRegularNms() const; + void SetFormat(int format); + void SetInputSize(int input_size); + void SetHScale(float h_scale); + void SetWScale(float w_scale); + void SetXScale(float x_scale); + void SetYScale(float y_scale); + void SetNmsIouThreshold(float nms_iou_threshold); + void SetNmsScoreThreshold(float nms_score_threshold); + void SetMaxDetections(long max_detections); + void SetDetectionsPreClass(long detections_pre_class); + void SetMaxClassesPreDetection(long max_classes_pre_detection); + void SetNumClasses(long num_classes); + void SetUseRegularNms(bool use_regular_nms); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_DETECTION_POST_PROCESS_H_ diff --git a/mindspore/lite/c_ops/div.cc b/mindspore/lite/c_ops/div.cc new file mode 100644 index 0000000000..dc378c522d --- /dev/null +++ b/mindspore/lite/c_ops/div.cc @@ -0,0 +1,33 @@ +/** + * 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 "c_ops/div.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Div::GetActivationType() const { return this->primitive->value.AsDiv()->activationType; } + +void Div::SetActivationType(int activation_type) { + this->primitive->value.AsDiv()->activationType = (schema::ActivationType)activation_type; +} + +#else + +int Div::GetActivationType() const { return this->primitive->value_as_Div()->activationType(); } + +void Div::SetActivationType(int activation_type) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/div.h b/mindspore/lite/c_ops/div.h new file mode 100644 index 0000000000..fa4efffe7f --- /dev/null +++ b/mindspore/lite/c_ops/div.h @@ -0,0 +1,46 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic.h" + +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_DIV_H_ +#define LITE_MINDSPORE_LITE_C_OPS_DIV_H_ + +namespace mindspore { +class Div : public Arithmetic { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Div(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} +#else + explicit Div(schema::Primitive *primitive) : Arithmetic(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetActivationType() const; + void SetActivationType(int activation_type); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_DIV_H_ diff --git a/mindspore/lite/c_ops/dropout.cc b/mindspore/lite/c_ops/dropout.cc new file mode 100644 index 0000000000..97b26d46d8 --- /dev/null +++ b/mindspore/lite/c_ops/dropout.cc @@ -0,0 +1,31 @@ +/** + * 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 "c_ops/dropout.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +float Dropout::GetRatio() const { return this->primitive->value.AsDropout()->ratio; } + +void Dropout::SetRatio(float ratio) { this->primitive->value.AsDropout()->ratio = ratio; } + +#else + +float Dropout::GetRatio() const { return this->primitive->value_as_Dropout()->ratio(); } + +void Dropout::SetRatio(float ratio) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/dropout.h b/mindspore/lite/c_ops/dropout.h new file mode 100644 index 0000000000..0a781162c2 --- /dev/null +++ b/mindspore/lite/c_ops/dropout.h @@ -0,0 +1,44 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_DROPOUT_H_ +#define LITE_MINDSPORE_LITE_C_OPS_DROPOUT_H_ + +namespace mindspore { +class Dropout : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Dropout(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Dropout(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + float GetRatio() const; + void SetRatio(float ratio); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_DROPOUT_H_ diff --git a/mindspore/lite/c_ops/eltwise.cc b/mindspore/lite/c_ops/eltwise.cc new file mode 100644 index 0000000000..882a54f8ac --- /dev/null +++ b/mindspore/lite/c_ops/eltwise.cc @@ -0,0 +1,31 @@ +/** + * 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 "c_ops/eltwise.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Eltwise::GetMode() const { return this->primitive->value.AsEltwise()->mode; } + +void Eltwise::SetMode(int mode) { this->primitive->value.AsEltwise()->mode = (schema::EltwiseMode)mode; } + +#else + +int Eltwise::GetMode() const { return this->primitive->value_as_Eltwise()->mode(); } + +void Eltwise::SetMode(int mode) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/eltwise.h b/mindspore/lite/c_ops/eltwise.h new file mode 100644 index 0000000000..4354018566 --- /dev/null +++ b/mindspore/lite/c_ops/eltwise.h @@ -0,0 +1,44 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_ELTWISE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_ELTWISE_H_ + +namespace mindspore { +class Eltwise : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Eltwise(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Eltwise(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int GetMode() const; + void SetMode(int mode); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_ELTWISE_H_ diff --git a/mindspore/lite/c_ops/elu.cc b/mindspore/lite/c_ops/elu.cc new file mode 100644 index 0000000000..955af3823e --- /dev/null +++ b/mindspore/lite/c_ops/elu.cc @@ -0,0 +1,31 @@ +/** + * 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 "c_ops/elu.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +float Elu::GetAlpha() const { return this->primitive->value.AsElu()->alpha; } + +void Elu::SetAlpha(float alpha) { this->primitive->value.AsElu()->alpha = alpha; } + +#else + +float Elu::GetAlpha() const { return this->primitive->value_as_Elu()->alpha(); } + +void Elu::SetAlpha(float alpha) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/elu.h b/mindspore/lite/c_ops/elu.h new file mode 100644 index 0000000000..7160256097 --- /dev/null +++ b/mindspore/lite/c_ops/elu.h @@ -0,0 +1,44 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_ELU_H_ +#define LITE_MINDSPORE_LITE_C_OPS_ELU_H_ + +namespace mindspore { +class Elu : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Elu(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Elu(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + float GetAlpha() const; + void SetAlpha(float alpha); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_ELU_H_ diff --git a/mindspore/lite/c_ops/embedding_lookup.cc b/mindspore/lite/c_ops/embedding_lookup.cc new file mode 100644 index 0000000000..e609560d18 --- /dev/null +++ b/mindspore/lite/c_ops/embedding_lookup.cc @@ -0,0 +1,72 @@ +/** + * 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 "c_ops/embedding_lookup.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +float EmbeddingLookup::GetMaxNorm() const { return this->primitive->value.AsEmbeddingLookup()->maxNorm; } + +void EmbeddingLookup::SetMaxNorm(float max_norm) { this->primitive->value.AsEmbeddingLookup()->maxNorm = max_norm; } + +#else + +float EmbeddingLookup::GetMaxNorm() const { return this->primitive->value_as_EmbeddingLookup()->maxNorm(); } + +void EmbeddingLookup::SetMaxNorm(float max_norm) {} +#endif +int EmbeddingLookup::InferShape(std::vector inputs_, + std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + if (inputs_.size() < kDoubleNum) { + MS_LOG(ERROR) << "Embedding Lookup should have at least two inputs"; + return 1; + } + + if (outputs_.size() != kSingleNum) { + MS_LOG(ERROR) << "Embedding Lookup should have one outputs"; + return 1; + } + + auto params_ = inputs_.front(); + MS_ASSERT(params_ != nullptr); + auto ids = inputs_.back(); + MS_ASSERT(ids != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + + auto embedding_shape = params_->shape(); + embedding_shape.erase(embedding_shape.begin()); + + std::vector output_shape(ids->shape()); + for (size_t i = 0; i < embedding_shape.size(); ++i) { + output_shape.push_back(embedding_shape.at(i)); + } + + for (int i = 1; i < inputs_.size() - 1; ++i) { + auto embedding_shape_t = inputs_.at(i)->shape(); + embedding_shape_t.erase(embedding_shape_t.begin()); + if (embedding_shape_t != embedding_shape) { + MS_LOG(ERROR) << "The embedded layers should have the same shape"; + return 1; + } + } + + output->set_shape(output_shape); + output->set_data_type(params_->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/embedding_lookup.h b/mindspore/lite/c_ops/embedding_lookup.h new file mode 100644 index 0000000000..744befbac6 --- /dev/null +++ b/mindspore/lite/c_ops/embedding_lookup.h @@ -0,0 +1,45 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_EMBEDDING_LOOKUP_H_ +#define LITE_MINDSPORE_LITE_C_OPS_EMBEDDING_LOOKUP_H_ + +namespace mindspore { +class EmbeddingLookup : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit EmbeddingLookup(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit EmbeddingLookup(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + float GetMaxNorm() const; + void SetMaxNorm(float max_norm); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_EMBEDDING_LOOKUP_H_ diff --git a/mindspore/lite/c_ops/embedding_lookup_sparse.cc b/mindspore/lite/c_ops/embedding_lookup_sparse.cc new file mode 100644 index 0000000000..284bd0fa86 --- /dev/null +++ b/mindspore/lite/c_ops/embedding_lookup_sparse.cc @@ -0,0 +1,57 @@ +/** + * 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 "c_ops/embedding_lookup_sparse.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector EmbeddingLookupSparse::GetSpIds() const { + return this->primitive->value.AsEmbeddingLookupSparse()->spIds; +} +std::vector EmbeddingLookupSparse::GetSpWeights() const { + return this->primitive->value.AsEmbeddingLookupSparse()->spWeights; +} +float EmbeddingLookupSparse::GetMaxNortm() const { return this->primitive->value.AsEmbeddingLookupSparse()->maxNortm; } + +void EmbeddingLookupSparse::SetSpIds(const std::vector &sp_ids) { + this->primitive->value.AsEmbeddingLookupSparse()->spIds = sp_ids; +} +void EmbeddingLookupSparse::SetSpWeights(const std::vector &sp_weights) { + this->primitive->value.AsEmbeddingLookupSparse()->spWeights = sp_weights; +} +void EmbeddingLookupSparse::SetMaxNortm(float max_nortm) { + this->primitive->value.AsEmbeddingLookupSparse()->maxNortm = max_nortm; +} + +#else + +std::vector EmbeddingLookupSparse::GetSpIds() const { + auto fb_vector = this->primitive->value_as_EmbeddingLookupSparse()->spIds(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +std::vector EmbeddingLookupSparse::GetSpWeights() const { + auto fb_vector = this->primitive->value_as_EmbeddingLookupSparse()->spWeights(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +float EmbeddingLookupSparse::GetMaxNortm() const { + return this->primitive->value_as_EmbeddingLookupSparse()->maxNortm(); +} + +void EmbeddingLookupSparse::SetSpIds(const std::vector &sp_ids) {} +void EmbeddingLookupSparse::SetSpWeights(const std::vector &sp_weights) {} +void EmbeddingLookupSparse::SetMaxNortm(float max_nortm) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/embedding_lookup_sparse.h b/mindspore/lite/c_ops/embedding_lookup_sparse.h new file mode 100644 index 0000000000..cf10c2d555 --- /dev/null +++ b/mindspore/lite/c_ops/embedding_lookup_sparse.h @@ -0,0 +1,48 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_EMBEDDING_LOOKUP_SPARSE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_EMBEDDING_LOOKUP_SPARSE_H_ + +namespace mindspore { +class EmbeddingLookupSparse : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit EmbeddingLookupSparse(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit EmbeddingLookupSparse(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + std::vector GetSpIds() const; + std::vector GetSpWeights() const; + float GetMaxNortm() const; + void SetSpIds(const std::vector &sp_ids); + void SetSpWeights(const std::vector &sp_weights); + void SetMaxNortm(float max_nortm); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_EMBEDDING_LOOKUP_SPARSE_H_ diff --git a/mindspore/lite/c_ops/equal.h b/mindspore/lite/c_ops/equal.h new file mode 100644 index 0000000000..f535c883d0 --- /dev/null +++ b/mindspore/lite/c_ops/equal.h @@ -0,0 +1,38 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_EQUAL_H_ +#define LITE_MINDSPORE_LITE_C_OPS_EQUAL_H_ + +namespace mindspore { +class Equal : public Arithmetic { + public: + explicit Equal(schema::Primitive *primitive) : Arithmetic(primitive) {} +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_EQUAL_H_ diff --git a/mindspore/lite/c_ops/exp.h b/mindspore/lite/c_ops/exp.h new file mode 100644 index 0000000000..f245b7a3a4 --- /dev/null +++ b/mindspore/lite/c_ops/exp.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic_self.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_EXP_H_ +#define LITE_MINDSPORE_LITE_C_OPS_EXP_H_ + +namespace mindspore { +class Exp : public ArithmeticSelf { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Exp(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} +#else + explicit Exp(schema::Primitive *primitive) : ArithmeticSelf(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_EXP_H_ diff --git a/mindspore/lite/c_ops/expand_dims.cc b/mindspore/lite/c_ops/expand_dims.cc new file mode 100644 index 0000000000..47d8a97bfe --- /dev/null +++ b/mindspore/lite/c_ops/expand_dims.cc @@ -0,0 +1,60 @@ +/** + * 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 "c_ops/expand_dims.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int ExpandDims::GetDim() const { return this->primitive->value.AsExpandDims()->dim; } + +void ExpandDims::SetDim(int dim) { this->primitive->value.AsExpandDims()->dim = dim; } + +#else + +int ExpandDims::GetDim() const { return this->primitive->value_as_ExpandDims()->dim(); } + +void ExpandDims::SetDim(int dim) {} +#endif +int ExpandDims::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + if (inputs_.size() != kSingleNum) { + MS_LOG(ERROR) << "input size is invalid"; + } + if (outputs_.size() != kSingleNum) { + MS_LOG(ERROR) << "output size is invalid"; + } + + int dim = GetDim(); + if (dim < 0) { + dim += input->shape().size() + 1; + } + if (dim > input->shape().size()) { + MS_LOG(ERROR) << "attribute dim out of range"; + return 1; + } + auto out_shape = input->shape(); + out_shape.insert(out_shape.begin() + dim, 1, 1); + output->set_shape(out_shape); + output->set_data_type(input->data_type()); + output->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/expand_dims.h b/mindspore/lite/c_ops/expand_dims.h new file mode 100644 index 0000000000..931182d064 --- /dev/null +++ b/mindspore/lite/c_ops/expand_dims.h @@ -0,0 +1,45 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_EXPAND_DIMS_H_ +#define LITE_MINDSPORE_LITE_C_OPS_EXPAND_DIMS_H_ + +namespace mindspore { +class ExpandDims : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit ExpandDims(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit ExpandDims(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetDim() const; + void SetDim(int dim); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_EXPAND_DIMS_H_ diff --git a/mindspore/lite/c_ops/fake_quant_with_min_max_vars.cc b/mindspore/lite/c_ops/fake_quant_with_min_max_vars.cc new file mode 100644 index 0000000000..957aa1f44a --- /dev/null +++ b/mindspore/lite/c_ops/fake_quant_with_min_max_vars.cc @@ -0,0 +1,45 @@ +/** + * 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 "c_ops/fake_quant_with_min_max_vars.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +bool FakeQuantWithMinMaxVars::GetNarrowRange() const { + return this->primitive->value.AsFakeQuantWithMinMaxVars()->narrowRange; +} +int FakeQuantWithMinMaxVars::GetNumBits() const { return this->primitive->value.AsFakeQuantWithMinMaxVars()->numBits; } + +void FakeQuantWithMinMaxVars::SetNarrowRange(bool narrow_range) { + this->primitive->value.AsFakeQuantWithMinMaxVars()->narrowRange = narrow_range; +} +void FakeQuantWithMinMaxVars::SetNumBits(int num_bits) { + this->primitive->value.AsFakeQuantWithMinMaxVars()->numBits = num_bits; +} + +#else + +bool FakeQuantWithMinMaxVars::GetNarrowRange() const { + return this->primitive->value_as_FakeQuantWithMinMaxVars()->narrowRange(); +} +int FakeQuantWithMinMaxVars::GetNumBits() const { + return this->primitive->value_as_FakeQuantWithMinMaxVars()->numBits(); +} + +void FakeQuantWithMinMaxVars::SetNarrowRange(bool narrow_range) {} +void FakeQuantWithMinMaxVars::SetNumBits(int num_bits) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/fake_quant_with_min_max_vars.h b/mindspore/lite/c_ops/fake_quant_with_min_max_vars.h new file mode 100644 index 0000000000..d29ac2c528 --- /dev/null +++ b/mindspore/lite/c_ops/fake_quant_with_min_max_vars.h @@ -0,0 +1,46 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_FAKE_QUANT_WITH_MIN_MAX_VARS_H_ +#define LITE_MINDSPORE_LITE_C_OPS_FAKE_QUANT_WITH_MIN_MAX_VARS_H_ + +namespace mindspore { +class FakeQuantWithMinMaxVars : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit FakeQuantWithMinMaxVars(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit FakeQuantWithMinMaxVars(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + bool GetNarrowRange() const; + int GetNumBits() const; + void SetNarrowRange(bool narrow_range); + void SetNumBits(int num_bits); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_FAKE_QUANT_WITH_MIN_MAX_VARS_H_ diff --git a/mindspore/lite/c_ops/fill.cc b/mindspore/lite/c_ops/fill.cc new file mode 100644 index 0000000000..7183bf622e --- /dev/null +++ b/mindspore/lite/c_ops/fill.cc @@ -0,0 +1,56 @@ +/** + * 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 "c_ops/fill.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector Fill::GetDims() const { return this->primitive->value.AsFill()->dims; } + +void Fill::SetDims(const std::vector &dims) { this->primitive->value.AsFill()->dims = dims; } + +#else + +std::vector Fill::GetDims() const { + auto fb_vector = this->primitive->value_as_Fill()->dims(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void Fill::SetDims(const std::vector &dims) {} +#endif +int Fill::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + auto output = outputs_.front(); + if (input == nullptr || output == nullptr) { + MS_LOG(ERROR) << "Fill input or output is null!"; + return 1; + } + + if (inputs_.size() != kSingleNum || outputs_.size() != kSingleNum) { + MS_LOG(ERROR) << "input size: " << inputs_.size() << ", output size: " << outputs_.size(); + return 1; + } + + std::vector output_shape; + (void)output_shape.insert(output_shape.begin(), GetDims().begin(), GetDims().end()); + output->set_shape(output_shape); + output->set_data_type(input->data_type()); + output->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/fill.h b/mindspore/lite/c_ops/fill.h new file mode 100644 index 0000000000..5227e13dba --- /dev/null +++ b/mindspore/lite/c_ops/fill.h @@ -0,0 +1,45 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_FILL_H_ +#define LITE_MINDSPORE_LITE_C_OPS_FILL_H_ + +namespace mindspore { +class Fill : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Fill(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Fill(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + std::vector GetDims() const; + void SetDims(const std::vector &dims); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_FILL_H_ diff --git a/mindspore/lite/c_ops/flatten.cc b/mindspore/lite/c_ops/flatten.cc new file mode 100644 index 0000000000..ce24d2ef55 --- /dev/null +++ b/mindspore/lite/c_ops/flatten.cc @@ -0,0 +1,47 @@ +/** + * 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 "c_ops/flatten.h" + +namespace mindspore { +int Flatten::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + auto output = outputs_.front(); + if (input == nullptr || output == nullptr) { + MS_LOG(ERROR) << "Flatten input or output is null!"; + return 1; + } + + if (inputs_.size() != kSingleNum || outputs_.size() != kSingleNum) { + MS_LOG(ERROR) << "input size: " << inputs_.size() << ", output size: " << outputs_.size(); + return 1; + } + + auto input_shape = input->shape(); + std::vector output_shape(2); + output_shape[0] = input_shape[0]; + output_shape[1] = 1; + for (int i = 1; i < input_shape.size(); i++) { + output_shape[1] *= input_shape[i]; + } + output->set_shape(output_shape); + output->set_data_type(input->data_type()); + output->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/flatten.h b/mindspore/lite/c_ops/flatten.h new file mode 100644 index 0000000000..b373340198 --- /dev/null +++ b/mindspore/lite/c_ops/flatten.h @@ -0,0 +1,43 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_FLATTEN_H_ +#define LITE_MINDSPORE_LITE_C_OPS_FLATTEN_H_ + +namespace mindspore { +class Flatten : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Flatten(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Flatten(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_FLATTEN_H_ diff --git a/mindspore/lite/c_ops/floor.h b/mindspore/lite/c_ops/floor.h new file mode 100644 index 0000000000..daf59f7783 --- /dev/null +++ b/mindspore/lite/c_ops/floor.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic_self.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_FLOOR_H_ +#define LITE_MINDSPORE_LITE_C_OPS_FLOOR_H_ + +namespace mindspore { +class Floor : public ArithmeticSelf { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Floor(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} +#else + explicit Floor(schema::Primitive *primitive) : ArithmeticSelf(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_FLOOR_H_ diff --git a/mindspore/lite/c_ops/floor_div.h b/mindspore/lite/c_ops/floor_div.h new file mode 100644 index 0000000000..a73f5379d9 --- /dev/null +++ b/mindspore/lite/c_ops/floor_div.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_FLOOR_DIV_H_ +#define LITE_MINDSPORE_LITE_C_OPS_FLOOR_DIV_H_ + +namespace mindspore { +class FloorDiv : public Arithmetic { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit FloorDiv(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} +#else + explicit FloorDiv(schema::Primitive *primitive) : Arithmetic(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_FLOOR_DIV_H_ diff --git a/mindspore/lite/c_ops/floor_mod.h b/mindspore/lite/c_ops/floor_mod.h new file mode 100644 index 0000000000..c2b4f2236a --- /dev/null +++ b/mindspore/lite/c_ops/floor_mod.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_FLOOR_MOD_H_ +#define LITE_MINDSPORE_LITE_C_OPS_FLOOR_MOD_H_ + +namespace mindspore { +class FloorMod : public Arithmetic { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit FloorMod(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} +#else + explicit FloorMod(schema::Primitive *primitive) : Arithmetic(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_FLOOR_MOD_H_ diff --git a/mindspore/lite/c_ops/full_connection.cc b/mindspore/lite/c_ops/full_connection.cc new file mode 100644 index 0000000000..4a59469638 --- /dev/null +++ b/mindspore/lite/c_ops/full_connection.cc @@ -0,0 +1,80 @@ +/** + * 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 "c_ops/full_connection.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +bool FullConnection::GetHasBias() const { return this->primitive->value.AsFullConnection()->hasBias; } +int FullConnection::GetAxis() const { return this->primitive->value.AsFullConnection()->axis; } +bool FullConnection::GetUseAxis() const { return this->primitive->value.AsFullConnection()->useAxis; } + +void FullConnection::SetHasBias(bool has_bias) { this->primitive->value.AsFullConnection()->hasBias = has_bias; } +void FullConnection::SetAxis(int axis) { this->primitive->value.AsFullConnection()->axis = axis; } +void FullConnection::SetUseAxis(bool use_axis) { this->primitive->value.AsFullConnection()->useAxis = use_axis; } + +#else + +bool FullConnection::GetHasBias() const { return this->primitive->value_as_FullConnection()->hasBias(); } +int FullConnection::GetAxis() const { return this->primitive->value_as_FullConnection()->axis(); } +bool FullConnection::GetUseAxis() const { return this->primitive->value_as_FullConnection()->useAxis(); } + +void FullConnection::SetHasBias(bool has_bias) {} +void FullConnection::SetAxis(int axis) {} +void FullConnection::SetUseAxis(bool use_axis) {} +#endif +int FullConnection::InferShape(std::vector inputs_, + std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input0 = inputs_.front(); + MS_ASSERT(input0 != nullptr); + auto input1 = inputs_.at(1); + MS_ASSERT(input1 != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + + if ((GetHasBias() && inputs_.size() != kMultiNum) || (!GetHasBias() && inputs_.size() != kDoubleNum)) { + MS_LOG(ERROR) << "Input tensors num error"; + return 1; + } + if (GetAxis() < 1 || GetAxis() > input0->shape().size()) { + MS_LOG(ERROR) << "FullConnection axis invalid"; + return 1; + } + int new_k = 1; + for (size_t i = GetAxis(); i < input0->shape().size(); ++i) { + new_k *= input0->shape().at(i); + } + if (new_k != input1->shape().at(1)) { + MS_LOG(ERROR) << "Input1 size invalid"; + return 1; + } + if (GetHasBias()) { + if (inputs_.at(2)->shape()[0] != input1->shape()[0]) { + MS_LOG(ERROR) << "bias size invalid"; + return 1; + } + } + std::vector out_shape{inputs_[0]->shape()}; + out_shape.resize(GetAxis() + 1); + out_shape[GetAxis()] = input1->shape()[0]; + output->set_shape(out_shape); + output->set_data_type(input0->data_type()); + output->SetFormat(input0->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/full_connection.h b/mindspore/lite/c_ops/full_connection.h new file mode 100644 index 0000000000..9d3732988d --- /dev/null +++ b/mindspore/lite/c_ops/full_connection.h @@ -0,0 +1,49 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_FULL_CONNECTION_H_ +#define LITE_MINDSPORE_LITE_C_OPS_FULL_CONNECTION_H_ + +namespace mindspore { +class FullConnection : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit FullConnection(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit FullConnection(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + bool GetHasBias() const; + int GetAxis() const; + bool GetUseAxis() const; + void SetHasBias(bool has_bias); + void SetAxis(int axis); + void SetUseAxis(bool use_axis); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_FULL_CONNECTION_H_ diff --git a/mindspore/lite/c_ops/fused_batchnorm.cc b/mindspore/lite/c_ops/fused_batchnorm.cc new file mode 100644 index 0000000000..c8fce2de78 --- /dev/null +++ b/mindspore/lite/c_ops/fused_batchnorm.cc @@ -0,0 +1,39 @@ +/** + * 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 "c_ops/fused_batchnorm.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +float FusedBatchNorm::GetEpsilon() const { return this->primitive->value.AsFusedBatchNorm()->epsilon; } +float FusedBatchNorm::GetMomentum() const { return this->primitive->value.AsFusedBatchNorm()->momentum; } +int FusedBatchNorm::GetSpatial() const { return this->primitive->value.AsFusedBatchNorm()->spatial; } + +void FusedBatchNorm::SetEpsilon(float epsilon) { this->primitive->value.AsFusedBatchNorm()->epsilon = epsilon; } +void FusedBatchNorm::SetMomentum(float momentum) { this->primitive->value.AsFusedBatchNorm()->momentum = momentum; } +void FusedBatchNorm::SetSpatial(int spatial) { this->primitive->value.AsFusedBatchNorm()->spatial = spatial; } + +#else + +float FusedBatchNorm::GetEpsilon() const { return this->primitive->value_as_FusedBatchNorm()->epsilon(); } +float FusedBatchNorm::GetMomentum() const { return this->primitive->value_as_FusedBatchNorm()->momentum(); } +int FusedBatchNorm::GetSpatial() const { return this->primitive->value_as_FusedBatchNorm()->spatial(); } + +void FusedBatchNorm::SetEpsilon(float epsilon) {} +void FusedBatchNorm::SetMomentum(float momentum) {} +void FusedBatchNorm::SetSpatial(int spatial) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/fused_batchnorm.h b/mindspore/lite/c_ops/fused_batchnorm.h new file mode 100644 index 0000000000..6614e2a43f --- /dev/null +++ b/mindspore/lite/c_ops/fused_batchnorm.h @@ -0,0 +1,48 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_FUSED_BATCH_NORM_H_ +#define LITE_MINDSPORE_LITE_C_OPS_FUSED_BATCH_NORM_H_ + +namespace mindspore { +class FusedBatchNorm : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit FusedBatchNorm(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit FusedBatchNorm(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + float GetEpsilon() const; + float GetMomentum() const; + int GetSpatial() const; + void SetEpsilon(float epsilon); + void SetMomentum(float momentum); + void SetSpatial(int spatial); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_FUSED_BATCH_NORM_H_ diff --git a/mindspore/lite/c_ops/gather.cc b/mindspore/lite/c_ops/gather.cc new file mode 100644 index 0000000000..2e23c9646b --- /dev/null +++ b/mindspore/lite/c_ops/gather.cc @@ -0,0 +1,87 @@ +/** + * 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 "c_ops/gather.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Gather::GetAxis() const { return this->primitive->value.AsGather()->axis; } +int Gather::GetBatchDims() const { return this->primitive->value.AsGather()->batchDims; } + +void Gather::SetAxis(int axis) { this->primitive->value.AsGather()->axis = axis; } +void Gather::SetBatchDims(int batch_dims) { this->primitive->value.AsGather()->batchDims = batch_dims; } + +#else + +int Gather::GetAxis() const { return this->primitive->value_as_Gather()->axis(); } +int Gather::GetBatchDims() const { return this->primitive->value_as_Gather()->batchDims(); } + +void Gather::SetAxis(int axis) {} +void Gather::SetBatchDims(int batch_dims) {} +#endif +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 1; + } + if (outputs_.size() != kSingleNum) { + MS_LOG(ERROR) << "Gather should have one outputs"; + return 1; + } + + auto input = inputs_.at(0); + MS_ASSERT(input != nullptr); + auto indices = inputs_.at(1); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(input != nullptr); + + int axis = GetAxis(); + int batch_dims = GetBatchDims(); + if (axis < 0) { + axis += input->shape().size(); + } + auto indices_shape = indices->shape(); + int indices_rank = indices_shape.size(); + if (indices_rank < batch_dims + 1) { + MS_LOG(ERROR) << "input[1]'s rank is less than batchDim + 1"; + return 1; + } + if (batch_dims != 0) { + MS_LOG(ERROR) << "batchDims " << batch_dims << " != 0, which is not support"; + return 1; + } + auto in_shape = input->shape(); + int in_rank = in_shape.size(); + if (in_rank < axis + 1) { + MS_LOG(ERROR) << "input[0]'s rank is less than axis + 1"; + return 1; + } + + std::vector out_shape{in_shape}; + out_shape.erase(out_shape.begin() + axis); + for (size_t i = 0; i < indices_rank; i++) { + out_shape.insert(out_shape.begin() + axis, indices_shape[i]); + } + + output->set_shape(out_shape); + output->set_data_type(input->data_type()); + output->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/gather.h b/mindspore/lite/c_ops/gather.h new file mode 100644 index 0000000000..67126fca54 --- /dev/null +++ b/mindspore/lite/c_ops/gather.h @@ -0,0 +1,47 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_GATHER_H_ +#define LITE_MINDSPORE_LITE_C_OPS_GATHER_H_ + +namespace mindspore { +class Gather : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Gather(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Gather(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetAxis() const; + int GetBatchDims() const; + void SetAxis(int axis); + void SetBatchDims(int batch_dims); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_GATHER_H_ diff --git a/mindspore/lite/c_ops/gather_nd.cc b/mindspore/lite/c_ops/gather_nd.cc new file mode 100644 index 0000000000..476b88170d --- /dev/null +++ b/mindspore/lite/c_ops/gather_nd.cc @@ -0,0 +1,74 @@ +/** + * 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 "c_ops/gather_nd.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int GatherNd::GetBatchDims() const { return this->primitive->value.AsGatherNd()->batchDims; } + +void GatherNd::SetBatchDims(int batch_dims) { this->primitive->value.AsGatherNd()->batchDims = batch_dims; } + +#else + +int GatherNd::GetBatchDims() const { return this->primitive->value_as_GatherNd()->batchDims(); } + +void GatherNd::SetBatchDims(int batch_dims) {} +#endif +int GatherNd::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + if (inputs_.size() != kDoubleNum) { + MS_LOG(ERROR) << "GatherNd should have two inputs"; + return 1; + } + if (outputs_.size() != kSingleNum) { + MS_LOG(ERROR) << "GatherNd should have one outputs"; + return 1; + } + + auto input = inputs_.at(0); + MS_ASSERT(input != nullptr); + auto indices = inputs_.at(1); + MS_ASSERT(indices != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + + auto in_shape = input->shape(); + int in_rank = in_shape.size(); + auto indices_shape = indices->shape(); + int indices_rank = indices_shape.size(); + + if (indices_shape[indices_rank - 1] > in_rank) { + MS_LOG(ERROR) << "Input of indices data is error!"; + return 1; + } + + std::vector out_shape; + int i = 0; + for (i = 0; i < indices_rank - 1; ++i) { + out_shape.emplace_back(indices_shape[i]); + } + for (i = indices_shape[indices_rank - 1]; i < in_rank; ++i) { + out_shape.emplace_back(in_shape[i]); + } + + output->set_shape(out_shape); + output->set_data_type(input->data_type()); + output->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/gather_nd.h b/mindspore/lite/c_ops/gather_nd.h new file mode 100644 index 0000000000..012b1c695d --- /dev/null +++ b/mindspore/lite/c_ops/gather_nd.h @@ -0,0 +1,45 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_GATHER_ND_H_ +#define LITE_MINDSPORE_LITE_C_OPS_GATHER_ND_H_ + +namespace mindspore { +class GatherNd : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit GatherNd(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit GatherNd(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetBatchDims() const; + void SetBatchDims(int batch_dims); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_GATHER_ND_H_ diff --git a/mindspore/lite/c_ops/greater.h b/mindspore/lite/c_ops/greater.h new file mode 100644 index 0000000000..e8890dd219 --- /dev/null +++ b/mindspore/lite/c_ops/greater.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_GREATER_H_ +#define LITE_MINDSPORE_LITE_C_OPS_GREATER_H_ + +namespace mindspore { +class Greater : public Arithmetic { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Greater(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} +#else + explicit Greater(schema::Primitive *primitive) : Arithmetic(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_GREATER_H_ diff --git a/mindspore/lite/c_ops/greater_equal.h b/mindspore/lite/c_ops/greater_equal.h new file mode 100644 index 0000000000..3fa7b95a21 --- /dev/null +++ b/mindspore/lite/c_ops/greater_equal.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_GREATER_EQUAL_H_ +#define LITE_MINDSPORE_LITE_C_OPS_GREATER_EQUAL_H_ + +namespace mindspore { +class GreaterEqual : public Arithmetic { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit GreaterEqual(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} +#else + explicit GreaterEqual(schema::Primitive *primitive) : Arithmetic(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_GREATER_EQUAL_H_ diff --git a/mindspore/lite/c_ops/l2_norm.cc b/mindspore/lite/c_ops/l2_norm.cc new file mode 100644 index 0000000000..163f74ad26 --- /dev/null +++ b/mindspore/lite/c_ops/l2_norm.cc @@ -0,0 +1,38 @@ +/** + * 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 "c_ops/l2_norm.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector L2Norm::GetAxis() const { return this->primitive->value.AsL2Norm()->axis; } +float L2Norm::GetEpsilon() const { return this->primitive->value.AsL2Norm()->epsilon; } + +void L2Norm::SetAxis(const std::vector &axis) { this->primitive->value.AsL2Norm()->axis = axis; } +void L2Norm::SetEpsilon(float epsilon) { this->primitive->value.AsL2Norm()->epsilon = epsilon; } + +#else + +std::vector L2Norm::GetAxis() const { + auto fb_vector = this->primitive->value_as_L2Norm()->axis(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +float L2Norm::GetEpsilon() const { return this->primitive->value_as_L2Norm()->epsilon(); } + +void L2Norm::SetAxis(const std::vector &axis) {} +void L2Norm::SetEpsilon(float epsilon) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/l2_norm.h b/mindspore/lite/c_ops/l2_norm.h new file mode 100644 index 0000000000..2ca793e686 --- /dev/null +++ b/mindspore/lite/c_ops/l2_norm.h @@ -0,0 +1,46 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_L2_NORM_H_ +#define LITE_MINDSPORE_LITE_C_OPS_L2_NORM_H_ + +namespace mindspore { +class L2Norm : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit L2Norm(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit L2Norm(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + std::vector GetAxis() const; + float GetEpsilon() const; + void SetAxis(const std::vector &axis); + void SetEpsilon(float epsilon); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_L2_NORM_H_ diff --git a/mindspore/lite/c_ops/leaky_relu.cc b/mindspore/lite/c_ops/leaky_relu.cc new file mode 100644 index 0000000000..b339ece178 --- /dev/null +++ b/mindspore/lite/c_ops/leaky_relu.cc @@ -0,0 +1,33 @@ +/** + * 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 "c_ops/leaky_relu.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +float LeakyReLU::GetNegativeSlope() const { return this->primitive->value.AsLeakyReLU()->negativeSlope; } + +void LeakyReLU::SetNegativeSlope(float negative_slope) { + this->primitive->value.AsLeakyReLU()->negativeSlope = negative_slope; +} + +#else + +float LeakyReLU::GetNegativeSlope() const { return this->primitive->value_as_LeakyReLU()->negativeSlope(); } + +void LeakyReLU::SetNegativeSlope(float negative_slope) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/leaky_relu.h b/mindspore/lite/c_ops/leaky_relu.h new file mode 100644 index 0000000000..155dfc2753 --- /dev/null +++ b/mindspore/lite/c_ops/leaky_relu.h @@ -0,0 +1,44 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_LEAKY_RE_L_U_H_ +#define LITE_MINDSPORE_LITE_C_OPS_LEAKY_RE_L_U_H_ + +namespace mindspore { +class LeakyReLU : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit LeakyReLU(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit LeakyReLU(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + float GetNegativeSlope() const; + void SetNegativeSlope(float negative_slope); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_LEAKY_RE_L_U_H_ diff --git a/mindspore/lite/c_ops/less.h b/mindspore/lite/c_ops/less.h new file mode 100644 index 0000000000..06a4cf6e1e --- /dev/null +++ b/mindspore/lite/c_ops/less.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_LESS_H_ +#define LITE_MINDSPORE_LITE_C_OPS_LESS_H_ + +namespace mindspore { +class Less : public Arithmetic { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Less(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} +#else + explicit Less(schema::Primitive *primitive) : Arithmetic(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_LESS_H_ diff --git a/mindspore/lite/c_ops/less_equal.h b/mindspore/lite/c_ops/less_equal.h new file mode 100644 index 0000000000..78c7170956 --- /dev/null +++ b/mindspore/lite/c_ops/less_equal.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_LESS_EQUAL_H_ +#define LITE_MINDSPORE_LITE_C_OPS_LESS_EQUAL_H_ + +namespace mindspore { +class LessEqual : public Arithmetic { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit LessEqual(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} +#else + explicit LessEqual(schema::Primitive *primitive) : Arithmetic(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_LESS_EQUAL_H_ diff --git a/mindspore/lite/c_ops/local_response_normalization.cc b/mindspore/lite/c_ops/local_response_normalization.cc new file mode 100644 index 0000000000..65456450c9 --- /dev/null +++ b/mindspore/lite/c_ops/local_response_normalization.cc @@ -0,0 +1,67 @@ +/** + * 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 "c_ops/local_response_normalization.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int LocalResponseNormalization::GetDepthRadius() const { + return this->primitive->value.AsLocalResponseNormalization()->depth_radius; +} +float LocalResponseNormalization::GetBias() const { + return this->primitive->value.AsLocalResponseNormalization()->bias; +} +float LocalResponseNormalization::GetAlpha() const { + return this->primitive->value.AsLocalResponseNormalization()->alpha; +} +float LocalResponseNormalization::GetBeta() const { + return this->primitive->value.AsLocalResponseNormalization()->beta; +} + +void LocalResponseNormalization::SetDepthRadius(int depth_radius) { + this->primitive->value.AsLocalResponseNormalization()->depth_radius = depth_radius; +} +void LocalResponseNormalization::SetBias(float bias) { + this->primitive->value.AsLocalResponseNormalization()->bias = bias; +} +void LocalResponseNormalization::SetAlpha(float alpha) { + this->primitive->value.AsLocalResponseNormalization()->alpha = alpha; +} +void LocalResponseNormalization::SetBeta(float beta) { + this->primitive->value.AsLocalResponseNormalization()->beta = beta; +} + +#else + +int LocalResponseNormalization::GetDepthRadius() const { + return this->primitive->value_as_LocalResponseNormalization()->depth_radius(); +} +float LocalResponseNormalization::GetBias() const { + return this->primitive->value_as_LocalResponseNormalization()->bias(); +} +float LocalResponseNormalization::GetAlpha() const { + return this->primitive->value_as_LocalResponseNormalization()->alpha(); +} +float LocalResponseNormalization::GetBeta() const { + return this->primitive->value_as_LocalResponseNormalization()->beta(); +} + +void LocalResponseNormalization::SetDepthRadius(int depth_radius) {} +void LocalResponseNormalization::SetBias(float bias) {} +void LocalResponseNormalization::SetAlpha(float alpha) {} +void LocalResponseNormalization::SetBeta(float beta) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/local_response_normalization.h b/mindspore/lite/c_ops/local_response_normalization.h new file mode 100644 index 0000000000..30a28c2857 --- /dev/null +++ b/mindspore/lite/c_ops/local_response_normalization.h @@ -0,0 +1,50 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_LOCAL_RESPONSE_NORMALIZATION_H_ +#define LITE_MINDSPORE_LITE_C_OPS_LOCAL_RESPONSE_NORMALIZATION_H_ + +namespace mindspore { +class LocalResponseNormalization : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit LocalResponseNormalization(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit LocalResponseNormalization(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int GetDepthRadius() const; + float GetBias() const; + float GetAlpha() const; + float GetBeta() const; + void SetDepthRadius(int depth_radius); + void SetBias(float bias); + void SetAlpha(float alpha); + void SetBeta(float beta); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_LOCAL_RESPONSE_NORMALIZATION_H_ diff --git a/mindspore/lite/c_ops/log.h b/mindspore/lite/c_ops/log.h new file mode 100644 index 0000000000..52b92764ba --- /dev/null +++ b/mindspore/lite/c_ops/log.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic_self.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_LOG_H_ +#define LITE_MINDSPORE_LITE_C_OPS_LOG_H_ + +namespace mindspore { +class Log : public ArithmeticSelf { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Log(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} +#else + explicit Log(schema::Primitive *primitive) : ArithmeticSelf(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_LOG_H_ diff --git a/mindspore/lite/c_ops/logical_and.h b/mindspore/lite/c_ops/logical_and.h new file mode 100644 index 0000000000..cab8648157 --- /dev/null +++ b/mindspore/lite/c_ops/logical_and.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_LOGICAL_AND_H_ +#define LITE_MINDSPORE_LITE_C_OPS_LOGICAL_AND_H_ + +namespace mindspore { +class LogicalAnd : public Arithmetic { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit LogicalAnd(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} +#else + explicit LogicalAnd(schema::Primitive *primitive) : Arithmetic(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_LOGICAL_AND_H_ diff --git a/mindspore/lite/c_ops/logical_not.h b/mindspore/lite/c_ops/logical_not.h new file mode 100644 index 0000000000..16a8a5d702 --- /dev/null +++ b/mindspore/lite/c_ops/logical_not.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic_self.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_LOGICAL_NOT_H_ +#define LITE_MINDSPORE_LITE_C_OPS_LOGICAL_NOT_H_ + +namespace mindspore { +class LogicalNot : public ArithmeticSelf { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit LogicalNot(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} +#else + explicit LogicalNot(schema::Primitive *primitive) : ArithmeticSelf(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_LOGICAL_NOT_H_ diff --git a/mindspore/lite/c_ops/logical_or.h b/mindspore/lite/c_ops/logical_or.h new file mode 100644 index 0000000000..ab34a7fb39 --- /dev/null +++ b/mindspore/lite/c_ops/logical_or.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_LOGICAL_OR_H_ +#define LITE_MINDSPORE_LITE_C_OPS_LOGICAL_OR_H_ + +namespace mindspore { +class LogicalOr : public Arithmetic { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit LogicalOr(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} +#else + explicit LogicalOr(schema::Primitive *primitive) : Arithmetic(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_LOGICAL_OR_H_ diff --git a/mindspore/lite/c_ops/lrn.cc b/mindspore/lite/c_ops/lrn.cc new file mode 100644 index 0000000000..04b00c065a --- /dev/null +++ b/mindspore/lite/c_ops/lrn.cc @@ -0,0 +1,43 @@ +/** + * 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 "c_ops/lrn.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +float Lrn::GetAlpha() const { return this->primitive->value.AsLrn()->alpha; } +float Lrn::GetBeta() const { return this->primitive->value.AsLrn()->beta; } +float Lrn::GetBias() const { return this->primitive->value.AsLrn()->bias; } +int Lrn::GetSize() const { return this->primitive->value.AsLrn()->size; } + +void Lrn::SetAlpha(float alpha) { this->primitive->value.AsLrn()->alpha = alpha; } +void Lrn::SetBeta(float beta) { this->primitive->value.AsLrn()->beta = beta; } +void Lrn::SetBias(float bias) { this->primitive->value.AsLrn()->bias = bias; } +void Lrn::SetSize(int size) { this->primitive->value.AsLrn()->size = size; } + +#else + +float Lrn::GetAlpha() const { return this->primitive->value_as_Lrn()->alpha(); } +float Lrn::GetBeta() const { return this->primitive->value_as_Lrn()->beta(); } +float Lrn::GetBias() const { return this->primitive->value_as_Lrn()->bias(); } +int Lrn::GetSize() const { return this->primitive->value_as_Lrn()->size(); } + +void Lrn::SetAlpha(float alpha) {} +void Lrn::SetBeta(float beta) {} +void Lrn::SetBias(float bias) {} +void Lrn::SetSize(int size) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/lrn.h b/mindspore/lite/c_ops/lrn.h new file mode 100644 index 0000000000..ed61bc86bd --- /dev/null +++ b/mindspore/lite/c_ops/lrn.h @@ -0,0 +1,50 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_LRN_H_ +#define LITE_MINDSPORE_LITE_C_OPS_LRN_H_ + +namespace mindspore { +class Lrn : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Lrn(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Lrn(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + float GetAlpha() const; + float GetBeta() const; + float GetBias() const; + int GetSize() const; + void SetAlpha(float alpha); + void SetBeta(float beta); + void SetBias(float bias); + void SetSize(int size); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_LRN_H_ diff --git a/mindspore/lite/c_ops/lstm.cc b/mindspore/lite/c_ops/lstm.cc new file mode 100644 index 0000000000..e9e08a1b88 --- /dev/null +++ b/mindspore/lite/c_ops/lstm.cc @@ -0,0 +1,77 @@ +/** + * 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 "c_ops/lstm.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +bool Lstm::GetBidirection() const { return this->primitive->value.AsLstm()->bidirection; } + +void Lstm::SetBidirection(bool bidirection) { this->primitive->value.AsLstm()->bidirection = bidirection; } + +#else + +bool Lstm::GetBidirection() const { return this->primitive->value_as_Lstm()->bidirection(); } + +void Lstm::SetBidirection(bool bidirection) {} +#endif + +const int kLstmInputNum = 6; +const int kLstmOutputNum = 3; +int Lstm::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + if (inputs_.size() != kLstmInputNum || outputs_.size() != kLstmOutputNum) { + MS_LOG(ERROR) << "OpLstm inputs or outputs size error."; + return 1; + } + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto weight_i = inputs_.front(); + MS_ASSERT(input0 != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + + std::vector in_shape = input->shape(); + std::vector w_shape = weight_i->shape(); // layer, hidden_size * 4, input_size + if (in_shape.size() != 3 || w_shape.size() != 3) { + MS_LOG(ERROR) << "OpLstm input dims should be 3."; + return 1; + } + + int hidden_size = w_shape[1] / 4; + + // set output + std::vector out_shape(in_shape); + out_shape[2] = hidden_size; + if (GetBidirection()) { + out_shape.insert(out_shape.begin() + 1, 2); + } + output->set_shape(out_shape); + + // set hidden state, cell state + std::vector state_shape(in_shape); + state_shape[0] = GetBidirection() ? 2 : 1; + state_shape[2] = hidden_size; + outputs_[1]->set_shape(state_shape); + outputs_[2]->set_shape(state_shape); + + for (int i = 0; i < kLstmOutputNum; i++) { + outputs_[i]->set_data_type(input->data_type()); + outputs_[i]->SetFormat(input->GetFormat()); + } + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/lstm.h b/mindspore/lite/c_ops/lstm.h new file mode 100644 index 0000000000..0d8b8678cd --- /dev/null +++ b/mindspore/lite/c_ops/lstm.h @@ -0,0 +1,45 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_LSTM_H_ +#define LITE_MINDSPORE_LITE_C_OPS_LSTM_H_ + +namespace mindspore { +class Lstm : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Lstm(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Lstm(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + bool GetBidirection() const; + void SetBidirection(bool bidirection); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_LSTM_H_ diff --git a/mindspore/lite/c_ops/matmul.cc b/mindspore/lite/c_ops/matmul.cc new file mode 100644 index 0000000000..3265315f40 --- /dev/null +++ b/mindspore/lite/c_ops/matmul.cc @@ -0,0 +1,77 @@ +/** + * 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 "c_ops/matmul.h" +#include + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +bool MatMul::GetTransposeA() const { return this->primitive->value.AsMatMul()->transposeA; } +bool MatMul::GetTransposeB() const { return this->primitive->value.AsMatMul()->transposeB; } + +void MatMul::SetTransposeA(bool transpose_a) { this->primitive->value.AsMatMul()->transposeA = transpose_a; } +void MatMul::SetTransposeB(bool transpose_b) { this->primitive->value.AsMatMul()->transposeB = transpose_b; } + +#else + +bool MatMul::GetTransposeA() const { return this->primitive->value_as_MatMul()->transposeA(); } +bool MatMul::GetTransposeB() const { return this->primitive->value_as_MatMul()->transposeB(); } + +void MatMul::SetTransposeA(bool transpose_a) {} +void MatMul::SetTransposeB(bool transpose_b) {} +#endif +int MatMul::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + if (inputs_.size() != kDoubleNum) { + MS_LOG(ERROR) << "OpMatMul inputs size: " << inputs_.size(); + return 1; + } + auto input0 = inputs_.front(); + MS_ASSERT(input0 != nullptr); + auto input1 = inputs_.at(1); + MS_ASSERT(input1 != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + + std::vector a_shape = input0->shape(); + std::vector b_shape = input1->shape(); + if (a_shape.size() < 2 || b_shape.size() < 2) { + MS_LOG(ERROR) << "inputs shape is invalid"; + return 1; + } + + for (int i = 0; i < a_shape.size() - 2; ++i) { + if (a_shape[i] != b_shape[i]) { + MS_LOG(ERROR) << "Op MatMul's dimensions must be equal"; + return 1; + } + } + + if (GetTransposeA()) { + std::swap(a_shape[a_shape.size() - 1], a_shape[a_shape.size() - 2]); + } + if (GetTransposeB()) { + std::swap(b_shape[b_shape.size() - 1], b_shape[b_shape.size() - 2]); + } + std::vector c_shape(a_shape); + c_shape[c_shape.size() - 1] = b_shape[b_shape.size() - 1]; + output->set_shape(c_shape); + output->set_data_type(input0->data_type()); + output->SetFormat(input0->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/matmul.h b/mindspore/lite/c_ops/matmul.h new file mode 100644 index 0000000000..08e8cdece3 --- /dev/null +++ b/mindspore/lite/c_ops/matmul.h @@ -0,0 +1,47 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_MAT_MUL_H_ +#define LITE_MINDSPORE_LITE_C_OPS_MAT_MUL_H_ + +namespace mindspore { +class MatMul : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit MatMul(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit MatMul(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + bool GetTransposeA() const; + bool GetTransposeB() const; + void SetTransposeA(bool transpose_a); + void SetTransposeB(bool transpose_b); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_MAT_MUL_H_ diff --git a/mindspore/lite/c_ops/matrix_diag.cc b/mindspore/lite/c_ops/matrix_diag.cc new file mode 100644 index 0000000000..3b6772ae0e --- /dev/null +++ b/mindspore/lite/c_ops/matrix_diag.cc @@ -0,0 +1,45 @@ +/** + * 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 "c_ops/matrix_diag.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int MatrixDiag::GetK() const { return this->primitive->value.AsMatrixDiag()->k; } +int MatrixDiag::GetNumRows() const { return this->primitive->value.AsMatrixDiag()->numRows; } +int MatrixDiag::GetNumCols() const { return this->primitive->value.AsMatrixDiag()->numCols; } +float MatrixDiag::GetPaddingValue() const { return this->primitive->value.AsMatrixDiag()->paddingValue; } + +void MatrixDiag::SetK(int k) { this->primitive->value.AsMatrixDiag()->k = k; } +void MatrixDiag::SetNumRows(int num_rows) { this->primitive->value.AsMatrixDiag()->numRows = num_rows; } +void MatrixDiag::SetNumCols(int num_cols) { this->primitive->value.AsMatrixDiag()->numCols = num_cols; } +void MatrixDiag::SetPaddingValue(float padding_value) { + this->primitive->value.AsMatrixDiag()->paddingValue = padding_value; +} + +#else + +int MatrixDiag::GetK() const { return this->primitive->value_as_MatrixDiag()->k(); } +int MatrixDiag::GetNumRows() const { return this->primitive->value_as_MatrixDiag()->numRows(); } +int MatrixDiag::GetNumCols() const { return this->primitive->value_as_MatrixDiag()->numCols(); } +float MatrixDiag::GetPaddingValue() const { return this->primitive->value_as_MatrixDiag()->paddingValue(); } + +void MatrixDiag::SetK(int k) {} +void MatrixDiag::SetNumRows(int num_rows) {} +void MatrixDiag::SetNumCols(int num_cols) {} +void MatrixDiag::SetPaddingValue(float padding_value) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/matrix_diag.h b/mindspore/lite/c_ops/matrix_diag.h new file mode 100644 index 0000000000..1ef2fcf7d1 --- /dev/null +++ b/mindspore/lite/c_ops/matrix_diag.h @@ -0,0 +1,50 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_MATRIX_DIAG_H_ +#define LITE_MINDSPORE_LITE_C_OPS_MATRIX_DIAG_H_ + +namespace mindspore { +class MatrixDiag : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit MatrixDiag(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit MatrixDiag(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int GetK() const; + int GetNumRows() const; + int GetNumCols() const; + float GetPaddingValue() const; + void SetK(int k); + void SetNumRows(int num_rows); + void SetNumCols(int num_cols); + void SetPaddingValue(float padding_value); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_MATRIX_DIAG_H_ diff --git a/mindspore/lite/c_ops/maximum.h b/mindspore/lite/c_ops/maximum.h new file mode 100644 index 0000000000..37ab2c3541 --- /dev/null +++ b/mindspore/lite/c_ops/maximum.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_MAXIMUM_H_ +#define LITE_MINDSPORE_LITE_C_OPS_MAXIMUM_H_ + +namespace mindspore { +class Maximum : public Arithmetic { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Maximum(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} +#else + explicit Maximum(schema::Primitive *primitive) : Arithmetic(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_MAXIMUM_H_ diff --git a/mindspore/lite/c_ops/mean.cc b/mindspore/lite/c_ops/mean.cc new file mode 100644 index 0000000000..ada5739a56 --- /dev/null +++ b/mindspore/lite/c_ops/mean.cc @@ -0,0 +1,94 @@ +/** + * 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 "c_ops/mean.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector Mean::GetAxis() const { return this->primitive->value.AsMean()->axis; } +bool Mean::GetKeepDims() const { return this->primitive->value.AsMean()->keepDims; } + +void Mean::SetAxis(const std::vector &axis) { this->primitive->value.AsMean()->axis = axis; } +void Mean::SetKeepDims(bool keep_dims) { this->primitive->value.AsMean()->keepDims = keep_dims; } + +#else + +std::vector Mean::GetAxis() const { + auto fb_vector = this->primitive->value_as_Mean()->axis(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +bool Mean::GetKeepDims() const { return this->primitive->value_as_Mean()->keepDims(); } + +void Mean::SetAxis(const std::vector &axis) {} +void Mean::SetKeepDims(bool keep_dims) {} +#endif +namespace { +constexpr size_t kInputSize = 1; +constexpr size_t kOutputSize = 1; +} // namespace +int Mean::InferShape(std::vector inputs_, std::vector outputs_) { + if (inputs_.size() != kInputSize || outputs_.size() != kOutputSize) { + return 1; + } + auto input = inputs_.front(); + auto output = outputs_.front(); + if (input == nullptr || output == nullptr) { + return 1; + } + if (this->primitive == nullptr) { + return 1; + } + + bool keep_dims = static_cast(GetKeepDims()); + std::vector in_shape = input->shape(); + std::vector out_shape; + const auto &axes = GetAxis(); + auto num_axes = axes.size(); + // reduce on all axes + if (num_axes == 0) { + if (keep_dims) { + for (auto i = 0; i < in_shape.size(); i++) { + out_shape.push_back(1); + } + } + output->set_shape(out_shape); + output->set_data_type(input->data_type()); + return 0; + } + + // reduce on selected axes + for (size_t i = 0; i < in_shape.size(); i++) { + bool reduce_axis = false; + for (int idx = 0; idx < num_axes; ++idx) { + if (static_cast(axes[idx]) == i) { + reduce_axis = true; + break; + } + } + if (reduce_axis) { + if (keep_dims) { + out_shape.push_back(1); + } + } else { + out_shape.push_back(in_shape[i]); + } + } + output->set_shape(out_shape); + output->set_data_type(input->data_type()); + output->SetFormat(input->GetFormat()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/mean.h b/mindspore/lite/c_ops/mean.h new file mode 100644 index 0000000000..56d6efdd4f --- /dev/null +++ b/mindspore/lite/c_ops/mean.h @@ -0,0 +1,47 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_MEAN_H_ +#define LITE_MINDSPORE_LITE_C_OPS_MEAN_H_ + +namespace mindspore { +class Mean : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Mean(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Mean(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + std::vector GetAxis() const; + bool GetKeepDims() const; + void SetAxis(const std::vector &axis); + void SetKeepDims(bool keep_dims); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_MEAN_H_ diff --git a/mindspore/lite/c_ops/minimum.h b/mindspore/lite/c_ops/minimum.h new file mode 100644 index 0000000000..c738273fc7 --- /dev/null +++ b/mindspore/lite/c_ops/minimum.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_MINIMUM_H_ +#define LITE_MINDSPORE_LITE_C_OPS_MINIMUM_H_ + +namespace mindspore { +class Minimum : public Arithmetic { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Minimum(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} +#else + explicit Minimum(schema::Primitive *primitive) : Arithmetic(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_MINIMUM_H_ diff --git a/mindspore/lite/c_ops/mul.cc b/mindspore/lite/c_ops/mul.cc new file mode 100644 index 0000000000..d26152db5c --- /dev/null +++ b/mindspore/lite/c_ops/mul.cc @@ -0,0 +1,31 @@ +/** + * 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 "c_ops/mul.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Mul::GetActivationType() const { return this->primitive->value.AsMul()->activationType; } + +void Mul::SetActivationType(int activation_type) { this->primitive->value.AsMul()->activationType = activation_type; } + +#else + +int Mul::GetActivationType() const { return this->primitive->value_as_Mul()->activationType(); } + +void Mul::SetActivationType(int activation_type) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/mul.h b/mindspore/lite/c_ops/mul.h new file mode 100644 index 0000000000..b73070d9da --- /dev/null +++ b/mindspore/lite/c_ops/mul.h @@ -0,0 +1,45 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#include "c_ops/arithmetic.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_MUL_H_ +#define LITE_MINDSPORE_LITE_C_OPS_MUL_H_ + +namespace mindspore { +class Mul : public Arithmetic { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Mul(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} +#else + explicit Mul(schema::Primitive *primitive) : Arithmetic(primitive) {} +#endif + int GetActivationType() const; + void SetActivationType(int activation_type); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_MUL_H_ diff --git a/mindspore/lite/c_ops/nchw2nhwc.cc b/mindspore/lite/c_ops/nchw2nhwc.cc new file mode 100644 index 0000000000..8732e76523 --- /dev/null +++ b/mindspore/lite/c_ops/nchw2nhwc.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 "c_ops/nchw2nhwc.h" + +namespace mindspore { +int Nchw2Nhwc::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + std::vector nchw_shape = input->shape(); + if (nchw_shape.size() != 4) { + output->set_shape(nchw_shape); + } else { + std::vector nhwc_shape{nchw_shape}; + nhwc_shape[NHWC_N] = nchw_shape[NCHW_N]; + nhwc_shape[NHWC_H] = nchw_shape[NCHW_H]; + nhwc_shape[NHWC_W] = nchw_shape[NCHW_W]; + nhwc_shape[NHWC_C] = nchw_shape[NCHW_C]; + output->set_shape(nhwc_shape); + } + output->SetFormat(schema::Format_NHWC); + output->set_data_type(input->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/nchw2nhwc.h b/mindspore/lite/c_ops/nchw2nhwc.h new file mode 100644 index 0000000000..dbe2be247b --- /dev/null +++ b/mindspore/lite/c_ops/nchw2nhwc.h @@ -0,0 +1,43 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_NCHW_2_NHWC_H_ +#define LITE_MINDSPORE_LITE_C_OPS_NCHW_2_NHWC_H_ + +namespace mindspore { +class Nchw2Nhwc : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Nchw2Nhwc(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Nchw2Nhwc(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_NCHW_2_NHWC_H_ diff --git a/mindspore/lite/c_ops/nhwc2nchw.cc b/mindspore/lite/c_ops/nhwc2nchw.cc new file mode 100644 index 0000000000..009e161b81 --- /dev/null +++ b/mindspore/lite/c_ops/nhwc2nchw.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 "c_ops/nhwc2nchw.h" + +namespace mindspore { +int Nhwc2Nchw::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + std::vector nhwc_shape = input->shape(); + if (nhwc_shape.size() != 4) { + output->set_shape(nhwc_shape); + } else { + std::vector nchw_shape{nhwc_shape}; + nchw_shape[NCHW_N] = nhwc_shape[NHWC_N]; + nchw_shape[NCHW_C] = nhwc_shape[NHWC_C]; + nchw_shape[NCHW_H] = nhwc_shape[NHWC_H]; + nchw_shape[NCHW_W] = nhwc_shape[NHWC_W]; + output->set_shape(nchw_shape); + } + output->SetFormat(schema::Format_NCHW); + output->set_data_type(input->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/nhwc2nchw.h b/mindspore/lite/c_ops/nhwc2nchw.h new file mode 100644 index 0000000000..55eafe7a70 --- /dev/null +++ b/mindspore/lite/c_ops/nhwc2nchw.h @@ -0,0 +1,43 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_NHWC_2_NCHW_H_ +#define LITE_MINDSPORE_LITE_C_OPS_NHWC_2_NCHW_H_ + +namespace mindspore { +class Nhwc2Nchw : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Nhwc2Nchw(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Nhwc2Nchw(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_NHWC_2_NCHW_H_ diff --git a/mindspore/lite/c_ops/not_equal.h b/mindspore/lite/c_ops/not_equal.h new file mode 100644 index 0000000000..818fb249dc --- /dev/null +++ b/mindspore/lite/c_ops/not_equal.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_NOT_EQUAL_H_ +#define LITE_MINDSPORE_LITE_C_OPS_NOT_EQUAL_H_ + +namespace mindspore { +class NotEqual : public Arithmetic { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit NotEqual(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} +#else + explicit NotEqual(schema::Primitive *primitive) : Arithmetic(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_NOT_EQUAL_H_ diff --git a/mindspore/lite/c_ops/one_hot.cc b/mindspore/lite/c_ops/one_hot.cc new file mode 100644 index 0000000000..2df75ac1d7 --- /dev/null +++ b/mindspore/lite/c_ops/one_hot.cc @@ -0,0 +1,79 @@ +/** + * 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 "c_ops/one_hot.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int OneHot::GetAxis() const { return this->primitive->value.AsOneHot()->axis; } + +void OneHot::SetAxis(int axis) { this->primitive->value.AsOneHot()->axis = axis; } + +#else + +int OneHot::GetAxis() const { return this->primitive->value_as_OneHot()->axis(); } + +void OneHot::SetAxis(int axis) {} +#endif +namespace { +constexpr size_t kOneHotInputNum = 4; +} +int OneHot::InferShape(std::vector inputs, std::vector outputs) { + if (this->primitive == nullptr) { + return 1; + } + + int axis = GetAxis(); + + // indices, depth, on_value, off_value + if (inputs.size() != kOneHotInputNum) { + MS_LOG(ERROR) << "OneHot got inputs num " << inputs.size() << ", should be " << kOneHotInputNum; + return 1; + } + auto depth_tensor = inputs.at(1); + if (depth_tensor == nullptr) { + return 1; + } + const int *depth = static_cast(depth_tensor->Data()); + + auto input = inputs.front(); + if (input == nullptr) { + return 1; + } + const auto input_shape = input->shape(); + int input_rank = static_cast(input_shape.size()); + if (axis < 0) { + axis += input_rank + 1; + } + std::vector output_shape(input_shape); + output_shape.insert(output_shape.cbegin() + axis, *depth); + + auto output = outputs.front(); + if (output == nullptr) { + return 1; + } + output->set_shape(output_shape); + + auto on_value = inputs.at(2); + if (on_value == nullptr) { + return 1; + } + output->set_data_type(on_value->data_type()); + output->SetFormat(on_value->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/one_hot.h b/mindspore/lite/c_ops/one_hot.h new file mode 100644 index 0000000000..b93b2230d8 --- /dev/null +++ b/mindspore/lite/c_ops/one_hot.h @@ -0,0 +1,45 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_ONE_HOT_H_ +#define LITE_MINDSPORE_LITE_C_OPS_ONE_HOT_H_ + +namespace mindspore { +class OneHot : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit OneHot(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit OneHot(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetAxis() const; + void SetAxis(int axis); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_ONE_HOT_H_ diff --git a/mindspore/lite/c_ops/pad.cc b/mindspore/lite/c_ops/pad.cc new file mode 100644 index 0000000000..240480e730 --- /dev/null +++ b/mindspore/lite/c_ops/pad.cc @@ -0,0 +1,76 @@ +/** + * 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 "c_ops/pad.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector Pad::GetPaddings() const { return this->primitive->value.AsPad()->paddings; } +int Pad::GetPaddingMode() const { return this->primitive->value.AsPad()->paddingMode; } +float Pad::GetConstantValue() const { return this->primitive->value.AsPad()->constantValue; } + +void Pad::SetPaddings(const std::vector &paddings) { this->primitive->value.AsPad()->paddings = paddings; } +void Pad::SetPaddingMode(int padding_mode) { this->primitive->value.AsPad()->paddingMode = padding_mode; } +void Pad::SetConstantValue(float constant_value) { this->primitive->value.AsPad()->constantValue = constant_value; } + +#else + +std::vector Pad::GetPaddings() const { + auto fb_vector = this->primitive->value_as_Pad()->paddings(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +int Pad::GetPaddingMode() const { return this->primitive->value_as_Pad()->paddingMode(); } +float Pad::GetConstantValue() const { return this->primitive->value_as_Pad()->constantValue(); } + +void Pad::SetPaddings(const std::vector &paddings) {} +void Pad::SetPaddingMode(int padding_mode) {} +void Pad::SetConstantValue(float constant_value) {} +#endif +namespace { +const size_t kPaddingsSize = 8; +const size_t kInputRank = 4; +} // namespace +int Pad::InferShape(std::vector inputs, std::vector outputs) { + MS_ASSERT(this->primitive != nullptr); + if (this->primitive == nullptr) { + return 1; + } + + auto paddings = GetPaddings(); + + auto input = inputs.front(); + if (input == nullptr) { + return 1; + } + auto input_shape = input->shape(); + std::vector output_shape; + MS_ASSERT(input->shape().size() <= kInputRank); + for (size_t i = 0; i < input_shape.size(); i++) { + auto paddings_index = i + kInputRank - input_shape.size(); + auto shape = input_shape[i] + (paddings)[2 * paddings_index] + (paddings)[2 * paddings_index + 1]; + output_shape.push_back(shape); + } + + auto output = outputs.front(); + if (output == nullptr) { + return 1; + } + output->SetFormat(input->GetFormat()); + output->set_shape(output_shape); + output->set_data_type(input->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/pad.h b/mindspore/lite/c_ops/pad.h new file mode 100644 index 0000000000..e0b1a54138 --- /dev/null +++ b/mindspore/lite/c_ops/pad.h @@ -0,0 +1,49 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_PAD_H_ +#define LITE_MINDSPORE_LITE_C_OPS_PAD_H_ + +namespace mindspore { +class Pad : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Pad(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Pad(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + std::vector GetPaddings() const; + int GetPaddingMode() const; + float GetConstantValue() const; + void SetPaddings(const std::vector &paddings); + void SetPaddingMode(int padding_mode); + void SetConstantValue(float constant_value); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_PAD_H_ diff --git a/mindspore/lite/c_ops/permute.cc b/mindspore/lite/c_ops/permute.cc new file mode 100644 index 0000000000..3578071fd0 --- /dev/null +++ b/mindspore/lite/c_ops/permute.cc @@ -0,0 +1,34 @@ +/** + * 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 "c_ops/permute.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector Permute::GetOrder() const { return this->primitive->value.AsPermute()->order; } + +void Permute::SetOrder(const std::vector &order) { this->primitive->value.AsPermute()->order = order; } + +#else + +std::vector Permute::GetOrder() const { + auto fb_vector = this->primitive->value_as_Permute()->order(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void Permute::SetOrder(const std::vector &order) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/permute.h b/mindspore/lite/c_ops/permute.h new file mode 100644 index 0000000000..0348a41d45 --- /dev/null +++ b/mindspore/lite/c_ops/permute.h @@ -0,0 +1,44 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_PERMUTE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_PERMUTE_H_ + +namespace mindspore { +class Permute : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Permute(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Permute(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + std::vector GetOrder() const; + void SetOrder(const std::vector &order); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_PERMUTE_H_ diff --git a/mindspore/lite/c_ops/pooling.cc b/mindspore/lite/c_ops/pooling.cc new file mode 100644 index 0000000000..48cb4afaf9 --- /dev/null +++ b/mindspore/lite/c_ops/pooling.cc @@ -0,0 +1,139 @@ +/** + * 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 "c_ops/pooling.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Pooling::GetFormat() const { return this->primitive->value.AsPooling()->format; } +int Pooling::GetPoolingMode() const { return this->primitive->value.AsPooling()->poolingMode; } +bool Pooling::GetGlobal() const { return this->primitive->value.AsPooling()->global; } +int Pooling::GetWindowW() const { return this->primitive->value.AsPooling()->windowW; } +int Pooling::GetWindowH() const { return this->primitive->value.AsPooling()->windowH; } +int Pooling::GetStrideW() const { return this->primitive->value.AsPooling()->strideW; } +int Pooling::GetStrideH() const { return this->primitive->value.AsPooling()->strideH; } +int Pooling::GetPadMode() const { return this->primitive->value.AsPooling()->padMode; } +int Pooling::GetPadUp() const { return this->primitive->value.AsPooling()->padUp; } +int Pooling::GetPadDown() const { return this->primitive->value.AsPooling()->padDown; } +int Pooling::GetPadLeft() const { return this->primitive->value.AsPooling()->padLeft; } +int Pooling::GetPadRight() const { return this->primitive->value.AsPooling()->padRight; } +int Pooling::GetRoundMode() const { return this->primitive->value.AsPooling()->roundMode; } + +void Pooling::SetFormat(int format) { this->primitive->value.AsPooling()->format = (schema::Format)format; } +void Pooling::SetPoolingMode(int pooling_mode) { + this->primitive->value.AsPooling()->poolingMode = (schema::PoolMode)pooling_mode; +} +void Pooling::SetGlobal(bool global) { this->primitive->value.AsPooling()->global = global; } +void Pooling::SetWindowW(int window_w) { this->primitive->value.AsPooling()->windowW = window_w; } +void Pooling::SetWindowH(int window_h) { this->primitive->value.AsPooling()->windowH = window_h; } +void Pooling::SetStrideW(int stride_w) { this->primitive->value.AsPooling()->strideW = stride_w; } +void Pooling::SetStrideH(int stride_h) { this->primitive->value.AsPooling()->strideH = stride_h; } +void Pooling::SetPadMode(int pad_mode) { this->primitive->value.AsPooling()->padMode = (schema::PadMode)pad_mode; } +void Pooling::SetPadUp(int pad_up) { this->primitive->value.AsPooling()->padUp = pad_up; } +void Pooling::SetPadDown(int pad_down) { this->primitive->value.AsPooling()->padDown = pad_down; } +void Pooling::SetPadLeft(int pad_left) { this->primitive->value.AsPooling()->padLeft = pad_left; } +void Pooling::SetPadRight(int pad_right) { this->primitive->value.AsPooling()->padRight = pad_right; } +void Pooling::SetRoundMode(int round_mode) { + this->primitive->value.AsPooling()->roundMode = (schema::RoundMode)round_mode; +} + +#else + +int Pooling::GetFormat() const { return this->primitive->value_as_Pooling()->format(); } +int Pooling::GetPoolingMode() const { return this->primitive->value_as_Pooling()->poolingMode(); } +bool Pooling::GetGlobal() const { return this->primitive->value_as_Pooling()->global(); } +int Pooling::GetWindowW() const { return this->primitive->value_as_Pooling()->windowW(); } +int Pooling::GetWindowH() const { return this->primitive->value_as_Pooling()->windowH(); } +int Pooling::GetStrideW() const { return this->primitive->value_as_Pooling()->strideW(); } +int Pooling::GetStrideH() const { return this->primitive->value_as_Pooling()->strideH(); } +int Pooling::GetPadMode() const { return this->primitive->value_as_Pooling()->padMode(); } +int Pooling::GetPadUp() const { return this->primitive->value_as_Pooling()->padUp(); } +int Pooling::GetPadDown() const { return this->primitive->value_as_Pooling()->padDown(); } +int Pooling::GetPadLeft() const { return this->primitive->value_as_Pooling()->padLeft(); } +int Pooling::GetPadRight() const { return this->primitive->value_as_Pooling()->padRight(); } +int Pooling::GetRoundMode() const { return this->primitive->value_as_Pooling()->roundMode(); } + +void Pooling::SetFormat(int format) {} +void Pooling::SetPoolingMode(int pooling_mode) {} +void Pooling::SetGlobal(bool global) {} +void Pooling::SetWindowW(int window_w) {} +void Pooling::SetWindowH(int window_h) {} +void Pooling::SetStrideW(int stride_w) {} +void Pooling::SetStrideH(int stride_h) {} +void Pooling::SetPadMode(int pad_mode) {} +void Pooling::SetPadUp(int pad_up) {} +void Pooling::SetPadDown(int pad_down) {} +void Pooling::SetPadLeft(int pad_left) {} +void Pooling::SetPadRight(int pad_right) {} +void Pooling::SetRoundMode(int round_mode) {} +#endif +int Pooling::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + int input_h = input->shape().at(1); + int input_w = input->shape().at(2); + + MS_ASSERT(pooling_prim != nullptr); + auto window_h = GetWindowH(); + auto window_w = GetWindowH(); + if (GetGlobal()) { + window_h = input_h; + window_w = input_w; + } + + int output_h = 0; + int output_w = 0; + pad_l_ = GetPadLeft(); + pad_u_ = GetPadUp(); + pad_d_ = GetPadDown(); + pad_r_ = GetPadRight(); + if ((schema::PadMode)GetPadMode() == schema::PadMode_SAME) { + output_w = std::ceil(static_cast(input_w) / static_cast(GetStrideW())); + output_h = std::ceil(static_cast(input_h) / static_cast(GetStrideH())); + auto pad_h_all = ((output_h - 1) * GetStrideH() + (window_h - 1) + 1 - input_h); + auto pad_w_all = ((output_w - 1) * GetStrideW() + (window_w - 1) + 1 - input_w); + pad_u_ = pad_h_all / 2; + pad_d_ = pad_h_all - pad_u_; + pad_l_ = pad_w_all / 2; + pad_r_ = pad_w_all - pad_l_; + } else { + auto round_mode = GetRoundMode(); + if (round_mode == schema::RoundMode_FLOOR) { + output_h = std::floor(static_cast(input_h + pad_u_ + pad_d_ - window_h) / GetStrideH()) + 1; + output_w = std::floor(static_cast(input_w + pad_l_ + pad_r_ - window_w) / GetStrideW()) + 1; + } else if (round_mode == schema::RoundMode_CEIL) { + output_h = std::ceil(static_cast(input_h + pad_u_ + pad_d_ - window_h) / GetStrideH()) + 1; + output_w = std::ceil(static_cast(input_w + pad_l_ + pad_r_ - window_w) / GetStrideW()) + 1; + } else { + MS_LOG(ERROR) << "unsupported round mode."; + } + } + + // todo: fmk type + auto input_shape = input->shape(); + input_shape.at(1) = output_h; + input_shape.at(2) = output_w; + output->set_shape(input_shape); + output->set_data_type(input->data_type()); + + // todo: temp fix + output->SetFormat(schema::Format_NHWC); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/pooling.h b/mindspore/lite/c_ops/pooling.h new file mode 100644 index 0000000000..587a0c61e7 --- /dev/null +++ b/mindspore/lite/c_ops/pooling.h @@ -0,0 +1,80 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_POOLING_H_ +#define LITE_MINDSPORE_LITE_C_OPS_POOLING_H_ + +namespace mindspore { +class Pooling : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Pooling(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Pooling(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetFormat() const; + int GetPoolingMode() const; + bool GetGlobal() const; + int GetWindowW() const; + int GetWindowH() const; + int GetStrideW() const; + int GetStrideH() const; + int GetPadMode() const; + int GetPadUp() const; + int GetPadDown() const; + int GetPadLeft() const; + int GetPadRight() const; + int GetRoundMode() const; + void SetFormat(int format); + void SetPoolingMode(int pooling_mode); + void SetGlobal(bool global); + void SetWindowW(int window_w); + void SetWindowH(int window_h); + void SetStrideW(int stride_w); + void SetStrideH(int stride_h); + void SetPadMode(int pad_mode); + void SetPadUp(int pad_up); + void SetPadDown(int pad_down); + void SetPadLeft(int pad_left); + void SetPadRight(int pad_right); + void SetRoundMode(int round_mode); + + int PadUp() const { return this->pad_u_; } + int PadDown() const { return this->pad_d_; } + int PadLeft() const { return this->pad_l_; } + int PadRight() const { return this->pad_r_; } + + protected: + int pad_u_ = 0; + int pad_d_ = 0; + int pad_l_ = 0; + int pad_r_ = 0; +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_POOLING_H_ diff --git a/mindspore/lite/c_ops/pooling_grad.cc b/mindspore/lite/c_ops/pooling_grad.cc new file mode 100644 index 0000000000..93cb55ec6b --- /dev/null +++ b/mindspore/lite/c_ops/pooling_grad.cc @@ -0,0 +1,85 @@ +/** + * 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 "c_ops/pooling_grad.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int PoolingGrad::GetFormat() const { return this->primitive->value.AsPoolingGrad()->format; } +int PoolingGrad::GetPoolingMode() const { return this->primitive->value.AsPoolingGrad()->poolingMode; } +bool PoolingGrad::GetGlobal() const { return this->primitive->value.AsPoolingGrad()->global; } +int PoolingGrad::GetWindowW() const { return this->primitive->value.AsPoolingGrad()->windowW; } +int PoolingGrad::GetWindowH() const { return this->primitive->value.AsPoolingGrad()->windowH; } +int PoolingGrad::GetStrideW() const { return this->primitive->value.AsPoolingGrad()->strideW; } +int PoolingGrad::GetStrideH() const { return this->primitive->value.AsPoolingGrad()->strideH; } +int PoolingGrad::GetPadMode() const { return this->primitive->value.AsPoolingGrad()->padMode; } +int PoolingGrad::GetPadUp() const { return this->primitive->value.AsPoolingGrad()->padUp; } +int PoolingGrad::GetPadDown() const { return this->primitive->value.AsPoolingGrad()->padDown; } +int PoolingGrad::GetPadLeft() const { return this->primitive->value.AsPoolingGrad()->padLeft; } +int PoolingGrad::GetPadRight() const { return this->primitive->value.AsPoolingGrad()->padRight; } +int PoolingGrad::GetRoundMode() const { return this->primitive->value.AsPoolingGrad()->roundMode; } + +void PoolingGrad::SetFormat(int format) { this->primitive->value.AsPoolingGrad()->format = (schema::Format)format; } +void PoolingGrad::SetPoolingMode(int pooling_mode) { + this->primitive->value.AsPoolingGrad()->poolingMode = (schema::PoolMode)pooling_mode; +} +void PoolingGrad::SetGlobal(bool global) { this->primitive->value.AsPoolingGrad()->global = global; } +void PoolingGrad::SetWindowW(int window_w) { this->primitive->value.AsPoolingGrad()->windowW = window_w; } +void PoolingGrad::SetWindowH(int window_h) { this->primitive->value.AsPoolingGrad()->windowH = window_h; } +void PoolingGrad::SetStrideW(int stride_w) { this->primitive->value.AsPoolingGrad()->strideW = stride_w; } +void PoolingGrad::SetStrideH(int stride_h) { this->primitive->value.AsPoolingGrad()->strideH = stride_h; } +void PoolingGrad::SetPadMode(int pad_mode) { + this->primitive->value.AsPoolingGrad()->padMode = (schema::PadMode)pad_mode; +} +void PoolingGrad::SetPadUp(int pad_up) { this->primitive->value.AsPoolingGrad()->padUp = pad_up; } +void PoolingGrad::SetPadDown(int pad_down) { this->primitive->value.AsPoolingGrad()->padDown = pad_down; } +void PoolingGrad::SetPadLeft(int pad_left) { this->primitive->value.AsPoolingGrad()->padLeft = pad_left; } +void PoolingGrad::SetPadRight(int pad_right) { this->primitive->value.AsPoolingGrad()->padRight = pad_right; } +void PoolingGrad::SetRoundMode(int round_mode) { + this->primitive->value.AsPoolingGrad()->roundMode = (schema::RoundMode)round_mode; +} + +#else + +int PoolingGrad::GetFormat() const { return this->primitive->value_as_PoolingGrad()->format(); } +int PoolingGrad::GetPoolingMode() const { return this->primitive->value_as_PoolingGrad()->poolingMode(); } +bool PoolingGrad::GetGlobal() const { return this->primitive->value_as_PoolingGrad()->global(); } +int PoolingGrad::GetWindowW() const { return this->primitive->value_as_PoolingGrad()->windowW(); } +int PoolingGrad::GetWindowH() const { return this->primitive->value_as_PoolingGrad()->windowH(); } +int PoolingGrad::GetStrideW() const { return this->primitive->value_as_PoolingGrad()->strideW(); } +int PoolingGrad::GetStrideH() const { return this->primitive->value_as_PoolingGrad()->strideH(); } +int PoolingGrad::GetPadMode() const { return this->primitive->value_as_PoolingGrad()->padMode(); } +int PoolingGrad::GetPadUp() const { return this->primitive->value_as_PoolingGrad()->padUp(); } +int PoolingGrad::GetPadDown() const { return this->primitive->value_as_PoolingGrad()->padDown(); } +int PoolingGrad::GetPadLeft() const { return this->primitive->value_as_PoolingGrad()->padLeft(); } +int PoolingGrad::GetPadRight() const { return this->primitive->value_as_PoolingGrad()->padRight(); } +int PoolingGrad::GetRoundMode() const { return this->primitive->value_as_PoolingGrad()->roundMode(); } + +void PoolingGrad::SetFormat(int format) {} +void PoolingGrad::SetPoolingMode(int pooling_mode) {} +void PoolingGrad::SetGlobal(bool global) {} +void PoolingGrad::SetWindowW(int window_w) {} +void PoolingGrad::SetWindowH(int window_h) {} +void PoolingGrad::SetStrideW(int stride_w) {} +void PoolingGrad::SetStrideH(int stride_h) {} +void PoolingGrad::SetPadMode(int pad_mode) {} +void PoolingGrad::SetPadUp(int pad_up) {} +void PoolingGrad::SetPadDown(int pad_down) {} +void PoolingGrad::SetPadLeft(int pad_left) {} +void PoolingGrad::SetPadRight(int pad_right) {} +void PoolingGrad::SetRoundMode(int round_mode) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/pooling_grad.h b/mindspore/lite/c_ops/pooling_grad.h new file mode 100644 index 0000000000..4b0f5d3ea5 --- /dev/null +++ b/mindspore/lite/c_ops/pooling_grad.h @@ -0,0 +1,68 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_POOLING_GRAD_H_ +#define LITE_MINDSPORE_LITE_C_OPS_POOLING_GRAD_H_ + +namespace mindspore { +class PoolingGrad : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit PoolingGrad(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit PoolingGrad(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int GetFormat() const; + int GetPoolingMode() const; + bool GetGlobal() const; + int GetWindowW() const; + int GetWindowH() const; + int GetStrideW() const; + int GetStrideH() const; + int GetPadMode() const; + int GetPadUp() const; + int GetPadDown() const; + int GetPadLeft() const; + int GetPadRight() const; + int GetRoundMode() const; + void SetFormat(int format); + void SetPoolingMode(int pooling_mode); + void SetGlobal(bool global); + void SetWindowW(int window_w); + void SetWindowH(int window_h); + void SetStrideW(int stride_w); + void SetStrideH(int stride_h); + void SetPadMode(int pad_mode); + void SetPadUp(int pad_up); + void SetPadDown(int pad_down); + void SetPadLeft(int pad_left); + void SetPadRight(int pad_right); + void SetRoundMode(int round_mode); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_POOLING_GRAD_H_ diff --git a/mindspore/lite/c_ops/power.cc b/mindspore/lite/c_ops/power.cc new file mode 100644 index 0000000000..03ea09a4ec --- /dev/null +++ b/mindspore/lite/c_ops/power.cc @@ -0,0 +1,62 @@ +/** + * 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 "c_ops/power.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +float Power::GetPower() const { return this->primitive->value.AsPower()->power; } +float Power::GetScale() const { return this->primitive->value.AsPower()->scale; } +float Power::GetShift() const { return this->primitive->value.AsPower()->shift; } + +void Power::SetPower(float power) { this->primitive->value.AsPower()->power = power; } +void Power::SetScale(float scale) { this->primitive->value.AsPower()->scale = scale; } +void Power::SetShift(float shift) { this->primitive->value.AsPower()->shift = shift; } + +#else + +float Power::GetPower() const { return this->primitive->value_as_Power()->power(); } +float Power::GetScale() const { return this->primitive->value_as_Power()->scale(); } +float Power::GetShift() const { return this->primitive->value_as_Power()->shift(); } + +void Power::SetPower(float power) {} +void Power::SetScale(float scale) {} +void Power::SetShift(float shift) {} +#endif +int Power::InferShape(std::vector inputs, std::vector outputs) { + MS_ASSERT(this->primitive != nullptr); + auto x_tensor = inputs[0]; + MS_ASSERT(x_tensor != nullptr); + lite::tensor::Tensor *exp_tensor = nullptr; + if (inputs.size() == 2) { + exp_tensor = inputs[1]; + MS_ASSERT(exp_tensor != nullptr); + } + auto output_tensor = outputs[0]; + MS_ASSERT(output_tensor != nullptr); + if (exp_tensor) { + if (exp_tensor->shape() != x_tensor->shape() || exp_tensor->data_type() != x_tensor->data_type()) { + MS_LOG(ERROR) << "Power inputs shape or type is not equal!"; + return 1; + } + } + + output_tensor->SetFormat(x_tensor->GetFormat()); + output_tensor->set_shape(x_tensor->shape()); + output_tensor->set_data_type(x_tensor->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/power.h b/mindspore/lite/c_ops/power.h new file mode 100644 index 0000000000..923194c97e --- /dev/null +++ b/mindspore/lite/c_ops/power.h @@ -0,0 +1,49 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_POWER_H_ +#define LITE_MINDSPORE_LITE_C_OPS_POWER_H_ + +namespace mindspore { +class Power : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Power(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Power(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + float GetPower() const; + float GetScale() const; + float GetShift() const; + void SetPower(float power); + void SetScale(float scale); + void SetShift(float shift); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_POWER_H_ diff --git a/mindspore/lite/c_ops/power_grad.cc b/mindspore/lite/c_ops/power_grad.cc new file mode 100644 index 0000000000..b8cc0711b9 --- /dev/null +++ b/mindspore/lite/c_ops/power_grad.cc @@ -0,0 +1,39 @@ +/** + * 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 "c_ops/power_grad.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +float PowerGrad::GetPower() const { return this->primitive->value.AsPowerGrad()->power; } +float PowerGrad::GetScale() const { return this->primitive->value.AsPowerGrad()->scale; } +float PowerGrad::GetShift() const { return this->primitive->value.AsPowerGrad()->shift; } + +void PowerGrad::SetPower(float power) { this->primitive->value.AsPowerGrad()->power = power; } +void PowerGrad::SetScale(float scale) { this->primitive->value.AsPowerGrad()->scale = scale; } +void PowerGrad::SetShift(float shift) { this->primitive->value.AsPowerGrad()->shift = shift; } + +#else + +float PowerGrad::GetPower() const { return this->primitive->value_as_PowerGrad()->power(); } +float PowerGrad::GetScale() const { return this->primitive->value_as_PowerGrad()->scale(); } +float PowerGrad::GetShift() const { return this->primitive->value_as_PowerGrad()->shift(); } + +void PowerGrad::SetPower(float power) {} +void PowerGrad::SetScale(float scale) {} +void PowerGrad::SetShift(float shift) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/power_grad.h b/mindspore/lite/c_ops/power_grad.h new file mode 100644 index 0000000000..e5952cb87b --- /dev/null +++ b/mindspore/lite/c_ops/power_grad.h @@ -0,0 +1,48 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_POWER_GRAD_H_ +#define LITE_MINDSPORE_LITE_C_OPS_POWER_GRAD_H_ + +namespace mindspore { +class PowerGrad : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit PowerGrad(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit PowerGrad(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + float GetPower() const; + float GetScale() const; + float GetShift() const; + void SetPower(float power); + void SetScale(float scale); + void SetShift(float shift); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_POWER_GRAD_H_ diff --git a/mindspore/lite/c_ops/prelu.cc b/mindspore/lite/c_ops/prelu.cc new file mode 100644 index 0000000000..3433939ee9 --- /dev/null +++ b/mindspore/lite/c_ops/prelu.cc @@ -0,0 +1,34 @@ +/** + * 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 "c_ops/prelu.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector Prelu::GetSlope() const { return this->primitive->value.AsPrelu()->slope; } + +void Prelu::SetSlope(const std::vector &slope) { this->primitive->value.AsPrelu()->slope = slope; } + +#else + +std::vector Prelu::GetSlope() const { + auto fb_vector = this->primitive->value_as_Prelu()->slope(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void Prelu::SetSlope(const std::vector &slope) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/prelu.h b/mindspore/lite/c_ops/prelu.h new file mode 100644 index 0000000000..c7b5910c2b --- /dev/null +++ b/mindspore/lite/c_ops/prelu.h @@ -0,0 +1,45 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#include "c_ops/activation.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_PRELU_H_ +#define LITE_MINDSPORE_LITE_C_OPS_PRELU_H_ + +namespace mindspore { +class Prelu : public Activation { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Prelu(schema::PrimitiveT *primitive) : Activation(primitive) {} +#else + explicit Prelu(schema::Primitive *primitive) : Activation(primitive) {} +#endif + std::vector GetSlope() const; + void SetSlope(const std::vector &slope); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_PRELU_H_ diff --git a/mindspore/lite/c_ops/primitive_c.h b/mindspore/lite/c_ops/primitive_c.h new file mode 100644 index 0000000000..363aed120a --- /dev/null +++ b/mindspore/lite/c_ops/primitive_c.h @@ -0,0 +1,73 @@ +/** + * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/). + * + * 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. + */ + +#ifndef MINDSPORE_CORE_C_OPS_PRIMITIVE_C_H_ +#define MINDSPORE_CORE_C_OPS_PRIMITIVE_C_H_ +#include +#include +#include +#include "src/ir/tensor.h" +#include "ir/primitive.h" +#include "ir/value.h" + +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif +namespace mindspore { +constexpr uint32_t kSingleNum = 1; +constexpr uint32_t kDoubleNum = 2; +constexpr uint32_t kMultiNum = 3; +constexpr uint32_t kDimension_4d = 4; +enum NCHW_SHAPE { NCHW_N = 0, NCHW_C = 1, NCHW_H = 2, NCHW_W = 3 }; +enum NHWC_SHAPE { NHWC_N = 0, NHWC_H = 1, NHWC_W = 2, NHWC_C = 3 }; +enum HWCK_SHAPE { HWCK_H = 0, HWCK_W = 1, HWCK_C = 2, HWCK_K = 3 }; +enum HWKC_SHAPE { HWKC_H = 0, HWKC_W = 1, HWKC_K = 2, HWKC_C = 3 }; +enum KCHW_SHAPE { KCHW_K = 0, KCHW_C = 1, KCHW_H = 2, KCHW_W = 3 }; +enum CKHW_SHAPE { CKHW_C = 0, CKHW_K = 1, CKHW_H = 2, CKHW_W = 3 }; +enum CHWK_SHAPE { CHWK_C = 0, CHWK_H = 1, CHWK_W = 2, CHWK_K = 3 }; +enum KHWC_SHAPE { KHWC_K = 0, KHWC_H = 1, KHWC_W = 2, KHWC_C = 3 }; + +const std::set kSupportDataType = {kNumberTypeUInt8, kNumberTypeInt32, kNumberTypeFloat32}; + +class PrimitiveC : public Primitive { + public: + explicit PrimitiveC(const std::string &name) : Primitive(name) {} + +#ifdef PRIMITIVE_WRITEABLE + explicit PrimitiveC(schema::PrimitiveT *primitive) : Primitive(""), primitive(primitive) {} +#else + explicit PrimitiveC(schema::Primitive *primitive) : Primitive(""), primitive(primitive) {} +#endif + static Primitive *CreatePrimitive(schema::Primitive *primitive); + virtual ~PrimitiveC() {} + const bool GetInferFlag() const { return this->infer_flag_; } + void SetInferFlag(bool flag) { this->infer_flag_ = flag; } + virtual int InferShape(std::vector inputs_, std::vector outputs_) = 0; + + protected: +#ifdef PRIMITIVE_WRITEABLE + schema::PrimitiveT *primitive; +#else + schema::Primitive *primitive; +#endif + bool infer_flag_ = true; +}; +} // namespace mindspore +#endif // MINDSPORE_CORE_C_OPS_PRIMITIVE_C_H_ diff --git a/mindspore/lite/c_ops/prior_box.cc b/mindspore/lite/c_ops/prior_box.cc new file mode 100644 index 0000000000..5de4f2b257 --- /dev/null +++ b/mindspore/lite/c_ops/prior_box.cc @@ -0,0 +1,127 @@ +/** + * 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 "c_ops/prior_box.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector PriorBox::GetMinSizes() const { return this->primitive->value.AsPriorBox()->max_sizes; } +std::vector PriorBox::GetMaxSizes() const { return this->primitive->value.AsPriorBox()->max_sizes; } +std::vector PriorBox::GetAspectRatios() const { return this->primitive->value.AsPriorBox()->aspect_ratios; } +std::vector PriorBox::GetVariances() const { return this->primitive->value.AsPriorBox()->variances; } +int PriorBox::GetImageSizeW() const { return this->primitive->value.AsPriorBox()->image_size_w; } +int PriorBox::GetImageSizeH() const { return this->primitive->value.AsPriorBox()->image_size_h; } +float PriorBox::GetStepW() const { return this->primitive->value.AsPriorBox()->step_w; } +float PriorBox::GetStepH() const { return this->primitive->value.AsPriorBox()->step_h; } +bool PriorBox::GetClip() const { return this->primitive->value.AsPriorBox()->clip; } +bool PriorBox::GetFlip() const { return this->primitive->value.AsPriorBox()->flip; } +float PriorBox::GetOffset() const { return this->primitive->value.AsPriorBox()->offset; } + +void PriorBox::SetMinSizes(const std::vector &min_sizes) { + this->primitive->value.AsPriorBox()->min_sizes = min_sizes; +} +void PriorBox::SetMaxSizes(const std::vector &max_sizes) { + this->primitive->value.AsPriorBox()->max_sizes = max_sizes; +} +void PriorBox::SetAspectRatios(const std::vector &aspect_ratios) { + this->primitive->value.AsPriorBox()->aspect_ratios = aspect_ratios; +} +void PriorBox::SetVariances(const std::vector &variances) { + this->primitive->value.AsPriorBox()->variances = variances; +} +void PriorBox::SetImageSizeW(int image_size_w) { this->primitive->value.AsPriorBox()->image_size_w = image_size_w; } +void PriorBox::SetImageSizeH(int image_size_h) { this->primitive->value.AsPriorBox()->image_size_h = image_size_h; } +void PriorBox::SetStepW(float step_w) { this->primitive->value.AsPriorBox()->step_w = step_w; } +void PriorBox::SetStepH(float step_h) { this->primitive->value.AsPriorBox()->step_h = step_h; } +void PriorBox::SetClip(bool clip) { this->primitive->value.AsPriorBox()->clip = clip; } +void PriorBox::SetFlip(bool flip) { this->primitive->value.AsPriorBox()->flip = flip; } +void PriorBox::SetOffset(float offset) { this->primitive->value.AsPriorBox()->offset = offset; } + +#else + +std::vector PriorBox::GetMinSizes() const { + auto fb_vector = this->primitive->value_as_PriorBox()->min_sizes(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +std::vector PriorBox::GetMaxSizes() const { + auto fb_vector = this->primitive->value_as_PriorBox()->max_sizes(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +std::vector PriorBox::GetAspectRatios() const { + auto fb_vector = this->primitive->value_as_PriorBox()->aspect_ratios(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +std::vector PriorBox::GetVariances() const { + auto fb_vector = this->primitive->value_as_PriorBox()->variances(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +int PriorBox::GetImageSizeW() const { return this->primitive->value_as_PriorBox()->image_size_w(); } +int PriorBox::GetImageSizeH() const { return this->primitive->value_as_PriorBox()->image_size_h(); } +float PriorBox::GetStepW() const { return this->primitive->value_as_PriorBox()->step_w(); } +float PriorBox::GetStepH() const { return this->primitive->value_as_PriorBox()->step_h(); } +bool PriorBox::GetClip() const { return this->primitive->value_as_PriorBox()->clip(); } +bool PriorBox::GetFlip() const { return this->primitive->value_as_PriorBox()->flip(); } +float PriorBox::GetOffset() const { return this->primitive->value_as_PriorBox()->offset(); } + +void PriorBox::SetMinSizes(const std::vector &min_sizes) {} +void PriorBox::SetMaxSizes(const std::vector &max_sizes) {} +void PriorBox::SetAspectRatios(const std::vector &aspect_ratios) {} +void PriorBox::SetVariances(const std::vector &variances) {} +void PriorBox::SetImageSizeW(int image_size_w) {} +void PriorBox::SetImageSizeH(int image_size_h) {} +void PriorBox::SetStepW(float step_w) {} +void PriorBox::SetStepH(float step_h) {} +void PriorBox::SetClip(bool clip) {} +void PriorBox::SetFlip(bool flip) {} +void PriorBox::SetOffset(float offset) {} +#endif +namespace { +constexpr int kPriorBoxPoints = 4; +constexpr int kPriorBoxN = 1; +constexpr int kPriorBoxW = 1; +constexpr int kPriorBoxC = 2; +} // namespace + +int PriorBox::InferShape(std::vector inputs_, std::vector outputs_) { + std::vector different_aspect_ratios{1.0f}; + auto aspect_ratios = GetAspectRatios(); + MS_ASSERT(aspect_ratios != nullptr); + for (auto i = 0; i < aspect_ratios.size(); i++) { + float ratio = (aspect_ratios)[i]; + bool exist = std::any_of(different_aspect_ratios.begin(), different_aspect_ratios.end(), + [&](float v) { return abs(ratio - v) < 1e-6; }); + if (!exist) { + different_aspect_ratios.emplace_back(ratio); + if (GetFlip()) { + different_aspect_ratios.emplace_back(1.0f / ratio); + } + } + } + int32_t num_priors_box = GetMinSizes().size() * different_aspect_ratios.size() + GetMaxSizes().size(); + auto input = inputs_.at(0); + MS_ASSERT(input != nullptr); + int32_t h = input->Height() * input->Width() * num_priors_box * kPriorBoxPoints; + + std::vector output_shape{kPriorBoxN, h, kPriorBoxW, kPriorBoxC}; + auto output = outputs_.at(0); + MS_ASSERT(output != nullptr); + + output->set_shape(output_shape); + output->set_data_type(kNumberTypeFloat32); + output->SetFormat(input->GetFormat()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/prior_box.h b/mindspore/lite/c_ops/prior_box.h new file mode 100644 index 0000000000..e0d72a5a8b --- /dev/null +++ b/mindspore/lite/c_ops/prior_box.h @@ -0,0 +1,65 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_PRIOR_BOX_H_ +#define LITE_MINDSPORE_LITE_C_OPS_PRIOR_BOX_H_ + +namespace mindspore { +class PriorBox : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit PriorBox(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit PriorBox(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + std::vector GetMinSizes() const; + std::vector GetMaxSizes() const; + std::vector GetAspectRatios() const; + std::vector GetVariances() const; + int GetImageSizeW() const; + int GetImageSizeH() const; + float GetStepW() const; + float GetStepH() const; + bool GetClip() const; + bool GetFlip() const; + float GetOffset() const; + void SetMinSizes(const std::vector &min_sizes); + void SetMaxSizes(const std::vector &max_sizes); + void SetAspectRatios(const std::vector &aspect_ratios); + void SetVariances(const std::vector &variances); + void SetImageSizeW(int image_size_w); + void SetImageSizeH(int image_size_h); + void SetStepW(float step_w); + void SetStepH(float step_h); + void SetClip(bool clip); + void SetFlip(bool flip); + void SetOffset(float offset); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_PRIOR_BOX_H_ diff --git a/mindspore/lite/c_ops/quant_dtype_cast.cc b/mindspore/lite/c_ops/quant_dtype_cast.cc new file mode 100644 index 0000000000..3e08e6f1c5 --- /dev/null +++ b/mindspore/lite/c_ops/quant_dtype_cast.cc @@ -0,0 +1,49 @@ +/** + * 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 "c_ops/quant_dtype_cast.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int QuantDTypeCast::GetSrcT() const { return this->primitive->value.AsQuantDTypeCast()->srcT; } +int QuantDTypeCast::GetDstT() const { return this->primitive->value.AsQuantDTypeCast()->dstT; } + +void QuantDTypeCast::SetSrcT(int src_t) { this->primitive->value.AsQuantDTypeCast()->srcT = src_t; } +void QuantDTypeCast::SetDstT(int dst_t) { this->primitive->value.AsQuantDTypeCast()->dstT = dst_t; } + +#else + +int QuantDTypeCast::GetSrcT() const { return this->primitive->value_as_QuantDTypeCast()->srcT(); } +int QuantDTypeCast::GetDstT() const { return this->primitive->value_as_QuantDTypeCast()->dstT(); } + +void QuantDTypeCast::SetSrcT(int src_t) {} +void QuantDTypeCast::SetDstT(int dst_t) {} +#endif +int QuantDTypeCast::InferShape(std::vector inputs_, + std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + output->set_shape(input->shape()); + auto param = primitive->value_as_QuantDTypeCast(); + MS_ASSERT(input->data_type() == param->srcT); + output->set_data_type(static_cast(param->dstT())); + output->SetFormat(input->GetFormat()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/quant_dtype_cast.h b/mindspore/lite/c_ops/quant_dtype_cast.h new file mode 100644 index 0000000000..e7e128e9e2 --- /dev/null +++ b/mindspore/lite/c_ops/quant_dtype_cast.h @@ -0,0 +1,47 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_QUANT_D_TYPE_CAST_H_ +#define LITE_MINDSPORE_LITE_C_OPS_QUANT_D_TYPE_CAST_H_ + +namespace mindspore { +class QuantDTypeCast : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit QuantDTypeCast(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit QuantDTypeCast(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetSrcT() const; + int GetDstT() const; + void SetSrcT(int src_t); + void SetDstT(int dst_t); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_QUANT_D_TYPE_CAST_H_ diff --git a/mindspore/lite/c_ops/range.cc b/mindspore/lite/c_ops/range.cc new file mode 100644 index 0000000000..e436fedd91 --- /dev/null +++ b/mindspore/lite/c_ops/range.cc @@ -0,0 +1,59 @@ +/** + * 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 "c_ops/range.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Range::GetDType() const { return this->primitive->value.AsRange()->dType; } +int Range::GetStart() const { return this->primitive->value.AsRange()->start; } +int Range::GetLimit() const { return this->primitive->value.AsRange()->limit; } +int Range::GetDelta() const { return this->primitive->value.AsRange()->delta; } + +void Range::SetDType(int d_type) { this->primitive->value.AsRange()->dType = d_type; } +void Range::SetStart(int start) { this->primitive->value.AsRange()->start = start; } +void Range::SetLimit(int limit) { this->primitive->value.AsRange()->limit = limit; } +void Range::SetDelta(int delta) { this->primitive->value.AsRange()->delta = delta; } + +#else + +int Range::GetDType() const { return this->primitive->value_as_Range()->dType(); } +int Range::GetStart() const { return this->primitive->value_as_Range()->start(); } +int Range::GetLimit() const { return this->primitive->value_as_Range()->limit(); } +int Range::GetDelta() const { return this->primitive->value_as_Range()->delta(); } + +void Range::SetDType(int d_type) {} +void Range::SetStart(int start) {} +void Range::SetLimit(int limit) {} +void Range::SetDelta(int delta) {} +#endif +int Range::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + + int shape_size = std::ceil(static_cast(GetLimit() - GetStart()) / GetDelta()); + std::vector in_shape(1); + in_shape.push_back(shape_size); + output->set_shape(in_shape); + output->set_data_type(input->data_type()); + output->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/range.h b/mindspore/lite/c_ops/range.h new file mode 100644 index 0000000000..7317d20880 --- /dev/null +++ b/mindspore/lite/c_ops/range.h @@ -0,0 +1,51 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_RANGE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_RANGE_H_ + +namespace mindspore { +class Range : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Range(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Range(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetDType() const; + int GetStart() const; + int GetLimit() const; + int GetDelta() const; + void SetDType(int d_type); + void SetStart(int start); + void SetLimit(int limit); + void SetDelta(int delta); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_RANGE_H_ diff --git a/mindspore/lite/c_ops/rank.cc b/mindspore/lite/c_ops/rank.cc new file mode 100644 index 0000000000..dfe568aca0 --- /dev/null +++ b/mindspore/lite/c_ops/rank.cc @@ -0,0 +1,33 @@ +/** + * 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 "c_ops/rank.h" + +namespace mindspore { +int Rank::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + std::vector in_shape(1, 1); + output->set_shape(in_shape); + output->set_data_type(input->data_type()); + output->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/rank.h b/mindspore/lite/c_ops/rank.h new file mode 100644 index 0000000000..bd593e37e1 --- /dev/null +++ b/mindspore/lite/c_ops/rank.h @@ -0,0 +1,43 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_RANK_H_ +#define LITE_MINDSPORE_LITE_C_OPS_RANK_H_ + +namespace mindspore { +class Rank : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Rank(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Rank(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_RANK_H_ diff --git a/mindspore/lite/c_ops/reduce.cc b/mindspore/lite/c_ops/reduce.cc new file mode 100644 index 0000000000..445a506de3 --- /dev/null +++ b/mindspore/lite/c_ops/reduce.cc @@ -0,0 +1,99 @@ +/** + * 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 "c_ops/reduce.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector Reduce::GetAxes() const { return this->primitive->value.AsReduce()->axes; } +int Reduce::GetKeepDims() const { return this->primitive->value.AsReduce()->keepDims; } +int Reduce::GetMode() const { return this->primitive->value.AsReduce()->mode; } + +void Reduce::SetAxes(const std::vector &axes) { this->primitive->value.AsReduce()->axes = axes; } +void Reduce::SetKeepDims(int keep_dims) { this->primitive->value.AsReduce()->keepDims = keep_dims; } +void Reduce::SetMode(int mode) { this->primitive->value.AsReduce()->mode = (schema::ReduceMode)mode; } + +#else + +std::vector Reduce::GetAxes() const { + auto fb_vector = this->primitive->value_as_Reduce()->axes(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +int Reduce::GetKeepDims() const { return this->primitive->value_as_Reduce()->keepDims(); } +int Reduce::GetMode() const { return this->primitive->value_as_Reduce()->mode(); } + +void Reduce::SetAxes(const std::vector &axes) {} +void Reduce::SetKeepDims(int keep_dims) {} +void Reduce::SetMode(int mode) {} +#endif +namespace { +constexpr size_t kInputSize = 1; +constexpr size_t kOutputSize = 1; +} // namespace +int Reduce::InferShape(std::vector inputs_, std::vector outputs_) { + if (inputs_.size() != kInputSize || outputs_.size() != kOutputSize) { + return 1; + } + auto input = inputs_.front(); + auto output = outputs_.front(); + if (input == nullptr || output == nullptr) { + return 1; + } + if (this->primitive == nullptr) { + return 1; + } + + bool keep_dims = static_cast(GetKeepDims()); + std::vector in_shape = input->shape(); + std::vector out_shape; + const auto &axes = GetAxes(); + auto num_axes = axes.size(); + // reduce on all axes + if (num_axes == 0) { + if (keep_dims) { + for (auto i = 0; i < in_shape.size(); i++) { + out_shape.push_back(1); + } + } + output->set_shape(out_shape); + output->set_data_type(input->data_type()); + return 0; + } + + // reduce on selected axes + for (size_t i = 0; i < in_shape.size(); i++) { + bool reduce_axis = false; + for (int idx = 0; idx < num_axes; ++idx) { + if (static_cast((axes)[idx]) == i) { + reduce_axis = true; + break; + } + } + if (reduce_axis) { + if (keep_dims) { + out_shape.push_back(1); + } + } else { + out_shape.push_back(in_shape[i]); + } + } + output->set_shape(out_shape); + output->set_data_type(input->data_type()); + output->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/reduce.h b/mindspore/lite/c_ops/reduce.h new file mode 100644 index 0000000000..0b50be1219 --- /dev/null +++ b/mindspore/lite/c_ops/reduce.h @@ -0,0 +1,49 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_REDUCE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_REDUCE_H_ + +namespace mindspore { +class Reduce : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Reduce(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Reduce(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + std::vector GetAxes() const; + int GetKeepDims() const; + int GetMode() const; + void SetAxes(const std::vector &axes); + void SetKeepDims(int keep_dims); + void SetMode(int mode); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_REDUCE_H_ diff --git a/mindspore/lite/c_ops/reshape.cc b/mindspore/lite/c_ops/reshape.cc new file mode 100644 index 0000000000..83789cede7 --- /dev/null +++ b/mindspore/lite/c_ops/reshape.cc @@ -0,0 +1,153 @@ +/** + * 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 "c_ops/reshape.h" +#include + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Reshape::GetFormat() const { return this->primitive->value.AsReshape()->format; } +std::vector Reshape::GetShape() const { return this->primitive->value.AsReshape()->shape; } + +void Reshape::SetFormat(int format) { this->primitive->value.AsReshape()->format = format; } +void Reshape::SetShape(const std::vector &shape) { this->primitive->value.AsReshape()->shape = shape; } + +#else + +int Reshape::GetFormat() const { return this->primitive->value_as_Reshape()->format(); } +std::vector Reshape::GetShape() const { + auto fb_vector = this->primitive->value_as_Reshape()->shape(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void Reshape::SetFormat(int format) {} +void Reshape::SetShape(const std::vector &shape) {} +#endif + +int Reshape::CalNewShape(const lite::tensor::Tensor *in_tensor, std::vector *out_shape) const { + size_t in_shape_size = 1; + for (size_t i = 0; i < in_tensor->shape().size(); i++) { + in_shape_size *= in_tensor->shape()[i]; + } + + int64_t inferIndex = -1; + size_t out_shapeSize = 1; + for (size_t i = 0; i < out_shape->size(); i++) { + if (out_shape->at(i) == -1) { + if (inferIndex == -1) { + inferIndex = i; + } else { + MS_LOG(ERROR) << "output shape should has no more than one dim which need infer"; + return 1; + } + } else if (out_shape->at(i) < 0) { + MS_LOG(ERROR) << "output shape dim should be non-negative"; + return 1; + } else if (out_shape->at(i) == 0) { + out_shape->at(i) = in_tensor->shape().at(i); + out_shapeSize *= out_shape->at(i); + } else { + out_shapeSize *= out_shape->at(i); + } + } + + if (inferIndex == -1 && out_shapeSize != in_shape_size) { + MS_LOG(ERROR) << "output shapeSize: " << out_shapeSize << " should be equal to input shapeSize: " << in_shape_size; + return 1; + } + if (inferIndex != -1) { + out_shape->at(inferIndex) = in_shape_size / out_shapeSize; + } + return 0; +} + +template +void CalShape(const T *data, const std::vector &inputs, std::vector *out_shape, + int shape_size) { + int input_count = inputs[0]->ElementsNum(); + + int index = 0; + int size = 1; + for (size_t i = 0; i < shape_size; i++) { + if (data[i] == -1) { + index = i; + } else { + size *= data[i]; + } + out_shape->push_back(data[i]); + } + if (data[index] == -1) { + (*out_shape)[index] = input_count / size; + } +} + +int Reshape::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + + std::vector out_shape; + if (inputs_.size() == kDoubleNum) { + auto shape_tensor = inputs_.at(1); + if (shape_tensor->Data() == nullptr) { + MS_LOG(INFO) << "Do infer shape in runtime."; + return 1; + } + size_t shape_size = shape_tensor->ElementsNum(); + switch (shape_tensor->data_type()) { + case kNumberTypeInt8: { + auto data = reinterpret_cast(shape_tensor->Data()); + CalShape(data, inputs_, &out_shape, shape_size); + } break; + case kNumberTypeInt32: { + auto data = reinterpret_cast(shape_tensor->Data()); + CalShape(data, inputs_, &out_shape, shape_size); + } break; + case kNumberTypeFloat: { + auto data = reinterpret_cast(shape_tensor->Data()); + CalShape(data, inputs_, &out_shape, shape_size); + } break; + case kNumberTypeUInt32: { + auto data = reinterpret_cast(shape_tensor->Data()); + CalShape(data, inputs_, &out_shape, shape_size); + } break; + default: { + MS_LOG(ERROR) << "Reshape weight tensor has unsupported dataType: " << shape_tensor->data_type(); + return 1; + } + } + } else if (inputs_.size() == kSingleNum) { + std::copy(GetShape().begin(), GetShape().end(), std::back_inserter(out_shape)); + } else { + MS_LOG(ERROR) << "inputs tensor size invalid."; + return 1; + } + + auto ret = CalNewShape(inputs_.front(), &out_shape); + if (ret != 0) { + MS_LOG(ERROR) << "CalNewShape error"; + return ret; + } + + output->set_shape(out_shape); + output->set_data_type(input->data_type()); + output->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/reshape.h b/mindspore/lite/c_ops/reshape.h new file mode 100644 index 0000000000..7fa1244a2d --- /dev/null +++ b/mindspore/lite/c_ops/reshape.h @@ -0,0 +1,50 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_RESHAPE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_RESHAPE_H_ + +namespace mindspore { +class Reshape : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Reshape(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Reshape(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetFormat() const; + std::vector GetShape() const; + void SetFormat(int format); + void SetShape(const std::vector &shape); + + private: + int CalNewShape(const lite::tensor::Tensor *in_tensor, std::vector *out_shape) const; +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_RESHAPE_H_ diff --git a/mindspore/lite/c_ops/resize.cc b/mindspore/lite/c_ops/resize.cc new file mode 100644 index 0000000000..02860e8ed3 --- /dev/null +++ b/mindspore/lite/c_ops/resize.cc @@ -0,0 +1,82 @@ +/** + * 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 "c_ops/resize.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Resize::GetFormat() const { return this->primitive->value.AsResize()->format; } +int Resize::GetMethod() const { return this->primitive->value.AsResize()->method; } +long Resize::GetNewHeight() const { return this->primitive->value.AsResize()->newHeight; } +long Resize::GetNewWidth() const { return this->primitive->value.AsResize()->newWidth; } +bool Resize::GetAlignCorners() const { return this->primitive->value.AsResize()->alignCorners; } +bool Resize::GetPreserveAspectRatio() const { return this->primitive->value.AsResize()->preserveAspectRatio; } + +void Resize::SetFormat(int format) { this->primitive->value.AsResize()->format = (schema::Format)format; } +void Resize::SetMethod(int method) { this->primitive->value.AsResize()->method = (schema::ResizeMethod)method; } +void Resize::SetNewHeight(long new_height) { this->primitive->value.AsResize()->newHeight = new_height; } +void Resize::SetNewWidth(long new_width) { this->primitive->value.AsResize()->newWidth = new_width; } +void Resize::SetAlignCorners(bool align_corners) { this->primitive->value.AsResize()->alignCorners = align_corners; } +void Resize::SetPreserveAspectRatio(bool preserve_aspect_ratio) { + this->primitive->value.AsResize()->preserveAspectRatio = preserve_aspect_ratio; +} + +#else + +int Resize::GetFormat() const { return this->primitive->value_as_Resize()->format(); } +int Resize::GetMethod() const { return this->primitive->value_as_Resize()->method(); } +long Resize::GetNewHeight() const { return this->primitive->value_as_Resize()->newHeight(); } +long Resize::GetNewWidth() const { return this->primitive->value_as_Resize()->newWidth(); } +bool Resize::GetAlignCorners() const { return this->primitive->value_as_Resize()->alignCorners(); } +bool Resize::GetPreserveAspectRatio() const { return this->primitive->value_as_Resize()->preserveAspectRatio(); } + +void Resize::SetFormat(int format) {} +void Resize::SetMethod(int method) {} +void Resize::SetNewHeight(long new_height) {} +void Resize::SetNewWidth(long new_width) {} +void Resize::SetAlignCorners(bool align_corners) {} +void Resize::SetPreserveAspectRatio(bool preserve_aspect_ratio) {} +#endif +namespace { +constexpr int kInputRank = 4; +} // namespace +int Resize::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + if (input == nullptr) { + return 1; + } + MS_ASSERT(input->shape().size() == kInputRank); + + auto output = outputs_.front(); + if (output == nullptr) { + return 1; + } + auto new_height = GetNewHeight(); + auto new_width = GetNewWidth(); + + std::vector output_shape; + output_shape.push_back(input->Batch()); + output_shape.push_back(new_height); + output_shape.push_back(new_width); + output_shape.push_back(input->Channel()); + output->set_shape(output_shape); + output->set_data_type(input->data_type()); + output->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/resize.h b/mindspore/lite/c_ops/resize.h new file mode 100644 index 0000000000..b04c6fceea --- /dev/null +++ b/mindspore/lite/c_ops/resize.h @@ -0,0 +1,55 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_RESIZE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_RESIZE_H_ + +namespace mindspore { +class Resize : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Resize(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Resize(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetFormat() const; + int GetMethod() const; + long GetNewHeight() const; + long GetNewWidth() const; + bool GetAlignCorners() const; + bool GetPreserveAspectRatio() const; + void SetFormat(int format); + void SetMethod(int method); + void SetNewHeight(long new_height); + void SetNewWidth(long new_width); + void SetAlignCorners(bool align_corners); + void SetPreserveAspectRatio(bool preserve_aspect_ratio); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_RESIZE_H_ diff --git a/mindspore/lite/c_ops/reverse.cc b/mindspore/lite/c_ops/reverse.cc new file mode 100644 index 0000000000..fc8c161cd7 --- /dev/null +++ b/mindspore/lite/c_ops/reverse.cc @@ -0,0 +1,34 @@ +/** + * 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 "c_ops/reverse.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector Reverse::GetAxis() const { return this->primitive->value.AsReverse()->axis; } + +void Reverse::SetAxis(const std::vector &axis) { this->primitive->value.AsReverse()->axis = axis; } + +#else + +std::vector Reverse::GetAxis() const { + auto fb_vector = this->primitive->value_as_Reverse()->axis(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void Reverse::SetAxis(const std::vector &axis) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/reverse.h b/mindspore/lite/c_ops/reverse.h new file mode 100644 index 0000000000..b8ac1fa151 --- /dev/null +++ b/mindspore/lite/c_ops/reverse.h @@ -0,0 +1,44 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_REVERSE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_REVERSE_H_ + +namespace mindspore { +class Reverse : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Reverse(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Reverse(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + std::vector GetAxis() const; + void SetAxis(const std::vector &axis); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_REVERSE_H_ diff --git a/mindspore/lite/c_ops/reverse_sequence.cc b/mindspore/lite/c_ops/reverse_sequence.cc new file mode 100644 index 0000000000..36a01468f9 --- /dev/null +++ b/mindspore/lite/c_ops/reverse_sequence.cc @@ -0,0 +1,60 @@ +/** + * 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 "c_ops/reverse_sequence.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int ReverseSequence::GetSeqAxis() const { return this->primitive->value.AsReverseSequence()->seqAxis; } +int ReverseSequence::GetBatchAxis() const { return this->primitive->value.AsReverseSequence()->batchAxis; } +std::vector ReverseSequence::GetSeqLengths() const { + return this->primitive->value.AsReverseSequence()->seqLengths; +} + +void ReverseSequence::SetSeqAxis(int seq_axis) { this->primitive->value.AsReverseSequence()->seqAxis = seq_axis; } +void ReverseSequence::SetBatchAxis(int batch_axis) { + this->primitive->value.AsReverseSequence()->batchAxis = batch_axis; +} +void ReverseSequence::SetSeqLengths(const std::vector &seq_lengths) { + this->primitive->value.AsReverseSequence()->seqLengths = seq_lengths; +} + +#else + +int ReverseSequence::GetSeqAxis() const { return this->primitive->value_as_ReverseSequence()->seqAxis(); } +int ReverseSequence::GetBatchAxis() const { return this->primitive->value_as_ReverseSequence()->batchAxis(); } +std::vector ReverseSequence::GetSeqLengths() const { + auto fb_vector = this->primitive->value_as_ReverseSequence()->seqLengths(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void ReverseSequence::SetSeqAxis(int seq_axis) {} +void ReverseSequence::SetBatchAxis(int batch_axis) {} +void ReverseSequence::SetSeqLengths(const std::vector &seq_lengths) {} +#endif +int ReverseSequence::InferShape(std::vector inputs, + std::vector outputs) { + auto input = inputs.front(); + auto output = outputs.front(); + MS_ASSERT(input != nullptr); + MS_ASSERT(output != nullptr); + + output->set_shape(input->shape()); + output->set_data_type(input->data_type()); + output->SetFormat(input->GetFormat()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/reverse_sequence.h b/mindspore/lite/c_ops/reverse_sequence.h new file mode 100644 index 0000000000..962d125873 --- /dev/null +++ b/mindspore/lite/c_ops/reverse_sequence.h @@ -0,0 +1,49 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_REVERSE_SEQUENCE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_REVERSE_SEQUENCE_H_ + +namespace mindspore { +class ReverseSequence : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit ReverseSequence(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit ReverseSequence(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetSeqAxis() const; + int GetBatchAxis() const; + std::vector GetSeqLengths() const; + void SetSeqAxis(int seq_axis); + void SetBatchAxis(int batch_axis); + void SetSeqLengths(const std::vector &seq_lengths); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_REVERSE_SEQUENCE_H_ diff --git a/mindspore/lite/c_ops/roi_pooling.cc b/mindspore/lite/c_ops/roi_pooling.cc new file mode 100644 index 0000000000..912faf34fd --- /dev/null +++ b/mindspore/lite/c_ops/roi_pooling.cc @@ -0,0 +1,75 @@ +/** + * 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 "c_ops/roi_pooling.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int ROIPooling::GetPooledH() const { return this->primitive->value.AsROIPooling()->pooledH; } +int ROIPooling::GetPooledW() const { return this->primitive->value.AsROIPooling()->pooledW; } +float ROIPooling::GetScale() const { return this->primitive->value.AsROIPooling()->scale; } + +void ROIPooling::SetPooledH(int pooled_h) { this->primitive->value.AsROIPooling()->pooledH = pooled_h; } +void ROIPooling::SetPooledW(int pooled_w) { this->primitive->value.AsROIPooling()->pooledW = pooled_w; } +void ROIPooling::SetScale(float scale) { this->primitive->value.AsROIPooling()->scale = scale; } + +#else + +int ROIPooling::GetPooledH() const { return this->primitive->value_as_ROIPooling()->pooledH(); } +int ROIPooling::GetPooledW() const { return this->primitive->value_as_ROIPooling()->pooledW(); } +float ROIPooling::GetScale() const { return this->primitive->value_as_ROIPooling()->scale(); } + +void ROIPooling::SetPooledH(int pooled_h) {} +void ROIPooling::SetPooledW(int pooled_w) {} +void ROIPooling::SetScale(float scale) {} +#endif + +int ROIPooling::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + if (inputs_.size() != kDoubleNum) { + MS_LOG(ERROR) << "inputs number is not equal to " << kDoubleNum; + return 1; + } + auto input = inputs_.front(); + if (input == nullptr) { + return 1; + } + auto roi = inputs_.at(1); + if (roi == nullptr) { + return 1; + } + auto output = outputs_.front(); + if (output == nullptr) { + return 1; + } + + auto new_h = GetPooledH(); + auto new_w = GetPooledW(); + + auto shape_data = roi->shape(); + + std::vector output_shape; + output_shape.push_back(shape_data[0]); + output_shape.push_back(new_h); + output_shape.push_back(new_w); + output_shape.push_back(input->Channel()); + output->set_shape(output_shape); + output->set_data_type(input->data_type()); + output->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/roi_pooling.h b/mindspore/lite/c_ops/roi_pooling.h new file mode 100644 index 0000000000..16f833c2ac --- /dev/null +++ b/mindspore/lite/c_ops/roi_pooling.h @@ -0,0 +1,49 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_R_O_I_POOLING_H_ +#define LITE_MINDSPORE_LITE_C_OPS_R_O_I_POOLING_H_ + +namespace mindspore { +class ROIPooling : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit ROIPooling(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit ROIPooling(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetPooledH() const; + int GetPooledW() const; + float GetScale() const; + void SetPooledH(int pooled_h); + void SetPooledW(int pooled_w); + void SetScale(float scale); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_R_O_I_POOLING_H_ diff --git a/mindspore/lite/c_ops/rsqrt.h b/mindspore/lite/c_ops/rsqrt.h new file mode 100644 index 0000000000..f108e2db86 --- /dev/null +++ b/mindspore/lite/c_ops/rsqrt.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic_self.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_RSQRT_H_ +#define LITE_MINDSPORE_LITE_C_OPS_RSQRT_H_ + +namespace mindspore { +class Rsqrt : public ArithmeticSelf { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Rsqrt(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} +#else + explicit Rsqrt(schema::Primitive *primitive) : ArithmeticSelf(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_RSQRT_H_ diff --git a/mindspore/lite/c_ops/scale.cc b/mindspore/lite/c_ops/scale.cc new file mode 100644 index 0000000000..4b5e82c741 --- /dev/null +++ b/mindspore/lite/c_ops/scale.cc @@ -0,0 +1,31 @@ +/** + * 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 "c_ops/scale.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Scale::GetAxis() const { return this->primitive->value.AsScale()->axis; } + +void Scale::SetAxis(int axis) { this->primitive->value.AsScale()->axis = axis; } + +#else + +int Scale::GetAxis() const { return this->primitive->value_as_Scale()->axis(); } + +void Scale::SetAxis(int axis) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/scale.h b/mindspore/lite/c_ops/scale.h new file mode 100644 index 0000000000..6c6daff345 --- /dev/null +++ b/mindspore/lite/c_ops/scale.h @@ -0,0 +1,44 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_SCALE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_SCALE_H_ + +namespace mindspore { +class Scale : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Scale(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Scale(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int GetAxis() const; + void SetAxis(int axis); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_SCALE_H_ diff --git a/mindspore/lite/c_ops/scatter_nd.cc b/mindspore/lite/c_ops/scatter_nd.cc new file mode 100644 index 0000000000..6573fec69a --- /dev/null +++ b/mindspore/lite/c_ops/scatter_nd.cc @@ -0,0 +1,62 @@ +/** + * 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 "c_ops/scatter_nd.h" + +namespace mindspore { +namespace { +constexpr int kScatterNDInputNum = 3; +constexpr int kScatterNDOutputNum = 1; +constexpr int kScatterShapeIndex = 0; +constexpr int kScatterIndicesIndex = 1; +constexpr int kScatterUpdateIndex = 2; +} // namespace + +int ScatterND::InferShape(std::vector inputs_, std::vector outputs_) { + if (inputs_.size() != kScatterNDInputNum) { + MS_LOG(ERROR) << "inputs number is not equal to " << kScatterNDInputNum; + return 1; + } + if (outputs_.size() != kScatterNDOutputNum) { + MS_LOG(ERROR) << "outputs number is not equal to " << kScatterNDInputNum; + return 1; + } + auto shape = inputs_.at(kScatterShapeIndex); + if (shape == nullptr) { + MS_LOG(ERROR) << "shape null pointer dereferencing."; + return 1; + } + auto indices = inputs_.at(kScatterIndicesIndex); + if (indices == nullptr) { + MS_LOG(ERROR) << "indices null pointer dereferencing."; + return 1; + } + auto update = inputs_.at(kScatterUpdateIndex); + if (update == nullptr) { + MS_LOG(ERROR) << "update null pointer dereferencing."; + return 1; + } + auto output = outputs_.front(); + auto shape_data = reinterpret_cast(shape->Data()); + std::vector out_shape(shape_data, shape_data + shape->DataSize()); + output->set_shape(out_shape); + output->set_data_type(update->data_type()); + output->SetFormat(update->GetFormat()); + + return 0; +} + +} // namespace mindspore diff --git a/mindspore/lite/c_ops/scatter_nd.h b/mindspore/lite/c_ops/scatter_nd.h new file mode 100644 index 0000000000..3ce4b86dd6 --- /dev/null +++ b/mindspore/lite/c_ops/scatter_nd.h @@ -0,0 +1,43 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_SCATTER_ND_H_ +#define LITE_MINDSPORE_LITE_C_OPS_SCATTER_ND_H_ + +namespace mindspore { +class ScatterND : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit ScatterND(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit ScatterND(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_SCATTER_ND_H_ diff --git a/mindspore/lite/c_ops/shape.cc b/mindspore/lite/c_ops/shape.cc new file mode 100644 index 0000000000..544e71f117 --- /dev/null +++ b/mindspore/lite/c_ops/shape.cc @@ -0,0 +1,53 @@ +/** + * 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 "c_ops/shape.h" + +namespace mindspore { +namespace { +constexpr int kShapeInputNum = 1; +constexpr int kShapeOutputNum = 1; + +} // namespace +int Shape::InferShape(std::vector inputs_, std::vector outputs_) { + if (inputs_.size() != kShapeInputNum) { + MS_LOG(ERROR) << "inputs to Shape operator should be 1, but " << inputs_.size() << " is given."; + return 1; + } + if (outputs_.size() != kShapeOutputNum) { + MS_LOG(ERROR) << "outputs to Shape operator should be 1, but " << outputs_.size() << " is given."; + return 1; + } + + auto in_tensor = inputs_.front(); + auto out_tensor = outputs_.front(); + std::vector out_shape; + out_shape.push_back(static_cast(in_tensor->shape().size())); + + auto ret_shape = out_tensor->set_shape(out_shape); + if (ret_shape != 1 || size_t(out_tensor->shape()[0]) != in_tensor->shape().size()) { + MS_LOG(ERROR) << "Set shape fails."; + return 1; + } + auto ret_dtype = out_tensor->set_data_type(in_tensor->data_type()); + if (ret_dtype != in_tensor->data_type()) { + MS_LOG(ERROR) << "Set datatype fails."; + return 1; + } + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/shape.h b/mindspore/lite/c_ops/shape.h new file mode 100644 index 0000000000..06ab67591d --- /dev/null +++ b/mindspore/lite/c_ops/shape.h @@ -0,0 +1,43 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_SHAPE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_SHAPE_H_ + +namespace mindspore { +class Shape : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Shape(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Shape(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_SHAPE_H_ diff --git a/mindspore/lite/c_ops/sin.h b/mindspore/lite/c_ops/sin.h new file mode 100644 index 0000000000..ba93a69045 --- /dev/null +++ b/mindspore/lite/c_ops/sin.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic_self.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_SIN_H_ +#define LITE_MINDSPORE_LITE_C_OPS_SIN_H_ + +namespace mindspore { +class Sin : public ArithmeticSelf { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Sin(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} +#else + explicit Sin(schema::Primitive *primitive) : ArithmeticSelf(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_SIN_H_ diff --git a/mindspore/lite/c_ops/slice.cc b/mindspore/lite/c_ops/slice.cc new file mode 100644 index 0000000000..855ae6f444 --- /dev/null +++ b/mindspore/lite/c_ops/slice.cc @@ -0,0 +1,90 @@ +/** + * 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 "c_ops/slice.h" + +namespace mindspore { +namespace { +constexpr int kSliceInputNum = 1; +constexpr int kSliceOutputNum = 1; +} // namespace +#ifdef PRIMITIVE_WRITEABLE +int SliceOp::GetFormat() const { return this->primitive->value.AsSlice()->format; } +std::vector SliceOp::GetBegin() const { return this->primitive->value.AsSlice()->begin; } +std::vector SliceOp::GetSize() const { return this->primitive->value.AsSlice()->size; } + +void SliceOp::SetFormat(int format) { this->primitive->value.AsSlice()->format = format; } +void SliceOp::SetBegin(const std::vector &begin) { this->primitive->value.AsSlice()->begin = begin; } +void SliceOp::SetSize(const std::vector &size) { this->primitive->value.AsSlice()->size = size; } + +#else + +int SliceOp::GetFormat() const { return this->primitive->value_as_Slice()->format(); } +std::vector SliceOp::GetBegin() const { + auto fb_vector = this->primitive->value_as_Slice()->begin(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +std::vector SliceOp::GetSize() const { + auto fb_vector = this->primitive->value_as_Slice()->size(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void SliceOp::SetFormat(int format) {} +void SliceOp::SetBegin(const std::vector &begin) {} +void SliceOp::SetSize(const std::vector &size) {} +#endif + +int SliceOp::InferShape(std::vector inputs, std::vector outputs) { + MS_ASSERT(this->primitive != nullptr); + if (inputs.size() != kSliceInputNum || outputs.size() != kSliceOutputNum) { + MS_LOG(ERROR) << "input size:" << inputs.size() << ",output size:" << outputs.size(); + return 1; + } + auto input = inputs.at(0); + auto input_shape = input->shape(); + std::vector slice_begin(GetBegin().begin(), GetBegin().end()); + std::vector slice_size(GetSize().begin(), GetSize().end()); + std::vector output_shape(input_shape.size()); + for (int i = 0; i < input_shape.size(); ++i) { + if (slice_size[i] < 0 && slice_size[i] != -1) { + MS_LOG(ERROR) << "Invalid size input!size[" << i << "]=" << slice_size[i]; + return 1; + } + if (slice_begin[i] < 0) { + MS_LOG(ERROR) << "Invalid begin input " << slice_begin[i] << " which should be >= 0"; + return 1; + } + if (input_shape[i] <= slice_begin[i]) { + MS_LOG(ERROR) << "Invalid begin input!begin[" << i << "]=" << slice_begin[i] + << " which should be <= " << input_shape[i]; + return 1; + } + if (slice_size[i] > (input_shape[i] - slice_begin[i])) { + MS_LOG(ERROR) << "Invalid size input " << slice_size[i] + << " which should be <= " << input_shape[i] - slice_begin[i]; + return 1; + } + + output_shape[i] = slice_size[i] < 0 ? input_shape[i] - slice_begin[i] : slice_size[i]; + } + + outputs[0]->set_shape(output_shape); + outputs[0]->set_data_type(input->data_type()); + outputs[0]->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/slice.h b/mindspore/lite/c_ops/slice.h new file mode 100644 index 0000000000..7ac75f8d12 --- /dev/null +++ b/mindspore/lite/c_ops/slice.h @@ -0,0 +1,49 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_SLICE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_SLICE_H_ + +namespace mindspore { +class SliceOp : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit SliceOp(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit SliceOp(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetFormat() const; + std::vector GetBegin() const; + std::vector GetSize() const; + void SetFormat(int format); + void SetBegin(const std::vector &begin); + void SetSize(const std::vector &size); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_SLICE_H_ diff --git a/mindspore/lite/c_ops/softmax.cc b/mindspore/lite/c_ops/softmax.cc new file mode 100644 index 0000000000..68d3baaedf --- /dev/null +++ b/mindspore/lite/c_ops/softmax.cc @@ -0,0 +1,43 @@ +/** + * 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 "c_ops/softmax.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int SoftMax::GetAxis() const { return this->primitive->value.AsSoftMax()->axis; } + +void SoftMax::SetAxis(int axis) { this->primitive->value.AsSoftMax()->axis = axis; } + +#else + +int SoftMax::GetAxis() const { return this->primitive->value_as_SoftMax()->axis(); } + +void SoftMax::SetAxis(int axis) {} +#endif +int SoftMax::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + output->set_shape(input->shape()); + output->set_data_type(input->data_type()); + output->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/softmax.h b/mindspore/lite/c_ops/softmax.h new file mode 100644 index 0000000000..e1bb81838f --- /dev/null +++ b/mindspore/lite/c_ops/softmax.h @@ -0,0 +1,45 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_SOFT_MAX_H_ +#define LITE_MINDSPORE_LITE_C_OPS_SOFT_MAX_H_ + +namespace mindspore { +class SoftMax : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit SoftMax(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit SoftMax(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetAxis() const; + void SetAxis(int axis); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_SOFT_MAX_H_ diff --git a/mindspore/lite/c_ops/softmax_cross_entropy.cc b/mindspore/lite/c_ops/softmax_cross_entropy.cc new file mode 100644 index 0000000000..53827142f4 --- /dev/null +++ b/mindspore/lite/c_ops/softmax_cross_entropy.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 "c_ops/softmax_cross_entropy.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector SoftmaxCrossEntropy::GetAxis() const { return this->primitive->value.AsSoftmaxCrossEntropy()->axis; } + +void SoftmaxCrossEntropy::SetAxis(const std::vector &axis) { + this->primitive->value.AsSoftmaxCrossEntropy()->axis = axis; +} + +#else + +std::vector SoftmaxCrossEntropy::GetAxis() const { + auto fb_vector = this->primitive->value_as_SoftmaxCrossEntropy()->axis(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void SoftmaxCrossEntropy::SetAxis(const std::vector &axis) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/softmax_cross_entropy.h b/mindspore/lite/c_ops/softmax_cross_entropy.h new file mode 100644 index 0000000000..327591692f --- /dev/null +++ b/mindspore/lite/c_ops/softmax_cross_entropy.h @@ -0,0 +1,44 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_SOFTMAX_CROSS_ENTROPY_H_ +#define LITE_MINDSPORE_LITE_C_OPS_SOFTMAX_CROSS_ENTROPY_H_ + +namespace mindspore { +class SoftmaxCrossEntropy : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit SoftmaxCrossEntropy(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit SoftmaxCrossEntropy(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + std::vector GetAxis() const; + void SetAxis(const std::vector &axis); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_SOFTMAX_CROSS_ENTROPY_H_ diff --git a/mindspore/lite/c_ops/space_to_batch.cc b/mindspore/lite/c_ops/space_to_batch.cc new file mode 100644 index 0000000000..d49b867f17 --- /dev/null +++ b/mindspore/lite/c_ops/space_to_batch.cc @@ -0,0 +1,110 @@ +/** + * 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 "c_ops/space_to_batch.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector SpaceToBatch::GetBlockShape() const { return this->primitive->value.AsSpaceToBatch()->blockShape; } +std::vector SpaceToBatch::GetPaddings() const { return this->primitive->value.AsSpaceToBatch()->paddings; } + +void SpaceToBatch::SetBlockShape(const std::vector &block_shape) { + this->primitive->value.AsSpaceToBatch()->blockShape = block_shape; +} +void SpaceToBatch::SetPaddings(const std::vector &paddings) { + this->primitive->value.AsSpaceToBatch()->paddings = paddings; +} + +#else + +std::vector SpaceToBatch::GetBlockShape() const { + auto fb_vector = this->primitive->value_as_SpaceToBatch()->blockShape(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +std::vector SpaceToBatch::GetPaddings() const { + auto fb_vector = this->primitive->value_as_SpaceToBatch()->paddings(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void SpaceToBatch::SetBlockShape(const std::vector &block_shape) {} +void SpaceToBatch::SetPaddings(const std::vector &paddings) {} +#endif +namespace { +constexpr int kSpaceToBatchNDOutputNum = 1; +constexpr int kSpaceToBatchNDInputNum = 1; +constexpr int kBlockSizesSize = 2; +constexpr int kPaddingsSize = 4; +} // namespace + +int SpaceToBatch::InferShape(std::vector inputs, std::vector outputs) { + MS_ASSERT(this->primitive != nullptr); + if (outputs.size() != kSpaceToBatchNDOutputNum || inputs.size() != kSpaceToBatchNDInputNum) { + MS_LOG(ERROR) << "Invalid output/input size! output size: " << outputs.size() << ",input size: " << inputs.size(); + return 1; + } + + auto input = inputs.at(0); + if (input->GetFormat() != schema::Format_NHWC) { + MS_LOG(ERROR) << "space_to_batch only support NHWC now!"; + return 1; + } + auto input_shape = input->shape(); + if (input_shape.size() != kDimension_4d) { + MS_LOG(ERROR) << "input shape dimension size should == " << kDimension_4d; + return 1; + } + + if (GetBlockShape().size() != kBlockSizesSize) { + MS_LOG(ERROR) << "Block shape size should be " << kBlockSizesSize; + return 1; + } + if (GetPaddings().size() != kPaddingsSize) { + MS_LOG(ERROR) << "Crops size should be " << kPaddingsSize; + return 1; + } + + for (int &iter : GetBlockShape()) { + block_sizes_.emplace_back(iter); + } + + in_shape_.clear(); + padded_in_shape_.clear(); + paddings_.clear(); + in_shape_.emplace_back(input_shape.at(NHWC_N)); + padded_in_shape_.emplace_back(input_shape.at(NHWC_N)); + for (int i = 0; i < kBlockSizesSize; i++) { + in_shape_.emplace_back(input_shape.at(i + 1)); + padded_in_shape_.emplace_back(input_shape.at(i + 1) + (paddings_.at(2 * i) + paddings_.at(2 * i + 1))); + paddings_.emplace_back(paddings_.at(2 * i)); + paddings_.emplace_back(paddings_.at(2 * i + 1)); + if (paddings_.back() % block_sizes_.at(i)) { + MS_LOG(ERROR) << "Padded shape does not divide block size " << block_sizes_.at(i); + return 1; + } + } + in_shape_.emplace_back(input_shape.at(NHWC_C)); + padded_in_shape_.emplace_back(input_shape.at(NHWC_C)); + + std::vector output_shape(input_shape.size()); + output_shape[NHWC_N] = input_shape[NHWC_N] * (block_sizes_[NHWC_N] * block_sizes_[NHWC_H]); + output_shape[NHWC_H] = input_shape[NHWC_H] / block_sizes_[NHWC_N]; + output_shape[NHWC_W] = input_shape[NHWC_W] / block_sizes_[NHWC_H]; + output_shape[NHWC_C] = input_shape[NHWC_C]; + outputs[0]->set_shape(output_shape); + outputs[0]->set_data_type(input->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/space_to_batch.h b/mindspore/lite/c_ops/space_to_batch.h new file mode 100644 index 0000000000..0f5d7ee45d --- /dev/null +++ b/mindspore/lite/c_ops/space_to_batch.h @@ -0,0 +1,58 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_SPACE_TO_BATCH_H_ +#define LITE_MINDSPORE_LITE_C_OPS_SPACE_TO_BATCH_H_ + +namespace mindspore { +class SpaceToBatch : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit SpaceToBatch(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit SpaceToBatch(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + std::vector GetBlockShape() const; + std::vector GetPaddings() const; + void SetBlockShape(const std::vector &block_shape); + void SetPaddings(const std::vector &paddings); + + std::vector BlockSizes() { return block_sizes_; } + std::vector Paddings() { return block_sizes_; } + std::vector InShape() { return block_sizes_; } + std::vector PaddedInShape() { return block_sizes_; } + + private: + std::vector block_sizes_; + std::vector paddings_; + std::vector in_shape_; + std::vector padded_in_shape_; +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_SPACE_TO_BATCH_H_ diff --git a/mindspore/lite/c_ops/space_to_batch_nd.cc b/mindspore/lite/c_ops/space_to_batch_nd.cc new file mode 100644 index 0000000000..02e4231786 --- /dev/null +++ b/mindspore/lite/c_ops/space_to_batch_nd.cc @@ -0,0 +1,45 @@ +/** + * 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 "c_ops/space_to_batch_nd.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector SpaceToBatchND::GetBlockShape() const { return this->primitive->value.AsSpaceToBatchND()->blockShape; } +std::vector SpaceToBatchND::GetPaddings() const { return this->primitive->value.AsSpaceToBatchND()->paddings; } + +void SpaceToBatchND::SetBlockShape(const std::vector &block_shape) { + this->primitive->value.AsSpaceToBatchND()->blockShape = block_shape; +} +void SpaceToBatchND::SetPaddings(const std::vector &paddings) { + this->primitive->value.AsSpaceToBatchND()->paddings = paddings; +} + +#else + +std::vector SpaceToBatchND::GetBlockShape() const { + auto fb_vector = this->primitive->value_as_SpaceToBatchND()->blockShape(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +std::vector SpaceToBatchND::GetPaddings() const { + auto fb_vector = this->primitive->value_as_SpaceToBatchND()->paddings(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void SpaceToBatchND::SetBlockShape(const std::vector &block_shape) {} +void SpaceToBatchND::SetPaddings(const std::vector &paddings) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/space_to_batch_nd.h b/mindspore/lite/c_ops/space_to_batch_nd.h new file mode 100644 index 0000000000..e99ea2954e --- /dev/null +++ b/mindspore/lite/c_ops/space_to_batch_nd.h @@ -0,0 +1,46 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_SPACE_TO_BATCH_N_D_H_ +#define LITE_MINDSPORE_LITE_C_OPS_SPACE_TO_BATCH_N_D_H_ + +namespace mindspore { +class SpaceToBatchND : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit SpaceToBatchND(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit SpaceToBatchND(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + std::vector GetBlockShape() const; + std::vector GetPaddings() const; + void SetBlockShape(const std::vector &block_shape); + void SetPaddings(const std::vector &paddings); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_SPACE_TO_BATCH_N_D_H_ diff --git a/mindspore/lite/c_ops/space_to_depth.cc b/mindspore/lite/c_ops/space_to_depth.cc new file mode 100644 index 0000000000..7e65b0c569 --- /dev/null +++ b/mindspore/lite/c_ops/space_to_depth.cc @@ -0,0 +1,73 @@ +/** + * 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 "c_ops/space_to_depth.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int SpaceToDepth::GetBlockSize() const { return this->primitive->value.AsSpaceToDepth()->blockSize; } +int SpaceToDepth::GetFormat() const { return this->primitive->value.AsSpaceToDepth()->format; } + +void SpaceToDepth::SetBlockSize(int block_size) { this->primitive->value.AsSpaceToDepth()->blockSize = block_size; } +void SpaceToDepth::SetFormat(int format) { this->primitive->value.AsSpaceToDepth()->format = format; } + +#else + +int SpaceToDepth::GetBlockSize() const { return this->primitive->value_as_SpaceToDepth()->blockSize(); } +int SpaceToDepth::GetFormat() const { return this->primitive->value_as_SpaceToDepth()->format(); } + +void SpaceToDepth::SetBlockSize(int block_size) {} +void SpaceToDepth::SetFormat(int format) {} +#endif +namespace { +constexpr int kSpaceToDepthOutputNum = 1; +constexpr int kSpaceToDepthInputNum = 1; +} // namespace + +int SpaceToDepth::InferShape(std::vector inputs, std::vector outputs) { + MS_ASSERT(this->primitive != nullptr); + if (outputs.size() != kSpaceToDepthOutputNum || inputs.size() != kSpaceToDepthInputNum) { + MS_LOG(ERROR) << "Invalid output/input size! output size: " << outputs.size() << ",input size: " << inputs.size(); + return 1; + } + + auto input = inputs.at(0); + if (input->GetFormat() != schema::Format_NHWC) { + MS_LOG(ERROR) << "space_to_depth only support NHWC now!"; + return 1; + } + auto input_shape = input->shape(); + if (input_shape.size() != kDimension_4d) { + MS_LOG(ERROR) << "input shape dimension size should == " << kDimension_4d; + return 1; + } + + int32_t block_size = GetBlockSize(); + if (input_shape[NHWC_C] % (block_size * block_size) != 0 || input_shape[NHWC_C] == 0) { + MS_LOG(ERROR) << "input dimension c size " << input_shape[NHWC_C] << " should be mulitple of block_size(" + << block_size << ") * block_size)!"; + return 1; + } + std::vector output_shape(input_shape.size()); + output_shape[NHWC_N] = input_shape[NHWC_N]; + output_shape[NHWC_H] = input_shape[NHWC_H] / block_size; + output_shape[NHWC_W] = input_shape[NHWC_W] / block_size; + output_shape[NHWC_C] = input_shape[NHWC_C] * (block_size * block_size); + outputs[0]->set_shape(output_shape); + outputs[0]->set_data_type(input->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/space_to_depth.h b/mindspore/lite/c_ops/space_to_depth.h new file mode 100644 index 0000000000..eff8c58ce8 --- /dev/null +++ b/mindspore/lite/c_ops/space_to_depth.h @@ -0,0 +1,47 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_SPACE_TO_DEPTH_H_ +#define LITE_MINDSPORE_LITE_C_OPS_SPACE_TO_DEPTH_H_ + +namespace mindspore { +class SpaceToDepth : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit SpaceToDepth(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit SpaceToDepth(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetBlockSize() const; + int GetFormat() const; + void SetBlockSize(int block_size); + void SetFormat(int format); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_SPACE_TO_DEPTH_H_ diff --git a/mindspore/lite/c_ops/sparse_to_dense.cc b/mindspore/lite/c_ops/sparse_to_dense.cc new file mode 100644 index 0000000000..b26c63f584 --- /dev/null +++ b/mindspore/lite/c_ops/sparse_to_dense.cc @@ -0,0 +1,62 @@ +/** + * 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 "c_ops/sparse_to_dense.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector SparseToDense::GetOutputShape() const { return this->primitive->value.AsSparseToDense()->outputShape; } +std::vector SparseToDense::GetSparseValue() const { return this->primitive->value.AsSparseToDense()->sparseValue; } +std::vector SparseToDense::GetDefaultValue() const { + return this->primitive->value.AsSparseToDense()->defaultValue; +} +bool SparseToDense::GetValidateIndices() const { return this->primitive->value.AsSparseToDense()->validateIndices; } + +void SparseToDense::SetOutputShape(const std::vector &output_shape) { + this->primitive->value.AsSparseToDense()->outputShape = output_shape; +} +void SparseToDense::SetSparseValue(const std::vector &sparse_value) { + this->primitive->value.AsSparseToDense()->sparseValue = sparse_value; +} +void SparseToDense::SetDefaultValue(const std::vector &default_value) { + this->primitive->value.AsSparseToDense()->defaultValue = default_value; +} +void SparseToDense::SetValidateIndices(bool validate_indices) { + this->primitive->value.AsSparseToDense()->validateIndices = validate_indices; +} + +#else + +std::vector SparseToDense::GetOutputShape() const { + auto fb_vector = this->primitive->value_as_SparseToDense()->outputShape(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +std::vector SparseToDense::GetSparseValue() const { + auto fb_vector = this->primitive->value_as_SparseToDense()->sparseValue(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +std::vector SparseToDense::GetDefaultValue() const { + auto fb_vector = this->primitive->value_as_SparseToDense()->defaultValue(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +bool SparseToDense::GetValidateIndices() const { return this->primitive->value_as_SparseToDense()->validateIndices(); } + +void SparseToDense::SetOutputShape(const std::vector &output_shape) {} +void SparseToDense::SetSparseValue(const std::vector &sparse_value) {} +void SparseToDense::SetDefaultValue(const std::vector &default_value) {} +void SparseToDense::SetValidateIndices(bool validate_indices) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/sparse_to_dense.h b/mindspore/lite/c_ops/sparse_to_dense.h new file mode 100644 index 0000000000..e3705cf73c --- /dev/null +++ b/mindspore/lite/c_ops/sparse_to_dense.h @@ -0,0 +1,50 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_SPARSE_TO_DENSE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_SPARSE_TO_DENSE_H_ + +namespace mindspore { +class SparseToDense : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit SparseToDense(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit SparseToDense(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + std::vector GetOutputShape() const; + std::vector GetSparseValue() const; + std::vector GetDefaultValue() const; + bool GetValidateIndices() const; + void SetOutputShape(const std::vector &output_shape); + void SetSparseValue(const std::vector &sparse_value); + void SetDefaultValue(const std::vector &default_value); + void SetValidateIndices(bool validate_indices); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_SPARSE_TO_DENSE_H_ diff --git a/mindspore/lite/c_ops/split.cc b/mindspore/lite/c_ops/split.cc new file mode 100644 index 0000000000..5f59a80967 --- /dev/null +++ b/mindspore/lite/c_ops/split.cc @@ -0,0 +1,83 @@ +/** + * 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 "c_ops/split.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Split::GetNumberSplit() const { return this->primitive->value.AsSplit()->numberSplit; } +std::vector Split::GetSizeSplits() const { return this->primitive->value.AsSplit()->sizeSplits; } +int Split::GetSplitDim() const { return this->primitive->value.AsSplit()->splitDim; } + +void Split::SetNumberSplit(int number_split) { this->primitive->value.AsSplit()->numberSplit = number_split; } +void Split::SetSizeSplits(const std::vector &size_splits) { + this->primitive->value.AsSplit()->sizeSplits = size_splits; +} +void Split::SetSplitDim(int split_dim) { this->primitive->value.AsSplit()->splitDim = split_dim; } + +#else + +int Split::GetNumberSplit() const { return this->primitive->value_as_Split()->numberSplit(); } +std::vector Split::GetSizeSplits() const { + auto fb_vector = this->primitive->value_as_Split()->sizeSplits(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +int Split::GetSplitDim() const { return this->primitive->value_as_Split()->splitDim(); } + +void Split::SetNumberSplit(int number_split) {} +void Split::SetSizeSplits(const std::vector &size_splits) {} +void Split::SetSplitDim(int split_dim) {} +#endif +namespace { +constexpr int kSplitInputNum = 1; +} // namespace +int Split::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + + MS_ASSERT(spilt_prim != nullptr); + if (inputs_.size() != kSplitInputNum) { + MS_LOG(ERROR) << "inputs number is not equal to " << kSplitInputNum; + return 1; + } + auto output = outputs_.front(); + if (output == nullptr) { + MS_LOG(ERROR) << "output null pointer dereferencing."; + return 1; + } + int number_split = GetNumberSplit(); + if (outputs_.size() != number_split) { + MS_LOG(ERROR) << "outputs number is not equal to " << number_split; + return 1; + } + int split_dim = GetSplitDim(); + std::vector input_shape = input->shape(); + std::vector size_split; + size_split.insert(size_split.begin(), GetSizeSplits().begin(), GetSizeSplits().end()); + + for (int i = 0; i < number_split; ++i) { + std::vector output_shape; + output_shape.insert(output_shape.begin(), input_shape.begin(), input_shape.end()); + auto split_dim_i = size_split.empty() ? input_shape[split_dim] / number_split : size_split[i]; + output_shape[split_dim] = split_dim_i; + outputs_[i]->set_shape(output_shape); + outputs_[i]->set_data_type(input->data_type()); + outputs_[i]->SetFormat(input->GetFormat()); + } + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/split.h b/mindspore/lite/c_ops/split.h new file mode 100644 index 0000000000..a206adbe65 --- /dev/null +++ b/mindspore/lite/c_ops/split.h @@ -0,0 +1,49 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_SPLIT_H_ +#define LITE_MINDSPORE_LITE_C_OPS_SPLIT_H_ + +namespace mindspore { +class Split : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Split(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Split(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetNumberSplit() const; + std::vector GetSizeSplits() const; + int GetSplitDim() const; + void SetNumberSplit(int number_split); + void SetSizeSplits(const std::vector &size_splits); + void SetSplitDim(int split_dim); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_SPLIT_H_ diff --git a/mindspore/lite/c_ops/sqrt.h b/mindspore/lite/c_ops/sqrt.h new file mode 100644 index 0000000000..ffb1f11e0e --- /dev/null +++ b/mindspore/lite/c_ops/sqrt.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic_self.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_SQRT_H_ +#define LITE_MINDSPORE_LITE_C_OPS_SQRT_H_ + +namespace mindspore { +class Sqrt : public ArithmeticSelf { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Sqrt(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} +#else + explicit Sqrt(schema::Primitive *primitive) : ArithmeticSelf(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_SQRT_H_ diff --git a/mindspore/lite/c_ops/square.h b/mindspore/lite/c_ops/square.h new file mode 100644 index 0000000000..0e2953295b --- /dev/null +++ b/mindspore/lite/c_ops/square.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic_self.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_SQUARE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_SQUARE_H_ + +namespace mindspore { +class Square : public ArithmeticSelf { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Square(schema::PrimitiveT *primitive) : ArithmeticSelf(primitive) {} +#else + explicit Square(schema::Primitive *primitive) : ArithmeticSelf(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_SQUARE_H_ diff --git a/mindspore/lite/c_ops/squared_difference.h b/mindspore/lite/c_ops/squared_difference.h new file mode 100644 index 0000000000..6c2198e63d --- /dev/null +++ b/mindspore/lite/c_ops/squared_difference.h @@ -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 +#include +#include +#include "ir/dtype/type_id.h" +#include "c_ops/arithmetic.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_SQUARED_DIFFERENCE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_SQUARED_DIFFERENCE_H_ + +namespace mindspore { +class SquaredDifference : public Arithmetic { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit SquaredDifference(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} +#else + explicit SquaredDifference(schema::Primitive *primitive) : Arithmetic(primitive) {} +#endif +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_SQUARED_DIFFERENCE_H_ diff --git a/mindspore/lite/c_ops/squeeze.cc b/mindspore/lite/c_ops/squeeze.cc new file mode 100644 index 0000000000..bced5e173c --- /dev/null +++ b/mindspore/lite/c_ops/squeeze.cc @@ -0,0 +1,84 @@ +/** + * 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 "c_ops/squeeze.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector Squeeze::GetAxis() const { return this->primitive->value.AsSqueeze()->axis; } + +void Squeeze::SetAxis(const std::vector &axis) { this->primitive->value.AsSqueeze()->axis = axis; } + +#else + +std::vector Squeeze::GetAxis() const { + auto fb_vector = this->primitive->value_as_Squeeze()->axis(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void Squeeze::SetAxis(const std::vector &axis) {} +#endif +namespace { +constexpr int kSqueezeInputNum = 1; +constexpr int kSqueezeOutputNum = 1; +} // namespace +int Squeeze::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + if (kSqueezeInputNum != inputs_.size()) { + MS_LOG(ERROR) << "Add should has " << kSqueezeInputNum << " inputs"; + return -1; + } + if (kSqueezeOutputNum != outputs_.size()) { + MS_LOG(ERROR) << "Add should has " << kSqueezeOutputNum << " outputs"; + return -1; + } + auto *in_tensor = inputs_.front(); + auto in_shape = in_tensor->shape(); + std::vector out_shape; + + // todo: getAxis + auto axis = GetAxis(); + std::vector axes_; + for (auto iter = axis.begin(); iter != axis.end(); iter++) { + axes_.push_back(*iter); + } + + if (axes_.size() == 0) { + for (int i = 0; i < in_shape.size(); i++) { + if (in_shape[i] != 1) { + out_shape.push_back(in_shape[i]); + } + } + } else { + int axisIdx = 0; + for (int i = 0; i < in_shape.size(); i++) { + if (axisIdx < axes_.size() && axes_[axisIdx] == i) { + MS_ASSERT(in_shape[i] == 1); + axisIdx++; + continue; + } else { + out_shape.push_back(in_shape[i]); + } + } + } + + outputs_.front()->set_shape(out_shape); + outputs_.front()->set_data_type(in_tensor->data_type()); + outputs_.front()->SetFormat(in_tensor->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/squeeze.h b/mindspore/lite/c_ops/squeeze.h new file mode 100644 index 0000000000..f2397d94d4 --- /dev/null +++ b/mindspore/lite/c_ops/squeeze.h @@ -0,0 +1,45 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_SQUEEZE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_SQUEEZE_H_ + +namespace mindspore { +class Squeeze : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Squeeze(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Squeeze(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + std::vector GetAxis() const; + void SetAxis(const std::vector &axis); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_SQUEEZE_H_ diff --git a/mindspore/lite/c_ops/stack.cc b/mindspore/lite/c_ops/stack.cc new file mode 100644 index 0000000000..417d368588 --- /dev/null +++ b/mindspore/lite/c_ops/stack.cc @@ -0,0 +1,93 @@ +/** + * 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 "c_ops/stack.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Stack::GetAxis() const { return this->primitive->value.AsStack()->axis; } +int Stack::GetN() const { return this->primitive->value.AsStack()->n; } +std::vector Stack::GetIsScale() const { return this->primitive->value.AsStack()->isScale; } + +void Stack::SetAxis(int axis) { this->primitive->value.AsStack()->axis = axis; } +void Stack::SetN(int n) { this->primitive->value.AsStack()->n = n; } +void Stack::SetIsScale(const std::vector &is_scale) { this->primitive->value.AsStack()->isScale = is_scale; } + +#else + +int Stack::GetAxis() const { return this->primitive->value_as_Stack()->axis(); } +int Stack::GetN() const { return this->primitive->value_as_Stack()->n(); } +std::vector Stack::GetIsScale() const { + auto fb_vector = this->primitive->value_as_Stack()->isScale(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void Stack::SetAxis(int axis) {} +void Stack::SetN(int n) {} +void Stack::SetIsScale(const std::vector &is_scale) {} +#endif +namespace { +constexpr int kStackOutputNum = 1; +constexpr int kStackMinInputNum = 2; +} // namespace + +int Stack::InferShape(std::vector inputs, std::vector outputs) { + MS_ASSERT(this->primitive != nullptr); + if (outputs.size() != kStackOutputNum) { + MS_LOG(ERROR) << "Invalid output size:" << outputs.size(); + return 1; + } + if (inputs.size() < kStackMinInputNum) { + MS_LOG(ERROR) << "Invalid input size " << inputs.size(); + return 1; + } + auto input = inputs.at(0); + auto input_shape = input->shape(); + + std::vector output_shape = input_shape; + int axis = GetAxis() < 0 ? GetAxis() + input_shape.size() : GetAxis(); + if (axis < 0 || axis > input_shape.size()) { + MS_LOG(ERROR) << "Invalid axis " << GetAxis(); + return 1; + } + schema::Format input0_format = input->GetFormat(); + for (size_t i = 1; i < inputs.size(); ++i) { + if (inputs[i]->GetFormat() != input0_format) { + MS_LOG(ERROR) << "All inputs should have the same format!"; + return 1; + } + + auto input_shape_tmp = inputs[i]->shape(); + if (input_shape_tmp.size() != input_shape.size()) { + MS_LOG(ERROR) << "All input shape size should be the same!"; + return 1; + } + for (size_t j = 0; j < input_shape.size(); ++j) { + if (input_shape_tmp[j] != input_shape[j]) { + MS_LOG(ERROR) << "All input shape should be the same!"; + return 1; + } + } + } + + output_shape.insert(output_shape.begin() + axis, inputs.size()); + outputs[0]->set_shape(output_shape); + outputs[0]->set_data_type(input->data_type()); + outputs[0]->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/stack.h b/mindspore/lite/c_ops/stack.h new file mode 100644 index 0000000000..5bc06c2002 --- /dev/null +++ b/mindspore/lite/c_ops/stack.h @@ -0,0 +1,49 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_STACK_H_ +#define LITE_MINDSPORE_LITE_C_OPS_STACK_H_ + +namespace mindspore { +class Stack : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Stack(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Stack(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetAxis() const; + int GetN() const; + std::vector GetIsScale() const; + void SetAxis(int axis); + void SetN(int n); + void SetIsScale(const std::vector &is_scale); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_STACK_H_ diff --git a/mindspore/lite/c_ops/strided_slice.cc b/mindspore/lite/c_ops/strided_slice.cc new file mode 100644 index 0000000000..112f037782 --- /dev/null +++ b/mindspore/lite/c_ops/strided_slice.cc @@ -0,0 +1,221 @@ +/** + * 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 "c_ops/strided_slice.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int StridedSlice::GetBeginMask() const { return this->primitive->value.AsStridedSlice()->beginMask; } +int StridedSlice::GetEndMask() const { return this->primitive->value.AsStridedSlice()->endMask; } +int StridedSlice::GetEllipsisMask() const { return this->primitive->value.AsStridedSlice()->ellipsisMask; } +int StridedSlice::GetNewAxisMask() const { return this->primitive->value.AsStridedSlice()->newAxisMask; } +int StridedSlice::GetShrinkAxisMask() const { return this->primitive->value.AsStridedSlice()->shrinkAxisMask; } +std::vector StridedSlice::GetBegin() const { return this->primitive->value.AsStridedSlice()->begin; } +std::vector StridedSlice::GetEnd() const { return this->primitive->value.AsStridedSlice()->end; } +std::vector StridedSlice::GetStride() const { return this->primitive->value.AsStridedSlice()->stride; } +std::vector StridedSlice::GetIsScale() const { return this->primitive->value.AsStridedSlice()->isScale; } + +void StridedSlice::SetBeginMask(int begin_mask) { this->primitive->value.AsStridedSlice()->beginMask = begin_mask; } +void StridedSlice::SetEndMask(int end_mask) { this->primitive->value.AsStridedSlice()->endMask = end_mask; } +void StridedSlice::SetEllipsisMask(int ellipsis_mask) { + this->primitive->value.AsStridedSlice()->ellipsisMask = ellipsis_mask; +} +void StridedSlice::SetNewAxisMask(int new_axis_mask) { + this->primitive->value.AsStridedSlice()->newAxisMask = new_axis_mask; +} +void StridedSlice::SetShrinkAxisMask(int shrink_axis_mask) { + this->primitive->value.AsStridedSlice()->shrinkAxisMask = shrink_axis_mask; +} +void StridedSlice::SetBegin(const std::vector &begin) { this->primitive->value.AsStridedSlice()->begin = begin; } +void StridedSlice::SetEnd(const std::vector &end) { this->primitive->value.AsStridedSlice()->end = end; } +void StridedSlice::SetStride(const std::vector &stride) { + this->primitive->value.AsStridedSlice()->stride = stride; +} +void StridedSlice::SetIsScale(const std::vector &is_scale) { + this->primitive->value.AsStridedSlice()->isScale = is_scale; +} + +#else + +int StridedSlice::GetBeginMask() const { return this->primitive->value_as_StridedSlice()->beginMask(); } +int StridedSlice::GetEndMask() const { return this->primitive->value_as_StridedSlice()->endMask(); } +int StridedSlice::GetEllipsisMask() const { return this->primitive->value_as_StridedSlice()->ellipsisMask(); } +int StridedSlice::GetNewAxisMask() const { return this->primitive->value_as_StridedSlice()->newAxisMask(); } +int StridedSlice::GetShrinkAxisMask() const { return this->primitive->value_as_StridedSlice()->shrinkAxisMask(); } +std::vector StridedSlice::GetBegin() const { + auto fb_vector = this->primitive->value_as_StridedSlice()->begin(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +std::vector StridedSlice::GetEnd() const { + auto fb_vector = this->primitive->value_as_StridedSlice()->end(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +std::vector StridedSlice::GetStride() const { + auto fb_vector = this->primitive->value_as_StridedSlice()->stride(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +std::vector StridedSlice::GetIsScale() const { + auto fb_vector = this->primitive->value_as_StridedSlice()->isScale(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void StridedSlice::SetBeginMask(int begin_mask) {} +void StridedSlice::SetEndMask(int end_mask) {} +void StridedSlice::SetEllipsisMask(int ellipsis_mask) {} +void StridedSlice::SetNewAxisMask(int new_axis_mask) {} +void StridedSlice::SetShrinkAxisMask(int shrink_axis_mask) {} +void StridedSlice::SetBegin(const std::vector &begin) {} +void StridedSlice::SetEnd(const std::vector &end) {} +void StridedSlice::SetStride(const std::vector &stride) {} +void StridedSlice::SetIsScale(const std::vector &is_scale) {} +#endif +namespace { +constexpr int kStridedSliceOutputNum = 1; +constexpr int kStridedSliceInputNum = 1; +} // namespace + +void StridedSlice::ApplyNewAxisMask() { + for (int i = 0; i < new_axis_mask_.size(); i++) { + if (new_axis_mask_.at(i)) { + ndim_ += 1; + in_shape_.insert(in_shape_.begin() + i, 1); + begins_.at(i) = 0; + ends_.at(i) = 1; + strides_.at(i) = 1; + + begins_.emplace_back(0); + ends_.emplace_back(in_shape_.at(ndim_ - 1)); + strides_.emplace_back(1); + + begins_mask_.at(i) = false; + ends_mask_.at(i) = false; + ellipsis_mask_.at(i) = false; + shrink_axis_mask_.at(i) = false; + } + } +} + +std::vector StridedSlice::ApplyShrinkMask(std::vector out_shape) { + auto old_out_shape = out_shape; + out_shape.clear(); + for (int i = 0; i < shrink_axis_mask_.size(); i++) { + if (shrink_axis_mask_.at(i)) { + ends_.at(i) = begins_.at(i) + 1; + strides_.at(i) = 1; + } else { + out_shape.emplace_back(old_out_shape.at(i)); + } + } + for (int i = shrink_axis_mask_.size(); i < old_out_shape.size(); i++) { + out_shape.emplace_back(old_out_shape.at(i)); + } + return out_shape; +} + +/*only one bit will be used if multiple bits are true.*/ +void StridedSlice::ApplyEllipsisMask() { + for (int i = 0; i < ellipsis_mask_.size(); i++) { + if (ellipsis_mask_.at(i)) { + begins_.at(i) = 0; + ends_.at(i) = in_shape_.at(i); + break; + } + } +} + +void StridedSlice::ApplyBeginMask() { + for (int i = 0; i < ndim_; i++) { + if (begins_mask_.at(i)) { + begins_.at(i) = 0; + } + } +} + +void StridedSlice::ApplyEndMask() { + for (int i = 0; i < ndim_; i++) { + if (ends_mask_.at(i)) { + ends_.at(i) = in_shape_.at(i); + } + } +} + +int StridedSlice::InferShape(std::vector inputs, std::vector outputs) { + MS_ASSERT(this->primitive != nullptr); + if (outputs.size() != kStridedSliceOutputNum) { + MS_LOG(ERROR) << "Invalid output size:" << outputs.size(); + return 1; + } + if (inputs.size() != kStridedSliceInputNum) { + MS_LOG(ERROR) << "Invalid input size " << inputs.size(); + return 1; + } + auto input = inputs.at(0); + MS_ASSERT(input != nullptr); + auto input_shape = input->shape(); + std::vector output_shape; + ndim_ = static_cast(GetBegin().size()); + + MS_ASSERT(ndim_ == static_cast(strided_slice_prim->end()->size())); + MS_ASSERT(ndim_ == static_cast(strided_slice_prim->stride()->size())); + MS_ASSERT(ndim_ == static_cast(input_shape.size())); + + for (int i = 0; i < ndim_; i++) { + in_shape_.emplace_back(input_shape.at(i)); + begins_.emplace_back((GetBegin())[i]); + ends_.emplace_back((GetEnd())[i]); + strides_.emplace_back((GetStride())[i]); + } + + // set all mask to original input shape + begins_mask_.resize(ndim_); + ends_mask_.resize(ndim_); + ellipsis_mask_.resize(ndim_); + new_axis_mask_.resize(ndim_); + shrink_axis_mask_.resize(ndim_); + + // convert bit to vector + for (int i = 0; i < ndim_; i++) { + begins_mask_.at(i) = static_cast(GetBeginMask()) & (1 << i); + ends_mask_.at(i) = static_cast(GetEndMask()) & (1 << i); + ellipsis_mask_.at(i) = static_cast(GetEllipsisMask()) & (1 << i); + new_axis_mask_.at(i) = static_cast(GetNewAxisMask()) & (1 << i); + shrink_axis_mask_.at(i) = static_cast(GetShrinkAxisMask()) & (1 << i); + } + + ApplyNewAxisMask(); + ApplyBeginMask(); + ApplyEndMask(); + ApplyEllipsisMask(); + + output_shape.clear(); + output_shape.resize(in_shape_.size()); + for (int i = 0; i < in_shape_.size(); i++) { + if (i < ndim_ && new_axis_mask_.at(i)) { + output_shape.at(i) = 1; + } else { + output_shape.at(i) = (ends_.at(i) - begins_.at(i)) / strides_.at(i); + } + } + + output_shape = ApplyShrinkMask(output_shape); + + outputs.front()->set_shape(output_shape); + outputs.front()->set_data_type(input->data_type()); + outputs[0]->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/strided_slice.h b/mindspore/lite/c_ops/strided_slice.h new file mode 100644 index 0000000000..db7e8d0708 --- /dev/null +++ b/mindspore/lite/c_ops/strided_slice.h @@ -0,0 +1,84 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_STRIDED_SLICE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_STRIDED_SLICE_H_ + +namespace mindspore { +class StridedSlice : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit StridedSlice(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit StridedSlice(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetBeginMask() const; + int GetEndMask() const; + int GetEllipsisMask() const; + int GetNewAxisMask() const; + int GetShrinkAxisMask() const; + std::vector GetBegin() const; + std::vector GetEnd() const; + std::vector GetStride() const; + std::vector GetIsScale() const; + void SetBeginMask(int begin_mask); + void SetEndMask(int end_mask); + void SetEllipsisMask(int ellipsis_mask); + void SetNewAxisMask(int new_axis_mask); + void SetShrinkAxisMask(int shrink_axis_mask); + void SetBegin(const std::vector &begin); + void SetEnd(const std::vector &end); + void SetStride(const std::vector &stride); + void SetIsScale(const std::vector &is_scale); + + int NDims() { return this->ndim_; } + void ApplyNewAxisMask(); + std::vector ApplyShrinkMask(std::vector out_shape); + void ApplyBeginMask(); + void ApplyEndMask(); + void ApplyEllipsisMask(); + std::vector GetInShape() { return this->in_shape_; } + std::vector GetBegins() { return this->begins_; } + std::vector GetEnds() { return this->ends_; } + std::vector GetStrides() { return this->strides_; } + + protected: + int ndim_; + std::vector in_shape_; + std::vector begins_; + std::vector ends_; + std::vector strides_; + std::vector begins_mask_; + std::vector ends_mask_; + std::vector ellipsis_mask_; + std::vector new_axis_mask_; + std::vector shrink_axis_mask_; +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_STRIDED_SLICE_H_ diff --git a/mindspore/lite/c_ops/sub.cc b/mindspore/lite/c_ops/sub.cc new file mode 100644 index 0000000000..330e149cda --- /dev/null +++ b/mindspore/lite/c_ops/sub.cc @@ -0,0 +1,33 @@ +/** + * 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 "c_ops/sub.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Sub::GetActivationType() const { return this->primitive->value.AsSub()->activationType; } + +void Sub::SetActivationType(int activation_type) { + this->primitive->value.AsSub()->activationType = (schema::ActivationType)activation_type; +} + +#else + +int Sub::GetActivationType() const { return this->primitive->value_as_Sub()->activationType(); } + +void Sub::SetActivationType(int activation_type) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/sub.h b/mindspore/lite/c_ops/sub.h new file mode 100644 index 0000000000..f8703cb60c --- /dev/null +++ b/mindspore/lite/c_ops/sub.h @@ -0,0 +1,46 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#include "c_ops/arithmetic.h" + +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_SUB_H_ +#define LITE_MINDSPORE_LITE_C_OPS_SUB_H_ + +namespace mindspore { +class Sub : public Arithmetic { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Sub(schema::PrimitiveT *primitive) : Arithmetic(primitive) {} +#else + explicit Sub(schema::Primitive *primitive) : Arithmetic(primitive) {} +#endif + int GetActivationType() const; + void SetActivationType(int activation_type); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_SUB_H_ diff --git a/mindspore/lite/c_ops/tile.cc b/mindspore/lite/c_ops/tile.cc new file mode 100644 index 0000000000..5cb201497f --- /dev/null +++ b/mindspore/lite/c_ops/tile.cc @@ -0,0 +1,55 @@ +/** + * 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 "c_ops/tile.h" +#include + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector Tile::GetMultiples() const { return this->primitive->value.AsTile()->multiples; } + +void Tile::SetMultiples(const std::vector &multiples) { this->primitive->value.AsTile()->multiples = multiples; } + +#else + +std::vector Tile::GetMultiples() const { + auto fb_vector = this->primitive->value_as_Tile()->multiples(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void Tile::SetMultiples(const std::vector &multiples) {} +#endif +int Tile::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + + std::vector out_shape; + std::vector multiples; + std::copy(GetMultiples().begin(), GetMultiples().end(), std::back_inserter(multiples)); + for (size_t i = 0; i < input->shape().size(); ++i) { + int tmp = input->shape()[i] * multiples[i]; + out_shape.push_back(tmp); + } + + output->SetFormat(input->GetFormat()); + output->set_shape(out_shape); + output->set_data_type(input->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/tile.h b/mindspore/lite/c_ops/tile.h new file mode 100644 index 0000000000..b84e76afa5 --- /dev/null +++ b/mindspore/lite/c_ops/tile.h @@ -0,0 +1,45 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_TILE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_TILE_H_ + +namespace mindspore { +class Tile : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Tile(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Tile(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + std::vector GetMultiples() const; + void SetMultiples(const std::vector &multiples); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_TILE_H_ diff --git a/mindspore/lite/c_ops/topk.cc b/mindspore/lite/c_ops/topk.cc new file mode 100644 index 0000000000..44fe02acac --- /dev/null +++ b/mindspore/lite/c_ops/topk.cc @@ -0,0 +1,62 @@ +/** + * 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 "c_ops/topk.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int TopK::GetK() const { return this->primitive->value.AsTopK()->k; } +bool TopK::GetSorted() const { return this->primitive->value.AsTopK()->sorted; } + +void TopK::SetK(int k) { this->primitive->value.AsTopK()->k = k; } +void TopK::SetSorted(bool sorted) { this->primitive->value.AsTopK()->sorted = sorted; } + +#else + +int TopK::GetK() const { return this->primitive->value_as_TopK()->k(); } +bool TopK::GetSorted() const { return this->primitive->value_as_TopK()->sorted(); } + +void TopK::SetK(int k) {} +void TopK::SetSorted(bool sorted) {} +#endif +int TopK::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + if (inputs_.size() != kSingleNum || outputs_.size() != kDoubleNum) { + MS_LOG(ERROR) << "input size: " << inputs_.size() << ", output size: " << outputs_.size(); + return 1; + } + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output0 = outputs_.front(); + MS_ASSERT(output0 != nullptr); + auto output1 = outputs_.at(1); + MS_ASSERT(output1 != nullptr); + + MS_ASSERT(topk_prim != nullptr); + + auto out_shape = input->shape(); + out_shape[out_shape.size() - 1] = GetK(); + + output0->set_shape(out_shape); + output0->set_data_type(input->data_type()); + output0->SetFormat(input->GetFormat()); + + output1->set_shape(out_shape); + output1->set_data_type(kNumberTypeInt32); + output1->SetFormat(input->GetFormat()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/topk.h b/mindspore/lite/c_ops/topk.h new file mode 100644 index 0000000000..6e08babe58 --- /dev/null +++ b/mindspore/lite/c_ops/topk.h @@ -0,0 +1,47 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_TOP_K_H_ +#define LITE_MINDSPORE_LITE_C_OPS_TOP_K_H_ + +namespace mindspore { +class TopK : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit TopK(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit TopK(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetK() const; + bool GetSorted() const; + void SetK(int k); + void SetSorted(bool sorted); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_TOP_K_H_ diff --git a/mindspore/lite/c_ops/transpose.cc b/mindspore/lite/c_ops/transpose.cc new file mode 100644 index 0000000000..a7d1a028bc --- /dev/null +++ b/mindspore/lite/c_ops/transpose.cc @@ -0,0 +1,69 @@ +/** + * 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 "c_ops/transpose.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector Transpose::GetPerm() const { return this->primitive->value.AsTranspose()->perm; } +bool Transpose::GetConjugate() const { return this->primitive->value.AsTranspose()->conjugate; } + +void Transpose::SetPerm(const std::vector &perm) { this->primitive->value.AsTranspose()->perm = perm; } +void Transpose::SetConjugate(bool conjugate) { this->primitive->value.AsTranspose()->conjugate = conjugate; } + +#else + +std::vector Transpose::GetPerm() const { + auto fb_vector = this->primitive->value_as_Transpose()->perm(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} +bool Transpose::GetConjugate() const { return this->primitive->value_as_Transpose()->conjugate(); } + +void Transpose::SetPerm(const std::vector &perm) {} +void Transpose::SetConjugate(bool conjugate) {} +#endif +int Transpose::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + + MS_ASSERT(inputs_.size() == kSingleNum); + MS_ASSERT(outputs_.size() == kSingleNum); + + int conjugate = GetConjugate(); + if (conjugate) { + MS_LOG(ERROR) << "Transpose conjugate is not support currently"; + return 1; + } + std::vector perm; + perm.insert(perm.begin(), GetPerm().begin(), GetPerm().end()); + + std::vector in_shape = input->shape(); + std::vector out_shape; + out_shape.resize(perm.size()); + for (int i = 0; i < perm.size(); ++i) { + out_shape[i] = in_shape[perm[i]]; + } + + output->set_shape(out_shape); + output->set_data_type(input->data_type()); + output->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/transpose.h b/mindspore/lite/c_ops/transpose.h new file mode 100644 index 0000000000..dc595a16fe --- /dev/null +++ b/mindspore/lite/c_ops/transpose.h @@ -0,0 +1,47 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_TRANSPOSE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_TRANSPOSE_H_ + +namespace mindspore { +class Transpose : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Transpose(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Transpose(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + std::vector GetPerm() const; + bool GetConjugate() const; + void SetPerm(const std::vector &perm); + void SetConjugate(bool conjugate); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_TRANSPOSE_H_ diff --git a/mindspore/lite/c_ops/unique.cc b/mindspore/lite/c_ops/unique.cc new file mode 100644 index 0000000000..58cc3e7135 --- /dev/null +++ b/mindspore/lite/c_ops/unique.cc @@ -0,0 +1,52 @@ +/** + * 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 "c_ops/unique.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Unique::GetOutType() const { return this->primitive->value.AsUnique()->outType; } + +void Unique::SetOutType(int out_type) { this->primitive->value.AsUnique()->outType = out_type; } + +#else + +int Unique::GetOutType() const { return this->primitive->value_as_Unique()->outType(); } + +void Unique::SetOutType(int out_type) {} +#endif +int Unique::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + if (inputs_.size() != kSingleNum || outputs_.size() != kDoubleNum) { + MS_LOG(ERROR) << "input size: " << inputs_.size() << ", output size: " << outputs_.size(); + return 1; + } + auto &input = inputs_.at(0); + MS_ASSERT(input != nullptr); + auto &output0 = outputs_.at(0); + MS_ASSERT(output0 != nullptr); + auto &output1 = outputs_.at(1); + MS_ASSERT(output1 != nullptr); + output0->set_shape(input->shape()); + output0->set_data_type(input->data_type()); + output1->set_shape(input->shape()); + output1->set_data_type(kNumberTypeInt32); + output1->SetFormat(input->GetFormat()); + output0->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/unique.h b/mindspore/lite/c_ops/unique.h new file mode 100644 index 0000000000..96cb0ce584 --- /dev/null +++ b/mindspore/lite/c_ops/unique.h @@ -0,0 +1,45 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_UNIQUE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_UNIQUE_H_ + +namespace mindspore { +class Unique : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Unique(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Unique(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetOutType() const; + void SetOutType(int out_type); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_UNIQUE_H_ diff --git a/mindspore/lite/c_ops/unsqueeze.cc b/mindspore/lite/c_ops/unsqueeze.cc new file mode 100644 index 0000000000..27ad72490c --- /dev/null +++ b/mindspore/lite/c_ops/unsqueeze.cc @@ -0,0 +1,80 @@ +/** + * 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 "c_ops/unsqueeze.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector Unsqueeze::GetAxis() const { return this->primitive->value.AsUnsqueeze()->axis; } + +void Unsqueeze::SetAxis(const std::vector &axis) { this->primitive->value.AsUnsqueeze()->axis = axis; } + +#else +bool predicate(int n) { return n != 1; } +std::vector Unsqueeze::GetAxis() const { + auto fb_vector = this->primitive->value_as_Unsqueeze()->axis(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void Unsqueeze::SetAxis(const std::vector &axis) {} +#endif +int Unsqueeze::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + if (inputs_.size() != kSingleNum) { + MS_LOG(ERROR) << "input size is invalid"; + } + if (outputs_.size() != kSingleNum) { + MS_LOG(ERROR) << "output size is invalid"; + } + + auto dims = GetAxis().data(); + auto in_shape = input->shape(); + auto in_rank = in_shape.size(); + auto dim_rank = GetAxis().size(); + std::vector out_shape; + + if (dim_rank == 0) { + std::copy_if(in_shape.begin(), in_shape.end(), out_shape.begin(), [](int n) -> bool { return n != 1; }); + } else { + auto sz = in_rank + dim_rank; + int in_itr = 0; + int ax_itr = 0; + for (int i = 0; i < sz; i++) { + if (ax_itr < dim_rank && dims[ax_itr] == i) { + out_shape.emplace_back(1); + ax_itr++; + } else if (ax_itr < dim_rank && dims[ax_itr] + sz == i) { + out_shape.emplace_back(1); + ax_itr++; + } else { + if (in_shape[in_itr] > 1) { + out_shape.emplace_back(in_shape[in_itr]); + } + in_itr++; + } + } + } + + output->SetFormat(input->GetFormat()); + output->set_shape(out_shape); + output->set_data_type(input->data_type()); + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/unsqueeze.h b/mindspore/lite/c_ops/unsqueeze.h new file mode 100644 index 0000000000..bfeba00a57 --- /dev/null +++ b/mindspore/lite/c_ops/unsqueeze.h @@ -0,0 +1,45 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_UNSQUEEZE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_UNSQUEEZE_H_ + +namespace mindspore { +class Unsqueeze : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Unsqueeze(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Unsqueeze(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + std::vector GetAxis() const; + void SetAxis(const std::vector &axis); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_UNSQUEEZE_H_ diff --git a/mindspore/lite/c_ops/unstack.cc b/mindspore/lite/c_ops/unstack.cc new file mode 100644 index 0000000000..337586b173 --- /dev/null +++ b/mindspore/lite/c_ops/unstack.cc @@ -0,0 +1,59 @@ +/** + * 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 "c_ops/unstack.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +int Unstack::GetNum() const { return this->primitive->value.AsUnstack()->num; } +int Unstack::GetAxis() const { return this->primitive->value.AsUnstack()->axis; } + +void Unstack::SetNum(int num) { this->primitive->value.AsUnstack()->num = num; } +void Unstack::SetAxis(int axis) { this->primitive->value.AsUnstack()->axis = axis; } + +#else + +int Unstack::GetNum() const { return this->primitive->value_as_Unstack()->num(); } +int Unstack::GetAxis() const { return this->primitive->value_as_Unstack()->axis(); } + +void Unstack::SetNum(int num) {} +void Unstack::SetAxis(int axis) {} +#endif +int Unstack::InferShape(std::vector inputs, std::vector outputs) { + auto input = inputs.at(0); + MS_ASSERT(input != nullptr); + auto input_shape = input->shape(); + int axis = GetAxis() < 0 ? GetAxis() + input_shape.size() : GetAxis(); + if (axis < 0 || axis >= input_shape.size()) { + MS_LOG(ERROR) << "Invalid axis " << GetAxis(); + return 1; + } + + std::vector output_shape; + for (size_t i = 0; i < input_shape.size(); ++i) { + if (i != axis) { + output_shape.push_back(input_shape.at(i)); + } + } + for (auto &out : outputs) { + MS_ASSERT(out != nullptr); + out->set_shape(output_shape); + out->set_data_type(input->data_type()); + out->SetFormat(input->GetFormat()); + } + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/unstack.h b/mindspore/lite/c_ops/unstack.h new file mode 100644 index 0000000000..0db29a7ae6 --- /dev/null +++ b/mindspore/lite/c_ops/unstack.h @@ -0,0 +1,47 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_UNSTACK_H_ +#define LITE_MINDSPORE_LITE_C_OPS_UNSTACK_H_ + +namespace mindspore { +class Unstack : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Unstack(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Unstack(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + int GetNum() const; + int GetAxis() const; + void SetNum(int num); + void SetAxis(int axis); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_UNSTACK_H_ diff --git a/mindspore/lite/c_ops/upsample.cc b/mindspore/lite/c_ops/upsample.cc new file mode 100644 index 0000000000..e7aaac2a79 --- /dev/null +++ b/mindspore/lite/c_ops/upsample.cc @@ -0,0 +1,38 @@ +/** + * 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 "c_ops/upsample.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +string Upsample::GetMode() const { return this->primitive->value.AsUpsample()->mode; } +std::vector Upsample::GetScales() const { return this->primitive->value.AsUpsample()->scales; } + +void Upsample::SetMode(string mode) { this->primitive->value.AsUpsample()->mode = mode; } +void Upsample::SetScales(const std::vector &scales) { this->primitive->value.AsUpsample()->scales = scales; } + +#else + +string Upsample::GetMode() const { return this->primitive->value_as_Upsample()->mode()->str(); } +std::vector Upsample::GetScales() const { + auto fb_vector = this->primitive->value_as_Upsample()->scales(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void Upsample::SetMode(string mode) {} +void Upsample::SetScales(const std::vector &scales) {} +#endif +} // namespace mindspore diff --git a/mindspore/lite/c_ops/upsample.h b/mindspore/lite/c_ops/upsample.h new file mode 100644 index 0000000000..c113e46800 --- /dev/null +++ b/mindspore/lite/c_ops/upsample.h @@ -0,0 +1,47 @@ +/** + * 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 +#include +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_UPSAMPLE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_UPSAMPLE_H_ + +namespace mindspore { +class Upsample : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Upsample(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Upsample(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + string GetMode() const; + std::vector GetScales() const; + void SetMode(string mode); + void SetScales(const std::vector &scales); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_UPSAMPLE_H_ diff --git a/mindspore/lite/c_ops/where.cc b/mindspore/lite/c_ops/where.cc new file mode 100644 index 0000000000..6b9de95b02 --- /dev/null +++ b/mindspore/lite/c_ops/where.cc @@ -0,0 +1,93 @@ +/** + * 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 "c_ops/where.h" + +namespace mindspore { +#ifdef PRIMITIVE_WRITEABLE +std::vector Where::GetCondition() const { return this->primitive->value.AsWhere()->condition; } + +void Where::SetCondition(const std::vector &condition) { + this->primitive->value.AsWhere()->condition = condition; +} + +#else + +std::vector Where::GetCondition() const { + auto fb_vector = this->primitive->value_as_Where()->condition(); + return std::vector(fb_vector->begin(), fb_vector->end()); +} + +void Where::SetCondition(const std::vector &condition) {} +#endif +int Where::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + if (inputs_.size() != kSingleNum || outputs_.size() != kSingleNum) { + MS_LOG(ERROR) << "where input or output number invalid, Input size:" << inputs_.size() + << ", output size: " << outputs_.size(); + return 1; + } + if (inputs_.size() < 3) { + MS_LOG(ERROR) << "Input shape tensors should b"; + return 1; + } + auto input0 = inputs_.at(0); + auto input1 = inputs_.at(1); + auto input2 = inputs_.at(2); + int num = input0->ElementsNum(); + int num1 = input1->ElementsNum(); + int num2 = input2->ElementsNum(); + int nummax = num > num1 ? num : (num1 > num2 ? num1 : num2); + + auto shape_tmp = inputs_.at(0)->shape(); + auto shape_tmp1 = inputs_.at(1)->shape(); + auto shape_tmp2 = inputs_.at(2)->shape(); + int axisout = 0; + int temp = 0; + for (int j = 0; j < shape_tmp.size(); j++) { + if (shape_tmp[j] == shape_tmp1[j] && shape_tmp[j] != shape_tmp2[j]) { + axisout = j; + break; + } + if (shape_tmp[j] == shape_tmp2[j] && shape_tmp[j] != shape_tmp1[j]) { + axisout = j; + break; + } + if (shape_tmp1[j] == shape_tmp2[j] && shape_tmp[j] != shape_tmp1[j]) { + axisout = j; + break; + } + temp += 1; + if (temp == shape_tmp.size()) { + outputs_[0]->set_shape(shape_tmp); + output->set_data_type(input->data_type()); + return 0; + } + } + + auto output_shape = shape_tmp; + output_shape[axisout] = nummax; + outputs_[0]->set_shape(output_shape); + output->set_data_type(input->data_type()); + output->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/where.h b/mindspore/lite/c_ops/where.h new file mode 100644 index 0000000000..ea7403cb9c --- /dev/null +++ b/mindspore/lite/c_ops/where.h @@ -0,0 +1,45 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_WHERE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_WHERE_H_ + +namespace mindspore { +class Where : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit Where(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit Where(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; + std::vector GetCondition() const; + void SetCondition(const std::vector &condition); +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_WHERE_H_ diff --git a/mindspore/lite/c_ops/zeros_like.cc b/mindspore/lite/c_ops/zeros_like.cc new file mode 100644 index 0000000000..20cc2a8ceb --- /dev/null +++ b/mindspore/lite/c_ops/zeros_like.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 "c_ops/zeros_like.h" + +namespace mindspore { +int ZerosLike::InferShape(std::vector inputs_, std::vector outputs_) { + MS_ASSERT(this->primitive != nullptr); + auto input = inputs_.front(); + MS_ASSERT(input != nullptr); + auto output = outputs_.front(); + MS_ASSERT(output != nullptr); + if (inputs_.size() != kSingleNum || outputs_.size() != kSingleNum) { + MS_LOG(ERROR) << "zeroslike input or output number invalid, Input size:" << inputs_.size() + << ", output size: " << outputs_.size(); + return 1; + } + output->set_shape(input->shape()); + output->set_data_type(input->data_type()); + output->SetFormat(input->GetFormat()); + + return 0; +} +} // namespace mindspore diff --git a/mindspore/lite/c_ops/zeros_like.h b/mindspore/lite/c_ops/zeros_like.h new file mode 100644 index 0000000000..0968fede79 --- /dev/null +++ b/mindspore/lite/c_ops/zeros_like.h @@ -0,0 +1,43 @@ +/** + * 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 +#include +#include +#include "ir/dtype/type_id.h" +#include "mindspore/lite/c_ops/primitive_c.h" +#ifdef PRIMITIVE_WRITEABLE +#include "schema/inner/model_generated.h" +#else +#include "schema/model_generated.h" +#endif + +#ifndef LITE_MINDSPORE_LITE_C_OPS_ZEROS_LIKE_H_ +#define LITE_MINDSPORE_LITE_C_OPS_ZEROS_LIKE_H_ + +namespace mindspore { +class ZerosLike : public PrimitiveC { + public: +#ifdef PRIMITIVE_WRITEABLE + explicit ZerosLike(schema::PrimitiveT *primitive) : PrimitiveC(primitive) {} +#else + explicit ZerosLike(schema::Primitive *primitive) : PrimitiveC(primitive) {} +#endif + int InferShape(std::vector inputs_, std::vector outputs_) override; +}; +} // namespace mindspore + +#endif // LITE_MINDSPORE_LITE_C_OPS_ZEROS_LIKE_H_