SlideShare a Scribd company logo
Development C-extension with using PyBind11
PyConJP 2022
Takayuki Suzuki
Shiseido, co, ltd,.
Table of contents
1. Introduction
2. What is Pybind11?
3. Pybind11 usage and integration with peripheral libraries
4. Test & Memory Check
5. DevOps Case Studies
6. Summary
1
Intro
Self Introduction
• Python Experience:about 10 years
• Working at Shiseido Co.
• Research and development of evaluation methods for skin images
2
Awareness of the problem
Organizations have cultures, and programming languages accompany cultures. Especially when
efficiency is required, there are historical assets, or for reasons of member skills. There are
cases where development in C/C++ is necessary.
On the other hand, we want to use Python because of its properties as a glue language and the
abundance of libraries.
What to do?
In this hour, I will introduce the libraries I have used to link Python and C and give examples of
how to test them!
• Source code is available on Github
• https://github.com/SzTk/pyconjp2022_dev_with_pybind11
3
How Python and the C language work together
How to call C from Python?
The official document says...
• How to include and build Python.h on the C language side (C extension)
• How to import ctypes on the Python side and call the library
• CFFI
Currently, CFFI seems to be the best choice(?).
PyBind11 basically provides functionality similar to CFFI → shared this time
4
What is Pybind11?
What is Pybind11?
Pybind11 is a library that helps Python extend C
Apparently derived from Boost.Python.
The machine learning library dlib also
uses it.
5
Quick Start
Examples of usage
C language side
https://pybind11.readthedocs.io/en/stable/basics.html
setup.py
6
Quick Start
Examples of usage
C language, so different data types will cause an error.
7
Pybind11 usage and integration
with peripheral libraries
Numby data type
Modules can also be created that use Numpy as input/output.
C language side
• Function that takes two np.arrays and
returns an element-wise summed np.array
• The returned numpy type data cannot be
created in standard C (of course), so it is
created by calling Python’s API.
PyBind11 provides a wrapper in
pybind11/numpy.h.
• There are multiple ways to access numpy
data. Here, we use the method of
accessing without range checks, etc.,
giving priority to efficiency.
8
Numby data type
Examples of usage
Naturally, if a type other than the specified type is received, an error (or unexpected behavior)
will occur.
9
Eigen
Eigen is a Header Only linear algebra library available in C++
Fast enough to be comparable to GOTO Blas
0
2000
4000
6000
8000
10000
12000
14000
16000
18000
20000
10 100 1000
MFLOPS
matrix size
matrix matrix product
’eigen3’
’GOTO2’
’INTEL_MKL’
’eigen2’
’ATLAS’
’gmm’
’ublas’
http://download.tuxfamily.org/eigen/btl-results-110323/axpy.pdf
It seems that the matrix-matrix multiplication is no different from GOTO2/INTEL MKL
10
Eigen is also available
C language side
setup.py
• Function that performs multiplication of
two matrices using Eigen and returns the
result
• In setup.py, specify the folder where you
downloaded eigen for include dirs
• The returned data is allocated memory
space on the C language side (unlike the
numpy example).
• The method of memory management
(whether to manage in C or to pass
management to Python) can be specified
by specifying return value policy.
11
Eigen Execution Example
12
Eigen Notes
• Efficient execution of matrix operations (not only
Eigen) requires attention to the order in which data
is stored in memory.
• Numpy arrays are C CONTIGUOUSS (=row-major)
by default
• Eigen’s default is column-major
• We need to either explicitly specify that Eigen also
receives RowMajor (which is the method employed
in the example above) or specify F CONTIGUOUSS
in numpy
13
Eigen Notes
In the above example, an error will occur if the array is passed after setting F CONTIGUOUSS
(because Raw Major = C CONTIGUOUSS in the argument type)
14
Linking to libraries
If the C language side depends on an external library (.dll for Windows, .so for Linux, etc.), the
dependent library should be listed in setup.py.
This example is linked with opencv in /usr/local/lib
If you distribute packages, you need to figure out how to distribute libraries as well
15
Test & Memory Check
Test
About the Test
When developing extensions in C or C++, Python’s GC does not manage memory areas
allocated in C modules.
It is necessary to check for memory leaks in the C language part.
I will share my method of checking.
16
GTEST
GTEST can be used for testing at the C language level.
https://github.com/google/googletest
GTEST is a library for testing the C++ language developed by Google
Note that testing at the Python level is done using nosetests.
17
GTEST Usage Example
My practice of GTEST
• Separate IF and C language parts with Python
• Test the C language part with GTEST
• Since it is C language, it is compiled (using CMake, etc.) to generate an executable binary
for testing.
• Python part tested with nosetests
18
GTEST Usage Example
Consider testing the matmul function used in eigen
Cut out only the matmul function to a separate file for testing
This has an intentional memory leak in it.
19
GTEST Usage Example
Write test code for GTEST
20
GTEST Usage Example 例
CMakeFile.txt
21
GTEST Usage Example
Build and Run
Execution Result
22
GTEST Usage Exampl
The Python IF part can be done as follows
• Write matmul is defined externally
• Add the file in which matmul is defined to setup.py
23
memory check
In addition to testing, memory leaks must also be checked.
• Valgrind is an OSS development tool for memory debugging, memory leak detection,
profiling, etc.
• This time, the test is done by passing the GTEST run command to valgrind.
24
memory check
• For memory leaks, just look at
LEAK SUMMARY.
• The leak summary points out
that 40 bytes of memory have
been forgotten to be deleted.
• should be reduced to zero,
including POSSIBLY
25
DevOps Case Studies
DevOps Pipeline
In our case, the skin analysis program is available for internal use.
To automate the process up to this point
In my case, I am using Azure DevOps to create a pipeline
After access management using Azure Active Directory It is useful to be able to publish
packages for internal use
26
Summary
Summary
Summary
• Depending on organizational culture and efficiency requirements, a mixture of C and
Python may be used for development
• Introduced how to use Pybind11 to link Python and C languages
• Practical methods of testing the C language portion of the program were introduced.
• Introduced a case study of DevOps use in our company.
Even today, there are still cultural areas where the C language is still alive.
I believe that working with other languages is also a cross-cultural exchange.
Why don’t you all try to touch the C extension of Python and have a cross-cultural exchange.
27
Questions?
27

More Related Content

Similar to Development_C_Extension_with_Pybind11.pdf

Python
PythonPython
Anaconda Python KNIME & Orange Installation
Anaconda Python KNIME & Orange InstallationAnaconda Python KNIME & Orange Installation
Anaconda Python KNIME & Orange Installation
Girinath Pillai
 
What is Python? An overview of Python for science.
What is Python? An overview of Python for science.What is Python? An overview of Python for science.
What is Python? An overview of Python for science.
Nicholas Pringle
 
The Popper Experimentation Protocol and CLI tool
The Popper Experimentation Protocol and CLI toolThe Popper Experimentation Protocol and CLI tool
The Popper Experimentation Protocol and CLI tool
Ivo Jimenez
 
Python_final_print_vison_academy_9822506209.pdf
Python_final_print_vison_academy_9822506209.pdfPython_final_print_vison_academy_9822506209.pdf
Python_final_print_vison_academy_9822506209.pdf
VisionAcademyProfSac
 
Reproducibility in artificial intelligence
Reproducibility in artificial intelligenceReproducibility in artificial intelligence
Reproducibility in artificial intelligence
Carlos Toxtli
 
PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python Extensions
Henry Schreiner
 
Introduction to Google Colaboratory.pdf
Introduction to Google Colaboratory.pdfIntroduction to Google Colaboratory.pdf
Introduction to Google Colaboratory.pdf
Yomna Mahmoud Ibrahim Hassan
 
Colab workshop (for Computer vision Students)
Colab workshop (for Computer vision Students)Colab workshop (for Computer vision Students)
Colab workshop (for Computer vision Students)
Asim Hameed Khan
 
Kubernetes 101
Kubernetes 101Kubernetes 101
Kubernetes 101
Stanislav Pogrebnyak
 
Python For Audio Signal Processing ( PDFDrive ).pdf
Python For Audio Signal Processing ( PDFDrive ).pdfPython For Audio Signal Processing ( PDFDrive ).pdf
Python For Audio Signal Processing ( PDFDrive ).pdf
shaikriyaz89
 
Python_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfPython_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdf
bhagyashri686896
 
Python_vision_academy notes
Python_vision_academy notes Python_vision_academy notes
Python_vision_academy notes
rajaniraut
 
Python_final_print_batch_II_vision_academy (1).pdf
Python_final_print_batch_II_vision_academy (1).pdfPython_final_print_batch_II_vision_academy (1).pdf
Python_final_print_batch_II_vision_academy (1).pdf
rupaliakhute
 
Python_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfPython_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdf
sannykhopade
 
Python 3.5: An agile, general-purpose development language.
Python 3.5: An agile, general-purpose development language.Python 3.5: An agile, general-purpose development language.
Python 3.5: An agile, general-purpose development language.
Carlos Miguel Ferreira
 
DevOps for TYPO3 Teams and Projects
DevOps for TYPO3 Teams and ProjectsDevOps for TYPO3 Teams and Projects
DevOps for TYPO3 Teams and Projects
Fedir RYKHTIK
 
The genesis of clusterlib - An open source library to tame your favourite sup...
The genesis of clusterlib - An open source library to tame your favourite sup...The genesis of clusterlib - An open source library to tame your favourite sup...
The genesis of clusterlib - An open source library to tame your favourite sup...
Arnaud Joly
 
Hands-on Lab: Red Hat Container Development & OpenShift
Hands-on Lab: Red Hat Container Development & OpenShiftHands-on Lab: Red Hat Container Development & OpenShift
Hands-on Lab: Red Hat Container Development & OpenShift
Amazon Web Services
 
PHP Mega Meetup, Sep, 2020, Anti patterns in php
PHP Mega Meetup, Sep, 2020, Anti patterns in phpPHP Mega Meetup, Sep, 2020, Anti patterns in php
PHP Mega Meetup, Sep, 2020, Anti patterns in php
Ahmed Abdou
 

Similar to Development_C_Extension_with_Pybind11.pdf (20)

Python
PythonPython
Python
 
Anaconda Python KNIME & Orange Installation
Anaconda Python KNIME & Orange InstallationAnaconda Python KNIME & Orange Installation
Anaconda Python KNIME & Orange Installation
 
What is Python? An overview of Python for science.
What is Python? An overview of Python for science.What is Python? An overview of Python for science.
What is Python? An overview of Python for science.
 
The Popper Experimentation Protocol and CLI tool
The Popper Experimentation Protocol and CLI toolThe Popper Experimentation Protocol and CLI tool
The Popper Experimentation Protocol and CLI tool
 
Python_final_print_vison_academy_9822506209.pdf
Python_final_print_vison_academy_9822506209.pdfPython_final_print_vison_academy_9822506209.pdf
Python_final_print_vison_academy_9822506209.pdf
 
Reproducibility in artificial intelligence
Reproducibility in artificial intelligenceReproducibility in artificial intelligence
Reproducibility in artificial intelligence
 
PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python Extensions
 
Introduction to Google Colaboratory.pdf
Introduction to Google Colaboratory.pdfIntroduction to Google Colaboratory.pdf
Introduction to Google Colaboratory.pdf
 
Colab workshop (for Computer vision Students)
Colab workshop (for Computer vision Students)Colab workshop (for Computer vision Students)
Colab workshop (for Computer vision Students)
 
Kubernetes 101
Kubernetes 101Kubernetes 101
Kubernetes 101
 
Python For Audio Signal Processing ( PDFDrive ).pdf
Python For Audio Signal Processing ( PDFDrive ).pdfPython For Audio Signal Processing ( PDFDrive ).pdf
Python For Audio Signal Processing ( PDFDrive ).pdf
 
Python_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfPython_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdf
 
Python_vision_academy notes
Python_vision_academy notes Python_vision_academy notes
Python_vision_academy notes
 
Python_final_print_batch_II_vision_academy (1).pdf
Python_final_print_batch_II_vision_academy (1).pdfPython_final_print_batch_II_vision_academy (1).pdf
Python_final_print_batch_II_vision_academy (1).pdf
 
Python_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfPython_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdf
 
Python 3.5: An agile, general-purpose development language.
Python 3.5: An agile, general-purpose development language.Python 3.5: An agile, general-purpose development language.
Python 3.5: An agile, general-purpose development language.
 
DevOps for TYPO3 Teams and Projects
DevOps for TYPO3 Teams and ProjectsDevOps for TYPO3 Teams and Projects
DevOps for TYPO3 Teams and Projects
 
The genesis of clusterlib - An open source library to tame your favourite sup...
The genesis of clusterlib - An open source library to tame your favourite sup...The genesis of clusterlib - An open source library to tame your favourite sup...
The genesis of clusterlib - An open source library to tame your favourite sup...
 
Hands-on Lab: Red Hat Container Development & OpenShift
Hands-on Lab: Red Hat Container Development & OpenShiftHands-on Lab: Red Hat Container Development & OpenShift
Hands-on Lab: Red Hat Container Development & OpenShift
 
PHP Mega Meetup, Sep, 2020, Anti patterns in php
PHP Mega Meetup, Sep, 2020, Anti patterns in phpPHP Mega Meetup, Sep, 2020, Anti patterns in php
PHP Mega Meetup, Sep, 2020, Anti patterns in php
 

Recently uploaded

8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
TaghreedAltamimi
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
VALiNTRY360
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
SOCRadar
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
Yara Milbes
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 

Recently uploaded (20)

8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 

Development_C_Extension_with_Pybind11.pdf

  • 1. Development C-extension with using PyBind11 PyConJP 2022 Takayuki Suzuki Shiseido, co, ltd,.
  • 2. Table of contents 1. Introduction 2. What is Pybind11? 3. Pybind11 usage and integration with peripheral libraries 4. Test & Memory Check 5. DevOps Case Studies 6. Summary 1
  • 4. Self Introduction • Python Experience:about 10 years • Working at Shiseido Co. • Research and development of evaluation methods for skin images 2
  • 5. Awareness of the problem Organizations have cultures, and programming languages accompany cultures. Especially when efficiency is required, there are historical assets, or for reasons of member skills. There are cases where development in C/C++ is necessary. On the other hand, we want to use Python because of its properties as a glue language and the abundance of libraries. What to do? In this hour, I will introduce the libraries I have used to link Python and C and give examples of how to test them! • Source code is available on Github • https://github.com/SzTk/pyconjp2022_dev_with_pybind11 3
  • 6. How Python and the C language work together How to call C from Python? The official document says... • How to include and build Python.h on the C language side (C extension) • How to import ctypes on the Python side and call the library • CFFI Currently, CFFI seems to be the best choice(?). PyBind11 basically provides functionality similar to CFFI → shared this time 4
  • 8. What is Pybind11? Pybind11 is a library that helps Python extend C Apparently derived from Boost.Python. The machine learning library dlib also uses it. 5
  • 9. Quick Start Examples of usage C language side https://pybind11.readthedocs.io/en/stable/basics.html setup.py 6
  • 10. Quick Start Examples of usage C language, so different data types will cause an error. 7
  • 11. Pybind11 usage and integration with peripheral libraries
  • 12. Numby data type Modules can also be created that use Numpy as input/output. C language side • Function that takes two np.arrays and returns an element-wise summed np.array • The returned numpy type data cannot be created in standard C (of course), so it is created by calling Python’s API. PyBind11 provides a wrapper in pybind11/numpy.h. • There are multiple ways to access numpy data. Here, we use the method of accessing without range checks, etc., giving priority to efficiency. 8
  • 13. Numby data type Examples of usage Naturally, if a type other than the specified type is received, an error (or unexpected behavior) will occur. 9
  • 14. Eigen Eigen is a Header Only linear algebra library available in C++ Fast enough to be comparable to GOTO Blas 0 2000 4000 6000 8000 10000 12000 14000 16000 18000 20000 10 100 1000 MFLOPS matrix size matrix matrix product ’eigen3’ ’GOTO2’ ’INTEL_MKL’ ’eigen2’ ’ATLAS’ ’gmm’ ’ublas’ http://download.tuxfamily.org/eigen/btl-results-110323/axpy.pdf It seems that the matrix-matrix multiplication is no different from GOTO2/INTEL MKL 10
  • 15. Eigen is also available C language side setup.py • Function that performs multiplication of two matrices using Eigen and returns the result • In setup.py, specify the folder where you downloaded eigen for include dirs • The returned data is allocated memory space on the C language side (unlike the numpy example). • The method of memory management (whether to manage in C or to pass management to Python) can be specified by specifying return value policy. 11
  • 17. Eigen Notes • Efficient execution of matrix operations (not only Eigen) requires attention to the order in which data is stored in memory. • Numpy arrays are C CONTIGUOUSS (=row-major) by default • Eigen’s default is column-major • We need to either explicitly specify that Eigen also receives RowMajor (which is the method employed in the example above) or specify F CONTIGUOUSS in numpy 13
  • 18. Eigen Notes In the above example, an error will occur if the array is passed after setting F CONTIGUOUSS (because Raw Major = C CONTIGUOUSS in the argument type) 14
  • 19. Linking to libraries If the C language side depends on an external library (.dll for Windows, .so for Linux, etc.), the dependent library should be listed in setup.py. This example is linked with opencv in /usr/local/lib If you distribute packages, you need to figure out how to distribute libraries as well 15
  • 20. Test & Memory Check
  • 21. Test About the Test When developing extensions in C or C++, Python’s GC does not manage memory areas allocated in C modules. It is necessary to check for memory leaks in the C language part. I will share my method of checking. 16
  • 22. GTEST GTEST can be used for testing at the C language level. https://github.com/google/googletest GTEST is a library for testing the C++ language developed by Google Note that testing at the Python level is done using nosetests. 17
  • 23. GTEST Usage Example My practice of GTEST • Separate IF and C language parts with Python • Test the C language part with GTEST • Since it is C language, it is compiled (using CMake, etc.) to generate an executable binary for testing. • Python part tested with nosetests 18
  • 24. GTEST Usage Example Consider testing the matmul function used in eigen Cut out only the matmul function to a separate file for testing This has an intentional memory leak in it. 19
  • 25. GTEST Usage Example Write test code for GTEST 20
  • 26. GTEST Usage Example 例 CMakeFile.txt 21
  • 27. GTEST Usage Example Build and Run Execution Result 22
  • 28. GTEST Usage Exampl The Python IF part can be done as follows • Write matmul is defined externally • Add the file in which matmul is defined to setup.py 23
  • 29. memory check In addition to testing, memory leaks must also be checked. • Valgrind is an OSS development tool for memory debugging, memory leak detection, profiling, etc. • This time, the test is done by passing the GTEST run command to valgrind. 24
  • 30. memory check • For memory leaks, just look at LEAK SUMMARY. • The leak summary points out that 40 bytes of memory have been forgotten to be deleted. • should be reduced to zero, including POSSIBLY 25
  • 32. DevOps Pipeline In our case, the skin analysis program is available for internal use. To automate the process up to this point In my case, I am using Azure DevOps to create a pipeline After access management using Azure Active Directory It is useful to be able to publish packages for internal use 26
  • 34. Summary Summary • Depending on organizational culture and efficiency requirements, a mixture of C and Python may be used for development • Introduced how to use Pybind11 to link Python and C languages • Practical methods of testing the C language portion of the program were introduced. • Introduced a case study of DevOps use in our company. Even today, there are still cultural areas where the C language is still alive. I believe that working with other languages is also a cross-cultural exchange. Why don’t you all try to touch the C extension of Python and have a cross-cultural exchange. 27