ember.cpp 0.1.0
C++ inference engine for Llama-family and paperkiln models (CPU, CUDA, WebAssembly)
Loading...
Searching...
No Matches
ember_ir::rec::Program Struct Reference
Collaboration diagram for ember_ir::rec::Program:
Collaboration graph

Public Member Functions

float eval (const E &e) const
 
void assign (const Assign &a, float *out) const
 

Public Attributes

std::vector< std::string > index_names
 
std::vector< int > index_sizes
 
std::map< std::string, int > index_of
 
std::vector< Tensor > tensors
 
std::map< std::string, int > tensor_of
 
std::vector< int > inputs
 
std::vector< int > weights
 
std::vector< int > states
 
std::vector< int > lets
 
int out = -1
 
std::vector< Assign > init
 
std::vector< Assign > let
 
std::vector< Assign > update
 
Assign readout
 
std::vector< int > init_of_state
 
std::vector< Recurrence::StateTensor > state_info
 
std::vector< Recurrence::WeightDecl > weight_info
 
std::vector< size_t > in_widths
 
size_t out_width = 0
 
bool exact = true
 
std::vector< int > iv
 
std::vector< const float * > data
 
std::vector< std::vector< float > > let_buf
 
std::vector< std::vector< float > > new_state
 

Detailed Description

Definition at line 153 of file graph_ir_recurrence.cpp.

Member Function Documentation

◆ assign()

void ember_ir::rec::Program::assign ( const Assign &  a,
float *  out 
) const

Definition at line 523 of file graph_ir_recurrence.cpp.

523 {
524 const size_t k = a.lhs.size();
525 for (size_t d = 0; d < k; ++d) iv[a.lhs[d]] = 0;
526 const size_t n = tensors[a.tensor].numel;
527 for (size_t flat = 0; flat < n; ++flat) {
528 out[flat] = eval(a.body);
529 for (size_t d = k; d-- > 0;) {
530 if (++iv[a.lhs[d]] < index_sizes[a.lhs[d]]) break;
531 iv[a.lhs[d]] = 0;
532 }
533 }
534}
int n
std::vector< E > a
size_t j
float eval(const E &e) const
std::vector< Tensor > tensors

References a, eval(), index_sizes, iv, j, k, n, out, and tensors.

◆ eval()

float ember_ir::rec::Program::eval ( const E &  e) const

Definition at line 472 of file graph_ir_recurrence.cpp.

472 {
473 switch (e.k) {
474 case K::Num: return e.v;
475 case K::Idx: return static_cast<float>(iv[e.id]);
476 case K::Read: {
477 const Tensor& t = tensors[e.id];
478 size_t flat = 0;
479 for (size_t d = 0; d < e.ix.size(); ++d) {
480 const long c = (e.ix[d].index >= 0 ? iv[e.ix[d].index] : 0) + e.ix[d].off;
481 if (c < 0 || c >= t.sizes[d]) return 0.0f; // outside the shape: +0
482 flat += static_cast<size_t>(c) * t.strides[d];
483 }
484 return data[e.id][flat];
485 }
486 case K::Neg: return -eval(e.a[0]);
487 case K::Add: { const float x = eval(e.a[0]); const float y = eval(e.a[1]); return x + y; }
488 case K::Sub: { const float x = eval(e.a[0]); const float y = eval(e.a[1]); return x - y; }
489 case K::Mul: { const float x = eval(e.a[0]); const float y = eval(e.a[1]); return x * y; }
490 case K::Div: { const float x = eval(e.a[0]); const float y = eval(e.a[1]); return x / y; }
491 case K::Pow: { const float x = eval(e.a[0]); const float y = eval(e.a[1]); return std::pow(x, y); }
492 case K::Exp: return std::exp(eval(e.a[0]));
493 case K::Log: return std::log(eval(e.a[0]));
494 case K::Sqrt: return std::sqrt(eval(e.a[0]));
495 case K::Tanh: return std::tanh(eval(e.a[0]));
496 case K::Erf: return std::erf(eval(e.a[0]));
497 case K::Abs: return std::fabs(eval(e.a[0]));
498 case K::Sigmoid: {
499 const float x = eval(e.a[0]);
500 const float ex = std::exp(-x);
501 const float den = 1.0f + ex;
502 return 1.0f / den;
503 }
504 case K::Relu: { const float x = eval(e.a[0]); return x > 0.0f ? x : 0.0f; }
505 case K::Max: { const float x = eval(e.a[0]); const float y = eval(e.a[1]); return x >= y ? x : y; }
506 case K::Min: { const float x = eval(e.a[0]); const float y = eval(e.a[1]); return x <= y ? x : y; }
507 case K::Where: return eval(e.a[0]) > 0.0f ? eval(e.a[1]) : eval(e.a[2]);
508 case K::Sum: {
509 float acc = 0.0f; // +0
510 const int n = index_sizes[e.id];
511 for (int j = 0; j < n; ++j) {
512 iv[e.id] = j;
513 const float x = eval(e.a[0]);
514 acc = acc + x;
515 }
516 return acc;
517 }
518 }
519 return 0.0f;
520}
float c
std::vector< const float * > data

References c, data, eval(), index_sizes, iv, j, n, and tensors.

Referenced by assign(), and eval().

Member Data Documentation

◆ data

std::vector<const float*> ember_ir::rec::Program::data
mutable

◆ exact

bool ember_ir::rec::Program::exact = true

Definition at line 168 of file graph_ir_recurrence.cpp.

◆ in_widths

std::vector<size_t> ember_ir::rec::Program::in_widths

Definition at line 166 of file graph_ir_recurrence.cpp.

◆ index_names

std::vector<std::string> ember_ir::rec::Program::index_names

Definition at line 154 of file graph_ir_recurrence.cpp.

◆ index_of

std::map<std::string, int> ember_ir::rec::Program::index_of

Definition at line 156 of file graph_ir_recurrence.cpp.

◆ index_sizes

std::vector<int> ember_ir::rec::Program::index_sizes

Definition at line 155 of file graph_ir_recurrence.cpp.

Referenced by assign(), and eval().

◆ init

std::vector<Assign> ember_ir::rec::Program::init

Definition at line 161 of file graph_ir_recurrence.cpp.

◆ init_of_state

std::vector<int> ember_ir::rec::Program::init_of_state

Definition at line 163 of file graph_ir_recurrence.cpp.

◆ inputs

std::vector<int> ember_ir::rec::Program::inputs

Definition at line 159 of file graph_ir_recurrence.cpp.

◆ iv

std::vector<int> ember_ir::rec::Program::iv
mutable

Definition at line 170 of file graph_ir_recurrence.cpp.

Referenced by assign(), and eval().

◆ let

std::vector<Assign> ember_ir::rec::Program::let

Definition at line 161 of file graph_ir_recurrence.cpp.

◆ let_buf

std::vector<std::vector<float> > ember_ir::rec::Program::let_buf
mutable

Definition at line 172 of file graph_ir_recurrence.cpp.

◆ lets

std::vector<int> ember_ir::rec::Program::lets

Definition at line 159 of file graph_ir_recurrence.cpp.

◆ new_state

std::vector<std::vector<float> > ember_ir::rec::Program::new_state

Definition at line 172 of file graph_ir_recurrence.cpp.

◆ out

int ember_ir::rec::Program::out = -1

Definition at line 160 of file graph_ir_recurrence.cpp.

Referenced by assign().

◆ out_width

size_t ember_ir::rec::Program::out_width = 0

Definition at line 167 of file graph_ir_recurrence.cpp.

◆ readout

Assign ember_ir::rec::Program::readout

Definition at line 162 of file graph_ir_recurrence.cpp.

◆ state_info

std::vector<Recurrence::StateTensor> ember_ir::rec::Program::state_info

Definition at line 164 of file graph_ir_recurrence.cpp.

Referenced by ember_ir::rec::Recurrence::state().

◆ states

std::vector<int> ember_ir::rec::Program::states

Definition at line 159 of file graph_ir_recurrence.cpp.

◆ tensor_of

std::map<std::string, int> ember_ir::rec::Program::tensor_of

Definition at line 158 of file graph_ir_recurrence.cpp.

◆ tensors

std::vector<Tensor> ember_ir::rec::Program::tensors

Definition at line 157 of file graph_ir_recurrence.cpp.

Referenced by assign(), and eval().

◆ update

std::vector<Assign> ember_ir::rec::Program::update

Definition at line 161 of file graph_ir_recurrence.cpp.

◆ weight_info

std::vector<Recurrence::WeightDecl> ember_ir::rec::Program::weight_info

Definition at line 165 of file graph_ir_recurrence.cpp.

◆ weights

std::vector<int> ember_ir::rec::Program::weights

Definition at line 159 of file graph_ir_recurrence.cpp.


The documentation for this struct was generated from the following file: