Skip to content

Commit

Permalink
Rename TensorVector to TensorList (#4220)
Browse files Browse the repository at this point in the history
TensorList was replaced by a refactored TensorVector that supports both
the TensorList APIs as well as non-contiguous mode in which in can be
constructed from individual samples. The TensorList was kept as an
alias to TensorVector to limit the code changes.

As the TensorList name is more prelevant across DALI codebase, TensorVector
is renamed into TensorList leaving us with just one intended batch type.

The TensorVector was kept as [[deprecated]] alias to support plugins
in backward compatible way.

The tests are merged into one file, taking the ones from original
TensorVector and the ones that were ported to it from TensorList tests.
Old TensorList tests are removed as they were less generic.

Changes are fully automated, both the search&replace as well as code
formatting.

Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
  • Loading branch information
klecki committed Sep 6, 2022
1 parent 41e8107 commit 0ab8cc8
Show file tree
Hide file tree
Showing 57 changed files with 2,467 additions and 3,097 deletions.
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Examples:
If you select `Existing tests apply` option, please list which test cases cover the introduced
functionality. For example:
- test_operator_gaussian_blur.py: test_gaussian*
- tensor_vector_test.cc: TensorVectorVariableBatchSizeTest*
- tensor_list_test.cc: TensorListVariableBatchSizeTest*
--->
- [ ] Existing tests apply
- [ ] New tests added
Expand Down
4 changes: 2 additions & 2 deletions dali/benchmark/displacement_cpu_bench.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ void DisplacementBench(benchmark::State& st) {//NOLINT

// The inputs and outputs to CPUBackend are: shared_ptr<Tensor<CPUBackend>>;
// create input and output, initialize input
auto tensor_in = std::make_shared<TensorVector<CPUBackend>>(1);
auto tensor_out = std::make_shared<TensorVector<CPUBackend>>(1);
auto tensor_in = std::make_shared<TensorList<CPUBackend>>(1);
auto tensor_out = std::make_shared<TensorList<CPUBackend>>(1);
// If we want to specify input, we can share data
// tensor_in->ShareData(img, N * sizeof(T));
// Here we let underlying buffer allocate it by itself. We have to specify size and type
Expand Down
4 changes: 2 additions & 2 deletions dali/benchmark/operator_bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class OperatorBench : public DALIBenchmark {

auto op_ptr = InstantiateOperator(op_spec);

auto data_in = std::make_shared<TensorVector<CPUBackend>>(batch_size);
auto data_in = std::make_shared<TensorList<CPUBackend>>(batch_size);
data_in->set_type<T>();
data_in->Resize(uniform_list_shape(batch_size, TensorShape<>{H, W, C}));
data_in->SetLayout("HWC");
Expand All @@ -72,7 +72,7 @@ class OperatorBench : public DALIBenchmark {
ThreadPool tp(num_threads, 0, false, "OperatorBench");
ws.SetThreadPool(&tp);

Setup<TensorVector<CPUBackend>>(op_ptr, op_spec, ws, batch_size);
Setup<TensorList<CPUBackend>>(op_ptr, op_spec, ws, batch_size);
op_ptr->Run(ws);
for (auto _ : st) {
op_ptr->Run(ws);
Expand Down
2 changes: 1 addition & 1 deletion dali/c_api/c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void SetExternalInputTensors(daliPipelineHandle *pipe_handle, const char *name,
// that is tied to the pipeline.
int device_id = pipeline->device_id();

dali::TensorVector<Backend> data(curr_batch_size);
dali::TensorList<Backend> data(curr_batch_size);
data.set_pinned(flags & DALI_ext_pinned);
data.set_sample_dim(sample_dim);
data.set_type(type_id);
Expand Down
2 changes: 1 addition & 1 deletion dali/operators/decoder/decoder_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class DecodeTestBase : public GenericDecoderTest<ImgType> {
DeviceWorkspace *ws) override {
// single input - encoded images
// single output - decoded images
TensorVector<CPUBackend> out(inputs[0]->num_samples());
TensorList<CPUBackend> out(inputs[0]->num_samples());
std::vector<Tensor<CPUBackend>> tmp_out(inputs[0]->num_samples());
const TensorList<CPUBackend> &encoded_data = *inputs[0];
const int c = this->GetNumColorComp();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ class nvJPEGDecoder : public Operator<MixedBackend>, CachedDecoderImpl {
nvjpeg_params_.resize(samples_hw_batched_.size());

int j = 0;
TensorVector<CPUBackend> tv(samples_hw_batched_.size());
TensorList<CPUBackend> tv(samples_hw_batched_.size());

const auto &input = ws.Input<CPUBackend>(0);
tv.SetupLike(input);
Expand Down
8 changes: 4 additions & 4 deletions dali/operators/generic/constant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ If set and not empty, the layout must match the dimensionality of the output.)co

namespace {
template <typename Dst, typename Src>
void FillTensorVector(
TensorVector<CPUBackend> &dst, const TensorListShape<> &shape, const std::vector<Src> &src) {
void FillTensorList(TensorList<CPUBackend> &dst, const TensorListShape<> &shape,
const std::vector<Src> &src) {
dst.SetContiguity(BatchContiguity::Noncontiguous);
dst.set_type<Dst>();
dst.Resize(shape);
Expand Down Expand Up @@ -103,9 +103,9 @@ void Constant<CPUBackend>::RunImpl(HostWorkspace &ws) {
TYPE_SWITCH(output_type_, type2id, type, CONSTANT_OP_SUPPORTED_TYPES,
(
if (!fdata_.empty()) {
FillTensorVector<type>(output_, max_output_shape_, fdata_);
FillTensorList<type>(output_, max_output_shape_, fdata_);
} else {
FillTensorVector<type>(output_, max_output_shape_, idata_);
FillTensorList<type>(output_, max_output_shape_, idata_);
}
), (DALI_FAIL(make_string("Unsupported type: ", output_type_)))); // NOLINT
}
Expand Down
2 changes: 1 addition & 1 deletion dali/operators/generic/constant.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Constant : public Operator<Backend> {
TensorLayout layout_;
DALIDataType output_type_;
using storage_t = std::conditional_t<std::is_same<Backend, CPUBackend>::value,
TensorVector<CPUBackend>, TensorList<GPUBackend>>;
TensorList<CPUBackend>, TensorList<GPUBackend>>;
storage_t output_;
};

Expand Down
4 changes: 2 additions & 2 deletions dali/operators/generic/lookup_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ namespace dali {
namespace {

template <typename Output, typename Input>
void LookupValuesImpl(ThreadPool &tp, TensorVector<CPUBackend> &output,
const TensorVector<CPUBackend> &input, const TensorListShape<> &shape,
void LookupValuesImpl(ThreadPool &tp, TensorList<CPUBackend> &output,
const TensorList<CPUBackend> &input, const TensorListShape<> &shape,
const Output *lookup_table, const Output default_value) {
for (int sample_idx = 0; sample_idx < shape.num_samples(); sample_idx++) {
auto data_size = shape.tensor_size(sample_idx);
Expand Down
2 changes: 1 addition & 1 deletion dali/operators/generic/shapes.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Shapes : public Operator<Backend> {
}

template <typename type>
void ConvertShape(TensorVector<CPUBackend> &out, const TensorListShape<> &shape) {
void ConvertShape(TensorList<CPUBackend> &out, const TensorListShape<> &shape) {
int n = out.num_samples();
assert(n == shape.num_samples());
for (int i = 0; i < n; i++) {
Expand Down
6 changes: 3 additions & 3 deletions dali/operators/generic/slice/subscript.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -65,7 +65,7 @@ struct SubscriptArg {

void Load(const ArgumentWorkspace &ws, int batch_size) {
if (src == ArgSource::Input) {
const TensorVector<CPUBackend> &inp = ws.ArgumentInput(name);
const TensorList<CPUBackend> &inp = ws.ArgumentInput(name);
const auto &shape = inp.shape();

int n = batch_size;
Expand All @@ -80,7 +80,7 @@ struct SubscriptArg {
DALIDataType type_id = inp.type();
TYPE_SWITCH(type_id, type2id, T, (INTEGER_TYPES), (
for (int i = 0; i < n; i++) {
// TODO(michalz): Add tensor<T> and mutable_tensor<T> to TensorVector?
// TODO(michalz): Add tensor<T> and mutable_tensor<T> to TensorList?
values[i] = ConvertSat<int64_t>(static_cast<const T*>(inp.raw_tensor(i))[0]);
}
), DALI_FAIL(make_string("Array index must be of integral type. Got: ", type_id))); // NOLINT
Expand Down
18 changes: 9 additions & 9 deletions dali/operators/geometry/mt_transform_attr_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ TEST(MTTransformAttr, MTVector) {
TEST(MTTransformAttr, MInputTInput) {
OpSpec spec("MTTransformAttr");
ArgumentWorkspace ws;
auto Minp = std::make_shared<TensorVector<CPUBackend>>();
auto Tinp = std::make_shared<TensorVector<CPUBackend>>();
auto Minp = std::make_shared<TensorList<CPUBackend>>();
auto Tinp = std::make_shared<TensorList<CPUBackend>>();
Minp->set_pinned(false);
Tinp->set_pinned(false);
TensorListShape<2> Mtls = {{{ 2, 3 }, { 2, 3 }}};
Expand Down Expand Up @@ -186,8 +186,8 @@ TEST(MTTransformAttr, MInputTInput) {
TEST(MTTransformAttr, MScalarInputTScalarInput) {
OpSpec spec("MTTransformAttr");
ArgumentWorkspace ws;
auto Minp = std::make_shared<TensorVector<CPUBackend>>();
auto Tinp = std::make_shared<TensorVector<CPUBackend>>();
auto Minp = std::make_shared<TensorList<CPUBackend>>();
auto Tinp = std::make_shared<TensorList<CPUBackend>>();
Minp->set_pinned(false);
Tinp->set_pinned(false);

Expand Down Expand Up @@ -227,7 +227,7 @@ TEST(MTTransformAttr, MScalarInputTScalarInput) {
TEST(MTTransformAttr, MTInput) {
OpSpec spec("MTTransformAttr");
ArgumentWorkspace ws;
auto MTinp = std::make_shared<TensorVector<CPUBackend>>();
auto MTinp = std::make_shared<TensorList<CPUBackend>>();
MTinp->set_pinned(false);
TensorListShape<> tls = {{{ 2, 3 }, { 2, 3 }}};
int N = tls.num_samples();;
Expand Down Expand Up @@ -302,8 +302,8 @@ TEST(MTTransformAttr, MVectorTScalar_ErrorNotDivisible) {
TEST(MTTransformAttr, MInputTInput_Error) {
OpSpec spec("MTTransformAttr");
ArgumentWorkspace ws;
auto Minp = std::make_shared<TensorVector<CPUBackend>>();
auto Tinp = std::make_shared<TensorVector<CPUBackend>>();
auto Minp = std::make_shared<TensorList<CPUBackend>>();
auto Tinp = std::make_shared<TensorList<CPUBackend>>();
Minp->set_pinned(false);
Tinp->set_pinned(false);
TensorListShape<2> Mtls = {{{ 2, 3 }, { 2, 3 }}};
Expand Down Expand Up @@ -336,7 +336,7 @@ TEST(MTTransformAttr, MInputTInput_Error) {
TEST(MTTransformAttr, MTInput_ErrorSize) {
OpSpec spec("MTTransformAttr");
ArgumentWorkspace ws;
auto MTinp = std::make_shared<TensorVector<CPUBackend>>();
auto MTinp = std::make_shared<TensorList<CPUBackend>>();
MTinp->set_pinned(false);
TensorListShape<> tls = {{{ 2, 3 }, { 2, 3 }}};
int N = tls.num_samples();;
Expand All @@ -357,7 +357,7 @@ TEST(MTTransformAttr, MTInput_ErrorSize) {
TEST(MTTransformAttr, MInput_ZeroRows) {
OpSpec spec("MTTransformAttr");
ArgumentWorkspace ws;
auto MTinp = std::make_shared<TensorVector<CPUBackend>>();
auto MTinp = std::make_shared<TensorList<CPUBackend>>();
MTinp->set_pinned(false);
TensorListShape<> tls = {{{ 0, 3 }, { 0, 3 }}};
int N = tls.num_samples();;
Expand Down
2 changes: 1 addition & 1 deletion dali/operators/image/remap/displacement_filter_impl_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class DisplacementFilter<CPUBackend, Displacement, per_channel_transform>
float fill_value_;

bool has_mask_;
const TensorVector<CPUBackend> *mask_ = nullptr;
const TensorList<CPUBackend> *mask_ = nullptr;
};

} // namespace dali
Expand Down
4 changes: 2 additions & 2 deletions dali/operators/image/remap/warp_affine_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class WarpAffineParamProvider
}
}

void UseInputAsParams(const TensorVector<CPUBackend> &input, bool invert) {
void UseInputAsParams(const TensorList<CPUBackend> &input, bool invert) {
CheckParamInput(input);

auto *params = this->template AllocParams<mm::memory_kind::host>();
Expand All @@ -127,7 +127,7 @@ class WarpAffineParamProvider
params[i] = *static_cast<const MappingParams *>(input.raw_tensor(i));
}
}
}
}

void UseInputAsParams(const TensorList<GPUBackend> &input, bool invert) {
CheckParamInput(input);
Expand Down
14 changes: 7 additions & 7 deletions dali/operators/image/remap/warp_param_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ class InterpTypeProvider {
void SetInterp(const OpSpec &spec, const ArgumentWorkspace &ws, int num_samples) {
interp_types_.clear();
if (spec.HasTensorArgument("interp_type")) {
auto &tensor_vector = ws.ArgumentInput("interp_type");
int n = tensor_vector.shape().num_samples();
auto &tensor_list = ws.ArgumentInput("interp_type");
int n = tensor_list.shape().num_samples();
DALI_ENFORCE(n == 1 || n == num_samples,
"interp_type must be a single value or contain one value per sample");
interp_types_.resize(n);
for (int i = 0; i < n; i++)
interp_types_[i] = tensor_vector.tensor<DALIInterpType>(i)[0];
interp_types_[i] = tensor_list.tensor<DALIInterpType>(i)[0];
} else {
interp_types_.resize(1, spec.template GetArgument<DALIInterpType>("interp_type"));
}
Expand Down Expand Up @@ -264,12 +264,12 @@ class WarpParamProvider : public InterpTypeProvider, public BorderTypeProvider<B

virtual void GetExplicitPerSampleSize(std::vector<SpatialShape> &out_sizes) const {
assert(HasExplicitPerSampleSize());
const auto &tensor_vector = ws_->ArgumentInput(size_arg_name_);
TYPE_SWITCH(tensor_vector.type(), type2id, shape_t,
const auto &tensor_list = ws_->ArgumentInput(size_arg_name_);
TYPE_SWITCH(tensor_list.type(), type2id, shape_t,
(int16_t, int32_t, int64_t, uint16_t, uint32_t, uint64_t, float),
(GetTypedPerSampleSize(out_sizes, view<const shape_t>(tensor_vector))),
(GetTypedPerSampleSize(out_sizes, view<const shape_t>(tensor_list))),
(DALI_FAIL(make_string("Warp: Unsupported argument type for \"", size_arg_name_, "\": ",
tensor_vector.type())))
tensor_list.type())))
); // NOLINT
}

Expand Down
5 changes: 2 additions & 3 deletions dali/operators/image/resize/resize_op_impl_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ class ResizeOpImplCPU : public ResizeBase<CPUBackend>::Impl {
}
}

void RunResize(HostWorkspace &ws,
TensorVector<CPUBackend> &output,
const TensorVector<CPUBackend> &input) override {
void RunResize(HostWorkspace &ws, TensorList<CPUBackend> &output,
const TensorList<CPUBackend> &input) override {
auto in_view = view<const In>(input);
auto in_frames_view = reshape(in_view, in_shape_, true);
auto out_view = view<Out>(output);
Expand Down
18 changes: 9 additions & 9 deletions dali/operators/math/expressions/arithmetic_test.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019-2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// Copyright (c) 2019-2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,10 +33,10 @@ TEST(ArithmeticOpsTest, TreePropagation) {
auto expr = ParseExpressionString(expr_str);
auto &expr_ref = *expr;
HostWorkspace ws;
std::shared_ptr<TensorVector<CPUBackend>> in[3];
std::shared_ptr<TensorList<CPUBackend>> in[3];
DALIDataType types[3] = {DALI_UINT8, DALI_INT16, DALI_INT32};
for (int i = 0; i < 3; i++) {
in[i] = std::make_shared<TensorVector<CPUBackend>>();
in[i] = std::make_shared<TensorList<CPUBackend>>();
in[i]->Resize({{1}, {2}}, types[i]);
}
in[0]->SetLayout(TensorLayout());
Expand Down Expand Up @@ -68,9 +68,9 @@ TEST(ArithmeticOpsTest, PropagateScalarInput) {
auto expr = ParseExpressionString(expr_str);
auto &expr_ref = *expr;
HostWorkspace ws;
std::shared_ptr<TensorVector<CPUBackend>> in[1];
std::shared_ptr<TensorList<CPUBackend>> in[1];
for (auto &ptr : in) {
ptr = std::make_shared<TensorVector<CPUBackend>>();
ptr = std::make_shared<TensorList<CPUBackend>>();
ptr->Resize({{}, {}}, DALI_INT32);
}
ws.AddInput(in[0]);
Expand All @@ -85,9 +85,9 @@ TEST(ArithmeticOpsTest, PreservePseudoScalarInput) {
auto expr = ParseExpressionString(expr_str);
auto &expr_ref = *expr;
HostWorkspace ws;
std::shared_ptr<TensorVector<CPUBackend>> in[1];
std::shared_ptr<TensorList<CPUBackend>> in[1];
for (auto &ptr : in) {
ptr = std::make_shared<TensorVector<CPUBackend>>();
ptr = std::make_shared<TensorList<CPUBackend>>();
ptr->Resize({{1}, {1}}, DALI_INT32);
}
ws.AddInput(in[0]);
Expand All @@ -102,9 +102,9 @@ TEST(ArithmeticOpsTest, TreePropagationError) {
auto expr = ParseExpressionString(expr_str);
auto &expr_ref = *expr;
HostWorkspace ws;
std::shared_ptr<TensorVector<CPUBackend>> in[3];
std::shared_ptr<TensorList<CPUBackend>> in[3];
for (auto &ptr : in) {
ptr = std::make_shared<TensorVector<CPUBackend>>();
ptr = std::make_shared<TensorList<CPUBackend>>();
ptr->Resize({{1}, {2}}, DALI_INT32);
}
in[0]->SetLayout(TensorLayout());
Expand Down
4 changes: 2 additions & 2 deletions dali/operators/python_function/dltensor_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ TensorListShape<> GetDLTensorListShape(const std::vector<DLMTensorPtr>& dl_tenso
}

template <>
void CopyOutputData(TensorVector<CPUBackend> &output, std::vector<DLMTensorPtr> &dl_tensors,
int batch_size, HostWorkspace &workspace) {
void CopyOutputData(TensorList<CPUBackend> &output, std::vector<DLMTensorPtr> &dl_tensors,
int batch_size, HostWorkspace &workspace) {
auto &thread_pool = workspace.GetThreadPool();
auto out_shape = output.shape();
for (int i = 0; i < batch_size; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion dali/operators/reader/nemo_asr_reader_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ NemoAsrReader::NemoAsrReader(const OpSpec& spec)

prefetched_decoded_audio_.resize(prefetch_queue_depth_);
for (auto& batch : prefetched_decoded_audio_) {
batch = std::make_unique<TensorVector<CPUBackend>>();
batch = std::make_unique<TensorList<CPUBackend>>();
batch->set_pinned(false);
}
}
Expand Down
4 changes: 2 additions & 2 deletions dali/operators/reader/nemo_asr_reader_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class NemoAsrReader : public DataReader<CPUBackend, AsrSample> {
ThreadPool thread_pool_;

// prefetch_depth * batch_size set of buffers that we reuse to decode audio
using TensorVectorPtr = std::unique_ptr<TensorVector<CPUBackend>>;
std::vector<TensorVectorPtr> prefetched_decoded_audio_;
using TensorListPtr = std::unique_ptr<TensorList<CPUBackend>>;
std::vector<TensorListPtr> prefetched_decoded_audio_;

std::unordered_map<void*, int> decoded_map_;
};
Expand Down
2 changes: 1 addition & 1 deletion dali/operators/segmentation/random_object_bbox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ bool RandomObjectBBox::PickForegroundBox(SampleContext<BlobLabel> &context) {
return ret;
}

void RandomObjectBBox::AllocateTempStorage(const TensorVector<CPUBackend> &input) {
void RandomObjectBBox::AllocateTempStorage(const TensorList<CPUBackend> &input) {
int64_t max_blob_bytes = 0;
int64_t max_filtered_bytes = 0;
int N = input.num_samples();
Expand Down
2 changes: 1 addition & 1 deletion dali/operators/segmentation/random_object_bbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class RandomObjectBBox : public Operator<CPUBackend> {

void GetBgFgAndWeights(ClassVec &classes, WeightVec &weights, int &background, int sample_idx);

void AllocateTempStorage(const TensorVector<CPUBackend> &tls);
void AllocateTempStorage(const TensorList<CPUBackend> &tls);

template <typename BlobLabel>
struct SampleContext {
Expand Down
Loading

0 comments on commit 0ab8cc8

Please sign in to comment.