pytorch/caffe2/operators/length_split_op.cc
Mingda Li f2f43ad2da Add new LengthsSplit operator (#10974)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/10974

Pull Request resolved: https://github.com/pytorch/pytorch/pull/10291

This new operator will do the following:

Given a LENGTHS vector and n_splits, output a "split" LENGTHS vector where:

1. Each length in input vector is split into n_splits values (thus output vector should have LENGTHS.size(0) * n_splits elements)
2. The new lengths in output should be evenly split, and if the length is not divisible by n_splits, then order new values in descending order. (e.g. n_splits = 3, length = 5 -> 2 2 1)
3. If n_splits > some element in the array, its split elements will contain 0s. (e.g. n_splits = 3, length = 2 - > 1 1 0)

Reviewed By: bddppq, chocjy

Differential Revision: D9013119

fbshipit-source-id: 82bf3371ec08c41fc3379177f0007afc142e0d84
2018-09-10 15:40:28 -07:00

38 lines
1.2 KiB
C++

#include "caffe2/operators/length_split_op.h"
namespace caffe2 {
REGISTER_CPU_OPERATOR(LengthsSplit, LengthsSplitOp<CPUContext>);
OPERATOR_SCHEMA(LengthsSplit)
.NumInputs(1, 2)
.NumOutputs(1)
.ScalarType(TensorProto::INT32)
.SetDoc(R"DOC(
Given input vector LENGTHS, and input n_split, LengthsSplit returns
a single output vector. It "splits" each length into n_split values which add
up to the original length. It will attempt to do equal splits, and if not possible,
it orders larger values first. If the n_split is larger than the length, zero
padding will be applied.
e.g. LENGTHS = [9 4 5]
n_split = 3
Y = [3 3 3 2 1 1 2 2 1]
e.g. LENGTHS = [2, 1, 2]
n_split = 3
Y = [1 1 0 1 0 0 1 1 0]
)DOC")
.Arg("n_split", "Number of splits for each element in LENGTHS")
.Input(0, "LENGTHS", "Mx1 Input tensor denoting INT32 lengths")
.Input(
1,
"n_split",
"(Optional) Number of splits for each element in LENGTHS (overrides argument)")
.Output(0, "Y", "(M*n_split)x1 Output vector denoting split lengths");
// TODO: Write gradient for this when needed
GRADIENT_NOT_IMPLEMENTED_YET(LengthsSplit);
} // namespace caffe2