コード例 #1
0
def _generic_reduce_window_lower(ctx, *args, jaxpr, consts, window_dimensions,
                                 window_strides, padding, base_dilation,
                                 window_dilation):
    operands, init_values = util.split_list(args, [len(args) // 2])
    _, init_value_avals = util.split_list(ctx.avals_in, [len(operands)])
    scalar_types = [mlir.aval_to_ir_type(aval) for aval in init_value_avals]
    rw = mhlo.ReduceWindowOp(
        map(mlir.aval_to_ir_type, ctx.avals_out),
        operands,
        init_values,
        mlir.dense_int_elements(window_dimensions),
        window_strides=mlir.dense_int_elements(window_strides),
        base_dilations=mlir.dense_int_elements(base_dilation),
        window_dilations=mlir.dense_int_elements(window_dilation),
        padding=ir.DenseIntElementsAttr.get(np.asarray(padding, np.int64),
                                            shape=(len(padding), 2)))
    reducer = rw.regions[0].blocks.append(*(scalar_types + scalar_types))
    with ir.InsertionPoint(reducer):
        if jaxpr.effects:
            raise NotImplementedError(
                'Cannot lower effectful `reduce_window`.')
        out_nodes, _ = mlir.jaxpr_subcomp(ctx.module_context, jaxpr,
                                          mlir.TokenSet(), consts,
                                          *([a] for a in reducer.arguments))
        mhlo.ReturnOp(util.flatten(out_nodes))
    return rw.results
コード例 #2
0
ファイル: windowed_reductions.py プロジェクト: wayfeng/jax
def _select_and_scatter_lower(ctx, operand, source, init_value, *,
                              select_jaxpr, select_consts, scatter_jaxpr,
                              scatter_consts, window_dimensions,
                              window_strides, padding):
    operand_aval, source_aval, init_value_aval = ctx.avals_in
    aval_out, = ctx.avals_out
    scalar_aval = operand_aval.update(shape=())
    scalar_type = mlir.aval_to_ir_type(scalar_aval)
    op = mhlo.SelectAndScatterOp(
        mlir.aval_to_ir_type(aval_out), operand, source, init_value,
        mlir.dense_int_elements(window_dimensions),
        mlir.dense_int_elements(window_strides),
        ir.DenseIntElementsAttr.get(np.asarray(padding, np.int64)))
    select = op.select.blocks.append(scalar_type, scalar_type)
    with ir.InsertionPoint(select):
        out_nodes = mlir.jaxpr_subcomp(ctx.module_context, select_jaxpr,
                                       select_consts,
                                       *([a] for a in select.arguments))
        mhlo.ReturnOp(util.flatten(out_nodes))
    scatter = op.scatter.blocks.append(scalar_type, scalar_type)
    with ir.InsertionPoint(scatter):
        out_nodes = mlir.jaxpr_subcomp(ctx.module_context, scatter_jaxpr,
                                       scatter_consts,
                                       *([a] for a in scatter.arguments))
        mhlo.ReturnOp(util.flatten(out_nodes))
    return op.results
コード例 #3
0
ファイル: convolution.py プロジェクト: xueeinstein/jax
def _conv_general_dilated_lower(
    ctx, lhs, rhs, *, window_strides, padding,
    lhs_dilation, rhs_dilation, dimension_numbers, feature_group_count,
    batch_group_count, precision, preferred_element_type,
    expand_complex_convolutions=False, **unused_kwargs):
  lhs_aval, rhs_aval = ctx.avals_in
  aval_out, = ctx.avals_out
  assert isinstance(dimension_numbers, ConvDimensionNumbers)
  dtype = lhs_aval.dtype
  if expand_complex_convolutions and np.issubdtype(dtype, np.complexfloating):
    if preferred_element_type is not None:
      # Convert complex dtype to types used for real and imaginary parts
      assert np.issubdtype(preferred_element_type, np.complexfloating)
      preferred_element_type = _real_dtype(preferred_element_type)
    complex_conv = mlir.lower_fun(
      partial(
        _complex_mul,
        partial(conv_general_dilated, window_strides=window_strides,
                padding=padding, lhs_dilation=lhs_dilation,
                rhs_dilation=rhs_dilation, dimension_numbers=dimension_numbers,
                feature_group_count=feature_group_count,
                batch_group_count=batch_group_count, precision=precision,
                preferred_element_type=preferred_element_type)),
      multiple_results=False)
    return complex_conv(ctx, lhs, rhs)

  lhs_spec, rhs_spec, out_spec = dimension_numbers
  dnums = mhlo.ConvDimensionNumbers.get(
    input_batch_dimension=lhs_spec[0],
    input_feature_dimension=lhs_spec[1],
    input_spatial_dimensions=list(lhs_spec[2:]),
    kernel_output_feature_dimension=rhs_spec[0],
    kernel_input_feature_dimension=rhs_spec[1],
    kernel_spatial_dimensions=list(rhs_spec[2:]),
    output_batch_dimension=out_spec[0],
    output_feature_dimension=out_spec[1],
    output_spatial_dimensions=list(out_spec[2:]))
  num_spatial_dims = len(rhs_spec) - 2
  window_reversal = mlir.dense_bool_elements([False] * num_spatial_dims)
  return [
      mhlo.ConvOp(
          mlir.aval_to_ir_type(aval_out),
          lhs,
          rhs,
          dimension_numbers=dnums,
          feature_group_count=mlir.i64_attr(feature_group_count),
          batch_group_count=mlir.i64_attr(batch_group_count),
          window_strides=mlir.dense_int_elements(window_strides),
          padding=mlir.dense_int_elements(padding),
          lhs_dilation=mlir.dense_int_elements(lhs_dilation),
          rhs_dilation=mlir.dense_int_elements(rhs_dilation),
          window_reversal=window_reversal,
          precision_config=lax.precision_attr(precision)).result
  ]
コード例 #4
0
ファイル: fft.py プロジェクト: frederikwilde/jax
def _fft_lowering(ctx, x, *, fft_type, fft_lengths):
    out_aval, = ctx.avals_out
    return [
        mhlo.FftOp(mlir.aval_to_ir_type(out_aval), x,
                   mhlo.FftTypeAttr.get(fft_type.name),
                   mlir.dense_int_elements(fft_lengths)).result
    ]
コード例 #5
0
ファイル: coo.py プロジェクト: cloudhan/jax
def _coo_todense_gpu_lowering(coo_todense_mhlo, ctx, data, row, col, *, spinfo):
  data_aval, row_aval, _ = ctx.avals_in
  dtype = data_aval.dtype
  if not (np.issubdtype(dtype, np.floating) or np.issubdtype(dtype, np.complexfloating)):
    warnings.warn(f"coo_todense cusparse/hipsparse lowering not available for dtype={dtype}. "
                  "Falling back to default implementation.", CuSparseEfficiencyWarning)
    return _coo_todense_lowering(ctx, data, row, col, spinfo=spinfo)

  if spinfo.rows_sorted:
    shape = spinfo.shape
    transpose = False
  elif spinfo.cols_sorted:
    row, col = col, row
    transpose = True
    shape = spinfo.shape[::-1]
  else:
    warnings.warn("coo_todense GPU lowering requires matrices with sorted rows or sorted cols. "
                  "To sort the rows in your matrix, use e.g. mat = mat._sort_rows(). Falling "
                  "back to the default implementation.", CuSparseEfficiencyWarning)
    return _coo_todense_lowering(ctx, data, row, col, spinfo=spinfo)

  result = coo_todense_mhlo(
      data, row, col, shape=shape, data_dtype=dtype, index_dtype=row_aval.dtype)
  return (
      [mhlo.TransposeOp(result, mlir.dense_int_elements([1, 0])).result]
      if transpose else [result])
コード例 #6
0
ファイル: windowed_reductions.py プロジェクト: wayfeng/jax
def _reduce_window_lower(reduce_op, init_value, ctx, operand, *,
                         window_dimensions, window_strides, padding,
                         base_dilation, window_dilation):
    aval_out, = ctx.avals_out
    operand_aval, = ctx.avals_in
    scalar_aval = operand_aval.update(shape=())
    scalar_type = mlir.aval_to_ir_type(scalar_aval)
    rw = mhlo.ReduceWindowOp(
        mlir.aval_to_ir_types(aval_out), [operand],
        [mlir.full_like_aval(init_value(scalar_aval.dtype), scalar_aval)],
        mlir.dense_int_elements(window_dimensions),
        mlir.dense_int_elements(window_strides),
        mlir.dense_int_elements(base_dilation),
        mlir.dense_int_elements(window_dilation),
        ir.DenseIntElementsAttr.get(np.asarray(padding, np.int64)))
    reducer = rw.regions[0].blocks.append(scalar_type, scalar_type)
    with ir.InsertionPoint(reducer):
        mhlo.ReturnOp(reduce_op(*reducer.arguments))
    return rw.results
コード例 #7
0
def _generic_reduce_window_lower(ctx, avals_in, avals_out, *args, jaxpr,
                                 consts, window_dimensions, window_strides,
                                 padding, base_dilation, window_dilation):
    operands, init_values = util.split_list(args, [len(args) // 2])
    _, init_value_avals = util.split_list(avals_in, [len(operands)])
    scalar_types = [mlir.aval_to_ir_type(aval) for aval in init_value_avals]
    rw = mhlo.ReduceWindowOp(
        map(mlir.aval_to_ir_type, avals_out), operands, init_values,
        mlir.dense_int_elements(window_dimensions),
        mlir.dense_int_elements(window_strides),
        mlir.dense_int_elements(base_dilation),
        mlir.dense_int_elements(window_dilation),
        ir.DenseIntElementsAttr.get(np.asarray(padding, np.int64)))
    reducer = rw.regions[0].blocks.append(*(scalar_types + scalar_types))
    with ir.InsertionPoint(reducer):
        out_nodes = mlir.jaxpr_subcomp(ctx, jaxpr, consts,
                                       *([a] for a in reducer.arguments))
        mhlo.ReturnOp(util.flatten(out_nodes))
    return rw.results
コード例 #8
0
 def _broadcast(x, dims):
     etype = ir.RankedTensorType(x.type).element_type
     return mhlo.BroadcastOp(ir.RankedTensorType(dims, etype), x,
                             mlir.dense_int_elements(dims))
コード例 #9
0
 def _broadcast(x, dims):
     return mhlo.BroadcastOp(x, mlir.dense_int_elements(dims))
コード例 #10
0
def _select_and_gather_add_lowering(ctx,
                                    tangents,
                                    operand,
                                    *,
                                    select_prim,
                                    window_dimensions,
                                    window_strides,
                                    padding,
                                    base_dilation,
                                    window_dilation,
                                    max_bits=64):
    _, operand_aval, = ctx.avals_in
    out_aval, = ctx.avals_out
    dtype = operand_aval.dtype
    etype = mlir.dtype_to_ir_type(dtype)
    nbits = dtypes.finfo(dtype).bits

    assert nbits <= max_bits
    double_word_reduction = nbits * 2 <= max_bits

    const = lambda dtype, x: mlir.ir_constant(np.array(x, dtype=dtype),
                                              canonicalize_types=False)

    if jax._src.lib.mlir_api_version >= 9:

        def _broadcast(x, dims):
            return mhlo.BroadcastOp(x, mlir.dense_int_elements(dims))
    else:

        def _broadcast(x, dims):
            etype = ir.RankedTensorType(x.type).element_type
            return mhlo.BroadcastOp(ir.RankedTensorType(dims, etype), x,
                                    mlir.dense_int_elements(dims))

    if double_word_reduction:
        # TODO(b/73062247): XLA doesn't yet implement ReduceWindow on tuples, so
        # we implement a pair-wise ReduceWindow by packing two k-bit values into
        # 2k-bit unsigned integer using bit tricks.
        word_dtype = lax._UINT_DTYPES[nbits]
        double_word_dtype = lax._UINT_DTYPES[nbits * 2]
        word_type = mlir.dtype_to_ir_type(word_dtype)
        double_word_type = mlir.dtype_to_ir_type(double_word_dtype)

        # Packs two values into a tuple.
        def pack(a, b):
            a_dims = ir.RankedTensorType(a.type).shape
            b_dims = ir.RankedTensorType(b.type).shape
            a = mhlo.BitcastConvertOp(
                ir.RankedTensorType.get(a_dims, word_type), a)
            b = mhlo.BitcastConvertOp(
                ir.RankedTensorType.get(b_dims, word_type), b)
            a = mhlo.ConvertOp(
                ir.RankedTensorType.get(a_dims, double_word_type), a)
            b = mhlo.ConvertOp(
                ir.RankedTensorType.get(b_dims, double_word_type), b)
            a = mhlo.ShiftLeftOp(
                a, _broadcast(const(double_word_dtype, nbits), a_dims))
            return mhlo.OrOp(a, b)

        # Unpacks the first element of a tuple.
        def fst(t):
            dims = ir.RankedTensorType(t.type).shape
            st = mhlo.ShiftRightLogicalOp(t, const(double_word_dtype, nbits))
            return mhlo.BitcastConvertOp(
                ir.RankedTensorType.get(dims, etype),
                mhlo.ConvertOp(ir.RankedTensorType.get(dims, word_type),
                               st)).result

        # Unpacks the second element of a tuple.
        def snd(t):
            dims = ir.RankedTensorType(t.type).shape
            return mhlo.BitcastConvertOp(
                ir.RankedTensorType.get(dims, etype),
                mhlo.ConvertOp(ir.RankedTensorType.get(dims, word_type),
                               t)).result

    else:
        # The double-word trick above only works if we have a sufficiently large
        # type. As an alternative, we can pack two half words into a single word,
        # at the cost of precision.
        # TODO(b/73062247): add support for tuple reductions and remove this case.
        warnings.warn(
            "Using reduced precision for gradient of reduce-window "
            "min/max operator to work around missing XLA support for "
            "pair-reductions. This is likely from a second or "
            "higher derivative of a max-pooling operation.")
        r_nbits = nbits // 2
        # Drop/round the bottom mantissa bits.
        nexp = dtypes.finfo(dtype).nexp
        nmant = r_nbits - nexp - 1

        double_word_dtype = word_dtype = lax._UINT_DTYPES[nbits]
        double_word_type = word_type = mlir.dtype_to_ir_type(word_dtype)

        # Packs two values into a tuple.
        def pack(a, b):
            a_dims = ir.RankedTensorType(a.type).shape
            b_dims = ir.RankedTensorType(b.type).shape
            if jax._src.lib.mlir_api_version >= 21:
                a = mhlo.ReducePrecisionOp(a,
                                           exponent_bits=mlir.i32_attr(nexp),
                                           mantissa_bits=mlir.i32_attr(nmant))
                b = mhlo.ReducePrecisionOp(b,
                                           exponent_bits=mlir.i32_attr(nexp),
                                           mantissa_bits=mlir.i32_attr(nmant))
            else:
                a = mhlo.ReducePrecisionOp(a.type,
                                           a,
                                           exponent_bits=mlir.i32_attr(nexp),
                                           mantissa_bits=mlir.i32_attr(nmant))
                b = mhlo.ReducePrecisionOp(b.type,
                                           b,
                                           exponent_bits=mlir.i32_attr(nexp),
                                           mantissa_bits=mlir.i32_attr(nmant))
            a = mhlo.BitcastConvertOp(
                ir.RankedTensorType.get(a_dims, word_type), a)
            b = mhlo.BitcastConvertOp(
                ir.RankedTensorType.get(b_dims, word_type), b)
            b = mhlo.ShiftRightLogicalOp(
                b, _broadcast(const(word_dtype, r_nbits), b_dims))
            return mhlo.OrOp(a, b)

        # Unpacks the first element of a tuple.
        def fst(t):
            st = mhlo.AndOp(t,
                            const(word_dtype, ((1 << r_nbits) - 1) << r_nbits))
            return mhlo.BitcastConvertOp(ir.RankedTensorType.get([], etype),
                                         st).result

        # Unpacks the second element of a tuple.
        def snd(t):
            dims = ir.RankedTensorType(t.type).shape
            return mhlo.BitcastConvertOp(
                ir.RankedTensorType.get(dims, etype),
                mhlo.ShiftLeftOp(t, _broadcast(const(word_dtype, r_nbits),
                                               dims))).result

    assert select_prim is lax.ge_p or select_prim is lax.le_p, select_prim
    init = -np.inf if select_prim is lax.ge_p else np.inf
    rw = mhlo.ReduceWindowOp(
        [ir.RankedTensorType.get(out_aval.shape, double_word_type)],
        pack(operand, tangents),
        pack(const(dtype, init), const(dtype, 0)),
        mlir.dense_int_elements(window_dimensions),
        window_strides=mlir.dense_int_elements(window_strides),
        base_dilations=mlir.dense_int_elements(base_dilation),
        window_dilations=mlir.dense_int_elements(window_dilation),
        padding=ir.DenseIntElementsAttr.get(np.asarray(padding, np.int64),
                                            shape=(len(padding), 2)))
    scalar_type = ir.RankedTensorType.get([], double_word_type)
    reducer = rw.regions[0].blocks.append(scalar_type, scalar_type)
    with ir.InsertionPoint(reducer):
        x, y = reducer.arguments
        assert select_prim is lax.ge_p or select_prim is lax.le_p
        which = "GE" if select_prim is lax.ge_p else "LE"
        out = mhlo.SelectOp(mlir.compare_mhlo(fst(x), fst(y), which), x, y)
        mhlo.ReturnOp(out)
    return [snd(rw.result)]
コード例 #11
0
ファイル: prng.py プロジェクト: xueeinstein/jax
 def _broadcast(x, aval):
     return mhlo.BroadcastInDimOp(
         mlir.aval_to_ir_type(aval_out), x,
         mlir.dense_int_elements(range(rank - len(aval.shape),
                                       rank))).result