zheng-da commented on a change in pull request #14090: Add GPU version of contrib.boolean_mask URL: https://github.com/apache/incubator-mxnet/pull/14090#discussion_r254934757
########## File path: src/operator/contrib/boolean_mask.cu ########## @@ -0,0 +1,191 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +/*! + * Copyright (c) 2018 by Contributors + * \file boolean_mask.cu +*/ + +#include "./boolean_mask-inl.h" +#include <cub/cub.cuh> + +namespace mxnet { +namespace op { + +struct BooleanMaskForwardKernel { + template<typename DType> + static void MSHADOW_XINLINE Map(int i, + DType* out, + const DType* data, + const int32_t* idx, + const size_t col_size) { + int row_id = i / col_size; + int col_id = i % col_size; + int32_t prev = (row_id == 0) ? 0 : idx[row_id - 1]; + int32_t curr = idx[row_id]; + if (prev != curr) { + out[prev * col_size + col_id] = data[i]; + } + } +}; + +struct BooleanMaskBackwardKernel { + template<typename DType> + static void MSHADOW_XINLINE Map(int i, + DType* igrad, + const DType* ograd, + const int32_t* idx, + const size_t col_size) { + int row_id = i / col_size; + int col_id = i % col_size; + int32_t prev = (row_id == 0) ? 0 : idx[row_id - 1]; + int32_t curr = idx[row_id]; + if (prev != curr) { + igrad[i] = ograd[prev * col_size + col_id]; + } + } +}; + +template<> +inline void BooleanMaskForward<gpu>(const nnvm::NodeAttrs& attrs, + const OpContext &ctx, + const std::vector<NDArray> &inputs, + const std::vector<OpReqType> &req, + const std::vector<NDArray> &outputs) { + using namespace mshadow; + CHECK_EQ(inputs.size(), 2U); + CHECK_EQ(outputs.size(), 1U); + const BooleanMaskParam& param = nnvm::get<BooleanMaskParam>(attrs.parsed); + const int axis = param.axis; + const NDArray &data = inputs[0]; + const NDArray &idx = inputs[1]; + const NDArray &out = outputs[0]; + CHECK_EQ(axis, 0) << "Not supported yet"; + CHECK_EQ(data.shape()[axis], idx.shape()[0]); + CHECK_EQ(idx.shape().ndim(), 1U); + Stream<gpu>* s = ctx.get_stream<gpu>(); + // count the number of 1s in `idx`, so that we could know the output dimension + size_t idx_size = idx.shape()[0]; + int32_t valid_num = 0; + int32_t* prefix_sum = nullptr; + void* d_temp_storage = nullptr; + size_t temp_storage_bytes = 0; + cub::DeviceScan::InclusiveSum(d_temp_storage, + temp_storage_bytes, + prefix_sum, + prefix_sum, + idx_size, + Stream<gpu>::GetStream(s)); Review comment: could you please add comments to explain the purpose of the code? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on 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 With regards, Apache Git Services