2019-06-06 14:21:15 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2015-2018 Skymind, Inc.
|
|
|
|
*
|
|
|
|
* This program and the accompanying materials are made available under the
|
|
|
|
* terms of the Apache License, Version 2.0 which is available at
|
|
|
|
* https://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.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
//
|
|
|
|
// This file contains operations added by 3rd parties
|
|
|
|
//
|
|
|
|
// @author raver119@gmail.com
|
|
|
|
//
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <system/op_boilerplate.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
#if NOT_EXCLUDED(OP_firas_sparse)
|
|
|
|
|
|
|
|
#ifndef LIBND4J_THIRD_PARTY_H
|
|
|
|
#define LIBND4J_THIRD_PARTY_H
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <system/op_boilerplate.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
#include <memory>
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <helpers/shape.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
#include <loops/random.h>
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <array/NDArray.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
#include <ops/declarable/DeclarableOp.h>
|
|
|
|
#include <ops/declarable/OpRegistrator.h>
|
|
|
|
#include <ops/declarable/CustomOperations.h>
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
namespace sd {
|
2019-06-06 14:21:15 +02:00
|
|
|
namespace ops {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This op is special one, and suited only for ProjectionLayer by @firasdib
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @tparam T
|
|
|
|
*/
|
|
|
|
CUSTOM_OP_IMPL(firas_sparse, 1, 1, false, 0, -1) {
|
|
|
|
auto x = INPUT_VARIABLE(0);
|
|
|
|
auto z = OUTPUT_VARIABLE(0);
|
|
|
|
|
|
|
|
int batchSize = x->sizeAt(0);
|
|
|
|
int numColumns = x->sizeAt(1);
|
|
|
|
|
|
|
|
std::vector<int> indices(*block.getIArguments());
|
|
|
|
std::map<int, int> sparse2dense;
|
|
|
|
|
|
|
|
|
|
|
|
int cnt = 0;
|
|
|
|
for (auto v: indices) {
|
|
|
|
std::pair<int, int> pair(v, cnt++);
|
|
|
|
sparse2dense.insert(pair);
|
|
|
|
}
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
ResultSet rows = x->allTensorsAlongDimension({1});
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-08-02 19:01:03 +02:00
|
|
|
//PRAGMA_OMP_PARALLEL_FOR
|
2019-06-06 14:21:15 +02:00
|
|
|
for (int r = 0; r < batchSize; r++) {
|
2019-12-20 20:35:39 +01:00
|
|
|
auto row = rows.at(r);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
for (int e = 0; e < numColumns; e += 2) {
|
|
|
|
int idx = row->e<int>(e);
|
|
|
|
if (idx < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
int denseIdx = sparse2dense.at(idx);
|
|
|
|
|
|
|
|
|
2019-08-02 19:01:03 +02:00
|
|
|
float value = row->e<float>(e);
|
2019-06-06 14:21:15 +02:00
|
|
|
float current = z->e<float>(r, denseIdx);
|
|
|
|
z->p(r, denseIdx, value + current);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-02 19:01:03 +02:00
|
|
|
//STORE_RESULT(*z);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
DECLARE_SHAPE_FN(firas_sparse) {
|
|
|
|
auto inP = inputShape->at(0);
|
|
|
|
|
|
|
|
std::vector<Nd4jLong> shape({shape::shapeOf(inP)[0], (Nd4jLong) block.getIArguments()->size()});
|
2019-08-02 19:01:03 +02:00
|
|
|
auto newShape = ConstantShapeHelper::getInstance()->createShapeInfo(ArrayOptions::dataType(inP), 'c', shape);
|
2019-06-06 14:21:15 +02:00
|
|
|
return SHAPELIST(newShape);
|
|
|
|
}
|
|
|
|
|
|
|
|
DECLARE_TYPES(firas_sparse) {
|
|
|
|
getOpDescriptor()
|
2020-03-02 10:49:41 +01:00
|
|
|
->setAllowedInputTypes(sd::DataType::ANY)
|
2019-06-06 14:21:15 +02:00
|
|
|
->setAllowedOutputTypes({ALL_FLOATS});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif //LIBND4J_THIRD_PARTY_H
|