IP Library › Granted Patent US 12,400,068
Granted Patent B2
US 12,400,068 · App. 17/587,984 · Granted Aug 26, 2025

Systems and methods for natural language code search

Inventors: Akhilesh Deepak Gotmare (Singapore, SG); Junnan Li (Singapore, SG); Chu Hong Hoi (Singapore, SG)
Assignee: Salesforce, Inc.
G06F40/151G06F40/30G06F40/40G06N3/04
View Patent ↗
Loading inventors, assignments & file history…
Monitor This Case
Get email alerts when status or documents change.
Order Certified Copies
Most orders are placed with the USPTO same day — all within 24 business hours.
Order via The Patent Place →
Pre-filled with this patent's details
Quick Facts
Patent No.
US 12,400,068
App. No.
17/587,984
Granted
Aug 26, 2025
Kind
B2
Abstract

Embodiments are directed to translating a natural language query into a code snippet in a programing language that semantically represents the query. The embodiments include a cascading neural network that includes an encoder network and a classifier network. The encoder network being faster but less accurate than the classifier network. The encoder network is trained using a contrastive learning framework to identify code candidates from a large set of code snippets. The classifier network is trained using a binary classifier to identify the code snippet that semantically represents the query from the code candidates.

Claims (61)

1. A method for translating a natural language query into a code snippet in a programming language, the method comprising:

generating, at an encoder network, a plurality of code snippet indices and a natural language query representation from a plurality of code snippets and the natural language query, respectively;

determining one or more code candidates based on distance metrics between the natural language query representations and the plurality of code snippet indices;

generating text-code pairs from the natural language query and the determined one or more code candidates based on the distance metrics, a text-code pair including the natural language query and a code candidate from the determined one or more code candidates;

jointly encoding, by a single encoder of a classifier network, an input sequence concatenating the natural language query and the code candidate from the text-code pair into one joint representation, wherein the single encoder and the encoder network share one or more encoder layers;

predicting, by a classification head of the classifier network, a matching probability that the natural language query and the code candidate are a match using an input of the one joint representation outputted from the single encoder; and

outputting the code candidate in the programming language in response to the natural language query based on the matching probability.

2. The method of claim 1 , further comprising:

training, the encoder network to determine the code candidates on a contrastive loss function.

3. The method of claim 1 , further comprising:

training, the classifier network to determine the code snippet from the pairs using a cross-entropy objective function.

4. The method of claim 1 , wherein the classifier network sequentially follows the encoder network, and the encoder network is an order of magnitude faster and an order of magnitude less accurate than the classifier network.

5. The method of claim 1 , wherein the encoder network is trained on a different loss function from the classifier network.

6. The method of claim 1 , wherein the encoder network shares a portion of a neural network structure with the classifier network.

7. The method of claim 1 , wherein generating the code candidates further comprises:

generating encodings from the natural language query; and

determining, using the code snippet index, the encodings of the code candidates that are within a distance determined by a distance function from the encodings of the natural language query.

8. The method of claim 1 , wherein determining the code snippet further comprises:

determining a confidence score that a code candidate of each pair is the semantic representation of the natural language query;

ranking confidence scores of the pairs; and

selecting a code candidate of a pair corresponding to a highest confidence score as the code snippet that is the semantic representation of the natural language query.

9. A system for translating a natural language query into a code snippet in a programming language, the system comprising:

a memory configured to store a cascading neural network;

a processor coupled to the memory and configured to execute instructions for causing the cascading neural network to:

generate, at an encoder network of the cascading neural network, a plurality of code snippet indices and a natural language query representation from a plurality of code snippets and the natural language query, respectively;

determine one or more code candidates based on distance metrics between the natural language query representation and the plurality of code snippet indices;

generate text-code pairs from the natural language query and the determined one or more code candidates based on the distance metrics, a text-code pair including the natural language query and a code candidate from the determined one or more code candidates;

jointly encode, by a single encoder of a classifier network, an input sequence concatenating the natural language query and the code candidate from the text-code pair into one joint representation, wherein the single encoder and the encoder network share one or more encoder layers;

predict, by a classification head of the classifier network, a matching probability that the natural language query and the code candidate are a match using an input of the one joint representation outputted from the single encoder; and

output the code candidate in the programming language in response to the natural language query, based on the matching probability.

10. The system of claim 9 , wherein the processor is further configured to:

train, the encoder network to determine the code candidates on a contrastive loss function; and

train, the classifier network to determine the code snippet from the pairs using a cross-entropy objective function.

11. The system of claim 9 , wherein the classifier network sequentially follows the encoder network, and the encoder network is an order of magnitude faster and an order of magnitude less accurate than the classifier network.

12. The system of claim 9 , wherein the encoder network shares a portion of a neural network structure with the classifier network.

13. The system of claim 9 , wherein to generate the code candidates the processor is further configured to:

generate encodings from the natural language query; and

determine, using the code snippet index, encodings of the code candidates that are within a distance determined by a distance function from the encodings of the natural language query.

14. The system of claim 9 , wherein to determine the code snippet the processor is further configured to:

determine a confidence score that a code candidate of each pair is a semantic representation of the natural language query;

rank confidence scores of the pairs; and

select a code candidate of a pair corresponding to a highest confidence score as the code snippet that is the semantic representation of the natural language query.

15. A non-transitory computer readable medium having instructions stored thereon, that when executed by a processor cause the processor to perform operations for translating a natural language query into a code snippet in a programming language, the operations comprising:

generating, at an encoder network, a plurality of code snippet indices and a natural language query representation from a plurality of code snippets and the natural language query, respectively;

determining one or more code candidates based on distance metrics between the natural language query representations and the plurality of code snippet indices;

generating text-code pairs from the natural language query and the determined one or more code candidates based on the distance metrics, a text-code pair including the natural language query and a code candidate from the determined one or more code candidates;

jointly encoding, by a single encoder of a classifier network, an input sequence concatenating the natural language query and the code candidate from the text-code pair into one joint representation, wherein the single encoder and the encoder network share one or more encoder layers;

predicting, by a classification head of the classifier network, a matching probability that the natural language query and the code candidate are a match using an input of the one joint representation outputted from the single encoder; and

outputting the code candidate in the programming language in response to the natural language query based on the matching probability.

16. The non-transitory computer readable medium of claim 15 , further comprising:

training, the encoder network to determine the code candidates on a contrastive loss function; and

training, the classifier network to determine the code snippet from the pairs using a cross-entropy objective function.

17. The non-transitory computer readable medium of claim 15 , wherein the classifier network sequentially follows the encoder network, and the encoder network is an order of magnitude faster and an order of magnitude less accurate than the classifier network.

18. The non-transitory computer readable medium of claim 15 , wherein the encoder network shares a portion of a neural network structure with the classifier network.

19. The non-transitory computer readable medium of claim 15 , wherein generating the code candidates further comprises:

generating encodings from the natural language query; and

determining, using the code snippet index, encodings of the code candidates that are within a distance determined by a distance function from the encodings of the natural language query.

20. The non-transitory computer readable medium of claim 15 , wherein determining the code snippet further comprises:

determining a confidence score that a code candidate of each pair is a semantic representation of the natural language query;

ranking confidence scores of the pairs; and

selecting a code candidate of a pair corresponding to a highest confidence score as the code snippet that is the semantic representation of the natural language query.

Assignments (3)
CHANGE OF NAME Recorded Aug 4, 2026
From: SALESFORCE.COM, INC.
To: SALESFORCE, INC.
Reel/Frame 076118/0548 →
CORRECTIVE ASSIGNMENT TO CORRECT THE RECEIVING PARTY ADDRESS FROM BLVD. TO STREET AS INDICATED IN THE EXECUTED ASSIGNMENT PREVIOUSLY RECORDED ON REEL 060660 FRAME 0121. ASSIGNOR(S) HEREBY CONFIRMS THE ASSIGNMENT. Recorded Apr 11, 2023
From: GOTMARE, AKHILESH DEEPAK; LI, JUNNAN; HOI, CHU HONG
To: SALESFORCE.COM, INC.
Reel/Frame 063557/0822 →
ASSIGNMENT OF ASSIGNOR'S INTEREST Recorded Jul 28, 2022
From: GOTMARE, AKHILESH DEEPAK; LI, JUNNAN; HOI, CHU HONG
To: SALESFORCE.COM, INC.
Reel/Frame 060660/0121 →
Continuity (2)
Provisional Application 63252393 · Oct 5, 2021
Related Publication 20230109681A1 · Apr 13, 2023
References Cited (16)
US 11132512B2 · Wu · 2021 [cited by examiner]
US 11734339B2 · Kale · 2023 [cited by examiner]
US 20120254143A1 · Varma · 2012 [cited by examiner]
US 20200117446A1 · Smith · 2020 [cited by examiner]
US 20200334520A1 · Chen · 2020 [cited by examiner]
US 20210191696A1 · Ibarra Von Borstel · 2021 [cited by examiner]
US 20210303989A1 · Bird · 2021 [cited by examiner]
US 20230042051A1 · Clement · 2023 [cited by examiner]
US 20230100376A1 · Liu · 2023 [cited by examiner]
CoSQA: 20,000+ Web Queries for Code Search and Question Answering, arXiv:2105.13239v1 (Year: 2021). [cited by examiner]
Lobato, “Neural networks with optimal accuracy and speed in their predictions,” Toward Data Science.com, Jul. 20, 2017 (available at: https://towardsdatascience.com/neural-networks-with-optimal-accuracy-and-speed-in-the… [cited by examiner]
Pengcheng Yin, Bowen Deng, Edgar Chen, Bogdan Vasilescu, Graham Neubig, “Learning to Mine Aligned Code and Natural Language Pairs from Stack Overflow”, 2018, IEEE, ACM/IEEE 15th International Conference on Mining Softwa… [cited by examiner]
International Search Report and Written Opinion for PCT/US2022/077458, dated Jan. 4, 2023, 11 pages. [cited by applicant]
Gotmare et al., “Cascaded Fast and Slow Models for Efficient Semantic Code Search”, arXiv.Org., Cornell University Library, 201 Olin Library Cornell University Ithaca, NY 14853, Oct. 15, 2021, XP091077176, pp. 1-12. [cited by applicant]
Guo et al., “GraphCodeBert: Pre-Training Code Representations with Data Flow”, Published as conference paper at ICLR 2021, Sep. 13, 2021, Retrieved from the Internet: URL:https://arxiv.org/pdf/2009.08366.pdf, pp. 1-18. [cited by applicant]
Salza et al., “On the Effectiveness of Transfer Learning for Code Search”, arXiv.Org., 201 Olin Library Cornell University Ithaca, NY 14853, Aug. 12, 2021, XP091032288, 16 pages. [cited by applicant]