| 1 | # Licensed to the Apache Software Foundation (ASF) under one | ||
| 2 | # or more contributor license agreements. See the NOTICE file | ||
| 3 | # distributed with this work for additional information | ||
| 4 | # regarding copyright ownership. The ASF licenses this file | ||
| 5 | # to you under the Apache License, Version 2.0 (the | ||
| 6 | # "License"); you may not use this file except in compliance | ||
| 7 | # with the License. You may obtain a copy of the License at | ||
| 8 | # | ||
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 10 | # | ||
| 11 | # Unless required by applicable law or agreed to in writing, | ||
| 12 | # software distributed under the License is distributed on an | ||
| 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| 14 | # KIND, either express or implied. See the License for the | ||
| 15 | # specific language governing permissions and limitations | ||
| 16 | # under the License. | ||
| 17 | |||
| 18 | package AI::MXNet::RecordIO; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use AI::MXNet::NS; | ||
| 22 | use AI::MXNet::Function::Parameters; | ||
| 23 | use AI::MXNet::Types; | ||
| 24 | use AI::MXNet::Base; | ||
| 25 | use Mouse; | ||
| 26 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | AI::MXNet::RecordIO - Read/write RecordIO format data | ||
| 30 | =cut | ||
| 31 | |||
| 32 | =head2 new | ||
| 33 | |||
| 34 | Parameters | ||
| 35 | ---------- | ||
| 36 | uri : Str | ||
| 37 | uri path to recordIO file. | ||
| 38 | flag: Str | ||
| 39 | "r" for reading or "w" writing. | ||
| 40 | =cut | ||
| 41 | |||
| 42 | has 'uri' => (is => 'ro', isa => 'Str', required => 1); | ||
| 43 | has 'flag' => (is => 'ro', isa => enum([qw/r w/]), required => 1); | ||
| 44 | has 'handle' => (is => 'rw', isa => 'RecordIOHandle'); | ||
| 45 | has [qw/writable | ||
| 46 | is_open/] => (is => 'rw', isa => 'Bool'); | ||
| 47 | |||
| 48 | sub BUILD | ||
| 49 | { | ||
| 50 | my $self = shift; | ||
| 51 | $self->is_open(0); | ||
| 52 | $self->open(); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub DEMOLISH | ||
| 56 | { | ||
| 57 | shift->close; | ||
| 58 | } | ||
| 59 | |||
| 60 | =head2 open | ||
| 61 | |||
| 62 | Open record file. | ||
| 63 | =cut | ||
| 64 | |||
| 65 | method open() | ||
| 66 | { | ||
| 67 | my $handle; | ||
| 68 | if($self->flag eq 'w') | ||
| 69 | { | ||
| 70 | $handle = check_call(AI::MXNetCAPI::RecordIOWriterCreate($self->uri)); | ||
| 71 | $self->writable(1); | ||
| 72 | } | ||
| 73 | else | ||
| 74 | { | ||
| 75 | $handle = check_call(AI::MXNetCAPI::RecordIOReaderCreate($self->uri)); | ||
| 76 | $self->writable(0); | ||
| 77 | } | ||
| 78 | $self->handle($handle); | ||
| 79 | $self->is_open(1); | ||
| 80 | } | ||
| 81 | |||
| 82 | =head2 close | ||
| 83 | |||
| 84 | Close record file. | ||
| 85 | =cut | ||
| 86 | |||
| 87 | method close() | ||
| 88 | { | ||
| 89 | return if not $self->is_open; | ||
| 90 | if($self->writable) | ||
| 91 | { | ||
| 92 | check_call(AI::MXNetCAPI::RecordIOWriterFree($self->handle)); | ||
| 93 | } | ||
| 94 | else | ||
| 95 | { | ||
| 96 | check_call(AI::MXNetCAPI::RecordIOReaderFree($self->handle)); | ||
| 97 | } | ||
| 98 | $self->is_open(0); | ||
| 99 | } | ||
| 100 | |||
| 101 | =head2 reset | ||
| 102 | |||
| 103 | Reset pointer to first item. If record is opened with 'w', | ||
| 104 | this will truncate the file to empty. | ||
| 105 | =cut | ||
| 106 | |||
| 107 | method reset() | ||
| 108 | { | ||
| 109 | $self->close; | ||
| 110 | $self->open; | ||
| 111 | } | ||
| 112 | |||
| 113 | =head2 write | ||
| 114 | |||
| 115 | Write a string buffer as a record. | ||
| 116 | |||
| 117 | Parameters | ||
| 118 | ---------- | ||
| 119 | $buf : a buffer to write. | ||
| 120 | =cut | ||
| 121 | |||
| 122 | method write(Str $buf) | ||
| 123 | { | ||
| 124 | assert($self->writable); | ||
| 125 | check_call( | ||
| 126 | AI::MXNetCAPI::RecordIOWriterWriteRecord( | ||
| 127 | $self->handle, | ||
| 128 | $buf, | ||
| 129 | length($buf) | ||
| 130 | ) | ||
| 131 | ); | ||
| 132 | } | ||
| 133 | |||
| 134 | =head2 read | ||
| 135 | |||
| 136 | Read a record as a string. | ||
| 137 | |||
| 138 | Returns | ||
| 139 | ---------- | ||
| 140 | $buf : string | ||
| 141 | =cut | ||
| 142 | |||
| 143 | method read() | ||
| 144 | { | ||
| 145 | assert(not $self->writable); | ||
| 146 | return scalar(check_call( | ||
| 147 | AI::MXNetCAPI::RecordIOReaderReadRecord( | ||
| 148 | $self->handle, | ||
| 149 | ) | ||
| 150 | )); | ||
| 151 | } | ||
| 152 | |||
| 153 | method MXRecordIO(@args) { return AI::MXNet::RecordIO->new(uri => $args[0], flag => $args[1]) } | ||
| 154 | method MXIndexedRecordIO(@args) | ||
| 155 | { | ||
| 156 | return AI::MXNet::IndexedRecordIO->new( | ||
| 157 | idx_path => $args[0], uri => $args[1], flag => $args[2] | ||
| 158 | ) | ||
| 159 | } | ||
| 160 | |||
| 161 | package AI::MXNet::IRHeader; | ||
| 162 | use Mouse; | ||
| 163 | has [qw/flag id id2/] => (is => 'rw', isa => 'Int'); | ||
| 164 | has 'label' => (is => 'rw', isa => 'AcceptableInput'); | ||
| 165 | around BUILDARGS => sub { | ||
| 166 | my $orig = shift; | ||
| 167 | my $class = shift; | ||
| 168 | if(@_ == 4) | ||
| 169 | { | ||
| 170 | return $class->$orig(flag => $_[0], label => $_[1], id => $_[2], id2 => $_[3]); | ||
| 171 | } | ||
| 172 | return $class->$orig(@_); | ||
| 173 | }; | ||
| 174 | my @order = qw/flag label id id2/; | ||
| 175 | use overload '@{}' => sub { my $self = shift; [map { $self->$_ } @order] }; | ||
| 176 | |||
| 177 | package AI::MXNet::RecordIO; | ||
| 178 | |||
| 179 | =head2 unpack | ||
| 180 | |||
| 181 | unpack a MXImageRecord to a string | ||
| 182 | |||
| 183 | Parameters | ||
| 184 | ---------- | ||
| 185 | s : str | ||
| 186 | string buffer from MXRecordIO.read | ||
| 187 | |||
| 188 | Returns | ||
| 189 | ------- | ||
| 190 | header : AI::MXNet::IRHeader | ||
| 191 | header of the image record | ||
| 192 | s : str | ||
| 193 | unpacked string | ||
| 194 | =cut | ||
| 195 | |||
| 196 | method unpack(Str $s) | ||
| 197 | { | ||
| 198 | my $h; | ||
| 199 | my $h_size = 24; | ||
| 200 | ($h, $s) = (substr($s, 0, $h_size), substr($s, $h_size)); | ||
| 201 | my $header = AI::MXNet::IRHeader->new(unpack('IfQQ', $h)); | ||
| 202 | if($header->flag > 0) | ||
| 203 | { | ||
| 204 | my $label; | ||
| 205 | ($label, $s) = (substr($s, 0, 4*$header->flag), substr($s, 4*$header->flag)); | ||
| 206 | my $pdl_type = PDL::Type->new(DTYPE_MX_TO_PDL->{float32}); | ||
| 207 | my $pdl = PDL->new_from_specification($pdl_type, $header->flag); | ||
| 208 | ${$pdl->get_dataref} = $label; | ||
| 209 | $pdl->upd_data; | ||
| 210 | $header->label($pdl); | ||
| 211 | } | ||
| 212 | return ($header, $s) | ||
| 213 | } | ||
| 214 | |||
| 215 | =head2 pack | ||
| 216 | |||
| 217 | pack a string into MXImageRecord | ||
| 218 | |||
| 219 | Parameters | ||
| 220 | ---------- | ||
| 221 | $header : AI::MXNet::IRHeader or ArrayRef suitable for AI::MXNet::IRHeader->new(@{ ArrayRef }) | ||
| 222 | header of the image record. | ||
| 223 | $header->label can be a number or an array ref. | ||
| 224 | s : str | ||
| 225 | string to pack | ||
| 226 | =cut | ||
| 227 | |||
| 228 | method pack(AI::MXNet::IRHeader|ArrayRef $header, Str $s) | ||
| 229 | { | ||
| 230 | $header = AI::MXNet::IRHeader->new(@$header) unless blessed $header; | ||
| 231 | if(not ref $header->label) | ||
| 232 | { | ||
| 233 | $header->flag(0); | ||
| 234 | } | ||
| 235 | else | ||
| 236 | { | ||
| 237 | my $label = AI::MXNet::NDArray->array($header->label, dtype=>'float32')->aspdl; | ||
| 238 | $header->label(0); | ||
| 239 | $header->flag($label->nelem); | ||
| 240 | my $buf = ${$label->get_dataref}; | ||
| 241 | $s = "$buf$s"; | ||
| 242 | } | ||
| 243 | $s = pack('IfQQ', @{ $header }) . $s; | ||
| 244 | return $s; | ||
| 245 | } | ||
| 246 | |||
| 247 | package AI::MXNet::IndexedRecordIO; | ||
| 248 | use Mouse; | ||
| 249 | use AI::MXNet::Base; | ||
| 250 | extends 'AI::MXNet::RecordIO'; | ||
| 251 | |||
| 252 | =head1 NAME | ||
| 253 | |||
| 254 | AI::MXNet::IndexedRecordIO - Read/write RecordIO format data supporting random access. | ||
| 255 | =cut | ||
| 256 | |||
| 257 | =head2 new | ||
| 258 | |||
| 259 | Parameters | ||
| 260 | ---------- | ||
| 261 | idx_path : str | ||
| 262 | Path to index file | ||
| 263 | uri : str | ||
| 264 | Path to record file. Only support file types that are seekable. | ||
| 265 | flag : str | ||
| 266 | 'w' for write or 'r' for read | ||
| 267 | =cut | ||
| 268 | |||
| 269 | has 'idx_path' => (is => 'ro', isa => 'Str', required => 1); | ||
| 270 | has [qw/idx | ||
| 271 | keys fidx/] => (is => 'rw', init_arg => undef); | ||
| 272 | |||
| 273 | method open() | ||
| 274 | { | ||
| 275 | $self->SUPER::open(); | ||
| 276 | $self->idx({}); | ||
| 277 | $self->keys([]); | ||
| 278 | open(my $f, $self->flag eq 'r' ? '<' : '>', $self->idx_path); | ||
| 279 | $self->fidx($f); | ||
| 280 | if(not $self->writable) | ||
| 281 | { | ||
| 282 | while(<$f>) | ||
| 283 | { | ||
| 284 | chomp; | ||
| 285 | my ($key, $val) = split(/\t/); | ||
| 286 | push @{ $self->keys }, $key; | ||
| 287 | $self->idx->{$key} = $val; | ||
| 288 | } | ||
| 289 | } | ||
| 290 | } | ||
| 291 | |||
| 292 | method close() | ||
| 293 | { | ||
| 294 | return if not $self->is_open; | ||
| 295 | $self->SUPER::close(); | ||
| 296 | $self->fidx(undef); | ||
| 297 | } | ||
| 298 | |||
| 299 | =head2 seek | ||
| 300 | |||
| 301 | Query current read head position. | ||
| 302 | =cut | ||
| 303 | |||
| 304 | method seek(Int $idx) | ||
| 305 | { | ||
| 306 | assert(not $self->writable); | ||
| 307 | my $pos = $self->idx->{$idx}; | ||
| 308 | check_call(AI::MXNetCAPI::RecordIOReaderSeek($self->handle, $pos)); | ||
| 309 | } | ||
| 310 | |||
| 311 | =head2 tell | ||
| 312 | |||
| 313 | Query current write head position. | ||
| 314 | =cut | ||
| 315 | |||
| 316 | method tell() | ||
| 317 | { | ||
| 318 | assert($self->writable); | ||
| 319 | return scalar(check_call(AI::MXNetCAPI::RecordIOWriterTell($self->handle))); | ||
| 320 | } | ||
| 321 | |||
| 322 | =head2 read_idx | ||
| 323 | |||
| 324 | Read record with the index. | ||
| 325 | |||
| 326 | Parameters: | ||
| 327 | ----------- | ||
| 328 | $idx | ||
| 329 | =cut | ||
| 330 | |||
| 331 | method read_idx(Int $idx) | ||
| 332 | { | ||
| 333 | $self->seek($idx); | ||
| 334 | return $self->read(); | ||
| 335 | } | ||
| 336 | |||
| 337 | =head2 write_idx | ||
| 338 | |||
| 339 | Write record with index. | ||
| 340 | |||
| 341 | Parameters: | ||
| 342 | ----------- | ||
| 343 | Int $idx | ||
| 344 | Str $buf | ||
| 345 | =cut | ||
| 346 | |||
| 347 | method write_idx(Int $idx, Str $buf) | ||
| 348 | { | ||
| 349 | my $pos = $self->tell(); | ||
| 350 | $self->write($buf); | ||
| 351 | my $f = $self->fidx; | ||
| 352 | print $f "$idx\t$pos\n"; | ||
| 353 | $self->idx->{$idx} = $pos; | ||
| 354 | push @{ $self->keys }, $idx; | ||
| 355 | } | ||
| 356 | |||
| 357 | 1; |