Skip to content
Closed
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 cactus-engine/cactus_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ CACTUS_FFI_EXPORT int cactus_graph_mean(cactus_graph_t graph, cactus_node_t x, i
CACTUS_FFI_EXPORT int cactus_graph_variance(cactus_graph_t graph, cactus_node_t x, int32_t axis, cactus_node_t* out);
CACTUS_FFI_EXPORT int cactus_graph_min(cactus_graph_t graph, cactus_node_t x, int32_t axis, cactus_node_t* out);
CACTUS_FFI_EXPORT int cactus_graph_max(cactus_graph_t graph, cactus_node_t x, int32_t axis, cactus_node_t* out);
CACTUS_FFI_EXPORT int cactus_graph_cumsum(cactus_graph_t graph, cactus_node_t x, int32_t axis, cactus_node_t* out);

CACTUS_FFI_EXPORT int cactus_graph_concat(
cactus_graph_t graph, cactus_node_t a, cactus_node_t b, int32_t axis,
Expand Down
11 changes: 11 additions & 0 deletions cactus-engine/src/graph_ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,17 @@ int cactus_graph_max(cactus_graph_t graph, cactus_node_t x, int32_t axis, cactus
}
}

int cactus_graph_cumsum(cactus_graph_t graph, cactus_node_t x, int32_t axis, cactus_node_t* out) {
if (!graph || !out) return fail_invalid("Invalid args to cactus_graph_cumsum");
try {
*out = static_cast<cactus_node_t>(as_graph(graph)->graph.cumsum(static_cast<size_t>(x), axis));
return 0;
} catch (const std::exception& e) {
last_error_message = e.what();
return -1;
}
}

int cactus_graph_concat(cactus_graph_t graph, cactus_node_t a, cactus_node_t b, int32_t axis, cactus_node_t* out) {
if (!graph || !out) {
last_error_message = "Invalid args to cactus_graph_concat";
Expand Down
3 changes: 2 additions & 1 deletion cactus-graph/cactus_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ enum class OpType {
ABS, POW, FLATTEN, VIEW,
MATMUL, TRANSPOSE, RESHAPE, SLICE, GATHER, EMBEDDING,
BILINEAR_INTERPOLATION,
SUM, MEAN, VARIANCE, MIN, MAX,
SUM, MEAN, VARIANCE, MIN, MAX, CUMSUM,
RMS_NORM, ROPE, ROPE_GPTJ, SOFTMAX,
ATTENTION, ATTENTION_INT8_HYBRID, REL_POS_BIAS,
CONV1D_CAUSAL, CONV1D_K3, CONV1D_K7S3, CONV1D,
Expand Down Expand Up @@ -497,6 +497,7 @@ class CactusGraph {
size_t variance(size_t input, int axis);
size_t min(size_t input, int axis);
size_t max(size_t input, int axis);
size_t cumsum(size_t input, int axis);
size_t softmax(size_t input, int axis = -1);
size_t topk(size_t input, size_t k);

Expand Down
17 changes: 17 additions & 0 deletions cactus-graph/src/builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,23 @@ size_t CactusGraph::variance(size_t input, int axis) { return reduction_op(OpTyp
size_t CactusGraph::min(size_t input, int axis) { return reduction_op(OpType::MIN, input, axis); }
size_t CactusGraph::max(size_t input, int axis) { return reduction_op(OpType::MAX, input, axis); }

size_t CactusGraph::cumsum(size_t input, int axis) {
const auto& input_buffer = get_output_buffer(input);
if (input_buffer.shape.empty()) {
throw std::runtime_error("Cumsum requires at least one dimension");
}

int actual_axis = axis;
if (actual_axis < 0) {
actual_axis += static_cast<int>(input_buffer.shape.size());
}
if (actual_axis < 0 || static_cast<size_t>(actual_axis) >= input_buffer.shape.size()) {
throw std::runtime_error("Invalid axis for cumsum operation");
}

return add_node(OpType::CUMSUM, {input}, input_buffer.shape, {.axis = actual_axis, .output_precision = input_buffer.precision});
}

size_t CactusGraph::rms_norm(size_t input, size_t weight, float epsilon) {
OpParams params{.epsilon = epsilon};
return add_node(OpType::RMS_NORM, {input, weight}, {}, params);
Expand Down
47 changes: 38 additions & 9 deletions cactus-graph/src/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ static bool init_dispatch() {
dispatch_flat[static_cast<int>(OpType::VARIANCE)] = compute_reduce_node;
dispatch_flat[static_cast<int>(OpType::MIN)] = compute_reduce_node;
dispatch_flat[static_cast<int>(OpType::MAX)] = compute_reduce_node;
dispatch_flat[static_cast<int>(OpType::CUMSUM)] = compute_reduce_node;
dispatch_flat[static_cast<int>(OpType::FLATTEN)] = compute_reshape_node;
dispatch_flat[static_cast<int>(OpType::VIEW)] = compute_reshape_node;
dispatch_flat[static_cast<int>(OpType::RESHAPE)] = compute_reshape_node;
Expand Down Expand Up @@ -200,7 +201,7 @@ static const char* op_type_names[] = {
"ABS", "POW", "FLATTEN", "VIEW",
"MATMUL", "TRANSPOSE", "RESHAPE", "SLICE", "GATHER", "EMBEDDING",
"BILINEAR_INTERPOLATION",
"SUM", "MEAN", "VARIANCE", "MIN", "MAX",
"SUM", "MEAN", "VARIANCE", "MIN", "MAX", "CUMSUM",
"RMS_NORM", "ROPE", "ROPE_GPTJ", "SOFTMAX",
"ATTENTION", "ATTENTION_INT8_HYBRID", "REL_POS_BIAS",
"CONV1D_CAUSAL", "CONV1D_K3", "CONV1D_K7S3", "CONV1D",
Expand Down Expand Up @@ -297,11 +298,20 @@ void CactusGraph::execute(const std::string& profile_file) {
BufferPool& pool = buffer_pool_;
const size_t n = nodes_.size();

auto get_env_int = [](const char* name, int fallback) -> int {
const char* val = std::getenv(name);
return val ? std::atoi(val) : fallback;
};

bool trace_execution = get_env_int("CACTUS_TRACE_EXECUTE", 0) != 0;
bool need_debug = !profile_file.empty();
if (!need_debug) {
static const bool env_debug = check_debug_env();
need_debug = env_debug;
}
if (trace_execution) {
need_debug = true;
}

if (!need_debug) {
for (size_t i = 0; i < n; ++i) {
Expand Down Expand Up @@ -331,11 +341,6 @@ void CactusGraph::execute(const std::string& profile_file) {
}
}

auto get_env_int = [](const char* name, int fallback) -> int {
const char* val = std::getenv(name);
return val ? std::atoi(val) : fallback;
};

auto get_env_str = [](const char* name) -> std::string {
const char* val = std::getenv(name);
return val ? std::string(val) : std::string();
Expand Down Expand Up @@ -390,11 +395,28 @@ void CactusGraph::execute(const std::string& profile_file) {
for (size_t node_idx = 0; node_idx < n; ++node_idx) {
auto& node = nodes_[node_idx];

if (node->op_type != OpType::INPUT) {
node->output_buffer.allocate_from_pool(pool);
if (node->op_type == OpType::INPUT) {
continue;
}

if (enable_profiling && node->op_type != OpType::INPUT) {
node->output_buffer.allocate_from_pool(pool);

if (trace_execution) {
std::cerr << "[cactus:execute] begin idx=" << node_idx
<< " id=" << node->id
<< " op=" << get_op_name(node->op_type)
<< " shape=[";
for (size_t dim_idx = 0; dim_idx < node->output_buffer.shape.size(); ++dim_idx) {
if (dim_idx > 0) std::cerr << ",";
std::cerr << node->output_buffer.shape[dim_idx];
}
std::cerr << "]" << std::endl;
}

if (node->op_type == OpType::KV_CACHE_STATE || node->op_type == OpType::CONV_CACHE_STATE) {
dispatch_node(*node, nodes_, node_index_map_);
populated_node_ids_.insert(node->id);
} else if (enable_profiling) {
auto start = std::chrono::high_resolution_clock::now();
dispatch_node(*node, nodes_, node_index_map_);
if (node->op_type == OpType::PERSISTENT) {
Expand All @@ -419,6 +441,13 @@ void CactusGraph::execute(const std::string& profile_file) {
populated_node_ids_.insert(node->id);
}
}

if (trace_execution) {
std::cerr << "[cactus:execute] done idx=" << node_idx
<< " id=" << node->id
<< " op=" << get_op_name(node->op_type)
<< std::endl;
}
}

std::unique_ptr<std::ofstream> capture_file_stream;
Expand Down
22 changes: 22 additions & 0 deletions cactus-graph/src/ops_math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,28 @@ void compute_reduce_node(GraphNode& node, const std::vector<std::unique_ptr<Grap
throw std::runtime_error("Reduction operations only support FP16 precision");
}

if (node.op_type == OpType::CUMSUM) {
if (axis < 0 || static_cast<size_t>(axis) >= input_buffer.shape.size()) {
throw std::runtime_error("Cumsum axis out of range");
}

auto dims = AxisDims::from_shape(input_buffer.shape, static_cast<size_t>(axis));
const __fp16* input = input_buffer.data_as<__fp16>();
__fp16* output = node.output_buffer.data_as<__fp16>();

for (size_t outer = 0; outer < dims.outer; ++outer) {
for (size_t inner = 0; inner < dims.inner; ++inner) {
float running = 0.0f;
for (size_t axis_index = 0; axis_index < dims.axis_size; ++axis_index) {
size_t flat_index = ((outer * dims.axis_size) + axis_index) * dims.inner + inner;
running += static_cast<float>(input[flat_index]);
output[flat_index] = static_cast<__fp16>(running);
}
}
}
return;
}

if (axis == -1) {
switch (node.op_type) {
case OpType::SUM: {
Expand Down
1 change: 1 addition & 0 deletions cactus-graph/src/param_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ const Schema& op_schema(OpType op_type) {
{OpType::VARIANCE, {{ParamField::Axis, FieldPersistence::Persistent}}},
{OpType::MIN, {{ParamField::Axis, FieldPersistence::Persistent}}},
{OpType::MAX, {{ParamField::Axis, FieldPersistence::Persistent}}},
{OpType::CUMSUM, {{ParamField::Axis, FieldPersistence::Persistent}}},
{OpType::INDEX, {{ParamField::Axis, FieldPersistence::Persistent}, {ParamField::IndexValue, FieldPersistence::Persistent}}},
{OpType::CONCAT, {{ParamField::Axis, FieldPersistence::Persistent}}},
{OpType::CAT, {{ParamField::Axis, FieldPersistence::Persistent}}},
Expand Down
Loading
Loading