Skip to main content

GNES is Generic Neural Elastic Search, a cloud-native semantic search system based on deep neural network.

Project description

GNES Generic Neural Elastic Search, logo made by Han Xiao

PyPI Docker Cloud Build Status Documentation Status PyPI - License

HighlightsOverviewInstallGetting StartedDocumentationTutorialContributingRelease NotesBlog

What is it

GNES [jee-nes] is Generic Neural Elastic Search, a cloud-native semantic search system based on deep neural network.

GNES enables large-scale index and semantic search for text-to-text, image-to-image, video-to-video and any-to-any content form.


💭 To know more about the key tenets of GNES, read this blog post



Cloud-Native & Elastic





GNES is all-in-microservice! Encoder, indexer, preprocessor and router are all running in their own containers. They communicate via versioned APIs and collaborate under the orchestration of Docker Swarm/Kubernetes etc. Scaling, load-balancing, automated recovering, they come off-the-shelf in GNES. How long would it take to deploy a change that involves just switching a layer in VGG? In GNES, this is just one line change in a YAML file. We abstract the encoding and indexing logic to a YAML config, so that you can change or stack encoders and indexers without even touching the codebase. Taking advantage of fast-evolving AI/ML/NLP/CV communities, we learn from best-of-breed deep learning models and plug them into GNES, making sure you always enjoy the state-of-the-art performance.


Generic & Universal


Model as Plugin


Best Practice

Searching for texts, image or even short-videos? Using Python/C/Java/Go/HTTP as the client? Doesn't matter which content form you have or which language do you use, GNES can handle them all. When built-in models do not meet your requirments, simply build your own with one Python file and one YAML file. No need to rebuilt GNES framework, as your models will be loaded as plugins and directly rollout online. We love to learn the best practice from the community, helping our GNES to achieve the next level of availability, resiliency, performance, and durability. If you have any ideas or suggestions, feel free to contribute.


component overview

Install GNES

There are two ways to get GNES, either as a Docker image or as a PyPi package. For cloud users, we highly recommend using GNES via Docker.

Run GNES as a Docker Container

docker run gnes/gnes:latest-alpine

This command downloads the latest GNES image (based on Alpine Linux) and runs it in a container. When the container runs, it prints an informational message and exits.

💡 Choose the right GNES image

Besides the alpine image optimized for the space, we also provide Buster (Debian 10.0) and Ubuntu 18.04-based images. The table below summarizes all available GNES tags. One can fill in {ver} with latest, stable or v0..xx. latest refers to the latest master of this repository, which may not be stable. We recommend you to use an official release by changing the latest to a version number, say v0.0.24, or simply using stable for the last release, e.g. gnes:stable-ubuntu

Tag Size and layers Description
{ver}-alpine based on Alpine Linux;
no deep learning libraries;
extremely lightweight and portable, enables fast scaling on even edge devices.
{ver}-buster based on Debian 10.0;
no deep learning libraries;
recommended for building or extending a GNES-Hub image.
{ver}-ubuntu18 based on Ubuntu 18.04;
no deep learning libraries.
{ver}-full based on Ubuntu 16.04;
python-3.6.8, cuda-10.0, tf1.14, pytorch1.1, faiss, multiple pretrained models;
heavy but self-contained, useful in testing GNES end-to-endly.

Install GNES via pip

You can also install GNES as a Python package via:

pip install gnes

Note that this will only install a "barebone" version of GNES, consists of the minimal dependencies for running GNES, i.e. no third-party pretrained models, deep learning/NLP/CV packages are installed. We make this setup as the default installation behavior, as in GNES, models serve as plugins, and a model interested to NLP engineers may not be interested to CV engineers.

🚸 Tensorflow, Pytorch and torchvision are not part of GNES installation. Depending on your model, you may have to install them in advance.

Either way, if you end up reading the following message after $ gnes or $ docker run gnes/gnes, then you are ready to go!

success installation of GNES

Getting Started

🐣 Preliminaries

Before we start, let me first introduce two important concepts serving as the backbone of GNES: microservice and runtime.


For machine learning engineers and data scientists who are not familiar with the concept of cloud-native and microservice, one can picture a microservice as an app (on your smartphone). Each app runs independently, and an app may cooperate with other apps to accomplish a task. In GNES, we have four fundamental apps, aka. microservices, they are:

  • Preprocessor: transforming a real-world object to a list of workable semantic units;
  • Encoder: representing a semantic unit with vector representation;
  • Indexer: storing the vectors into memory/disk that allows fast-access;
  • Router: forwarding messages between microservices: e.g. batching, mapping, reducing.

In GNES, we have implemented dozens of preprocessor, encoder, indexer to process different content forms, such as image, text, video. It is also super easy to plug in your own implementation, which we shall see an example in the sequel.


Okay, now that we have a bunch of apps, what are we expecting them to do? In a typical search system, there are two fundamental tasks: indexing and querying. Indexing is storing the documents, querying is searching the documents, pretty straightforward. In a neural search system, one may also face another task: training, where one fine-tunes an encoder/preprocessor according to the data distribution in order to achieve better search relevance. These three tasks: indexing, querying and training are what we call three runtimes in GNES.

💡 The key to understand GNES is to know which runtime requires what microservices, and each microservice does what.

Build your first GNES app on local machine

Let's start with a typical indexing procedure by writing a YAML config (see the left column of the table):

YAML configGNES workflow (generated by GNES board)
port: 5566
- name: Preprocessor
 yaml_path: text-prep.yml
- name: Encoder
 yaml_path: gpt2.yml
- name: Indexer
 yaml_path: b-indexer.yml
GNES workflow of example 1

Now let's see what the YAML config says. First impression, it is pretty intuitive. It defines a pipeline workflow consists of preprocessing, encoding and indexing, where the output of the former component is the input of the next. This pipeline is a typical workflow of index or query runtime. Under each component, we also associate it with a YAML config specifying how it should work. Right now they are not important for understanding the big picture, nonetheless curious readers can checkout how each YAML looks like by expanding the items below.

Preprocessor config: text-prep.yml (click to expand...)
  start_doc_id: 0
  random_doc_id: True
  deliminator: "[.!?]+"
  is_trained: true
Encoder config: gpt2.yml (click to expand...)
  - !GPT2Encoder
      model_dir: $GPT2_CI_MODEL
      pooling_stragy: REDUCE_MEAN
      is_trained: true
  - !PCALocalEncoder
      output_dim: 32
      num_locals: 8
      batch_size: 2048
  - !PQEncoder
      cluster_per_byte: 8
      num_bytes: 8
  work_dir: ./
  name: gpt2bin-pipe
Indexer config: b-indexer.yml (click to expand...)
  num_bytes: 8
  data_path: /out_data/idx.binary
  work_dir: ./
  name: bindexer

On the right side of the above table, you can see how the actual data flow looks like. There is an additional component gRPCFrontend automatically added to the workflow, it allows you to feed the data and fetch the result via gRPC protocol through port 5566.

Now it's time to run! GNES board can automatically generate a starting script/config based on the YAML config you give, saving troubles of writing them on your own.

GNES Board

💡 You can also start a GNES board locally. Simply run docker run -d -p gnes/gnes compose --flask

As a cloud-native application, GNES requires an orchestration engine to coordinate all micro-services. We support Kubernetes, Docker Swarm and shell-based multi-process. Let's see what the generated script looks like in this case.

Shell-based starting script (click to expand...)
#!/usr/bin/env bash
set -e

trap 'kill $(jobs -p)' EXIT

printf "starting service gRPCFrontend with 0 replicas...\n"
gnes frontend --grpc_port 5566 --port_out 49668 --socket_out PUSH_BIND --port_in 60654 --socket_in PULL_CONNECT  &
printf "starting service Preprocessor with 0 replicas...\n"
gnes preprocess --yaml_path text-prep.yml --port_in 49668 --socket_in PULL_CONNECT --port_out 61911 --socket_out PUSH_BIND  &
printf "starting service Encoder with 0 replicas...\n"
gnes encode --yaml_path gpt2.yml --port_in 61911 --socket_in PULL_CONNECT --port_out 49947 --socket_out PUSH_BIND  &
printf "starting service Indexer with 0 replicas...\n"
gnes index --yaml_path b-indexer.yml --port_in 49947 --socket_in PULL_CONNECT --port_out 60654 --socket_out PUSH_BIND  &

DockerSwarm compose file (click to expand...)
version: '3.4'
    image: gnes/gnes-full:latest
    command: frontend --grpc_port 5566 --port_out 49668 --socket_out PUSH_BIND --port_in
      60654 --socket_in PULL_CONNECT --host_in Indexer30
    - 5566:5566
    image: gnes/gnes-full:latest
    command: preprocess --port_in 49668 --socket_in PULL_CONNECT
      --port_out 61911 --socket_out PUSH_BIND --yaml_path /Preprocessor10_yaml --host_in
    - Preprocessor10_yaml
    image: gnes/gnes-full:latest
    command: encode --port_in 61911 --socket_in PULL_CONNECT
      --port_out 49947 --socket_out PUSH_BIND --yaml_path /Encoder20_yaml --host_in
    - Encoder20_yaml
    image: gnes/gnes-full:latest
    command: index --port_in 49947 --socket_in PULL_CONNECT
      --port_out 60654 --socket_out PUSH_BIND --yaml_path /Indexer30_yaml --host_in
    - Indexer30_yaml
volumes: {}
    driver: overlay
    attachable: true
    file: text-prep.yml
    file: gpt2.yml
    file: b-indexer.yml       

For the sake of simplicity, we will just use the generated shell-script to start GNES. Create a new file say, copy the content to it and run it via $ bash ./ You should see the output as follows:

success running GNES in shell

This suggests the GNES app is ready and waiting for the incoming data. You may now feed data to it through the gRPCFrontend. Depending on your language (Python, C, Java, Go, HTTP, Shell, etc.) and the content form (image, video, text, etc), the data feeding part can be slightly different.

To stop a running GNES, you can simply do control + c.

Scale your GNES app to the cloud

Now let's juice it up a bit. To be honest, building a single-machine process-based pipeline is not impressive anyway. The true power of GNES is that you can scale any component at any time you want. Encoding is slow? Adding more machines. Preprocessing takes too long? More machines. Index file is too large? Adding shards, aka. more machines!

In this example, we compose a more complicated GNES workflow for images. This workflow consists of multiple preprocessors, encoders and two types of indexers. In particular, we introduce two types of indexers: one for storing the encoded binary vectors, the other for storing the original images, i.e. full-text index. These two types of indexers work in parallel. Check out the YAML file on the left side of table for more details, note how replicas is defined for each component.

YAML configGNES workflow (generated by GNES board)
port: 5566
- name: Preprocessor
  replicas: 2
  yaml_path: image-prep.yml
- name: Encoder
  replicas: 3
  yaml_path: incep-v3.yml
- - name: Indexer
    yaml_path: faiss.yml
    replicas: 4
  - name: Indexer
    yaml_path: fulltext.yml
    replicas: 3
GNES workflow of example 2

You may realize that besides the gRPCFrontend, multiple Router have been added to the workflow. Routers serve as a message broker between microservices, determining how and where the message is received and sent. In the last pipeline example, the data flow is too simple so there is no need for adding any router. In this example routers are necessary for connecting multiple preprocessors and encoders, otherwise preprocessors wouldn't know where to send the message. GNES Board automatically adds router to the workflow when necessary based on the type of two consecutive layers. It may also add stacked routers, as you can see between encoder and indexer in the right graph.

Again, the detailed YAML config of each component is not important for understanding the big picture, hence we omit it for now.

This time we will run GNES via DockerSwarm. To do that simply copy the generated DockerSwarm YAML config to a file say my-gnes.yml, and then do

docker stack deploy --compose-file my-gnes.yml gnes-531

Note that gnes-531 is your GNES stack name, keep that name in mind. If you forget about that name, you can always use docker stack ls to find out. To tell whether the whole stack is running successfully or not, you can use docker service ls -f name=gnes-531. The number of replicas 1/1 or 4/4 suggests everything is fine.

Generally, a complete and successful Docker Swarm starting process should look like the following:

success running GNES in shell

When the GNES stack is ready and waiting for the incoming data, you may now feed data to it through the gRPCFrontend. Depending on your language (Python, C, Java, Go, HTTP, Shell, etc.) and the content form (image, video, text, etc), the data feeding part can be slightly different.

To stop a running GNES stack, you can use docker stack rm gnes-531.

Customize GNES to your need

With the help of GNES Board, you can easily compose a GNES app for different purposes. The table below summarizes some common compositions with the corresponding workflow visualizations. Note, we hide the component-wise YAML config (i.e. yaml_path) for the sake of clarity.

YAML configGNES workflow (generated by GNES board)
Parallel preprocessing only
port: 5566
- name: Preprocessor
  replicas: 2
GNES workflow of example 3
Training an encoder
port: 5566
- name: Preprocessor
  replicas: 3
- name: Encoder
GNES workflow of example 4
Index-time with 3 vector-index shards
port: 5566
- name: Preprocessor
- name: Encoder
- name: Indexer
  replicas: 3
GNES workflow of example 5
Query-time with 2 vector-index shards followed by 3 full-text-index shards
port: 5566
- name: Preprocessor
- name: Encoder
- name: Indexer
  income: sub
  replicas: 2
- name: Indexer
  income: sub
  replicas: 3
GNES workflow of example 5

Take-home messages

Now that you know how to compose and run a GNES app, let's make a short recap of what we have learned.

  • GNES is all-in-microservice, there are four fundamental components: preprocessor, encoder, indexer and router.
  • GNES has three runtimes: training, indexing, and querying. The key to compose a GNES app is to clarify which runtime requires what microservices (defined in the YAML config), and each microservice does what (defined in the component-wise YAML config).
  • GNES requires an orchestration engine to coordinate all microservices. It supports Kubernetes, Docker Swarm and a shell-based multi-process solution.
  • GNES Board is a convenient tool for visualizing the workflow, generating starting script or cloud configuration.
  • The real power of GNES is elasticity on every level. Router is automatically added between microservices for connecting the pieces together.

👨‍💻️ What's next?

The next step is feeding data to GNES for training, indexing and querying. Checkout the tutorials and documentations for more details.



The official documentation of GNES is hosted on It is automatically built, updated and archived on every new release.


🚧 Tutorial is still under construction. Stay tuned! Meanwhile, we sincerely welcome you to contribute your own learning experience / case study with GNES!


Thanks for your interest in contributing! GNES always welcome the contribution from the open-source community, individual committers and other partners. Without you, GNES can't be successful.

Currently there are three major directions of contribution:

  • Porting state-of-the-art models to GNES. This includes new preprocessing algorithms, new DNN networks for encoding, and new high-performance index. Believe me, it is super easy to wrap an algorithm and use it in GNES. Checkout this example.
  • Adding tutorial and learning experience. What is good and what can be improved? If you apply GNES in your domain, whether it's about NLP or CV, whether it's a blog post or a Reddit/Twitter thread, we are always eager to hear your thoughts.
  • Completing the user experience of other programming languages. GNES offers a generic interface with gRPC and protobuf, therefore it is easy to add an interface for other languages, e.g. Java, C, Go.

Make sure to read the contributor guidelines before your first commit.

For contributors looking to get deeper into the API we suggest cloning the repository and checking out the unit tests for examples of how to call methods.

Citing GNES

If you use GNES in an academic paper, you are more than welcome to make a citation. Here are the two ways of citing GNES:

  1. \footnote{}
  2. @misc{tencent2019GNES,
      title={GNES: Generic Neural Elastic Search},
      author={Xiao, Han and Yan, Jianfeng and Wang, Feng and Fu, Jie},


If you have downloaded a copy of the GNES binary or source code, please note that the GNES binary and source code are both licensed under the Apache License, Version 2.0.

Tencent is pleased to support the open source community by making GNES available.
Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for gnes, version 0.0.28
Filename, size File type Python version Upload date Hashes
Filename, size gnes-0.0.28-cp36-cp36m-macosx_10_13_x86_64.whl (259.7 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size gnes-0.0.28.tar.gz (129.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page