Enzyme icon indicating copy to clipboard operation
Enzyme copied to clipboard

Compilation Fails for Posit numbers

Open unrealwill opened this issue 4 years ago • 2 comments

Hello,

I tried to use Posit representation for floating point numbers . I grab a header only library that does it as a drop-in replacement for double

git clone https://github.com/stillwater-sc/universal

testPosit.cpp

#include <iostream>
using namespace std;

// https://github.com/stillwater-sc/universal
#pragma clang diagnostic ignored "-Wc++17-extensions"
#include <universal/number/posit/posit.hpp>

extern int enzyme_dup;
extern int enzyme_dupnoneed;
extern int enzyme_out;
extern int enzyme_const;

void __enzyme_autodiff(...);

template<typename T>
T sq( T x)
{
  return x*x;
}

template<typename T>
void fun1( T* x, T* out )
{
    *out = sq(x[0] - 1.0);
}


template<typename Real>
Real MyKernel(const Real& a, const Real& b) {
    return a * b;  // replace this with your kernel computation
}

constexpr double pi = 3.14159265358979323846;

using Real = sw::universal::posit<32,2>;


int main(int argc, char** argv )
{
  cout << "testMemoryAllocator "<< endl;

  Real a = sqrt(2);
  Real b = pi;
  std::cout << "Result: " << MyKernel(a, b) << std::endl;

  Real x = 3.0;
  Real out = 0.0;
  {
  out = 0.0;
  fun1( &x, &out );
  cout << "out " << endl;
  cout << out << endl;
  }

  {
  Real dx = 0.0;
  Real gout = 1.0;
  Real out = 0.0;
  __enzyme_autodiff( fun1<Real>, enzyme_dup, &x, &dx,
                                         enzyme_dup,&out,&gout );

  cout << "out " << endl;
  cout << out << endl;
  cout << "dx " << endl;
  cout << dx << endl;
}

}

clang testPosit.cpp -I/home/username/universal/include -lstdc++ -lm -Xclang -load -Xclang /usr/local/lib/ClangEnzyme-11.so -O2 -o testPosit

Here is the compilation log : errorPosit.log

Thanks

unrealwill avatar Jul 26 '21 17:07 unrealwill

Passing the -enzyme-globals-default-inactive=1 flag allows the compilation to work but the derivarive is not as expected

clang testPosit.cpp -I/home/username/universal/include -lstdc++ -lm -Xclang -load -Xclang /usr/local/lib/ClangEnzyme-11.so -mllvm -enzyme-globals-default-inactive=1 -O2 -o testPosit

testMemoryAllocator 
Result: 4.44288
out 
4
out 
16
dx 
0

unrealwill avatar Jul 26 '21 17:07 unrealwill

The original error seems to simply be not handling ostream (hopefully handled here once I finish getting it ready to land: https://github.com/wsmoses/Enzyme/pull/242).

As for the differentiation itself, this may require a bit more diving in as the underlying representation may result in integer TBAA emitted which is assumed inactive.

wsmoses avatar Jul 27 '21 02:07 wsmoses