← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
ckernel_checked_latent_loop.h File Reference

Go to the source code of this file.

Data Structures

struct  ck_checked_latent_loop

Functions

int ck_run_checked_latent_loop (const ck_checked_latent_loop *request)

Function Documentation

◆ ck_run_checked_latent_loop()

int ck_run_checked_latent_loop ( const ck_checked_latent_loop * request)

Definition at line 21 of file checked_latent_loop.c.

21 {
22 if(!r || r->policy_version!=1 || !r->embed || !r->prefix || !r->aggregate || !r->flow ||
23 ck_validate_guided_flow_request(r->flow) || !r->width || r->width>8192 ||
24 !r->latent_width || r->latent_width>4096 || !r->capacity || r->capacity>512 ||
25 !r->prompt_rows || r->prompt_rows>r->capacity || !r->max_frames || r->max_frames>1024 ||
26 !r->min_frames || r->min_frames>r->max_frames ||
27 r->flow->latent_elements!=r->latent_width || r->flow->history_elements!=r->latent_width ||
28 r->flow->condition_elements!=r->width || r->flow->stream>UINT64_MAX-r->max_frames ||
29 r->staging_elements<r->max_frames*r->latent_width || r->staging_elements>SIZE_MAX/4 ||
30 r->output_elements<r->max_frames*r->latent_width || r->output_elements>SIZE_MAX/4) return -1;
31 const ck_guided_flow_request *f=r->flow;
32 const void *ptr[]={r->backbone_arena,r->aggregator_arena,f->model.arena,f->update.arena,
33 f->condition,f->history,f->times,f->workspace,f->output,r->staging,r->output,r->valid_frames,r,f};
34 size_t sizes[]={r->backbone_bytes,r->aggregator_bytes,f->model.arena_bytes,f->update.arena_bytes,
35 r->width*4,r->latent_width*4,(f->steps+1)*4,f->workspace_elements*4,f->output_elements*4,
36 r->staging_elements*4,r->output_elements*4,sizeof(size_t),sizeof(*r),sizeof(*f)};
37 for(size_t i=0;i<14;++i) {
38 if(!region(ptr[i],sizes[i],i==11?_Alignof(size_t):i==12?_Alignof(ck_checked_latent_loop):i==13?_Alignof(ck_guided_flow_request):4)) return -1;
39 for(size_t j=0;j<i;++j) if(overlap(ptr[i],sizes[i],ptr[j],sizes[j])) return -1;
40 }
41 size_t bo[]={r->request_rows,r->embeddings,r->hidden,r->stop_logits,
42 r->decode_input,r->decode_hidden,r->decode_stop_logits,r->decode_position};
43 size_t bn[]={1,r->capacity*r->width,r->capacity*r->width,r->capacity*2,r->width,r->width,2,1};
44 size_t ao[]={r->aggregate_latent,r->aggregate_class_id,r->aggregate_output};
45 size_t an[]={r->latent_width,1,r->width};
46 if(!slices(r->backbone_bytes,bo,bn,r->decode?8:4) || !slices(r->aggregator_bytes,ao,an,3)) return -1;
47 *r->valid_frames=0;
49 memset((float *)flow.history,0,r->latent_width*4);
50 int32_t *rows=(int32_t *)(r->backbone_arena+r->request_rows);
51 float *emb=(float *)(r->backbone_arena+r->embeddings);
52 float *hidden=(float *)(r->backbone_arena+r->hidden);
53 float *stop=(float *)(r->backbone_arena+r->stop_logits);
54 *rows=(int32_t)r->prompt_rows;
55 if(flow.should_cancel && flow.should_cancel(flow.cancel_context)) return -7;
56 if(r->embed(r->backbone_arena,r->backbone_bytes)) return -2;
57 for(size_t step=0;step<r->max_frames;++step) {
58 if(flow.should_cancel && flow.should_cancel(flow.cancel_context)) return -7;
59 if(*rows<1 || (size_t)*rows>r->capacity) return -1;
60 size_t row=(size_t)*rows-1;
61 if(step && r->decode) {
62 memcpy(r->backbone_arena+r->decode_input,emb+row*r->width,r->width*4);
63 *(int32_t *)(r->backbone_arena+r->decode_position)=(int32_t)row;
64 if(r->decode(r->backbone_arena,r->backbone_bytes)) return -2;
65 hidden=(float *)(r->backbone_arena+r->decode_hidden);
66 stop=(float *)(r->backbone_arena+r->decode_stop_logits);row=0;
67 } else {
68 if(r->prefix(r->backbone_arena,r->backbone_bytes)) return -2;
69 if(*rows<1 || (size_t)*rows>r->capacity) return -1;
70 row=(size_t)*rows-1;
71 }
72 if(!finite_values(hidden+row*r->width,r->width) || !finite_values(stop+row*2,2)) return -3;
73 memcpy((float *)flow.condition,hidden+row*r->width,r->width*4);
74 flow.stream=f->stream+step;
75 int status=ck_run_checked_guided_flow(&flow);
76 if(status) return status;
77 memcpy(r->staging+step*r->latent_width,flow.output,r->latent_width*4);
78 if(step+1>=r->min_frames && stop[row*2+1]>stop[row*2]) {
79 if(flow.should_cancel && flow.should_cancel(flow.cancel_context)) return -7;
80 memcpy(r->output,r->staging,(step+1)*r->latent_width*4);
81 *r->valid_frames=step+1;return 0;
82 }
83 if(step+1==r->max_frames || (size_t)*rows>=r->capacity) return 2;
84 memcpy(r->aggregator_arena+r->aggregate_latent,flow.output,r->latent_width*4);
85 *(int32_t *)(r->aggregator_arena+r->aggregate_class_id)=0;
86 if(flow.should_cancel && flow.should_cancel(flow.cancel_context)) return -7;
87 if(r->aggregate(r->aggregator_arena,r->aggregator_bytes)) return -2;
88 float *next=(float *)(r->aggregator_arena+r->aggregate_output);
89 if(!finite_values(next,r->width)) return -3;
90 memcpy(emb+(size_t)*rows*r->width,next,r->width*4);
91 memcpy((float *)flow.history,flow.output,r->latent_width*4);
92 ++*rows;
93 }
94 return 2;
95}
static int slices(size_t bytes, const size_t *offsets, const size_t *counts, size_t n)
static int finite_values(const float *p, size_t n)
static int slices(size_t bytes, const size_t *off, const size_t *len, size_t count)
static int region(const void *p, size_t n, size_t align)
static int finite_values(const float *p, size_t n)
static int overlap(const void *a, size_t na, const void *b, size_t nb)
int ck_validate_guided_flow_request(const ck_guided_flow_request *request)
int ck_run_checked_guided_flow(const ck_guided_flow_request *request)

References ck_checked_latent_loop::aggregate, ck_checked_latent_loop::aggregate_class_id, ck_checked_latent_loop::aggregate_latent, ck_checked_latent_loop::aggregate_output, ck_checked_latent_loop::aggregator_arena, ck_checked_latent_loop::aggregator_bytes, ck_flow_model::arena, ck_flow_update::arena, ck_flow_model::arena_bytes, ck_flow_update::arena_bytes, ck_checked_latent_loop::backbone_arena, ck_checked_latent_loop::backbone_bytes, ck_guided_flow_request::cancel_context, ck_checked_latent_loop::capacity, ck_run_checked_guided_flow(), ck_validate_guided_flow_request(), ck_guided_flow_request::condition, ck_guided_flow_request::condition_elements, ck_checked_latent_loop::decode, ck_checked_latent_loop::decode_hidden, ck_checked_latent_loop::decode_input, ck_checked_latent_loop::decode_position, ck_checked_latent_loop::decode_stop_logits, ck_checked_latent_loop::embed, ck_checked_latent_loop::embeddings, finite_values(), finite_values(), ck_checked_latent_loop::flow, ck_checked_latent_loop::hidden, ck_guided_flow_request::history, ck_guided_flow_request::history_elements, ck_guided_flow_request::latent_elements, ck_checked_latent_loop::latent_width, ck_checked_latent_loop::max_frames, ck_checked_latent_loop::min_frames, ck_guided_flow_request::model, ck_checked_latent_loop::output, ck_guided_flow_request::output, ck_checked_latent_loop::output_elements, ck_guided_flow_request::output_elements, overlap(), ck_checked_latent_loop::policy_version, ck_checked_latent_loop::prefix, ck_checked_latent_loop::prompt_rows, region(), ck_checked_latent_loop::request_rows, ck_guided_flow_request::should_cancel, slices(), slices(), ck_checked_latent_loop::staging, ck_checked_latent_loop::staging_elements, ck_guided_flow_request::steps, ck_checked_latent_loop::stop_logits, ck_guided_flow_request::stream, ck_guided_flow_request::times, ck_guided_flow_request::update, ck_checked_latent_loop::valid_frames, ck_checked_latent_loop::width, ck_guided_flow_request::workspace, and ck_guided_flow_request::workspace_elements.