Unit testing _one crate_ compiled under two different configs

Short Version:

Is there a way to write a unit test that says:

crate Foo, with cfg=red
crate Foo, with cfg=blue
gives identical results for the following op

Long Version

  1. Ignore floating point == issues – I’m actually checking that relative error < 0.00001

  2. I have a numeric library where we have:

nouns: Ten0, Ten1, Ten2, Ten3, Ten4 – the [0, 1, 2, 3, 4]-dimensional Tensors together with some basic ops (blas, convolutions, + … )

  1. I want to have two impls – one that is pure CPU, where the Tensors are stored in RAM; and one that is pure GPU, where all the Tensors are stored in GPU memory and the funcs are written in CUDA.

  2. I want to have an identical API, but have a cfg flag for either cpu or gpu.

  3. Now – here is the tricky part: I want a way to say:

consider the following set of tensors ops
I want you to run it with cfg=cpu anc cfg=gpu
and check that their relative error is < 0.00001

I am not sure how to express this as it includes importing one library with two different configs.


Pre-emptive FAQ

Why not just have crate num-cpu and num-gpu ?

We still don’t get around the problem. Suppose we had num-cpu and num-gpu. Furthermore, suppose we had

crate num-algo

which could either use cfg=cpu or cfg=gpu

I still want to be able to unit test

num-algo func foo, with cfg=cpu and
num-algo func foo, with cfg=gpu

have realtive error < 0.0000001


You could have two "proxy" crates, that both depend on num-algo and reexport it, but specify different features in their Cargo.tomls. See also this thread:

then you could call the functions as

assert!(num::abs(cpu_proxy_crate::num_algo::foo(42) - pu_proxy_crate::num_algo::foo(42)) < 0.0000001 );
1 Like