-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci(run-tests.yml): add GitHub Actions workflow for running tests
refactor(Context.php): change Context class from final to non-final feat(Context.php): add optional LLamaCPPFFI parameter to createWithParameter method fix(TokenGeneratedEvent.php): fix namespace typo in TokenGeneratedEvent class refactor(LLamaCPP.php): add support for multi-threaded token generation and evaluation refactor(LLamaCPPFFI.php): remove comments for generated wrapper methods feat(LLamaCPPFFI.php): add llama_init_from_file method to allocate memory for the model feat(LLamaCPPFFI.php): add llama_tokenize method to convert text into tokens feat(LLamaCPPFFI.php): add llama_eval method to run llama inference and obtain logits and probabilities feat(LLamaCPPFFI.php): add llama_sample_top_p_top_k method to sample top-k and top-p from the logits chore(LLamaCPPFFI.php): add docblocks to llama_token_to_str and llama_free methods refactor(Locator.php): import class_exists function feat(GenerationParameters.php): add noOfThreads parameter to GenerationParameters class feat(ModelParameters.php): add parameter description to ModelParameters class test(ContextTest.php): add unit tests for Context class methods test(LLamaCPPTest.php): add tests for LLamaCPP class generate and generateAll methods feat(GenerationParametersTest.php): add tests for GenerationParameters class constructor and getters test(ModelParametersTest.php): add test for ModelParameters constructor and getters
- Loading branch information
Showing
12 changed files
with
443 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: Tests | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
test: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
fail-fast: true | ||
matrix: | ||
os: [ubuntu-latest] | ||
php: [8.2, 8.1] | ||
stability: [prefer-lowest, prefer-stable] | ||
|
||
name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }} | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: ${{ matrix.php }} | ||
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo | ||
coverage: none | ||
|
||
- name: Setup problem matchers | ||
run: | | ||
echo "::add-matcher::${{ runner.tool_cache }}/php.json" | ||
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" | ||
- name: Install dependencies | ||
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction | ||
|
||
- name: Execute tests | ||
run: vendor/bin/phpunit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
namespace Kambo\Tests\LLamaCPP; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Kambo\LLamaCPP\Parameters\ModelParameters; | ||
use Kambo\LLamaCPP\Native\LLamaCPPFFI; | ||
use Kambo\LLamaCPP\Context; | ||
use FFI\CData; | ||
use FFI; | ||
|
||
class ContextTest extends TestCase | ||
{ | ||
private LLamaCPPFFI $ffiMock; | ||
private ModelParameters $modelParametersMock; | ||
private FFI $ffi; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->ffiMock = $this->getMockBuilder(LLamaCPPFFI::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->modelParametersMock = $this->getMockBuilder(ModelParameters::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->ffi = FFI::cdef('struct llama_context; struct llama_context_params { | ||
int n_ctx; // text context | ||
int n_parts; // -1 for default | ||
int seed; // RNG seed, 0 for random | ||
bool f16_kv; // use fp16 for KV cache | ||
bool logits_all; // the llama_eval() call computes all logits, not just the last one | ||
bool vocab_only; // only load the vocabulary, no weights | ||
bool use_mlock; // force system to keep model in RAM | ||
bool embedding; // embedding mode only | ||
};'); | ||
} | ||
|
||
public function testConstruct() | ||
{ | ||
$this->ffiMock->method('llama_context_default_params') | ||
->willReturn($this->ffi->new('struct llama_context_params')); | ||
|
||
$this->modelParametersMock->method('getNCtx') | ||
->willReturn(512); | ||
$this->modelParametersMock->method('getNParts') | ||
->willReturn(16); | ||
$this->modelParametersMock->method('getSeed') | ||
->willReturn(42); | ||
$this->modelParametersMock->method('isF16KV') | ||
->willReturn(false); | ||
$this->modelParametersMock->method('isLogitsAll') | ||
->willReturn(true); | ||
$this->modelParametersMock->method('isVocabOnly') | ||
->willReturn(false); | ||
$this->modelParametersMock->method('isUseMlock') | ||
->willReturn(false); | ||
$this->modelParametersMock->method('isEmbedding') | ||
->willReturn(true); | ||
$this->modelParametersMock->method('getModelPath') | ||
->willReturn('/path/to/model'); | ||
|
||
$this->ffiMock->method('llama_init_from_file') | ||
->willReturn(FFI::new('int')); | ||
|
||
$context = new Context($this->ffiMock, $this->modelParametersMock); | ||
|
||
$this->assertInstanceOf(Context::class, $context); | ||
} | ||
|
||
public function testCreateWithParameter() | ||
{ | ||
$this->ffiMock->method('llama_context_default_params') | ||
->willReturn($this->ffi->new('struct llama_context_params')); | ||
|
||
$this->modelParametersMock->method('getNCtx') | ||
->willReturn(512); | ||
|
||
$this->ffiMock->method('llama_init_from_file') | ||
->willReturn(FFI::new('int')); | ||
|
||
$context = Context::createWithParameter($this->modelParametersMock, $this->ffiMock); | ||
|
||
$this->assertInstanceOf(Context::class, $context); | ||
} | ||
|
||
public function testGetCtx() | ||
{ | ||
$this->ffiMock->method('llama_context_default_params') | ||
->willReturn($this->ffi->new('struct llama_context_params')); | ||
|
||
$cdataStub = FFI::new('int'); | ||
$this->ffiMock->method('llama_init_from_file') | ||
->willReturn($cdataStub); | ||
|
||
$context = new Context($this->ffiMock, $this->modelParametersMock); | ||
|
||
$this->assertSame( | ||
$cdataStub, | ||
$context->getCtx() | ||
); | ||
} | ||
|
||
public function testDestruct() | ||
{ | ||
$this->ffiMock->method('llama_context_default_params') | ||
->willReturn($this->ffi->new('struct llama_context_params')); | ||
|
||
$this->ffiMock->method('llama_init_from_file') | ||
->willReturn(FFI::new('int')); | ||
|
||
$context = new Context($this->ffiMock, $this->modelParametersMock); | ||
|
||
// Expect llama_free to be called once with the given CData stub | ||
$this->ffiMock->expects($this->once()) | ||
->method('llama_free') | ||
->with(FFI::new('int')); | ||
|
||
// Trigger __destruct by unsetting the context | ||
unset($context); | ||
} | ||
} |
Oops, something went wrong.