43
43
template <typename TensorType>
44
44
void print_tensor_stats (const TensorType& x)
45
45
{
46
- std::cout << " Stats: " ;
46
+ std::cout << " Stats: " ;
47
47
const ttb_indx nd = x.ndims ();
48
48
for (ttb_indx i=0 ; i<nd; ++i) {
49
49
std::cout << x.size (i);
@@ -65,19 +65,22 @@ void print_tensor_stats(const TensorType& x)
65
65
66
66
template <typename TensorType>
67
67
void save_tensor (const TensorType& x_in, const std::string& filename,
68
- const std::string format, const std::string type, bool gz,
68
+ const std::string format, const std::string type,
69
+ const ttb_indx index_base, bool gz,
69
70
bool header)
70
71
{
71
72
std::cout << " \n Output:\n "
72
- << " File: " << filename << std::endl
73
- << " Format: " << format << std::endl
74
- << " Type: " << type;
73
+ << " File: " << filename << std::endl
74
+ << " Index Base: " << index_base << std::endl
75
+ << " Format: " << format << std::endl
76
+ << " Type: " << type;
75
77
if (type == " text" && gz)
76
78
std::cout << " (compressed)" ;
77
79
if (type == " binary" && !header)
78
80
std::cout << " (no header)" ;
79
81
std::cout << std::endl;
80
- Genten::TensorWriter<Genten::DefaultHostExecutionSpace> writer (filename,gz);
82
+ Genten::TensorWriter<Genten::DefaultHostExecutionSpace> writer (
83
+ filename,index_base,gz);
81
84
if (format == " sparse" ) {
82
85
Genten::Sptensor x_out (x_in);
83
86
print_tensor_stats (x_out);
@@ -97,10 +100,12 @@ void save_tensor(const TensorType& x_in, const std::string& filename,
97
100
}
98
101
99
102
void read_tensor_file (const std::string& filename,
103
+ const ttb_indx index_base,
100
104
std::string& format, std::string& type, bool gz,
101
105
Genten::Sptensor& x_sparse, Genten::Tensor& x_dense)
102
106
{
103
- Genten::TensorReader<Genten::DefaultHostExecutionSpace> reader (filename,0 ,gz);
107
+ Genten::TensorReader<Genten::DefaultHostExecutionSpace> reader (
108
+ filename,index_base,gz);
104
109
reader.read ();
105
110
106
111
if (reader.isSparse ()) {
@@ -125,14 +130,16 @@ int main(int argc, char* argv[])
125
130
auto args = Genten::build_arg_list (argc,argv);
126
131
const bool help =
127
132
Genten::parse_ttb_bool (args, " --help" , " --no-help" , false );
128
- if (argc < 9 || argc > 11 || help) {
133
+ if (argc < 9 || argc > 16 || help) {
129
134
std::cout << " \n convert-tensor: a helper utility for converting tensor data between\n "
130
135
<< " tensor formats (sparse or dense), and file types (text or binary).\n\n "
131
136
<< " Usage: " << argv[0 ] << " --input-file <string> --output-file <string> --output-format <sparse|dense> --output-type <text|binary> [options] \n "
132
137
<< " Options:\n "
133
- << " --input-gz Input tensor is Gzip compressed (text-only, default: off)\n "
134
- << " --output-gz Output tensor is Gzip compressed (text-only, default: off)\n "
135
- << " --output-header Write header to output file (binary-only, default: on)\n " ;
138
+ << " --input-gz Input tensor is Gzip compressed (text-only, default: off)\n "
139
+ << " --output-gz Output tensor is Gzip compressed (text-only, default: off)\n "
140
+ << " --output-header Write header to output file (binary-only, default: on)\n "
141
+ << " --input-index-base Starting index for input tensor (sparse-only, default: 0)\n "
142
+ << " --output-index-base Starting index for output tensor (sparse-only, default: 0)\n " ;
136
143
return 0 ;
137
144
}
138
145
@@ -153,6 +160,10 @@ int main(int argc, char* argv[])
153
160
Genten::parse_ttb_bool (args, " --output-gz" , " --no-output-gz" , false );
154
161
const bool output_header =
155
162
Genten::parse_ttb_bool (args, " --output-header" , " --no-output-header" , true );
163
+ const ttb_indx input_index_base =
164
+ Genten::parse_ttb_indx (args, " --input-index-base" , 0 , 0 , INT_MAX);
165
+ const ttb_indx output_index_base =
166
+ Genten::parse_ttb_indx (args, " --output-index-base" , 0 , 0 , INT_MAX);
156
167
157
168
if (input_filename == " " )
158
169
Genten::error (" input filename must be specified" );
@@ -168,29 +179,30 @@ int main(int argc, char* argv[])
168
179
Genten::error (" No header option only supported for binary output files" );
169
180
170
181
std::cout << " \n Input:\n "
171
- << " File: " << input_filename << std::endl;
182
+ << " File: " << input_filename << std::endl
183
+ << " Index base: " << input_index_base << std::endl;
172
184
173
185
std::string input_format = " unknown" ;
174
186
std::string input_type = " unknown" ;
175
187
Genten::Sptensor x_sparse;
176
188
Genten::Tensor x_dense;
177
- read_tensor_file (input_filename, input_format, input_type, input_gz ,
178
- x_sparse, x_dense);
189
+ read_tensor_file (input_filename, input_index_base, input_format, input_type ,
190
+ input_gz, x_sparse, x_dense);
179
191
180
- std::cout << " Format: " << input_format << std::endl
181
- << " Type: " << input_type;
192
+ std::cout << " Format: " << input_format << std::endl
193
+ << " Type: " << input_type;
182
194
if (input_type == " text" && input_gz)
183
195
std::cout << " (compressed)" ;
184
196
std::cout << std::endl;
185
197
if (input_format == " sparse" ) {
186
198
print_tensor_stats (x_sparse);
187
199
save_tensor (x_sparse, output_filename, output_format, output_type,
188
- output_gz, output_header);
200
+ output_index_base, output_gz, output_header);
189
201
}
190
202
else if (input_format == " dense" ) {
191
203
print_tensor_stats (x_dense);
192
204
save_tensor (x_dense, output_filename, output_format, output_type,
193
- output_gz, output_header);
205
+ output_index_base, output_gz, output_header);
194
206
}
195
207
else
196
208
Genten::error (" Invalid input tensor format!" );
0 commit comments