Search code examples
haskellmatrixaccelerate-haskell

How to define the matrix product in accelerate-haskell


I am trying to define a typesafe matrix computation library on top of accelerate, partly for educational purposes, partly to see whether this is a practical approach.

But I am completely stuck when it comes to define the product of to matrices properly - i.e. in a way GHC accepts/compiles my code.

I have had a few tries, which were variations of this:

Linear.hs

{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ScopedTypeVariables #-}

import qualified Data.Array.Accelerate as A

import GHC.TypeLits
import Data.Array.Accelerate ( (:.)(..), Array
                             , Exp, Shape, FullShape, Slice
                             , DIM0, DIM1, DIM2, Z(Z)
                             , IsFloating, IsNum, Elt, Acc
                             , Any(Any), All(All))
import           Data.Proxy

newtype Matrix (rows :: Nat) (cols :: Nat) a = AccMatrix {unMatrix :: Acc (Array DIM2 a)}
(#*#) :: forall k m n a. (KnownNat k, KnownNat m, KnownNat n, IsNum a, Elt a) =>
    Matrix k m a -> Matrix m n a -> Matrix k n a
 v #*# w = let v' = unMatrix v
               w' = unMatrix w
           in AccMatrix $ A.generate (A.index2 k' n') undefined
          where k' = fromInteger $ natVal (Proxy :: Proxy k)
                n' = fromInteger $ natVal (Proxy :: Proxy n)
                aux :: Acc (Array (FullShape (Z :. Int) :. Int) e) -> Acc (Array (FullShape (Z :. All) :. Int) e) -> Exp ((Z :. Int) :. Int) -> Exp e
                aux v w sh = let (Z:.i:.j) = A.unlift sh
                                 v' = A.slice v (A.lift $ Z:.i:.All)
                                 w' = A.slice w (A.lift $ Z:.All:.j)
                              in A.the $ A.sum $ A.zipWith (*) v' w'

The error stack build gives me is

.../src/Linear.hs:196:55:
    Couldn't match type ‘A.Plain ((Z :. head0) :. head1)’
                   with ‘(Z :. Int) :. Int’
    The type variables ‘head0’, ‘head1’ are ambiguous
    Expected type: Exp (A.Plain ((Z :. head0) :. head1))
      Actual type: Exp ((Z :. Int) :. Int)
    Relevant bindings include
      i :: head0 (bound at src/Linear.hs:196:38)
      j :: head1 (bound at src/Linear.hs:196:41)
    In the first argument of ‘A.unlift’, namely ‘sh’
    In the expression: A.unlift sh

.../src/Linear.hs:197:47:
    Couldn't match type ‘FullShape (A.Plain (Z :. head0))’
                   with ‘Z :. Int’
    The type variable ‘head0’ is ambiguous
    Expected type: Acc
                     (Array (FullShape (A.Plain (Z :. head0) :. All)) e)
      Actual type: Acc (Array (FullShape (Z :. Int) :. Int) e)
    Relevant bindings include
      v' :: Acc (Array (A.SliceShape (A.Plain (Z :. head0)) :. Int) e)
        (bound at src/Linear.hs:197:34)
      i :: head0 (bound at src/Linear.hs:196:38)
    In the first argument of ‘A.slice’, namely ‘v’
    In the expression: A.slice v (A.lift $ Z :. i :. All)

.../src/Linear.hs:198:39:
    Couldn't match type ‘A.SliceShape (A.Plain ((Z :. All) :. head1))’
                   with ‘A.SliceShape (A.Plain (Z :. head0)) :. Int’
    The type variables ‘head0’, ‘head1’ are ambiguous
    Expected type: Acc
                     (Array (A.SliceShape (A.Plain (Z :. head0)) :. Int) e)
      Actual type: Acc
                     (Array (A.SliceShape (A.Plain ((Z :. All) :. head1))) e)
    Relevant bindings include
      w' :: Acc (Array (A.SliceShape (A.Plain (Z :. head0)) :. Int) e)
        (bound at src/Linear.hs:198:34)
      v' :: Acc (Array (A.SliceShape (A.Plain (Z :. head0)) :. Int) e)
        (bound at src/Linear.hs:197:34)
      i :: head0 (bound at src/Linear.hs:196:38)
      j :: head1 (bound at src/Linear.hs:196:41)
    In the expression: A.slice w (A.lift $ Z :. All :. j)
    In an equation for ‘w'’: w' = A.slice w (A.lift $ Z :. All :. j)

.../src/Linear.hs:198:47:
    Couldn't match type ‘FullShape (A.Plain ((Z :. All) :. head1))’
                   with ‘(Z :. Int) :. Int’
    The type variable ‘head1’ is ambiguous
    Expected type: Acc
                     (Array (FullShape (A.Plain ((Z :. All) :. head1))) e)
      Actual type: Acc (Array (FullShape (Z :. All) :. Int) e)
    Relevant bindings include
      j :: head1 (bound at src/Linear.hs:196:41)
    In the first argument of ‘A.slice’, namely ‘w’
    In the expression: A.slice w (A.lift $ Z :. All :. j)

I have consulted the documentation of Accelerate, and I am also reading accelerate-arithmetic which has a similar aim but does not use TypeLits in order to assert the array/vector dimensions.

I also tried to make a vanilla version (i.e. without my own matrix type), in case my types were wrong, which I believe is suffering from the same misconception about the usage of slice. I am including this just for completeness' sake, I can add the error messages,but I chose to omit them as I believe they do not relate to the problem above.

(#*#) :: forall a. (IsNum a, Elt a) =>
    Acc (Array DIM2 a) -> Acc (Array DIM2 a) -> Maybe (Acc (Array DIM2 a))   
v #*# w = let Z:.k :.m = A.unlift $ A.arrayShape $ I.run v
              Z:.m':.n = A.unlift $ A.arrayShape $ I.run w
           in if m /= m'
                 then Nothing
                 else Just $ AccMatrix $ A.generate (A.index2 k n) (aux v w)
          where aux :: Acc (Array DIM2 a) -> Acc (Array DIM2 a) -> Exp DIM2 -> Exp a
                aux v w sh = let (Z:.i:.j) = A.unlift sh
                                 v' = A.slice v (A.lift $ Z:.i:.All)
                                 w' = A.slice w (A.lift $ Z:.All:.j)
                              in A.the $ A.sum $ A.zipWith (*) v' w'

Solution

  • Your code is actually correct. Unfortunately the typechecker isn't smart enough to figure it out, so you have to help it:

    let (Z:.i:.j) = A.unlift sh
    

    becomes

    let (Z:.i:.j) = A.unlift sh :: (Z :. Exp Int) :. Exp Int
    

    The critical thing here is that A.unlift :: A.Unlift c e => c (A.Plain e) -> e but A.Plain is an associated type family (and therefore non-injective) so the type e cannot be determined without a type signature, and e is required to select an instance to use for Unlift c e. This is where the 'ambiguous type' errors come from - it is really e that is ambiguous.


    You also have an unrelated error. aux should have the type

    aux :: (IsNum e, Elt e) => ...
    

    or

    aux :: (e ~ a) => ... 
    

    in the latter case the a is the one in type signature of (#*#) so it already has the constraints IsNum, Elt