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

PDF
Python for Delphi Developers - Part 2
PDF
Interfacing C/C++ and Python with SWIG
PPTX
PyCourse - Self driving python course
PDF
PyPy London Demo Evening 2013
PPTX
Introduction to Jupyter notebook and MS Azure Machine Learning Studio
PPTX
Introduction to Jupyter notebook and MS Azure Machine Learning Studio
PDF
Cython compiler
PPTX
Fast Python: Master the Basics to Write Faster Code
Python for Delphi Developers - Part 2
Interfacing C/C++ and Python with SWIG
PyCourse - Self driving python course
PyPy London Demo Evening 2013
Introduction to Jupyter notebook and MS Azure Machine Learning Studio
Introduction to Jupyter notebook and MS Azure Machine Learning Studio
Cython compiler
Fast Python: Master the Basics to Write Faster Code

Similar to Development_C_Extension_with_Pybind11.pdf (20)

PPTX
PDF
Anaconda Python KNIME & Orange Installation
PDF
10 useful Python development setup tips to boost your productivity
PPTX
What is Python? An overview of Python for science.
PDF
The Popper Experimentation Protocol and CLI tool
PDF
Python_final_print_vison_academy_9822506209.pdf
PPTX
Reproducibility in artificial intelligence
PDF
PyCon2022 - Building Python Extensions
PDF
Introduction to Google Colaboratory.pdf
PPTX
Colab workshop (for Computer vision Students)
PPTX
Kubernetes 101
PDF
Python For Audio Signal Processing ( PDFDrive ).pdf
PDF
Python_final_print_batch_II_vision_academy.pdf
PDF
Python_final_print_batch_II_vision_academy.pdf
PDF
Python_vision_academy notes
PDF
Python_final_print_batch_II_vision_academy.pdf
PDF
Python_final_print_batch_II_vision_academy (1).pdf
PDF
Python 3.5: An agile, general-purpose development language.
PDF
python training in chandigarh
PDF
DevOps for TYPO3 Teams and Projects
Anaconda Python KNIME & Orange Installation
10 useful Python development setup tips to boost your productivity
What is Python? An overview of Python for science.
The Popper Experimentation Protocol and CLI tool
Python_final_print_vison_academy_9822506209.pdf
Reproducibility in artificial intelligence
PyCon2022 - Building Python Extensions
Introduction to Google Colaboratory.pdf
Colab workshop (for Computer vision Students)
Kubernetes 101
Python For Audio Signal Processing ( PDFDrive ).pdf
Python_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdf
Python_vision_academy notes
Python_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy (1).pdf
Python 3.5: An agile, general-purpose development language.
python training in chandigarh
DevOps for TYPO3 Teams and Projects
Ad

Recently uploaded (20)

PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
AI in Product Development-omnex systems
PPTX
ai tools demonstartion for schools and inter college
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPT
Introduction Database Management System for Course Database
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
top salesforce developer skills in 2025.pdf
PDF
System and Network Administraation Chapter 3
PPTX
history of c programming in notes for students .pptx
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
AI in Product Development-omnex systems
ai tools demonstartion for schools and inter college
ManageIQ - Sprint 268 Review - Slide Deck
CHAPTER 2 - PM Management and IT Context
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Odoo Companies in India – Driving Business Transformation.pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Odoo POS Development Services by CandidRoot Solutions
Introduction Database Management System for Course Database
VVF-Customer-Presentation2025-Ver1.9.pptx
How Creative Agencies Leverage Project Management Software.pdf
Design an Analysis of Algorithms II-SECS-1021-03
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
top salesforce developer skills in 2025.pdf
System and Network Administraation Chapter 3
history of c programming in notes for students .pptx
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Ad

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