code: def float32_to_bfloat16(fval: float, truncate: bool = False) -> int:
      ival = int.from_bytes(struct.pack("<f", fval), "little")
      if truncate:
          return ival >> 16
      # NaN requires at least 1 significand bit set
      if isnan(fval):
          return 0x7FC0  # sign=0, exp=all-ones, sig=0b1000000
      # drop bottom 16-bits
      # round remaining bits using round-to-nearest-even
      rounded = ((ival >> 16) & 1) + 0x7FFF
      return (ival + rounded) >> 16
  
imports: import collections.abc
  import numbers
  import struct
  from cmath import isnan
  from typing import Any, Callable, Dict, KeysView, List, Optional, Sequence, Tuple, TypeVar, Union, cast
  import google.protobuf.message
  import numpy
  from onnx import IR_VERSION, AttributeProto, FunctionProto, GraphProto, MapProto, ModelProto, NodeProto, OperatorSetIdProto, OptionalProto, SequenceProto, SparseTensorProto, TensorProto, TensorShapeProto, TrainingInfoProto, TypeProto, ValueInfoProto, defs, mapping, subbyte
label_desc: Use image node_id 12 for calling a global function with example usage: float32_to_bfloat16(fval, truncate) and returns: unknown, unknown, int