tkonolige commented on a change in pull request #7125: URL: https://github.com/apache/tvm/pull/7125#discussion_r546831211
########## File path: python/tvm/relay/op/_transform.py ########## @@ -63,6 +63,8 @@ _reg.register_injective_schedule("sparse_to_dense") _reg.register_injective_schedule("matrix_set_diag") _reg.register_injective_schedule("adv_index") +_reg.register_injective_schedule("sparsereshape") Review comment: I think `sparse_reshape` would be a more appropriate name given the above naming conventions. ########## File path: python/tvm/relay/op/transform.py ########## @@ -1320,3 +1320,52 @@ def adv_index(inputs): Output tensor. """ return _make.adv_index(Tuple(inputs)) + + +def sparsereshape(sparse_indices, sparse_values, prev_shape, new_shape): + """ + Reshape a Sparse Tensor Review comment: Could you note that this function only support tensors in COO format, not CSR. In other parts of the codebase, we tend to use CSR. ########## File path: src/relay/op/tensor/transform.cc ########## @@ -1553,6 +1553,52 @@ RELAY_REGISTER_OP("meshgrid") .set_attr<FTVMCompute>("FTVMCompute", MeshgridCompute) .set_attr<TOpPattern>("TOpPattern", kInjective); +TVM_REGISTER_NODE_TYPE(SparseReshapeAttrs); + +bool SparseReshapeRel(const Array<Type>& types, int num_inputs, const Attrs& attrs, + const TypeReporter& reporter) { + // types: [sparse_indices, sparse_values, result] + // ICHECK_EQ(types.size(), 3); + auto sparse_indices = types[0].as<TensorTypeNode>(); + const auto* param = attrs.as<SparseReshapeAttrs>(); + CHECK(param != nullptr); + Array<PrimExpr> new_sparse_indices_shape{sparse_indices->shape[0], + static_cast<int>((param->new_shape).size())}; + reporter->Assign(types[2], TensorType(new_sparse_indices_shape, sparse_indices->dtype)); + return true; +} + +Array<te::Tensor> SparseReshapeCompute(const Attrs& attrs, const Array<te::Tensor>& inputs, + const Type& out_type) { + // ICHECK_EQ(inputs.size(), 2); + const auto* param = attrs.as<SparseReshapeAttrs>(); + CHECK(param != nullptr); + return {topi::SparseReshape(inputs[0], inputs[1], param->prev_shape, param->new_shape)}; +} + +Expr MakeSparseReshape(Expr sparse_indices, Expr sparse_values, Array<Integer> prev_shape, + Array<Integer> new_shape) { + auto attrs = make_object<SparseReshapeAttrs>(); + attrs->prev_shape = std::move(prev_shape); + attrs->new_shape = std::move(new_shape); + static const Op& op = Op::Get("sparsereshape"); + return Call(op, {sparse_indices, sparse_values}, Attrs(attrs), {}); +} + +TVM_REGISTER_GLOBAL("relay.op._make.sparsereshape").set_body_typed(MakeSparseReshape); + +RELAY_REGISTER_OP("sparsereshape") + .describe(R"code(Return twice of normal addition of two tensors. Review comment: Update the description ########## File path: src/relay/op/tensor/transform.cc ########## @@ -1553,6 +1553,52 @@ RELAY_REGISTER_OP("meshgrid") .set_attr<FTVMCompute>("FTVMCompute", MeshgridCompute) .set_attr<TOpPattern>("TOpPattern", kInjective); +TVM_REGISTER_NODE_TYPE(SparseReshapeAttrs); + +bool SparseReshapeRel(const Array<Type>& types, int num_inputs, const Attrs& attrs, + const TypeReporter& reporter) { + // types: [sparse_indices, sparse_values, result] + // ICHECK_EQ(types.size(), 3); Review comment: uncomment or remove ########## File path: src/relay/op/tensor/transform.cc ########## @@ -1553,6 +1553,52 @@ RELAY_REGISTER_OP("meshgrid") .set_attr<FTVMCompute>("FTVMCompute", MeshgridCompute) .set_attr<TOpPattern>("TOpPattern", kInjective); +TVM_REGISTER_NODE_TYPE(SparseReshapeAttrs); + +bool SparseReshapeRel(const Array<Type>& types, int num_inputs, const Attrs& attrs, + const TypeReporter& reporter) { + // types: [sparse_indices, sparse_values, result] + // ICHECK_EQ(types.size(), 3); + auto sparse_indices = types[0].as<TensorTypeNode>(); + const auto* param = attrs.as<SparseReshapeAttrs>(); + CHECK(param != nullptr); Review comment: ```suggestion ICHECK(param != nullptr); ``` ########## File path: src/relay/op/tensor/transform.cc ########## @@ -1553,6 +1553,52 @@ RELAY_REGISTER_OP("meshgrid") .set_attr<FTVMCompute>("FTVMCompute", MeshgridCompute) .set_attr<TOpPattern>("TOpPattern", kInjective); +TVM_REGISTER_NODE_TYPE(SparseReshapeAttrs); + +bool SparseReshapeRel(const Array<Type>& types, int num_inputs, const Attrs& attrs, + const TypeReporter& reporter) { + // types: [sparse_indices, sparse_values, result] + // ICHECK_EQ(types.size(), 3); + auto sparse_indices = types[0].as<TensorTypeNode>(); + const auto* param = attrs.as<SparseReshapeAttrs>(); + CHECK(param != nullptr); + Array<PrimExpr> new_sparse_indices_shape{sparse_indices->shape[0], + static_cast<int>((param->new_shape).size())}; + reporter->Assign(types[2], TensorType(new_sparse_indices_shape, sparse_indices->dtype)); + return true; +} + +Array<te::Tensor> SparseReshapeCompute(const Attrs& attrs, const Array<te::Tensor>& inputs, + const Type& out_type) { + // ICHECK_EQ(inputs.size(), 2); Review comment: uncomment or remove. If you keep it, it would be great to have an error message too. ########## File path: src/relay/op/tensor/transform.cc ########## @@ -1553,6 +1553,52 @@ RELAY_REGISTER_OP("meshgrid") .set_attr<FTVMCompute>("FTVMCompute", MeshgridCompute) .set_attr<TOpPattern>("TOpPattern", kInjective); +TVM_REGISTER_NODE_TYPE(SparseReshapeAttrs); + +bool SparseReshapeRel(const Array<Type>& types, int num_inputs, const Attrs& attrs, + const TypeReporter& reporter) { + // types: [sparse_indices, sparse_values, result] + // ICHECK_EQ(types.size(), 3); + auto sparse_indices = types[0].as<TensorTypeNode>(); + const auto* param = attrs.as<SparseReshapeAttrs>(); + CHECK(param != nullptr); + Array<PrimExpr> new_sparse_indices_shape{sparse_indices->shape[0], + static_cast<int>((param->new_shape).size())}; + reporter->Assign(types[2], TensorType(new_sparse_indices_shape, sparse_indices->dtype)); + return true; +} + +Array<te::Tensor> SparseReshapeCompute(const Attrs& attrs, const Array<te::Tensor>& inputs, + const Type& out_type) { + // ICHECK_EQ(inputs.size(), 2); + const auto* param = attrs.as<SparseReshapeAttrs>(); + CHECK(param != nullptr); Review comment: ```suggestion ICHECK(param != nullptr); ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org