MCPcopy Create free account
hub / github.com/cactus-compute/cactus / compute_sample_node

Function compute_sample_node

cactus/graph/graph_ops_sample.cpp:8–41  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6#include <stdexcept>
7
8void compute_sample_node(GraphNode& node, const std::vector<std::unique_ptr<GraphNode>>& nodes, const std::unordered_map<size_t, size_t>& node_index_map) {
9 const auto& logits_buffer = get_input(node, 0, nodes, node_index_map);
10
11 float temperature = node.params.temperature;
12 float top_p = node.params.top_p;
13 float min_p = node.params.min_p;
14 float repetition_penalty = node.params.repetition_penalty;
15 size_t top_k = node.params.top_k;
16 size_t random_seed = node.params.random_seed;
17
18 const float* bias_values = node.params.bias_values.empty() ? nullptr : node.params.bias_values.data();
19 const uint32_t* bias_indices = node.params.bias_indices.empty() ? nullptr : node.params.bias_indices.data();
20 size_t bias_count = node.params.bias_values.size();
21
22 if (logits_buffer.shape.size() != 2) {
23 throw std::runtime_error("Sample expects 2D logits tensor [seq_len, vocab_size]");
24 }
25
26 size_t seq_len = logits_buffer.shape[0];
27 size_t vocab_size = logits_buffer.shape[1];
28 size_t last_token_offset = (seq_len - 1) * vocab_size;
29
30 if (logits_buffer.precision == Precision::FP16) {
31 const __fp16* logits_fp16 = logits_buffer.data_as<__fp16>();
32 cactus_sample_f16_ex(logits_fp16 + last_token_offset, node.output_buffer.data_as<uint32_t>(),
33 vocab_size, temperature, top_p, min_p, repetition_penalty, top_k, random_seed,
34 bias_values, bias_indices, bias_count);
35 } else {
36 const float* logits_fp32 = logits_buffer.data_as<float>();
37 cactus_sample_f32_ex(logits_fp32 + last_token_offset, node.output_buffer.data_as<uint32_t>(),
38 vocab_size, temperature, top_p, min_p, repetition_penalty, top_k, random_seed,
39 bias_values, bias_indices, bias_count);
40 }
41}
42
43void compute_topk_node(GraphNode& node, const std::vector<std::unique_ptr<GraphNode>>& nodes, const std::unordered_map<size_t, size_t>& node_index_map) {
44 const auto& input_buffer = get_input(node, 0, nodes, node_index_map);

Callers

nothing calls this directly

Calls 4

cactus_sample_f16_exFunction · 0.85
cactus_sample_f32_exFunction · 0.85
dataMethod · 0.80
sizeMethod · 0.80

Tested by

no test coverage detected