|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/*! |
| 21 | + * Copyright (c) 2018 by Contributors |
| 22 | + * \file shuffle_op.cc |
| 23 | + * \brief Operator to shuffle elements of an NDArray |
| 24 | + */ |
| 25 | +#if (__GNUC__ > 4 && !defined(__clang__major__)) || (__clang_major__ > 4 && __linux__) |
| 26 | + #define USE_GNU_PARALLEL_SHUFFLE |
| 27 | +#endif |
| 28 | + |
| 29 | +#include <mxnet/operator_util.h> |
| 30 | +#include <algorithm> |
| 31 | +#include <random> |
| 32 | +#include <vector> |
| 33 | +#ifdef USE_GNU_PARALLEL_SHUFFLE |
| 34 | + #include <parallel/algorithm> |
| 35 | +#endif |
| 36 | +#include "../elemwise_op_common.h" |
| 37 | + |
| 38 | +namespace mxnet { |
| 39 | +namespace op { |
| 40 | + |
| 41 | +namespace { |
| 42 | + |
| 43 | +template<typename DType, typename Rand> |
| 44 | +void Shuffle1D(DType* const out, const index_t size, Rand* const prnd) { |
| 45 | + #ifdef USE_GNU_PARALLEL_SHUFFLE |
| 46 | + auto rand_n = [prnd](index_t n) { |
| 47 | + std::uniform_int_distribution<index_t> dist(0, n - 1); |
| 48 | + return dist(*prnd); |
| 49 | + }; |
| 50 | + __gnu_parallel::random_shuffle(out, out + size, rand_n); |
| 51 | + #else |
| 52 | + std::shuffle(out, out + size, *prnd); |
| 53 | + #endif |
| 54 | +} |
| 55 | + |
| 56 | +template<typename DType, typename Rand> |
| 57 | +void ShuffleND(DType* const out, const index_t size, const index_t first_axis_len, |
| 58 | + Rand* const prnd) { |
| 59 | + // Fisher-Yates shuffling |
| 60 | + const index_t stride = size / first_axis_len; |
| 61 | + auto rand_n = [prnd](index_t n) { |
| 62 | + std::uniform_int_distribution<index_t> dist(0, n - 1); |
| 63 | + return dist(*prnd); |
| 64 | + }; |
| 65 | + CHECK_GT(first_axis_len, 0U); |
| 66 | + for (index_t i = first_axis_len - 1; i > 0; --i) { |
| 67 | + const index_t j = rand_n(i + 1); |
| 68 | + if (i != j) { |
| 69 | + std::swap_ranges(out + stride * i, out + stride * (i + 1), out + stride * j); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +} // namespace |
| 75 | + |
| 76 | +void ShuffleForwardCPU(const nnvm::NodeAttrs& attrs, |
| 77 | + const OpContext& ctx, |
| 78 | + const std::vector<TBlob>& inputs, |
| 79 | + const std::vector<OpReqType>& req, |
| 80 | + const std::vector<TBlob>& outputs) { |
| 81 | + using namespace mxnet_op; |
| 82 | + if (req[0] == kNullOp) { |
| 83 | + return; |
| 84 | + } |
| 85 | + CHECK_NE(req[0], kAddTo) << "Shuffle does not support AddTo"; |
| 86 | + const TShape& input_shape = inputs[0].shape_; |
| 87 | + const index_t size = inputs[0].Size(); |
| 88 | + const index_t first_axis_len = input_shape[0]; |
| 89 | + Stream<cpu> *s = ctx.get_stream<cpu>(); |
| 90 | + MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, DType, { |
| 91 | + Tensor<cpu, 1, DType> in = inputs[0].get_with_shape<cpu, 1, DType>(Shape1(size), s); |
| 92 | + Tensor<cpu, 1, DType> out = outputs[0].get_with_shape<cpu, 1, DType>(Shape1(size), s); |
| 93 | + auto& prnd = ctx.requested[0].get_random<cpu, index_t>(ctx.get_stream<cpu>())->GetRndEngine(); |
| 94 | + if (req[0] != kWriteInplace) { |
| 95 | + std::copy(in.dptr_, in.dptr_ + size, out.dptr_); |
| 96 | + } |
| 97 | + if (input_shape.ndim() == 1) { |
| 98 | + Shuffle1D(out.dptr_, size, &prnd); |
| 99 | + } else { |
| 100 | + ShuffleND(out.dptr_, size, first_axis_len, &prnd); |
| 101 | + } |
| 102 | + }); |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +// No parameter is declared. |
| 107 | +// No backward computation is registered. Shuffling is not differentiable. |
| 108 | + |
| 109 | +NNVM_REGISTER_OP(_shuffle) |
| 110 | +.add_alias("shuffle") |
| 111 | +.describe(R"code(Randomly shuffle the elements. |
| 112 | +
|
| 113 | +This shuffles the array along the first axis. |
| 114 | +The order of the elements in each subarray does not change. |
| 115 | +For example, if a 2D array is given, the order of the rows randomly changes, |
| 116 | +but the order of the elements in each row does not change. |
| 117 | +)code") |
| 118 | +.set_num_inputs(1) |
| 119 | +.set_num_outputs(1) |
| 120 | +.set_attr<nnvm::FInferShape>("FInferShape", ElemwiseShape<1, 1>) |
| 121 | +.set_attr<nnvm::FInferType>("FInferType", ElemwiseType<1, 1>) |
| 122 | +.set_attr<FResourceRequest>("FResourceRequest", |
| 123 | + [](const nnvm::NodeAttrs& attrs) { |
| 124 | + return std::vector<ResourceRequest>{ResourceRequest::kRandom, ResourceRequest::kTempSpace}; |
| 125 | + }) |
| 126 | +.set_attr<nnvm::FInplaceOption>("FInplaceOption", |
| 127 | + [](const NodeAttrs& attrs) { |
| 128 | + return std::vector<std::pair<int, int>>{{0, 0}}; |
| 129 | + }) |
| 130 | +.set_attr<FCompute>("FCompute<cpu>", ShuffleForwardCPU) |
| 131 | +.add_argument("data", "NDArray-or-Symbol", "Data to be shuffled."); |
| 132 | + |
| 133 | +} // namespace op |
| 134 | +} // namespace mxnet |
0 commit comments