use super::prelude::*;
impl NodeCodegen for onnx_ir::topk::TopKNode {
fn inputs(&self) -> &[Argument] {
&self.inputs
}
fn outputs(&self) -> &[Argument] {
&self.outputs
}
fn forward(&self, scope: &mut ScopeAtPosition<'_>) -> TokenStream {
let values_output = arg_to_ident(&self.outputs[0]);
let indices_output = arg_to_ident(&self.outputs[1]);
let axis = self.config.axis.to_tokens();
let (prelude, k) = match &self.config.k {
onnx_ir::topk::TopKInput::Static(k_value) => (TokenStream::new(), k_value.to_tokens()),
onnx_ir::topk::TopKInput::Runtime(r) => {
let arg = &self.inputs[r.input_index];
let prelude = match &arg.ty {
ArgType::ScalarNative(_) => {
let ident = arg_to_ident(arg);
quote! { let __topk_k: usize = #ident as usize; }
}
ArgType::ScalarTensor(_) | ArgType::Tensor(_) => {
let tensor = scope.arg(arg);
quote! {
let __topk_k: usize = {
let __data = #tensor.to_data().convert::<i64>();
__data.as_slice::<i64>().unwrap()[0] as usize
};
}
}
other => panic!("TopK k must be a scalar or rank-1 tensor, got {other:?}"),
};
(prelude, quote! { __topk_k })
}
};
let input = scope.arg(self.inputs.first().unwrap());
if prelude.is_empty() {
quote! {
let (#values_output, #indices_output) = #input.topk_with_indices(#k, #axis);
}
} else {
quote! {
let (#values_output, #indices_output) = {
#prelude
#input.topk_with_indices(#k, #axis)
};
}
}
}
}
#[cfg(test)]
mod tests {
use super::super::test_helpers::*;
use burn::tensor::DType;
use insta::assert_snapshot;
use onnx_ir::topk::{TopKConfig, TopKInput, TopKNodeBuilder};
#[test]
fn test_top_k() {
let config = TopKConfig::new(1, TopKInput::Static(5));
let node = TopKNodeBuilder::new("topk1")
.input_tensor("input", 2, DType::F32)
.output_tensor("values", 2, DType::F32)
.output_tensor("indices", 2, DType::I64)
.config(config)
.build();
let code = codegen_forward_default(&node);
assert_snapshot!(code, @r"
pub fn forward(&self, input: Tensor<B, 2>) -> (Tensor<B, 2>, Tensor<B, 2, Int>) {
let (values, indices) = input.topk_with_indices(5, 1);
(values, indices)
}
");
}
#[test]
fn test_top_k_runtime_k() {
let config = TopKConfig::new(
1,
TopKInput::Runtime(onnx_ir::ir::RuntimeInputRef::new("k".to_string(), 1)),
);
let node = TopKNodeBuilder::new("topk_rt")
.input_tensor("input", 2, DType::F32)
.input_tensor("k", 1, DType::I64)
.output_tensor("values", 2, DType::F32)
.output_tensor("indices", 2, DType::I64)
.config(config)
.build();
let code = codegen_forward_default(&node);
assert_snapshot!(code, @r"
pub fn forward(
&self,
input: Tensor<B, 2>,
k: Tensor<B, 1, Int>,
) -> (Tensor<B, 2>, Tensor<B, 2, Int>) {
let (values, indices) = {
let __topk_k: usize = {
let __data = k.to_data().convert::<i64>();
__data.as_slice::<i64>().unwrap()[0] as usize
};
input.topk_with_indices(__topk_k, 1)
};
(values, indices)
}
");
}
}