module Llama.KVCache (
kvCacheViewInit
, kvSelfSeqAdd
, kvSelfSeqDiv
, kvSelfSeqPosMax
, kvSelfDefrag
, kvSelfCanShift
, kvSelfUpdate
, kvSelfSeqKeep
, kvSelfSeqCopy
, kvSelfSeqRemove
, kvSelfClear
, kvSelfUsedCells
, kvSelfNumTokens
, kvCacheViewUpdate
) where
import Llama.Internal.Types
import Foreign
import Llama.Internal.Foreign
kvCacheViewInit :: Context -> Int -> IO LlamaKvCacheView
kvCacheViewInit :: Context -> Int -> IO LlamaKvCacheView
kvCacheViewInit (Context ForeignPtr CLlamaContext
fPtr) Int
n_seq_max_ = do
ForeignPtr CLlamaContext
-> (Ptr CLlamaContext -> IO LlamaKvCacheView)
-> IO LlamaKvCacheView
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CLlamaContext
fPtr ((Ptr CLlamaContext -> IO LlamaKvCacheView) -> IO LlamaKvCacheView)
-> (Ptr CLlamaContext -> IO LlamaKvCacheView)
-> IO LlamaKvCacheView
forall a b. (a -> b) -> a -> b
$ \Ptr CLlamaContext
contextPtr -> do
(Ptr LlamaKvCacheView -> IO LlamaKvCacheView)
-> IO LlamaKvCacheView
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr LlamaKvCacheView -> IO LlamaKvCacheView)
-> IO LlamaKvCacheView)
-> (Ptr LlamaKvCacheView -> IO LlamaKvCacheView)
-> IO LlamaKvCacheView
forall a b. (a -> b) -> a -> b
$ \Ptr LlamaKvCacheView
pView -> do
CLlamaContext -> CInt -> Ptr LlamaKvCacheView -> IO ()
c_llama_kv_cache_view_init_into (Ptr CLlamaContext -> CLlamaContext
CLlamaContext Ptr CLlamaContext
contextPtr) (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n_seq_max_) Ptr LlamaKvCacheView
pView
Ptr LlamaKvCacheView -> IO LlamaKvCacheView
forall a. Storable a => Ptr a -> IO a
peek Ptr LlamaKvCacheView
pView
kvSelfSeqAdd ::
Context ->
LlamaSeqId ->
LlamaPos ->
LlamaPos ->
LlamaPos ->
IO ()
kvSelfSeqAdd :: Context -> CInt -> CInt -> CInt -> CInt -> IO ()
kvSelfSeqAdd (Context ForeignPtr CLlamaContext
ctxFPtr) CInt
seqId CInt
p0 CInt
p1 CInt
delta =
ForeignPtr CLlamaContext -> (Ptr CLlamaContext -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CLlamaContext
ctxFPtr ((Ptr CLlamaContext -> IO ()) -> IO ())
-> (Ptr CLlamaContext -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CLlamaContext
ctxPtr ->
CLlamaContext -> CInt -> CInt -> CInt -> CInt -> IO ()
c_llama_kv_self_seq_add (Ptr CLlamaContext -> CLlamaContext
CLlamaContext Ptr CLlamaContext
ctxPtr) CInt
seqId CInt
p0 CInt
p1 CInt
delta
kvSelfSeqDiv ::
Context ->
LlamaSeqId ->
LlamaPos ->
LlamaPos ->
Int ->
IO ()
kvSelfSeqDiv :: Context -> CInt -> CInt -> CInt -> Int -> IO ()
kvSelfSeqDiv (Context ForeignPtr CLlamaContext
ctxFPtr) CInt
seqId CInt
p0 CInt
p1 Int
d =
ForeignPtr CLlamaContext -> (Ptr CLlamaContext -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CLlamaContext
ctxFPtr ((Ptr CLlamaContext -> IO ()) -> IO ())
-> (Ptr CLlamaContext -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CLlamaContext
ctxPtr ->
CLlamaContext -> CInt -> CInt -> CInt -> CInt -> IO ()
c_llama_kv_self_seq_div (Ptr CLlamaContext -> CLlamaContext
CLlamaContext Ptr CLlamaContext
ctxPtr) CInt
seqId CInt
p0 CInt
p1 (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
d)
kvSelfSeqPosMax ::
Context ->
LlamaSeqId ->
IO LlamaPos
kvSelfSeqPosMax :: Context -> CInt -> IO CInt
kvSelfSeqPosMax (Context ForeignPtr CLlamaContext
ctxFPtr) CInt
seqId =
ForeignPtr CLlamaContext
-> (Ptr CLlamaContext -> IO CInt) -> IO CInt
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CLlamaContext
ctxFPtr ((Ptr CLlamaContext -> IO CInt) -> IO CInt)
-> (Ptr CLlamaContext -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \Ptr CLlamaContext
ctxPtr ->
CLlamaContext -> CInt -> IO CInt
c_llama_kv_self_seq_pos_max (Ptr CLlamaContext -> CLlamaContext
CLlamaContext Ptr CLlamaContext
ctxPtr) CInt
seqId
kvSelfDefrag ::
Context ->
IO ()
kvSelfDefrag :: Context -> IO ()
kvSelfDefrag (Context ForeignPtr CLlamaContext
ctxFPtr) =
ForeignPtr CLlamaContext -> (Ptr CLlamaContext -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CLlamaContext
ctxFPtr ((Ptr CLlamaContext -> IO ()) -> IO ())
-> (Ptr CLlamaContext -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CLlamaContext
ctxPtr ->
CLlamaContext -> IO ()
c_llama_kv_self_defrag (Ptr CLlamaContext -> CLlamaContext
CLlamaContext Ptr CLlamaContext
ctxPtr)
kvSelfCanShift ::
Context ->
IO Bool
kvSelfCanShift :: Context -> IO Bool
kvSelfCanShift (Context ForeignPtr CLlamaContext
ctxFPtr) =
ForeignPtr CLlamaContext
-> (Ptr CLlamaContext -> IO Bool) -> IO Bool
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CLlamaContext
ctxFPtr ((Ptr CLlamaContext -> IO Bool) -> IO Bool)
-> (Ptr CLlamaContext -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \Ptr CLlamaContext
ctxPtr ->
CBool -> Bool
forall a. (Eq a, Num a) => a -> Bool
toBool (CBool -> Bool) -> IO CBool -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr CLlamaContext -> IO CBool
c_llama_kv_self_can_shift (Ptr CLlamaContext -> Ptr CLlamaContext
forall a b. Ptr a -> Ptr b
castPtr Ptr CLlamaContext
ctxPtr)
kvSelfUpdate ::
Context ->
IO ()
kvSelfUpdate :: Context -> IO ()
kvSelfUpdate (Context ForeignPtr CLlamaContext
ctxFPtr) =
ForeignPtr CLlamaContext -> (Ptr CLlamaContext -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CLlamaContext
ctxFPtr ((Ptr CLlamaContext -> IO ()) -> IO ())
-> (Ptr CLlamaContext -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CLlamaContext
ctxPtr ->
CLlamaContext -> IO ()
c_llama_kv_self_update (Ptr CLlamaContext -> CLlamaContext
CLlamaContext Ptr CLlamaContext
ctxPtr)
kvSelfSeqKeep ::
Context ->
LlamaSeqId ->
IO ()
kvSelfSeqKeep :: Context -> CInt -> IO ()
kvSelfSeqKeep (Context ForeignPtr CLlamaContext
ctxFPtr) CInt
seqId =
ForeignPtr CLlamaContext -> (Ptr CLlamaContext -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CLlamaContext
ctxFPtr ((Ptr CLlamaContext -> IO ()) -> IO ())
-> (Ptr CLlamaContext -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CLlamaContext
ctxPtr ->
CLlamaContext -> CInt -> IO ()
c_llama_kv_self_seq_keep (Ptr CLlamaContext -> CLlamaContext
CLlamaContext Ptr CLlamaContext
ctxPtr) CInt
seqId
kvSelfSeqCopy ::
Context ->
LlamaSeqId ->
LlamaSeqId ->
LlamaPos ->
LlamaPos ->
IO ()
kvSelfSeqCopy :: Context -> CInt -> CInt -> CInt -> CInt -> IO ()
kvSelfSeqCopy (Context ForeignPtr CLlamaContext
ctxFPtr) CInt
srcId CInt
dstId CInt
p0 CInt
p1 =
ForeignPtr CLlamaContext -> (Ptr CLlamaContext -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CLlamaContext
ctxFPtr ((Ptr CLlamaContext -> IO ()) -> IO ())
-> (Ptr CLlamaContext -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CLlamaContext
ctxPtr ->
CLlamaContext -> CInt -> CInt -> CInt -> CInt -> IO ()
c_llama_kv_self_seq_cp (Ptr CLlamaContext -> CLlamaContext
CLlamaContext Ptr CLlamaContext
ctxPtr) CInt
srcId CInt
dstId CInt
p0 CInt
p1
kvSelfSeqRemove ::
Context ->
LlamaSeqId ->
LlamaPos ->
LlamaPos ->
IO Bool
kvSelfSeqRemove :: Context -> CInt -> CInt -> CInt -> IO Bool
kvSelfSeqRemove (Context ForeignPtr CLlamaContext
ctxFPtr) CInt
seqId CInt
p0 CInt
p1 =
ForeignPtr CLlamaContext
-> (Ptr CLlamaContext -> IO Bool) -> IO Bool
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CLlamaContext
ctxFPtr ((Ptr CLlamaContext -> IO Bool) -> IO Bool)
-> (Ptr CLlamaContext -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \Ptr CLlamaContext
ctxPtr -> do
result <- CLlamaContext -> CInt -> CInt -> CInt -> IO CBool
c_llama_kv_self_seq_rm (Ptr CLlamaContext -> CLlamaContext
CLlamaContext Ptr CLlamaContext
ctxPtr) CInt
seqId CInt
p0 CInt
p1
return $ toBool result
kvSelfClear ::
Context ->
IO ()
kvSelfClear :: Context -> IO ()
kvSelfClear (Context ForeignPtr CLlamaContext
ctxFPtr) =
ForeignPtr CLlamaContext -> (Ptr CLlamaContext -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CLlamaContext
ctxFPtr ((Ptr CLlamaContext -> IO ()) -> IO ())
-> (Ptr CLlamaContext -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CLlamaContext
ctxPtr ->
CLlamaContext -> IO ()
c_llama_kv_self_clear (Ptr CLlamaContext -> CLlamaContext
CLlamaContext Ptr CLlamaContext
ctxPtr)
kvSelfUsedCells ::
Context ->
IO Int
kvSelfUsedCells :: Context -> IO Int
kvSelfUsedCells (Context ForeignPtr CLlamaContext
ctxFPtr) =
ForeignPtr CLlamaContext -> (Ptr CLlamaContext -> IO Int) -> IO Int
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CLlamaContext
ctxFPtr ((Ptr CLlamaContext -> IO Int) -> IO Int)
-> (Ptr CLlamaContext -> IO Int) -> IO Int
forall a b. (a -> b) -> a -> b
$ \Ptr CLlamaContext
ctxPtr ->
CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> Int) -> IO CInt -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CLlamaContext -> IO CInt
c_llama_kv_self_used_cells (Ptr CLlamaContext -> CLlamaContext
CLlamaContext Ptr CLlamaContext
ctxPtr)
kvSelfNumTokens ::
Context ->
IO Int
kvSelfNumTokens :: Context -> IO Int
kvSelfNumTokens (Context ForeignPtr CLlamaContext
ctxFPtr) =
ForeignPtr CLlamaContext -> (Ptr CLlamaContext -> IO Int) -> IO Int
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CLlamaContext
ctxFPtr ((Ptr CLlamaContext -> IO Int) -> IO Int)
-> (Ptr CLlamaContext -> IO Int) -> IO Int
forall a b. (a -> b) -> a -> b
$ \Ptr CLlamaContext
ctxPtr ->
CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> Int) -> IO CInt -> IO Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CLlamaContext -> IO CInt
c_llama_kv_self_n_tokens (Ptr CLlamaContext -> CLlamaContext
CLlamaContext Ptr CLlamaContext
ctxPtr)
kvCacheViewUpdate ::
Context ->
Ptr LlamaKvCacheView ->
IO ()
kvCacheViewUpdate :: Context -> Ptr LlamaKvCacheView -> IO ()
kvCacheViewUpdate (Context ForeignPtr CLlamaContext
ctxFPtr) Ptr LlamaKvCacheView
view =
ForeignPtr CLlamaContext -> (Ptr CLlamaContext -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr CLlamaContext
ctxFPtr ((Ptr CLlamaContext -> IO ()) -> IO ())
-> (Ptr CLlamaContext -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CLlamaContext
ctxPtr ->
CLlamaContext -> Ptr LlamaKvCacheView -> IO ()
c_llama_kv_cache_view_update (Ptr CLlamaContext -> CLlamaContext
CLlamaContext Ptr CLlamaContext
ctxPtr) Ptr LlamaKvCacheView
view