SlideShare a Scribd company logo
Type4Py
Practical Deep Similarity Learning-Based Type Inference for
Python
Amir M. Mir (PhD Student, TU Delft)
Evaldas Latoskinas (Software Eng., Amazon)
Sebastian Proksch (Assistant Prof., TU Delft)
Georgios Gousios (Associate Prof., TU Delft)
44th International Conference on Software Engineering (ICSE 2022)
May 2022
Talk Outline
● Intro
○ Motivation and challenges
○ Related Work
● Proposed Approach (Type4Py)
○ Type hints & Vector embeddings
○ Neural Model
● Dataset
○ Data Augmentation and Type-checking
○ Pre-processing
● Evaluation
○ Research questions
○ Obtained results
● Type4Py in Practice
○ Deployment & Telemetry in VS Code
● Conclusion & Future Work
2
Motivation
3
Motivation
● PEP 484
○ Optional type annotations
● Adding types manually is laborious
○ Static type inference
○ ML-based type prediction
4
SOTA ML-based Type Inference
5
● Typilus (PLDI’20)
○ GNN-based model
● TypeWriter (FSE’20)
○ HNN-based model
Current Problems and Challenges
● Suggesting the correct type in the Top-1
○ Developers tend to use the first suggestion by a tool
● Developer-provided type annotations may not always be valid for training and
evaluation
6
Type4Py
● Based on Deep Similarity Learning and Hierarchical Neural Network
● A type-checked dataset with 5.1K Python projects and 1.2M type annotations.
● A Visual Studio Code extension, which provides ML-based type auto-completion for
Python.
7
Type4Py vs. other ML-based approaches
8
Proposed Approach
9
Overview of Type4Py
10
Type hints/features
● Natural Information
○ Identifiers: names of parameters, functions, and variables
● Code Context
○ Usage of parameters and variables inside a function
● Visible Type Hints (VTH)
○ Recursive extraction of import statements from a module and its transitive
dependencies
11
Type hints/features
12
Identifiers
Visible type hints
Code Context
Vector Embeddings
● Applying NLP pre-processing techniques
○ tokenization, stop words removal, lemmatization
● Training a Word2Vec model to generate token embeddings:
○ Identifiers & code context
13
file
name
path
Neural Model
● Hierarchical Neural Network
○ 2x Recurrent Neural Networks (LSTM cells)
○ Linear layer
14
Neural Model
● Triplet loss
○ Learning to discriminate between similar and dissimilar types
15
Neural Model
● Using KNN search to suggest types from learned Type Clusters
16
Model Training
17
● Used Dropout to avoid overfitting
● Adam optimizer to minimize Triplet loss
● PyTorch’s data parallelism
● A single epoch takes ~4 minutes
Dataset
18
Code de-duplication
● Used our CD4Py tool
○ TF-IDF to represent source code files a vector
○ KNN search to find clusters of similar duplicate files
○ Similarity is transitive
■ keep a file from each cluster and remove all other identified duplicate files
○ Removed ~400K duplicate files
19
Dataset Augmentation
● Pyre, static type inference tool
● Inferring type of variables
20
Type Checking
● Developer-provided types rarely type-check
● Performed basic type-checking procedure:
○ Used mypy to type-check 288K source files in the dataset. 64% of them
type-checked.
○ Ignored source code files with syntax errors, which amounts to 64K files
○ Given 40K files w/ type errors, remove one type annotation at a time from a file
■ If it type-checks, include the file.
■ Otherwise, we continue this step up to 10 times.
○ Fixed 16.8K files with type errors
21
Dataset Characteristics
● ~201K source code files (11.9M LoC)
○ ~1.24M type annotations in total
● Split dataset by files
○ 70% training, 10% validation, 20% test
22
Top-10 frequent types
23
Pre-processing
● Excluded trivial functions such as __str__ and __len__
● Excluded Any and None type annotations
● Resolved type aliases, e.g., [] to List.
● Resolved qualified names of type annotations, e.g., np.array
● Rewrote the components of a base type whose nested level is greater than 2
to Any.
○ E.g, List[List[Tuple[int]]] to List[List[Any]]]
24
Evaluation
25
Evaluation Metrics
26
● Exact match
○ List[str] and List[str] would be an exact match
● Base type match
○ List[str] and List[int] would be a match
Evaluation Metrics
27
● Mean Reciprocal Rank (MRR)
● Differentiating between types:
○ Ubiquitous: {str, int, list, bool, float}
○ Common: seen >= 100 times in the train set
○ Rare: seen <100 times in the train set
Research Questions
28
● RQ1: What is the general type prediction performance of Type4Py?
● RQ2: How does Type4Py perform while considering different predictions
tasks?
● RQ3: How do each proposed type hint and the size of type vocabulary
contribute to the performance of Type4Py?
Type Prediction Performance (RQ1)
29
● Considering Top-1 and exact match, Type4Py achieves 75.8% and
outperforms:
○ Typilus by 9.7%
○ TypeWriter by 19.7%
● Considering MRR, Type4Py obtains 77.1% and outperforms:
○ Typilus by 8.1%
○ TypeWriter by 16.7%
● Type4Py achieves 100% exact match at Top-1 for the ubiquitous types
Different prediction tasks (RQ2)
30
● Three prediction tasks
○ Arguments
○ Return
○ Variables
● Considering MRR score
○ Exact match
Arguments
● Type4Py obtains 64.2% vs.
○ 5.5% higher than Typilus
○ 1% higher than TypeWriter
Return
● Type4Py obtains 57.9% vs.
○ 11.9% higher than Typilus
○ 3.7% higher than TypeWriter
Variables
● Type4Py obtains 81.4% vs.
○ 7.7% higher than Typilus
Efficacy of type hints (RQ3)
31
● Code context has the most impact on the model’s performance
○ MRR score drops by 7.4%
● By ignoring Visible Type Hints:
○ MRR score for ubiquitous types drops from 100% to 86%.
● By Ignoring identifiers:
○ MRR score drops by 3.3%
● By limiting type vocabulary to 1,000 types:
○ MRR score of rare types drops by 7.2%
Type4Py in Practice
32
Deployment
33
Deployment
34
● Converting pre-trained PyTorch Model to ONNX Model
○ Faster inference
● A small Flask application
○ Predict endpoint (POST)
○ Handles concurrent type prediction requests
○ Nginx as a proxy
VSCode Extension
35
https://marketplace.visualstudio.com/items?itemName=saltud.type4py
ML-based Type Auto-completion
36
VSCode Telemetry
37
Telemetry Stats
38
Total no. of prediction requests 93,559
No. of prediction requests from VS Code 65,162
Approx. no. of all users (VS Code) 127
No. of accepted types: 119
No. of rejected types: 49
Avg. processing time of prediction requests 786 ms
No. of failed prediction requests 10,147
NOTE: Telemetry data is considered from Jul. ‘21 to Apr. ‘22
Concluding Remarks
39
● Using a type-checked dataset to train and evaluate ML-based type
prediction model
● Considering the MRR metric, Type4Py significantly outperforms Typilus and
TypeWriter
● Type Clusters enables a much larger type vocabulary
● Type4Py cannot synthesize types
● Our VSCode extension helps to improve developers’ productivity
Future Work
40
● Exploring pointer networks to suggest new types
● Studying how to improve Type4Py from its usage in VSCode and telemetry
data
● Complenting Type4Py with static type inference
Open Science!
41
https://arxiv.org/abs/2101.04470
https://github.com/saltudelft/type4py
Paper
Code
https://doi.org/10.5281/zenodo.4044635
Dataset
Contact Info
42
@amir_mir93
s.a.m.mir@tudelft.nl

More Related Content

Similar to The presentation of Type4Py at the ICSE'22 conference

Elixir Brasil 2019 - Quality: A Panacéia para seu código Elixir
Elixir Brasil 2019 - Quality:  A Panacéia para seu código ElixirElixir Brasil 2019 - Quality:  A Panacéia para seu código Elixir
Elixir Brasil 2019 - Quality: A Panacéia para seu código Elixir
Weverton Timoteo
 
Code Generation Cambridge 2013 Introduction to Parsing with ANTLR4
Code Generation Cambridge 2013  Introduction to Parsing with ANTLR4Code Generation Cambridge 2013  Introduction to Parsing with ANTLR4
Code Generation Cambridge 2013 Introduction to Parsing with ANTLR4
Oliver Zeigermann
 
Instant search - A hands-on tutorial
Instant search  - A hands-on tutorialInstant search  - A hands-on tutorial
Instant search - A hands-on tutorial
Ganesh Venkataraman
 
Data Structures Algorithm and Career Guidance
Data Structures Algorithm and Career GuidanceData Structures Algorithm and Career Guidance
Data Structures Algorithm and Career Guidance
SwapnilNarayan
 
Neo4j: Graph-like power
Neo4j: Graph-like powerNeo4j: Graph-like power
Neo4j: Graph-like power
Roman Rodomansky
 
Meet the-other-elephant
Meet the-other-elephantMeet the-other-elephant
Meet the-other-elephant
Stefanie Janine Stölting
 
Typescript: Beginner to Advanced
Typescript: Beginner to AdvancedTypescript: Beginner to Advanced
Typescript: Beginner to Advanced
Talentica Software
 
Java generics(Under The Hood Of The Compiler) by Harmeet singh
Java generics(Under The Hood Of The Compiler) by Harmeet singhJava generics(Under The Hood Of The Compiler) by Harmeet singh
Java generics(Under The Hood Of The Compiler) by Harmeet singh
Harmeet Singh(Taara)
 
Python with data Sciences
Python with data SciencesPython with data Sciences
Python with data Sciences
Krishna Mohan Mishra
 
DAA Slides for Multiple topics such as different algorithms
DAA Slides for Multiple topics such as different algorithmsDAA Slides for Multiple topics such as different algorithms
DAA Slides for Multiple topics such as different algorithms
DEVARSHHIRENBHAIPARM
 
Understanding Digits in Identifier Names: An Exploratory Study
Understanding Digits in Identifier Names: An Exploratory StudyUnderstanding Digits in Identifier Names: An Exploratory Study
Understanding Digits in Identifier Names: An Exploratory Study
University of Hawai‘i at Mānoa
 
Type hints in python & mypy
Type hints in python & mypyType hints in python & mypy
Type hints in python & mypy
Anirudh
 
Blueprints: Introduction to Python programming
Blueprints: Introduction to Python programmingBlueprints: Introduction to Python programming
Blueprints: Introduction to Python programming
Bhalaji Nagarajan
 
Level-based Resume Classification on Nursing Positions
Level-based Resume Classification on Nursing PositionsLevel-based Resume Classification on Nursing Positions
Level-based Resume Classification on Nursing Positions
Jinho Choi
 
2._Java_Syntax_and_Data_Type.pptx.pdf
2._Java_Syntax_and_Data_Type.pptx.pdf2._Java_Syntax_and_Data_Type.pptx.pdf
2._Java_Syntax_and_Data_Type.pptx.pdf
luxasuhi
 
Machine learning Investigative Reporting NorthBaySolutions.pdf
Machine learning Investigative Reporting NorthBaySolutions.pdfMachine learning Investigative Reporting NorthBaySolutions.pdf
Machine learning Investigative Reporting NorthBaySolutions.pdf
ssusera5352a2
 
Translation of expression in compiler design
Translation of expression in compiler designTranslation of expression in compiler design
Translation of expression in compiler design
logeshrajaram1
 
Text categorization
Text categorizationText categorization
Text categorization
Shubham Pahune
 
Python Mastery: A Comprehensive Guide to Setting Up Your Development Environment
Python Mastery: A Comprehensive Guide to Setting Up Your Development EnvironmentPython Mastery: A Comprehensive Guide to Setting Up Your Development Environment
Python Mastery: A Comprehensive Guide to Setting Up Your Development Environment
Python Devloper
 
A Type-level Ruby Interpreter for Testing and Understanding
A Type-level Ruby Interpreter for Testing and UnderstandingA Type-level Ruby Interpreter for Testing and Understanding
A Type-level Ruby Interpreter for Testing and Understanding
mametter
 

Similar to The presentation of Type4Py at the ICSE'22 conference (20)

Elixir Brasil 2019 - Quality: A Panacéia para seu código Elixir
Elixir Brasil 2019 - Quality:  A Panacéia para seu código ElixirElixir Brasil 2019 - Quality:  A Panacéia para seu código Elixir
Elixir Brasil 2019 - Quality: A Panacéia para seu código Elixir
 
Code Generation Cambridge 2013 Introduction to Parsing with ANTLR4
Code Generation Cambridge 2013  Introduction to Parsing with ANTLR4Code Generation Cambridge 2013  Introduction to Parsing with ANTLR4
Code Generation Cambridge 2013 Introduction to Parsing with ANTLR4
 
Instant search - A hands-on tutorial
Instant search  - A hands-on tutorialInstant search  - A hands-on tutorial
Instant search - A hands-on tutorial
 
Data Structures Algorithm and Career Guidance
Data Structures Algorithm and Career GuidanceData Structures Algorithm and Career Guidance
Data Structures Algorithm and Career Guidance
 
Neo4j: Graph-like power
Neo4j: Graph-like powerNeo4j: Graph-like power
Neo4j: Graph-like power
 
Meet the-other-elephant
Meet the-other-elephantMeet the-other-elephant
Meet the-other-elephant
 
Typescript: Beginner to Advanced
Typescript: Beginner to AdvancedTypescript: Beginner to Advanced
Typescript: Beginner to Advanced
 
Java generics(Under The Hood Of The Compiler) by Harmeet singh
Java generics(Under The Hood Of The Compiler) by Harmeet singhJava generics(Under The Hood Of The Compiler) by Harmeet singh
Java generics(Under The Hood Of The Compiler) by Harmeet singh
 
Python with data Sciences
Python with data SciencesPython with data Sciences
Python with data Sciences
 
DAA Slides for Multiple topics such as different algorithms
DAA Slides for Multiple topics such as different algorithmsDAA Slides for Multiple topics such as different algorithms
DAA Slides for Multiple topics such as different algorithms
 
Understanding Digits in Identifier Names: An Exploratory Study
Understanding Digits in Identifier Names: An Exploratory StudyUnderstanding Digits in Identifier Names: An Exploratory Study
Understanding Digits in Identifier Names: An Exploratory Study
 
Type hints in python & mypy
Type hints in python & mypyType hints in python & mypy
Type hints in python & mypy
 
Blueprints: Introduction to Python programming
Blueprints: Introduction to Python programmingBlueprints: Introduction to Python programming
Blueprints: Introduction to Python programming
 
Level-based Resume Classification on Nursing Positions
Level-based Resume Classification on Nursing PositionsLevel-based Resume Classification on Nursing Positions
Level-based Resume Classification on Nursing Positions
 
2._Java_Syntax_and_Data_Type.pptx.pdf
2._Java_Syntax_and_Data_Type.pptx.pdf2._Java_Syntax_and_Data_Type.pptx.pdf
2._Java_Syntax_and_Data_Type.pptx.pdf
 
Machine learning Investigative Reporting NorthBaySolutions.pdf
Machine learning Investigative Reporting NorthBaySolutions.pdfMachine learning Investigative Reporting NorthBaySolutions.pdf
Machine learning Investigative Reporting NorthBaySolutions.pdf
 
Translation of expression in compiler design
Translation of expression in compiler designTranslation of expression in compiler design
Translation of expression in compiler design
 
Text categorization
Text categorizationText categorization
Text categorization
 
Python Mastery: A Comprehensive Guide to Setting Up Your Development Environment
Python Mastery: A Comprehensive Guide to Setting Up Your Development EnvironmentPython Mastery: A Comprehensive Guide to Setting Up Your Development Environment
Python Mastery: A Comprehensive Guide to Setting Up Your Development Environment
 
A Type-level Ruby Interpreter for Testing and Understanding
A Type-level Ruby Interpreter for Testing and UnderstandingA Type-level Ruby Interpreter for Testing and Understanding
A Type-level Ruby Interpreter for Testing and Understanding
 

Recently uploaded

Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
TIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptxTIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptx
CVCSOfficial
 
P5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civilP5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civil
AnasAhmadNoor
 
Height and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdfHeight and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdf
q30122000
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
Accident detection system project report.pdf
Accident detection system project report.pdfAccident detection system project report.pdf
Accident detection system project report.pdf
Kamal Acharya
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
upoux
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
SENTIMENT ANALYSIS ON PPT AND Project template_.pptx
SENTIMENT ANALYSIS ON PPT AND Project template_.pptxSENTIMENT ANALYSIS ON PPT AND Project template_.pptx
SENTIMENT ANALYSIS ON PPT AND Project template_.pptx
b0754201
 
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENTNATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
Addu25809
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
21UME003TUSHARDEB
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
Atif Razi
 
2. protection of river banks and bed erosion protection works.ppt
2. protection of river banks and bed erosion protection works.ppt2. protection of river banks and bed erosion protection works.ppt
2. protection of river banks and bed erosion protection works.ppt
abdatawakjira
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
Divyanshu
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
Roger Rozario
 
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Transcat
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
MadhavJungKarki
 
OOPS_Lab_Manual - programs using C++ programming language
OOPS_Lab_Manual - programs using C++ programming languageOOPS_Lab_Manual - programs using C++ programming language
OOPS_Lab_Manual - programs using C++ programming language
PreethaV16
 
Zener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and ApplicationsZener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and Applications
Shiny Christobel
 

Recently uploaded (20)

Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
TIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptxTIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptx
 
P5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civilP5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civil
 
Height and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdfHeight and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdf
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
Accident detection system project report.pdf
Accident detection system project report.pdfAccident detection system project report.pdf
Accident detection system project report.pdf
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
SENTIMENT ANALYSIS ON PPT AND Project template_.pptx
SENTIMENT ANALYSIS ON PPT AND Project template_.pptxSENTIMENT ANALYSIS ON PPT AND Project template_.pptx
SENTIMENT ANALYSIS ON PPT AND Project template_.pptx
 
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENTNATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
 
2. protection of river banks and bed erosion protection works.ppt
2. protection of river banks and bed erosion protection works.ppt2. protection of river banks and bed erosion protection works.ppt
2. protection of river banks and bed erosion protection works.ppt
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
 
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
Tools & Techniques for Commissioning and Maintaining PV Systems W-Animations ...
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
 
OOPS_Lab_Manual - programs using C++ programming language
OOPS_Lab_Manual - programs using C++ programming languageOOPS_Lab_Manual - programs using C++ programming language
OOPS_Lab_Manual - programs using C++ programming language
 
Zener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and ApplicationsZener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and Applications
 

The presentation of Type4Py at the ICSE'22 conference

  • 1. Type4Py Practical Deep Similarity Learning-Based Type Inference for Python Amir M. Mir (PhD Student, TU Delft) Evaldas Latoskinas (Software Eng., Amazon) Sebastian Proksch (Assistant Prof., TU Delft) Georgios Gousios (Associate Prof., TU Delft) 44th International Conference on Software Engineering (ICSE 2022) May 2022
  • 2. Talk Outline ● Intro ○ Motivation and challenges ○ Related Work ● Proposed Approach (Type4Py) ○ Type hints & Vector embeddings ○ Neural Model ● Dataset ○ Data Augmentation and Type-checking ○ Pre-processing ● Evaluation ○ Research questions ○ Obtained results ● Type4Py in Practice ○ Deployment & Telemetry in VS Code ● Conclusion & Future Work 2
  • 4. Motivation ● PEP 484 ○ Optional type annotations ● Adding types manually is laborious ○ Static type inference ○ ML-based type prediction 4
  • 5. SOTA ML-based Type Inference 5 ● Typilus (PLDI’20) ○ GNN-based model ● TypeWriter (FSE’20) ○ HNN-based model
  • 6. Current Problems and Challenges ● Suggesting the correct type in the Top-1 ○ Developers tend to use the first suggestion by a tool ● Developer-provided type annotations may not always be valid for training and evaluation 6
  • 7. Type4Py ● Based on Deep Similarity Learning and Hierarchical Neural Network ● A type-checked dataset with 5.1K Python projects and 1.2M type annotations. ● A Visual Studio Code extension, which provides ML-based type auto-completion for Python. 7
  • 8. Type4Py vs. other ML-based approaches 8
  • 11. Type hints/features ● Natural Information ○ Identifiers: names of parameters, functions, and variables ● Code Context ○ Usage of parameters and variables inside a function ● Visible Type Hints (VTH) ○ Recursive extraction of import statements from a module and its transitive dependencies 11
  • 13. Vector Embeddings ● Applying NLP pre-processing techniques ○ tokenization, stop words removal, lemmatization ● Training a Word2Vec model to generate token embeddings: ○ Identifiers & code context 13 file name path
  • 14. Neural Model ● Hierarchical Neural Network ○ 2x Recurrent Neural Networks (LSTM cells) ○ Linear layer 14
  • 15. Neural Model ● Triplet loss ○ Learning to discriminate between similar and dissimilar types 15
  • 16. Neural Model ● Using KNN search to suggest types from learned Type Clusters 16
  • 17. Model Training 17 ● Used Dropout to avoid overfitting ● Adam optimizer to minimize Triplet loss ● PyTorch’s data parallelism ● A single epoch takes ~4 minutes
  • 19. Code de-duplication ● Used our CD4Py tool ○ TF-IDF to represent source code files a vector ○ KNN search to find clusters of similar duplicate files ○ Similarity is transitive ■ keep a file from each cluster and remove all other identified duplicate files ○ Removed ~400K duplicate files 19
  • 20. Dataset Augmentation ● Pyre, static type inference tool ● Inferring type of variables 20
  • 21. Type Checking ● Developer-provided types rarely type-check ● Performed basic type-checking procedure: ○ Used mypy to type-check 288K source files in the dataset. 64% of them type-checked. ○ Ignored source code files with syntax errors, which amounts to 64K files ○ Given 40K files w/ type errors, remove one type annotation at a time from a file ■ If it type-checks, include the file. ■ Otherwise, we continue this step up to 10 times. ○ Fixed 16.8K files with type errors 21
  • 22. Dataset Characteristics ● ~201K source code files (11.9M LoC) ○ ~1.24M type annotations in total ● Split dataset by files ○ 70% training, 10% validation, 20% test 22
  • 24. Pre-processing ● Excluded trivial functions such as __str__ and __len__ ● Excluded Any and None type annotations ● Resolved type aliases, e.g., [] to List. ● Resolved qualified names of type annotations, e.g., np.array ● Rewrote the components of a base type whose nested level is greater than 2 to Any. ○ E.g, List[List[Tuple[int]]] to List[List[Any]]] 24
  • 26. Evaluation Metrics 26 ● Exact match ○ List[str] and List[str] would be an exact match ● Base type match ○ List[str] and List[int] would be a match
  • 27. Evaluation Metrics 27 ● Mean Reciprocal Rank (MRR) ● Differentiating between types: ○ Ubiquitous: {str, int, list, bool, float} ○ Common: seen >= 100 times in the train set ○ Rare: seen <100 times in the train set
  • 28. Research Questions 28 ● RQ1: What is the general type prediction performance of Type4Py? ● RQ2: How does Type4Py perform while considering different predictions tasks? ● RQ3: How do each proposed type hint and the size of type vocabulary contribute to the performance of Type4Py?
  • 29. Type Prediction Performance (RQ1) 29 ● Considering Top-1 and exact match, Type4Py achieves 75.8% and outperforms: ○ Typilus by 9.7% ○ TypeWriter by 19.7% ● Considering MRR, Type4Py obtains 77.1% and outperforms: ○ Typilus by 8.1% ○ TypeWriter by 16.7% ● Type4Py achieves 100% exact match at Top-1 for the ubiquitous types
  • 30. Different prediction tasks (RQ2) 30 ● Three prediction tasks ○ Arguments ○ Return ○ Variables ● Considering MRR score ○ Exact match Arguments ● Type4Py obtains 64.2% vs. ○ 5.5% higher than Typilus ○ 1% higher than TypeWriter Return ● Type4Py obtains 57.9% vs. ○ 11.9% higher than Typilus ○ 3.7% higher than TypeWriter Variables ● Type4Py obtains 81.4% vs. ○ 7.7% higher than Typilus
  • 31. Efficacy of type hints (RQ3) 31 ● Code context has the most impact on the model’s performance ○ MRR score drops by 7.4% ● By ignoring Visible Type Hints: ○ MRR score for ubiquitous types drops from 100% to 86%. ● By Ignoring identifiers: ○ MRR score drops by 3.3% ● By limiting type vocabulary to 1,000 types: ○ MRR score of rare types drops by 7.2%
  • 34. Deployment 34 ● Converting pre-trained PyTorch Model to ONNX Model ○ Faster inference ● A small Flask application ○ Predict endpoint (POST) ○ Handles concurrent type prediction requests ○ Nginx as a proxy
  • 38. Telemetry Stats 38 Total no. of prediction requests 93,559 No. of prediction requests from VS Code 65,162 Approx. no. of all users (VS Code) 127 No. of accepted types: 119 No. of rejected types: 49 Avg. processing time of prediction requests 786 ms No. of failed prediction requests 10,147 NOTE: Telemetry data is considered from Jul. ‘21 to Apr. ‘22
  • 39. Concluding Remarks 39 ● Using a type-checked dataset to train and evaluate ML-based type prediction model ● Considering the MRR metric, Type4Py significantly outperforms Typilus and TypeWriter ● Type Clusters enables a much larger type vocabulary ● Type4Py cannot synthesize types ● Our VSCode extension helps to improve developers’ productivity
  • 40. Future Work 40 ● Exploring pointer networks to suggest new types ● Studying how to improve Type4Py from its usage in VSCode and telemetry data ● Complenting Type4Py with static type inference