Skip to content

Add a preprocessor in Turbo#548

Open
zzlin237 wants to merge 60 commits into
alibaba:mainfrom
zzlin237:refactor/turbo_preprocessor
Open

Add a preprocessor in Turbo#548
zzlin237 wants to merge 60 commits into
alibaba:mainfrom
zzlin237:refactor/turbo_preprocessor

Conversation

@zzlin237

@zzlin237 zzlin237 commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

Description: A preprocessor is an optional internal component of the Turbo quantizer, primarily responsible for preprocessing data prior to retrieval. Its main functionalities include rotation and dimensionality reduction. Currently, only FHT rotation has been implemented, with support for OPQ rotation, dimensionality reduction, and other features planned for future releases.
Benefits: A module developed for Turbo

@zzlin237 zzlin237 requested a review from zhourrr as a code owner July 3, 2026 03:20

#pragma once

#include <turbo/quantizer/quantizer.h>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

framework的头文件不应该依赖turbo的头文件,zvec::turbo::Quantizer应该使用前向声明来规避这个include

@iaojnh iaojnh Jul 15, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个问题在PR #546 已经修改了,记得解冲突

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已解决冲突

int serialize(std::string *out) const override;
int deserialize(const void *data, size_t len) override;

//! Rotator type tag (kFht = 1).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kFht不是0吗?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

void FhtRotator::apply_inverse(const float *in, float *out) const {
const size_t dim = static_cast<size_t>(in_dim_);
// Copy input into working buffer
std::vector<float> data(in, in + dim);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

既然最后都要memcpy到out上,为何不一开始就在out上操作?这个data有点冗余。

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apply改了,此处忘记改了,已解决

Comment thread src/turbo/turbo.cc
#include "scalar/rotate/fht/fht.h"

#if defined(__SSE2__)
#include "sse/rotate/fht/fht.h"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把 defined 判断放每个simd实现的内部头文件?这样include方比较简单,虽然当前pr没差别,只有一处include。

Comment thread src/turbo/turbo.cc
#if defined(__AVX512F__)
if (zvec::ailego::internal::CpuFeatures::static_flags_.AVX512F &&
zvec::ailego::internal::CpuFeatures::static_flags_.AVX512DQ &&
(cpu_arch_type == CpuArchType::kAuto ||

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加个cpu arch type match的helper?

Comment thread src/turbo/turbo.cc
switch (rotate_type) {
case RotateType::kFht: {
RotatorKernels k;
// Default: scalar

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把scalar定义放最后吧,实现和使用间隔比较远

Comment thread src/turbo/turbo.cc
zvec::ailego::internal::CpuFeatures::static_flags_.AVX512DQ &&
(cpu_arch_type == CpuArchType::kAuto ||
cpu_arch_type == CpuArchType::kAVX512)) {
k.rotate = avx512::fht_rotate_avx512;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以用aggregation初始化,其他类似
return {avx512::fht_rotate_avx512, avx512::fht_unrotate_avx512};

Comment thread src/turbo/turbo.cc
#if defined(__AVX2__)
#include "avx2/rotate/fht/fht.h"
#endif
#if defined(__AVX512F__)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

turbo.cc编译时没开simd开关,这些if define实际不起作用?

static constexpr FhtPrimitives kPrim = {
fht_flip_sign_avx2, fht_inplace_avx2, fht_kacs_walk_avx2,
fht_inv_kacs_walk_avx2, fht_vec_rescale_avx2};
fht_rotate_impl(out, in_dim, ctx, kPrim);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in参数完全没用到,要求输入已经在out了,这个接口有问题吧?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants