Skip to content

Commit

Permalink
Change DALIOpType to dali::OpType enum class (#597)
Browse files Browse the repository at this point in the history
Remove cpu_op(), gpu_op(), etc from OpGraph
to simplify access to nodes.

Signed-off-by: Krzysztof Lecki <klecki@nvidia.com>
  • Loading branch information
klecki committed Mar 6, 2019
1 parent 57012b6 commit fdf5617
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 153 deletions.
23 changes: 23 additions & 0 deletions dali/common.h
Expand Up @@ -55,6 +55,29 @@ using uint32 = uint32_t;
// Basic data type for our indices and dimension sizes
typedef int64_t Index;

enum class OpType {
GPU = 0,
CPU = 1,
MIXED = 2,
SUPPORT = 3,
COUNT = 4
};

static std::string to_string(OpType op_type) {
switch (op_type) {
case OpType::CPU:
return "cpu";
case OpType::GPU:
return "gpu";
case OpType::MIXED:
return "mixed";
case OpType::SUPPORT:
return "support";
default:
return "<invalid>";
}
}

struct DALISize {
int width;
int height;
Expand Down
48 changes: 24 additions & 24 deletions dali/pipeline/executor/executor.cc
Expand Up @@ -218,10 +218,10 @@ void Executor::RunGPU() {

// Record events for each output requested by the user
cudaEvent_t event = gpu_output_events_[i].GetEvent(queue_idx);
if (graph_->NodeType(src_id) == DALI_MIXED) {
if (graph_->NodeType(src_id) == OpType::MIXED) {
auto &ws = wsb.mixed_op_data[src_idx];
CUDA_CALL(cudaEventRecord(event, ws.stream()));
} else if (graph_->NodeType(src_id) == DALI_GPU) {
} else if (graph_->NodeType(src_id) == OpType::GPU) {
auto &ws = wsb.gpu_op_data[src_idx];
CUDA_CALL(cudaEventRecord(event, ws.stream()));
} else {
Expand Down Expand Up @@ -401,8 +401,8 @@ void Executor::SetupDataForGraph(WorkspaceBlob *wsb) {
// Get each regular input and add them to this op's workspace.

NodeID parent_node_id = graph_->TensorSourceID(node.spec.Input(j));
DALIOpType parent_op_type = graph_->NodeType(parent_node_id);
DALI_ENFORCE(parent_op_type == DALI_SUPPORT,
OpType parent_op_type = graph_->NodeType(parent_node_id);
DALI_ENFORCE(parent_op_type == OpType::SUPPORT,
"Executor encountered support op with non-support input.");
int parent_idx = graph_->NodeIdx(parent_node_id);
int input_src_idx = graph_->TensorIdxInSource(node.spec.Input(j));
Expand All @@ -427,8 +427,8 @@ void Executor::SetupDataForGraph(WorkspaceBlob *wsb) {
for (int j = 0; j < node.spec.NumRegularInput(); ++j) {
// Get each regular input and add them to this op's workspace.
NodeID parent_node_id = graph_->TensorSourceID(node.spec.Input(j));
DALIOpType parent_op_type = graph_->NodeType(parent_node_id);
DALI_ENFORCE(parent_op_type == DALI_CPU,
OpType parent_op_type = graph_->NodeType(parent_node_id);
DALI_ENFORCE(parent_op_type == OpType::CPU,
"Executor encountered cpu op with non-cpu input.");
int parent_idx = graph_->NodeIdx(parent_node_id);
int input_src_idx = graph_->TensorIdxInSource(node.spec.Input(j));
Expand All @@ -442,8 +442,8 @@ void Executor::SetupDataForGraph(WorkspaceBlob *wsb) {
for (const auto &arg_pair : node.spec.ArgumentInputs()) {
// Get each argument input and add them to this op's workspace.
NodeID parent_node_id = graph_->TensorSourceID(node.spec.Input(arg_pair.second));
DALIOpType parent_op_type = graph_->NodeType(parent_node_id);
DALI_ENFORCE(parent_op_type == DALI_SUPPORT,
OpType parent_op_type = graph_->NodeType(parent_node_id);
DALI_ENFORCE(parent_op_type == OpType::SUPPORT,
"Executor encountered argument input produced by non-cpu op.");
int parent_idx = graph_->NodeIdx(parent_node_id);
int input_src_idx = graph_->TensorIdxInSource(node.spec.Input(arg_pair.second));
Expand Down Expand Up @@ -478,8 +478,8 @@ void Executor::SetupDataForGraph(WorkspaceBlob *wsb) {
// Go get each set of input Tensors and add
// them to this mixed ops workspace.
NodeID parent_node_id = graph_->TensorSourceID(node.spec.Input(j));
DALIOpType parent_op_type = graph_->NodeType(parent_node_id);
DALI_ENFORCE(parent_op_type == DALI_CPU,
OpType parent_op_type = graph_->NodeType(parent_node_id);
DALI_ENFORCE(parent_op_type == OpType::CPU,
"Executor encountered mixed op with non-cpu input.");
int parent_idx = graph_->NodeIdx(parent_node_id);
int input_src_idx = graph_->TensorIdxInSource(node.spec.Input(j));
Expand Down Expand Up @@ -516,11 +516,11 @@ void Executor::SetupDataForGraph(WorkspaceBlob *wsb) {
for (int j = 0; j < node.spec.NumRegularInput(); ++j) {
// Get each input and add them to this GPU op's workspace.
NodeID parent_node_id = graph_->TensorSourceID(node.spec.Input(j));
DALIOpType parent_op_type = graph_->NodeType(parent_node_id);
OpType parent_op_type = graph_->NodeType(parent_node_id);
int parent_idx = graph_->NodeIdx(parent_node_id);
int input_src_idx = graph_->TensorIdxInSource(node.spec.Input(j));

if (parent_op_type == DALI_MIXED) {
if (parent_op_type == OpType::MIXED) {
MixedWorkspace &src_ws = wsb->mixed_op_data[parent_idx];
if (node.spec.InputDevice(j) == "cpu") {
const auto input = src_ws.SharedCPUOutput(input_src_idx);
Expand All @@ -531,7 +531,7 @@ void Executor::SetupDataForGraph(WorkspaceBlob *wsb) {
} else {
DALI_FAIL("Executor encountered gpu op with non-cpu/gpu input.");
}
} else if (parent_op_type == DALI_GPU) {
} else if (parent_op_type == OpType::GPU) {
DeviceWorkspace &src_ws = wsb->gpu_op_data[parent_idx];
if (node.spec.InputDevice(j) == "cpu") {
// Note: This path should currently never occur, as we
Expand All @@ -553,8 +553,8 @@ void Executor::SetupDataForGraph(WorkspaceBlob *wsb) {
for (const auto &arg_pair : node.spec.ArgumentInputs()) {
// Get each argument input and add them to this op's workspace.
NodeID parent_node_id = graph_->TensorSourceID(node.spec.Input(arg_pair.second));
DALIOpType parent_op_type = graph_->NodeType(parent_node_id);
DALI_ENFORCE(parent_op_type == DALI_SUPPORT,
OpType parent_op_type = graph_->NodeType(parent_node_id);
DALI_ENFORCE(parent_op_type == OpType::SUPPORT,
"Executor encountered argument input produced by non-cpu op.");
int parent_idx = graph_->NodeIdx(parent_node_id);
int input_src_idx = graph_->TensorIdxInSource(node.spec.Input(arg_pair.second));
Expand Down Expand Up @@ -679,7 +679,7 @@ void Executor::SetupStreamsForGraph(WorkspaceBlob *wsb) {
ws.set_stream(gpu_op_stream);
const OpNode& node = graph_->gpu_node(i);
for (const auto& p : node.parents) {
if (graph_->NodeType(p) == DALI_MIXED) {
if (graph_->NodeType(p) == OpType::MIXED) {
// We need to block on this op's event to
// make sure that we respect the dependency
int parent_op_idx = graph_->NodeIdx(p);
Expand Down Expand Up @@ -745,14 +745,14 @@ void Executor::SetOutputBuffersForIter(int queue_idx, WorkspaceBlob *wsb) {
NodeID node_id = info.prod_and_idx.first;
int output_idx = info.prod_and_idx.second;
// Contiguous CPU outputs come from mixed or GPU ops
DALI_ENFORCE(graph_->NodeType(node_id) == DALI_MIXED ||
graph_->NodeType(node_id) == DALI_GPU);
DALI_ENFORCE(graph_->NodeType(node_id) == OpType::MIXED ||
graph_->NodeType(node_id) == OpType::GPU);

if (graph_->NodeType(node_id) == DALI_MIXED) {
if (graph_->NodeType(node_id) == OpType::MIXED) {
int mixed_op_id = graph_->NodeIdx(node_id);
wsb->mixed_op_data[mixed_op_id].SetOutput(
output_idx, cpu_outputs_[i].Get(queue_idx));
} else { // DALI_GPU
} else { // OpType::GPU
int gpu_op_id = graph_->NodeIdx(node_id);
wsb->gpu_op_data[gpu_op_id].SetOutput(output_idx,
cpu_outputs_[i].Get(queue_idx));
Expand All @@ -761,7 +761,7 @@ void Executor::SetOutputBuffersForIter(int queue_idx, WorkspaceBlob *wsb) {
for (size_t j = 0; j < info.con_and_idx.size(); ++j) {
node_id = info.con_and_idx[j].first;
int input_idx = info.con_and_idx[j].second;
DALI_ENFORCE(graph_->NodeType(node_id) == DALI_GPU);
DALI_ENFORCE(graph_->NodeType(node_id) == OpType::GPU);

int gpu_op_id = graph_->NodeIdx(node_id);
wsb->gpu_op_data[gpu_op_id].SetInput(
Expand All @@ -774,11 +774,11 @@ void Executor::SetOutputBuffersForIter(int queue_idx, WorkspaceBlob *wsb) {
NodeID node_id = info.prod_and_idx.first;
int output_idx = info.prod_and_idx.second;

if (graph_->NodeType(node_id) == DALI_MIXED) {
if (graph_->NodeType(node_id) == OpType::MIXED) {
int mixed_op_id = graph_->NodeIdx(node_id);
wsb->mixed_op_data[mixed_op_id].SetOutput(output_idx,
gpu_outputs_[i].Get(queue_idx));
} else if (graph_->NodeType(node_id) == DALI_GPU) {
} else if (graph_->NodeType(node_id) == OpType::GPU) {
int gpu_op_id = graph_->NodeIdx(node_id);
wsb->gpu_op_data[gpu_op_id].SetOutput(output_idx,
gpu_outputs_[i].Get(queue_idx));
Expand All @@ -790,7 +790,7 @@ void Executor::SetOutputBuffersForIter(int queue_idx, WorkspaceBlob *wsb) {
for (size_t j = 0; j < info.con_and_idx.size(); ++j) {
node_id = info.con_and_idx[j].first;
int input_idx = info.con_and_idx[j].second;
DALI_ENFORCE(graph_->NodeType(node_id) == DALI_GPU);
DALI_ENFORCE(graph_->NodeType(node_id) == OpType::GPU);

int gpu_op_id = graph_->NodeIdx(node_id);
wsb->gpu_op_data[gpu_op_id].SetInput(input_idx,
Expand Down
6 changes: 3 additions & 3 deletions dali/pipeline/executor/executor_test.cc
Expand Up @@ -374,7 +374,7 @@ TEST_F(ExecutorTest, TestRunBasicGraph) {
exe.Build(&graph, outputs);

// Set the data for the external source
auto *src_op = dynamic_cast<ExternalSource<CPUBackend>*>(&graph.cpu_op(0));
auto *src_op = dynamic_cast<ExternalSource<CPUBackend>*>(graph.cpu_node(0).op.get());
ASSERT_NE(src_op, nullptr);
TensorList<CPUBackend> tl;
this->MakeJPEGBatch(&tl, this->batch_size_);
Expand Down Expand Up @@ -421,7 +421,7 @@ TEST_F(ExecutorTest, TestRunBasicGraphWithCB) {
exe.Build(&graph, outputs);

// Set the data for the external source
auto *src_op = dynamic_cast<ExternalSource<CPUBackend>*>(&graph.cpu_op(0));
auto *src_op = dynamic_cast<ExternalSource<CPUBackend>*>(graph.cpu_node(0).op.get());
ASSERT_NE(src_op, nullptr);
TensorList<CPUBackend> tl;
this->MakeJPEGBatch(&tl, this->batch_size_);
Expand Down Expand Up @@ -479,7 +479,7 @@ TEST_F(ExecutorTest, TestPrefetchedExecution) {
exe.Build(&graph, outputs);

// Set the data for the external source
auto *src_op = dynamic_cast<ExternalSource<CPUBackend>*>(&graph.cpu_op(0));
auto *src_op = dynamic_cast<ExternalSource<CPUBackend>*>(graph.cpu_node(0).op.get());
ASSERT_NE(src_op, nullptr);
TensorList<CPUBackend> tl;
this->MakeJPEGBatch(&tl, this->batch_size_*2);
Expand Down
34 changes: 17 additions & 17 deletions dali/pipeline/executor/pipelined_executor.cc
Expand Up @@ -67,8 +67,8 @@ void PipelinedExecutor::SetupStageOutputsForGraph() {
bool found_stage_boundary = false;
for (auto &meta : consumer_meta) {
const auto& node_type = graph_->NodeType(meta.node);
if (node_type != DALI_SUPPORT &&
node_type != DALI_CPU) {
if (node_type != OpType::SUPPORT &&
node_type != OpType::CPU) {
// We've located a tensor that is an output of
// the stage.
found_stage_boundary = true;
Expand Down Expand Up @@ -109,13 +109,13 @@ void PipelinedExecutor::SetupStageOutputsForGraph() {
bool has_gpu_consumer = false;
for (auto &meta : consumer_meta) {
auto type = graph_->NodeType(meta.node);
if (type != DALI_CPU) {
if (type != OpType::CPU) {
// We've located a tensor that is an output of
// the stage.
auto &consumer = graph_->node(meta.node);
has_gpu_consumer =
has_gpu_consumer ||
type == DALI_GPU ||
type == OpType::GPU ||
(consumer.spec.name() == "MakeContiguous" &&
consumer.spec.OutputDevice(0) == "gpu");
found_stage_boundary = true;
Expand Down Expand Up @@ -155,12 +155,12 @@ void PipelinedExecutor::SetupStageOutputsForGraph() {

if (graph_->TensorIsType<CPUBackend>(tensor_name)) {
for (auto &meta : consumer_meta) {
if (graph_->NodeType(meta.node) != DALI_MIXED) {
if (graph_->NodeType(meta.node) != OpType::MIXED) {
if (!has_info_object) {
OutputInfo info;
info.prod_and_idx = std::make_pair(node.id, j);

bool pinned = graph_->NodeType(meta.node) == DALI_GPU;
bool pinned = graph_->NodeType(meta.node) == OpType::GPU;

mixed_stage_cpu_output_info_.push_back(info);
mixed_stage_cpu_outputs_.push_back(
Expand All @@ -176,7 +176,7 @@ void PipelinedExecutor::SetupStageOutputsForGraph() {
}
} else {
for (auto &meta : consumer_meta) {
if (graph_->NodeType(meta.node) != DALI_MIXED) {
if (graph_->NodeType(meta.node) != OpType::MIXED) {
if (!has_info_object) {
OutputInfo info;
info.prod_and_idx = std::make_pair(node.id, j);
Expand Down Expand Up @@ -204,7 +204,7 @@ void PipelinedExecutor::SetStageOutputsForIter(
auto &tvp = support_stage_outputs_[i];
auto &info = support_stage_output_info_[i];
NodeID node_id = info.prod_and_idx.first;
DALI_ENFORCE(graph_->NodeType(node_id) == DALI_SUPPORT);
DALI_ENFORCE(graph_->NodeType(node_id) == OpType::SUPPORT);

int support_op_id = graph_->NodeIdx(node_id);
int output_idx = info.prod_and_idx.second;
Expand All @@ -218,10 +218,10 @@ void PipelinedExecutor::SetStageOutputsForIter(
int input_idx = info.con_and_idx[j].second;
const OpSpec& spec = op_node.spec;
std::string arg_name = spec.ArgumentInputName(input_idx);
if (graph_->NodeType(node_id) == DALI_MIXED) {
if (graph_->NodeType(node_id) == OpType::MIXED) {
wsb->mixed_op_data[child_op_id].SetArgumentInput(
tvp.Get(queue_idx), arg_name);
} else if (graph_->NodeType(node_id) == DALI_GPU) {
} else if (graph_->NodeType(node_id) == OpType::GPU) {
wsb->gpu_op_data[child_op_id].SetArgumentInput(
tvp.Get(queue_idx), arg_name);
} else {
Expand All @@ -235,7 +235,7 @@ void PipelinedExecutor::SetStageOutputsForIter(
auto &tvp = cpu_stage_outputs_[i];
auto &info = cpu_stage_output_info_[i];
NodeID node_id = info.prod_and_idx.first;
DALI_ENFORCE(graph_->NodeType(node_id) == DALI_CPU);
DALI_ENFORCE(graph_->NodeType(node_id) == OpType::CPU);

int cpu_op_id = graph_->NodeIdx(node_id);
int output_idx = info.prod_and_idx.second;
Expand All @@ -244,7 +244,7 @@ void PipelinedExecutor::SetStageOutputsForIter(

for (size_t j = 0; j < info.con_and_idx.size(); ++j) {
node_id = info.con_and_idx[j].first;
if (graph_->NodeType(node_id) == DALI_MIXED) {
if (graph_->NodeType(node_id) == OpType::MIXED) {
int mixed_op_id = graph_->NodeIdx(node_id);
int input_idx = info.con_and_idx[j].second;
wsb->mixed_op_data[mixed_op_id].SetInput(
Expand All @@ -264,7 +264,7 @@ void PipelinedExecutor::SetStageOutputsForIter(
}
}
}
} else if (graph_->NodeType(node_id) == DALI_CPU) {
} else if (graph_->NodeType(node_id) == OpType::CPU) {
int cpu_op_id = graph_->NodeIdx(node_id);
int input_idx = info.con_and_idx[j].second;
wsb->cpu_op_data[cpu_op_id].SetInput(
Expand All @@ -279,7 +279,7 @@ void PipelinedExecutor::SetStageOutputsForIter(
auto &tlp = mixed_stage_cpu_outputs_[i];
auto &info = mixed_stage_cpu_output_info_[i];
NodeID node_id = info.prod_and_idx.first;
DALI_ENFORCE(graph_->NodeType(node_id) == DALI_MIXED);
DALI_ENFORCE(graph_->NodeType(node_id) == OpType::MIXED);

int mixed_op_id = graph_->NodeIdx(node_id);
int output_idx = info.prod_and_idx.second;
Expand All @@ -288,7 +288,7 @@ void PipelinedExecutor::SetStageOutputsForIter(

for (size_t j = 0; j < info.con_and_idx.size(); ++j) {
node_id = info.con_and_idx[j].first;
DALI_ENFORCE(graph_->NodeType(node_id) == DALI_GPU);
DALI_ENFORCE(graph_->NodeType(node_id) == OpType::GPU);

int gpu_op_id = graph_->NodeIdx(node_id);
int input_idx = info.con_and_idx[j].second;
Expand All @@ -302,7 +302,7 @@ void PipelinedExecutor::SetStageOutputsForIter(
auto &tlp = mixed_stage_gpu_outputs_[i];
auto &info = mixed_stage_gpu_output_info_[i];
NodeID node_id = info.prod_and_idx.first;
DALI_ENFORCE(graph_->NodeType(node_id) == DALI_MIXED);
DALI_ENFORCE(graph_->NodeType(node_id) == OpType::MIXED);

int mixed_op_id = graph_->NodeIdx(node_id);
int output_idx = info.prod_and_idx.second;
Expand All @@ -311,7 +311,7 @@ void PipelinedExecutor::SetStageOutputsForIter(

for (size_t j = 0; j < info.con_and_idx.size(); ++j) {
node_id = info.con_and_idx[j].first;
DALI_ENFORCE(graph_->NodeType(node_id) == DALI_GPU);
DALI_ENFORCE(graph_->NodeType(node_id) == OpType::GPU);

int gpu_op_id = graph_->NodeIdx(node_id);
int input_idx = info.con_and_idx[j].second;
Expand Down

0 comments on commit fdf5617

Please sign in to comment.