void min(const T* arg, T* out, const Shape& in_shape, const Shape& out_shape, const AxisSet& reduction_axes) { T minval = std::numeric_limits<T>::has_infinity ? std::numeric_limits<T>::infinity() : std::numeric_limits<T>::max(); CoordinateTransform output_transform(out_shape); for (const Coordinate& output_coord : output_transform) { out[output_transform.index(output_coord)] = minval; } CoordinateTransform input_transform(in_shape); for (const Coordinate& input_coord : input_transform) { Coordinate output_coord = project(input_coord, reduction_axes); T x = arg[input_transform.index(input_coord)]; T min = out[output_transform.index(output_coord)]; if (x < min) { out[output_transform.index(output_coord)] = x; } } }
BICAPI void safe_compute_transform_from_tags( int npoints, Real **tag_list1, Real **tag_list2, Trans_type trans_type, General_transform *transform ) { #if !HAVE_FORK compute_transform_from_tags( npoints, tag_list1, tag_list2, trans_type, transform ); #else int fildes[2]; FILE *fpin, *fpout; Status status; int statptr; General_transform computed_transform; /* Create a pipe */ if (pipe(fildes)) { create_linear_transform(transform, NULL); return; } /* Fork */ if (fork()) { /* Parent */ (void) close(fildes[1]); fpin = fdopen(fildes[0], "r"); status = input_transform(fpin, NULL, transform); (void) fclose(fpin); do { (void) wait(&statptr); } while (WIFSTOPPED(statptr)); if (WEXITSTATUS(statptr) || status != OK) { if( status == OK ) delete_general_transform( transform ); create_linear_transform(transform, NULL); return; } } else { /* Child */ (void) close(fildes[0]); fpout = fdopen(fildes[1], "w"); compute_transform_from_tags(npoints, tag_list1, tag_list2, trans_type, &computed_transform); status = output_transform(fpout, NULL, NULL, NULL, &computed_transform); delete_general_transform( &computed_transform ); (void) fclose(fpout); if (status != OK) { exit(EXIT_FAILURE); } else { exit(EXIT_SUCCESS); } } return; #endif }
void reverse_sequence(const T* arg, T* out, const Shape& arg_shape, size_t batch_axis, size_t sequence_axis, U* sequence_lengths) { CoordinateTransform input_transform(arg_shape); for (const Coordinate& in_coord : input_transform) { size_t batch_index = in_coord[batch_axis]; auto orig_seq_index = static_cast<size_t>(sequence_lengths[batch_index]); if (orig_seq_index > arg_shape.at(sequence_axis)) { throw ngraph_error( "One of the elements of sequence lengths is greater than sequence axis " "dimension"); } if (orig_seq_index == 0) { orig_seq_index = 1; } size_t sequence_index = in_coord[sequence_axis] < orig_seq_index ? orig_seq_index - in_coord[sequence_axis] - 1 : in_coord[sequence_axis]; //make a copy of in_coord and update sequence_index Coordinate out_coord = in_coord; out_coord[sequence_axis] = sequence_index; out[input_transform.index(out_coord)] = arg[input_transform.index(in_coord)]; } }
void sum(const T* arg, T* out, const Shape& in_shape, const Shape& out_shape, const AxisSet& reduction_axes) { CoordinateTransform output_transform(out_shape); for (const Coordinate& output_coord : output_transform) { out[output_transform.index(output_coord)] = 0; } CoordinateTransform input_transform(in_shape); for (const Coordinate& input_coord : input_transform) { Coordinate output_coord = project(input_coord, reduction_axes); out[output_transform.index(output_coord)] += arg[input_transform.index(input_coord)]; } }
void pad(const T* arg0, const T* arg1, T* out, const Shape& arg0_shape, const Shape& out_shape, const Shape& padding_below, const Shape& padding_above, const Shape& padding_interior) { Coordinate input_start(arg0_shape.size(), 0); // start at (0,0,...,0) Coordinate input_end = out_shape; // end at (d'0,d'1,...,d'n), the outer corner of the post-padding shape Strides input_strides(arg0_shape.size(), 1); AxisVector input_axis_order(arg0_shape.size()); for (size_t i = 0; i < arg0_shape.size(); i++) { input_axis_order[i] = i; } Strides input_dilation(arg0_shape.size()); for (size_t i = 0; i < arg0_shape.size(); i++) { input_dilation[i] = padding_interior[i] + 1; } // Need to cast these to CoordinateDiff in order to make CoordinateTransform happy. CoordinateDiff padding_below_signed; CoordinateDiff padding_above_signed; for (size_t i = 0; i < padding_below.size(); i++) { padding_below_signed.push_back(padding_below[i]); padding_above_signed.push_back(padding_above[i]); } CoordinateTransform input_transform(arg0_shape, input_start, input_end, input_strides, input_axis_order, padding_below_signed, padding_above_signed, input_dilation); CoordinateTransform output_transform(out_shape); CoordinateTransform::Iterator output_it = output_transform.begin(); for (const Coordinate& in_coord : input_transform) { const Coordinate& out_coord = *output_it; T v = input_transform.has_source_coordinate(in_coord) ? arg0[input_transform.index(in_coord)] : *arg1; out[output_transform.index(out_coord)] = v; ++output_it; } }