Layout is Pc,H,W,C4 where P - is a plane based on channels.
| 277 | |
| 278 | // Layout is Pc,H,W,C4 where P - is a plane based on channels. |
| 279 | Status ConvertToPHWC4Half(absl::Span<const float> in, const BHWC& shape, |
| 280 | absl::Span<HalfBits> out) { |
| 281 | RETURN_IF_ERROR(ValidateConvertToPHWC4(in, shape, out)); |
| 282 | |
| 283 | // Layout is Pc,H,W,C4 where P - is a plane based on channels. |
| 284 | int num_planes = IntegralDivideRoundUp(shape.c, kPhwc4ChannelsInPlane); |
| 285 | const int num_pixels = shape.h * shape.w; |
| 286 | // A layer is a set of kPhwc4ChannelsInPlane channels images. |
| 287 | const int num_full_planes = shape.c / kPhwc4ChannelsInPlane; |
| 288 | for (int b = 0; b < shape.b; b++) { |
| 289 | HalfBits* dest = |
| 290 | out.data() + b * num_pixels * num_planes * kPhwc4ChannelsInPlane; |
| 291 | for (int p = 0; p < num_full_planes; p++) { |
| 292 | const float* src = |
| 293 | in.data() + shape.LinearIndex({b, 0, 0, p * kPhwc4ChannelsInPlane}); |
| 294 | for (int i = 0; i < num_pixels; i++) { |
| 295 | dest[0] = fp16_ieee_from_fp32_value(src[0]); |
| 296 | dest[1] = fp16_ieee_from_fp32_value(src[1]); |
| 297 | dest[2] = fp16_ieee_from_fp32_value(src[2]); |
| 298 | dest[3] = fp16_ieee_from_fp32_value(src[3]); |
| 299 | src += shape.c; |
| 300 | dest += kPhwc4ChannelsInPlane; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | // Padding last kPhwc4ChannelsInPlane-channel layer to multiple of |
| 306 | // kPhwc4ChannelsInPlane. |
| 307 | const int padded_size = num_pixels * num_planes * kPhwc4ChannelsInPlane; |
| 308 | const int remaining_channels = |
| 309 | shape.c - num_full_planes * kPhwc4ChannelsInPlane; |
| 310 | if (remaining_channels == 0) { |
| 311 | return OkStatus(); |
| 312 | } |
| 313 | |
| 314 | for (int b = 0; b < shape.b; b++) { |
| 315 | const float* src = |
| 316 | in.data() + |
| 317 | shape.LinearIndex({b, 0, 0, num_full_planes * kPhwc4ChannelsInPlane}); |
| 318 | HalfBits* dest = out.data() + b * padded_size + |
| 319 | num_pixels * num_full_planes * kPhwc4ChannelsInPlane; |
| 320 | switch (remaining_channels) { |
| 321 | case 1: |
| 322 | for (int p = 0; p < num_pixels; p++) { |
| 323 | dest[0] = fp16_ieee_from_fp32_value(src[0]); |
| 324 | dest[1] = 0; |
| 325 | dest[2] = 0; |
| 326 | dest[3] = 0; |
| 327 | src += shape.c; |
| 328 | dest += kPhwc4ChannelsInPlane; |
| 329 | } |
| 330 | break; |
| 331 | case 2: |
| 332 | for (int p = 0; p < num_pixels; p++) { |
| 333 | dest[0] = fp16_ieee_from_fp32_value(src[0]); |
| 334 | dest[1] = fp16_ieee_from_fp32_value(src[1]); |
| 335 | dest[2] = 0; |
| 336 | dest[3] = 0; |
nothing calls this directly
no test coverage detected