Skip to content

Commit

Permalink
feat: add model details in context (#52)
Browse files Browse the repository at this point in the history
* feat: add model details in context

* fix: type and key

* chore(example): cleanup
  • Loading branch information
jhen0409 authored Apr 26, 2024
1 parent cf685ad commit f2a4c53
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 39 deletions.
9 changes: 9 additions & 0 deletions android/src/main/java/com/rnllama/LlamaContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class LlamaContext {
private int id;
private ReactApplicationContext reactContext;
private long context;
private WritableMap modelDetails;
private int jobId = -1;
private DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter;

Expand Down Expand Up @@ -63,6 +64,7 @@ public LlamaContext(int id, ReactApplicationContext reactContext, ReadableMap pa
// float rope_freq_scale
params.hasKey("rope_freq_scale") ? (float) params.getDouble("rope_freq_scale") : 0.0f
);
this.modelDetails = loadModelDetails(this.context);
this.reactContext = reactContext;
eventEmitter = reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
}
Expand All @@ -71,6 +73,10 @@ public long getContext() {
return context;
}

public WritableMap getModelDetails() {
return modelDetails;
}

private void emitPartialCompletion(WritableMap tokenResult) {
WritableMap event = Arguments.createMap();
event.putInt("contextId", LlamaContext.this.id);
Expand Down Expand Up @@ -297,6 +303,9 @@ protected static native long initContext(
float rope_freq_base,
float rope_freq_scale
);
protected static native WritableMap loadModelDetails(
long contextPtr
);
protected static native WritableMap loadSession(
long contextPtr,
String path
Expand Down
1 change: 1 addition & 0 deletions android/src/main/java/com/rnllama/RNLlama.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ protected WritableMap doInBackground(Void... voids) {
result.putInt("contextId", id);
result.putBoolean("gpu", false);
result.putString("reasonNoGPU", "Currently not supported");
result.putMap("model", llamaContext.getModelDetails());
return result;
} catch (Exception e) {
exception = e;
Expand Down
32 changes: 32 additions & 0 deletions android/src/main/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,38 @@ Java_com_rnllama_LlamaContext_initContext(
return reinterpret_cast<jlong>(llama->ctx);
}

JNIEXPORT jobject JNICALL
Java_com_rnllama_LlamaContext_loadModelDetails(
JNIEnv *env,
jobject thiz,
jlong context_ptr
) {
UNUSED(thiz);
auto llama = context_map[(long) context_ptr];

int count = llama_model_meta_count(llama->model);
auto meta = createWriteableMap(env);
for (int i = 0; i < count; i++) {
char key[256];
llama_model_meta_key_by_index(llama->model, i, key, sizeof(key));
char val[256];
llama_model_meta_val_str_by_index(llama->model, i, val, sizeof(val));

putString(env, meta, key, val);
}

auto result = createWriteableMap(env);

char desc[1024];
llama_model_desc(llama->model, desc, sizeof(desc));
putString(env, result, "desc", desc);
putDouble(env, result, "size", llama_model_size(llama->model));
putDouble(env, result, "nParams", llama_model_n_params(llama->model));
putMap(env, result, "metadata", meta);

return reinterpret_cast<jobject>(result);
}

JNIEXPORT jobject JNICALL
Java_com_rnllama_LlamaContext_loadSession(
JNIEnv *env,
Expand Down
16 changes: 8 additions & 8 deletions docs/API/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ llama.rn

#### Defined in

[index.ts:43](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L43)
[index.ts:43](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L43)

___

Expand All @@ -53,7 +53,7 @@ ___

#### Defined in

[index.ts:41](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L41)
[index.ts:41](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L41)

___

Expand All @@ -63,7 +63,7 @@ ___

#### Defined in

[index.ts:39](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L39)
[index.ts:39](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L39)

___

Expand All @@ -80,7 +80,7 @@ ___

#### Defined in

[index.ts:29](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L29)
[index.ts:29](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L29)

## Functions

Expand All @@ -104,7 +104,7 @@ ___

#### Defined in

[grammar.ts:824](https://github.com/mybigday/llama.rn/blob/17714d4/src/grammar.ts#L824)
[grammar.ts:824](https://github.com/mybigday/llama.rn/blob/a2b459e/src/grammar.ts#L824)

___

Expand All @@ -124,7 +124,7 @@ ___

#### Defined in

[index.ts:160](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L160)
[index.ts:165](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L165)

___

Expand All @@ -138,7 +138,7 @@ ___

#### Defined in

[index.ts:176](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L176)
[index.ts:181](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L181)

___

Expand All @@ -158,4 +158,4 @@ ___

#### Defined in

[index.ts:156](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L156)
[index.ts:161](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L161)
37 changes: 24 additions & 13 deletions docs/API/classes/LlamaContext.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

- [gpu](LlamaContext.md#gpu)
- [id](LlamaContext.md#id)
- [model](LlamaContext.md#model)
- [reasonNoGPU](LlamaContext.md#reasonnogpu)

### Methods
Expand Down Expand Up @@ -40,7 +41,7 @@

#### Defined in

[index.ts:60](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L60)
[index.ts:62](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L62)

## Properties

Expand All @@ -50,7 +51,7 @@

#### Defined in

[index.ts:56](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L56)
[index.ts:56](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L56)

___

Expand All @@ -60,7 +61,17 @@ ___

#### Defined in

[index.ts:54](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L54)
[index.ts:54](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L54)

___

### model

**model**: `Object` = `{}`

#### Defined in

[index.ts:60](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L60)

___

Expand All @@ -70,7 +81,7 @@ ___

#### Defined in

[index.ts:58](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L58)
[index.ts:58](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L58)

## Methods

Expand All @@ -93,7 +104,7 @@ ___

#### Defined in

[index.ts:129](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L129)
[index.ts:134](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L134)

___

Expand All @@ -114,7 +125,7 @@ ___

#### Defined in

[index.ts:84](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L84)
[index.ts:89](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L89)

___

Expand All @@ -134,7 +145,7 @@ ___

#### Defined in

[index.ts:121](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L121)
[index.ts:126](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L126)

___

Expand All @@ -154,7 +165,7 @@ ___

#### Defined in

[index.ts:125](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L125)
[index.ts:130](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L130)

___

Expand All @@ -176,7 +187,7 @@ Load cached prompt & completion state from a file.

#### Defined in

[index.ts:73](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L73)
[index.ts:78](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L78)

___

Expand All @@ -190,7 +201,7 @@ ___

#### Defined in

[index.ts:151](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L151)
[index.ts:156](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L156)

___

Expand All @@ -214,7 +225,7 @@ Save current cached prompt & completion state to a file.

#### Defined in

[index.ts:80](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L80)
[index.ts:85](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L85)

___

Expand All @@ -228,7 +239,7 @@ ___

#### Defined in

[index.ts:113](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L113)
[index.ts:118](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L118)

___

Expand All @@ -248,4 +259,4 @@ ___

#### Defined in

[index.ts:117](https://github.com/mybigday/llama.rn/blob/17714d4/src/index.ts#L117)
[index.ts:122](https://github.com/mybigday/llama.rn/blob/a2b459e/src/index.ts#L122)
Loading

0 comments on commit f2a4c53

Please sign in to comment.