arrayfire / arrayfire-rust

Rust wrapper for ArrayFire
BSD 3-Clause "New" or "Revised" License
815 stars 58 forks source link

[BUG] Binary ops with a f16 array does not compile #320

Closed Benjamin-Philip closed 1 year ago

Benjamin-Philip commented 2 years ago

Description

Binary operations (for eg: add) where one of the arrays is f16 does not compile, raising the following compiler error:

error[E0277]: the trait bound `f16: ImplicitPromote<f32>` is not satisfied

This is because the ImplicitPromote<*> traits have not been implemented for f16. There also doesn't seem to be a ImplicitPromote<f16> trait. Implementing these traits for f16 should fix the issue.

Reproducible Code and/or Steps

The following code:

use arrayfire::{add, print, Array, Dim4};
use half::f16;

fn main() {
    let values: [f32; 3] = [1.0, 2.0, 3.0];
    let dims = Dim4::new(&[3, 1, 1, 1]);

    let half_values = values.iter().map(|&x| f16::from_f32(x)).collect::<Vec<_>>();

    let vals = Array::new(&values, dims);
    let hvals = Array::new(&half_values, dims);

    print(&add(&hvals, &vals, false));
    print(&add(&vals, &hvals, false));
}

Gives these errors:

error[E0271]: type mismatch resolving `<Array<f32> as Convertable>::OutType == f16`
  --> src/main.rs:13:12
   |
13 |     print(&add(&hvals, &vals, false));
   |            ^^^ expected `f32`, found struct `f16`

error[E0277]: the trait bound `f32: ImplicitPromote<f16>` is not satisfied
   --> src/main.rs:13:24
    |
13  |     print(&add(&hvals, &vals, false));
    |            ---         ^^^^^ the trait `ImplicitPromote<f16>` is not implemented for `f32`
    |            |
    |            required by a bound introduced by this call
    |
    = help: the following implementations were found:
              <f32 as ImplicitPromote<bool>>
              <f32 as ImplicitPromote<f64>>
              <f32 as ImplicitPromote<i16>>
              <f32 as ImplicitPromote<i32>>
            and 95 others
note: required by a bound in `arrayfire::add`
   --> /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/arrayfire-3.8.0/src/core/arith.rs:474:1
    |
474 | overloaded_binary_func!("Addition of two Arrays", add, add_helper, af_add);
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `arrayfire::add`
    = note: this error originates in the macro `overloaded_binary_func` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0271]: type mismatch resolving `<Array<f32> as Convertable>::OutType == f16`
  --> src/main.rs:13:12
   |
13 |     print(&add(&hvals, &vals, false));
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `f32`, found struct `f16`

error[E0277]: the trait bound `f32: ImplicitPromote<f16>` is not satisfied
   --> src/main.rs:14:16
    |
14  |     print(&add(&vals, &hvals, false));
    |            --- ^^^^^ the trait `ImplicitPromote<f16>` is not implemented for `f32`
    |            |
    |            required by a bound introduced by this call
    |
    = help: the following implementations were found:
              <f32 as ImplicitPromote<bool>>
              <f32 as ImplicitPromote<f64>>
              <f32 as ImplicitPromote<i16>>
              <f32 as ImplicitPromote<i32>>
            and 95 others
note: required by a bound in `arrayfire::add`
   --> /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/arrayfire-3.8.0/src/core/arith.rs:474:1
    |
474 | overloaded_binary_func!("Addition of two Arrays", add, add_helper, af_add);
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `arrayfire::add`
    = note: this error originates in the macro `overloaded_binary_func` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `f16: ImplicitPromote<f32>` is not satisfied
   --> src/main.rs:14:23
    |
14  |     print(&add(&vals, &hvals, false));
    |            ---        ^^^^^^ the trait `ImplicitPromote<f32>` is not implemented for `f16`
    |            |
    |            required by a bound introduced by this call
    |
note: required by a bound in `arrayfire::add`
   --> /home/ben/.cargo/registry/src/github.com-1ecc6299db9ec823/arrayfire-3.8.0/src/core/arith.rs:474:1
    |
474 | overloaded_binary_func!("Addition of two Arrays", add, add_helper, af_add);
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `arrayfire::add`
    = note: this error originates in the macro `overloaded_binary_func` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `f16: ImplicitPromote<f32>` is not satisfied
  --> src/main.rs:13:12
   |
13 |     print(&add(&hvals, &vals, false));
   |            ^^^ the trait `ImplicitPromote<f32>` is not implemented for `f16`

error[E0277]: the trait bound `f32: ImplicitPromote<f16>` is not satisfied
  --> src/main.rs:14:12
   |
14 |     print(&add(&vals, &hvals, false));
   |            ^^^ the trait `ImplicitPromote<f16>` is not implemented for `f32`
   |
   = help: the following implementations were found:
             <f32 as ImplicitPromote<bool>>
             <f32 as ImplicitPromote<f64>>
             <f32 as ImplicitPromote<i16>>
             <f32 as ImplicitPromote<i32>>
           and 95 others

Some errors have detailed explanations: E0271, E0277.
For more information about an error, try `rustc --explain E0271`.
error: could not compile `afbug` due to 7 previous errors

System Information

LSB Version: :core-4.1-amd64:core-4.1-noarch Distributor ID: Fedora Description: Fedora release 36 (Thirty Six) Release: 36 Codename: ThirtySix nvidia-smi not found rocm-smi not found. clinfo not found.

Checklist