Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/internal/1665-arm-d-b5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Migrate tensor and model container reads and writes to the slot API.
414 changes: 218 additions & 196 deletions src/builtins_tensor.c

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions src/model_infer.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,8 @@ Value* builtin_eigen_generate(Value *arg) {
fprintf(stderr, "eigen_generate: requires [prompt_ids, temperature, max_tokens]\n");
TRACE_NONDET_RECORD("eigen_generate", make_list(0));
}
Value *prompt_list = list_get_borrow(arg, 0);
EIGS_VIEW(prompt_list_view);
Value *prompt_list = list_get_view(arg, 0, &prompt_list_view);
if (prompt_list->type != VAL_LIST) {
fprintf(stderr, "eigen_generate: prompt_ids must be a list\n");
TRACE_NONDET_RECORD("eigen_generate", make_list(0));
Expand All @@ -569,9 +570,9 @@ Value* builtin_eigen_generate(Value *arg) {
double top_p = 0.0;
/* #1637: optional numbers -- null keeps the default, a bool raises. */
double mt = max_tokens;
eigs_opt_num(list_get_borrow(arg, 1), &temperature, __func__);
eigs_opt_num(list_get_borrow(arg, 2), &mt, __func__);
if (arg->data.list.count >= 4) eigs_opt_num(list_get_borrow(arg, 3), &top_p, __func__);
eigs_slot_opt_num(list_slot(arg, 1), &temperature, __func__);
eigs_slot_opt_num(list_slot(arg, 2), &mt, __func__);
if (arg->data.list.count >= 4) eigs_slot_opt_num(list_slot(arg, 3), &top_p, __func__);
if (g_has_error) return make_null();
max_tokens = (int)mt;

Expand Down Expand Up @@ -607,8 +608,7 @@ Value* builtin_eigen_generate(Value *arg) {

int *prompt_ids = xcalloc(prompt_len, sizeof(int));
for (int i = 0; i < prompt_len; i++) {
Value *v = list_get_borrow(prompt_list, i);
prompt_ids[i] = (int)eigs_num_arg(v, "eigen_generate"); /* #1637: a bool raises */
prompt_ids[i] = (int)eigs_slot_num(list_slot(prompt_list, i), "eigen_generate"); /* #1637: a bool raises */
}
if (g_has_error) { free(prompt_ids); return make_null(); }

Expand Down Expand Up @@ -651,12 +651,13 @@ Value* builtin_eigen_eval_loss(Value *arg) {
* a real score. */
ARG_GUARD(1, "eigen_eval_loss", "[prompt_ids, target_id]", make_num(-1.0));
}
Value *prompt_list = list_get_borrow(arg, 0);
EIGS_VIEW(prompt_list_view);
Value *prompt_list = list_get_view(arg, 0, &prompt_list_view);
if (prompt_list->type != VAL_LIST) {
fprintf(stderr, "eigen_eval_loss: prompt_ids must be a list\n");
ARG_GUARD(1, "eigen_eval_loss", "a list of prompt ids", make_num(-1.0));
}
if (list_get_borrow(arg, 1)->type != VAL_NUM) {
if (list_elem_type(arg, 1) != VAL_NUM) {
fprintf(stderr, "eigen_eval_loss: target_id must be a number\n");
ARG_GUARD(1, "eigen_eval_loss", "a numeric target_id", make_num(-1.0));
}
Expand Down Expand Up @@ -692,8 +693,7 @@ Value* builtin_eigen_eval_loss(Value *arg) {

int *prompt_ids = xcalloc(prompt_len, sizeof(int));
for (int i = 0; i < prompt_len; i++) {
Value *v = list_get_borrow(prompt_list, i);
int t = (int)eigs_num_arg(v, "eigen_eval_loss"); /* #1637: a bool raises */
int t = (int)eigs_slot_num(list_slot(prompt_list, i), "eigen_eval_loss"); /* #1637: a bool raises */
if (t < 0 || t >= vocab_size) t = 0;
prompt_ids[i] = t;
}
Expand Down
9 changes: 6 additions & 3 deletions src/model_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,12 +600,15 @@ Value* eigs_json_parse_value(const char *s, int *pos);

Value* json_obj_get(Value *obj, const char *key) {
if (!obj) return NULL;
if (obj->type == VAL_DICT) return dict_get(obj, key);
if (obj->type == VAL_DICT) {
int i = env_hash_find_dict(obj, key, env_hash_name(key));
return i < 0 ? NULL : dict_value_get_return(obj, i);
}
if (obj->type != VAL_LIST) return NULL;
for (int i = 0; i + 1 < obj->data.list.count; i += 2) {
Value *k = list_get_borrow(obj, i);
Value *k = list_get_ref(obj, i);
if (k && k->type == VAL_STR && strcmp(k->data.str, key) == 0) {
return list_get_borrow(obj, i + 1);
return list_get_return(obj, i + 1);
}
}
return NULL;
Expand Down
19 changes: 10 additions & 9 deletions src/model_train.c
Original file line number Diff line number Diff line change
Expand Up @@ -1444,16 +1444,19 @@ Value* builtin_native_train_step(Value *arg) {
return make_str("{\"status\": \"error\", \"error\": \"Model not loaded\"}");
}

Value *in_list = list_get_borrow(arg, 0);
Value *out_list = list_get_borrow(arg, 1);
EIGS_VIEW(in_list_view);

Value *in_list = list_get_view(arg, 0, &in_list_view);
EIGS_VIEW(out_list_view);
Value *out_list = list_get_view(arg, 1, &out_list_view);
if (in_list->type != VAL_LIST || out_list->type != VAL_LIST) {
return make_str("{\"status\": \"error\", \"error\": \"input_ids and output_ids must be lists\"}");
}

float lr = 0.001f;
if (list_get_borrow(arg, 2)->type == VAL_BOOL) { eigs_num_arg(list_get_borrow(arg, 2), __func__); return make_null(); } /* #1637 */
if (list_get_borrow(arg, 2)->type == VAL_NUM) lr = eigs_list_num(arg, 2, __func__);
else if (list_get_borrow(arg, 2)->type == VAL_STR) lr = strtod(list_get_borrow(arg, 2)->data.str, NULL);
if (list_elem_type(arg, 2) == VAL_BOOL) { eigs_slot_num(list_slot(arg, 2), __func__); return make_null(); } /* #1637 */
if (list_elem_type(arg, 2) == VAL_NUM) lr = eigs_list_num(arg, 2, __func__);
else if (list_elem_type(arg, 2) == VAL_STR) lr = strtod(list_get_ref(arg, 2)->data.str, NULL);
if (lr <= 0 || lr > 1) lr = 0.001f;

int input_len = in_list->data.list.count;
Expand All @@ -1468,12 +1471,10 @@ Value* builtin_native_train_step(Value *arg) {
int *input_ids = xcalloc(input_len > 0 ? input_len : 1, sizeof(int));
int *output_ids = xcalloc(output_len > 0 ? output_len : 1, sizeof(int));
for (int i = 0; i < input_len; i++) {
Value *v = list_get_borrow(in_list, i);
input_ids[i] = (int)eigs_num_arg(v, "native_train_step"); /* #1637: a bool raises */
input_ids[i] = (int)eigs_slot_num(list_slot(in_list, i), "native_train_step"); /* #1637: a bool raises */
}
for (int i = 0; i < output_len; i++) {
Value *v = list_get_borrow(out_list, i);
output_ids[i] = (int)eigs_num_arg(v, "native_train_step");
output_ids[i] = (int)eigs_slot_num(list_slot(out_list, i), "native_train_step");
}
if (g_has_error) { free(input_ids); free(output_ids); return make_null(); }

Expand Down
10 changes: 10 additions & 0 deletions tests/test_slot_escape_regress.eigs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@ for i in range of 70:
deep is [deep]
assert_eq of [deep == deep, true, "a deep nest equals itself"]

# A ragged tensor holding a bare number where a row belongs: B5 read it with
# list_get_ref (NULL for a number) and crashed; the type must be tested first.
assert_eq of [add of [[[1, 2], 3], [1, 1]], [[2, 3], [4, 4]], "ragged broadcast add treats a number row as a scalar"]
assert_eq of [gather of [[[1, 2], 5], [0, 0]], [1, 0], "gather on a ragged tensor"]
try:
r is relu of ([[7, 1], 1])
assert_eq of ["no raise", "raise", "relu on a ragged tensor raises"]
catch e:
assert_eq of [e.kind, "type_mismatch", "relu on a ragged tensor raises a type error"]

test_summary of null
4 changes: 4 additions & 0 deletions tools/container_migrated_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ src/eigenscript.c
src/trace.c
src/vm.c
src/builtins.c
src/builtins_tensor.c
src/model_infer.c
src/model_train.c
src/model_io.c
1 change: 0 additions & 1 deletion tools/num_read_allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# count), when a function with raw tokens is missing here, or a row is stale.
# Builtins never belong here: they read through eigs_num_arg / eigs_list_num /
# eigs_opt_num / eigs_elem_num, which raise on a bool.
builtins_tensor.c|builtin_numerical_grad_rows|3|in-place writes of a matrix cell that tensor_cells_numeric proved a number before any mutation
builtins_tensor.c|builtin_tensor_matmul|1|writes the NaN-boxed null sentinel into a result buffer cell (EIGS_STRICT=0 path); a write, no Value is read
container.h|dict_value_get_num|1|accessor numeric fast path: reads the number only behind its own type == VAL_NUM test (#1665 arm D)
container.h|dict_value_get_owned|1|owned numeric read copies a proven VAL_NUM box for flip-safe ownership
Expand Down
44 changes: 22 additions & 22 deletions tools/strict_shape_contracts.json
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,7 @@
"b38ee26c123f3a682f61cfacc3cd75d7dd6423aa2eafbceb49e64b2e63ee47bf"
],
"call_eigs_fn": [
"123bbc53a201557a82d9c75caf6dc231d9c2485e9d2ccce3a296ccca5c9f95d6"
"7b0b1ef9ee65309c4c965af3c4167aa1e832da6185c80f16f3b05bb42d2b860c"
],
"env_set_local": [
"bde886fc51cd3b927e7c8c487a7a0d270453133c4e598506927a2d549788afbf"
Expand Down Expand Up @@ -2268,7 +2268,7 @@
"f6bb04ca6134dd41d1a67ee91ea2b9587e13477f13ed51afaa3834de1d4f2b78"
],
"tensor_unary": [
"4abceb6bf32d03e72bf0b7245447987b78885d079328ae6504e48bf8afa170f9"
"6cd5986e901ac953b121ba49c8b8d0eac1c53a1cf27cb8889b7f8f9a7e1b9a11"
]
}
},
Expand Down Expand Up @@ -2309,7 +2309,7 @@
"40100cc6cf4d92300cf1d3fbae23cb21adf4241bd01edecc9a683a07fb30bc01"
],
"tensor_to_flat": [
"935bb2e66301b7e41ac4d999bb1e5bfee0b4b4ab8afd54bb73d0369f71c67898"
"d6c74fa5dc6c5d4d702b6f2eb1746dbe61271fd05f9bae972bdefa05d607cedc"
]
}
},
Expand All @@ -2325,11 +2325,11 @@
],
"disposition": "exempt",
"body_sha256": [
"6923863238d3db9fb71ddeb0c0b56d92e9436253f886350d6b89560b5f55e963"
"a247feeafdfc05d844eb1bdd0f3eed2be3cf37a4801ca34add1628d8f141b8a5"
],
"source_sha256": {
"builtin_tensor_load": [
"6923863238d3db9fb71ddeb0c0b56d92e9436253f886350d6b89560b5f55e963"
"a247feeafdfc05d844eb1bdd0f3eed2be3cf37a4801ca34add1628d8f141b8a5"
]
}
},
Expand All @@ -2352,7 +2352,7 @@
"ff086ad360320823ab29cb9cfbeed6ec340ae78f959cb18c187c227db55f947f"
],
"tensor_unary": [
"4abceb6bf32d03e72bf0b7245447987b78885d079328ae6504e48bf8afa170f9"
"6cd5986e901ac953b121ba49c8b8d0eac1c53a1cf27cb8889b7f8f9a7e1b9a11"
]
}
},
Expand All @@ -2368,11 +2368,11 @@
],
"disposition": "exempt",
"body_sha256": [
"28d209db0561173b9426149c963f32bbb8a1d4bd15ded1aad450e3e84e4b986e"
"868db4f26fa6b315a63c25e1474c2ba2bbea74c1ff294876bfef6c715906a0e3"
],
"source_sha256": {
"builtin_tensor_log_softmax": [
"28d209db0561173b9426149c963f32bbb8a1d4bd15ded1aad450e3e84e4b986e"
"868db4f26fa6b315a63c25e1474c2ba2bbea74c1ff294876bfef6c715906a0e3"
]
}
},
Expand Down Expand Up @@ -2431,10 +2431,10 @@
"f6eec99bd0153900403028372a294cf84be9485ce98b7ad8843ce4e2a5d390ee"
],
"tensor_flatten_recursive": [
"3844bd92ee4238bec0b3bd2645eb4bd17d18a6d681a7ad7a8e21291282e33d14"
"be389e2e858e13f1dc09f309722e7e6ccebf4bb2bb829b2b1ad176401d2e86ae"
],
"tensor_total": [
"a0a594c0e05d41ef7ef4a8c262d56646d628f3b3f5c4f94b40e9cf1637d8ca03"
"e08db1676199c3179be8948bba78f43af63fecc404ef75b8d0b55ac59be72484"
]
}
},
Expand Down Expand Up @@ -2469,7 +2469,7 @@
"a345f0c30c4b1b80943b9c718c0002fc19981d0336a9a1ea0aca3b5966bf8462"
],
"tensor_unary": [
"4abceb6bf32d03e72bf0b7245447987b78885d079328ae6504e48bf8afa170f9"
"6cd5986e901ac953b121ba49c8b8d0eac1c53a1cf27cb8889b7f8f9a7e1b9a11"
]
}
},
Expand All @@ -2492,10 +2492,10 @@
"817e544211920e1216ff102393d66f3cb7a8def4a9ea05611872433b5ad8f87c"
],
"tensor_flatten_recursive": [
"3844bd92ee4238bec0b3bd2645eb4bd17d18a6d681a7ad7a8e21291282e33d14"
"be389e2e858e13f1dc09f309722e7e6ccebf4bb2bb829b2b1ad176401d2e86ae"
],
"tensor_total": [
"a0a594c0e05d41ef7ef4a8c262d56646d628f3b3f5c4f94b40e9cf1637d8ca03"
"e08db1676199c3179be8948bba78f43af63fecc404ef75b8d0b55ac59be72484"
]
}
},
Expand Down Expand Up @@ -2533,7 +2533,7 @@
"9829b048a1af1233bfeae6329573e79d83cd7acb01e548d3737011a625fbdc02"
],
"tensor_to_flat": [
"935bb2e66301b7e41ac4d999bb1e5bfee0b4b4ab8afd54bb73d0369f71c67898"
"d6c74fa5dc6c5d4d702b6f2eb1746dbe61271fd05f9bae972bdefa05d607cedc"
]
}
},
Expand Down Expand Up @@ -2573,11 +2573,11 @@
],
"disposition": "exempt",
"body_sha256": [
"d5ab1a467607560bfce18d36abd18fa5b25d7fe43179da3c305e29f379b027e7"
"f78ba93eb0b22a35b1c8a328a1f557b7096bf05bdbf38fe3259876cc7d1b8bb9"
],
"source_sha256": {
"builtin_tensor_shape": [
"d5ab1a467607560bfce18d36abd18fa5b25d7fe43179da3c305e29f379b027e7"
"f78ba93eb0b22a35b1c8a328a1f557b7096bf05bdbf38fe3259876cc7d1b8bb9"
]
}
},
Expand Down Expand Up @@ -2609,7 +2609,7 @@
"40100cc6cf4d92300cf1d3fbae23cb21adf4241bd01edecc9a683a07fb30bc01"
],
"tensor_to_flat": [
"935bb2e66301b7e41ac4d999bb1e5bfee0b4b4ab8afd54bb73d0369f71c67898"
"d6c74fa5dc6c5d4d702b6f2eb1746dbe61271fd05f9bae972bdefa05d607cedc"
]
}
},
Expand All @@ -2632,7 +2632,7 @@
"f38d9057e3c1e2a049e7cd4d4f38df6aace72af77d1a718b357b4f2a5bf4f54d"
],
"tensor_unary": [
"4abceb6bf32d03e72bf0b7245447987b78885d079328ae6504e48bf8afa170f9"
"6cd5986e901ac953b121ba49c8b8d0eac1c53a1cf27cb8889b7f8f9a7e1b9a11"
]
}
},
Expand Down Expand Up @@ -2667,10 +2667,10 @@
"196480c8b98743ea57d8bf3bc4bdf0b0fdc3ca70c56ef6cd1289342e15fcd544"
],
"tensor_flatten_recursive": [
"3844bd92ee4238bec0b3bd2645eb4bd17d18a6d681a7ad7a8e21291282e33d14"
"be389e2e858e13f1dc09f309722e7e6ccebf4bb2bb829b2b1ad176401d2e86ae"
],
"tensor_total": [
"a0a594c0e05d41ef7ef4a8c262d56646d628f3b3f5c4f94b40e9cf1637d8ca03"
"e08db1676199c3179be8948bba78f43af63fecc404ef75b8d0b55ac59be72484"
]
}
},
Expand Down Expand Up @@ -2698,11 +2698,11 @@
],
"disposition": "exempt",
"body_sha256": [
"aad531cea0cb998c5ab4bcfbc19ca755f163227267af03b6ad6f3ae66a8c751f"
"0de72aaa352907156e4244579f587cadc755557642f4e297485a5c4a019233eb"
],
"source_sha256": {
"builtin_tensor_zeros_like": [
"aad531cea0cb998c5ab4bcfbc19ca755f163227267af03b6ad6f3ae66a8c751f"
"0de72aaa352907156e4244579f587cadc755557642f4e297485a5c4a019233eb"
],
"make_buffer_like": [
"40100cc6cf4d92300cf1d3fbae23cb21adf4241bd01edecc9a683a07fb30bc01"
Expand Down
Loading