parent
71b63c3fcf
commit
f0c07c3fa6
@ -0,0 +1,146 @@
|
||||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed 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.
|
||||
*/
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "common/utils.h"
|
||||
#include "dataset/engine/data_buffer.h"
|
||||
#include "dataset/engine/datasetops/take_op.h"
|
||||
#include "dataset/engine/db_connector.h"
|
||||
#include "dataset/engine/execution_tree.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace dataset {
|
||||
// Builder constructor. Creates the builder object.
|
||||
TakeOp::Builder::Builder(int32_t count) : build_max_takes_(count) {}
|
||||
|
||||
Status TakeOp::Builder::SanityCheck() const {
|
||||
if (build_max_takes_ <= 0) {
|
||||
std::string err_msg("Take count must be greater than 0.");
|
||||
RETURN_STATUS_UNEXPECTED(err_msg);
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
// The builder "build" method creates the final object.
|
||||
Status TakeOp::Builder::Build(std::shared_ptr<TakeOp> *ptr) {
|
||||
RETURN_IF_NOT_OK(SanityCheck());
|
||||
*ptr = std::make_shared<TakeOp>(build_max_takes_);
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
// Constructor of the TakeOp.
|
||||
TakeOp::TakeOp(int32_t count) : PipelineOp(0), max_takes_(count), take_count_(0) {}
|
||||
|
||||
// A print method typically used for debugging
|
||||
void TakeOp::Print(std::ostream &out, bool show_all) const {
|
||||
// Call base class printer first
|
||||
PipelineOp::Print(out, show_all);
|
||||
|
||||
// Then display our own stuff
|
||||
out << "TakeOp:"
|
||||
<< "\nCurrent take count: " << take_count_ << "\nMax take count: " << max_takes_;
|
||||
}
|
||||
|
||||
// This function will be call muti times to returns the buffer, when meet required max take count or meet
|
||||
// EOF buffer then this will stop.
|
||||
Status TakeOp::GetNextBuffer(std::unique_ptr<DataBuffer> *p_buffer, int32_t worker_id, bool retry_if_eoe) {
|
||||
if (child_.empty()) {
|
||||
RETURN_STATUS_UNEXPECTED("TakeOp can't be the leaf node.");
|
||||
}
|
||||
|
||||
std::unique_ptr<DataBuffer> buf;
|
||||
|
||||
bool last_repeat = !BitTest(op_ctrl_flags_, kDeOpRepeated) || BitTest(op_ctrl_flags_, kDeOpLastRepeat);
|
||||
if (take_count_ == max_takes_) {
|
||||
if (state_ == OpState::kDeOpRunning) {
|
||||
MS_LOG(INFO) << "meet max count and push-back eoe buffer.";
|
||||
auto eoe_buffer = std::make_unique<DataBuffer>(0, DataBuffer::kDeBFlagEOE);
|
||||
*p_buffer = std::move(eoe_buffer);
|
||||
state_ = OpState::kDeOpIdle;
|
||||
|
||||
// Reset the count and drain
|
||||
if (!last_repeat) {
|
||||
take_count_ = 0;
|
||||
RETURN_IF_NOT_OK(child_[0]->GetNextBuffer(&buf, worker_id, true));
|
||||
while (!buf->eoe() && !buf->eof()) {
|
||||
RETURN_IF_NOT_OK(child_[0]->GetNextBuffer(&buf, worker_id, true));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
MS_LOG(INFO) << "meet max count and push-back eof buffer.";
|
||||
auto eof_buffer = std::make_unique<DataBuffer>(0, DataBuffer::kDeBFlagEOF);
|
||||
*p_buffer = std::move(eof_buffer);
|
||||
take_count_ = 0;
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
RETURN_IF_NOT_OK(child_[0]->GetNextBuffer(&buf, worker_id, true));
|
||||
// Loop until non EOE is received
|
||||
if (buf->eoe()) {
|
||||
take_count_ = 0;
|
||||
*p_buffer = std::move(buf);
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
// Check if the last buf is next eof
|
||||
if (buf->eof()) {
|
||||
*p_buffer = std::move(buf);
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
// Get buffer and push back when take_count is still small
|
||||
if (take_count_ < max_takes_) {
|
||||
RETURN_IF_NOT_OK(FillBuffer(&buf, p_buffer));
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
// Function FillBuffer mainly prepare the buffer for returning
|
||||
Status TakeOp::FillBuffer(std::unique_ptr<DataBuffer> *buffer, std::unique_ptr<DataBuffer> *data_buffer) {
|
||||
int32_t buffer_size = (*buffer)->NumRows();
|
||||
if (take_count_ + buffer_size < max_takes_) {
|
||||
*data_buffer = std::move(*buffer);
|
||||
take_count_ = take_count_ + buffer_size;
|
||||
} else {
|
||||
MS_LOG(INFO) << "In last buffer: Push one buffer.";
|
||||
std::unique_ptr<TensorQTable> new_tensor_table = std::make_unique<TensorQTable>();
|
||||
while (take_count_ < max_takes_) {
|
||||
TensorRow new_row;
|
||||
RETURN_IF_NOT_OK((*buffer)->PopRow(&new_row));
|
||||
take_count_++;
|
||||
new_tensor_table->push_back(new_row);
|
||||
}
|
||||
(*buffer)->set_tensor_table(std::move(new_tensor_table));
|
||||
*data_buffer = std::move(*buffer);
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
// Class functor operator () override.
|
||||
// Most dataset ops operate by launching a thread (see ExecutionTree).
|
||||
// However, the TakeOp is defined as a inlined operator, so it is invalid to launch the
|
||||
// functor since this op runs inlined inside another operator. The function is overloaded to
|
||||
// ensure that it is not called by mistake (it will generate an error).
|
||||
Status TakeOp::operator()() { RETURN_STATUS_UNEXPECTED("Logic error. TakeOp is an inlined operator."); }
|
||||
|
||||
Status TakeOp::PrepareNodePostAction() {
|
||||
RETURN_IF_NOT_OK(PipelineOp::PrepareNodePostAction());
|
||||
tree_->AddToRepeatStack(shared_from_this());
|
||||
return Status::OK();
|
||||
}
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
@ -0,0 +1,107 @@
|
||||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed 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.
|
||||
*/
|
||||
#ifndef DATASET_ENGINE_DATASETOPS_TAKE_OP_H_
|
||||
#define DATASET_ENGINE_DATASETOPS_TAKE_OP_H_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "dataset/engine/datasetops/pipeline_op.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace dataset {
|
||||
class TakeOp : public PipelineOp {
|
||||
public:
|
||||
// The nested builder class inside of the TakeOp is used to help manage all of the arguments
|
||||
// for constructing it. This take op is very simple though, so this builder is really just
|
||||
// provided for a consistent look and feel for creators of Dataset operators overall.
|
||||
class Builder {
|
||||
public:
|
||||
// Builder constructor. Creates the builder object.
|
||||
// @note No default args
|
||||
// @param count - The number of takes to do
|
||||
// @return This is a constructor.
|
||||
explicit Builder(int32_t count);
|
||||
|
||||
// Default destructor
|
||||
~Builder() = default;
|
||||
|
||||
// The builder "build" method creates the final object.
|
||||
// @return shared_ptr to the new StorageOp object
|
||||
Status Build(std::shared_ptr<TakeOp> *);
|
||||
|
||||
private:
|
||||
int32_t build_max_takes_;
|
||||
|
||||
Status SanityCheck() const;
|
||||
};
|
||||
|
||||
// Constructor of the TakeOp.
|
||||
// @note The builder class should be used to call it
|
||||
// @param count - The number of takes to do
|
||||
explicit TakeOp(int32_t count);
|
||||
|
||||
// Destructor
|
||||
~TakeOp() = default;
|
||||
|
||||
// A print method typically used for debugging
|
||||
// @param out - The output stream to write output to
|
||||
// @param show_all - A bool to control if you want to show all info or just a summary
|
||||
void Print(std::ostream &out, bool show_all) const override;
|
||||
|
||||
// << Stream output operator overload
|
||||
// @notes This allows you to write the debug print info using stream operators
|
||||
// @param out - reference to the output stream being overloaded
|
||||
// @param ro - reference to the TakeOp to display
|
||||
// @return - the output stream must be returned
|
||||
friend std::ostream &operator<<(std::ostream &out, const TakeOp &ro) {
|
||||
ro.Print(out, false);
|
||||
return out;
|
||||
}
|
||||
|
||||
// Class functor operator () override.
|
||||
// Most dataset ops operate by launching a thread (see ExecutionTree).
|
||||
// However, the TakeOp is defined as a inlined operator, so it is invalid to launch the
|
||||
// functor since this op runs inlined inside another operator. The function is overloaded to
|
||||
// ensure that it is not called by mistake (it will generate an error).
|
||||
// @return Status - The error code return
|
||||
Status operator()() override;
|
||||
|
||||
// Gets a buffer from the child node. The caller is typically our parent node.
|
||||
// @note This function sets the `retryIfEoe` flag when popping from the child connector. This way,
|
||||
// this function will retry to pop the connector again and will get the non-EOE buffer if any.
|
||||
// @param p_buffer - output pointer to the buffer that it will fetch.
|
||||
// @param worker_id - The worker id
|
||||
// @param retry_if_eoe Set this flag to true to allow calling pop() again after the first pop() returns EOE.
|
||||
// @return Status - The error code return
|
||||
Status GetNextBuffer(std::unique_ptr<DataBuffer> *p_buffer, int32_t worker_id, bool retry_if_eoe) override;
|
||||
|
||||
// During tree prepare phase, operators may have specific post-operations to perform depending on
|
||||
// their role.
|
||||
// @notes Derived versions of this function should always call it's superclass version first
|
||||
// before providing their own implementations.
|
||||
Status PrepareNodePostAction() override;
|
||||
|
||||
private:
|
||||
int32_t max_takes_; // The number of takes that the user requested
|
||||
int32_t take_count_; // A counter for the current number of executed takes
|
||||
|
||||
Status FillBuffer(std::unique_ptr<DataBuffer> *buffer, std::unique_ptr<DataBuffer> *data_buffer);
|
||||
};
|
||||
} // namespace dataset
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // DATASET_ENGINE_DATASETOPS_TAKE_OP_H_
|
@ -0,0 +1,103 @@
|
||||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed 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.
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "common/common.h"
|
||||
#include "common/utils.h"
|
||||
#include "dataset/core/client.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "utils/log_adapter.h"
|
||||
|
||||
namespace common = mindspore::common;
|
||||
|
||||
using namespace mindspore::dataset;
|
||||
using mindspore::MsLogLevel::INFO;
|
||||
using mindspore::ExceptionType::NoExceptionType;
|
||||
using mindspore::LogStream;
|
||||
|
||||
class MindDataTestTakeOp : public UT::DatasetOpTesting {};
|
||||
|
||||
TEST_F(MindDataTestTakeOp, TestTakeProject) {
|
||||
// Start with an empty execution tree
|
||||
auto my_tree = std::make_shared<ExecutionTree>();
|
||||
|
||||
std::string dataset_path;
|
||||
dataset_path = datasets_root_path_ + "/testTFTestAllTypes/test.data";
|
||||
|
||||
// TFReaderOp
|
||||
std::shared_ptr<TFReaderOp> my_tfreader_op;
|
||||
TFReaderOp::Builder builder;
|
||||
builder.SetDatasetFilesList({dataset_path})
|
||||
.SetRowsPerBuffer(16)
|
||||
.SetWorkerConnectorSize(16)
|
||||
.SetNumWorkers(16);
|
||||
std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
|
||||
schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema.json", {});
|
||||
builder.SetDataSchema(std::move(schema));
|
||||
Status rc = builder.Build(&my_tfreader_op);
|
||||
ASSERT_TRUE(rc.IsOk());
|
||||
|
||||
// TakeOp
|
||||
std::shared_ptr<TakeOp> my_take_op;
|
||||
TakeOp::Builder builder_take(5);
|
||||
rc = builder_take.Build(&my_take_op);
|
||||
ASSERT_TRUE(rc.IsOk());
|
||||
|
||||
rc = my_tree->AssociateNode(my_tfreader_op);
|
||||
ASSERT_TRUE(rc.IsOk());
|
||||
rc = my_tree->AssociateNode(my_take_op);
|
||||
ASSERT_TRUE(rc.IsOk());
|
||||
|
||||
// Set children/root layout.
|
||||
rc = my_take_op->AddChild(my_tfreader_op);
|
||||
ASSERT_TRUE(rc.IsOk());
|
||||
rc = my_tree->AssignRoot(my_take_op);
|
||||
ASSERT_TRUE(rc.IsOk());
|
||||
|
||||
MS_LOG(INFO) << "Launching tree and begin iteration.";
|
||||
rc = my_tree->Prepare();
|
||||
|
||||
ASSERT_TRUE(rc.IsOk());
|
||||
|
||||
rc = my_tree->Launch();
|
||||
ASSERT_TRUE(rc.IsOk());
|
||||
|
||||
// Start the loop of reading tensors from our pipeline
|
||||
DatasetIterator di(my_tree);
|
||||
TensorRow tensor_list;
|
||||
rc = di.FetchNextTensorRow(&tensor_list);
|
||||
ASSERT_TRUE(rc.IsOk());
|
||||
|
||||
int row_count = 0;
|
||||
while (!tensor_list.empty()) {
|
||||
MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
|
||||
|
||||
// Display the tensor by calling the printer on it
|
||||
for (int i = 0; i < tensor_list.size(); i++) {
|
||||
std::ostringstream ss;
|
||||
ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
|
||||
MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
|
||||
}
|
||||
|
||||
rc = di.FetchNextTensorRow(&tensor_list);
|
||||
ASSERT_TRUE(rc.IsOk());
|
||||
row_count++;
|
||||
}
|
||||
|
||||
ASSERT_EQ(row_count, 5);
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue