Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 of 2 tasks
Benjamin-Philip opened this issue Jun 3, 2022 · 0 comments · Fixed by #321
Closed
1 of 2 tasks

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

Benjamin-Philip opened this issue Jun 3, 2022 · 0 comments · Fixed by #321
Labels
Milestone

Comments

@Benjamin-Philip
Copy link
Contributor

Benjamin-Philip commented Jun 3, 2022

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

  • Using the latest available ArrayFire release
  • GPU drivers are up to date
Benjamin-Philip added a commit to Benjamin-Philip/ex_af that referenced this issue Jun 12, 2022
- Support binary ops
- Move helper functions to a helper module
- Switch arrayfire dep to a fork (See arrayfire/arrayfire-rust#320)

A major thing to note is that the binary ops are half broken.
This is because there isn't a broadcast implementation yet so,
binary ops with tensors of different shapes will return invalid
results.
@9prady9 9prady9 added this to the 3.8.1 milestone Jun 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants