sarah-quinones / gemm

MIT License
76 stars 11 forks source link

std::any::TypeId is different for types from different versions of the same library #2

Closed coreylowman closed 1 year ago

coreylowman commented 1 year ago

Description

I'm not sure if this is a bug or something else, but I'm hitting this explicit panic in gemm dispatch when using f16.

This is because I'm using f16 from half-rs main branch (half = { git = "https://github.com/starkat99/half-rs.git", branch = "main", optional = true, features = ["num-traits", "rand_distr"] }, whereas this crate is using half-rs from crates.io.

You can see this results in two different versions being compiled by cargo tree:

dfdx v0.11.2 (C:\Users\clowm\Documents\programming\dfdx)
├── half v3.0.0-dev (https://github.com/starkat99/half-rs.git?branch=main#1d7f8622)
├── gemm v0.15.2
...
│   ├── gemm-f16 v0.15.2
...
│   │   ├── half v2.2.1
│   │   │   └── num-traits v0.2.15 (*)
...

The following shows that the two type ids are different for me:

use std::any::TypeId;

fn main() {
    println!("half f16 = {:?}", TypeId::of::<half::f16>());
    println!("gemm f16 = {:?}", TypeId::of::<gemm::f16>());
}

Outputs:

half f16 = TypeId { t: 2603904629608812109 }
gemm f16 = TypeId { t: 1360783734638873359 }

Request

I'm not really sure. Do you have thoughts on best approach here? This could also be an issue if using different versions for num_complex crate.

sarah-quinones commented 1 year ago

yeah, i'm aware of this. the way i intend gemm to be used is that you don't use it with the type as is. instead, you can write your own matmul wrapper that takes the half type you want. (in this case f16 from git), cast the pointers to the ones of the type that gemm works with (exposed as gemm::f16), then call gemm with those pointers.

this is what i do in faer for complex number support

coreylowman commented 1 year ago

Ohhhhh wow that is clever! Thank you I'll try this out