forked from tensorflow/models
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_context.cc
More file actions
173 lines (142 loc) · 5.15 KB
/
Copy pathtask_context.cc
File metadata and controls
173 lines (142 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* Copyright 2016 Google Inc. 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.
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 "syntaxnet/task_context.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/platform/env.h"
namespace syntaxnet {
namespace {
const char *const kShardPrintFormat = "%05d";
} // namespace
TaskInput *TaskContext::GetInput(const string &name) {
// Return existing input if it exists.
for (int i = 0; i < spec_.input_size(); ++i) {
if (spec_.input(i).name() == name) return spec_.mutable_input(i);
}
// Create new input.
TaskInput *input = spec_.add_input();
input->set_name(name);
return input;
}
TaskInput *TaskContext::GetInput(const string &name, const string &file_format,
const string &record_format) {
TaskInput *input = GetInput(name);
if (!file_format.empty()) {
bool found = false;
for (int i = 0; i < input->file_format_size(); ++i) {
if (input->file_format(i) == file_format) found = true;
}
if (!found) input->add_file_format(file_format);
}
if (!record_format.empty()) {
bool found = false;
for (int i = 0; i < input->record_format_size(); ++i) {
if (input->record_format(i) == record_format) found = true;
}
if (!found) input->add_record_format(record_format);
}
return input;
}
void TaskContext::SetParameter(const string &name, const string &value) {
// If the parameter already exists update the value.
for (int i = 0; i < spec_.parameter_size(); ++i) {
if (spec_.parameter(i).name() == name) {
spec_.mutable_parameter(i)->set_value(value);
return;
}
}
// Add new parameter.
TaskSpec::Parameter *param = spec_.add_parameter();
param->set_name(name);
param->set_value(value);
}
string TaskContext::GetParameter(const string &name) const {
// First try to find parameter in task specification.
for (int i = 0; i < spec_.parameter_size(); ++i) {
if (spec_.parameter(i).name() == name) return spec_.parameter(i).value();
}
// Parameter not found, return empty string.
return "";
}
int TaskContext::GetIntParameter(const string &name) const {
string value = GetParameter(name);
return utils::ParseUsing<int>(value, 0, utils::ParseInt32);
}
int64 TaskContext::GetInt64Parameter(const string &name) const {
string value = GetParameter(name);
return utils::ParseUsing<int64>(value, 0ll, utils::ParseInt64);
}
bool TaskContext::GetBoolParameter(const string &name) const {
string value = GetParameter(name);
return value == "true";
}
double TaskContext::GetFloatParameter(const string &name) const {
string value = GetParameter(name);
return utils::ParseUsing<double>(value, .0, utils::ParseDouble);
}
string TaskContext::Get(const string &name, const char *defval) const {
// First try to find parameter in task specification.
for (int i = 0; i < spec_.parameter_size(); ++i) {
if (spec_.parameter(i).name() == name) return spec_.parameter(i).value();
}
// Parameter not found, return default value.
return defval;
}
string TaskContext::Get(const string &name, const string &defval) const {
return Get(name, defval.c_str());
}
int TaskContext::Get(const string &name, int defval) const {
string value = Get(name, "");
return utils::ParseUsing<int>(value, defval, utils::ParseInt32);
}
int64 TaskContext::Get(const string &name, int64 defval) const {
string value = Get(name, "");
return utils::ParseUsing<int64>(value, defval, utils::ParseInt64);
}
double TaskContext::Get(const string &name, double defval) const {
string value = Get(name, "");
return utils::ParseUsing<double>(value, defval, utils::ParseDouble);
}
bool TaskContext::Get(const string &name, bool defval) const {
string value = Get(name, "");
return value.empty() ? defval : value == "true";
}
string TaskContext::InputFile(const TaskInput &input) {
CHECK_EQ(input.part_size(), 1) << input.name();
return input.part(0).file_pattern();
}
bool TaskContext::Supports(const TaskInput &input, const string &file_format,
const string &record_format) {
// Check file format.
if (input.file_format_size() > 0) {
bool found = false;
for (int i = 0; i < input.file_format_size(); ++i) {
if (input.file_format(i) == file_format) {
found = true;
break;
}
}
if (!found) return false;
}
// Check record format.
if (input.record_format_size() > 0) {
bool found = false;
for (int i = 0; i < input.record_format_size(); ++i) {
if (input.record_format(i) == record_format) {
found = true;
break;
}
}
if (!found) return false;
}
return true;
}
} // namespace syntaxnet