result error

success version

result error

命名大小写

result error

success version

result error
:wq

命名大小写

result error

增加cpu迁移部分

odd commit

fix commit

clang-format

fix ci error

add blank

cppcheck

aicpu_util.h format

add ,

fix error

fix error

fix ci

rebase error

window compile

window compile

windows compile

windows compile

头文件修改

use exception

fix clang

test_ops.py add Tensor

update cppcheck.txt

format fix

sparse_ops commit change

set output shape and type

fix ci

公共文件,添加放在中间

删除公共文件修改

c++ style type change

删除utils的改动
This commit is contained in:
lh735291378 2022-03-31 10:43:06 +08:00
parent fbb1109752
commit 2126a9435f
9 changed files with 734 additions and 0 deletions

View File

@ -0,0 +1,361 @@
/**
* Copyright 2022 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 "plugin/device/cpu/kernel/eigen/sparse_sparse_maximum_cpu_kernel.h"
#include <Eigen/Dense>
#include <unsupported/Eigen/CXX11/Tensor>
#include <algorithm>
#include <iostream>
#include <numeric>
#include "plugin/device/cpu/hal/device/cpu_device_address.h"
namespace mindspore {
namespace kernel {
namespace {
constexpr size_t kInputsNum = 6;
constexpr size_t kOutputsNum = 2;
constexpr size_t kMatrixDimNum = 2;
constexpr size_t kVectorDimNum = 1;
const uint32_t kInputa_indices = 0;
const uint32_t kInputa_values = 1;
const uint32_t kInputa_shapes = 2;
const uint32_t kInputb_indices = 3;
const uint32_t kInputb_values = 4;
const uint32_t kInputb_shapes = 5;
const uint32_t kOutput_indices = 0;
const uint32_t kOutput_values = 1;
#define SPARSE_SPARSE_MAXIMUM_COMPUTE_CASE(DTYPE, TYPE) \
case (DTYPE): { \
ret = LaunchKernel<TYPE>(inputs, outputs); \
break; \
}
#define EIGEN_SHAPE_CAST(INPUT) static_cast<Eigen::DenseIndex>(AnfAlgo::GetInputDeviceShape(node_ptr, INPUT)[0])
inline static int cmp(
const Eigen::TensorMap<Eigen::Tensor<int64_t, 2, Eigen::RowMajor, Eigen::DenseIndex>, Eigen::Aligned> &a_idx,
const Eigen::TensorMap<Eigen::Tensor<int64_t, 2, Eigen::RowMajor, Eigen::DenseIndex>, Eigen::Aligned> &b_idx,
const int64_t a_row, const int64_t b_row, const int dims) {
for (int d = 0; d < dims; ++d) {
const int64_t a = a_idx(a_row, d);
const int64_t b = b_idx(b_row, d);
if (a < b) {
return -1;
} else if (a > b) {
return 1;
}
}
return 0;
}
template <typename T>
void UnionSparseIndicesAndValues(
const Eigen::TensorMap<Eigen::Tensor<int64_t, 2, Eigen::RowMajor, Eigen::DenseIndex>, Eigen::Aligned> a_indices_mat,
const Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, Eigen::DenseIndex>, Eigen::Aligned> a_values,
int64_t a_nnz,
const Eigen::TensorMap<Eigen::Tensor<int64_t, 2, Eigen::RowMajor, Eigen::DenseIndex>, Eigen::Aligned> b_indices_mat,
const Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, Eigen::DenseIndex>, Eigen::Aligned> b_values,
int64_t b_nnz, int num_dims, std::vector<T> *a_augmented_values, std::vector<T> *b_augmented_values,
std::vector<std::pair<bool, int64_t>> *entries_to_copy) {
entries_to_copy->reserve(a_nnz + b_nnz);
a_augmented_values->reserve(a_nnz);
b_augmented_values->reserve(b_nnz);
int64_t i = 0, j = 0;
const T kZero = static_cast<T>(0);
while (i < a_nnz && j < b_nnz) {
switch (cmp(a_indices_mat, b_indices_mat, i, j, num_dims)) {
case -1:
entries_to_copy->emplace_back(true, i);
a_augmented_values->push_back(a_values(i));
b_augmented_values->push_back(kZero);
++i;
break;
case 0:
entries_to_copy->emplace_back(true, i);
a_augmented_values->push_back(a_values(i));
b_augmented_values->push_back(b_values(j));
++i;
++j;
break;
case 1:
entries_to_copy->emplace_back(false, j);
a_augmented_values->push_back(kZero);
b_augmented_values->push_back(b_values(j));
++j;
break;
}
}
// Handles leftovers; at most one loop runs.
while (i < a_nnz) {
entries_to_copy->emplace_back(true, i);
a_augmented_values->push_back(a_values(i++));
b_augmented_values->push_back(kZero);
}
while (j < b_nnz) {
entries_to_copy->emplace_back(false, j);
a_augmented_values->push_back(kZero);
b_augmented_values->push_back(b_values(j++));
}
}
} // namespace
void SparseSparseMaximumCpuKernelMod::InitKernel(const CNodePtr &kernel_node) {
MS_EXCEPTION_IF_NULL(kernel_node);
kernel_name_ = common::AnfAlgo::GetCNodeName(kernel_node);
node_ptr = kernel_node;
size_t input_num = common::AnfAlgo::GetInputTensorNum(kernel_node);
CHECK_KERNEL_INPUTS_NUM(input_num, kInputsNum, kernel_name_);
size_t output_num = common::AnfAlgo::GetOutputTensorNum(kernel_node);
CHECK_KERNEL_OUTPUTS_NUM(output_num, kOutputsNum, kernel_name_);
}
bool SparseSparseMaximumCpuKernelMod::Launch(const std::vector<kernel::AddressPtr> &inputs,
const std::vector<kernel::AddressPtr> &,
const std::vector<kernel::AddressPtr> &outputs) {
bool ret = false;
auto data_type = AnfAlgo::GetInputDeviceDataType(node_ptr, kInputa_values);
switch (data_type) {
SPARSE_SPARSE_MAXIMUM_COMPUTE_CASE(kNumberTypeInt8, int8_t)
SPARSE_SPARSE_MAXIMUM_COMPUTE_CASE(kNumberTypeInt16, int16_t)
SPARSE_SPARSE_MAXIMUM_COMPUTE_CASE(kNumberTypeInt32, int32_t)
SPARSE_SPARSE_MAXIMUM_COMPUTE_CASE(kNumberTypeInt64, int64_t)
SPARSE_SPARSE_MAXIMUM_COMPUTE_CASE(kNumberTypeUInt8, uint8_t)
SPARSE_SPARSE_MAXIMUM_COMPUTE_CASE(kNumberTypeUInt16, uint16_t)
SPARSE_SPARSE_MAXIMUM_COMPUTE_CASE(kNumberTypeFloat16, Eigen::half)
SPARSE_SPARSE_MAXIMUM_COMPUTE_CASE(kNumberTypeFloat32, float)
SPARSE_SPARSE_MAXIMUM_COMPUTE_CASE(kNumberTypeFloat64, double)
default:
MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', Unsupported input data type: " << data_type << ".";
}
return ret;
}
void SparseSparseMaximumCpuKernelMod::CheckInputShape(const std::vector<kernel::AddressPtr> &inputs,
const int64_t a_nnz, const int64_t b_nnz,
const int64_t num_dims) {
const int64_t a_values_shape0 = AnfAlgo::GetInputDeviceShape(node_ptr, kInputa_values)[0];
const int64_t b_values_shape0 = AnfAlgo::GetInputDeviceShape(node_ptr, kInputb_values)[0];
const int64_t a_shapes_shape0 = AnfAlgo::GetInputDeviceShape(node_ptr, kInputa_shapes)[0];
const int64_t b_shapes_shape0 = AnfAlgo::GetInputDeviceShape(node_ptr, kInputb_shapes)[0];
const int64_t b_indices_shape1 = AnfAlgo::GetInputDeviceShape(node_ptr, kInputb_indices)[1];
auto a_shape_addr = reinterpret_cast<int64_t *>(inputs[kInputa_shapes]->addr);
auto b_shape_addr = reinterpret_cast<int64_t *>(inputs[kInputb_shapes]->addr);
auto a_dtype = AnfAlgo::GetInputDeviceDataType(node_ptr, kInputa_values);
auto b_dtype = AnfAlgo::GetInputDeviceDataType(node_ptr, kInputb_values);
if (a_values_shape0 != a_nnz) {
MS_LOG(EXCEPTION) << "For '" << kernel_name_
<< "', x1_values.shape[0] should be same to x1_indices.shape[0], but got values size: "
<< a_values_shape0 << ", and " << a_nnz;
}
if (b_values_shape0 != b_nnz) {
MS_LOG(EXCEPTION) << "For '" << kernel_name_
<< "', x2_values.shape[0] should be same to x2_indices.shape[0], but got values size: "
<< b_values_shape0 << ", and " << b_nnz;
}
if (num_dims <= 0) {
MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', Tensors must not be empty.";
}
if (b_indices_shape1 != num_dims) {
MS_LOG(EXCEPTION) << "For '" << kernel_name_
<< "', b_indices.shape[1] and a_indices.shape[1] must match, but got values size: "
<< b_indices_shape1 << ", and " << num_dims;
}
if (a_shapes_shape0 != num_dims) {
MS_LOG(EXCEPTION) << "For '" << kernel_name_
<< "', a_indices.shape[1] and a_shape.shape[0] must match, but got values size: " << num_dims
<< ", and " << a_shapes_shape0;
}
if (a_shapes_shape0 != b_shapes_shape0) {
MS_LOG(EXCEPTION) << "For '" << kernel_name_
<< "', operands do not have the same ranks, got ranks: " << a_shapes_shape0 << ", and "
<< b_shapes_shape0;
}
for (int64_t i = 0; i < num_dims; ++i) {
if (a_shape_addr[i] != b_shape_addr[i]) {
MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "', operand's shapes do not match at index " << i
<< ", got value: " << a_shapes_shape0 << ", and " << b_shapes_shape0;
}
}
if (a_dtype != b_dtype) {
MS_LOG(EXCEPTION) << "For '" << kernel_name_
<< "', The type of input a must be the same as b, got ranks: " << a_dtype << ", and " << b_dtype;
}
}
template <typename T>
bool SparseSparseMaximumCpuKernelMod::LaunchKernel(const std::vector<kernel::AddressPtr> &inputs,
const std::vector<kernel::AddressPtr> &outputs) {
const auto shape = AnfAlgo::GetInputDeviceShape(node_ptr, kInputa_indices);
const int64_t a_nnz = shape[0];
const int64_t num_dims = shape[1];
const int64_t b_nnz = AnfAlgo::GetInputDeviceShape(node_ptr, kInputb_indices)[0];
auto output_indices_dtype = AnfAlgo::GetInputDeviceDataType(node_ptr, kInputa_indices);
auto output_values_dtype = AnfAlgo::GetInputDeviceDataType(node_ptr, kInputa_values);
CheckInputShape(inputs, a_nnz, b_nnz, num_dims);
auto a_values_ptr = reinterpret_cast<T *>(inputs[kInputa_values]->addr);
Eigen::DSizes<Eigen::DenseIndex, 1> a_values_size(EIGEN_SHAPE_CAST(kInputa_values));
Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, Eigen::DenseIndex>, Eigen::Aligned> a_values(a_values_ptr,
a_values_size);
auto b_values_ptr = reinterpret_cast<T *>(inputs[kInputb_values]->addr);
Eigen::DSizes<Eigen::DenseIndex, 1> b_values_size(EIGEN_SHAPE_CAST(kInputb_values));
Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, Eigen::DenseIndex>, Eigen::Aligned> b_values(b_values_ptr,
b_values_size);
auto a_indices_ptr = reinterpret_cast<int64_t *>(inputs[kInputa_indices]->addr);
Eigen::DSizes<Eigen::DenseIndex, kIndex2> a_indices_size(EIGEN_SHAPE_CAST(kInputa_values),
EIGEN_SHAPE_CAST(kInputa_shapes));
Eigen::TensorMap<Eigen::Tensor<int64_t, kIndex2, Eigen::RowMajor, Eigen::DenseIndex>, Eigen::Aligned> a_indices_mat(
a_indices_ptr, a_indices_size);
auto b_indices_ptr = reinterpret_cast<int64_t *>(inputs[kInputb_indices]->addr);
Eigen::DSizes<Eigen::DenseIndex, kIndex2> b_indices_size(EIGEN_SHAPE_CAST(kInputb_values),
EIGEN_SHAPE_CAST(kInputb_shapes));
Eigen::TensorMap<Eigen::Tensor<int64_t, kIndex2, Eigen::RowMajor, Eigen::DenseIndex>, Eigen::Aligned> b_indices_mat(
b_indices_ptr, b_indices_size);
auto a_shape_ptr = reinterpret_cast<int64_t *>(inputs[kInputa_shapes]->addr);
Eigen::DSizes<Eigen::DenseIndex, 1> a_shape_size(EIGEN_SHAPE_CAST(kInputa_shapes));
Eigen::TensorMap<Eigen::Tensor<int64_t, 1, Eigen::RowMajor, Eigen::DenseIndex>, Eigen::Aligned> a_shape(a_shape_ptr,
a_shape_size);
auto b_shape_ptr = reinterpret_cast<int64_t *>(inputs[kInputb_shapes]->addr);
Eigen::DSizes<Eigen::DenseIndex, 1> b_shape_size(EIGEN_SHAPE_CAST(kInputb_shapes));
Eigen::TensorMap<Eigen::Tensor<int64_t, 1, Eigen::RowMajor, Eigen::DenseIndex>, Eigen::Aligned> b_shape(b_shape_ptr,
b_shape_size);
std::vector<T> a_augmented_values, b_augmented_values;
std::vector<std::pair<bool, int64_t>> entries_to_copy; // from_a?, idx
UnionSparseIndicesAndValues(a_indices_mat, a_values, a_nnz, b_indices_mat, b_values, b_nnz, num_dims,
&a_augmented_values, &b_augmented_values, &entries_to_copy);
const int64_t sum_nnz = a_augmented_values.size();
auto output_indices_ptr = reinterpret_cast<int64_t *>(outputs[kOutput_indices]->addr);
Eigen::DSizes<Eigen::DenseIndex, kIndex2> output_indices_size(static_cast<Eigen::DenseIndex>(sum_nnz),
static_cast<Eigen::DenseIndex>(num_dims));
Eigen::TensorMap<Eigen::Tensor<int64_t, kIndex2, Eigen::RowMajor, Eigen::DenseIndex>, Eigen::Aligned>
output_indices_mat(output_indices_ptr, output_indices_size);
for (int64_t i = 0; i < sum_nnz; ++i) {
const bool from_a = entries_to_copy[i].first;
const int64_t idx = entries_to_copy[i].second;
output_indices_mat.chip<0>(i) = from_a ? a_indices_mat.chip<0>(idx) : b_indices_mat.chip<0>(idx);
}
using UnalignedTensorMap = Eigen::TensorMap<Eigen::Tensor<const T, 1, Eigen::RowMajor>, Eigen::Unaligned>;
auto a_augmented_values_t = UnalignedTensorMap(a_augmented_values.data(), sum_nnz);
auto b_augmented_values_t = UnalignedTensorMap(b_augmented_values.data(), sum_nnz);
auto output_values_ptr = reinterpret_cast<T *>(outputs[kOutput_values]->addr);
Eigen::DSizes<Eigen::DenseIndex, 1> output_values_size(static_cast<Eigen::DenseIndex>(sum_nnz));
Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, Eigen::DenseIndex>, Eigen::Aligned> output_values(
output_values_ptr, output_values_size);
// cppcheck-suppress unreadVariable
output_values = a_augmented_values_t.binaryExpr(b_augmented_values_t, Eigen::internal::scalar_max_op<T, T>());
ShapeVector out_indcie_shape, out_values_shape;
out_indcie_shape.push_back(sum_nnz);
out_indcie_shape.push_back(num_dims);
out_values_shape.push_back(sum_nnz);
common::AnfAlgo::SetOutputInferTypeAndShape({output_indices_dtype, output_values_dtype},
{out_indcie_shape, out_values_shape}, cnode_ptr_.lock().get());
return true;
}
std::vector<KernelAttr> SparseSparseMaximumCpuKernelMod::GetOpSupport() {
static std::vector<KernelAttr> support_list = {KernelAttr()
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt8)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt8)
.AddInputAttr(kNumberTypeInt64)
.AddOutputAttr(kNumberTypeInt64)
.AddOutputAttr(kNumberTypeInt8),
KernelAttr()
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt16)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt16)
.AddInputAttr(kNumberTypeInt64)
.AddOutputAttr(kNumberTypeInt64)
.AddOutputAttr(kNumberTypeInt16),
KernelAttr()
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt32)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt32)
.AddInputAttr(kNumberTypeInt64)
.AddOutputAttr(kNumberTypeInt64)
.AddOutputAttr(kNumberTypeInt32),
KernelAttr()
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt64)
.AddOutputAttr(kNumberTypeInt64)
.AddOutputAttr(kNumberTypeInt64),
KernelAttr()
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeUInt8)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeUInt8)
.AddInputAttr(kNumberTypeInt64)
.AddOutputAttr(kNumberTypeInt64)
.AddOutputAttr(kNumberTypeUInt8),
KernelAttr()
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeUInt16)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeUInt16)
.AddInputAttr(kNumberTypeInt64)
.AddOutputAttr(kNumberTypeInt64)
.AddOutputAttr(kNumberTypeUInt16),
KernelAttr()
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeFloat16)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeFloat16)
.AddInputAttr(kNumberTypeInt64)
.AddOutputAttr(kNumberTypeInt64)
.AddOutputAttr(kNumberTypeFloat16),
KernelAttr()
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeFloat32)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeFloat32)
.AddInputAttr(kNumberTypeInt64)
.AddOutputAttr(kNumberTypeInt64)
.AddOutputAttr(kNumberTypeFloat32),
KernelAttr()
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeFloat64)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeInt64)
.AddInputAttr(kNumberTypeFloat64)
.AddInputAttr(kNumberTypeInt64)
.AddOutputAttr(kNumberTypeInt64)
.AddOutputAttr(kNumberTypeFloat64)};
return support_list;
}
MS_KERNEL_FACTORY_REG(NativeCpuKernelMod, SparseSparseMaximum, SparseSparseMaximumCpuKernelMod);
} // namespace kernel
} // namespace mindspore

View File

@ -0,0 +1,53 @@
/**
* Copyright 2022 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_CCSRC_BACKEND_KERNEL_COMPILER_CPU_EIGEN_SPARSE_SPARSE_MAXIMUM_CPU_KERNEL_H_
#define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_EIGEN_SPARSE_SPARSE_MAXIMUM_CPU_KERNEL_H_
#include <map>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "plugin/device/cpu/kernel/cpu_kernel.h"
#include "plugin/factory/ms_factory.h"
namespace mindspore {
namespace kernel {
class SparseSparseMaximumCpuKernelMod : public DeprecatedNativeCpuKernelMod {
public:
SparseSparseMaximumCpuKernelMod() = default;
~SparseSparseMaximumCpuKernelMod() override = default;
void InitKernel(const CNodePtr &kernel_node) override;
bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
const std::vector<AddressPtr> &outputs) override;
protected:
std::vector<KernelAttr> GetOpSupport() override;
private:
template <typename T>
bool LaunchKernel(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &outputs);
void CheckInputShape(const std::vector<kernel::AddressPtr> &inputs, const int64_t a_nnz, const int64_t b_nnz,
const int64_t num_dims);
CNodePtr node_ptr;
};
} // namespace kernel
} // namespace mindspore
#endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_EIGEN_SPARSE_SPARSE_MAXIMUM_CPU_KERNEL_H_

View File

@ -310,6 +310,7 @@ constexpr auto kCOOTensorDenseMatmul = "COOTensorDenseMatmul";
constexpr auto kSparseTensorDenseMatmul = "SparseTensorDenseMatmul";
constexpr auto kSparseToDenseV2 = "SparseToDenseV2";
constexpr auto kSparseAddmm = "SparseAddmm";
constexpr auto kSparseSparseMaximum = "SparseSparseMaximum";
constexpr auto kCSRReduceSum = "CSRReduceSum";
constexpr auto kCSRMV = "CSRMV";
constexpr auto kCSRMM = "CSRMM";
@ -980,6 +981,7 @@ GVAR_DEF(PrimitivePtr, kPrimCSRTensorGetDenseShape, std::make_shared<Primitive>(
GVAR_DEF(PrimitivePtr, kPrimSparseTensorDenseMatmul, std::make_shared<Primitive>(kSparseTensorDenseMatmul));
GVAR_DEF(PrimitivePtr, kPrimSparseToDenseV2, std::make_shared<Primitive>(kSparseToDenseV2));
GVAR_DEF(PrimitivePtr, kPrimSparseAddmm, std::make_shared<Primitive>(kSparseAddmm));
GVAR_DEF(PrimitivePtr, kPrimSparseSparseMaximum, std::make_shared<Primitive>(kSparseSparseMaximum));
GVAR_DEF(PrimitivePtr, kPrimCOOTensorDenseMatmul, std::make_shared<Primitive>(kCOOTensorDenseMatmul));
GVAR_DEF(PrimitivePtr, kPrimCSRReduceSum, std::make_shared<Primitive>(kCSRReduceSum));
GVAR_DEF(PrimitivePtr, kPrimCSRMV, std::make_shared<Primitive>(kCSRMV));

View File

@ -0,0 +1,151 @@
/**
* Copyright 2022 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 <memory>
#include <set>
#include <string>
#include <vector>
#include "abstract/ops/primitive_infer_map.h"
#include "mindapi/src/helper.h"
#include "ops/op_utils.h"
#include "ops/sparsesparsemaximum.h"
#include "utils/check_convert_utils.h"
#include "utils/tensor_construct_utils.h"
namespace mindspore {
namespace ops {
namespace {
void CheckSparseSparseMaximumInputs(const std::vector<AbstractBasePtr> &input_args, const std::string &op_name) {
auto x1_indices = CheckAndConvertUtils::CheckArgs<abstract::AbstractTensor>(op_name, input_args, 0);
auto x1_values = CheckAndConvertUtils::CheckArgs<abstract::AbstractTensor>(op_name, input_args, 1);
auto x1_shape = CheckAndConvertUtils::CheckArgs<abstract::AbstractTensor>(op_name, input_args, 2);
auto x2_indices = CheckAndConvertUtils::CheckArgs<abstract::AbstractTensor>(op_name, input_args, 3);
auto x2_values = CheckAndConvertUtils::CheckArgs<abstract::AbstractTensor>(op_name, input_args, 4);
auto x2_shape = CheckAndConvertUtils::CheckArgs<abstract::AbstractTensor>(op_name, input_args, 5);
auto x1_indices_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(x1_indices->BuildShape())[kShape];
auto x1_values_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(x1_values->BuildShape())[kShape];
auto x1_shape_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(x1_shape->BuildShape())[kShape];
auto x2_indices_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(x2_indices->BuildShape())[kShape];
auto x2_values_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(x2_values->BuildShape())[kShape];
auto x2_shape_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(x2_shape->BuildShape())[kShape];
const int64_t indice_size = 2;
const int64_t values_size = 1;
const int64_t shape_size = 1;
(void)CheckAndConvertUtils::CheckInteger("x1_indices rank", SizeToLong(x1_indices_shape.size()), kEqual, indice_size,
op_name);
(void)CheckAndConvertUtils::CheckInteger("x2_indices rank", SizeToLong(x2_indices_shape.size()), kEqual, indice_size,
op_name);
(void)CheckAndConvertUtils::CheckInteger("x1_values rank", SizeToLong(x1_values_shape.size()), kEqual, values_size,
op_name);
(void)CheckAndConvertUtils::CheckInteger("x2_values rank", SizeToLong(x2_values_shape.size()), kEqual, values_size,
op_name);
(void)CheckAndConvertUtils::CheckInteger("x1_shape rank", SizeToLong(x1_shape_shape.size()), kEqual, shape_size,
op_name);
(void)CheckAndConvertUtils::CheckInteger("x2_shape rank", SizeToLong(x2_shape_shape.size()), kEqual, shape_size,
op_name);
if (x1_indices_shape[1] != x1_shape_shape[0]) {
MS_EXCEPTION(ValueError)
<< "For SparseSparseMaximum, x1_indices.shape[1] and x1_shape.shape[0] must be same, but got "
<< x1_indices_shape[0] << " and " << x1_shape_shape[0];
}
if (x2_indices_shape[1] != x2_shape_shape[0]) {
MS_EXCEPTION(ValueError)
<< "For SparseSparseMaximum, x2_indices.shape[1] and x2_shape.shape[0] must be same, but got "
<< x2_indices_shape[0] << " and " << x2_shape_shape[0];
}
if (x1_indices_shape[0] != x1_values_shape[0]) {
MS_EXCEPTION(ValueError)
<< "For SparseSparseMaximum, x1_indices.shape[0] and x1_value.shape[0] must be same, but got "
<< x1_indices_shape[0] << " and " << x1_values_shape[0];
}
if (x2_indices_shape[0] != x2_values_shape[0]) {
MS_EXCEPTION(ValueError)
<< "For SparseSparseMaximum, x2_indices.shape[0] and x2_value.shape[0] must be same, but got "
<< x2_indices_shape[0] << " and " << x2_values_shape[0];
}
if (x1_shape_shape[0] != x2_shape_shape[0]) {
MS_EXCEPTION(ValueError) << "For SparseSparseMaximum, rank of shapes must be same, but got " << x1_shape_shape[0]
<< " and " << x2_shape_shape[0];
}
}
abstract::TupleShapePtr SparseSparseMaximumInferShape(const PrimitivePtr &primitive,
const std::vector<AbstractBasePtr> &input_args) {
auto op_name = primitive->name();
CheckSparseSparseMaximumInputs(input_args, op_name);
auto x1_indice_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
auto x2_indice_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[3]->BuildShape())[kShape];
abstract::ShapePtr y_indices_shape;
abstract::ShapePtr y_values_shape;
ShapeVector out_indice_shape = {-1, x1_indice_shape[1]};
ShapeVector out_value_shape = {-1};
ShapeVector min_out_indice_shape = {};
ShapeVector max_out_indice_shape = {};
ShapeVector min_out_value_shape = {};
ShapeVector max_out_value_shape = {};
if (x1_indice_shape[0] > x2_indice_shape[0]) {
min_out_indice_shape.push_back(x1_indice_shape[0]);
min_out_value_shape.push_back(x1_indice_shape[0]);
} else {
min_out_indice_shape.push_back(x2_indice_shape[0]);
min_out_value_shape.push_back(x2_indice_shape[0]);
}
min_out_indice_shape.push_back(x1_indice_shape[1]);
max_out_indice_shape.push_back(x1_indice_shape[0] + x2_indice_shape[0]);
max_out_indice_shape.push_back(x1_indice_shape[1]);
max_out_value_shape.push_back(x1_indice_shape[0] + x2_indice_shape[0]);
y_indices_shape = std::make_shared<abstract::Shape>(out_indice_shape, min_out_indice_shape, max_out_indice_shape);
y_values_shape = std::make_shared<abstract::Shape>(out_value_shape, min_out_value_shape, max_out_value_shape);
return std::make_shared<abstract::TupleShape>(std::vector<abstract::BaseShapePtr>{y_indices_shape, y_values_shape});
}
TuplePtr SparseSparseMaximumInferType(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
auto op_name = primitive->name();
(void)CheckAndConvertUtils::CheckTensorTypeValid("x1_indices", input_args[kInputIndex0]->BuildType(), {kInt64},
op_name);
(void)CheckAndConvertUtils::CheckTensorTypeValid("x1_shape", input_args[kInputIndex2]->BuildType(), {kInt64},
op_name);
(void)CheckAndConvertUtils::CheckTensorTypeValid("x2_indices", input_args[kInputIndex3]->BuildType(), {kInt64},
op_name);
(void)CheckAndConvertUtils::CheckTensorTypeValid("x2_shape", input_args[kInputIndex5]->BuildType(), {kInt64},
op_name);
auto x1_indices_type = input_args[kInputIndex1]->BuildType();
auto x2_indices_type = input_args[kInputIndex4]->BuildType();
(void)CheckAndConvertUtils::CheckTensorTypeSame({{"x1_indices", x1_indices_type}, {"x2_indices", x2_indices_type}},
common_valid_types, op_name);
return std::make_shared<Tuple>(std::vector<TypePtr>{kInt64, x1_indices_type});
}
} // namespace
MIND_API_OPERATOR_IMPL(SparseSparseMaximum, BaseOperator);
AbstractBasePtr SparseSparseMaximumInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
const std::vector<AbstractBasePtr> &input_args) {
MS_EXCEPTION_IF_NULL(primitive);
const int64_t input_num = 6;
CheckAndConvertUtils::CheckInputArgs(input_args, kEqual, input_num, primitive->name());
auto types = SparseSparseMaximumInferType(primitive, input_args);
auto shapes = SparseSparseMaximumInferShape(primitive, input_args);
return abstract::MakeAbstract(shapes, types);
}
REGISTER_PRIMITIVE_EVAL_IMPL(SparseSparseMaximum, prim::kPrimSparseSparseMaximum, SparseSparseMaximumInfer, nullptr,
true);
} // namespace ops
} // namespace mindspore

View File

@ -0,0 +1,45 @@
/**
* Copyright 2022 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_OPS_SPARSESPARSEMAXIMUM_H_
#define MINDSPORE_CORE_OPS_SPARSESPARSEMAXIMUM_H_
#include <memory>
#include <vector>
#include "mindapi/base/types.h"
#include "ops/base_operator.h"
namespace mindspore {
namespace ops {
constexpr auto kNameSparseSparseMaximum = "SparseSparseMaximum";
class MIND_API SparseSparseMaximum : public BaseOperator {
public:
/// \brief Constructor.
MIND_API_BASE_MEMBER(SparseSparseMaximum);
SparseSparseMaximum() : BaseOperator(kNameSparseSparseMaximum) {
InitIOName({"x1_indices", "x1_values", "x1_shape", "x2_indices", "x2_values", "x2_shape"},
{"y_indices", "y_values"});
}
/// \brief Init.
void Init() const {}
};
abstract::AbstractBasePtr SparseSparseMaximumInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
const std::vector<abstract::AbstractBasePtr> &input_args);
} // namespace ops
} // namespace mindspore
#endif // MINDSPORE_CORE_OPS_CTCLOSS_H_

View File

@ -326,6 +326,7 @@ from .sparse_apply_proximal_gradient_descent import _sparse_apply_proximal_gradi
from .sparse_apply_momentum import _sparse_apply_momentum_aicpu
from .linear_sum_assignment import _linear_sum_assignment_aicpu
from .orgqr import _orgqr_aicpu
from .sparsesparsemaximum import _sparsesparsemaximum_aicpu
from .sparse_to_dense_v2 import _sparse_to_dense_v2_aicpu
from .sparse_sparse_minimum import _sparse_sparse_minimum_aicpu
from .broadcast_to import _broadcast_to_aicpu

View File

@ -0,0 +1,53 @@
# Copyright 2022 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.
# ============================================================================
"""sparsesparsemaximum op"""
from mindspore.ops.op_info_register import op_info_register, AiCPURegOp, DataType
sparsesparsemaximum_op_info = AiCPURegOp("SparseSparseMaximum") \
.fusion_type("OPAQUE") \
.input(0, "x1_indices", "required") \
.input(1, "x1_values", "required") \
.input(2, "x1_shape", "required") \
.input(3, "x2_indices", "required") \
.input(4, "x2_values", "required") \
.input(5, "x2_shape", "required") \
.output(0, "y_indices", "required") \
.output(1, "y_values", "required") \
.dtype_format(DataType.I64_Default, DataType.I8_Default, DataType.I64_Default, DataType.I64_Default, \
DataType.I8_Default, DataType.I64_Default, DataType.I64_Default, DataType.I8_Default) \
.dtype_format(DataType.I64_Default, DataType.I16_Default, DataType.I64_Default, DataType.I64_Default, \
DataType.I16_Default, DataType.I64_Default, DataType.I64_Default, DataType.I16_Default) \
.dtype_format(DataType.I64_Default, DataType.I32_Default, DataType.I64_Default, DataType.I64_Default, \
DataType.I32_Default, DataType.I64_Default, DataType.I64_Default, DataType.I32_Default) \
.dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.I64_Default, DataType.I64_Default, \
DataType.I64_Default, DataType.I64_Default, DataType.I64_Default, DataType.I64_Default) \
.dtype_format(DataType.I64_Default, DataType.U8_Default, DataType.I64_Default, DataType.I64_Default, \
DataType.U8_Default, DataType.I64_Default, DataType.I64_Default, DataType.U8_Default) \
.dtype_format(DataType.I64_Default, DataType.U16_Default, DataType.I64_Default, DataType.I64_Default, \
DataType.U16_Default, DataType.I64_Default, DataType.I64_Default, DataType.U16_Default) \
.dtype_format(DataType.I64_Default, DataType.F16_Default, DataType.I64_Default, DataType.I64_Default, \
DataType.F16_Default, DataType.I64_Default, DataType.I64_Default, DataType.F16_Default) \
.dtype_format(DataType.I64_Default, DataType.F32_Default, DataType.I64_Default, DataType.I64_Default, \
DataType.F32_Default, DataType.I64_Default, DataType.I64_Default, DataType.F32_Default) \
.dtype_format(DataType.I64_Default, DataType.F64_Default, DataType.I64_Default, DataType.I64_Default, \
DataType.F64_Default, DataType.I64_Default, DataType.I64_Default, DataType.F64_Default) \
.get_op_info()
@op_info_register(sparsesparsemaximum_op_info)
def _sparsesparsemaximum_aicpu():
"""SparseSparseMaximum AiCPU register"""
return

View File

@ -234,6 +234,67 @@ class SparseSlice(Primitive):
outputs=['y_indices', 'y_values', 'y_shape'])
class SparseSparseMaximum(Primitive):
"""
return a sparse tensor representation max element of two input sparse tensor.
Inputs:
- **x1_indices** - A 2-D Tensor, type int64, represents the position of the element in the x1 sparse tensor.
each element value should be a non-negative int number. the shape of which should be :math:`(n1, m,)`.
- **x1_values** - A 1-D Tensor, represents the value corresponding to the position in the `indices`.
the shape of which should be :math:`(n1,)`.
- **x1_shape** - A 1-D Tensor, type int64, which specifies the shape of x1 sparse tensor.
the shape of which should be :math:`(m,)`.
- **x2_indices** - A 2-D Tensor, type int64, represents the position of the element in the x2 sparse tensor.
each element value should be a non-negative int number. the shape of which should be :math:`(n2, m,)`.
- **x2_values** - A 1-D Tensor, represents the value corresponding to the position in the `indices`.
the shape of which should be :math:`(n2,)`.
- **x2_shape** - A 1-D Tensor, type int64, which specifies the shape of x2 sparse tensor.
the shape of which should be :math:`(m,)`.
Returns:
- **y_indices** - A 2-D Tensor, type int64. It represents the position of the element-wise max of
two input tensors.
- **y_values** - A 1-D Tensor. It represents the value corresponding to the position
in the `y_indices`. Has the same type as x1_values.
Raises:
TypeError: If the dtype of `x1_indices`, `x2_indices`, `x1_indices` and `x2_indices` isn't int64.
TypeError: If the dtype of `x1_values` and `x2_values` isn't support.
TypeError: If the dtype of `x1_values` and `x2_values` isn't same.
TypeError: If the input is not tensor.
ValueError: If x1_indices.shape[0] and x1_values.shape[0] isn't same.
ValueError: If x2_indices.shape[0] and x2_values.shape[0] isn't same.
ValueError: If x1_indices.shape[1] and x1_shape.shape[0] isn't same.
ValueError: If x2_indices.shape[0] and x2_values.shape[0] isn't same.
ValueError: If the `x1_shape` and `x2_shape` mismatch with each other.
Supported Platforms:
``Ascend`` ``CPU``
Examples:
>>> x1_indices = Tensor([[0, 1], [1, 2]])
>>> x1_values = Tensor([1, 2], dtype=ms.float32)
>>> x1_shape = Tensor([3, 3])
>>> x2_indices = Tensor([[0, 1], [1, 1]])
>>> x2_values = Tensor([3, 4], dtype=ms.float32)
>>> x2_shape = Tensor([3, 3])
>>> SparseSparseMaximum = ops.SparseSparseMaximum()
>>> y_indices, y_values = SparseSparseMaximum(x1_indices, x1_values, x1_shape, x2_indices, x2_values, x2_shape)
>>> print(y_indices)
[[0. 1.]
[1. 1.]
[1. 2.]]
>>> print(y_values)
[3. 4. 2.]
"""
@prim_attr_register
def __init__(self):
"""Initialize SparseSparseMaximum."""
self.init_prim_io_names(inputs=['x1_indices', 'x1_values', 'x1_shape', 'x2_indices', 'x2_values', 'x2_shape'],
outputs=['y_indices', 'y_values'])
class SparseToDense(PrimitiveWithInfer):
"""
Converts a sparse representation into a dense tensor.

View File

@ -143,6 +143,7 @@ from mindspore.ops.operations.sparse_ops import CSRSparseMatrixToSparseTensor
from mindspore.ops.operations.sparse_ops import SparseAddmm
from mindspore.ops.operations.sparse_ops import SparseConcat
from mindspore.ops.operations.sparse_ops import SparseTensorToCSRSparseMatrix
from mindspore.ops.operations.sparse_ops import SparseSparseMaximum
from mindspore.ops.operations.sparse_ops import SparseSparseMinimum
from mindspore.ops.operations.sparse_ops import SparseSegmentSqrtN
from mindspore.ops.operations.sparse_ops import SparseSegmentSqrtNWithNumSegments
@ -4364,6 +4365,12 @@ test_case_other_ops = [
Tensor(np.array([0, 2]).astype(np.int32)),
Tensor(np.array([5.3, 2.4]).astype(np.float32))],
'skip': ['backward']}),
('SparseSparseMaximum', {
'block': SparseSparseMaximum(),
'desc_inputs': [Tensor([[0, 0]], mstype.int64), Tensor([1], mstype.int64),
Tensor([3, 3], mstype.int64), Tensor([[0, 0], [1, 1]], mstype.int64),
Tensor([2, 100], mstype.int64), Tensor([3, 3], mstype.int64)],
'skip': ['backward']}),
('SparseReshape', {
'block': SparseReshape(),
'desc_inputs': [Tensor(np.array([[0, 0, 0],