mv x/ => c/

This commit is contained in:
xushiwei
2024-05-17 22:33:57 +08:00
parent 367743530f
commit 087ba0d81d
20 changed files with 195 additions and 312 deletions

3
.gitmodules vendored
View File

@@ -1,3 +0,0 @@
[submodule "x/llama2/llama2.c"]
path = x/llama2/llama2.c
url = https://github.com/karpathy/llama2.c.git

View File

@@ -150,15 +150,15 @@ LLGo can easily import any libraries from the C ecosystem. Currently, this impor
The currently imported libraries include:
* [llama2.c](https://pkg.go.dev/github.com/goplus/llgo/x/llama2)
* [cjson](https://pkg.go.dev/github.com/goplus/llgo/x/cjson)
* [sqlite](https://pkg.go.dev/github.com/goplus/llgo/x/sqlite)
* [llama2.c](https://pkg.go.dev/github.com/goplus/llgo/c/llama2)
* [cjson](https://pkg.go.dev/github.com/goplus/llgo/c/cjson)
* [sqlite](https://pkg.go.dev/github.com/goplus/llgo/c/sqlite)
Here are some examples related to them:
* [llama2-c](_demo/llama2-c): inference Llama 2 (It's the first llgo AI example)
* [mkjson](x/cjson/_demo/mkjson/mkjson.go): create a json object and print it
* [sqlitedemo](x/sqlite/_demo/sqlitedemo/demo.go): a basic sqlite demo
* [mkjson](c/cjson/_demo/mkjson/mkjson.go): create a json object and print it
* [sqlitedemo](c/sqlite/_demo/sqlitedemo/demo.go): a basic sqlite demo
## Go syntax support

View File

@@ -2,7 +2,7 @@ package main
import (
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/x/llama2"
"github.com/goplus/llgo/c/llama2"
)
func main() {

View File

@@ -2,7 +2,7 @@ package main
import (
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/x/cjson"
"github.com/goplus/llgo/c/cjson"
)
func main() {

BIN
c/cjson/llgo_autogen.lla Normal file

Binary file not shown.

159
c/llama2/llama2.go Normal file
View File

@@ -0,0 +1,159 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package llama2
import (
_ "unsafe"
"github.com/goplus/llgo/c"
)
const (
LLGoPackage = "link"
)
// -----------------------------------------------------------------------------
// llgo:type C
type TokenIndex struct {
Str *c.Char
Id c.Int
}
// llgo:type C
type Tokenizer struct {
Vocab **c.Char
VocabScores *c.Float
SortedVocab *TokenIndex
VocabSize c.Int
MaxTokenLength c.Uint
BytePieces [512]uint8 // stores all single-byte strings
}
//go:linkname BuildTokenizer C.build_tokenizer
func BuildTokenizer(t *Tokenizer, tokenizerPath *c.Char, vocabSize c.Int)
//go:linkname FreeTokenizer C.free_tokenizer
func FreeTokenizer(t *Tokenizer)
// -----------------------------------------------------------------------------
// llgo:type C
type Config struct {
Dim c.Int // transformer dimension
HiddenDim c.Int // for ffn layers
NLayers c.Int // number of layers
NHeads c.Int // number of query heads
NKVHeads c.Int // number of key/value heads (can be < query heads because of multiquery)
VocabSize c.Int // vocabulary size, usually 256 (byte-level)
SeqLen c.Int // max sequence length
}
// llgo:type C
type TransformerWeights struct {
// token embedding table
TokenEmbeddingTable *c.Float // (vocab_size, dim)
// weights for rmsnorms
RmsAttWeight *c.Float // (layer, dim) rmsnorm weights
RmsFfnWeight *c.Float // (layer, dim)
// weights for matmuls. note dim == n_heads * head_size
Wq *c.Float // (layer, dim, n_heads * head_size)
Wk *c.Float // (layer, dim, n_kv_heads * head_size)
Wv *c.Float // (layer, dim, n_kv_heads * head_size)
Wo *c.Float // (layer, n_heads * head_size, dim)
// weights for ffn
W1 *c.Float // (layer, hidden_dim, dim)
W2 *c.Float // (layer, dim, hidden_dim)
W3 *c.Float // (layer, hidden_dim, dim)
// final rmsnorm
RmsFinalWeight *c.Float // (dim,)
// (optional) classifier weights for the logits, on the last layer
Wcls *c.Float
}
// llgo:type C
type RunState struct {
// current wave of activations
X *c.Float // activation at current time stamp (dim,)
Xb *c.Float // same, but inside a residual branch (dim,)
Xb2 *c.Float // an additional buffer just for convenience (dim,)
Hb *c.Float // buffer for hidden dimension in the ffn (hidden_dim,)
Hb2 *c.Float // buffer for hidden dimension in the ffn (hidden_dim,)
Q *c.Float // query (dim,)
K *c.Float // key (dim,)
V *c.Float // value (dim,)
Att *c.Float // buffer for scores/attention values (n_heads, seq_len)
Logits *c.Float // output logits
// kv cache
KeyCache *c.Float // (layer, seq_len, dim)
ValueCache *c.Float // (layer, seq_len, dim)
}
// llgo:type C
type Transformer struct {
Config Config // the hyperparameters of the architecture (the blueprint)
Weights TransformerWeights // the weights of the model
State RunState // buffers for the "wave" of activations in the forward pass
// some more state needed to properly clean up the memory mapping (sigh)
Fd c.Int // file descriptor for memory mapping
Data *c.Float // memory mapped data pointer
FileSize uintptr // size of the checkpoint file in bytes
}
//go:linkname BuildTransformer C.build_transformer
func BuildTransformer(t *Transformer, checkpointPath *c.Char)
//go:linkname FreeTransformer C.free_transformer
func FreeTransformer(t *Transformer)
// -----------------------------------------------------------------------------
// llgo:type C
type ProbIndex struct {
Prob c.Float
Index c.Int
} // struct used when sorting probabilities during top-p sampling
// llgo:type C
type Sampler struct {
VocabSize c.Int
Probindex *ProbIndex // buffer used in top-p sampling
Temperature c.Float
Topp c.Float
RngState uint64
}
//go:linkname BuildSampler C.build_sampler
func BuildSampler(sampler *Sampler, vocabSize c.Int, temperature c.Float, topp c.Float, rngSeed uint64)
//go:linkname FreeSampler C.free_sampler
func FreeSampler(sampler *Sampler)
// -----------------------------------------------------------------------------
//go:linkname Generate C.generate
func Generate(
transformer *Transformer, tokenizer *Tokenizer, sampler *Sampler,
prompt *c.Char, steps c.Int)
//go:linkname Chat C.chat
func Chat(
transformer *Transformer, tokenizer *Tokenizer, sampler *Sampler,
cliUserPrompt *c.Char, cliSystemPrompt *c.Char, steps c.Int)
// -----------------------------------------------------------------------------

View File

@@ -1,6 +1,6 @@
{
"cl": [
"clang -emit-llvm -S -o llgo_autogen.ll -c llama2/run.c",
"zip llgo_autogen.lla llgo_autogen.ll"
"rm llgo_autogen.lla; zip llgo_autogen.lla llgo_autogen.ll"
]
}

View File

@@ -2,7 +2,7 @@ package main
import (
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/x/sqlite"
"github.com/goplus/llgo/c/sqlite"
)
func main() {

BIN
c/sqlite/llgo_autogen.lla Normal file

Binary file not shown.

View File

@@ -22,12 +22,6 @@ import (
"github.com/goplus/llgo/c"
)
type (
Char = c.Char
Int = c.Int
Pointer = c.Pointer
)
const (
LLGoPackage = "link: sqlite3"
)
@@ -44,7 +38,7 @@ type Stmt struct {
// -----------------------------------------------------------------------------
type Errno Int
type Errno c.Int
const (
OK Errno = 0 // Successful result
@@ -83,10 +77,10 @@ const (
)
// llgo:link (Errno).Errstr C.sqlite3_errstr
func (err Errno) Errstr() *Char { return nil }
func (err Errno) Errstr() *c.Char { return nil }
// llgo:link (*Sqlite3).Errmsg C.sqlite3_errmsg
func (db *Sqlite3) Errmsg() *Char { return nil }
func (db *Sqlite3) Errmsg() *c.Char { return nil }
// llgo:link (*Sqlite3).Errcode C.sqlite3_errcode
func (db *Sqlite3) Errcode() Errno { return 0 }
@@ -97,13 +91,13 @@ func (db *Sqlite3) ExtendedErrcode() Errno { return 0 }
// -----------------------------------------------------------------------------
//go:linkname doOpen C.sqlite3_open
func doOpen(filename *Char, ppDb **Sqlite3) Errno
func doOpen(filename *c.Char, ppDb **Sqlite3) Errno
//go:linkname doOpenV2 C.sqlite3_open_v2
func doOpenV2(filename *Char, ppDb **Sqlite3, flags OpenFlags, zVfs *Char) Errno
func doOpenV2(filename *c.Char, ppDb **Sqlite3, flags OpenFlags, zVfs *c.Char) Errno
// OpenFlags represents SQLite open flags.
type OpenFlags Int
type OpenFlags c.Int
const (
OpenReadOnly OpenFlags = 0x00000001
@@ -132,7 +126,7 @@ const (
// Opening A New Database Connection
// filename: Database filename (UTF-8)
func Open(filename *Char) (db *Sqlite3, err Errno) {
func Open(filename *c.Char) (db *Sqlite3, err Errno) {
err = doOpen(filename, &db)
return
}
@@ -140,7 +134,7 @@ func Open(filename *Char) (db *Sqlite3, err Errno) {
// Opening A New Database Connection
// filename: Database filename (UTF-8)
// zVfs: Name of VFS module to use
func OpenV2(filename *Char, flags OpenFlags, zVfs *Char) (db *Sqlite3, err Errno) {
func OpenV2(filename *c.Char, flags OpenFlags, zVfs *c.Char) (db *Sqlite3, err Errno) {
err = doOpenV2(filename, &db, flags, zVfs)
return
}
@@ -158,22 +152,22 @@ func (db *Sqlite3) CloseV2() Errno { return 0 }
// -----------------------------------------------------------------------------
// llgo:link (*Sqlite3).doPrepare C.sqlite3_prepare
func (*Sqlite3) doPrepare(*Char, Int, **Stmt, **Char) Errno {
func (*Sqlite3) doPrepare(*c.Char, c.Int, **Stmt, **c.Char) Errno {
return 0
}
// llgo:link (*Sqlite3).doPrepareV2 C.sqlite3_prepare_v2
func (*Sqlite3) doPrepareV2(*Char, Int, **Stmt, **Char) Errno {
func (*Sqlite3) doPrepareV2(*c.Char, c.Int, **Stmt, **c.Char) Errno {
return 0
}
// llgo:link (*Sqlite3).doPrepareV3 C.sqlite3_prepare_v3
func (*Sqlite3) doPrepareV3(*Char, Int, PrepareFlags, **Stmt, **Char) Errno {
func (*Sqlite3) doPrepareV3(*c.Char, c.Int, PrepareFlags, **Stmt, **c.Char) Errno {
return 0
}
// PrepareFlags represents SQLite prepare flags.
type PrepareFlags Int
type PrepareFlags c.Int
const (
PreparePersistent PrepareFlags = 0x01
@@ -183,17 +177,17 @@ const (
// Compiling An SQL Statement
// tail: Pointer to unused portion of sql
func (db *Sqlite3) Prepare(sql string, tail **Char) (stmt *Stmt, err Errno) {
func (db *Sqlite3) Prepare(sql string, tail **c.Char) (stmt *Stmt, err Errno) {
err = db.doPrepare(c.GoStringData(sql), c.Int(len(sql)), &stmt, tail)
return
}
func (db *Sqlite3) PrepareV2(sql string, tail **Char) (stmt *Stmt, err Errno) {
func (db *Sqlite3) PrepareV2(sql string, tail **c.Char) (stmt *Stmt, err Errno) {
err = db.doPrepareV2(c.GoStringData(sql), c.Int(len(sql)), &stmt, tail)
return
}
func (db *Sqlite3) PrepareV3(sql string, flags PrepareFlags, tail **Char) (stmt *Stmt, err Errno) {
func (db *Sqlite3) PrepareV3(sql string, flags PrepareFlags, tail **c.Char) (stmt *Stmt, err Errno) {
err = db.doPrepareV3(c.GoStringData(sql), c.Int(len(sql)), flags, &stmt, tail)
return
}
@@ -206,10 +200,10 @@ func (stmt *Stmt) Close() Errno { return 0 }
// -----------------------------------------------------------------------------
// llgo:link (*Stmt).BindInt C.sqlite3_bind_int
func (*Stmt) BindInt(idx Int, val Int) Errno { return 0 }
func (*Stmt) BindInt(idx c.Int, val c.Int) Errno { return 0 }
// llgo:link (*Stmt).BindInt64 C.sqlite3_bind_int64
func (*Stmt) BindInt64(idx Int, val int64) Errno { return 0 }
func (*Stmt) BindInt64(idx c.Int, val int64) Errno { return 0 }
/*
const (
@@ -219,7 +213,9 @@ const (
*/
// llgo:link (*Stmt).BindText C.sqlite3_bind_text
func (*Stmt) BindText(idx Int, val *Char, nByte Int, destructor func(Pointer)) Errno { return 0 }
func (*Stmt) BindText(idx c.Int, val *c.Char, nByte c.Int, destructor func(c.Pointer)) Errno {
return 0
}
// -----------------------------------------------------------------------------
@@ -238,19 +234,19 @@ func (*Stmt) Step() Errno { return 0 }
// -----------------------------------------------------------------------------
// llgo:link (*Stmt).ColumnCount C.sqlite3_column_count
func (stmt *Stmt) ColumnCount() Int { return 0 }
func (stmt *Stmt) ColumnCount() c.Int { return 0 }
// llgo:link (*Stmt).ColumnName C.sqlite3_column_name
func (stmt *Stmt) ColumnName(idx Int) *Char { return nil }
func (stmt *Stmt) ColumnName(idx c.Int) *c.Char { return nil }
// llgo:link (*Stmt).ColumnInt C.sqlite3_column_int
func (stmt *Stmt) ColumnInt(idx Int) Int { return 0 }
func (stmt *Stmt) ColumnInt(idx c.Int) c.Int { return 0 }
// llgo:link (*Stmt).ColumnInt64 C.sqlite3_column_int64
func (stmt *Stmt) ColumnInt64(idx Int) int64 { return 0 }
func (stmt *Stmt) ColumnInt64(idx c.Int) int64 { return 0 }
// llgo:link (*Stmt).ColumnText C.sqlite3_column_text
func (stmt *Stmt) ColumnText(idx Int) *Char { return nil }
func (stmt *Stmt) ColumnText(idx c.Int) *c.Char { return nil }
// -----------------------------------------------------------------------------
@@ -258,8 +254,8 @@ func (stmt *Stmt) ColumnText(idx Int) *Char { return nil }
//
// llgo:link (*Sqlite3).Exec C.sqlite3_exec
func (*Sqlite3) Exec(
sql *Char, callback func(arg Pointer, resultCols Int, colVals, colNames **Char) Int,
arg Pointer, errmsg **Char) Errno {
sql *c.Char, callback func(arg c.Pointer, resultCols c.Int, colVals, colNames **c.Char) c.Int,
arg c.Pointer, errmsg **c.Char) Errno {
return 0
}

Binary file not shown.

View File

@@ -1,268 +0,0 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package llama2
import (
_ "unsafe"
"github.com/goplus/llgo/c"
)
const (
LLGoPackage = "link"
)
type (
Char = c.Char
Int = c.Int
Uint = c.Uint
Float = c.Float
)
// -----------------------------------------------------------------------------
/*
typedef struct {
char *str;
int id;
} TokenIndex;
typedef struct {
char** vocab;
float* vocab_scores;
TokenIndex *sorted_vocab;
int vocab_size;
unsigned int max_token_length;
unsigned char byte_pieces[512]; // stores all single-byte strings
} Tokenizer;
void build_tokenizer(Tokenizer* t, char* tokenizer_path, int vocab_size);
void free_tokenizer(Tokenizer* t);
typedef struct {
int dim; // transformer dimension
int hidden_dim; // for ffn layers
int n_layers; // number of layers
int n_heads; // number of query heads
int n_kv_heads; // number of key/value heads (can be < query heads because of multiquery)
int vocab_size; // vocabulary size, usually 256 (byte-level)
int seq_len; // max sequence length
} Config;
typedef struct {
// token embedding table
float* token_embedding_table; // (vocab_size, dim)
// weights for rmsnorms
float* rms_att_weight; // (layer, dim) rmsnorm weights
float* rms_ffn_weight; // (layer, dim)
// weights for matmuls. note dim == n_heads * head_size
float* wq; // (layer, dim, n_heads * head_size)
float* wk; // (layer, dim, n_kv_heads * head_size)
float* wv; // (layer, dim, n_kv_heads * head_size)
float* wo; // (layer, n_heads * head_size, dim)
// weights for ffn
float* w1; // (layer, hidden_dim, dim)
float* w2; // (layer, dim, hidden_dim)
float* w3; // (layer, hidden_dim, dim)
// final rmsnorm
float* rms_final_weight; // (dim,)
// (optional) classifier weights for the logits, on the last layer
float* wcls;
} TransformerWeights;
typedef struct {
// current wave of activations
float *x; // activation at current time stamp (dim,)
float *xb; // same, but inside a residual branch (dim,)
float *xb2; // an additional buffer just for convenience (dim,)
float *hb; // buffer for hidden dimension in the ffn (hidden_dim,)
float *hb2; // buffer for hidden dimension in the ffn (hidden_dim,)
float *q; // query (dim,)
float *k; // key (dim,)
float *v; // value (dim,)
float *att; // buffer for scores/attention values (n_heads, seq_len)
float *logits; // output logits
// kv cache
float* key_cache; // (layer, seq_len, dim)
float* value_cache; // (layer, seq_len, dim)
} RunState;
typedef struct {
Config config; // the hyperparameters of the architecture (the blueprint)
TransformerWeights weights; // the weights of the model
RunState state; // buffers for the "wave" of activations in the forward pass
// some more state needed to properly clean up the memory mapping (sigh)
int fd; // file descriptor for memory mapping
float* data; // memory mapped data pointer
ssize_t file_size; // size of the checkpoint file in bytes
} Transformer;
void build_transformer(Transformer *t, char* checkpoint_path);
void free_transformer(Transformer* t);
typedef struct {
float prob;
int index;
} ProbIndex; // struct used when sorting probabilities during top-p sampling
typedef struct {
int vocab_size;
ProbIndex* probindex; // buffer used in top-p sampling
float temperature;
float topp;
unsigned long long rng_state;
} Sampler;
void build_sampler(Sampler* sampler, int vocab_size, float temperature, float topp, unsigned long long rng_seed);
void free_sampler(Sampler* sampler);
void generate(Transformer *transformer, Tokenizer *tokenizer, Sampler *sampler, char *prompt, int steps);
*/
// -----------------------------------------------------------------------------
// llgo:type C
type TokenIndex struct {
Str *Char
Id Int
}
// llgo:type C
type Tokenizer struct {
Vocab **Char
VocabScores *Float
SortedVocab *TokenIndex
VocabSize Int
MaxTokenLength Uint
BytePieces [512]uint8 // stores all single-byte strings
}
//go:linkname BuildTokenizer C.build_tokenizer
func BuildTokenizer(t *Tokenizer, tokenizerPath *Char, vocabSize Int)
//go:linkname FreeTokenizer C.free_tokenizer
func FreeTokenizer(t *Tokenizer)
// -----------------------------------------------------------------------------
// llgo:type C
type Config struct {
Dim Int // transformer dimension
HiddenDim Int // for ffn layers
NLayers Int // number of layers
NHeads Int // number of query heads
NKVHeads Int // number of key/value heads (can be < query heads because of multiquery)
VocabSize Int // vocabulary size, usually 256 (byte-level)
SeqLen Int // max sequence length
}
// llgo:type C
type TransformerWeights struct {
// token embedding table
TokenEmbeddingTable *Float // (vocab_size, dim)
// weights for rmsnorms
RmsAttWeight *Float // (layer, dim) rmsnorm weights
RmsFfnWeight *Float // (layer, dim)
// weights for matmuls. note dim == n_heads * head_size
Wq *Float // (layer, dim, n_heads * head_size)
Wk *Float // (layer, dim, n_kv_heads * head_size)
Wv *Float // (layer, dim, n_kv_heads * head_size)
Wo *Float // (layer, n_heads * head_size, dim)
// weights for ffn
W1 *Float // (layer, hidden_dim, dim)
W2 *Float // (layer, dim, hidden_dim)
W3 *Float // (layer, hidden_dim, dim)
// final rmsnorm
RmsFinalWeight *Float // (dim,)
// (optional) classifier weights for the logits, on the last layer
Wcls *Float
}
// llgo:type C
type RunState struct {
// current wave of activations
X *Float // activation at current time stamp (dim,)
Xb *Float // same, but inside a residual branch (dim,)
Xb2 *Float // an additional buffer just for convenience (dim,)
Hb *Float // buffer for hidden dimension in the ffn (hidden_dim,)
Hb2 *Float // buffer for hidden dimension in the ffn (hidden_dim,)
Q *Float // query (dim,)
K *Float // key (dim,)
V *Float // value (dim,)
Att *Float // buffer for scores/attention values (n_heads, seq_len)
Logits *Float // output logits
// kv cache
KeyCache *Float // (layer, seq_len, dim)
ValueCache *Float // (layer, seq_len, dim)
}
// llgo:type C
type Transformer struct {
Config Config // the hyperparameters of the architecture (the blueprint)
Weights TransformerWeights // the weights of the model
State RunState // buffers for the "wave" of activations in the forward pass
// some more state needed to properly clean up the memory mapping (sigh)
Fd Int // file descriptor for memory mapping
Data *Float // memory mapped data pointer
FileSize uintptr // size of the checkpoint file in bytes
}
//go:linkname BuildTransformer C.build_transformer
func BuildTransformer(t *Transformer, checkpointPath *Char)
//go:linkname FreeTransformer C.free_transformer
func FreeTransformer(t *Transformer)
// -----------------------------------------------------------------------------
// llgo:type C
type ProbIndex struct {
Prob Float
Index Int
} // struct used when sorting probabilities during top-p sampling
// llgo:type C
type Sampler struct {
VocabSize Int
Probindex *ProbIndex // buffer used in top-p sampling
Temperature Float
Topp Float
RngState uint64
}
//go:linkname BuildSampler C.build_sampler
func BuildSampler(sampler *Sampler, vocabSize Int, temperature Float, topp Float, rngSeed uint64)
//go:linkname FreeSampler C.free_sampler
func FreeSampler(sampler *Sampler)
// -----------------------------------------------------------------------------
//go:linkname Generate C.generate
func Generate(
transformer *Transformer, tokenizer *Tokenizer, sampler *Sampler,
prompt *Char, steps Int)
//go:linkname Chat C.chat
func Chat(
transformer *Transformer, tokenizer *Tokenizer, sampler *Sampler,
cliUserPrompt *Char, cliSystemPrompt *Char, steps Int)
// -----------------------------------------------------------------------------

Binary file not shown.